import( "runtime/debug")... debug.PrintStack() ! K; w$ s' K) J' q: p' `: D a
要打印所有goroutine请使用堆栈跟踪Lookup和WriteTofrom runtime/pprof。 & X p. l: e2 ~& W- u
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. $ O" a# _2 ~2 f/ s, h
每个配置文件都有一个独特的名称。预定义了一些配置文件:- ]) n* d$ S' j6 H
goroutine - 所有当前 goroutine 堆栈跟踪$ s: r5 N+ y H" V
heap - 所有堆分配 + O, j8 Z4 S! B5 X样本 threadcreate - 导致创建新操作系统线程 6 J5 |, w- O( a; T/ F" X堆栈跟踪 block - 同步原语阻塞的堆栈跟踪 ) l# T4 J1 m A) E: Z! {9 A. t例如:4 Q( I; R: F$ u8 V* t- |
pprof.Lookup("goroutine").WriteTo(os.Stdout,1) + r' Y2 l8 I9 k. {% Z6 m