Golang GDB调试

1. 调试过程

1.1. 示例代码

package main

import (

    "fmt"

    "time"

)

func counting(c chan<- int) {

    for i := 0; i < 10; i++ {

        time.Sleep(2 * time.Second)

        c <- i

    }

    close(c)

}

func main() {

    msg := "Starting main"

    fmt.Println(msg)

    bus := make(chan int)

    msg = "starting a gofunc"

    go counting(bus)

    for count := range bus {

        fmt.Println("count:", count)

    }

}

1.2. 调试步骤

编译文件,生成可执行文件gdbfile:

 

go build -gcflags "-N -l" gdbfile.go

 

通过gdb命令启动调试:

 

gdb gdbfile

 

启动之后首先看看这个程序是不是可以运行起来,只要输入run命令回车后程序就开始运行,程序正常的话可以看到程序输出如下,和我们在命令行直接执行程序输出是一样的:

 

(gdb) run

Starting program: /home/xiemengjun/gdbfile

Starting main

count: 0

count: 1

count: 2

count: 3

count: 4

count: 5

count: 6

count: 7

count: 8

count: 9

[LWP 2771 exited]

[Inferior 1 (process 2771) exited normally]

 

好了,现在我们已经知道怎么让程序跑起来了,接下来开始给代码设置断点:

 

(gdb) b 23

Breakpoint 1 at 0x400d8d: file /home/xiemengjun/gdbfile.go, line 23.

(gdb) run

Starting program: /home/xiemengjun/gdbfile

Starting main

[New LWP 3284]

[Switching to LWP 3284]

 

Breakpoint 1, main.main () at /home/xiemengjun/gdbfile.go:23

23          fmt.Println("count:", count)

 

上面例子b 23表示在第23行设置了断点,之后输入run开始运行程序。现在程序在前面设置断点的地方停住了,我们需要查看断点相应上下文的源码,输入list就可以看到源码显示从当前停止行的前五行开始:

 

(gdb) list

18      fmt.Println(msg)

19      bus := make(chan int)

20      msg = "starting a gofunc"

21      go counting(bus)

22      for count := range bus {

23          fmt.Println("count:", count)

24      }

25  }

 

现在GDB在运行当前的程序的环境中已经保留了一些有用的调试信息,我们只需打印出相应的变量,查看相应变量的类型及值:

 

(gdb) info locals

count = 0

bus = 0xf840001a50

(gdb) p count

$1 = 0

(gdb) p bus

$2 = (chan int) 0xf840001a50

(gdb) whatis bus

type = chan int

 

接下来该让程序继续往下执行,请继续看下面的命令

 

(gdb) c

Continuing.

count: 0

[New LWP 3303]

[Switching to LWP 3303]

 

Breakpoint 1, main.main () at /home/xiemengjun/gdbfile.go:23

23 fmt.Println("count:", count)

(gdb) c

Continuing.

count: 1

[Switching to LWP 3302]

 

Breakpoint 1, main.main () at /home/xiemengjun/gdbfile.go:23

23 fmt.Println("count:", count)

 

每次输入c之后都会执行一次代码,又跳到下一次for循环,继续打印出来相应的信息。设想目前需要改变上下文相关变量的信息,跳过一些过程,并继续执行下一步,得出修改后想要的结果:

 

(gdb) info locals

count = 2

bus = 0xf840001a50

(gdb) set variable count=9

(gdb) info locals

count = 9

bus = 0xf840001a50

(gdb) c

Continuing.

count: 9

[Switching to LWP 3302]

 

Breakpoint 1, main.main () at /home/xiemengjun/gdbfile.go:23

23 fmt.Println("count:", count)  

 

最后稍微思考一下,前面整个程序运行的过程中到底创建了多少个goroutine,每个goroutine都在做什么:

 

(gdb) info goroutines

* 1 running runtime.gosched

* 2 syscall runtime.entersyscall

3 waiting runtime.gosched

4 runnable runtime.gosched

(gdb) goroutine 1 bt

#0 0x000000000040e33b in runtime.gosched () at /home/xiemengjun/go/src/pkg/runtime/proc.c:927

#1 0x0000000000403091 in runtime.chanrecv (c=void, ep=void, selected=void, received=void)

at /home/xiemengjun/go/src/pkg/runtime/chan.c:327

#2 0x000000000040316f in runtime.chanrecv2 (t=void, c=void)

at /home/xiemengjun/go/src/pkg/runtime/chan.c:420

#3 0x0000000000400d6f in main.main () at /home/xiemengjun/gdbfile.go:22

#4 0x000000000040d0c7 in runtime.main () at /home/xiemengjun/go/src/pkg/runtime/proc.c:244

#5 0x000000000040d16a in schedunlock () at /home/xiemengjun/go/src/pkg/runtime/proc.c:267

#6 0x0000000000000000 in ?? ()

你可能感兴趣的:(Golang GDB调试)