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 [debug]
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#### Global debug configurations
60
61If you run the same launch profiles across multiple projects, you can store them once in your user configuration. Invoke {#action zed::OpenDebugTasks} from the command palette to open the global `debug.json` file; Zed creates it next to your user `settings.json` and keeps it in sync with the debugger UI. The file lives at:
62
63- **macOS:** `~/Library/Application Support/Zed/debug.json`
64- **Linux/BSD:** `$XDG_CONFIG_HOME/zed/debug.json` (falls back to `~/.config/zed/debug.json`)
65- **Windows:** `%APPDATA%\Zed\debug.json`
66
67Populate this file with the same array of objects you would place in `.zed/debug.json`. Any scenarios defined there are merged into every workspace, so your favorite launch presets appear automatically in the "New Debug Session" dialog.
68
69### Launching & Attaching
70
71Zed debugger offers two ways to debug your program; you can either _launch_ a new instance of your program or _attach_ to an existing process.
72Which one you choose depends on what you are trying to achieve.
73
74When 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.
75Running unit tests or a debug build of your application is a good use case for launching.
76
77Compared 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.
78
79## Configuration
80
81Zed requires the `adapter` and `label` fields for all debug tasks. In addition, Zed will use the `build` field to run any necessary setup steps before the debugger starts [(see below)](#build-tasks), and can accept a `tcp_connection` field to connect to an existing process.
82
83All other fields are provided by the debug adapter and can contain [task variables](./tasks.md#variables). Most adapters support `request`, `program`, and `cwd`:
84
85```json [debug]
86[
87 {
88 // The label for the debug configuration and used to identify the debug session inside the debug panel & new process modal
89 "label": "Example Start debugger config",
90 // The debug adapter that Zed should use to debug the program
91 "adapter": "Example adapter name",
92 // Request:
93 // - launch: Zed will launch the program if specified, or show a debug terminal with the right configuration
94 // - attach: Zed will attach to a running program to debug it, or when the process_id is not specified, will show a process picker (only supported for node currently)
95 "request": "launch",
96 // The program to debug. This field supports path resolution with ~ or . symbols.
97 "program": "path_to_program",
98 // cwd: defaults to the current working directory of your project ($ZED_WORKTREE_ROOT)
99 "cwd": "$ZED_WORKTREE_ROOT"
100 }
101]
102```
103
104Check your debug adapter's documentation for more information on the fields it supports.
105
106### Build tasks
107
108Zed allows embedding a Zed task in the `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.
109
110```json [debug]
111[
112 {
113 "label": "Build Binary",
114 "adapter": "CodeLLDB",
115 "program": "path_to_program",
116 "request": "launch",
117 "build": {
118 "command": "make",
119 "args": ["build", "-j8"]
120 }
121 }
122]
123```
124
125Build tasks can also refer to the existing tasks by unsubstituted label:
126
127```json [debug]
128[
129 {
130 "label": "Build Binary",
131 "adapter": "CodeLLDB",
132 "program": "path_to_program",
133 "request": "launch",
134 "build": "my build task" // Or "my build task for $ZED_FILE"
135 }
136]
137```
138
139### Automatic scenario creation
140
141Given a Zed task, Zed can automatically create a scenario for you. Automatic scenario creation also powers our scenario creation from gutter.
142Automatic scenario creation is currently supported for Rust, Go, Python, JavaScript, and TypeScript.
143
144## Breakpoints
145
146To set a breakpoint, simply click next to the line number in the editor gutter.
147Breakpoints 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.
148At present, you can:
149
150- Add a log to a breakpoint, which will output a log message whenever that breakpoint is hit.
151- Make the breakpoint conditional, which will only stop at the breakpoint when the condition is met. The syntax for conditions is adapter-specific.
152- Add a hit count to a breakpoint, which will only stop at the breakpoint after it's hit a certain number of times.
153- Disable a breakpoint, which will prevent it from being hit while leaving it visible in the gutter.
154
155Some 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.
156
157All 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.
158The debug adapter will then stop whenever an exception of a given kind occurs. Which exception types are supported depends on the debug adapter.
159
160## Settings
161
162The settings for the debugger are grouped under the `debugger` key in `settings.json`:
163
164- `dock`: Determines the position of the debug panel in the UI.
165- `stepping_granularity`: Determines the stepping granularity.
166- `save_breakpoints`: Whether the breakpoints should be reused across Zed sessions.
167- `button`: Whether to show the debug button in the status bar.
168- `timeout`: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
169- `log_dap_communications`: Whether to log messages between active debug adapters and Zed.
170- `format_dap_log_messages`: Whether to format DAP messages when adding them to the debug adapter logger.
171
172### Dock
173
174- Description: The position of the debug panel in the UI.
175- Default: `bottom`
176- Setting: debugger.dock
177
178**Options**
179
1801. `left` - The debug panel will be docked to the left side of the UI.
1812. `right` - The debug panel will be docked to the right side of the UI.
1823. `bottom` - The debug panel will be docked to the bottom of the UI.
183
184```json [settings]
185"debugger": {
186 "dock": "bottom"
187},
188```
189
190### Stepping granularity
191
192- Description: The Step granularity that the debugger will use
193- Default: `line`
194- Setting: `debugger.stepping_granularity`
195
196**Options**
197
1981. Statement - The step should allow the program to run until the current statement has finished executing.
199 The meaning of a statement is determined by the adapter and it may be considered equivalent to a line.
200 For example 'for(int i = 0; i < 10; i++)' could be considered to have 3 statements 'int i = 0', 'i < 10', and 'i++'.
201
202```json [settings]
203{
204 "debugger": {
205 "stepping_granularity": "statement"
206 }
207}
208```
209
2102. Line - The step should allow the program to run until the current source line has executed.
211
212```json [settings]
213{
214 "debugger": {
215 "stepping_granularity": "line"
216 }
217}
218```
219
2203. Instruction - The step should allow one instruction to execute (e.g. one x86 instruction).
221
222```json [settings]
223{
224 "debugger": {
225 "stepping_granularity": "instruction"
226 }
227}
228```
229
230### Save Breakpoints
231
232- Description: Whether the breakpoints should be saved across Zed sessions.
233- Default: `true`
234- Setting: `debugger.save_breakpoints`
235
236**Options**
237
238`boolean` values
239
240```json [settings]
241{
242 "debugger": {
243 "save_breakpoints": true
244 }
245}
246```
247
248### Button
249
250- Description: Whether the button should be displayed in the debugger toolbar.
251- Default: `true`
252- Setting: `debugger.show_button`
253
254**Options**
255
256`boolean` values
257
258```json [settings]
259{
260 "debugger": {
261 "show_button": true
262 }
263}
264```
265
266### Timeout
267
268- Description: Time in milliseconds until timeout error when connecting to a TCP debug adapter.
269- Default: `2000`
270- Setting: `debugger.timeout`
271
272**Options**
273
274`integer` values
275
276```json [settings]
277{
278 "debugger": {
279 "timeout": 3000
280 }
281}
282```
283
284### Inline Values
285
286- Description: Whether to enable editor inlay hints showing the values of variables in your code during debugging sessions.
287- Default: `true`
288- Setting: `inlay_hints.show_value_hints`
289
290**Options**
291
292```json [settings]
293{
294 "inlay_hints": {
295 "show_value_hints": false
296 }
297}
298```
299
300Inline value hints can also be toggled from the Editor Controls menu in the editor toolbar.
301
302### Log Dap Communications
303
304- Description: Whether to log messages between active debug adapters and Zed. (Used for DAP development)
305- Default: false
306- Setting: debugger.log_dap_communications
307
308**Options**
309
310`boolean` values
311
312```json [settings]
313{
314 "debugger": {
315 "log_dap_communications": true
316 }
317}
318```
319
320### Format Dap Log Messages
321
322- Description: Whether to format DAP messages when adding them to the debug adapter logger. (Used for DAP development)
323- Default: false
324- Setting: debugger.format_dap_log_messages
325
326**Options**
327
328`boolean` values
329
330```json [settings]
331{
332 "debugger": {
333 "format_dap_log_messages": true
334 }
335}
336```
337
338### Customizing Debug Adapters
339
340- Description: Custom program path and arguments to override how Zed launches a specific debug adapter.
341- Default: Adapter-specific
342- Setting: `dap.$ADAPTER.binary` and `dap.$ADAPTER.args`
343
344You 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.
345
346```json [settings]
347{
348 "dap": {
349 "CodeLLDB": {
350 "binary": "/Users/name/bin/lldb-dap",
351 "args": ["--wait-for-debugger"]
352 }
353 }
354}
355```
356
357## Theme
358
359The Debugger supports the following theme options:
360
361- `debugger.accent`: Color used to accent breakpoint & breakpoint-related symbols
362- `editor.debugger_active_line.background`: Background color of active debug line
363
364## Troubleshooting
365
366If 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), providing as much context as possible. There are also some features you can use to gather more information about the problem:
367
368- 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.
369- 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.