Oracle Blog

Random musings from sometimes sunny Ireland

DTrace: digging into exec'd comand line args

Brendan Gregg showed me this trick to get at command line args for commands being exec'ed that are greater than the system 80 char limit. In dtrace trying to output the command args using curpsinfo->pr_psargs hits this limit.

The solution is to catch the exec calls and use the destructive dtrace stop() and system() calls to stop the process if it's hg and dump it's args using the system pargs command.

I also wanted to get the Java executing context for the Hg commands that we where executing from our Mercurial Plugin, which is written in Java. This I could do by catching the process create in dtrace. Catching the exec call and testing for hg, allowed me to ensure that I only tracked hg commands and not other exec'ed commands such as the pargs command.

Here we again dump the stack, but to track the correct exec's we make sure that this exec's parent is java and it's parent is not dtrace. With pargs it's parent is dtrace so this check prevents any recursion. Note: this one liner would also track other exec's from the Java program in it's current form, not just hg.

I've expanded the one liner below to add timings into the script:

#!/usr/sbin/dtrace -ws

/\*

mercurial_plugin_hgcalls.d

Trace hg calls made from Java application (NetBeans IDE), using pargs to