Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does strace connect to an already running process?

Tags:

strace

I do know that strace uses ptrace to do the job,

but it needs to run the target process with TRACE_ME on,

which don't apply for the case of an already running process.

how does it work on an already running process?

like image 445
new_perl Avatar asked Sep 20 '11 08:09

new_perl


People also ask

How does strace attach to a process?

strace -p <PID> ----> To attach a process to strace. "-p" option is for PID of the process. strace -e trace=read,write -p <PID> --> By this you can also trace a process/program for an event, like read and write (in this example).

How does strace call work?

strace works by using the ptrace system call which causes the kernel to halt the program being traced each time it enters or exits the kernel via a system call. The tracing program (in this case strace ) can then inspect the state of the program by using ptrace .

Does strace slow down a process?

According to a performance test conducted by Arnaldo Carvalho de Melo, a senior software engineer at Red Hat, the process traced using strace ran 173 times slower, which is disastrous for a production environment.


1 Answers

strace -p <PID> ----> To attach a process to strace. "-p" option is for PID of the process.

strace -e trace=read,write -p <PID> --> By this you can also trace a process/program for an event, like read and write (in this example). So here it will print all such events that include read and write system calls by the process.

Other such examples

-e trace= network  (Trace all the network related system calls.)  -e trace=signal    (Trace all signal related system calls.)  -e trace=ipc       (Trace all IPC related system calls.)  -e trace=desc      (Trace all file descriptor related system calls.)  -e trace=memory    (Trace all memory mapping related system calls.) 

and many more..

trace is one of the many options you can use with -e option.

Press Ctrl-C to abbort the tracing by strace.

Check HELP section for brief summary on strace by typing strace -h and man page for detailed info.

NOTE: A traced process runs slowly.

like image 198
Prabhat Kumar Singh Avatar answered Sep 22 '22 06:09

Prabhat Kumar Singh