Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic instrumentation with Clang

Tags:

c

clang

I'm trying to get up to speed on using Clang by doing a bit of dynamic code instrumentation with C (and maybe C++) where I take a source file and generate an instrumented output. I'd like to add a function call at the beginning of any block and also change all boolean expressions to call some function so I can track that too. For example:

foo = a && (b || c);

would become something like:

foo = EXPR_AND(a, EXPR_OR(b, c));

and thus I can track all combinations of conditions that occur.

I assume using a RecursiveASTVisitor would be the best approach, but is there an easy way to output the C code for each node I visit?

Any suggestions of what to look at to accomplish something like this would be most appreciated!

Note: After some further investigation, I just discovered libclang which looks like it could be my best friend. Coupled with a rewriter, I might just have what I need. Any pointers to good examples (I just found the excellent Apple developers meeting video on libclang) would be great.

like image 650
Robert Ankeney Avatar asked Nov 04 '22 07:11

Robert Ankeney


1 Answers

For a good example see this project.

It uses clang in order to instrument call function entering and exit, and it also inspects types of passed arguments.

like image 123
Dan Aloni Avatar answered Nov 09 '22 06:11

Dan Aloni