2013-02-04 8 views
17

Se voglio analizzare un processo multi-thread (di tutti i suoi thread), come dovrei farlo?Stracing per il collegamento a un processo multithreading

So che si può fare strace -f per seguire il processo biforcuto? Ma che ne dici di allegare a un processo che è già multi-thread quando inizio a fare lo strathing? È un modo per dire strace per tracciare tutte le chiamate di sistema di tutti i thread che appartengono a questo processo?

+6

Lo stesso 'strace -f' è sufficiente (ma non so come impedire la traccia * processi figlio * quando si traccia * tutti i thread * in questo modo). –

+3

Posso confermare che 'strace -fp ' si connette a tutti i thread esistenti. – amenthes

risposta

18

Ho appena fatto questo in modo sfacciato, elencando ogni tid per essere rintracciati.

Li potete trovare attraverso ps:

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

e poi, secondo man strace, è possibile collegare a più PID in una sola volta:

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

Dice pid, ma IIRC su Linux il pid e tid condividono lo stesso spazio dei nomi e questo sembrava funzionare:

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

Penso che potrebbe essere il meglio che puoi fare per ora. Ma suppongo che qualcuno potrebbe estendere strace con un flag per l'espansione dei tid. Probabilmente ci sarà ancora una corsa tra il trovare i processi e il collegarli a loro in cui uno di quelli appena iniziati sarebbe mancato. Sarebbe in sintonia con l'avvertenza esistente su 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' prende un elenco separato da virgole, quindi il seguente:' sudo strace -t -p $ (ls/proc/$ (pgrep PROGRAM_TO_TRACE)/task -1 | paste -sd "," -) '. – Aktau

+2

'strace -fp PID' dovrebbe prenderli tutti in ordine – Berkus

0

Come ha risposto a più commenti, strace -fp <pid> mostrerà la traccia di tutte le discussioni di proprietà di quel processo - anche quelli che elaborano già ha generato prima inizia strace.

Problemi correlati