如何转储 goroutine 堆栈跟踪?

编程入门 行业动态 更新时间:2024-10-24 00:30:59
本文介绍了如何转储 goroutine 堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有 Java 背景,我喜欢使用信号 QUIT 来检查 Java 线程转储.

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

解决方案

要打印 current goroutine 的堆栈跟踪,请使用 PrintStack() 来自 runtime/debug.

PrintStack 将 Stack 返回的堆栈跟踪打印到标准错误.

例如:

import(运行时/调试")...调试.PrintStack()

要打印所有 goroutine 的堆栈跟踪,请使用 查找和WriteTo 来自 runtime/pprof.

func Lookup(name string) *Profile//查找返回具有给定名称的配置文件,//如果不存在这样的配置文件,则为 nil.func (p *Profile) WriteTo(w io.Writer, debug int) 错误//WriteTo 将配置文件的 pprof 格式的快照写入 w.//如果写入 w 返回错误,WriteTo 返回该错误.//否则,WriteTo 返回 nil.

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

goroutine - 所有当前 goroutine 的堆栈跟踪堆 - 所有堆分配的抽样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 堆栈跟踪?

本文发布于:2023-10-26 19:04:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   goroutine

发布评论

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

>www.elefans.com

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