debugger.md

  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> Is your desired debugger not listed? You can install a [Debug Adapter extension](https://zed.dev/extensions?filter=debug-adapters) to add support for your favorite debugger.
 33> If that's not enough, you can contribute by creating an extension yourself. Check out our [debugger extensions](extensions/debugger-extensions.md) documentation for more information.
 34
 35## Getting Started
 36
 37For 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.
 38
 39For more advanced use cases, you can create debug configurations by directly editing the `.zed/debug.json` file in your project root directory.
 40
 41You can then use the `New Session Modal` to select a configuration and start debugging.
 42
 43### Launching & Attaching
 44
 45Zed debugger offers two ways to debug your program; you can either _launch_ a new instance of your program or _attach_ to an existing process.
 46Which one you choose depends on what you are trying to achieve.
 47
 48When 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.
 49
 50Compared 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.
 51
 52## Configuration
 53
 54While configuration fields are debug adapter-dependent, most adapters support the following fields:
 55
 56```json
 57[
 58  {
 59    // The label for the debug configuration and used to identify the debug session inside the debug panel & new session modal
 60    "label": "Example Start debugger config",
 61    // The debug adapter that Zed should use to debug the program
 62    "adapter": "Example adapter name",
 63    // Request:
 64    //  - launch: Zed will launch the program if specified or shows a debug terminal with the right configuration
 65    //  - 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)
 66    "request": "launch",
 67    // program: The program that you want to debug
 68    // This field supports path resolution with ~ or . symbols
 69    "program": "path_to_program",
 70    // cwd: defaults to the current working directory of your project ($ZED_WORKTREE_ROOT)
 71    "cwd": "$ZED_WORKTREE_ROOT"
 72  }
 73]
 74```
 75
 76All configuration fields support task variables. See [Tasks Variables](./tasks.md#variables)
 77
 78### Build tasks
 79
 80Zed 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.
 81
 82```json
 83[
 84  {
 85    "label": "Build Binary",
 86    "adapter": "CodeLLDB",
 87    "program": "path_to_program",
 88    "request": "launch",
 89    "build": {
 90      "command": "make",
 91      "args": ["build", "-j8"]
 92    }
 93  }
 94]
 95```
 96
 97Build tasks can also refer to the existing tasks by unsubstituted label:
 98
 99```json
100[
101  {
102    "label": "Build Binary",
103    "adapter": "CodeLLDB",
104    "program": "path_to_program",
105    "request": "launch",
106    "build": "my build task" // Or "my build task for $ZED_FILE"
107  }
108]
109```
110
111### Automatic scenario creation
112
113Given a Zed task, Zed can automatically create a scenario for you. Automatic scenario creation also powers our scenario creation from gutter.
114Automatic scenario creation is currently supported for Rust, Go and Python. Javascript/TypeScript support being worked on.
115
116### Example Configurations
117
118#### Go
119
120```json
121[
122  {
123    "label": "Go (Delve)",
124    "adapter": "Delve",
125    "program": "$ZED_FILE",
126    "request": "launch",
127    "mode": "debug"
128  }
129]
130```
131
132#### JavaScript
133
134##### Debug Active File
135
136```json
137[
138  {
139    "label": "Debug with node",
140    "adapter": "JavaScript",
141    "program": "$ZED_FILE",
142    "request": "launch",
143    "console": "integratedTerminal",
144    "type": "pwa-node"
145  }
146]
147```
148
149##### Attach debugger to a server running in web browser (`npx serve`)
150
151Given 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.
152
153```json
154[
155  {
156    "label": "Inspect ",
157    "adapter": "JavaScript",
158    "type": "pwa-chrome",
159    "request": "launch",
160    "url": "http://localhost:5500", // Fill your URL here.
161    "program": "$ZED_FILE",
162    "webRoot": "${ZED_WORKTREE_ROOT}"
163  }
164]
165```
166
167#### Python
168
169##### Debug Active File
170
171```json
172[
173  {
174    "label": "Python Active File",
175    "adapter": "Debugpy",
176    "program": "$ZED_FILE",
177    "request": "launch"
178  }
179]
180```
181
182##### Flask App
183
184For a common Flask Application with a file structure similar to the following:
185
186```
187.venv/
188app/
189  init.py
190  main.py
191  routes.py
192templates/
193  index.html
194static/
195  style.css
196requirements.txt
197```
198
199the following configuration can be used:
200
201```json
202[
203  {
204    "label": "Python: Flask",
205    "adapter": "Debugpy",
206    "request": "launch",
207    "module": "app",
208    "cwd": "$ZED_WORKTREE_ROOT",
209    "env": {
210      "FLASK_APP": "app",
211      "FLASK_DEBUG": "1"
212    },
213    "args": [
214      "run",
215      "--reload", // Enables Flask reloader that watches for file changes
216      "--debugger" // Enables Flask debugger
217    ],
218    "autoReload": {
219      "enable": true
220    },
221    "jinja": true,
222    "justMyCode": true
223  }
224]
225```
226
227#### Rust/C++/C
228
229##### Using pre-built binary
230
231```json
232[
233  {
234    "label": "Debug native binary",
235    "program": "$ZED_WORKTREE_ROOT/build/binary",
236    "request": "launch",
237    "adapter": "CodeLLDB" // GDB is available on non arm macs as well as linux
238  }
239]
240```
241
242##### Build binary then debug
243
244```json
245[
246  {
247    "label": "Build & Debug native binary",
248    "build": {
249      "command": "cargo",
250      "args": ["build"]
251    },
252    "program": "$ZED_WORKTREE_ROOT/target/debug/binary",
253    "request": "launch",
254    "adapter": "CodeLLDB" // GDB is available on non arm macs as well as linux
255  }
256]
257```
258
259#### TypeScript
260
261##### Attach debugger to a server running in web browser (`npx serve`)
262
263Given 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.
264
265```json
266[
267  {
268    "label": "Launch Chromee (TypeScript)",
269    "adapter": "JavaScript",
270    "type": "pwa-chrome",
271    "request": "launch",
272    "url": "http://localhost:5500",
273    "program": "$ZED_FILE",
274    "webRoot": "${ZED_WORKTREE_ROOT}",
275    "sourceMaps": true,
276    "build": {
277      "command": "npx",
278      "args": ["tsc"]
279    }
280  }
281]
282```
283
284#### Go
285
286Zed 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
287for any tests, so you can use the Play button in the gutter to debug these without configuration.
288
289##### Debug Go Packages
290
291To 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.
292
293```json
294[
295  {
296    "label": "Run server",
297    "adapter": "Delve",
298    "request": "launch",
299    "mode": "debug",
300    // For Delve, the program is the package name
301    "program": "./cmd/server"
302    // "args": [],
303    // "buildFlags": [],
304  }
305]
306```
307
308##### Debug Go Tests
309
310To 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).
311
312```json
313[
314  {
315    "label": "Run integration tests",
316    "adapter": "Delve",
317    "request": "launch",
318    "mode": "test",
319    "program": ".",
320    "buildFlags": ["-tags", "integration"]
321    // To filter down to just the test your cursor is in:
322    // "args": ["-test.run", "$ZED_SYMBOL"]
323  }
324]
325```
326
327##### Build and debug separately
328
329If 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,
330and the "build" command should build that.
331
332```json
333{
334  "label": "Debug Prebuilt Unit Tests",
335  "adapter": "Delve",
336  "request": "launch",
337  "mode": "exec",
338  "program": "${ZED_WORKTREE_ROOT}/__debug_unit",
339  "args": ["-test.v", "-test.run=${ZED_SYMBOL}"],
340  "build": {
341    "command": "go",
342    "args": [
343      "test",
344      "-c",
345      "-tags",
346      "unit",
347      "-gcflags\"all=-N -l\"",
348      "-o",
349      "__debug_unit",
350      "./pkg/..."
351    ]
352  }
353}
354```
355
356##### Attaching to an existing instance of Delve
357
358You 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.
359
360````
361{
362  "adapter": "Delve",
363  "label": "Connect to a running Delve instance",
364  "program": "/Users/zed/Projects/language_repositories/golang/hello/hello",
365  "cwd": "/Users/zed/Projects/language_repositories/golang/hello",
366  "args": [],
367  "env": {},
368  "request": "launch",
369  "mode": "exec",
370  "stopOnEntry": false,
371  "tcp_connection": { "host": "123.456.789.012", "port": 53412 }
372}
373```
374
375In 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.
376
377### Ruby
378
379To 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.
380
381The configuration should look like this:
382
383```json
384{
385  {
386    "adapter": "Ruby",
387    "label": "Run CLI",
388    "script": "cli.rb"
389    // If you want to customize how the script is run (for example using bundle exec)
390    // use "command" instead.
391    // "command": "bundle exec cli.rb"
392    //
393    // "args": []
394    // "env": {}
395    // "cwd": ""
396  }
397}
398````
399
400## Breakpoints
401
402To set a breakpoint, simply click next to the line number in the editor gutter.
403Breakpoints 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.
404At present, you can:
405
406- Add a log to a breakpoint, which will output a log message whenever that breakpoint is hit.
407- Make the breakpoint conditional, which will only stop at the breakpoint when the condition is met. The syntax for conditions is adapter-specific.
408- Add a hit count to a breakpoint, which will only stop at the breakpoint after it's hit a certain number of times.
409- Disable a breakpoint, which will prevent it from being hit while leaving it visible in the gutter.
410
411Some 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.
412
413All 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.
414The debug adapter will then stop whenever an exception of a given kind occurs. Which exception types are supported depends on the debug adapter.
415
416## Settings
417
418- `dock`: Determines the position of the debug panel in the UI.
419- `stepping_granularity`: Determines the stepping granularity.
420- `save_breakpoints`: Whether the breakpoints should be reused across Zed sessions.
421- `button`: Whether to show the debug button in the status bar.
422- `timeout`: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
423- `log_dap_communications`: Whether to log messages between active debug adapters and Zed.
424- `format_dap_log_messages`: Whether to format DAP messages when adding them to the debug adapter logger.
425
426### Dock
427
428- Description: The position of the debug panel in the UI.
429- Default: `bottom`
430- Setting: debugger.dock
431
432**Options**
433
4341. `left` - The debug panel will be docked to the left side of the UI.
4352. `right` - The debug panel will be docked to the right side of the UI.
4363. `bottom` - The debug panel will be docked to the bottom of the UI.
437
438```json
439"debugger": {
440  "dock": "bottom"
441},
442```
443
444### Stepping granularity
445
446- Description: The Step granularity that the debugger will use
447- Default: line
448- Setting: debugger.stepping_granularity
449
450**Options**
451
4521. Statement - The step should allow the program to run until the current statement has finished executing.
453   The meaning of a statement is determined by the adapter and it may be considered equivalent to a line.
454   For example 'for(int i = 0; i < 10; i++)' could be considered to have 3 statements 'int i = 0', 'i < 10', and 'i++'.
455
456```json
457{
458  "debugger": {
459    "stepping_granularity": "statement"
460  }
461}
462```
463
4642. Line - The step should allow the program to run until the current source line has executed.
465
466```json
467{
468  "debugger": {
469    "stepping_granularity": "line"
470  }
471}
472```
473
4743. Instruction - The step should allow one instruction to execute (e.g. one x86 instruction).
475
476```json
477{
478  "debugger": {
479    "stepping_granularity": "instruction"
480  }
481}
482```
483
484### Save Breakpoints
485
486- Description: Whether the breakpoints should be saved across Zed sessions.
487- Default: true
488- Setting: debugger.save_breakpoints
489
490**Options**
491
492`boolean` values
493
494```json
495{
496  "debugger": {
497    "save_breakpoints": true
498  }
499}
500```
501
502### Button
503
504- Description: Whether the button should be displayed in the debugger toolbar.
505- Default: true
506- Setting: debugger.show_button
507
508**Options**
509
510`boolean` values
511
512```json
513{
514  "debugger": {
515    "show_button": true
516  }
517}
518```
519
520### Timeout
521
522- Description: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
523- Default: 2000
524- Setting: debugger.timeout
525
526**Options**
527
528`integer` values
529
530```json
531{
532  "debugger": {
533    "timeout": 3000
534  }
535}
536```
537
538### Log Dap Communications
539
540- Description: Whether to log messages between active debug adapters and Zed. (Used for DAP development)
541- Default: false
542- Setting: debugger.log_dap_communications
543
544**Options**
545
546`boolean` values
547
548```json
549{
550  "debugger": {
551    "log_dap_communications": true
552  }
553}
554```
555
556### Format Dap Log Messages
557
558- Description: Whether to format DAP messages when adding them to the debug adapter logger. (Used for DAP development)
559- Default: false
560- Setting: debugger.format_dap_log_messages
561
562**Options**
563
564`boolean` values
565
566```json
567{
568  "debugger": {
569    "format_dap_log_messages": true
570  }
571}
572```
573
574## Theme
575
576The Debugger supports the following theme options:
577
578**debugger.accent**: Color used to accent breakpoint & breakpoint-related symbols
579**editor.debugger_active_line.background**: Background color of active debug line