如何转储goroutine stacktraces? [英] How to dump goroutine stacktraces?

查看:140
本文介绍了如何转储goroutine stacktraces?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java背景,并且我喜欢使用QUIT来检查Java线程转储。

如何让Golang打印出所有goroutines堆栈跟踪?

解决方案

要打印当前 goroutine的堆栈跟踪,请使用 PrintStack() from runtime / debug
$ b



PrintStack将标准错误打印到由Stack返回的堆栈轨迹。



例如:

  import(
runtime / debug

...
debug.PrintStack()

要打印所有 goroutines的堆栈跟踪,请使用 Lookup Wri从 runtime / pprof

$ $ p $ func查找(姓名字符串)*个人资料
//查找返回具有给定名称的配置文件
//或者如果不存在这样的配置文件则为零。

func(p * Profile)WriteTo(w io.Writer,debug int)错误
// WriteTo将配置文件的pprof格式的快照写入w。
//如果写入w返回错误,则WriteTo返回该错误。
//否则,WriteTo返回nil。




每个配置文件都有一个唯一的名称。一些配置文件是预定义的:

goroutine - 当前所有goroutines的堆栈跟踪

heap - 所有堆分配的样本

threadcreate - 导致创建新操作系统线程的堆栈跟踪

block - 导致在同步原语上阻塞的堆栈跟踪


例如:

  pprof.Lookup(goroutine)。WriteTo(os.Stdout,1)


I have Java background, and I love to use signal QUIT to inspect Java thread dump.

How to let Golang print out all goroutines stack trace?

解决方案

To print the stack trace for the current goroutine, use PrintStack() from runtime/debug.

PrintStack prints to standard error the stack trace returned by Stack.

For example:

import(
   "runtime/debug"
)
...    
debug.PrintStack()

To print the stack trace for all goroutines use Lookup and WriteTo from runtime/pprof.

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.

Each Profile has a unique name. A few profiles are predefined:

goroutine - stack traces of all current goroutines
heap - a sampling of all heap allocations
threadcreate - stack traces that led to the creation of new OS threads
block - stack traces that led to blocking on synchronization primitives

For example:

pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)

这篇关于如何转储goroutine stacktraces?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆