3

Possible Duplicate:
Tools to get a pictorial function call graph of code

I wrote a C++ function long time back in a dll (on windows) ... Some wrote api , calling my function using function pointers and some just called it ..

I want to know who all are callers(CODE PATHS) to my function . I want to analyze stack of every api calling into my function .

Is there a way i can do so easily . I mean when i look up callers (CODE PATHS) i would essentially have to manually do a lot of work because of function pointers in the way . So if there is any open source solution for the same , please share ..

i don't wish to use break points or write functions myself to show up stack . The reason is because t his is something like if a call path is hit , it would show up in my results .. but if a call path is NOT hit then what ? So I will know of a function X who calls into me only when function X is called ; but its not always the case

Community
  • 1
  • 1
MAG
  • 2,841
  • 6
  • 27
  • 47

2 Answers2

3

You do not say for which OS you need it, if it's Linux you have the @Magnus answer, if it's Windows you can read this.

To get the call stack in C++ is far from easy. You do not need too much code to get the call stack as memory addresses but to map them to function names can be very tricky (because you have to resolve the function at each address in each module).

Fortunately there is a Windows function to do that: StackWalk64 from the Debug Help Library (do not mind about the -64 suffix, it works both on 32 and 64 bit). There you can find all the functions you need to resolve the full stack in every known platform (using debug information, when present, to resolve and undecorate function names).

You can read this article on CodeGuru for a complete working example.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Ah, I was looking for something like that on Windows myself, cheers mate! – Magnus Jan 16 '13 at 10:22
  • This is something like if a call path is hit , it would show up in my results .. but if a call path is NOT hit then what ? So I will know of a function X who calls into me only when function X is called ; but its not always the case – MAG Jan 16 '13 at 10:34
  • @MAG if your function is NOT called then...which call stack you want to read??? Unless you're talking about **CODE PATH** then answer is **sorry you can't**, even with "plain" C++ isn't trivial at all but with pointers all around is pretty impossible. – Adriano Repetti Jan 16 '13 at 10:38
  • yes code paths is the ask ... between on linux does valgrinds give you code paths ? – MAG Jan 16 '13 at 10:43
  • @MAG no, we all did understand run-time stack. Try tools suggested here: http://stackoverflow.com/questions/517589/tools-to-get-a-pictorial-function-call-graph-of-code – Adriano Repetti Jan 16 '13 at 10:49
2

If your primary concern is finding all the callers to a function at runtime, you could look into callgrind or cachegrind as part of the valgrind suite. There's some GUI tools to visualise the connections and it's useful for for a number of other problems, too.

See here: http://valgrind.org/

EDIT: More precisely this tool: http://valgrind.org/info/tools.html#callgrind

Magnus
  • 980
  • 4
  • 10