TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Magic of strace

531 pointsby chadfowlerover 11 years ago

29 comments

rosserover 11 years ago
Small, somewhat nit-picky critique: the man pages for system calls are in section 2. If you want to see the docs for the &quot;read()&quot; syscall, and not the bash builtin &quot;read&quot;, saying &quot;man read&quot; w̶o̶n̶&#x27;̶t̶ may not (see follow-up) do what you expect. Instead, you should say<p><pre><code> man 2 read </code></pre> This should probably be mentioned somewhere.<p>Otherwise, great writeup. Thanks for sharing!<p>(edited)
评论 #7156490 未加载
评论 #7159382 未加载
评论 #7155906 未加载
评论 #7156691 未加载
评论 #7158709 未加载
MiWCryptohnover 11 years ago
Don&#x27;t forget it&#x27;s userspace equiv (strace is syscalls), ltrace. This tracks all lib calls made by process.<p>Under windows, strace is an SSL&#x2F;TLS monitoring tool (also hella useful). It shows payloads passed to CryptoAPI&#x2F;CNG libs so you can easily troubleshoot explicitly encrypted protocols like ldaps. Especially useful if you use client authenticated TLS where is is not possible to use a TLS mitm proxy to snoop the layer 7 data.
评论 #7156160 未加载
评论 #7155858 未加载
评论 #7156264 未加载
leohover 11 years ago
Mac OS X has a suite of tools built on a similar package called dtrace—opensnoop and execsnoop. Gives really nice real time lists of all files opened on the system and all binaries executed, respectively.
评论 #7156089 未加载
tedivmover 11 years ago
So what happened with the Lotus system, and how did strace help?
评论 #7156013 未加载
评论 #7156407 未加载
评论 #7157909 未加载
Argorakover 11 years ago
Thanks for the writeup! strace should definitely be in your toolbox. There is also systemtap, which I like a lot as well. It has some problem on Linux though, especially only being widely supported of Linux &gt; 3.5 if the distro you are using does not ship with patches. Custom userspace probes are a real strong point.<p>I wrote a short article about stap using Rubys probes as an example: <a href="http://www.asquera.de/blog/2014-01-26/stap-and-ruby-2" rel="nofollow">http:&#x2F;&#x2F;www.asquera.de&#x2F;blog&#x2F;2014-01-26&#x2F;stap-and-ruby-2</a>
评论 #7155976 未加载
yreadover 11 years ago
You can use Process monitor <a href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx" rel="nofollow">http:&#x2F;&#x2F;technet.microsoft.com&#x2F;en-us&#x2F;sysinternals&#x2F;bb896645.asp...</a> to see a similar overview of low level activity. You won&#x27;t see all the system calls, you can&#x27;t pipe the output directly, but there is a UI and you don&#x27;t have to look up file descriptors
评论 #7156935 未加载
评论 #7159138 未加载
评论 #7156289 未加载
评论 #7160844 未加载
_kp6zover 11 years ago
If you think strace is useful, wait until you try dtrace.
评论 #7156320 未加载
评论 #7156466 未加载
评论 #7156022 未加载
评论 #7156113 未加载
评论 #7160944 未加载
mrfusionover 11 years ago
Has anyone heard of a program that will take strace (or dtrace) output and create a pretty diagram showing which commands call which commands and which files they read or create?<p>We&#x27;ve got a fairly complicated bioinformatics pipeline that calls about 100 other programs, and creates or reads about 100 different files. I&#x27;d love a way to create a picture of what&#x27;s going on. Which files each program uses, etc.<p>If such a program doesn&#x27;t exist, would that be worth building? Could it be something I could potentially sell?
评论 #7157416 未加载
评论 #7156585 未加载
gopalvover 11 years ago
&quot;perf top -e syscalls:<i>statfs</i>&quot;<p>particularly when you don&#x27;t know which process is calling all the syscalls.<p>Mix &quot;perf record&quot; and &quot;perf trace&quot; &amp; you have the next generation of strace tools.
评论 #7156590 未加载
eldavidoover 11 years ago
I use strace all the time doing ops at Crittercism. Some of the random things it&#x27;s helped with&#x2F;taught me:<p>- allowed exploring forking behavior of daemons, in particular the nitty-gritty of gunicorn&#x27;s prefork behavior, and understanding the rationale behind single- and double-fork daemons generally (very important to understand for job control e.g. writing upstart&#x2F;init.d jobs)<p>- isolated hot reads to memcache in situ, by identifying the socket associated with the memcache connection, and finding which key was read the most by a process (we built better logging after the fact, but sometimes there&#x27;s no substitute for instrumenting prod during tough perf&#x2F;stress problems)<p>- let me explore the behavior of node.js&#x27;s several threads, and find one of them sending &quot;X&quot; over a socket to the other (still not quite sure what this is, some kind of heartbeat&#x2F;clock tick?)<p>- helped understanding &quot;primordial processes&quot; and the exact details of how forking&#x2F;reparenting work on linux<p>It&#x27;s a great tool and one that every ops&#x2F;infrastructure engineer should be familiar with.
评论 #7159759 未加载
评论 #7157933 未加载
dave1010ukover 11 years ago
Quick strace command that I use all the time to see what files a process is opening:<p><pre><code> strace -f &lt;command&gt; 2&gt;&amp;1 | grep ^open </code></pre> Really useful to see what config files something is reading (and the order) or to see what PHP (or similar) files are being included.<p>There&#x27;s normally other ways to do this (eg using a debugger) but sending strace&#x27;s stderr to stdout and piping through grep is useful in so many cases it&#x27;s become a command I use every day or 2.
评论 #7161396 未加载
justincormackover 11 years ago
For OSX you need to use dtruss, for NetBSD and FreeBSD ktrace is what you need.
评论 #7155983 未加载
np422over 11 years ago
Strace is easy to use, commonly available, and very useful in many situations.<p>More modern tools such as dtrace for the solaris and systemtap for linux addresses similar problems but with a broader coverage.
dicroceover 11 years ago
Also check out ltrace... Shows the calls to other libraries the process is making...<p>I&#x27;d also like to point out that a key to using strace successfully is the result column... Programs that fail often make system calls that fail right before they exit... You can often tell what the program is trying and failing to accomplish...
kyaghmourover 11 years ago
In case you&#x27;re curious, this is how ltrace (strace&#x27;s library equivalent) works: <a href="http://www.opersys.com/blog/ltrace-internals-140120" rel="nofollow">http:&#x2F;&#x2F;www.opersys.com&#x2F;blog&#x2F;ltrace-internals-140120</a>
Anthony-Gover 11 years ago
I’ve used strace before to help diagnose issues with buggy software I was using and I thought this was a great article.<p>I just thought I’d let people know that it can be a lot easier to read strace’s output if you read the output log file using Vim as it contains a syntax file which can highlight PIDs, function names, constants, strings, etc. Alternatively, if you don’t want to create an strace log file, you could pipe the output to Vim and it will automatically detect it as being strace output, e.g.<p><pre><code> strace program_name 2&gt;&amp;1 | vim -</code></pre>
csmithukover 11 years ago
strace taught me that glibc never does what you think it does behind the scenes!
memracomover 11 years ago
Totally agree that strace is an awesome tool. I&#x27;ve even used it with Java apps that were behaving wierdly, just attach and see what it is saying to the kernel.
chadfowlerover 11 years ago
Bending to the will of the people, I have appended a conclusion, clarifying the fate of the Lotus Domino server. <a href="http://chadfowler.com/blog/2014/01/26/the-magic-of-strace/" rel="nofollow">http:&#x2F;&#x2F;chadfowler.com&#x2F;blog&#x2F;2014&#x2F;01&#x2F;26&#x2F;the-magic-of-strace&#x2F;</a>
davyjonesover 11 years ago
Just a few hours ago, a newly minted Ubuntu binary was crashing due a library version mismatch. I thought I had updated the shared libraries to point to the new versions. But definitely something was still hooked to the old version. I just couldn&#x27;t figure out how&#x2F;where. ldd wasn&#x27;t of much help because everything was OK according to it. &quot;If only I can get a bit more info when the binary is running and spit out everything before the crash.&quot;<p>Tried my luck with gdb. Sure enough...there was libQt5DBus pointing to the old libs leading to the crash. If you are feeling particularly adventurous, you can step one instruction at a time after starting. Even without debug symbols, there is quite a lot of info that be used while troubleshooting.
peterwwillisover 11 years ago
There&#x27;s a lot of fun to be had with strace. I wrote a tiny perl script that spies on the file descriptors of another process and outputs it to your terminal: <a href="https://github.com/psypete/public-bin/blob/public-bin/src/system/dumpfd.pl" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;psypete&#x2F;public-bin&#x2F;blob&#x2F;public-bin&#x2F;src&#x2F;sy...</a>
kylequestover 11 years ago
Even in the 90s Java decompilers existed, so the &quot;We had no source code&quot; excuse sounds a bit strange :-)
评论 #7156165 未加载
mwcampbellover 11 years ago
It&#x27;s instructive to see how much simpler the strace output for a simple program is when the program is statically linked. Especially if you use an alternative libc like musl (<a href="http://musl-libc.org/" rel="nofollow">http:&#x2F;&#x2F;musl-libc.org&#x2F;</a>).
arca_voragoover 11 years ago
Don&#x27;t forget that sometimes strace is overkill, and similar more easily parsed things can be used instead, for example, &#x2F;usr&#x2F;bin&#x2F;time (vs bash time) has been coming in more and more handy for me.
alexnewmanover 11 years ago
The first level up on java is being able to tell useful things about it via simply straceing it. Once again another win for dtrace.
Derpdiherpover 11 years ago
Useful article. But the background of the blog flickers rather badly, it&#x27;s pretty migraine inducing.
alinspiredover 11 years ago
my favorite use of strace to learn which files (especially config files) are being open by a new daemon&#x2F;tool: strace -f -s1024 2&gt;&amp;1|grep open<p>also remember also useful &#x27;ltrace&#x27; - libraries tracing
LinuxIsNotUniXover 11 years ago
Unix tools? You mean Linux....
CrispEditorover 11 years ago
Please see<p>ftp:&#x2F;&#x2F;86.0.252.89&#x2F;pub&#x2F;release&#x2F;website&#x2F;tools&#x2F;trace-20140126-x86_64-b95.tar.gz<p>This is a tool called ptrace - which does everything that strace does and a lot more. You have working binaries in there, and most of the source - I havent extricated the full build dependencies so it all builds, but this includes extra facilities like reporting summaries of process trees, showing only connections or files, and shlib injection into a target process.<p>If people are interested more on this, contact me at CrispEditor-a.t-gmail.c-o-m
评论 #7155892 未加载
评论 #7155929 未加载