我如何转储所有Go进程的堆栈? [英] How can I dump all a Go process's stacks?

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

问题描述

Go进程正在运行。我想从外部为其每个goroutines b
  • 转储堆栈跟踪,而不是从外部转储堆栈跟踪取决于我添加到其源代码的任何内容

  • 而不会将其杀死。

  • 我该如何做到这一点?



    这应该很简单 - 该功能被要求: https://code.google.com/p/go/issues/detail?id=2516 ,并根据该主题的结论实施。那是两年前的事了。但是,问题线程和提交都不包含有关如何调用此功能的任何提示。



    功能请求提到SIGQUIT调用JVM上的相应功能,但这是不是;至少在go1.2上,SIGQUIT会执行#1和#2,但也会杀死进程。



    前一段时间有人问了一个相关问题:如何转储goroutine stacktraces?
    但他们没有明确要求#2或#3 ,没有一个答案符合#2,他们接受了不符合#2的答案。因此,这是一个不同的问题。如果您使用的是net / http,您可以通过调试处理程序访问goroutines。

    如果你看看下面的源代码:


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



    您将会看到 goroutineProfile ,在第62行。这个配置文件通过 writeGoroutine 写出来。如果使用调试> = 2调用writeGoroutine,则会写出所有的goroutine。



    您应该能够 curl http:// localhost:< port> / debug / pprof / goroutine?debug = 2 让所有goroutines倾倒。不幸的是,我没有看到任何可以调用该代码的信号处理程序的引用,但您可以查看pprof如何在上面的源代码中使用 runtime.Stack 来引用这很容易。


    A Go process is running. I want to

    1. dump a stack trace for each of its goroutines
    2. from the outside, without depending on anything I add to its source code
    3. without killing it.

    How can I do that?

    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.

    The feature request mentioned that SIGQUIT invokes the corresponding feature on the JVM, but that's not it; on go1.2 at least, SIGQUIT does #1 and #2 but also kills the process.

    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.

    解决方案

    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

    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.

    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天全站免登陆