Inside the function, how do I get my return address?

I am looking to create a list of memory locations from which this function was called. Is this code correct? How can this be improved? Suppose integers, long integers, and pointers are 4 bytes, and char is 1 byte. Suppose compiler optimizers are not executed. Suppose a function uses / sets its arguments, return value, and local variables.

void * CalledFromArray[ARRAY_SIZE];
int CallerRecorder(int i)
{
 int LocalVar1;
 int LocalVar2;
 CalledFromArray[i]=(void *) *( (long int *)(  (char *)&LocalVar2 + 36 )  );
 return 0;
}
+4
source share
1 answer

, . , GCC. , , . , const char* const pFunction , , __FUNCTION__. dlsym .

#define CallerRecorder(var) CallerRecorderNew((var), __FUNCTION__)

int CallerRecorderNew(int i, const char* const pFunction)
{
    // dlsym(..., pFunction)
}
+1

Source: https://habr.com/ru/post/1621318/


All Articles