c++assemblyprofilingintel-pin

How can I use Intel PIN to catch all loads to an array?


I'm profiling an application I have written using PIN. The source code of the application uses an array - I want PIN to catch every load instruction made to the array.

Currently, I have annotated the source code of the application I am trying to profile. Every time I read from the array, I first call a function startRegionOfInterest(). Once I finish reading from the array I call another function endRegionOfInterest(). I can use PIN to easily catch calls to these two functions - whenever a load exists between these two calls I assume it's a load to the array I'm interested in.

However, this is pretty coarse grained, and so I end up classifying a lot of loads that are NOT to the array of interest as reads to the array.

Is there an easier way for me to more precisely catch all loads made to the array I'm interested in?


Solution

  • In your startRegionOfInterest method, you can use some kind of indicator sequence to pass the array address to your PIN tool. E.g., store a magic constant, then store the array address, something like:

    volatile void *sink;
    
    void startRegionOfInterest(void *array) {
        sink = (void *)0x48829d2f384be;
        sink = array;
    }
    

    In your PIN tool, you look for a store of the magic constant (within the startRegionOfInterest call for extra specificity, if you want), and then you know the next store is the address of the array. You can communicate the length similarly.

    Implementing the sequence with inline asm instead you can remove the variability associated with compiler and optimizer behavior, although I think the volatile approach should work in practice (although you might have to skip some intervening non-store instructions. A godbolt.