1# Debugger
2
3Zed uses the [Debug Adapter Protocol (DAP)](https://microsoft.github.io/debug-adapter-protocol/) 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.
6Zed implements the client side of the protocol, and various _debug adapters_ implement the server side.
7
8This protocol enables features like setting breakpoints, stepping through code, inspecting variables,
9and more, in a consistent manner across different programming languages and runtime environments.
10
11## Supported Languages
12
13To debug code written in a specific language, Zed needs to find a debug adapter for that language. Some debug adapters are provided by Zed without additional setup, and some are provided by [language extensions](./extensions/debugger-extensions.md). The following languages currently have debug adapters available:
14
15<!-- keep this sorted -->
16
17- [C](./languages/c.md#debugging) (built-in)
18- [C++](./languages/cpp.md#debugging) (built-in)
19- [Go](./languages/go.md#debugging) (built-in)
20- [JavaScript](./languages/javascript.md#debugging) (built-in)
21- [PHP](./languages/php.md#debugging) (built-in)
22- [Python](./languages/python.md#debugging) (built-in)
23- [Ruby](./languages/ruby.md#debugging) (provided by extension)
24- [Rust](./languages/rust.md#debugging) (built-in)
25- [Swift](./languages/swift.md#debugging) (provided by extension)
26- [TypeScript](./languages/typescript.md#debugging) (built-in)
27
28> If your language isn't listed, you can contribute by adding a debug adapter for it. Check out our [debugger extensions](./extensions/debugger-extensions.md) documentation for more information.
29
30Follow those links for language- and adapter-specific information and examples, or read on for more about Zed's general debugging features that apply to all adapters.
31
32## Getting Started
33
34For most languages, the fastest way to get started is to run {#action debugger::Start} ({#kb debugger::Start}). This opens the _new process modal_, which shows you a contextual list of preconfigured debug tasks for the current project. Debug tasks are created from tests, entry points (like a `main` function), and from other sources — consult the documentation for your language for full information about what's supported.
35
36You can open the same modal by clicking the "plus" button at the top right of the debug panel.
37
38For languages that don't provide preconfigured debug tasks (this includes C, C++, and some extension-supported languages), you can define debug configurations in the `.zed/debug.json` file in your project root. This file should be an array of configuration objects:
39
40```json
41[
42 {
43 "adapter": "CodeLLDB",
44 "label": "First configuration"
45 // ...
46 },
47 {
48 "adapter": "Debugpy",
49 "label": "Second configuration"
50 // ...
51 }
52]
53```
54
55Check the documentation for your language for example configurations covering typical use-cases. Once you've added configurations to `.zed/debug.json`, they'll appear in the list in the new process modal.
56
57Zed will also load debug configurations from `.vscode/launch.json`, and show them in the new process modal if no configurations are found in `.zed/debug.json`.
58
59### Launching & Attaching
60
61Zed debugger offers two ways to debug your program; you can either _launch_ a new instance of your program or _attach_ to an existing process.
62Which one you choose depends on what you are trying to achieve.
63
64When 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.
65Running unit tests or a debug build of your application is a good use case for launching.
66
67Compared 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 for example, the bug is not reproducible outside of a production environment or some other circumstances.
68
69## Configuration
70
71While configuration fields are debug adapter-dependent, most adapters support the following fields:
72
73```json
74[
75 {
76 // The label for the debug configuration and used to identify the debug session inside the debug panel & new process modal
77 "label": "Example Start debugger config",
78 // The debug adapter that Zed should use to debug the program
79 "adapter": "Example adapter name",
80 // Request:
81 // - launch: Zed will launch the program if specified or shows a debug terminal with the right configuration
82 // - 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)
83 "request": "launch",
84 // program: The program that you want to debug
85 // This field supports path resolution with ~ or . symbols
86 "program": "path_to_program",
87 // cwd: defaults to the current working directory of your project ($ZED_WORKTREE_ROOT)
88 "cwd": "$ZED_WORKTREE_ROOT"
89 }
90]
91```
92
93All configuration fields support [task variables](./tasks.md#variables).
94
95### Build tasks
96
97Zed 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.
98
99```json
100[
101 {
102 "label": "Build Binary",
103 "adapter": "CodeLLDB",
104 "program": "path_to_program",
105 "request": "launch",
106 "build": {
107 "command": "make",
108 "args": ["build", "-j8"]
109 }
110 }
111]
112```
113
114Build tasks can also refer to the existing tasks by unsubstituted label:
115
116```json
117[
118 {
119 "label": "Build Binary",
120 "adapter": "CodeLLDB",
121 "program": "path_to_program",
122 "request": "launch",
123 "build": "my build task" // Or "my build task for $ZED_FILE"
124 }
125]
126```
127
128### Automatic scenario creation
129
130Given a Zed task, Zed can automatically create a scenario for you. Automatic scenario creation also powers our scenario creation from gutter.
131Automatic scenario creation is currently supported for Rust, Go, Python, JavaScript, and TypeScript.
132
133## Breakpoints
134
135To set a breakpoint, simply click next to the line number in the editor gutter.
136Breakpoints 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.
137At present, you can:
138
139- Add a log to a breakpoint, which will output a log message whenever that breakpoint is hit.
140- Make the breakpoint conditional, which will only stop at the breakpoint when the condition is met. The syntax for conditions is adapter-specific.
141- Add a hit count to a breakpoint, which will only stop at the breakpoint after it's hit a certain number of times.
142- Disable a breakpoint, which will prevent it from being hit while leaving it visible in the gutter.
143
144Some 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.
145
146All 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.
147The debug adapter will then stop whenever an exception of a given kind occurs. Which exception types are supported depends on the debug adapter.
148
149## Settings
150
151- `dock`: Determines the position of the debug panel in the UI.
152- `stepping_granularity`: Determines the stepping granularity.
153- `save_breakpoints`: Whether the breakpoints should be reused across Zed sessions.
154- `button`: Whether to show the debug button in the status bar.
155- `timeout`: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
156- `log_dap_communications`: Whether to log messages between active debug adapters and Zed.
157- `format_dap_log_messages`: Whether to format DAP messages when adding them to the debug adapter logger.
158
159### Dock
160
161- Description: The position of the debug panel in the UI.
162- Default: `bottom`
163- Setting: debugger.dock
164
165**Options**
166
1671. `left` - The debug panel will be docked to the left side of the UI.
1682. `right` - The debug panel will be docked to the right side of the UI.
1693. `bottom` - The debug panel will be docked to the bottom of the UI.
170
171```json
172"debugger": {
173 "dock": "bottom"
174},
175```
176
177### Stepping granularity
178
179- Description: The Step granularity that the debugger will use
180- Default: `line`
181- Setting: `debugger.stepping_granularity`
182
183**Options**
184
1851. Statement - The step should allow the program to run until the current statement has finished executing.
186 The meaning of a statement is determined by the adapter and it may be considered equivalent to a line.
187 For example 'for(int i = 0; i < 10; i++)' could be considered to have 3 statements 'int i = 0', 'i < 10', and 'i++'.
188
189```json
190{
191 "debugger": {
192 "stepping_granularity": "statement"
193 }
194}
195```
196
1972. Line - The step should allow the program to run until the current source line has executed.
198
199```json
200{
201 "debugger": {
202 "stepping_granularity": "line"
203 }
204}
205```
206
2073. Instruction - The step should allow one instruction to execute (e.g. one x86 instruction).
208
209```json
210{
211 "debugger": {
212 "stepping_granularity": "instruction"
213 }
214}
215```
216
217### Save Breakpoints
218
219- Description: Whether the breakpoints should be saved across Zed sessions.
220- Default: `true`
221- Setting: `debugger.save_breakpoints`
222
223**Options**
224
225`boolean` values
226
227```json
228{
229 "debugger": {
230 "save_breakpoints": true
231 }
232}
233```
234
235### Button
236
237- Description: Whether the button should be displayed in the debugger toolbar.
238- Default: `true`
239- Setting: `debugger.show_button`
240
241**Options**
242
243`boolean` values
244
245```json
246{
247 "debugger": {
248 "show_button": true
249 }
250}
251```
252
253### Timeout
254
255- Description: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
256- Default: `2000`
257- Setting: `debugger.timeout`
258
259**Options**
260
261`integer` values
262
263```json
264{
265 "debugger": {
266 "timeout": 3000
267 }
268}
269```
270
271### Inline Values
272
273- Description: Whether to enable editor inlay hints showing the values of variables in your code during debugging sessions.
274- Default: `true`
275- Setting: `inlay_hints.show_value_hints`
276
277**Options**
278
279```json
280{
281 "inlay_hints": {
282 "show_value_hints": false
283 }
284}
285```
286
287Inline value hints can also be toggled from the Editor Controls menu in the editor toolbar.
288
289### Log Dap Communications
290
291- Description: Whether to log messages between active debug adapters and Zed. (Used for DAP development)
292- Default: false
293- Setting: debugger.log_dap_communications
294
295**Options**
296
297`boolean` values
298
299```json
300{
301 "debugger": {
302 "log_dap_communications": true
303 }
304}
305```
306
307### Format Dap Log Messages
308
309- Description: Whether to format DAP messages when adding them to the debug adapter logger. (Used for DAP development)
310- Default: false
311- Setting: debugger.format_dap_log_messages
312
313**Options**
314
315`boolean` values
316
317```json
318{
319 "debugger": {
320 "format_dap_log_messages": true
321 }
322}
323```
324
325### Customizing Debug Adapters
326
327- Description: Custom program path and arguments to override how Zed launches a specific debug adapter.
328- Default: Adapter-specific
329- Setting: `dap.$ADAPTER.binary` and `dap.$ADAPTER.args`
330
331You can pass `binary`, `args`, or both. `binary` should be a path to a _debug adapter_ (like `lldb-dap`) not a _debugger_ (like `lldb` itself). The `args` setting overrides any arguments that Zed would otherwise pass to the adapter.
332
333```json
334{
335 "dap": {
336 "CodeLLDB": {
337 "binary": "/Users/name/bin/lldb-dap",
338 "args": ["--wait-for-debugger"]
339 }
340 }
341}
342```
343
344## Theme
345
346The Debugger supports the following theme options:
347
348- `debugger.accent`: Color used to accent breakpoint & breakpoint-related symbols
349- `editor.debugger_active_line.background`: Background color of active debug line
350
351## Troubleshooting
352
353If you're running into problems with the debugger, please [open a GitHub issue](https://github.com/zed-industries/zed/issues/new?template=04_bug_debugger.yml) or [schedule an onboarding call](https://cal.com/team/zed-research/debugger) with us so we can help understand and fix your issue.
354
355There are also some features you can use to gather more information about the problem:
356
357- When you have a session running in the debug panel, you can run the {#action dev::CopyDebugAdapterArguments} action to copy a JSON blob to the clipboard that describes how Zed initialized the session. This is especially useful when the session failed to start, and is great context to add if you open a GitHub issue.
358- You can also use the {#action dev::OpenDebugAdapterLogs} action to see a trace of all of Zed's communications with debug adapters during the most recent debug sessions.