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## Supported Debug Adapters
 10
 11Zed supports a variety of debug adapters for different programming languages:
 12
 13- JavaScript (node): Enables debugging of Node.js applications, including setting breakpoints, stepping through code, and inspecting variables in JavaScript.
 14
 15- Python (debugpy): Provides debugging capabilities for Python applications, supporting features like remote debugging, multi-threaded debugging, and Django/Flask application debugging.
 16
 17- LLDB: A powerful debugger for C, C++, Objective-C, and Swift, offering low-level debugging features and support for Apple platforms.
 18
 19- GDB: The GNU Debugger, which supports debugging for multiple programming languages including C, C++, Go, and Rust, across various platforms.
 20
 21- Go (dlv): 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.
 22
 23- PHP (xdebug): Provides debugging and profiling capabilities for PHP applications, including remote debugging and code coverage analysis.
 24
 25These adapters enable Zed to provide a consistent debugging experience across multiple languages while leveraging the specific features and capabilities of each debugger.
 26
 27Additionally, Ruby support (via rdbg) is being actively worked on.
 28
 29## Getting Started
 30
 31For 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.
 32
 33For more advanced use cases, you can create debug configurations by directly editing the `.zed/debug.json` file in your project root directory.
 34
 35You can then use the `New Session Modal` to select a configuration and start debugging.
 36
 37### Launching & Attaching
 38
 39Zed debugger offers two ways to debug your program; you can either _launch_ a new instance of your program or _attach_ to an existing process.
 40Which one you choose depends on what you are trying to achieve.
 41
 42When 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.
 43
 44Compared 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.
 45
 46## Configuration
 47
 48While configuration fields are debug adapter-dependent, most adapters support the following fields:
 49
 50```json
 51[
 52  {
 53    // The label for the debug configuration and used to identify the debug session inside the debug panel & new session modal
 54    "label": "Example Start debugger config",
 55    // The debug adapter that Zed should use to debug the program
 56    "adapter": "Example adapter name",
 57    // Request:
 58    //  - launch: Zed will launch the program if specified or shows a debug terminal with the right configuration
 59    //  - 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)
 60    "request": "launch",
 61    // program: The program that you want to debug
 62    // This field supports path resolution with ~ or . symbols
 63    "program": "path_to_program",
 64    // cwd: defaults to the current working directory of your project ($ZED_WORKTREE_ROOT)
 65    "cwd": "$ZED_WORKTREE_ROOT"
 66  }
 67]
 68```
 69
 70All configuration fields support task variables. See [Tasks Variables](./tasks.md#variables)
 71
 72### Build tasks
 73
 74Zed 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.
 75
 76```json
 77[
 78  {
 79    "label": "Build Binary",
 80    "adapter": "CodeLLDB",
 81    "program": "path_to_program",
 82    "request": "launch",
 83    "build": {
 84      "command": "make",
 85      "args": ["build", "-j8"]
 86    }
 87  }
 88]
 89```
 90
 91Build tasks can also refer to the existing tasks by unsubstituted label:
 92
 93```json
 94[
 95  {
 96    "label": "Build Binary",
 97    "adapter": "CodeLLDB",
 98    "program": "path_to_program",
 99    "request": "launch",
100    "build": "my build task" // Or "my build task for $ZED_FILE"
101  }
102]
103```
104
105### Automatic scenario creation
106
107Given a Zed task, Zed can automatically create a scenario for you. Automatic scenario creation also powers our scenario creation from gutter.
108Automatic scenario creation is currently supported for Rust, Go and Python. Javascript/TypeScript support being worked on.
109
110### Example Configurations
111
112#### Go
113
114```json
115[
116  {
117    "label": "Go (Delve)",
118    "adapter": "Delve",
119    "program": "$ZED_FILE",
120    "request": "launch",
121    "mode": "debug"
122  }
123]
124```
125
126#### JavaScript
127
128##### Debug Active File
129
130```json
131[
132  {
133    "label": "Debug with node",
134    "adapter": "JavaScript",
135    "program": "$ZED_FILE",
136    "request": "launch",
137    "console": "integratedTerminal",
138    "type": "pwa-node"
139  }
140]
141```
142
143##### Attach debugger to a server running in web browser (`npx serve`)
144
145Given 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.
146
147```json
148[
149  {
150    "label": "Inspect ",
151    "adapter": "JavaScript",
152    "type": "pwa-chrome",
153    "request": "launch",
154    "url": "http://localhost:5500", // Fill your URL here.
155    "program": "$ZED_FILE",
156    "webRoot": "${ZED_WORKTREE_ROOT}"
157  }
158]
159```
160
161#### Python
162
163##### Debug Active File
164
165```json
166[
167  {
168    "label": "Python Active File",
169    "adapter": "Debugpy",
170    "program": "$ZED_FILE",
171    "request": "launch"
172  }
173]
174```
175
176##### Flask App
177
178For a common Flask Application with a file structure similar to the following:
179
180```
181.venv/
182app/
183  init.py
184  main.py
185  routes.py
186templates/
187  index.html
188static/
189  style.css
190requirements.txt
191```
192
193the following configuration can be used:
194
195```json
196[
197  {
198    "label": "Python: Flask",
199    "adapter": "Debugpy",
200    "request": "launch",
201    "module": "app",
202    "cwd": "$ZED_WORKTREE_ROOT",
203    "env": {
204      "FLASK_APP": "app",
205      "FLASK_DEBUG": "1"
206    },
207    "args": [
208      "run",
209      "--reload", // Enables Flask reloader that watches for file changes
210      "--debugger" // Enables Flask debugger
211    ],
212    "autoReload": {
213      "enable": true
214    },
215    "jinja": true,
216    "justMyCode": true
217  }
218]
219```
220
221#### Rust/C++/C
222
223##### Using pre-built binary
224
225```json
226[
227  {
228    "label": "Debug native binary",
229    "program": "$ZED_WORKTREE_ROOT/build/binary",
230    "request": "launch",
231    "adapter": "CodeLLDB" // GDB is available on non arm macs as well as linux
232  }
233]
234```
235
236##### Build binary then debug
237
238```json
239[
240  {
241    "label": "Build & Debug native binary",
242    "build": {
243      "command": "cargo",
244      "args": ["build"]
245    },
246    "program": "$ZED_WORKTREE_ROOT/target/debug/binary",
247    "request": "launch",
248    "adapter": "CodeLLDB" // GDB is available on non arm macs as well as linux
249  }
250]
251```
252
253#### TypeScript
254
255##### Attach debugger to a server running in web browser (`npx serve`)
256
257Given 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.
258
259```json
260[
261  {
262    "label": "Launch Chromee (TypeScript)",
263    "adapter": "JavaScript",
264    "type": "pwa-chrome",
265    "request": "launch",
266    "url": "http://localhost:5500",
267    "program": "$ZED_FILE",
268    "webRoot": "${ZED_WORKTREE_ROOT}",
269    "sourceMaps": true,
270    "build": {
271      "command": "npx",
272      "args": ["tsc"]
273    }
274  }
275]
276```
277
278## Breakpoints
279
280To set a breakpoint, simply click next to the line number in the editor gutter.
281Breakpoints 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.
282At present, you can:
283
284- Add a log to a breakpoint, which will output a log message whenever that breakpoint is hit.
285- Make the breakpoint conditional, which will only stop at the breakpoint when the condition is met. The syntax for conditions is adapter-specific.
286- Add a hit count to a breakpoint, which will only stop at the breakpoint after it's hit a certain number of times.
287- Disable a breakpoint, which will prevent it from being hit while leaving it visible in the gutter.
288
289Some 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.
290
291All 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.
292The debug adapter will then stop whenever an exception of a given kind occurs. Which exception types are supported depends on the debug adapter.
293
294## Settings
295
296- `dock`: Determines the position of the debug panel in the UI.
297- `stepping_granularity`: Determines the stepping granularity.
298- `save_breakpoints`: Whether the breakpoints should be reused across Zed sessions.
299- `button`: Whether to show the debug button in the status bar.
300- `timeout`: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
301- `log_dap_communications`: Whether to log messages between active debug adapters and Zed.
302- `format_dap_log_messages`: Whether to format DAP messages when adding them to the debug adapter logger.
303
304### Dock
305
306- Description: The position of the debug panel in the UI.
307- Default: `bottom`
308- Setting: debugger.dock
309
310**Options**
311
3121. `left` - The debug panel will be docked to the left side of the UI.
3132. `right` - The debug panel will be docked to the right side of the UI.
3143. `bottom` - The debug panel will be docked to the bottom of the UI.
315
316```json
317"debugger": {
318  "dock": "bottom"
319},
320```
321
322### Stepping granularity
323
324- Description: The Step granularity that the debugger will use
325- Default: line
326- Setting: debugger.stepping_granularity
327
328**Options**
329
3301. Statement - The step should allow the program to run until the current statement has finished executing.
331   The meaning of a statement is determined by the adapter and it may be considered equivalent to a line.
332   For example 'for(int i = 0; i < 10; i++)' could be considered to have 3 statements 'int i = 0', 'i < 10', and 'i++'.
333
334```json
335{
336  "debugger": {
337    "stepping_granularity": "statement"
338  }
339}
340```
341
3422. Line - The step should allow the program to run until the current source line has executed.
343
344```json
345{
346  "debugger": {
347    "stepping_granularity": "line"
348  }
349}
350```
351
3523. Instruction - The step should allow one instruction to execute (e.g. one x86 instruction).
353
354```json
355{
356  "debugger": {
357    "stepping_granularity": "instruction"
358  }
359}
360```
361
362### Save Breakpoints
363
364- Description: Whether the breakpoints should be saved across Zed sessions.
365- Default: true
366- Setting: debugger.save_breakpoints
367
368**Options**
369
370`boolean` values
371
372```json
373{
374  "debugger": {
375    "save_breakpoints": true
376  }
377}
378```
379
380### Button
381
382- Description: Whether the button should be displayed in the debugger toolbar.
383- Default: true
384- Setting: debugger.show_button
385
386**Options**
387
388`boolean` values
389
390```json
391{
392  "debugger": {
393    "show_button": true
394  }
395}
396```
397
398### Timeout
399
400- Description: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
401- Default: 2000
402- Setting: debugger.timeout
403
404**Options**
405
406`integer` values
407
408```json
409{
410  "debugger": {
411    "timeout": 3000
412  }
413}
414```
415
416### Log Dap Communications
417
418- Description: Whether to log messages between active debug adapters and Zed. (Used for DAP development)
419- Default: false
420- Setting: debugger.log_dap_communications
421
422**Options**
423
424`boolean` values
425
426```json
427{
428  "debugger": {
429    "log_dap_communications": true
430  }
431}
432```
433
434### Format Dap Log Messages
435
436- Description: Whether to format DAP messages when adding them to the debug adapter logger. (Used for DAP development)
437- Default: false
438- Setting: debugger.format_dap_log_messages
439
440**Options**
441
442`boolean` values
443
444```json
445{
446  "debugger": {
447    "format_dap_log_messages": true
448  }
449}
450```
451
452## Theme
453
454The Debugger supports the following theme options:
455
456**debugger.accent**: Color used to accent breakpoint & breakpoint-related symbols
457**editor.debugger_active_line.background**: Background color of active debug line