1. Basic knowledge of Go 41. Debugging: DLV tool - detailed explanation of related commands

1.dlv --help - View the help information of the Delve debugger

  • Explanation: This command is used to view the help information of the Delve debugger, which provides a basic introduction to Delve and a detailed description of the available commands and options.
  • Usage: Enter dlv --help in the command line.
  • Results in: Displays help information for the Delve debugger, including the debugger's goals, basic usage, a list of available commands, and links to other help topics.

Delve is a source-level debugger for Go programs that can interact with the program by controlling the execution of the process, evaluating variables, and providing information such as thread/goroutine status, CPU register status, etc. This tool aims to provide a simple yet powerful interface for debugging Go programs.

Flags can be passed to the program to be debugged via the -- flag, for example:

dlv exec ./hello -- server --config conf/config.toml

Available Commands

  • attach: Attach to a running process and start debugging.
  • connect: Connect to the headless debugging server and use a terminal client to communicate.
  • core: Check the core dump file.
  • dap: Starts a headless TCP server that communicates via Debug Adapter Protocol (DAP).
  • debug: Compile and start debugging the main package or the specified package in the current directory.
  • exec: Execute the precompiled binary and start a debugging session.
  • help: Get help on any command.
  • test: Compile the test binary and start debugging the program.
  • trace: Compile and start tracing the program.
  • version: Print version information.

Additional help topics

  • dlv backend: Help about the --backend flag.
  • dlv log:Help about log flags.
  • dlv redirect: Help about file redirection.

Use dlv [command] --help to get more information about a specific command.

2.dlv attach - Connect to the running process and start debugging

  • Explanation: This command will cause Delve to take over an already running process and start a new debugging session. When exiting a debugging session, you can choose to let the process continue running or terminate it.

  • Usage: dlv attach pid [executable] [flags], connect to the specified process for debugging. The process ID, executable file path, and some flag parameters can be provided.

  • Results: Delve takes over the specified process, enabling the execution of the process to be inspected, modified, and controlled within a debugging session.

    • --continue: Continue the debugging process on startup.
    • --waitfor: Waits for a process with a name starting with the specified prefix.
    • --waitfor-duration: The total time spent waiting for the process.
    • --waitfor-interval: The time between process list checks.
  • Global flags:

    • --accept-multiclient: allows the headless server to accept multiple client connections via JSON-RPC or DAP.
    • --allow-non-terminal-interactive: allows Delve interactive sessions without a terminal for standard input, output, and error.
    • --api-version: Select the JSON-RPC API version. New clients should use v2. Can be reset via RPCServer.SetApiVersion. View documentation/api/json-rpc/README.md. (default 1)
    • --backend: Backend selection (see ‘dlv help backend’). (default value "default")
    • --check-go-version: Exit if the Go version used is incompatible with the Delve version (too old or new). (default true)
    • --headless: Run the debug server in headless mode only. The server will accept JSON-RPC or DAP client connections.
    • --init: Initialization file, executed by the terminal client.
    • -l or --listen: The listening address of the debug server. (Default "127.0.0.1:0")
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
    • --only-same-user: Allow connections only from the same user who started this Delve instance. (default true)

3.dlv connect - Connect to the running headless debugging server

  • Explanation: This command is used to connect to a running headless debugging server and interact with it using a terminal client.

  • Usage: dlv connect addr [flags], connect to the running debugging server through the specified address. Some flag parameters can be provided.

  • Results: Establishes a connection to a running headless debugging server, allowing the user to interact with the debugging server through a terminal client.

    • --backend: Backend selection (see ‘dlv help backend’). (default value "default")
    • --init: Initialization file, executed by the terminal client.
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
  • Global flags:

    • --backend: Backend selection (see ‘dlv help backend’). (default value "default")
    • --init: Initialization file, executed by the terminal client.
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').

4.dlv core - Analyze core dump file

  • Explanation: This command is used to analyze core dump files. Only Linux and Windows core dumps are supported. This command allows analysis of core dump files using Delve to understand the state of the process at the time the core dump was generated.

  • Usage: dlv core <executable> <core> [flags], opens the specified core file and related executable files, allowing inspection of the status of the process when the core dump was generated.

  • Results: Delve will load the specified core file and related executable files, enabling you to check the status of the process when the core dump is generated.

    • <executable>: The path to the executable file.
    • <core>: Path to the core dump file.
  • Global flags:

    • --accept-multiclient: allows the headless server to accept multiple client connections via JSON-RPC or DAP.
    • --allow-non-terminal-interactive: allows interactive Delve sessions without a terminal for standard input, output, and error.
    • --api-version: Select the JSON-RPC API version in headless mode. New clients should use v2. Can be reset via RPCServer.SetApiVersion. View documentation/api/json-rpc/README.md. (default 1)
    • --check-go-version: Exit if the Go version in use is incompatible with the Delve version (too old or too new). (default true)
    • --headless: Only runs the debug server in headless mode. The server will accept JSON-RPC or DAP client connections.
    • --init: Initialization file executed by the terminal client.
    • -l or --listen: The listening address of the debug server. (Default "127.0.0.1:0")
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
    • --only-same-user: Allow connections only from the same user who started this Delve instance. (default true)

5.dlv dap - Start the headless TCP server that communicates through Debug Adapter Protocol (DAP)

  • Explanation: This command is used to start a headless TCP server and communicate with the DAP client through Debug Adapter Protocol (DAP).

  • Usage: dlv dap [flags], start a headless TCP server, wait for the DAP client to connect, and perform corresponding operations according to the client's startup configuration, such as starting the compiled binary file, building and launch, build and test, replay rr traces, replay core dump files or attach to running processes.

  • Results: The server is in headless mode, requiring a DAP client (such as VS Code) to connect and request to launch a binary or attach to a process. Supported launch modes include launch + exec, launch + debug, launch + test, launch + replay, launch + core, and attach + local.

    • --client-addr: The host:port where the DAP client waits for the DAP server to dial. Use this flag to cause the server to dial to the specified host:port on startup to start a debugging session. The server process will exit at the end of the debugging session.
  • Global flags:

    • --check-go-version: Exit if the Go version in use is incompatible with the Delve version (too old or too new). (default true)
    • --disable-aslr: disables address space randomization.
    • -l or --listen: The listening address of the debug server. (default "127.0.0.1:0")
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
    • --only-same-user: Allow connections only from the same user who started this Delve instance. (default true)

6.dlv debug - Compile the program and start/attach the debugging process

  • Explanation: This command is used to compile the program with optimization disabled and start or attach a debugging process.

  • Usage: dlv debug [package] [flags], by default, Delve will compile the 'main' package in the current directory and start debugging. You can also specify a package name and Delve will compile the package and start a new debugging session.

  • Results: Compile the program and start/attach to the debug process.

    • [package]: The name of the package, defaults to the 'main' package if not specified.
  • Logo:

    • --continue: Continue debugging the process on startup.
    • --output: Output path to the binary file.
    • --tty: TTY for the target program.
  • Global flags:

    • --accept-multiclient: allows the headless server to accept multiple client connections via JSON-RPC or DAP.
    • --allow-non-terminal-interactive: allows interactive Delve sessions without a terminal for standard input, output, and error.
    • --api-version: Select the JSON-RPC API version in headless mode. New clients should use v2. Can be reset via RPCServer.SetApiVersion. View documentation/api/json-rpc/README.md. (default 1)
    • --backend: Backend selection (see ‘dlv help backend’). (default value "default")
    • --build-flags: Build flags passed to the compiler, for example: –build-flags="-tags=integration -mod=vendor -cover -v".
    • --check-go-version: Exit if the Go version in use is incompatible with the Delve version (too old or too new). (default true)
    • --disable-aslr: disables address space randomization.
    • --headless: Only runs the debug server in headless mode. The server will accept JSON-RPC or DAP client connections.
    • --init: Initialization file executed by the terminal client.
    • -l or --listen: The listening address of the debug server. (default "127.0.0.1:0")
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
    • --only-same-user: Allow connections only from the same user who started this Delve instance. (default true)
    • -r or --redirect: specifies redirection rules for the target process (see 'dlv help redirect').
    • --wd: The working directory of the running program.

7.dlv exec - Execute the precompiled binary and start a debugging session

  • Explanation: Execute the precompiled binary and immediately attach to it, starting a new debugging session.

  • Usage: dlv exec <path/to/binary> [flags], this command will cause Delve to execute the binary file and immediately attach to it, starting a new debugging session.

  • Results: Execute the precompiled binary and start a debugging session. It is important to note that if the binary is compiled without optimization disabled, it may make it difficult to debug correctly. It is recommended to use -gcflags="all=-N -l" to compile and debug binaries on Go 1.10 or higher, and -gcflags="-N -l" on earlier Go versions.

    • <path/to/binary>: Path to the binary file.
  • Logo:

    • --continue: Continue debugging the process on startup.
    • --tty: TTY for the target program.
  • Global flags:

    • --accept-multiclient: allows the headless server to accept multiple client connections via JSON-RPC or DAP.
    • --allow-non-terminal-interactive: allows interactive Delve sessions without a terminal for standard input, output, and error.
    • --api-version: Select the JSON-RPC API version in headless mode. New clients should use v2. Can be reset via RPCServer.SetApiVersion. View documentation/api/json-rpc/README.md. (default 1)
    • --backend: Backend selection (see ‘dlv help backend’). (default value "default")
    • --check-go-version: Exit if the Go version in use is incompatible with the Delve version (too old or too new). (default true)
    • --disable-aslr: disables address space randomization.
    • --headless: Only runs the debug server in headless mode. The server will accept JSON-RPC or DAP client connections.
    • --init: Initialization file executed by the terminal client.
    • -l or --listen: The listening address of the debug server. (default "127.0.0.1:0")
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
    • --only-same-user: Allow connections only from the same user who started this Delve instance. (default true)
    • -r or --redirect: specifies redirection rules for the target process (see 'dlv help redirect').
    • --wd: The working directory of the running program.

8.dlv test - Compile the test binary with optimization disabled and start a debugging session

  • Explanation: Compiles the test binary with optimizations disabled and immediately attaches to it, starting a new debugging session.

  • Usage: dlv test [package] [flags], this command allows starting a new debugging session in the context of a unit test. By default, Delve will debug tests in the current directory. You can also specify a package name and Delve will debug tests in that package. Double dashes -- can be used to pass parameters to the test program.

  • Results: Compile the test binary with optimizations disabled and start a debugging session. The test program will be run in a debug server, allowing step-by-step execution, setting breakpoints, etc.

    • [package]: The name of the package, specifying the package in which the test program to be debugged is located. If not specified, defaults to tests in the current directory.
  • Logo:

    • --help or -h: displays help information about the dlv test command.
    • --output: Flag used to specify the output path of the binary file. Specifies the output path of the test binary file.
  • Global flags:

    • --accept-multiclient: allows the headless server to accept multiple client connections via JSON-RPC or DAP.
    • --allow-non-terminal-interactive: allows interactive Delve sessions without a terminal for standard input, output, and error.
    • --api-version: Select the JSON-RPC API version in headless mode. New clients should use v2. Can be reset via RPCServer.SetApiVersion. View documentation/api/json-rpc/README.md. (default 1)
    • --backend: Backend selection (see ‘dlv help backend’). (default value "default")
    • --build-flags: Build flags passed to the compiler. For example: –build-flags="-tags=integration -mod=vendor -cover -v"
    • --check-go-version: Exit if the Go version in use is incompatible with the Delve version (too old or too new). (default true)
    • --disable-aslr: disables address space randomization.
    • --headless: Only runs the debug server in headless mode. The server will accept JSON-RPC or DAP client connections.
    • --init: Initialization file executed by the terminal client.
    • -l or --listen: The listening address of the debug server. (default "127.0.0.1:0")
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
    • --only-same-user: Allow connections only from the same user who started this Delve instance. (default true)
    • -r or --redirect: specifies redirection rules for the target process (see 'dlv help redirect').
    • --wd: The working directory of the running program.

9.dlv trace - Tracking program execution

  • Explanation: Trace program execution. Through the dlv trace command, you can set a trace point to understand the function being executed by the program. The output will be printed to stderr. If you need to only see the output of the trace operation, you can redirect stdout.

  • Usage: dlv trace [package] regexp [flags], this command sets a tracepoint (tracepoint) on each function that matches the provided regular expression, and outputs information when the tracepoint is hit. This is useful when you don't want to start an entire debugging session, but just want to know which functions the process is executing.

    • [package]: The name of the package, specifying the package in which the program to be traced is located. If not specified, defaults to the program in the current directory.
    • regexp: Regular expression used to match the function to be traced.
  • Logo:

    • --ebpf: uses eBPF for tracing (experimental).
    • -e or --exec: The binary to execute and trace.
    • --output: Flag used to specify the output path of the binary file. Specifies the output path of the traced binary file.
    • -p or --pid: The process ID to attach to.
    • -s or --stack: Displays the stack trace at the given depth. (Ignored when using --ebpf)
    • -t or --test: traces the test binary.
    • --timestamp: displays the timestamp in the output.
  • Global flags:

    • --backend: Backend selection (see ‘dlv help backend’). (default value "default")
    • --build-flags: Build flags passed to the compiler. For example: –build-flags="-tags=integration -mod=vendor -cover -v"
    • --check-go-version: Exit if the Go version in use is incompatible with the Delve version (too old or new). (default true)
    • --disable-aslr: disables address space randomization.
    • --log: Enable debug server logging.
    • --log-dest: Write log to the specified file or file descriptor (see 'dlv help log').
    • --log-output: A comma-separated list of components that should generate debug output (see 'dlv help log').
    • -r or --redirect: specifies redirection rules for the target process (see 'dlv help redirect').
    • --wd: The working directory of the running program.

10.dlv version - Check the dlv version number

  • Explanation: Check the dlv version number.
  • Usage method: dlv version.

11.dlv backend - Delve debugger backend selection

  • Explanation: This command is used to select the backend of the Delve debugger and determine how it executes in different environments. Allows selection of the Delve debugger's backend in different environments.
  • Usage: dlv backend, used to specify the backend selection of the Delve debugger.
  • Possible values:
    • default: Use lldb on macOS and the native backend everywhere else.
    • native: Local backend.
    • lldb: Use lldb-server or debugserver.
    • rr: Use Mozilla rr (https://github.com/mozilla/rr).

Twelve,dlv log - Enable Delve debugger logging

  • Explanation: This command is used to enable the logging function of the Delve debugger. Allows you to selectively enable logging for different components of the Delve debugger as needed and write logs to specified locations for better tracking and debugging of events and information that occur during program execution.

  • Usage: Select the component that produces the log by specifying the --log flag and using the --log-output flag.

  • List of available components:

    • debugger: Log debugger commands.
    • gdbwire: Log connections to the gdbserial backend.
    • lldbout: Copies the output of debugserver/lldb to standard output.
    • debuglineerr: Log recoverable errors while reading .debug_line files.
    • rpc: Log all RPC messages.
    • dap: Log all DAP messages.
    • fncall: Record the function call protocol.
    • minidump: Record minidump loading information.
    • stack: Record stack tracer information.
  • Additional options:

    • --log-dest: Used to specify the location of logging. If the argument is a number, it will be interpreted as a file descriptor, otherwise as a file path. This option also redirects "server listening at" messages in headless mode and DAP mode.

13.dlv redirect - Standard file descriptor that controls the target process

  • Explanation: This command is used to control the standard file descriptor of the target process. Allows control of the standard file descriptors of the target process to perform necessary redirection of input and output during debugging.
  • Usage: Use the -r and --tty parameters to control the standard file descriptor of the target process.
    • --ttyThe parameter allows redirecting all standard descriptors to a terminal, with the terminal path as argument to the --tty parameter.
    • -rThe syntax of the parameter is:-r [source:]destination, where source can be one of ‘stdin’, ‘stdout’ or ‘stderr’ and destination is the path to the file. If source is omitted, stdin is used by default.
  • Result: This command can be used to control the standard file descriptor of the target process, thereby redirecting input and output.

Guess you like

Origin blog.csdn.net/weixin_49015143/article/details/134912297