'recording' program execution w/clang


I had a question with regards to clang - ie whether or not it was
suited for the following.

Right now, I have a very, very hackish set of perl modules which
instrument my c and c++ code with tracing code, per step via macros.
ie: I start out with -

     fprintf(stderr, "HERE\n");

and end up with

__pf1(fprintf(stderr, "HERE\n"):wink:

The purpose of these macros is to print out the code steps as they are
executing, and to record stack entrances and exits.

Right now, it all works fairly well and is great for debugging, but I
would like to deprecate supporting it in favor of something more
standard. And I would like to implement what I have already
implemented in perl - namely reverse watchpoints - where you give the
value of the variables you are looking for when a piece of c executes,
and it watches ALL assignments and reports on which variables match
these values. This is especially good for untangling large object
hierarchies, and I was hoping to have the same power in C/C++ that I
do in perl.

But as it stands, my hack-of-a-perl-module to do this is reaching the
point of no return - it's not a true parser, so the more I add to it,
the more likely it is to break something elsewhere - and in any case a
c solution would probably be a lot faster.

So - does this sound like something clang could do easily? And, more
to the point, does something like this already exist?

Thanks much,


Hi Ed,

I'm not aware of any clang application that does what you want.

If you want to accomplish this as a source-to-source rewriter, then you can try using the rewriter infrastructure.

See RewriteObjC and RewriteBlocks for a couple applications of our Rewriter infrastructure.

If you dive into these and have any questions, let me know.


Hi Edward,

Is there a specific reason that you want to do this at the source level? This sort of thing may be easier to do at the LLVM IR level. There are several examples of this sort of thing in the lib/transforms/instrumentation directory in LLVM,

If you really have to do this at the source level, I believe Clang can support it, but doing it at the LLVM IR level would give support for C++ today as well as a number of other languages supported by llvm-gcc.



I want to be able to record two versions of the same program and then
do a diff to see in the code where the execution path changes..

For example, say you want to see in mutt what the 'd' key does in the
gui, ie: what parts of the source does it execute.. To do so, record
two sessions, one where you press the d key, and one where you don't.
Do a 'vimdiff' between the two, and you have your answer - without
needing to know the structure of the code at all.

Hence, I'd need actual code, because ultimately I want to use this
with an IDE to trace it back to the original source (and set further
debug points, etc.)


I'm not completely sure about everything you're trying to do, but it
seems like you have a lot of options depending on the result you're
looking for.

The source-to-source rewriter should be able to safely duplicate what
you've been doing in Perl; it's designed precisely for making global
transformations like this on source code.

At the LLVM IL level, you should also be able to create a similar kind
of trace by doing transformations based on debug info; it might be a
bit tricky to write, though.

There might be a much simpler, approach, though: I'm pretty sure you
can script GDB to create a similar trace for a program with debug

Also, http://code.google.com/p/chronomancer/ might be of interest.



I've looked at gdb (in fact that's what made me write the wrapper in
the first place, dbx's support for this) - the problem with it is that
it is exceedingly slow - tracing, for example, a perl executable
running a small perl script in the way i'm talking about takes a
couple of days using the scripting support inside of gdb.

Anyways, chronomancer looks very interesting; I'd love to have the
ability to do reverse tracepoints in C - namely giving a string to the
program to watch for, and seeing which variables are set to a value
with that substring in it, and when they are set to this value.. this
is another great way to debug - one that I already have in perl, would
be even better in C.