How can I dump all a Go process's stacks without killing it?

前端 未结 2 666
日久生厌
日久生厌 2020-12-30 05:19

A Go process is running. I want to

  1. dump a stack trace for each of its goroutines
  2. from the outside, without depending on anything I add to its source c
2条回答
  •  温柔的废话
    2020-12-30 05:55

    If you are using net/http you can access the goroutines via the debug handlers. If you look at the following source

    http://golang.org/src/pkg/runtime/pprof/pprof.go

    You will see the profile, goroutineProfile, on line 62. This profile writes out via writeGoroutine. If writeGoroutine is called with debug >= 2 then it will write out all goroutines.

    You should be able to curl http://localhost:/debug/pprof/goroutine?debug=2 to get all goroutines dumped. Unfortunately I didn't see any references to signal handlers that would call that code but you can look at references to how pprof makes use of runtime.Stack in the source above to implement this yourself pretty easily.

提交回复
热议问题