注意:有几个类似的问题,一般答案是“如果您想跟踪多线程进程,或跟踪子进程,请使用 -f”。这对我没有帮助,因为我已经在使用 -f 了。
我需要跟踪一个java应用程序,它启动一个子进程并定期执行一个脚本。我想查看为脚本提供的命令行参数,由于此处解释起来过于复杂,我无法对脚本进行包装。
我的问题是,跟踪 java 进程,我可以看到clone()
系统调用,但我看不到clone()
.
这是一个最小的测试程序:
package exectest;
import java.io.IOException;
public class Exectest {
public static void main(String[] args) {
for (;;) {
createProcess();
try {
Thread.sleep(15000);
} catch (InterruptedException ex) {
;
}
}
}
static void createProcess() {
String[] params;
params=new String[100000];
for (int i=0; i<100000; i++) {
params[i]=Integer.toString(i);
}
params[0]="echo";
params[1]="test";
try {
Runtime.getRuntime().exec(params);
} catch (IOException ex) {
;
}
}
}
运行它并开始跟踪,会产生:
# /path/to/java -version
java version "1.6.0_81"
Java(TM) SE Runtime Environment (build 1.6.0_81-b08)
Java HotSpot(TM) 64-Bit Server VM (build 20.81-b05, mixed mode)
# /path/to/java -jar /tmp/exectest.jar &
[1] 143280
# strace -tt -e '!futex' -f -p 143280
[pid 143281] 14:16:11.146692 mmap(NULL, 1052672, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7fb0a070d000
[pid 143281] 14:16:11.146767 clone(child_stack=0x7fb0a080cff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7fb0a080d9d0, tls=0x7fb0a080d700, child_tidptr=0x7fb0a080d9d0) = 143981
[pid 143281] 14:16:26.177072 mmap(NULL, 1052672, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7fb0a060c000
[pid 143281] 14:16:26.177150 clone(child_stack=0x7fb0a070bff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7fb0a070c9d0, tls=0x7fb0a070c700, child_tidptr=0x7fb0a070c9d0) = 144187
这会重复,直到我停止该过程。子进程中没有显示任何内容。如图所示,它是143281
执行调用的子线程clone()
,而不是“主”线程。但是,当我尝试跟踪子线程时,strace 确实遵循新进程,但它会在一段时间后使用 SIGTRAP 杀死父进程:
# strace -tt -e '!futex' -f -p 143281
....
[pid 143281] 14:19:26.529955 mprotect(0x7fb0a8107000, 4096, PROT_READ|PROT_WRITE) = 0
[pid 143281] 14:19:26.530049 mprotect(0x7fb0a8108000, 4096, PROT_READ|PROT_WRITE) = 0
[pid 143281] 14:19:26.530084 mmap(NULL, 1052672, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7fb045b6a000
[pid 143281] 14:19:26.530147 clone(Process 146745 attached
child_stack=0x7fb045c69ff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7fb045c6a9d0, tls=0x7fb045c6a700, child_tidptr=0x7fb045c6a9d0) = 146745
[pid 146745] 14:19:26.530275 set_robust_list(0x7fb045c6a9e0, 0x18) = 0
[pid 146745] 14:19:26.530322 gettid() = 146745
....
[pid 143307] 14:19:26.589462 --- SIGTRAP (Trace/breakpoint trap) @ 0 (0) ---
Process 143307 detached
[pid 146746] 14:19:26.589595 write(1, " 8414 8415 8416 8417 8418 8419 8"..., 4096) = 4096
[pid 146746] 14:19:26.590052 write(1, "9233 9234 9235 9236 9237 9238 92"..., 4096 <unfinished ...>
[pid 146358] 14:19:26.590751 +++ killed by SIGTRAP +++
[pid 146746] 14:19:26.590765 <... write resumed> ) = 4096
[pid 146745] 14:19:26.590775 +++ killed by SIGTRAP +++
[pid 146541] 14:19:26.590781 +++ killed by SIGTRAP +++
[pid 146176] 14:19:26.590787 +++ killed by SIGTRAP +++
[pid 145811] 14:19:26.590792 +++ killed by SIGTRAP +++
[pid 145408] 14:19:26.590797 +++ killed by SIGTRAP +++
这种情况发生在带有 RHEL 提供的 strace 的 RHEL6 系统上,以及从 Ubuntu 14.04 LTS 系统复制的 strace 二进制文件上。
是否有我遗漏的东西,我应该设置的任何 strace 选项,或者 strace 中的错误,或者我不知道的一般限制?有任何想法吗?