Skip to content

Commit caee58d

Browse files
thediveosuzmue
authored andcommitted
docs: update "debug a program as root" to work with snap and package tests
fixes issue #2250 Change-Id: I95dcf9d9c724059602877892c9f82de45c6d1f13 GitHub-Last-Rev: 92e7970 GitHub-Pull-Request: #2258 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/408454 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Auto-Submit: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Suzy Mueller <[email protected]>
1 parent 450ffa7 commit caee58d

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

docs/debugging.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,14 @@ When the target program needs to read from STDIN or access terminals (`tty`), us
354354

355355
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).
356356

357-
### Debugging a program as root
357+
### Debugging programs and tests as root
358358

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.
360360

361361
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.
362362

363+
#### Debug a program as root
364+
363365
For example, the following launch configuration will start `myprogram` and debug it by running `sudo dlv dap` command in the integrated terminal.
364366

365367
```json
@@ -391,6 +393,7 @@ In `.vscode/tasks.json`:
391393
"tasks": [
392394
{
393395
"label": "go: build (debug)",
396+
"type": "shell",
394397
"command": "go",
395398
"args": [
396399
"build",
@@ -400,7 +403,7 @@ In `.vscode/tasks.json`:
400403
],
401404
"options": {
402405
"cwd": "${fileDirname}"
403-
}
406+
},
404407
...
405408
}
406409
]
@@ -409,6 +412,8 @@ In `.vscode/tasks.json`:
409412

410413
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.
411414

415+
It might be useful to add `__debug_bin` to your `.gitignore` to avoid debugging binaries getting checked-in into your repository.
416+
412417
Then, configure the launch config to run the task before starting debugging.
413418

414419
In `.vscode/launch.json`:
@@ -430,6 +435,56 @@ In `.vscode/launch.json`:
430435

431436
Settings (`args`, `cwd`, `env`, ...) configured in the above `launch.json` will only apply when *running* the compiled binary, not when building the binary.
432437

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+
433488
### Manually installing `dlv`
434489

435490
On rare occasions, you may want to install `dlv` by yourself instead of letting the extension handle its installation.

0 commit comments

Comments
 (0)