itcuihao / blog

blog
6 stars 0 forks source link

panic 怎么定位 #239

Open itcuihao opened 5 years ago

itcuihao commented 5 years ago

panic 之后 recover 了, 但是只打印了 error 信息 如何定位哪个地方panic呢?

itcuihao commented 5 years ago
func PrintStack() {
    var buf [4096]byte
    n := runtime.Stack(buf[:], false)
    errb := buf[:n]
    common.Log.Error("堆栈信息:%v", errb)
    // os.Stdout.Write(buf[:n])
}

这样打印堆栈信息,就能快速定位问题了。

栗子:

runtime error: invalid memory address or nil pointer dereference
goroutine 1 [running]:
main.printStack()
        /home/haoc7/mygo/src/tests/t1.go:108 +0x5b
main.main.func1()
        /home/haoc7/mygo/src/tests/t1.go:46 +0x7b
panic(0x65e6e0, 0x8973c0)
        /usr/local/go/src/runtime/panic.go:513 +0x1b9
main.main()
        /home/haoc7/mygo/src/tests/t1.go:51 +0x59