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
8032cf1 src/goToolsInformation: update hardcoded latest version of gopls for release
6e5058c chore: Set permissions for GitHub actions
caee58d docs: update "debug a program as root" to work with snap and package tests
450ffa7 CHANGELOG.md: prepare for v0.33.1
1cd7544 build/all.bash: rearrange test order
27ca045 package.json: sync doc with [email protected]150c57f src/goVulncheck: show the vulncheck result in vulncheck.view
0d481b8 src/goMain: register VulncheckResultViewProvider
dd2efbc test/gopls/vulncheck: increase 'populate webview' test timeout
69e5534 src/goVulncheck: add VulncheckResultViewProvider
84ac6ba docs/debugging.md: instruction for `console` and debugging as root
c9cc7b4 test/integration/codelens: fix type error
cdd8871 codelens: skip run-test and debug-test links and explorer entry for TestMain(*testing.M)
5c2db73 src/language: remove deep-equal dependency
70500e7 vscode-go: update to LSP 3.17 and vscode-languageclient v8.0.0
1660a85 vscode-go: update @types/vscode and engines.vscode
7c79ef4 vscode-go: unify tsconfig for build and test
3b44015 test: fix type errors
924ca36 src/goVulncheck: rename VulncheckResult to VulncheckReport
070d2fa src/goExplorer.ts: add handler registrations to disposables
b75abf0 test/integration: increase test timeout
5a9bc67 vscode-go: make all context properties optional
ff8fb94 src/goMain.ts: move setGOROOTEnvVar to goEnv
1ea929e src/goMain.ts: move tools update logic to goInstallTools
47b1555 src/goMain.ts: cleanup activate function
a1303d3 src/goMain.ts: remove goCtx export
41cf6d4 src/goMain.ts: move diagnostic collections to context
7854360 src/goMain.ts: move runBuilds to commands
4eef175 vscode-go: refactor various commands to factory pattern
74c0476 src/goTest.ts: refactor goTest to factory pattern
044fec8 vscode-go: refactor goDoctor, goFillStruct, and goImpl to factory pattern
b8c8806 src/goModifyTags.ts: refactor goModifyTags to factory pattern
934fcc8 src/goMain.ts: refactor debugger factory activation
b0d0c71 src/goMain.ts: refactor codelens activation
4ba68e0 src/goMain.ts: refactor welcome panel activation
d9980fd src/goVulncheck: replace gopls custom command with `gopls vulncheck`
68e1355 src/goDocumentSymbols: refactor DocumentSymbolProvider to remove fallback code
43bc5f9 src/goDeveloperSurvey: prepare 2022 mid dev survey
29bdea3 build/all.bash: disable packages.json setting test
eea25f4 src/goMain.ts: remove unused imports
c0f7791 src/commands: separate command logic from goMain.ts
cf8170e src/goMain.ts: simplify command registration
b83469b src/goFillStruct.ts: fix type error
750ae4a src/context.ts: remove mutex from context
71a933a src/commands: create startLanguageServer command
5897ed5 src/language: create go extension context
7c02b7d test/gopls: fix strict type errors
8519776 test/gopls/testdata: update workspace testdata configuration
4ad096e src/goVulncheck.ts: only run vulncheck for active editors of go files
8abfcaa vscode-go: add strict typecheck step to CI
01cc238 package.json: update typescript
398f8dd src/debugAdapter: fix strict type errors
492f2fc src/misc: fix strict type errors in various commands and data providers
8de5e1f src/misc: fix strict type errors in util files
8d8695b src/language: fix strict type errors
1f5edd0 src/goTest: fix strict type errors
f08030f vscode-go: create separate tsconfig for builds
5d06597 package.json: change version to v0.34.0-dev
9227019 src/goLanguageServer: timeout LanguageClient.stop call
27e8258 src/util.ts: throw error when getGoVersion fails
Change-Id: I835bc5799682c8d2df889dd3eabda8fc37100d9e
Copy file name to clipboardExpand all lines: docs/debugging.md
+142Lines changed: 142 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -343,6 +343,148 @@ 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 programs and tests as root
358
+
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
+
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
+
#### Debug a program as root
364
+
365
+
For example, the following launch configuration will start `myprogram` and debug it by running `sudo dlv dap` command in the integrated terminal.
366
+
367
+
```json
368
+
{
369
+
"name": "Launch as Root",
370
+
"request": "launch",
371
+
"mode": "exec",
372
+
"asRoot": true,
373
+
"program": "${workspaceRoot}/myprogram",
374
+
"console": "integratedTerminal",
375
+
...
376
+
}
377
+
```
378
+
379
+
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:
380
+
381
+
* 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.
382
+
* Go environment variable settings usually associated in the home directory are different.
383
+
* 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.
384
+
385
+
Instead, you can arrange the `exec` launch mode to work with a pre-launch [task](https://code.visualstudio.com/docs/editor/tasks).
386
+
387
+
First, configure a debug build task to compile the target binary.
388
+
389
+
In `.vscode/tasks.json`:
390
+
```json
391
+
{
392
+
...
393
+
"tasks": [
394
+
{
395
+
"label": "go: build (debug)",
396
+
"type": "shell",
397
+
"command": "go",
398
+
"args": [
399
+
"build",
400
+
"-gcflags=all=-N -l",
401
+
"-o",
402
+
"${fileDirname}/__debug_bin"
403
+
],
404
+
"options": {
405
+
"cwd": "${fileDirname}"
406
+
},
407
+
...
408
+
}
409
+
]
410
+
}
411
+
```
412
+
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.
414
+
415
+
It might be useful to add `__debug_bin` to your `.gitignore` to avoid debugging binaries getting checked-in into your repository.
416
+
417
+
Then, configure the launch config to run the task before starting debugging.
418
+
419
+
In `.vscode/launch.json`:
420
+
```json
421
+
...
422
+
"configurations": [
423
+
{
424
+
"name": "Launch Package as root",
425
+
"type": "go",
426
+
"request": "launch",
427
+
"mode": "exec",
428
+
"asRoot": true,
429
+
"console": "integratedTerminal",
430
+
"program": "${fileDirname}/__debug_bin",
431
+
"preLaunchTask": "go: build (debug)",
432
+
}
433
+
]
434
+
```
435
+
436
+
Settings (`args`, `cwd`, `env`, ...) configured in the above `launch.json` will only apply when *running* the compiled binary, not when building the binary.
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
+
346
488
### Manually installing `dlv`
347
489
348
490
On rare occasions, you may want to install `dlv` by yourself instead of letting the extension handle its installation.
0 commit comments