GDB调试
This commit is contained in:
111
11.2.md
111
11.2.md
@@ -17,8 +17,119 @@ GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。使
|
|||||||
2. 传递-gcflags "-N -l" 参数,这样可以忽略Go内部做的一些优化,聚合变量和函数等优化,这样对于GDB调试来说非常困难,所以在编译的时候加入者两个参数避免这些优化。
|
2. 传递-gcflags "-N -l" 参数,这样可以忽略Go内部做的一些优化,聚合变量和函数等优化,这样对于GDB调试来说非常困难,所以在编译的时候加入者两个参数避免这些优化。
|
||||||
|
|
||||||
## 常用命令
|
## 常用命令
|
||||||
|
GDB的一些常用命令如下所示
|
||||||
|
|
||||||
|
- list
|
||||||
|
|
||||||
|
简写命令`l`,用来显示源代码,默认显示十行代码,后面可以带上参数显示的具体行,例如:`list 15`,显示十行代码,其中15行在显示的十行里面的中间,如下所示。
|
||||||
|
|
||||||
|
10 time.Sleep(2 * time.Second)
|
||||||
|
11 c <- i
|
||||||
|
12 }
|
||||||
|
13 close(c)
|
||||||
|
14 }
|
||||||
|
15
|
||||||
|
16 func main() {
|
||||||
|
17 msg := "Starting main"
|
||||||
|
18 fmt.Println(msg)
|
||||||
|
19 bus := make(chan int)
|
||||||
|
|
||||||
|
|
||||||
|
- break
|
||||||
|
|
||||||
|
简写命令`b`,用来设置断点,后面跟上参数设置断点的行数,例如`b 10`在第十行设置断点。
|
||||||
|
- delete
|
||||||
|
|
||||||
|
简写命令`d`,用来删除断点,后面跟上断点设置的序号,这个序号可以通过`info breakpoints`获取相应的设置的断点序号,如下是显示的设置断点序号。
|
||||||
|
|
||||||
|
Num Type Disp Enb Address What
|
||||||
|
2 breakpoint keep y 0x0000000000400dc3 in main.main at /home/xiemengjun/gdb.go:23
|
||||||
|
breakpoint already hit 1 time
|
||||||
|
|
||||||
|
- backtrace
|
||||||
|
|
||||||
|
简写命令`bt`,用来打印执行的代码过程,如下所示:
|
||||||
|
|
||||||
|
#0 main.main () at /home/xiemengjun/gdb.go:23
|
||||||
|
#1 0x000000000040d61e in runtime.main () at /home/xiemengjun/go/src/pkg/runtime/proc.c:244
|
||||||
|
#2 0x000000000040d6c1 in schedunlock () at /home/xiemengjun/go/src/pkg/runtime/proc.c:267
|
||||||
|
#3 0x0000000000000000 in ?? ()
|
||||||
|
- info
|
||||||
|
|
||||||
|
info命令用来显示信息,后面有几种参数,我们常用的有如下几种:
|
||||||
|
|
||||||
|
- `info locals`
|
||||||
|
|
||||||
|
显示当前执行的程序中的变量值
|
||||||
|
- `info breakpoints`
|
||||||
|
|
||||||
|
显示当前设置的断点列表
|
||||||
|
- `info goroutines`
|
||||||
|
|
||||||
|
显示当前执行的goroutine列表,如下代码所示,带*的表示当前执行的
|
||||||
|
|
||||||
|
* 1 running runtime.gosched
|
||||||
|
* 2 syscall runtime.entersyscall
|
||||||
|
3 waiting runtime.gosched
|
||||||
|
4 runnable runtime.gosched
|
||||||
|
- print
|
||||||
|
|
||||||
|
简写命令`p`,用来打印变量或者其他信息,后面跟上需要打印的变量名,当然还有一些很有用的函数$len()和$cap(),用来返回当前string、slices或者maps的长度和容量。
|
||||||
|
|
||||||
|
- whatis
|
||||||
|
|
||||||
|
用来显示当前变量的类型,后面跟上变量名,例如`whatis msg`,显示如下:
|
||||||
|
|
||||||
|
type = struct string
|
||||||
|
- next
|
||||||
|
|
||||||
|
简写命令`n`,用来单步调试,跳到下一步,当有断点之后,可以输入`n`跳转到下一步继续执行
|
||||||
|
- countinue
|
||||||
|
|
||||||
|
简称命令`c`,用来跳出当前断点处,后面可以跟参数N,跳过多少次断点
|
||||||
|
|
||||||
|
- set variable
|
||||||
|
|
||||||
|
该命令用来改变运行过程中的变量值,格式如:`set variable <var>=<value>`
|
||||||
## 调试过程
|
## 调试过程
|
||||||
|
我们通过下面这个代码来演示如何通过GDB来调试Go程序,下面是将要演示的代码:
|
||||||
|
|
||||||
|
//文件名gdbfile.go
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
编译文件,生成可执行文件gdbfile:
|
||||||
|
|
||||||
|
go build -gcflags "-N -l" -ldflags "-s" gdbfile.go
|
||||||
|
|
||||||
|
通过gdb命令启动调试:
|
||||||
|
|
||||||
|
gdb gdbfile
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## links
|
## links
|
||||||
* [目录](<preface.md>)
|
* [目录](<preface.md>)
|
||||||
|
|||||||
Reference in New Issue
Block a user