1# Debugger (Beta)
2
3Zed uses the Debug Adapter Protocol (DAP) to provide debugging functionality across multiple programming languages.
4DAP is a standardized protocol that defines how debuggers, editors, and IDEs communicate with each other.
5It allows Zed to support various debuggers without needing to implement language-specific debugging logic.
6This protocol enables features like setting breakpoints, stepping through code, inspecting variables,
7and more, in a consistent manner across different programming languages and runtime environments.
8
9> We currently offer onboarding support for users. We are eager to hear from you if you encounter any issues or have suggestions for improvement for our debugging experience.
10> You can schedule a call via [Cal.com](https://cal.com/team/zed-research/debugger)
11
12## Supported Debug Adapters
13
14Zed supports a variety of debug adapters for different programming languages out of the box:
15
16- JavaScript ([vscode-js-debug](https://github.com/microsoft/vscode-js-debug.git)): Enables debugging of Node.js applications, including setting breakpoints, stepping through code, and inspecting variables in JavaScript.
17
18- Python ([debugpy](https://github.com/microsoft/debugpy.git)): Provides debugging capabilities for Python applications, supporting features like remote debugging, multi-threaded debugging, and Django/Flask application debugging.
19
20- LLDB ([CodeLLDB](https://github.com/vadimcn/codelldb.git)): A powerful debugger for C, C++, Objective-C, and Swift, offering low-level debugging features and support for Apple platforms.
21
22- GDB ([GDB](https://sourceware.org/gdb/)): The GNU Debugger, which supports debugging for multiple programming languages including C, C++, Go, and Rust, across various platforms.
23
24- Go ([Delve](https://github.com/go-delve/delve)): Delve, a debugger for the Go programming language, offering both local and remote debugging capabilities with full support for Go's runtime and standard library.
25
26- PHP ([Xdebug](https://xdebug.org/)): Provides debugging and profiling capabilities for PHP applications, including remote debugging and code coverage analysis.
27
28- Ruby ([rdbg](https://github.com/ruby/debug)): Provides debugging for Ruby.
29
30These adapters enable Zed to provide a consistent debugging experience across multiple languages while leveraging the specific features and capabilities of each debugger.
31
32## Getting Started
33
34For basic debugging, you can set up a new configuration by opening the `New Session Modal` either via the `debugger: start` (default: f4) or by clicking the plus icon at the top right of the debug panel.
35
36For more advanced use cases, you can create debug configurations by directly editing the `.zed/debug.json` file in your project root directory.
37
38You can then use the `New Session Modal` to select a configuration and start debugging.
39
40### Launching & Attaching
41
42Zed debugger offers two ways to debug your program; you can either _launch_ a new instance of your program or _attach_ to an existing process.
43Which one you choose depends on what you are trying to achieve.
44
45When launching a new instance, Zed (and the underlying debug adapter) can often do a better job at picking up the debug information compared to attaching to an existing process, since it controls the lifetime of a whole program. Running unit tests or a debug build of your application is a good use case for launching.
46
47Compared to launching, attaching to an existing process might seem inferior, but that's far from truth; there are cases where you cannot afford to restart your program, because e.g. the bug is not reproducible outside of a production environment or some other circumstances.
48
49## Configuration
50
51While configuration fields are debug adapter-dependent, most adapters support the following fields:
52
53```json
54[
55 {
56 // The label for the debug configuration and used to identify the debug session inside the debug panel & new session modal
57 "label": "Example Start debugger config",
58 // The debug adapter that Zed should use to debug the program
59 "adapter": "Example adapter name",
60 // Request:
61 // - launch: Zed will launch the program if specified or shows a debug terminal with the right configuration
62 // - attach: Zed will attach to a running program to debug it or when the process_id is not specified we will show a process picker (only supported for node currently)
63 "request": "launch",
64 // program: The program that you want to debug
65 // This field supports path resolution with ~ or . symbols
66 "program": "path_to_program",
67 // cwd: defaults to the current working directory of your project ($ZED_WORKTREE_ROOT)
68 "cwd": "$ZED_WORKTREE_ROOT"
69 }
70]
71```
72
73All configuration fields support task variables. See [Tasks Variables](./tasks.md#variables)
74
75### Build tasks
76
77Zed also allows embedding a Zed task in a `build` field that is run before the debugger starts. This is useful for setting up the environment or running any necessary setup steps before the debugger starts.
78
79```json
80[
81 {
82 "label": "Build Binary",
83 "adapter": "CodeLLDB",
84 "program": "path_to_program",
85 "request": "launch",
86 "build": {
87 "command": "make",
88 "args": ["build", "-j8"]
89 }
90 }
91]
92```
93
94Build tasks can also refer to the existing tasks by unsubstituted label:
95
96```json
97[
98 {
99 "label": "Build Binary",
100 "adapter": "CodeLLDB",
101 "program": "path_to_program",
102 "request": "launch",
103 "build": "my build task" // Or "my build task for $ZED_FILE"
104 }
105]
106```
107
108### Automatic scenario creation
109
110Given a Zed task, Zed can automatically create a scenario for you. Automatic scenario creation also powers our scenario creation from gutter.
111Automatic scenario creation is currently supported for Rust, Go and Python. Javascript/TypeScript support being worked on.
112
113### Example Configurations
114
115#### JavaScript
116
117##### Debug Active File
118
119```json
120[
121 {
122 "label": "Debug with node",
123 "adapter": "JavaScript",
124 "program": "$ZED_FILE",
125 "request": "launch",
126 "console": "integratedTerminal",
127 "type": "pwa-node"
128 }
129]
130```
131
132##### Attach debugger to a server running in web browser (`npx serve`)
133
134Given an externally-ran web server (e.g. with `npx serve` or `npx live-server`) one can attach to it and open it with a browser.
135
136```json
137[
138 {
139 "label": "Inspect ",
140 "adapter": "JavaScript",
141 "type": "pwa-chrome",
142 "request": "launch",
143 "url": "http://localhost:5500", // Fill your URL here.
144 "program": "$ZED_FILE",
145 "webRoot": "${ZED_WORKTREE_ROOT}"
146 }
147]
148```
149
150#### Python
151
152##### Debug Active File
153
154```json
155[
156 {
157 "label": "Python Active File",
158 "adapter": "Debugpy",
159 "program": "$ZED_FILE",
160 "request": "launch"
161 }
162]
163```
164
165##### Flask App
166
167For a common Flask Application with a file structure similar to the following:
168
169```
170.venv/
171app/
172 init.py
173 main.py
174 routes.py
175templates/
176 index.html
177static/
178 style.css
179requirements.txt
180```
181
182the following configuration can be used:
183
184```json
185[
186 {
187 "label": "Python: Flask",
188 "adapter": "Debugpy",
189 "request": "launch",
190 "module": "app",
191 "cwd": "$ZED_WORKTREE_ROOT",
192 "env": {
193 "FLASK_APP": "app",
194 "FLASK_DEBUG": "1"
195 },
196 "args": [
197 "run",
198 "--reload", // Enables Flask reloader that watches for file changes
199 "--debugger" // Enables Flask debugger
200 ],
201 "autoReload": {
202 "enable": true
203 },
204 "jinja": true,
205 "justMyCode": true
206 }
207]
208```
209
210#### Rust/C++/C
211
212##### Using pre-built binary
213
214```json
215[
216 {
217 "label": "Debug native binary",
218 "program": "$ZED_WORKTREE_ROOT/build/binary",
219 "request": "launch",
220 "adapter": "CodeLLDB" // GDB is available on non arm macs as well as linux
221 }
222]
223```
224
225##### Build binary then debug
226
227```json
228[
229 {
230 "label": "Build & Debug native binary",
231 "build": {
232 "command": "cargo",
233 "args": ["build"]
234 },
235 "program": "$ZED_WORKTREE_ROOT/target/debug/binary",
236 "request": "launch",
237 "adapter": "CodeLLDB" // GDB is available on non arm macs as well as linux
238 }
239]
240```
241
242#### TypeScript
243
244##### Attach debugger to a server running in web browser (`npx serve`)
245
246Given an externally-ran web server (e.g. with `npx serve` or `npx live-server`) one can attach to it and open it with a browser.
247
248```json
249[
250 {
251 "label": "Launch Chromee (TypeScript)",
252 "adapter": "JavaScript",
253 "type": "pwa-chrome",
254 "request": "launch",
255 "url": "http://localhost:5500",
256 "program": "$ZED_FILE",
257 "webRoot": "${ZED_WORKTREE_ROOT}",
258 "sourceMaps": true,
259 "build": {
260 "command": "npx",
261 "args": ["tsc"]
262 }
263 }
264]
265```
266
267#### Go
268
269Zed uses [delve](https://github.com/go-delve/delve?tab=readme-ov-file) to debug Go applications. Zed will automatically create debug scenarios for `func main` in your main packages, and also
270for any tests, so you can use the Play button in the gutter to debug these without configuration.
271
272##### Debug Go Packages
273
274To debug a specific package, you can do so by setting the Delve mode to "debug". In this case "program" should be set to the package name.
275
276```json
277[
278 {
279 "label": "Go (Delve)",
280 "adapter": "Delve",
281 "program": "$ZED_FILE",
282 "request": "launch",
283 "mode": "debug"
284 }
285]
286```
287
288```json
289[
290 {
291 "label": "Run server",
292 "adapter": "Delve",
293 "request": "launch",
294 "mode": "debug",
295 // For Delve, the program can be a package name
296 "program": "./cmd/server"
297 // "args": [],
298 // "buildFlags": [],
299 }
300]
301```
302
303##### Debug Go Tests
304
305To debug the tests for a package, set the Delve mode to "test". The "program" is still the package name, and you can use the "buildFlags" to do things like set tags, and the "args" to set args on the test binary. (See `go help testflags` for more information on doing that).
306
307```json
308[
309 {
310 "label": "Run integration tests",
311 "adapter": "Delve",
312 "request": "launch",
313 "mode": "test",
314 "program": ".",
315 "buildFlags": ["-tags", "integration"]
316 // To filter down to just the test your cursor is in:
317 // "args": ["-test.run", "$ZED_SYMBOL"]
318 }
319]
320```
321
322##### Build and debug separately
323
324If you need to build your application with a specific command, you can use the "exec" mode of Delve. In this case "program" should point to an executable,
325and the "build" command should build that.
326
327```json
328{
329 "label": "Debug Prebuilt Unit Tests",
330 "adapter": "Delve",
331 "request": "launch",
332 "mode": "exec",
333 "program": "${ZED_WORKTREE_ROOT}/__debug_unit",
334 "args": ["-test.v", "-test.run=${ZED_SYMBOL}"],
335 "build": {
336 "command": "go",
337 "args": [
338 "test",
339 "-c",
340 "-tags",
341 "unit",
342 "-gcflags\"all=-N -l\"",
343 "-o",
344 "__debug_unit",
345 "./pkg/..."
346 ]
347 }
348}
349```
350
351##### Attaching to an existing instance of Delve
352
353You might find yourself needing to connect to an existing instance of Delve that's not necessarily running on your machine; in such case, you can use `tcp_arguments` to instrument Zed's connection to Delve.
354
355```
356{
357 "adapter": "Delve",
358 "label": "Connect to a running Delve instance",
359 "program": "/Users/zed/Projects/language_repositories/golang/hello/hello",
360 "cwd": "/Users/zed/Projects/language_repositories/golang/hello",
361 "args": [],
362 "env": {},
363 "request": "launch",
364 "mode": "exec",
365 "stopOnEntry": false,
366 "tcp_connection": { "host": "123.456.789.012", "port": 53412 }
367}
368```
369
370In such case Zed won't spawn a new instance of Delve, as it opts to use an existing one. The consequence of this is that _there will be no terminal_ in Zed; you have to interact with the Delve instance directly, as it handles stdin/stdout of the debuggee.
371
372#### Ruby
373
374To run a ruby task in the debugger, you will need to configure it in the `.zed/debug.json` file in your project. We don't yet have automatic detection of ruby tasks, nor do we support connecting to an existing process.
375
376The configuration should look like this:
377
378```json
379{
380 {
381 "adapter": "Ruby",
382 "label": "Run CLI",
383 "script": "cli.rb"
384 // If you want to customize how the script is run (for example using bundle exec)
385 // use "command" instead.
386 // "command": "bundle exec cli.rb"
387 //
388 // "args": []
389 // "env": {}
390 // "cwd": ""
391 }
392}
393```
394
395## Breakpoints
396
397To set a breakpoint, simply click next to the line number in the editor gutter.
398Breakpoints can be tweaked depending on your needs; to access additional options of a given breakpoint, right-click on the breakpoint icon in the gutter and select the desired option.
399At present, you can:
400
401- Add a log to a breakpoint, which will output a log message whenever that breakpoint is hit.
402- Make the breakpoint conditional, which will only stop at the breakpoint when the condition is met. The syntax for conditions is adapter-specific.
403- Add a hit count to a breakpoint, which will only stop at the breakpoint after it's hit a certain number of times.
404- Disable a breakpoint, which will prevent it from being hit while leaving it visible in the gutter.
405
406Some debug adapters (e.g. CodeLLDB and JavaScript) will also _verify_ whether your breakpoints can be hit; breakpoints that cannot be hit are surfaced more prominently in the UI.
407
408All breakpoints enabled for a given project are also listed in "Breakpoints" item in your debugging session UI. From "Breakpoints" item in your UI you can also manage exception breakpoints.
409The debug adapter will then stop whenever an exception of a given kind occurs. Which exception types are supported depends on the debug adapter.
410
411## Settings
412
413- `dock`: Determines the position of the debug panel in the UI.
414- `stepping_granularity`: Determines the stepping granularity.
415- `save_breakpoints`: Whether the breakpoints should be reused across Zed sessions.
416- `button`: Whether to show the debug button in the status bar.
417- `timeout`: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
418- `log_dap_communications`: Whether to log messages between active debug adapters and Zed.
419- `format_dap_log_messages`: Whether to format DAP messages when adding them to the debug adapter logger.
420
421### Dock
422
423- Description: The position of the debug panel in the UI.
424- Default: `bottom`
425- Setting: debugger.dock
426
427**Options**
428
4291. `left` - The debug panel will be docked to the left side of the UI.
4302. `right` - The debug panel will be docked to the right side of the UI.
4313. `bottom` - The debug panel will be docked to the bottom of the UI.
432
433```json
434"debugger": {
435 "dock": "bottom"
436},
437```
438
439### Stepping granularity
440
441- Description: The Step granularity that the debugger will use
442- Default: line
443- Setting: debugger.stepping_granularity
444
445**Options**
446
4471. Statement - The step should allow the program to run until the current statement has finished executing.
448 The meaning of a statement is determined by the adapter and it may be considered equivalent to a line.
449 For example 'for(int i = 0; i < 10; i++)' could be considered to have 3 statements 'int i = 0', 'i < 10', and 'i++'.
450
451```json
452{
453 "debugger": {
454 "stepping_granularity": "statement"
455 }
456}
457```
458
4592. Line - The step should allow the program to run until the current source line has executed.
460
461```json
462{
463 "debugger": {
464 "stepping_granularity": "line"
465 }
466}
467```
468
4693. Instruction - The step should allow one instruction to execute (e.g. one x86 instruction).
470
471```json
472{
473 "debugger": {
474 "stepping_granularity": "instruction"
475 }
476}
477```
478
479### Save Breakpoints
480
481- Description: Whether the breakpoints should be saved across Zed sessions.
482- Default: true
483- Setting: debugger.save_breakpoints
484
485**Options**
486
487`boolean` values
488
489```json
490{
491 "debugger": {
492 "save_breakpoints": true
493 }
494}
495```
496
497### Button
498
499- Description: Whether the button should be displayed in the debugger toolbar.
500- Default: true
501- Setting: debugger.show_button
502
503**Options**
504
505`boolean` values
506
507```json
508{
509 "debugger": {
510 "show_button": true
511 }
512}
513```
514
515### Timeout
516
517- Description: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
518- Default: 2000
519- Setting: debugger.timeout
520
521**Options**
522
523`integer` values
524
525```json
526{
527 "debugger": {
528 "timeout": 3000
529 }
530}
531```
532
533### Log Dap Communications
534
535- Description: Whether to log messages between active debug adapters and Zed. (Used for DAP development)
536- Default: false
537- Setting: debugger.log_dap_communications
538
539**Options**
540
541`boolean` values
542
543```json
544{
545 "debugger": {
546 "log_dap_communications": true
547 }
548}
549```
550
551### Format Dap Log Messages
552
553- Description: Whether to format DAP messages when adding them to the debug adapter logger. (Used for DAP development)
554- Default: false
555- Setting: debugger.format_dap_log_messages
556
557**Options**
558
559`boolean` values
560
561```json
562{
563 "debugger": {
564 "format_dap_log_messages": true
565 }
566}
567```
568
569## Theme
570
571The Debugger supports the following theme options:
572
573**debugger.accent**: Color used to accent breakpoint & breakpoint-related symbols
574**editor.debugger_active_line.background**: Background color of active debug line