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
+58-3Lines changed: 58 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -354,12 +354,14 @@ When the target program needs to read from STDIN or access terminals (`tty`), us
354
354
355
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
356
357
-
### Debugging a program as root
357
+
### Debugging programs and tests as root
358
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.
359
+
In order to run and debug a program or a package test 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
360
361
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
362
363
+
#### Debug a program as root
364
+
363
365
For example, the following launch configuration will start `myprogram` and debug it by running `sudo dlv dap` command in the integrated terminal.
364
366
365
367
```json
@@ -391,6 +393,7 @@ In `.vscode/tasks.json`:
391
393
"tasks": [
392
394
{
393
395
"label": "go: build (debug)",
396
+
"type": "shell",
394
397
"command": "go",
395
398
"args": [
396
399
"build",
@@ -400,7 +403,7 @@ In `.vscode/tasks.json`:
400
403
],
401
404
"options": {
402
405
"cwd": "${fileDirname}"
403
-
}
406
+
},
404
407
...
405
408
}
406
409
]
@@ -409,6 +412,8 @@ In `.vscode/tasks.json`:
409
412
410
413
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
414
415
+
It might be useful to add `__debug_bin` to your `.gitignore` to avoid debugging binaries getting checked-in into your repository.
416
+
412
417
Then, configure the launch config to run the task before starting debugging.
413
418
414
419
In `.vscode/launch.json`:
@@ -430,6 +435,56 @@ In `.vscode/launch.json`:
430
435
431
436
Settings (`args`, `cwd`, `env`, ...) configured in the above `launch.json` will only apply when *running* the compiled binary, not when building the binary.
432
437
438
+
#### Debug a package test as root
439
+
440
+
To debug package tests as root add the following launch and task configurations.
441
+
442
+
In `.vscode/tasks.json`:
443
+
```json
444
+
...
445
+
"tasks": [
446
+
{
447
+
...
448
+
},
449
+
{
450
+
"label": "go test (debug)",
451
+
"type": "shell",
452
+
"command": "go",
453
+
"args": [
454
+
"test",
455
+
"-c",
456
+
"-o",
457
+
"${fileDirname}/__debug_bin"
458
+
],
459
+
"options": {
460
+
"cwd": "${fileDirname}",
461
+
},
462
+
...
463
+
}
464
+
]
465
+
```
466
+
467
+
In `.vscode/launch.json`:
468
+
```json
469
+
...
470
+
"configurations": [
471
+
{
472
+
...
473
+
},
474
+
{
475
+
"name": "Debug Package Test as root",
476
+
"type": "go",
477
+
"request": "launch",
478
+
"mode": "exec",
479
+
"asRoot": true,
480
+
"program": "${fileDirname}/__debug_bin",
481
+
"cwd": "${fileDirname}",
482
+
"console": "integratedTerminal",
483
+
"preLaunchTask": "go test (debug)"
484
+
}
485
+
]
486
+
```
487
+
433
488
### Manually installing `dlv`
434
489
435
490
On rare occasions, you may want to install `dlv` by yourself instead of letting the extension handle its installation.
0 commit comments