使用debug.Stack()

package main
 
import (
    "fmt"
    "runtime/debug"
)
 
func test1() {
    test2()
}
 
func test2() {
    test3()
}
 
func test3() {
    // 可以通过 debug.PrintStack() 直接打印,也可以通过 debug.Stack() 方法获取堆栈然后自己打印
    fmt.Printf("%s", debug.Stack())
    debug.PrintStack()
}
 
func main() {
    test1()
}
 
------------------------------------
 
$ go run test_stacktrace.go
 
goroutine 1 [running]:
runtime/debug.Stack(0x0, 0x0, 0x0)
        /usr/lib/golang/src/runtime/debug/stack.go:24 +0x80
main.test3()
        /tmp/test_stacktrace.go:17 +0x24
main.test2()
        /tmp/test_stacktrace.go:13 +0x14
main.test1()
        /tmp/test_stacktrace.go:9 +0x14
main.main()
        /tmp/test_stacktrace.go:22 +0x14
 
goroutine 1 [running]:
runtime/debug.Stack(0x0, 0x0, 0x0)
        /usr/lib/golang/src/runtime/debug/stack.go:24 +0x80
runtime/debug.PrintStack()
        /usr/lib/golang/src/runtime/debug/stack.go:16 +0x18
main.test3()
        /tmp/test_stacktrace.go:18 +0x101
main.test2()
        /tmp/test_stacktrace.go:13 +0x14
main.test1()
        /tmp/test_stacktrace.go:9 +0x14
main.main()
        /tmp/test_stacktrace.go:22 +0x14
 

 

相关文章:

  • 2020-06-11
  • 2018-06-18
  • 2019-06-20
  • 2020-07-26
  • 2019-01-03
  • 2020-04-26
  • 2017-11-28
  • 2020-03-23
猜你喜欢
  • 2020-07-07
  • 2019-06-14
  • 2018-09-19
  • 2019-08-24
  • 2019-11-11
  • 2019-01-04
  • 2018-02-01
  • 2018-01-09
相关资源
相似解决方案