我如何转储所有Go进程的堆栈而不杀死它? [英] How can I dump all a Go process's stacks without killing it?

查看:86
本文介绍了我如何转储所有Go进程的堆栈而不杀死它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Go进程正在运行.我想

A Go process is running. I want to

  1. 为每个goroutine转储堆栈跟踪
  2. 从外部开始,无需依赖我添加到其源代码中的任何内容
  3. 不杀死它.

我该怎么做?

这应该很容易-请求此功能: https: //code.google.com/p/go/issues/detail?id=2516 ,然后根据该线程的结论进行实施.那是两年多以前了.但是问题线程或提交都没有任何有关如何调用此功能的提示.

This should be easy -- the feature was requested: https://code.google.com/p/go/issues/detail?id=2516 and, according to the conclusion of that thread, implemented. That was over two years ago. But neither the issue thread nor the commit contains any hint as to how to invoke this feature.

功能请求提到 SIGQUIT ,这是JVM接受在那里调用相应功能的信号.但是SIGQUIT并不是答案.至少在go1.2上,SIGQUIT会执行#1和#2的操作,但也会终止该进程.

The feature request mentioned SIGQUIT as the signal the JVM accepts to invoke the corresponding feature there. But SIGQUIT isn't the answer; on go1.2 at least, SIGQUIT does #1 and #2 but also kills the process.

不久前有人在这里问了一个相关问题:如何转储goroutine堆栈跟踪? > 但他们并没有明确要求#2或#3,没有一个答案符合#2,他们接受了一个不符合#2的答案.所以这是一个不同的问题.

Someone asked a related question here a while ago: How to dump goroutine stacktraces? but they didn't clearly ask for #2 or #3, none of the answers meet #2, and they accepted an answer that doesn't meet #2. So this is a different question.

推荐答案

如果使用的是net/http,则可以通过调试处理程序访问goroutine.如果您查看以下来源

If you are using net/http you can access the goroutines via the debug handlers. If you look at the following source

http://golang.org/src/pkg/runtime/pprof/pprof.go

您将在第62行看到配置文件goroutineProfile.此配置文件通过writeGoroutine写出.如果使用debug> = 2调用writeGoroutine,则它将写出所有goroutine.

You will see the profile, goroutineProfile, on line 62. This profile writes out via writeGoroutine. If writeGoroutine is called with debug >= 2 then it will write out all goroutines.

您应该能够curl http://localhost:<port>/debug/pprof/goroutine?debug=2删除所有goroutine.不幸的是,我没有看到任何调用该代码的信号处理程序的引用,但是您可以查看上面的源代码中pprof如何利用runtime.Stack来自己轻松实现的引用.

You should be able to curl http://localhost:<port>/debug/pprof/goroutine?debug=2 to get all goroutines dumped. Unfortunately I didn't see any references to signal handlers that would call that code but you can look at references to how pprof makes use of runtime.Stack in the source above to implement this yourself pretty easily.

这篇关于我如何转储所有Go进程的堆栈而不杀死它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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