You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/debugging.md
+87Lines changed: 87 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -343,6 +343,93 @@ VS Code implements a generic, language-agnostic debugger UI based on [Debug Adap
343
343
344
344
For information on debugging using the legacy debug adapter, please see the old [Debugging Documentation](https://github.com/golang/vscode-go/blob/master/docs/debugging.md). Note that many new or enhanced features discussed in this document may not be available with the legacy debug adapter.
345
345
346
+
### Handling STDIN
347
+
348
+
The Go extension and `dlv` started as a subprocess of the extension do not have access to `tty`. The Go extension captures and forwards STDOUT/STDERR of the debug program to VS Code, so they can appear in `DEBUG OUTPUT` panel. But this arrangement does not handle STDIN.
349
+
350
+
When the target program needs to read from STDIN or access terminals (`tty`), use the `"console"` launch option that controls where the `dlv` debugger and the target process run:
351
+
352
+
*`integratedTerminal` for the terminal inside VS Code
353
+
*`externalTerminal` for the terminal outside VS Code
354
+
355
+
The Go extension delegates interaction with terminals to VS Code [using Debug Adapter Protocol's `RunInTerminal` functionality](https://github.com/golang/vscode-go/discussions/1626). For configuring VS Code's terminal related behavior, see VS Code's [documentation](https://code.visualstudio.com/docs/editor/integrated-terminal).
356
+
357
+
### Debugging a program as root
358
+
359
+
In order to run and debug a program running as root, the debugger (`dlv`) must run with root privilege, too. You can start the debug session with root privilege utilizing the `"asRoot"` AND `"console"` launch options. This is currently supported only on Linux and Mac.
360
+
361
+
When `asRoot` is true, the Go extension will use the `sudo` command to run `dlv`. Since `sudo` may ask you to enter password, the debug session needs [terminal access](#handling-stdin) so set `"console": "integratedTerminal"` or `"console": "externalTerminal"` in the launch configuration.
362
+
363
+
For example, the following launch configuration will start `myprogram` and debug it by running `sudo dlv dap` command in the integrated terminal.
364
+
365
+
```json
366
+
{
367
+
"name": "Launch as Root",
368
+
"request": "launch",
369
+
"mode": "exec",
370
+
"asRoot": true,
371
+
"program": "${workspaceRoot}/myprogram",
372
+
"console": "integratedTerminal",
373
+
...
374
+
}
375
+
```
376
+
377
+
The `asRoot` setting can be used with `auto`/`test`/`debug` launch modes that **build** the target binary to debug. That means the `go` command will be invoked as root to compile the binary, too. This can cause issues:
378
+
379
+
* by default, `sudo` does not preserve the user's current environment variables (see documentations about sudo's `--preserve-env` option). For example, `PATH` or library paths required for build may be different.
380
+
* Go environment variable settings usually associated in the home directory are different.
381
+
* Module/build caches used during build as root may be different from the caches used in your normal build. If they are the same, you may encounter permission errors due to cache data written to the caches as root.
382
+
383
+
Instead, you can arrange the `exec` launch mode to work with a pre-launch [task](https://code.visualstudio.com/docs/editor/tasks).
384
+
385
+
First, configure a debug build task to compile the target binary.
386
+
387
+
In `.vscode/tasks.json`:
388
+
```json
389
+
{
390
+
...
391
+
"tasks": [
392
+
{
393
+
"label": "go: build (debug)",
394
+
"command": "go",
395
+
"args": [
396
+
"build",
397
+
"-gcflags=all=-N -l",
398
+
"-o",
399
+
"${fileDirname}/__debug_bin"
400
+
],
401
+
"options": {
402
+
"cwd": "${fileDirname}"
403
+
}
404
+
...
405
+
}
406
+
]
407
+
}
408
+
```
409
+
410
+
The `-gcflags=all=-N -l` flag tells the `go build` command to preserve the debug information. The `-o` flag causes the compiled binary to be placed in `"${fileDirname}/__debug_bin"`. Extra build flags and environment variables *used for build* should be configured here as `args` or `options`'s `env` settings.
411
+
412
+
Then, configure the launch config to run the task before starting debugging.
413
+
414
+
In `.vscode/launch.json`:
415
+
```json
416
+
...
417
+
"configurations": [
418
+
{
419
+
"name": "Launch Package as root",
420
+
"type": "go",
421
+
"request": "launch",
422
+
"mode": "exec",
423
+
"asRoot": true,
424
+
"console": "integratedTerminal",
425
+
"program": "${fileDirname}/__debug_bin",
426
+
"preLaunchTask": "go: build (debug)",
427
+
}
428
+
]
429
+
```
430
+
431
+
Settings (`args`, `cwd`, `env`, ...) configured in the above `launch.json` will only apply when *running* the compiled binary, not when building the binary.
432
+
346
433
### Manually installing `dlv`
347
434
348
435
On rare occasions, you may want to install `dlv` by yourself instead of letting the extension handle its installation.
0 commit comments