2013-02-04 14 views
17

Wenn ich einen Multithread-Prozess (mit all seinen Threads) erstellen möchte, wie soll ich das tun?Unterstützung zum Verbinden mit einem Multithread-Prozess

Ich weiß, dass man strace -f tun kann, um gegabelten Prozess zu folgen? Aber wie wäre es mit einem Prozess, der bereits multi-threaded ist, wenn ich mit der Verfolgung beginne? Gibt es eine Möglichkeit, strace alle Systemaufrufe aller Threads zu verfolgen, die zu diesem Prozess gehören?

+6

Das gleiche 'strace -f' ist ausreichend (aber ich weiß nicht, wie man die Verfolgung * von untergeordneten Prozessen * verhindert, wenn man * alle Threads * auf diese Weise verfolgt). –

+3

Ich kann bestätigen, dass 'strace -fp ' mit allen vorhandenen Threads verbindet. – amenthes

Antwort

18

Ich habe dies nur auf eine kludgy Weise getan, indem ich jeden zu verfolgenden Tick auflistete.

Sie können sie durch ps finden:

$ ps auxw -T | fgrep program_to_trace 
me pid tid1 ... 
me pid tid2 ... 
me pid tid3 ... 
me pid tid4 ... 

und dann, nach man strace, können Sie auf mehrere PIDs auf einmal anschließen:

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

Es sagt pid, aber iirc auf Linux die pid und tid teilen sich den gleichen Namensraum und dies schien zu funktionieren:

$ strace -f -p tid1 -p tid2 -p tid3 -p tid4 

Ich denke, das könnte das Beste sein, was Sie jetzt tun können. Aber ich nehme an, jemand könnte strace mit einem Flag erweitern Tids erweitern. Es würde wahrscheinlich immer noch ein Wettlauf zwischen dem Finden der Prozesse und dem Anhängen an sie geben, in dem ein frisch begonnener Prozess verpasst würde. Es würde passen mit dem bestehenden Vorbehalt über 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. 
+5

'-p' nimmt eine durch Kommas getrennte Liste, also folgendes:' sudo strace -t -p $ (ls/proc/$ (pgrep PROGRAM_TO_TRACE)/Aufgabe -1 | paste -sd "," -) '. – Aktau

+2

'strace -fp PID' sollte alle aufheben – Berkus

0

Wie in mehr Kommentaren beantwortet, strace -fp <pid> die Spur von allen Threads zeigen von diesem Prozess gehören - auch diejenigen, die bereits hervorgebracht hat Prozess vor strace beginnen.

Verwandte Themen