Linux Stracing 以附加到多线程进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14694582/
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
Stracing to attach to a multi-threaded process
提问by yangsuli
If I want to strace a multi-threaded process (of all of its threads), how should I do it?
如果我想跟踪一个多线程进程(所有线程),我应该怎么做?
I know that one can do strace -f to follow forked process? But how about attaching to a process which is already multi-threaded when I start stracing? Is a way to tell strace to trace all of system calls of all the threads which belong to this process?
我知道可以执行 strace -f 来跟踪分叉过程吗?但是当我开始跟踪时附加到一个已经是多线程的进程怎么样?是一种告诉 strace 跟踪属于该进程的所有线程的所有系统调用的方法吗?
回答by Scott Lamb
I just did this in a kludgy way, by listing each tid to be traced.
我只是以一种笨拙的方式做到了这一点,列出了要跟踪的每个 tid。
You can find them through ps
:
您可以通过ps
以下方式找到它们:
$ ps auxw -T | fgrep program_to_trace
me pid tid1 ...
me pid tid2 ...
me pid tid3 ...
me pid tid4 ...
and then, according to man strace
, you can attach to multiple pids at once:
然后,根据man strace
,您可以一次附加到多个 pid:
-p pid Attach to the process with the process ID pid and begin tracing. The trace may be terminated at any time by a keyboard interrupt
signal (CTRL-C). strace will respond by detaching itself from the traced process(es) leaving it (them) to continue running. Mul‐
tiple -p options can be used to attach to up to 32 processes in addition to command (which is optional if at least one -p option is
given).
It says pid
, but iirc on Linux the pid and tid share the same namespace, and this appeared to work:
它说pid
,但是在 Linux 上的 iirc pid 和 tid 共享相同的命名空间,这似乎有效:
$ strace -f -p tid1 -p tid2 -p tid3 -p tid4
I think that might be the best you can do for now. But I suppose someone could extend strace
with a flag for expanding tids. There would probably still be a race between finding the processes and attaching to them in which a freshly started one would be missed. It'd fit in with the existing caveat about strace -f
:
我认为这可能是你目前能做的最好的事情。但我想有人可以strace
用一个标志来扩展 tids。在寻找流程和依附于它们之间可能仍然存在一场竞赛,在这种竞赛中会错过一个新开始的流程。它符合现有的警告strace -f
:
-f Trace child processes as they are created by currently traced processes as a result of the fork(2) system call.
On non-Linux platforms the new process is attached to as soon as its pid is known (through the return value of fork(2) in the par‐
ent process). This means that such children may run uncontrolled for a while (especially in the case of a vfork(2)), until the par‐
ent is scheduled again to complete its (v)fork(2) call. On Linux the child is traced from its first instruction with no delay. If
the parent process decides to wait(2) for a child that is currently being traced, it is suspended until an appropriate child
process either terminates or incurs a signal that would cause it to terminate (as determined from the child's current signal dispo‐
sition).
On SunOS 4.x the tracing of vforks is accomplished with some dynamic linking trickery.
回答by Blake H
As answered in multiple comments, strace -fp <pid>
will show the trace of all threads owned by that process - even ones that process already has spawned before strace
begins.
正如多条评论中所回答的那样,strace -fp <pid>
将显示该进程拥有的所有线程的踪迹 - 即使是该进程在strace
开始之前已经产生的线程。