0

Let's say I have the following function which does some sort of math on a number:

int apply(int (*fp)(int n), int before)
{
    printf("before: %d\n", before);
    int after = fp(before);
    printf("after: %d\n", after);
    return after;
}

Is there any way here to infer what *fp points to within the body of the function? My thought was not, but perhaps there's some utility or other where you can give it a function address and it will return the function/memory label or name. Is that possible to do somehow?

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • There are ways to do this, but you have to compile for debug and it is highly compiler dependant. What compiler are you using? – Jerry Jeremiah Feb 25 '21 at 00:28
  • @JerryJeremiah gcc. – samuelbrody1249 Feb 25 '21 at 00:29
  • Possible duplicate: https://stackoverflow.com/questions/40706805/how-to-convert-a-function-pointer-to-function-name/40706869 There are multiple answers there and a couple of them are standard C++ but require tracking it yourself. But there is an answer about backtrace_symbols_fd which may be what you are after. – Jerry Jeremiah Feb 25 '21 at 00:30
  • Possible duplicate: https://stackoverflow.com/questions/3078851/c-get-the-name-of-the-function There are multiple answers here - not sure if they are helpful. – Jerry Jeremiah Feb 25 '21 at 00:33
  • `dladdr` looks like a simpler and safer starting point than `backtrace_symbols` to me, but I'm not sure of the technical details of either. – aschepler Feb 25 '21 at 00:38
  • Are you allowed to change the call site of apply? If you can, what about https://onlinegdb.com/SJNrQuNMd – Jerry Jeremiah Feb 25 '21 at 00:46
  • @JerryJeremiah oh I see, just using the compiler macro `__func__` ? – samuelbrody1249 Feb 25 '21 at 00:48
  • 1
    @samuelbrody1249 No, __func__ gives you the currently executing function. Maybe this is more what you after: https://onlinegdb.com/S16jEdNGO – Jerry Jeremiah Feb 25 '21 at 00:51

2 Answers2

1

Not within standard C, other than by doing if (fp == some_function).

Some systems may have such functionality but it's meant for debugging, not reflection.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • I see, thanks for that. On a separate note, do you know if C++ has something like that? – samuelbrody1249 Feb 25 '21 at 00:22
  • 1
    I don't know for sure but I suspect not. It's a difficult thing for a compiled language to do efficiently; you'd need some sort of enormous hash table in memory containing the names and addresses of every function in the program (including all libraries it links with). Or you try to find the program's binary (which is hard) and hope that its symbol tables haven't been stripped. – Nate Eldredge Feb 25 '21 at 00:24
  • @NateEldredge What about a map of a function pointer to name? If you used a macro to pass the function pointer it could automatically stringify the name passed in and add it to the map... – Jerry Jeremiah Feb 25 '21 at 00:35
  • @JerryJeremiah: True, but that wouldn't necessarily be the name of the function actually being called - the argument could have been a more elaborate function pointer expression. `apply(flag ? function_table[53] : other_table[14], 27);` – Nate Eldredge Feb 25 '21 at 00:50
0

Is there any way here to infer what *fp pointers to within the body of the function?

fp will be the address of whatever function is passed in; you can access the bytes there, but the bytes will be compiled code, so not especially friendly to look at. If you're using a debugger, you can of course step into the function.

Caleb
  • 124,013
  • 19
  • 183
  • 272