Dominique
Dominique

Reputation: 17493

Functions are used as variables, how to know which function is actually called using their memory address

I'm working with a set of functions, having the same signature, saying

typedef int(*function_type)(int a, int b)

All those functions (lots of them) get stored in action_list arrays.
Further in in the program the following things happen:

chapter->action = (function_type)action_list[i];
...
chapter->action(x, y);

What I'd like to know, is which exact function is called by that last line, which is easy: I just use the debugger, put a breakpoint on that line, press F11 (step into) and I see it.

Problem: that line chapter->action(x, y); exists on many places within my code, and I'm interested in the complete list of those called functions. Obviously I could search for all functions having the same signature and add a print([%s], __FUNCTION__) at the beginning of those, but as I'm talking here about a computer program of some millions of lines of source code, that's also not an option neither.

I've already tried:

printf("[%s]\n", chapter->action);

But this was a bad idea :-). The following was better, but now I'm stuck:

printf("[%p]\n", chapter->action);

Now I know where in memory these function are stored (I have the complete list), but how can I use these values to know which functions I'm dealing with? (I've already checked, while re-running the program, the same pointer values seem to be used)

Upvotes: 1

Views: 73

Answers (1)

Ajay Brahmakshatriya
Ajay Brahmakshatriya

Reputation: 9203

Given that you are under the Windows environment and are using Visual studio, I can suggest a simple method. What you can do is generate a DLL instead of an executable with all the functions exported. A simple wrapper program can then call the main function inside the dll. You can get a list of all the addresses that were called. Finally you can use SymFromAddr to get a string name of the function.

Seems like a long way, but right now that is all I can think of, if you want to avoid parsing the pdb. There are also some open source tools available that can help you with the pdb file. So you could use those too.

Upvotes: 1

Related Questions