gdb調(diào)試運行時go程序

> go version    ## golang版本
go version go1.12 linux/amd64
> gdb -v    ## gdb版本
GNU gdb (GDB) 8.3
> cat /etc/redhat-release   ## centos版本
CentOS Linux release 7.7.1908 (Core)

golang官方是不推薦使用該方式調(diào)試golang程序的,因為gdb無法準確的診斷golang運行時的數(shù)據(jù)結(jié)構(gòu)和表達式,官方文檔鏈接戳這里

我個人使用的過程中也遇見了很多問題,比如說腳本無法加載導致groutine命令無法使用

程序例子

//測試死鎖例子
package main
import (
    "log"
    "net/http"
    _ "net/http/pprof"
    "os"
    "os/signal"
    "sync"
    "time"
)

func main() {
    //啟動pprof
    go func() {
        http.ListenAndServe(":9999", nil)
    }()
  //模擬死鎖
    mux := sync.Mutex{}
    for j := 0; j < 10; j ++  {
        go func(num int) {
            for  {
                mux.Lock()
                //defer mux.Unlock()
                log.Printf("number %v get lock",num)
                if num != 9 {
                    mux.Unlock()
                }
                time.Sleep(time.Millisecond*100)
            }
        }(j)
    }
    ShutDownHook()
}
// ShutDownHook 關閉勾子事件
func ShutDownHook() {
    quit := make(chan os.Signal, 1)
    signal.Notify(quit, os.Interrupt, os.Kill)
    a := <-quit
    log.Println("close ",a)
}

使用gdb診斷運行程序死鎖狀態(tài)

  • 運行測試程序

    • 源碼運行
      go run test.go
    • 二進制運行
      禁用函數(shù)的內(nèi)聯(lián)調(diào)用和變量注冊,因為這些會使gdb調(diào)試更加困難
      go build -gcflags=all="-N -l" -o test test.go
  • 查找出進程id
    ps aux|grep test

  • 查看進程樹

    > pstree -p <pid>
    test(12611)-+-{test}(12612)
                |-{test}(12613)
                |-{test}(12614)
                |-{test}(12615)
                |-{test}(12616)
                |-{test}(12617)
                |-{test}(12618)
                |-{test}(12619)
    
  • 查看進程堆棧
    pstack可以查看出當前進程下所有線程運行的當前棧信息
    pstack <pid>

  • gdb

    • 連接運行的進程
      不支持golang的python命令腳本,所以groutine這些沒辦法調(diào)試了
      > gdb attach <pid>
        (gdb) ......
        warning: Unsupported auto-load script at offset 0 in section .debug_gdb_scripts of file /usr/local/go/bin/go.
        ......
        (gdb)
      
    • 查看所有線程當前棧信息
      可以查看到當前有8個線程處于鎖狀態(tài),1一個線程處于epollwait狀態(tài),也證明了golang的tcp在centos中使用的是epoll模型
      (gdb) info threads ##查看所有線程,這個是內(nèi)核線程,即golang調(diào)度器模型中的m,不是協(xié)程
        Id   Target Id                              Frame
      * 1    Thread 0x7f8f4fa00740 (LWP 12546) "go" runtime.futex () at /usr/local/go/src/runtime/sys_linux_amd64.s:536
        ......
        9    Thread 0x7f8f49686700 (LWP 12554) "go" runtime.epollwait () at /usr/local/go/src/runtime/sys_linux_amd64.s:675
      
      
      • 查看當前線程詳細棧
      (gdb) info stack
          0  runtime.futex () at /usr/local/go/src/runtime/sys_linux_amd64.s:536
          1  0x000000000042b41b in runtime.futexsleep (addr=0xe474e8 <runtime.m0+328>, val=0, ns=-1) at /usr/local/go/src/runtime/os_linux.go:46
          2  0x000000000040bc71 in runtime.notesleep (n=0xe474e8 <runtime.m0+328>) at /usr/local/go/src/runtime/lock_futex.go:151
          3  0x000000000043372c in runtime.stoplockedm () at /usr/local/go/src/runtime/proc.go:2076
          4  0x0000000000434f3c in runtime.schedule () at /usr/local/go/src/runtime/proc.go:2477
          5  0x00000000004350b1 in runtime.park_m (gp=0xc0000ccf00) at /usr/local/go/src/runtime/proc.go:2605
          6  0x000000000045775b in runtime.mcall () at /usr/local/go/src/runtime/asm_amd64.s:299
          7  0x0000000000457679 in runtime.rt0_go () at /usr/local/go/src/runtime/asm_amd64.s:201
          8  0x0000000000000000 in ?? ()
      
      • 切換線程
      (gdb) thread 9
      [Switching to thread 9 (Thread 0x7f8f49686700 (LWP 12554))]
          0  runtime.epollwait () at /usr/local/go/src/runtime/sys_linux_amd64.s:675
          675     MOVL    AX, ret+24(FP)
      
      • 查看線程中運行函數(shù)信息
      (gdb) info frame 1
      Stack frame at 0x7f8f49685d68:
       rip = 0x42b2d6 in runtime.netpoll (/usr/local/go/src/runtime/netpoll_epoll.go:71); saved rip = 0x4341b5
       called by frame at 0x7f8f49685e08, caller of frame at 0x7f8f49685720
       source language unknown.
       Arglist at 0x7f8f49685718, args: block=true, ~r1=...
       Locals at 0x7f8f49685718, Previous frame is sp is 0x7f8f49685d68
       Saved registers:
        rip at 0x7f8f49685d60
      

待續(xù)......

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容