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

如何调试Go程序? 我一直在使用 Gedit Go IDE ,但是它没有调试功能。 有没有办法浏览我的代码并检查内存? 还是我坚持打印声明? 我可以使用 OutputDebugString 吗?</ p> \ n </ div>

展开原文

原文

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?

dousui8263
dousui8263 再简单不过了,只需单击行左侧以在GoLand(Go语言的免费IDE)中设置一个断点。下载Jetbrains工具箱以获取它。
一年多之前 回复
doujiu5464
doujiu5464 delve项目(一个Go调试器,用Go编写)似乎很有希望!请参阅下面的答案
接近 6 年之前 回复

9个回答

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.
dtjo87679
dtjo87679 在Windows上,可以将DWARF调试信息添加到Windows二进制文件中,事实上,最新版本的Go编译器工具链可以执行此操作,从而使GDB调试在Windows上可以进行。
大约 6 年之前 回复



进行调试会话的另一项举措: 跳表</ strong> </ p>


与大多数调试器不同,跳表 要求您在程序感兴趣的点插入函数调用。 在这些程序位置,您可以告诉Hopwatch显示变量值并挂起程序(或goroutine)。

Hopwatch使用Websocket在程序和运行在HTML5页面中的调试器之间交换命令。</ p> \ n </ blockquote>

(因此它仍然类似于“打印语句”,但具有更优雅的方式来查看结果,而不会污染 stdout </ code>和 stderr </ 代码>)</ p>

“ </ p>

< blockquote>

当程序调用Break函数时,它将调试信息发送到浏览器页面并等待用户交互。

使用函数 Display </ code>, Printf </ code>或 Dump </ code>( go-spew </ code>),您可以在浏览器页面上记录信息。

在Hopwatch页面上,开发人员可以查看调试信息。 信息,然后选择继续执行程序。</ p>
</ blockquote>

“ </ p>
</ div>

展开原文

原文

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



有一个名为打开。 不确定效果如何。</ p>
</ div>

展开原文

原文

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



这很不幸,但是目前最好的方法是使用打印功能。 内置的print和println可以工作,但是fmt中的功能有时会更好,具体取决于您要获取的信息。</ p>
</ div>

展开原文

原文

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.



新计划(2014年5月开始): derekparker / delve </ code> </ strong> </ p>


Delve是 Go调试器,编写 在Go </ strong>中。

(尽管主要是针对Linux,OsX支持即将到来,Windows支持 unknown </ del> 2016年受支持)</ p>
</ blockquote>

功能</ h2>

\ n

  • 附加到已经运行的进程</ li>
  • 启动进程并开始调试会话</ li>
  • 设置断点,单步执行,逐步执行功能, 打印变量内容</ li>
    </ ul>
    </ blockquote>

    用法</ h2>


    调试器可以在三个中启动 方式:</ p>

    一步编译,运行和附加:</ p>
    </ blockquote>

      $ dlv -run 
    < / code> </ pre>


    提供专家的名称 </ p>
    </ blockquote>

      $ dlv -proc path / to / program 
    </ code> </ pre>


    提供当前正在运行的进程的pid,调试器将附加并开始会话。</ p>
    </ blockquote>

    < pre> $ sudo dlv -pid 44839
    </ code> </ pre>

    断点</ h2>


    Delve可以通过以下方式插入断点 一旦在调试会话中运行了breakpoint命令,但是为了便于调试,您还可以调用 runtime.Breakpoint()</ code>,Delve将处理该断点并在下一个源代码行中停止程序。</ p >
    </ blockquote>
    </ div>

展开原文

原文

New initiative (started May 2014): derekparker/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.

duanaoshu1989
duanaoshu1989 Delve现在还基于Windows构建。
4 年多之前 回复
du4010
du4010 从我看来,delve是最有前途的调试工作。
大约 5 年之前 回复



GDB 7.5 正式支持Go。</ p>
</ div>

展开原文

原文

GDB 7.5 officially supports Go.

doulai7239
doulai7239 您能否提供更多有关此答案的信息。
大约 8 年之前 回复



正在开发的另一种调试技术(2014年第四季度):”> 执行执行跟踪器</ strong> </ p>


跟踪包含</ p >


  • 与goroutine调度相关的事件</ strong>:


    • goroutine开始在处理器上执行,</ li>

    • 一个goroutine在同步原语上阻塞,</ li>
    • 一个goroutine创建或取消阻塞另一个goroutine; </ li>
      </ ul> </ li>
    • 与网络相关的事件</ strong>:


      • 网络IO上的goroutine块, </ li>
      • goroutine在网络IO上不受阻碍; </ li>
        </ ul> </ li>
      • 与系统调用相关的事件</ strong>:


        • 一个goroutine进入系统调用,< / li>
        • goroutine从syscall返回; </ li>
          </ ul> </ li>
        • 与垃圾收集器相关的事件</ strong>:


          • GC启动/停止, </ li>
          • 并发扫描开始/停止; 和</ li>
            </ ul> </ li>
          • 用户事件</ strong>。 </ li>
            </ ul>

            “处理器”是指逻辑处理器,以 GOMAXPROCS </ code>为单位。

            每个事件都包含事件ID,一个 精确的时间戳,操作系统线程ID,处理器ID,goroutine ID,堆栈跟踪和其他相关信息(例如,畅通的goroutine ID)。</ p>
            </ blockquote>


            ” n> n </ p >> </ F>

展开原文

原文

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



也许有关GDB入门的逐步说明会有所帮助。</ p>

我创建了愚蠢的 .go包含:</ p>

 包main 

import“ fmt”

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

func main(){
go x()
fmt.Printf(” Done。
“)
}
</ code> </ pre >

运行 8g silly.go </ code>和 8l -o silly silly.8 </ code>之后,我可以运行 gdb silly </ code>。 (据我所知,Ubuntu 11.04 32位附带的“ GNU gdb(Ubuntu / Linaro 7.2-1ubuntu11)7.2”。)</ p>

然后我可以键入 list < / code>, b 7 </ code>( break 7 </ code>的缩写)和 run </ code>。 它停在第7行,我可以运行:</ p>

 (gdb)p foo 
$ 1 = 5
</ code> </ pre>

< p>看看Eclipse / CDT调试器和/或DDD是否可以与Go一起使用会很有趣。</ p>
</ div>

展开原文

原文

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
", foo)
}

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

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.



获取 Jetbrains工具箱,下载GoLand,单击编辑器左侧,它将设置一个断点。</ p>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐