On 12/26/23, Thomas Schmitt <scdbac...@gmx.net> wrote: > man strace says: > -o filename Write the trace output to the file filename rather > than to stderr. > > So strace normally directs its output to stderr. That's file descriptor 2. > You can redirect it by the "2>file" gesture of the shell: > > $ strace echo hello 2>file > hello > $ cat file > execve("/bin/echo", ["echo"], [/* 35 vars */]) = 0 > brk(0) = 0x13ba000 > ... > exit_group(0) = ? > +++ exited with 0 +++ > > Or you may use the mentioned -o option which will keep the tracee's stderr > out of the file: > > $ strace -o file echo hello > hello > $ cat file > ...
three questions: 1) how do you set up the process to be straced as a parameter? Something like: prx="echo hello" logfile="file.txt" # strace "${prx}" 2>"${logfile}" ls -l "${logfile}"; wc -l "${logfile}"; cat "${logfile}" 2) how do you know for sure that you are stracing the same process that you are running and is logging some data? I had read up from the links that I included that there are ways to make the shell spit the process number before a process is run, but how do you then insert the stracing segment in the one liner in order the use that process id before the process is actually run? The way I understand the PID options of strace: strace --help ... -p PID, --attach=PID: trace process with process id PID, may be repeated --tips[=[[id:]ID][,[format:]FORMAT]]: show strace tips, tricks, and tweaks on exit id: non-negative integer or random; default is random format: none, compact, full; default is compact it seems to be attaching to some already running deamon or server process. How do you make it swallow a process that would go in one step/moment? 3) I have noticed that strace output is not totally predictable, so "parsing" is not that safe, straightforward prx="echo hello" logfile="file.txt" # strace "${prx}" 2>"${logfile}" ls -l "${logfile}"; wc -l "${logfile}"; cat "${logfile}" $ strace file strace.wlog.txt 2>&1 | tail -n 5 write(1, "strace.wlog.txt: ASCII text, wit"..., 56strace.wlog.txt: ASCII text, with very long lines (348) ) = 56 munmap(0x7f127f083000, 8281024) = 0 exit_group(0) = ? +++ exited with 0 +++ $ strace wget --help 2>&1 | tail -n 5 Email bug reports, questions, discussions to <bug-w...@gnu.org> and/or open issues at https://savannah.gnu.org/bugs/?func=additem&group=wget. ) = 956 exit_group(0) = ? +++ exited with 0 +++ $ Notice the diffing "munmap(0x7f127f083000, 8281024) = 0" line which I think comes from strace. lbrtchc