Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a call graph for certain c++ function in Visual Studio?

I wonder whether there is a tool for VS that can show me a call graph (that is, a diagram listing all possible execution paths) for a given C++ function. It would help in navigating a big code base, in cases where a function is called in only a few places.

For oft-called functions like printf it could simply say:

too many options...

Again I guess it is not really easy to make such tool so I wonder if it exists, but you know it seems possible to do it so you never know... :)

EDIT: I know about find all references, but that gives just call sites of the function, not the call site of the function that called the function that called the function...

EDIT: VS is 2010, but if necessary VS2012 is an option.

like image 422
NoSenseEtAl Avatar asked Apr 24 '13 10:04

NoSenseEtAl


People also ask

How do you extract a call from a graph?

On the bottom right, select the "Call graph" tab. This shows an interactive call graph that correlates to performance metrics in other windows as you click the functions. To export the graph, right click it and select "Export Graph".

How do I get the call hierarchy code in Visual Studio?

To display the Call Hierarchy window, right-click in the code editor on the name of a method, property, or constructor call, and then select View Call Hierarchy.

What is a call graph in C?

A call graph (also known as a call multigraph) is a control-flow graph, which represents calling relationships between subroutines in a computer program. Each node represents a procedure and each edge (f, g) indicates that procedure f calls procedure g. Thus, a cycle in the graph indicates recursive procedure calls.

What is function call graph?

Function call graphs, or call trees, are a standard tool often found in reverse engineering tools, code browsers, IDEs, and even file editors. They provide fundamental information about the control flow dependencies for the functions you're examining in your tool.


2 Answers

You mentioned that you know about finding all the references. Have you looked into viewing the Call Hierarchy? It's probably not your "dream method" but it does allow you to look at a function in terms of "calls to" and "calls from" the given function. The window also allows you to add multiple functions to view in a tree format. So basically you would tree up or down through the possible outcomes.

Right click on the desired method ( could be anywhere in the hierarchy ) =>

Select "View Call Hierarchy"

Note that if you can add more than one reference point to the window. Delete when needed

You could also use Ctrl+K or Ctrl+T


Another fine example, IMHO, of a disappointment in the differences between C++ and C# with VS. I think Code Maps would be just what you're looking for. Assuming of course you were working with Ultimate - but nope, not with C++.
like image 63
ChiefTwoPencils Avatar answered Sep 19 '22 13:09

ChiefTwoPencils


There's no such feature in C++/MSVC, as far as I know.

However, there's AQTime profiler for windows that has "static analysis" option that (IF I remember correctly) scans compiled executable, generates call graph and shows you unreacheable functions.

If I remember correctly, AQtime integrates into visual studio (professional edition, afaik).

Unfortunately, this is a commercial profiler that costs around $500, and this feature is not available in trial version. Last time I used static analysis was 3..4 years ago and I don't exactly remember details at the moment (and I don't have access to AQTime anymore). Anyway, it is a specialized tool, so I wouldn't recommend buying it unless you're optimizing code for speed 24/7.

Perhaps, by googling "static analysis", "code coverage" or researching other profilers you'll find somewhat similar tool that does the job for free.

Aside from that, doxygen can generate callgraphs for C++ code. In case of doxygen, you'll have to hunt for functions that are never called yourself.

Also, Visual Studio 2008 had a built-in caller graph feature (which, I think, uses intellisense). Basically, you right click any function and select "show callers" (or something like that), that'll open list of all functions (visual studio THINKS are calling your function) in a window. Because this feature was present in VS2008, it should be included in VS2010. However, it can't detect every caller for obvious reasons (virtual methods, callbacks, etc).

like image 32
SigTerm Avatar answered Sep 19 '22 13:09

SigTerm