Linux 如何使用strace跟踪子进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4053142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to track child process using strace?
提问by projectshave
I used straceto 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.
我曾经strace简要地附加到一个过程。该进程创建了 90 个线程。当我发现有问题的线程时,我不得不乏味地搜索父线程,然后是祖父线程,依此类推,一直到根进程。
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?
有什么技巧或工具可以快速找出哪个线程创建了另一个线程?或者更好的是,打印线程创建树,例如pstree?
采纳答案by Je Rog
strace -fto trace child process that's fork()ed.
strace -f跟踪已fork()编辑的子进程。
回答by stackmate
I can't see an easy way:
我看不到一个简单的方法:
You could use the -ffoption with -o filenameto produce multiple files (one per pid).
您可以使用-ffwith 选项-o filename生成多个文件(每个 pid 一个)。
eg:
例如:
strace -o process_dump -ff ./executable
grep clone process_dump*
that would help you see which parent created what. Maybe that would help you - at least then you could search backwards.
这将帮助您查看哪个父创建了什么。也许这会帮助你 - 至少你可以向后搜索。
回答by artless noise
There is a perl script called strace-graph. Here is a version from github. It is packaged with crosstool-ngversions of compilers. It works for me even used cross platform.
有一个名为strace-graph. 这是来自 github 的一个版本。它与编译器的crosstool-ng版本一起打包。它适用于我甚至使用跨平台。
ARM Linux box.
ARM Linux 盒子。
$ ./strace -f -q -s 100 -o app.trc -p 449
$ tftp -pr app.trc 172.0.0.133
X86_64 Linux box.
X86_64 Linux 盒子。
$ ./strace-graph /srv/tftp/app.trc
(anon)
+-- touch /tmp/ppp.sleep
+-- killall -HUP pppd
+-- amixer set Speaker 70%
+-- amixer set Speaker 70%
+-- amixer set Speaker 70%
+-- amixer set Speaker 70%
+-- amixer set Speaker 50%
+-- amixer set Speaker 70%
`-- amixer set Speaker 50%
The output can be used to help navigate the main trace log.
输出可用于帮助导航主跟踪日志。
回答by Shaboti
To capture traffic for a single process you can use strace, as @stackmate suggested.
要捕获单个进程的流量,您可以使用strace,如@stackmate 建议的那样。
strace -f -e trace=network -s 10000 -p <PID>;
or output it to a file.
或输出到文件。
strace -f -e trace=network -s 10000 -o dumpfile -p <PID>
-ffor all forked process, -sfor string size to print, and -oto dump the output to a file.
-f对于所有分叉进程,-s要打印的字符串大小,以及-o将输出转储到文件。

