Dynamic instrumentation with Clang

2019-04-29 10:58发布

问题:

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.

回答1:

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.



标签: c clang