如何转储goroutine stacktraces?

编程入门 行业动态 更新时间:2024-10-23 10:33:44
本文介绍了如何转储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?

本文发布于:2023-10-26 19:01:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:goroutine   stacktraces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!