如何调试Go语言编写的程序?

发布于 2024-09-12 16:47:31 字数 256 浏览 9 评论 0原文

如何调试 Go 程序?我一直在使用 Gedit Go IDE,但它没有调试功能。有没有办法单步执行我的代码并检查内存?或者我被打印语句困住了?我可以使用 OutputDebugString 吗?

How do I debug a Go program? I have been using the Gedit Go IDE, but it doesn't have debugging. Is there a way to step though my code and inspect memory? Or am I stuck with print statements? Can I use OutputDebugString?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

还如梦归 2024-09-19 16:47:31

更新:文档中现在有一个关于使用 GDB 调试 Go 代码。自编写此答案以来发生了很多变化,并且下面列出的一些限制已被删除。我将把这个答案的其余部分留给后人,但如果你想调试 Go 代码,请点击上面的链接。

Go 链接器现在发出 DWARF 调试符号可由 gdb 版本 7.x 解释。

上面链接的博客文章中的重点内容:

您可以...

  • 在 GDB 版本 7.x 中加载 Go 程序
  • 按行列出所有 Go、C 和汇编源文件(Go 运行时的部分内容是用 C 和汇编语言编写的),
  • 设置逐行断点并单步执行代码,
  • 打印堆栈跟踪并检查堆栈帧,
  • 找到地址并打印大多数变量的内容。

仍然存在一些不便之处:

  • Mac OS X 附带的 GDB 版本 6.x 无法读取发出的 DWARF 代码。我们很乐意接受补丁,以使 DWARF 输出与标准 OS X GDB 兼容,但直到问题得到解决为止。需要下载、构建并安装 GDB 7.x 才能在 OS X 下使用它。源代码可以在 找到http://sourceware.org/gdb/download/。由于 OS X 的特殊性,您需要使用 chgrp procmod 和 chmod g+s 在本地文件系统上安装二进制文件。
  • 名称由包名称限定,并且由于 GDB 不理解 Go 包,因此您必须通过其全名引用每个项目。例如,包 main 中名为 v 的变量必须用单引号引用为“main.v”。这样做的结果是变量和函数名称的制表符补全不起作用。
  • 词汇范围信息有些混乱。如果有多个同名变量,则第 n 个实例将具有“#n”形式的后缀。我们计划修复此问题,但这需要对编译器和链接器之间交换的数据进行一些更改。
  • 切片和字符串变量在运行时库中表示为其底层结构。它们看起来像 {data = 0x2aaaaab3e320, len = 1, cap = 1}。对于切片,您必须取消引用数据指针才能检查元素。

有些东西不起作用:

  • 无法检查通道、函数、接口和映射变量。
  • 只有 Go 变量带有类型信息注释;运行时的 C 变量不是。
  • Windows 和 ARM 二进制文件不包含 DWARF 调试信息,因此无法使用 GDB 检查。

Update: There is now an official page in the docs on Debugging Go Code with GDB. 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.

The Go linkers now emit DWARF debugging symbols that can be interpreted by gdb version 7.x.

Highlight from the blog post linked above:

You can...

  • load a Go program in GDB version 7.x
  • list all Go, C, and assembly source files by line (parts of the Go runtime are written in C and assembly),
  • set breakpoints by line and step through the code,
  • print stack traces and inspect stack frames, and
  • find the addresses and print the contents of most variables.

There are still some inconveniences:

  • The emitted DWARF code is unreadable by the GDB version 6.x that ships with Mac OS X. We would gladly accept patches to make the DWARF output compatible with the standard OS X GDB, but until that’s fixed you’ll need to download, build, and install GDB 7.x to use it under OS X. The source can be found at http://sourceware.org/gdb/download/. Due to the particulars of OS X you’ll need to install the binary on a local file system with chgrp procmod and chmod g+s.
  • Names are qualified with a package name and, as GDB doesn't understand Go packages, you must reference each item by its full name. For example, the variable named v in package main must be referred to as 'main.v', in single quotes. A consequence of this is that tab completion of variable and function names does not work.
  • Lexical scoping information is somewhat obfuscated. If there are multiple variables of the same name, the nth instance will have a suffix of the form ‘#n’. We plan to fix this, but it will require some changes to the data exchanged between the compiler and linker.
  • Slice and string variables are represented as their underlying structure in the runtime library. They will look something like {data = 0x2aaaaab3e320, len = 1, cap = 1}. For slices, you must dereference the data pointer to inspect the elements.

Some things don't work:

  • Channel, function, interface, and map variables cannot be inspected.
  • Only Go variables are annotated with type information; the runtime's C variables are not.
  • Windows and ARM binaries do not contain DWARF debugging information and, as such, cannot be inspected with GDB.
瘫痪情歌 2024-09-19 16:47:31

新举措(2014 年 5 月开始):derekparker/delve< /a>,现在(2021 年):go-delve/delve

Delve 是一个 Go 调试器,用 Go 编写
(主要针对 Linux,OsX 支持即将推出,Windows 支持 未知 2016 年支持)

功能

  • 附加到已运行的进程
  • 启动进程并开始调试会话
  • 设置断点、单步、单步执行函数、打印变量内容

用法

调试器可以通过三种方式启动:

一步完成编译、运行和附加:

$ dlv -run

提供您要调试的程序的名称,调试器将为您启动它。

$ dlv -proc path/to/program

提供当前正在运行的进程的 pid,调试器将连接并开始会话。

$ sudo dlv -pid 44839

断点

Delve 可以在调试会话中通过断点命令插入断点,但是为了便于调试,您也可以调用 runtime.Breakpoint(),Delve 将处理断点并在以下位置停止程序下一个源代码行。

New initiative (started May 2014): derekparker/delve, now (2021): go-delve/delve:

Delve is a Go debugger, written in Go.
(mainly for Linux though, OsX support is coming, Windows support unknown supported in 2016)

Features

  • Attach to an already running process
  • Launch a process and begin debug session
  • Set breakpoints, single step, step over functions, print variable contents

Usage

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.

$ sudo dlv -pid 44839

Breakpoints

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.

薄情伤 2024-09-19 16:47:31

Go 调试会话的另一个举措:hopwatch< /strong>

与大多数调试器不同,hopwatch 要求您在程序中感兴趣的位置插入函数调用。在这些程序位置,您可以告诉 Hopwatch 显示变量值并挂起程序(或 goroutine)。
Hopwatch 使用 Websockets 在您的程序和 HTML5 页面中运行的调试器之间交换命令。

(所以它仍然类似于“打印语句”,但有一种更优雅的方式来查看结果,而不会污染 stdoutstderr

hopwatch

当您的程序调用 Break 函数时,它会将调试信息发送到浏览器页面并等待用户交互。
使用 DisplayPrintfDump (go-spew) 函数,您可以在浏览器页面上记录信息.
在hopwatch页面,开发者可以查看调试信息并选择恢复程序的执行。

带源

Another initiative for go debugging session: hopwatch

Unlike most debuggers, hopwatch requires you to insert function calls at points of interest in your program. At these program locations, you can tell Hopwatch to display variable values and suspend the program (or goroutine).
Hopwatch uses Websockets to exchange commands between your program and the debugger running in a HTML5 page.

(so it is still similar to "print statement" but with a more elegant way to see the result without polluting stdout and stderr)

hopwatch

When your program calls the Break function, it sends debug information to the browser page and waits for user interaction.
Using the functions Display, Printf or Dump (go-spew), you can log information on the browser page.
On the hopwatch page, the developer can view debug information and choose to resume the execution of the program.

with source

ゞ花落谁相伴 2024-09-19 16:47:31

也许一些有关 GDB 入门的分步说明会有所帮助。

我创建的silly.go包含:

package main

import "fmt"

func x() {
    foo := 5
    fmt.Printf("foo: %v\n", foo)
}

func main() {
    go x()
    fmt.Printf("Done.\n")
}

运行8gsilly.go8l -osillysilly.8后,我可以运行gdbsilly。 (据我所知,我有“GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2”,它随 Ubuntu 11.04 32 位一起提供。)

然后我可以输入 list, b 7(break 7 的缩写)和 run。它停在第 7 行,我可以运行:

(gdb) p foo
$1 = 5

看看 Eclipse/CDT 调试器和/或 DDD 是否可以与 Go 一起工作会很有趣。

Perhaps some step by step instructions for getting started with GDB would help.

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. (I have "GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2" that as far as I know came with Ubuntu 11.04 32 bit.)

I can then type list, b 7 (short for break 7), and run. It stops at line 7, and I can run:

(gdb) p foo
$1 = 5

It would be interesting to see if the Eclipse/CDT debugger and/or DDD would work with Go.

风吹短裙飘 2024-09-19 16:47:31

GDB 7.5 正式支持 Go。

GDB 7.5 officially supports Go.

花开浅夏 2024-09-19 16:47:31

有一个名为 ogle 的实验性调试器包。不确定它的效果如何。

There is an experimental debugger package called ogle. Not sure how well it works.

仅此而已 2024-09-19 16:47:31

不幸的是,但目前最好的方法是使用打印功能。内置的 print 和 println 可以工作,但 fmt 中的函数有时会工作得更好,具体取决于您要查找的信息。

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.

眼眸里的快感 2024-09-19 16:47:31

另一种正在开发的调试技术(2014 年第 4 季度):Go Execution Tracer

跟踪包含

  • 与 goroutine 调度相关的事件
    • goroutine 开始在处理器上执行,
    • goroutine 在同步原语上阻塞,
    • 一个 goroutine 创建或解除阻塞另一个 goroutine;
  • 网络相关事件
    • goroutine 阻塞网络 IO,
    • goroutine 在网络 IO 上被解除阻塞;
  • 系统调用相关事件
    • goroutine 进入系统调用,
    • goroutine 从系统调用返回;
  • 与垃圾收集器相关的事件
    • GC 启动/停止,
    • 并发扫描开始/停止;和
  • 用户事件

“处理器”是指逻辑处理器,GOMAXPROCS 单元。
每个事件包含事件id、精确的时间戳、操作系统线程id、处理器id、goroutine id、堆栈跟踪和其他相关信息(例如未阻塞的goroutine id)。


https://lh5.googleusercontent.com/w0znUT_0_xbipG_UlQE5Uc4PbC8Mw1duHRLg_AKTOS4iS6emOD6jnQvSDACybOfCbuSqr2ulkxULXGOBQpZ2IejPHW_8NHufqmn8q5 u-fF_MSMCEgu6FwLNtMvowbq74nA

Another debug technique being developed (Q4 2014): Go Execution Tracer

The trace contains

  • events related to goroutine scheduling:
    • a goroutine starts executing on a processor,
    • a goroutine blocks on a synchronization primitive,
    • a goroutine creates or unblocks another goroutine;
  • network-related events:
    • a goroutine blocks on network IO,
    • a goroutine is unblocked on network IO;
  • syscalls-related events:
    • a goroutine enters into syscall,
    • a goroutine returns from syscall;
  • garbage-collector-related events:
    • GC start/stop,
    • concurrent sweep start/stop; and
  • user events.

By "processor" I mean a logical processor, unit of GOMAXPROCS.
Each event contains event id, a precise timestamp, OS thread id, processor id, goroutine id, stack trace and other relevant information (e.g. unblocked goroutine id).


https://lh5.googleusercontent.com/w0znUT_0_xbipG_UlQE5Uc4PbC8Mw1duHRLg_AKTOS4iS6emOD6jnQvSDACybOfCbuSqr2ulkxULXGOBQpZ2IejPHW_8NHufqmn8q5u-fF_MSMCEgu6FwLNtMvowbq74nA

﹎☆浅夏丿初晴 2024-09-19 16:47:31

获取JetBrains Toolbox,下载GoLand,点击编辑器左侧,即可会设置一个断点。

Get the JetBrains Toolbox, download GoLand, click on the left side of the editor, and it'll set a breakpoint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文