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