3

Is there any way to list (show in VS, write to file) all callers (objects, functions) of a function while the program is running? Possibly using the debugger?

I need to record all calls (including callers) of a function from the launch of the program to its termination.

A simple scan of the source code or the binary does not do the job because the program could operate as a server which receives requests to call the desired function.

If Visual Studio does not provide this feature, are there any other solutions to this problem?

Shiro
  • 2,610
  • 2
  • 20
  • 36
  • If you ensure the function is never inlined, there might be an API to see the call stack at a point, then you might be able to figure out the caller from the stack – Justin Jul 06 '17 at 22:52
  • But if possible I don't want to do this manually. – Shiro Jul 06 '17 at 22:55
  • You may be able to use the stack pointer to get the return address. The issue is that function names are removed from release executables. – Thomas Matthews Jul 06 '17 at 22:55
  • Another issue is that the operating system can load your program anywhere in memory, so that the addresses may be different on each invocation of your program. – Thomas Matthews Jul 06 '17 at 22:56
  • It can be a debug release. – Shiro Jul 06 '17 at 22:56
  • But those addresses need to be resolved to object or function names otherwise it's just too hard to read. – Shiro Jul 06 '17 at 22:58

3 Answers3

1

IMO, your best solution is to add logging capabilities.

Find all calls to your function. Print some kind of information to a log file before the function is called. This will give you an annotated history of the function calls.

Another idea is to use a documentation tool, like Doxygen, which can print a "caller" and "callee" diagram. However, this is not during your program's execution.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • The problem lies exactly in the runtime aspect. This way I won't see how the program behaves as a server which receives calls. – Shiro Jul 06 '17 at 23:02
  • This is true. You could replace the function with a macro (yuck) and log the function name (which is available as a macro, I don't remember what though) of the caller – Justin Jul 06 '17 at 23:03
  • Is there a macro that holds the callers name? – Shiro Jul 06 '17 at 23:05
  • But that is just the name of the called function not the name of the caller. – Shiro Jul 06 '17 at 23:18
  • @Shiro Exactly. That's why you can wrap the function in a macro: `#define MY_FUNCTION(...) do { logFunctionName(__FUNCTION__); myActualFunction(__VA_ARGS__); } while (0)` – Justin Jul 06 '17 at 23:26
1

If your program is a .NET application, you can use my Runtime Flow tool to view all callers of a function while the program is running.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
1

VS profiler tool also would be helpful for you, you could select the method "CPU sampling".

enter image description here

After it finished, you could select "Caller/Callee".

enter image description here

Reference:

https://learn.microsoft.com/en-us/visualstudio/profiling/caller-callee-view

Update: We can export the report: enter image description here

Jack Zhai
  • 6,230
  • 1
  • 12
  • 20