How to handle types when instrumenting C++ code to trace arguments, especially pointers with a LLVM-15 pass (original) (raw)

December 11, 2024, 4:24pm 1

Hello together,

I am trying to write a LLVM-15 pass which shall instrument arbitrary c++ code to trace function calls, arguments and return values. The idea is to inject printf calls at the beginning of each function and at the end. To get started I looked at the llvm tutor repo. (I don’t want to modify the code I want to trace, I just pass my pass to clang with ‘-fpass-plugin’. If you think I should approach this problem differently, you can also tell me.)

For function calls it seems to work fine.
The problems start when tracing arguments and return values, especially pointers. For tracing these pointers I want to dereference them (at least to a specific level) and then print the value. In LLVM-15 pointers are (by default) opaque. The migration instructions here mention that instructions now have types. I tried to iterate over the ‘users()’ of the pointer and get the type from there, but it doesn’t seem to work and also seems kind of unsafe?
As I understand I need to know the type of the pointer to dereference it and then print it in the end.

Structs I also want to print (at least to a certain depth). Should be kind of similar once pointers are solved, I hope at least…

Could somebody give me a hint how to approach this problem?