Search code examples
c++c++11intel-pin

Printing program and function name of each instruction with Pin tool


I'm new to writing a pin tool to instrument the program. Currently, I'm kind of stuck with printing out the program name (image? I would say) and the function that the instruction belongs to. For example, I I have a program foo.cpp and function name func() that simple addition and cout. Then, when I use a pin tool, I want to print like below

0xAddress foo (or lib64/ld-linux... etc) func disassembled_instruction (ex. move etc)

I can get the address and disassembled instructions, but not the program and function name.

Can anyone suggest me whether this is possible and how?

Thank you!


Solution

  • Program Name

    To get the full path to the main binary (hence the program name) you must set an instrumentation routine for IMG (image) in your main() using IMG_AddInstrumentFunction.

    In the analysis callback (passed to IMG_AddInstrumentFunction) use the IMG_IsMainExecutable function which simply returns a boolean indicating if the currently loaded image is the main binary (true) or not.

    If the former function (IMG_IsMainExecutable) returns true you can call IMG_Name to get its full path.

    For a full example see the Detecting the Loading and Unloading of Images (Image Instrumentation) example in the manual.

    Function Name

    Use PIN_InitSymbols in your main, before calling PIN_StartProgram.

    You can instrument at the routine level using RTN_AddInstrumentFunction (or get the routine from the instruction, BBL or TRACE).

    Once you have the RTN (routine), you can get its name with the RTN_Name function.

    Check the manual for the example Procedure Instruction Count (Routine Instrumentation) which should give you a good start on how to use these functions.

    Note: as obvious as its sounds, the target executable must have symbolic information (symbols): No symbols == no routine names.