strace on Mac OS X Leopard

One of the most important tools for sysadmins and programmers working in the Linux/BSD environment is called strace. As it took me some time to find out where is “strace for Mac”, I thought it would be worth documenting here…

Making a long story short: in Tiger it was called ktrace, in Leopard it’s called dtrace, but it’s simpler if you just call dtruss.

Here are some examples directly from the dtruss man page:

dtruss df -h     # run and examine the "df -h" command

dtruss -p 1871   # examine PID 1871

dtruss -n tar    # examine all processes called "tar"

For a more “real-life” example, please see this article: Why DTrace Makes Leopard a Must-Have Upgrade — you’ll even learn how to prevent Time Machine from consuming all your CPU.

UPDATE: Just after posting this I discovered that:

1. There’s a really cool GUI for DTrace called Instruments. After playing with it for just a few minutes I was able to detect that it was psyco that was causing Python 2.4 to segfault when running web2ldap. Now back to a little more tinkering to discover why

2. Leopard/DTrace provides one command that I always wanted: iotop, to show which processes are responsible for the disk I/O (more dtrace commands here). Now to the question: is there a Linux version? The answer is: yes, and it’s written in Python (and requires a kernel >= 2.6.20).

7 Responses to “strace on Mac OS X Leopard”

  1. Acácio Says:

    Hey!

    I’ve never found the correspondent for the “sample process” thing of activity monitor on terminal..

    That’s pretty nice :)

  2. Rodrigo Wanderley Says:

    Is there a relation between this DTrace and Sun OS’s DTrace? If there is I will really envy your OS.

  3. Rodrigo Wanderley Says:

    You sun of a gun! It is the same:

    http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/dtrace.1.html#//apple_ref/doc/man/1/dtrace

    You should be happy. :D

  4. Andy Sharp Says:

    $ sudo iotop
    Tracing… Please wait.
    dtrace: error on enabled probe ID 9 (ID 21762: profile:::tick-1sec): divide-by-zero in action #2 at DIF offset 28

    One heck of a great command.

  5. humberto Says:

    @Andy: it used to work here, but seems like one of the last software updates broke it… I guarantee you it’s not my fault. ;)

  6. Heikki Naski Says:

    Excellent post. First hit on googling “mac strace” and iotop is something I’ve also been yearning.

    Many thanks.

  7. stargood Says:

    Thanks for documenting this. This page is the number 1 google hit for “strace mac”.

Leave a Reply