[LLVMdev] Questions on writing a pass that adds instrumentation code

Jonas Wagner jonas.wagner at epfl.ch
Wed Jun 26 00:47:26 PDT 2013


Hi,

I suggest you have a look at
lib/Transforms/Instrumentation/EdgeProfiling.cpp. This pass inserts
counters for every edge in the program. you can probably copy many things
from there.

2. I currently create the global variables holding the counter by
> calling "module.getOrInsertGlobal(...)". Will this ensure that the
> global starts with the value 0, or do I have to do this via other means?
>

See EdgeProfiler::runOnModule. There, a global is created by calling new
GlobalVariable. It is automatically zero-initialized by virtue of being a
global variable. The EdgeProfiler pass also inserts a call to
llvm_start_edge_profiling at the beginning of the main function. This
function calls atexit to register the handler that will process the
counters when the application exits.

>
> 3. How can I add code that is called when the program exits?
>

See llvm_start_edge_profiling in runtime/libprofile/EdgeProfiling.c

4. Suppose I want to call a function 'foo()' instead of incrementing a
> counter, where 'foo()' is some instrumentation code that I would like to
> insert rather than something in the target program. *Ideally*, what I'd
> like to be able to do is write foo() in C or C++, compile it to LLVM
> bytecode, and then somehow pull that into programs that run my pass. Is
> there an easy way to do this (i.e. not using an IRBuilder to recreate
> the bytecode or something silly like that), or should I just say "if you
> want to use my thing, then compile foo.c into your program so the
> definition of foo() is available".
>

You could define your function foo() in a special runtime, and ensure that
it gets linked in during the linking step. Again, I'd have a look at how
the EdgeProfiler does this.

Cheers,
Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130626/f3bcc408/attachment.html>


More information about the llvm-dev mailing list