英文:
Get the stack of the "parent" goroutine along with that of the "child"
问题
在Go语言中,有没有一种方法可以获取所有goroutine的堆栈跟踪,以及“父” goroutine的堆栈跟踪(这里的“父”指的是执行调用go foo()
启动相关goroutine的goroutine)?可能需要打开一些调试标志。我提出这个问题的背景是,我有一个连接泄漏的问题,并且注意到有许多goroutine在awaitDone
(在sql包中)上被阻塞,而这些goroutine是在创建连接时生成的。
英文:
Is there a way in go, possibly with some debug flag switched on, to obtain a dump of the stack traces of all goroutines along with the stack traces of the "parent" goroutines (using "parent" here to mean the goroutine that executed the call to go foo()
that launched the goroutine in question). The context for this question is that I have a connection leak and noticed there are many goroutines blocked on awaitDone
(in the sql package), and these goroutines were spawned where the connections were created.
答案1
得分: 1
runtime.Stack()
将提供运行时所知的所有堆栈信息。
通过阅读输出,您可以看到,对于Goroutine,它们被设计为不包含其祖先的信息。
然而,在堆栈输出中,您可以访问Goroutine创建的位置。您可以在技术上将其与其他Goroutine的堆栈输出进行自动交叉匹配,以部分地和不准确地推断拓扑结构。
据我所知,这是许多Golang IDE解决调试问题的方式。除非Golang核心团队改变对Goroutine元数据的看法,我认为这是可能的最佳选择。
英文:
runtime.Stack()
will give you all the stack information that the runtime is aware of.
Reading the output you can see, for Goroutines, they are designed to contain no information of its ancestors.
However, in the stack output, you do have access to where a goroutine was created. You can, technically, auto cross match that with other Goroutine's stack output to deduce the topology partially and inaccurately.
AFAIK, this is how many Golang IDEs tackle the debugging problem. Unless Golang core team changes its mind on Goroutine's metadata, I think this is the best option possible.
答案2
得分: -2
如果我理解你的问题正确的话,你可以使用runtime包来打印/输出你想要的信息。
请查看https://golang.org/pkg/runtime/#Caller和skip参数。还有帧文档:https://golang.org/pkg/runtime/#Frames
这是你想要的吗?
英文:
If I understand what you're asking, you can probably print/output the info you want using the runtime package.
Take a look at
https://golang.org/pkg/runtime/#Caller and the skip paramter. Plus the frame docs: https://golang.org/pkg/runtime/#Frames
Is that what you're after?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论