如何使用strace跟踪子进程?
我使用 strace
简单地附加到一个进程。该进程创建了 90 个线程。当我找到有问题的线程时,我必须繁琐地搜索父线程,然后是祖父线程,依此类推,一直到根进程。
是否有技巧或工具可以快速找出哪个线程创建了另一个线程?或者更好的是,像 pstree
一样打印线程创建树?
I used strace
to attach to a process briefly. The process created 90 threads. When I found the offending thread, I had to tediously search for the parent thread, then the grandparent thread, and so on all the way to the root process.
Is there a trick or tool to quickly figure out which thread created another? Or better yet, print the tree of thread creations like pstree
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
strace -f
跟踪经过fork()
处理的子进程。strace -f
to trace child process that'sfork()
ed.我看不到一种简单的方法:
您可以使用
-ff
选项和-o filename
来生成多个文件(每个 pid 一个)。例如:
这将帮助您了解哪个父母创建了什么。也许这会对你有帮助——至少这样你就可以向后搜索。
I can't see an easy way:
You could use the
-ff
option with-o filename
to produce multiple files (one per pid).eg:
that would help you see which parent created what. Maybe that would help you - at least then you could search backwards.
有一个名为
strace-graph
的 Perl 脚本。这是来自 github 的版本。它与 crosstool-ng 版本的编译器一起打包。它甚至对我来说也适用跨平台。还有一个更现代 python3 脚本。它可以通过
pip3 install strace-process-tree
安装在 Debian/Ubuntu(以及许多其他系统)上。下面捕获数据的过程对于两者来说是相同的(但使用 strace-process-tree 来创建图表)。 谢谢Jan Tojnar
看来perl脚本可能会受到位腐烂的影响。对于使用较旧的 busybox 类型
ps
的跨平台解决方案,它可能会起作用。ARM Linux 盒子。
X86_64 Linux 盒子。
输出可用于帮助导航主跟踪日志。
strace 进程树示例
There is a perl script called
strace-graph
. Here is a version from github. It is packaged with crosstool-ng versions of compilers. It works for me even used cross platform.There is also a more modern python3 script. It can be installed on Debian/Ubuntu (and many other systems) with
pip3 install strace-process-tree
.The process below to capture data is the same for both (but use
strace-process-tree
instead to create the graph). Thanks Jan TojnarIt seems the perl script may suffer from bit rot. For a cross platform solution with older busybox type
ps
, it may work.ARM Linux box.
X86_64 Linux box.
The output can be used to help navigate the main trace log.
strace-process-tree example
要捕获单个进程的流量,您可以使用
strace
,如 @stackmate 建议的那样。或将其输出到文件中。
-f
用于所有分叉进程,-s
用于打印字符串大小,-o
用于将输出转储到文件。To capture traffic for a single process you can use
strace
, as @stackmate suggested.or output it to a file.
-f
for all forked process,-s
for string size to print, and-o
to dump the output to a file.