Instrument examples

Hi,
I am new to LLVM. I want to use LLVM to instrument codes, such as function calls and basic blocks. But I don’t know where to start. I wonder if there are any example codes to show how to instrument codes in the IR level?

Thanks.

Hi,

    I am new to LLVM. I want to use LLVM to instrument codes, such as
function calls and basic blocks. But I don't know where to start. I wonder
if there are any example codes to show how to instrument codes in the IR
level?

  No idea what exactly you want to achieve, but let me try.

  First, you might need a function doing the instrument. Say,

    void HowManyTimeThisFunctionGetCalled();

  Then you can insert a call to the function above while creating
a LLVM function [1]. In that way, every time that LLVM function
get called the function doing the instrument will be called.

  You might need to take a look on ${LLVM_SOURCE}/example/HowToUseJIT
to get a feel on how to create a LLVM module, function and basic block.

Regards,
chenwj

[1] http://llvm.org/docs/ProgrammersManual.html#Function

First, have you read the Language Reference Manual, the Programmer’s Manual, and the How to Write an LLVM Pass manual at ? Those are pretty much required reading for staring to learn how to write LLVM transform passes. As you start coding, you’ll find the doxygen docs (also on that web page) extremely valuable. As for examples, you might want to take a look at some of the passes in the SAFECode project (). The load/store instrumentation pass (safecode/lib/InsertPoolChecks/LoadStoreChecks.cpp) is a very simple example of a pass that instruments every load and store. Modifying that code to instrument function calls and basic blocks should be straightforward. There are, of course, numerous examples in LLVM itself (in lib/Transforms), too, although I’m not sure which passes are going to be small and trivial (maybe the profiling instrumentation passes). – John T.

First, have you read the Language Reference Manual, the Programmer’s Manual, and the How to Write an LLVM Pass manual at http://llvm.org/docs? Those are pretty much required reading for staring to learn how to write LLVM transform passes.

I have read the docs, and I am concerning about coding. What I need is something like the API reference of LLVM. And I cannot find such docs.

There are, of course, numerous examples in LLVM itself (in lib/Transforms), too, although I’m not sure which passes are going to be small and trivial (maybe the profiling instrumentation passes).

I think the code example in Transforms/Instrumentation is much helpful. It is simple and understandable for a beginner.

Thanks!

No idea what exactly you want to achieve, but let me try.

First, you might need a function doing the instrument. Say,

void HowManyTimeThisFunctionGetCalled();

Then you can insert a call to the function above while creating
a LLVM function [1]. In that way, every time that LLVM function
get called the function doing the instrument will be called.

You might need to take a look on ${LLVM_SOURCE}/example/HowToUseJIT
to get a feel on how to create a LLVM module, function and basic block.

Regards,
chenwj

[1] http://llvm.org/docs/ProgrammersManual.html#Function

Yes, instrumenting functions is just one of my purpose. The API document (though not very detailed) your provide is helpful. However, the problem of the document is that it is not up-to-date. For example, “Type::Int32Ty” is not used to acquire an integer type in LLVM2.9. So sometimes it cause confusion to me.

Thanks.

Yes, instrumenting functions is just one of my purpose. The API document
(though not very detailed) your provide is helpful. However, the problem of
the document is that it is not up-to-date. For example, "Type::Int32Ty" is
not used to acquire an integer type in LLVM2.9. So sometimes it cause
confusion to me.

  Be sure the version of the document you read is the same as what LLVM
you're using. As [1] says,

  "If you are using a released version of LLVM, see the download page to
   find your documentation."

  Good luck. :wink:

Regards,
chenwj

[1] About — LLVM 18.0.0git documentation