如何使用strace跟踪子进程?

发布于 2024-09-29 14:12:51 字数 175 浏览 8 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衾梦 2024-10-06 14:12:51

strace -f 跟踪经过 fork() 处理的子进程。

strace -f to trace child process that's fork()ed.

來不及說愛妳 2024-10-06 14:12:51

我看不到一种简单的方法:

您可以使用 -ff 选项和 -o filename 来生成多个文件(每个 pid 一个)。

例如:

strace -o process_dump -ff ./executable
grep clone process_dump*

这将帮助您了解哪个父母创建了什么。也许这会对你有帮助——至少这样你就可以向后搜索。

I can't see an easy way:

You could use the -ff option with -o filename to produce multiple files (one per 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.

我为君王 2024-10-06 14:12:51

有一个名为 strace-graph 的 Perl 脚本。这是来自 github 的版本。它与 crosstool-ng 版本的编译器一起打包。它甚至对我来说也适用跨平台。

还有一个更现代 python3 脚本。它可以通过 pip3 install strace-process-tree 安装在 Debian/Ubuntu(以及许多其他系统)上。

下面捕获数据的过程对于两者来说是相同的(但使用 strace-process-tree 来创建图表)。 谢谢Jan Tojnar

看来perl脚本可能会受到位腐烂的影响。对于使用较旧的 busybox 类型 ps 的跨平台解决方案,它可能会起作用。


ARM Linux 盒子。

$ ./strace -f -q -s 100 -o app.trc -p 449
$ tftp -pr app.trc 172.0.0.133

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%

输出可用于帮助导航主跟踪日志。


strace 进程树示例

$ strace -f -q -o app.trc sh -c 'for i in `seq 0 10` ; do /usr/bin/echo "Hello There"; done;'
$ strace-process-tree -A -C app.trc
356 sh -c 'for i in `seq 0 10` ; do /usr/bi...'
  |-357 seq 0 10
  |-358 /usr/bin/echo "Hello There"
  |-359 /usr/bin/echo "Hello There"
  |-360 /usr/bin/echo "Hello There"
  |-361 /usr/bin/echo "Hello There"
  |-362 /usr/bin/echo "Hello There"
  |-363 /usr/bin/echo "Hello There"
  |-364 /usr/bin/echo "Hello There"
  |-365 /usr/bin/echo "Hello There"
  |-366 /usr/bin/echo "Hello There"
  |-367 /usr/bin/echo "Hello There"
  `-368 /usr/bin/echo "Hello There"

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 Tojnar

It 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.

$ ./strace -f -q -s 100 -o app.trc -p 449
$ tftp -pr app.trc 172.0.0.133

X86_64 Linux box.

$ ./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.


strace-process-tree example

$ strace -f -q -o app.trc sh -c 'for i in `seq 0 10` ; do /usr/bin/echo "Hello There"; done;'
$ strace-process-tree -A -C app.trc
356 sh -c 'for i in `seq 0 10` ; do /usr/bi...'
  |-357 seq 0 10
  |-358 /usr/bin/echo "Hello There"
  |-359 /usr/bin/echo "Hello There"
  |-360 /usr/bin/echo "Hello There"
  |-361 /usr/bin/echo "Hello There"
  |-362 /usr/bin/echo "Hello There"
  |-363 /usr/bin/echo "Hello There"
  |-364 /usr/bin/echo "Hello There"
  |-365 /usr/bin/echo "Hello There"
  |-366 /usr/bin/echo "Hello There"
  |-367 /usr/bin/echo "Hello There"
  `-368 /usr/bin/echo "Hello There"
时光是把杀猪刀 2024-10-06 14:12:51

要捕获单个进程的流量,您可以使用 strace,如 @stackmate 建议的那样。

strace -f -e trace=network -s 10000 -p <PID>;

或将其输出到文件中。

strace -f -e trace=network -s 10000 -o dumpfile -p <PID>

-f 用于所有分叉进程,-s 用于打印字符串大小,-o 用于将输出转储到文件。

To capture traffic for a single process you can use strace, as @stackmate suggested.

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>

-f for all forked process, -s for string size to print, and -o to dump the output to a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文