要打印current goroutine的堆栈跟踪,请使用PrintStack()
from runtime/debug
.
PrintStack将stack返回的堆栈跟踪打印为标准错误.
例如:
import(
"runtime/debug"
)
...
debug.PrintStack()
要打印all个goroutine的堆栈跟踪,请使用runtime/pprof
中的Lookup
和WriteTo
.
func Lookup(name string) *Profile
// Lookup returns the profile with the given name,
// or nil if no such profile exists.
func (p *Profile) WriteTo(w io.Writer, debug int) error
// WriteTo writes a pprof-formatted snapshot of the profile to w.
// If a write to w returns an error, WriteTo returns that error.
// Otherwise, WriteTo returns nil.
每个配置文件都有唯一的名称.预定义了几个配置文件:
所有当前Goroutine的Goroutine堆栈跟踪
堆-所有堆分配的抽样
threadcreate-导致创建新操作系统线程的堆栈跟踪
导致同步基元阻塞的挡路堆栈跟踪
例如:
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)