0

I would like to add some instructions to C/C++ programs from within clang without using an LLVM pass. I know it is possible but I can't find any documentation on how to do so.

As an example, given the program below

// before instrumentation
int main() {
    int num = 0;
    printf("%d\n", num);
}

I would like to be able to add arbitrary instructions from within clang and create a program like

// after instrumentation
int main() {
    int num = 0;
    if(num != 0) {    // inserted code
        // do stuff   // inserted code
    }                 // inserted code
    printf("%d\n", num);
}

I found some projects that instrument code using clang (1, 2) but not any concrete documentation.

  • You mention "C/C++". Does this mean you are mixing C and C++? Otherwise, you should talk about C or C++. (Besides, the C language does not have `cout` and doesn't have the overload for `operator<<`. – Thomas Matthews Mar 19 '18 at 23:13
  • Making changes to a compiler is evil. When you change compilers, you'll have to port the same changes and many compilers can't be modified as easily. – Thomas Matthews Mar 19 '18 at 23:15
  • The instructions to be added might change between C/C++ but I assume the process of adding them remains the same. Also for the purposes of this question assume I want to be evil and instrument the code from clang. –  Mar 20 '18 at 00:14
  • 1
    I believe GCC/G++ can do it, see here: https://stackoverflow.com/questions/14850548/how-can-i-plant-assembly-instructions-in-the-prologue-and-epilogue-of-function-v – Dave M. Mar 20 '18 at 00:24
  • I need to use clang –  Apr 02 '18 at 07:23

0 Answers0