前言


gdbgolang


gdb安装


brewgdb


更新brew


brew update


查看是否存在gdb镜像


brew search gdb


安装gdb


brew install gdb


go build编译


go help build


go build -gcflags=all="-N -l" -ldflags=-compressdwarf=false


这里编译用到的参数含义解释如下:


gdb执行


gdb maingdb


$ gdb program

Reading symbols from program...

Loading Go Runtime support.


gdb命令


gdbgdb


gdbhelp [command]


gdb调试


下面,基于一个简单的golang程序进行gdb调试,目的是了解golang程序的初始化流程。


listgo


(gdb) list

1       package main

2       

3       import "fmt"

4       

5       func main() {

6               fmt.Println("hello world")

7       }


info files


(gdb) info files

Symbols from "/Users/guanjian/workspace/go/program/program".

Local exec file:

        `/Users/guanjian/workspace/go/program/program', file type mach-o-x86-64.

        Entry point: 0x10701e0:

        0x0000000001001000 - 0x00000000010cbc53 is .text

        0x00000000010cbc60 - 0x00000000010cbd62 is __TEXT.__symbol_stub1

        0x00000000010cbd80 - 0x0000000001100b19 is __TEXT.__rodata

        0x0000000001100b20 - 0x00000000011012bc is __TEXT.__typelink

        0x00000000011012c0 - 0x0000000001101328 is __TEXT.__itablink

        0x0000000001101328 - 0x0000000001101328 is __TEXT.__gosymtab

        0x0000000001101340 - 0x000000000115dae8 is __TEXT.__gopclntab

        0x000000000115e000 - 0x000000000115e020 is __DATA.__go_buildinfo

        0x000000000115e020 - 0x000000000115e178 is __DATA.__nl_symbol_ptr

        0x000000000115e180 - 0x000000000116c4a4 is __DATA.__noptrdata

        0x000000000116c4c0 - 0x00000000011738f0 is .data

        0x0000000001173900 - 0x00000000011a1170 is .bss

        0x00000000011a1180 - 0x00000000011a62f0 is __DATA.__noptrbss

(gdb)


Entry point: 0x10701e0:break *0x10701e0:


(gdb) break *0x10701e0

Breakpoint 1 at 0x10701e0: file /usr/local/go/src/runtime/rt0_darwin_amd64.s, line 8.


/usr/local/go/src/runtime/rt0_darwin_amd64.srt0.s


$ ls /usr/local/go/src/runtime/rt0_

rt0_aix_ppc64.s        rt0_ios_arm64.s        rt0_netbsd_arm.s

rt0_android_386.s      rt0_js_wasm.s          rt0_netbsd_arm64.s

rt0_android_amd64.s    rt0_linux_386.s        rt0_openbsd_386.s

rt0_android_arm.s      rt0_linux_amd64.s      rt0_openbsd_amd64.s

rt0_android_arm64.s    rt0_linux_arm.s        rt0_openbsd_arm.s

rt0_darwin_amd64.s     rt0_linux_arm64.s      rt0_openbsd_arm64.s

rt0_darwin_arm64.s     rt0_linux_mips64x.s    rt0_openbsd_mips64.s

rt0_dragonfly_amd64.s  rt0_linux_mipsx.s      rt0_plan9_386.s

rt0_freebsd_386.s      rt0_linux_ppc64.s      rt0_plan9_amd64.s

rt0_freebsd_amd64.s    rt0_linux_ppc64le.s    rt0_plan9_arm.s

rt0_freebsd_arm.s      rt0_linux_riscv64.s    rt0_solaris_amd64.s

rt0_freebsd_arm64.s    rt0_linux_s390x.s      rt0_windows_386.s

rt0_illumos_amd64.s    rt0_netbsd_386.s       rt0_windows_amd64.s

rt0_ios_amd64.s        rt0_netbsd_amd64.s     rt0_windows_arm.s


rt0_darwin_amd64.s


% cat -n rt0_darwin_amd64.s

     1 // Copyright 2009 The Go Authors. All rights reserved.

     2 // Use of this source code is governed by a BSD-style

     3 // license that can be found in the LICENSE file.

     4

     5 #include "textflag.h"

     6

     7 TEXT _rt0_amd64_darwin(SB),NOSPLIT,$-8

     8  JMP _rt0_amd64(SB) //这里上一步的断点位置

     9

    10 // When linking with -shared, this symbol is called when the shared library

    11 // is loaded.

    12 TEXT _rt0_amd64_darwin_lib(SB),NOSPLIT,$0

    13  JMP _rt0_amd64_lib(SB)


_rt0_amd64(SB)


Breakpoint 1 at 0x10701e0: file /usr/local/go/src/runtime/rt0_darwin_amd64.s, line 8.


_rt0_amd64runtime/asm_amd64.sruntime·rt0_go(SB)runtime·rt0_go(SB)


TEXT runtime·rt0_go(SB),NOSPLIT,$0

...

// ===== 调用初始化函数 =====

CALL runtime·args(SB)

CALL runtime·osinit(SB)

CALL runtime·schedinit(SB)

// ===== 创建 main goroutine 用于执行 runtime.main =====

MOVQ $runtime·mainPC(SB), AX

PUSHQ AX

PUSHQ $0

CALL runtime·newproc(SB)

POPQ AX

POPQ AX

// ===== 让当前线程开始执行 main goroutine =====

CALL runtime·mstart(SB)

RET

DATA runtime·mainPC+0(SB)/8,$runtime·main(SB)

GLOBL runtime·mainPC(SB),RODATA,$8


$runtime·main(SB)


(gdb) b runtime.main

Breakpoint 1 at 0x103b4c0: file /usr/local/go/src/runtime/proc.go, line 115.


gdb


问题整理


  • 问题1: macOs下gdb签名限制


(gdb) run

Starting program: /xxx/main 

Unable to find Mach task port for process-id 35564: (os/kern) failure (0x5).

(please check gdb is codesigned - see taskgated(8))


root


(gdb) list


(gdb) list

No symbol table is loaded.  Use the "file" command.


go build -ldflags=-compressdwarf=false


  • 问题3: 关于函数调用查找方法


b [file].[method]info breakpoints


cat -n file | head -n [number]


参考


《Go语言学习笔记》雨痕
go build 命令参数详解
使用GDB调试Go语言
mac下如何对gdb进行证书签名
Go使用gdb调试
Debugging Go Code with GDB
使用 GDB 调试 Go 代码
【Go源码阅读】使用GDB调试Go程序
gdb命令
使用gdb添加断点的几种方式
gdb 初次运行卡住 Starting program: [New Thread 0x1103 of process 843]