[英]How do I debug a program written in the Go language?
How do I debug a Go program?如何调试 Go 程序? I have been using the Gedit Go IDE , but it doesn't have debugging.
我一直在使用Gedit Go IDE ,但它没有调试功能。 Is there a way to step though my code and inspect memory?
有没有办法遍历我的代码并检查内存? Or am I stuck with print statements?
还是我坚持打印语句? Can I use OutputDebugString ?
我可以使用OutputDebugString吗?
Update : There is now an official page in the docs on Debugging Go Code with GDB .更新:现在在使用 GDB 调试 Go 代码的文档中有一个官方页面。 Much has changed since this answer was written, and several of the limitations listed below have been removed.
自从撰写此答案以来发生了很大变化,并且删除了下面列出的一些限制。 I'm leaving the rest of this answer for posterity, but if you want to debug Go code, follow the link above.
我将这个答案的其余部分留给后人,但如果您想调试 Go 代码,请点击上面的链接。
The Go linkers now emit DWARF debugging symbols that can be interpreted by gdb version 7.x. Go 链接器现在发出可由 gdb 7.x 版解释的 DWARF 调试符号。
Highlight from the blog post linked above:从上面链接的博客文章中突出显示:
You can...你可以...
There are still some inconveniences:仍然存在一些不便:
Some things don't work:有些事情不起作用:
New initiative (started May 2014): derekparker/delve
, now (2021): go-delve/delve :新计划(2014 年 5 月开始):
derekparker/delve
,现在(2021 年): go-delve/delve :
Delve is a Go debugger, written in Go .
Delve 是一个Go 调试器,用 Go 编写。
(mainly for Linux though, OsX support is coming, Windows support(不过主要是针对 Linux,OsX 支持即将到来,Windows 支持
unknownsupported in 2016 )未知
2016 年支持)
- Attach to an already running process
附加到一个已经运行的进程
The debugger can be launched in three ways:
调试器可以通过三种方式启动:
Compile, run, and attach in one step:
一步编译、运行和附加:
$ dlv -run
Provide the name of the program you want to debug, and the debugger will launch it for you.
提供您要调试的程序的名称,调试器将为您启动它。
$ dlv -proc path/to/program
Provide the pid of a currently running process, and the debugger will attach and begin the session.
提供当前正在运行的进程的 pid,调试器将附加并开始会话。
$ sudo dlv -pid 44839
Delve can insert breakpoints via the breakpoint command once inside a debug session, however for ease of debugging, you can also call
runtime.Breakpoint()
and Delve will handle the breakpoint and stop the program at the next source line.Delve 可以在调试会话中一次通过 breakpoint 命令插入断点,但是为了便于调试,您也可以调用
runtime.Breakpoint()
,Delve 将处理断点并在下一个源代码行停止程序。
Another initiative for go debugging session: hopwatch Go 调试会话的另一个举措: hopwatch
Unlike most debuggers, hopwatch requires you to insert function calls at points of interest in your program.
与大多数调试器不同,hopwatch 要求您在程序中感兴趣的点插入函数调用。 At these program locations, you can tell Hopwatch to display variable values and suspend the program (or goroutine).
在这些程序位置,您可以告诉 Hopwatch 显示变量值并挂起程序(或 goroutine)。
Hopwatch uses Websockets to exchange commands between your program and the debugger running in a HTML5 page.Hopwatch 使用 Websockets 在您的程序和运行在 HTML5 页面中的调试器之间交换命令。
(so it is still similar to "print statement" but with a more elegant way to see the result without polluting stdout
and stderr
) (所以它仍然类似于“打印语句”,但以更优雅的方式查看结果而不污染
stdout
和stderr
)
When your program calls the Break function, it sends debug information to the browser page and waits for user interaction.
当您的程序调用 Break 函数时,它会将调试信息发送到浏览器页面并等待用户交互。
Using the functionsDisplay
,Printf
orDump
(go-spew
), you can log information on the browser page.使用
Display
、Printf
或Dump
(go-spew
) 功能,您可以在浏览器页面上记录信息。
On the hopwatch page, the developer can view debug information and choose to resume the execution of the program.在hopwatch页面,开发者可以查看调试信息并选择继续执行程序。
Perhaps some step by step instructions for getting started with GDB would help.也许一些 GDB 入门的分步说明会有所帮助。
I created silly.go containing:我创建了包含:
package main
import "fmt"
func x() {
foo := 5
fmt.Printf("foo: %v\n", foo)
}
func main() {
go x()
fmt.Printf("Done.\n")
}
After running 8g silly.go
and 8l -o silly silly.8
, I can run gdb silly
.运行
8g silly.go
和8l -o silly silly.8
,我可以运行gdb silly
。 (I have "GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2" that as far as I know came with Ubuntu 11.04 32 bit.) (我有“GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2”,据我所知随 Ubuntu 11.04 32 位一起提供。)
I can then type list
, b 7
(short for break 7
), and run
.然后我可以输入
list
, b 7
( break 7
缩写),然后run
。 It stops at line 7, and I can run:它停在第 7 行,我可以运行:
(gdb) p foo
$1 = 5
It would be interesting to see if the Eclipse/CDT debugger and/or DDD would work with Go.看看 Eclipse/CDT 调试器和/或 DDD 是否可以与 Go 一起工作会很有趣。
GDB 7.5正式支持 Go。
Another debug technique being developed (Q4 2014): Go Execution Tracer另一种正在开发的调试技术(2014 年第四季度): Go Execution Tracer
The trace contains
跟踪包含
- events related to goroutine scheduling :
与 goroutine 调度相关的事件:
- a goroutine starts executing on a processor,
goroutine 开始在处理器上执行,
- a goroutine blocks on a synchronization primitive,
一个 goroutine 阻塞在同步原语上,
- a goroutine creates or unblocks another goroutine;
一个 goroutine 创建或解除阻塞另一个 goroutine;
- network-related events :
网络相关事件:
- a goroutine blocks on network IO,
一个 goroutine 阻塞网络 IO,
- a goroutine is unblocked on network IO;
一个 goroutine 在网络 IO 上被解锁;
- syscalls-related events :
系统调用相关事件:
- a goroutine enters into syscall,
一个 goroutine 进入系统调用,
- a goroutine returns from syscall;
一个 goroutine 从 syscall 返回;
- garbage-collector-related events :
垃圾收集器相关事件:
- GC start/stop,
GC 启动/停止,
- concurrent sweep start/stop;
并发扫描开始/停止; and
和
- user events .
用户事件。
By "processor" I mean a logical processor, unit of
GOMAXPROCS
.“处理器”是指逻辑处理器,即
GOMAXPROCS
单元。
Each event contains event id, a precise timestamp, OS thread id, processor id, goroutine id, stack trace and other relevant information (eg unblocked goroutine id).每个事件包含事件 id、精确的时间戳、操作系统线程 id、处理器 id、goroutine id、堆栈跟踪和其他相关信息(例如未阻塞的 goroutine id)。
It's unfortunate, but the best way right now is to use print functions.很不幸,但现在最好的方法是使用打印功能。 The built-in print and println will work, but the functions in fmt will sometimes work better depending on what information you're after.
内置的 print 和 println 可以工作,但是 fmt 中的函数有时会更好地工作,具体取决于您要查找的信息。
获取JetBrains Toolbox ,下载 GoLand,点击编辑器左侧的 ,它会设置一个断点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.