Search code examples
linuxintel-pin

How to get address space layout from Intel Pin on Linux?


I want to get the address space layout from Intel Pin on Linux.

At first, I try to read file - /proc/PID/maps and get the address space layout. But when do you execute such part of code?

If you put it before PIN_StartProgram, the maps file will not contain some regions, like heap;

If you put it in the Fini, and hook it with PIN_AddFiniFunction(Fini, 0);, it should be good. However, when you just trace one ls execution, you cannot see any output related address space layout. That's wired.


Solution

  • Perhaps not the best solution, but it worked for me. The main problem is that when the tool starts, the address space is not prepared yet. You can wait until all of the images are loaded and then read the contents of procfs.

    So you should add an instrumentation function for each image. For example, add the following statement to the main function:

    IMG_AddInstrumentFunction(Image, 0);
    

    Then you should read procfs, every time an image is loaded. This is because you do not know which image is the last image loaded (Of course, if you know which image is the last one, you can simply read the file only once, after that image is loaded):

    VOID Image(IMG img, VOID *v)
    {
        ...
        /* open /proc/PID/maps and read its contents */
        ...
    }
    

    During the execution of the program, you always have the latest mappings of the address space and everything will be fine. Albeit, you should always be careful with runtime layout modifications, situations such as heap size increase using brk() system call.