2

I'm trying to use kcachegrind to generate a callgraph, preferably with a clear indication of which blocks in the call graph correspond to my functions. I followed this answer as a test run, and got a different output. I don't understand what I need to do to repeat the results of this example. How do I get kcachegrind to display my function names on the call graph?

For convenience, here is the exact code I used:

int f2(int i) { return i + 2; }
int f1(int i) { return f2(2) + i + 1; }
int f0(int i) { return f1(1) + f2(2); }
int pointed(int i) { return i; }
int not_called(int i) { return 0; }

int main(int argc, char **argv) {
    int (*f)(int);
    f0(1);
    f1(1);
    f = pointed;
    if (argc == 1)
        f(1);
    if (argc == 2)
        not_called(1);
    return 0;
}

This file (main.c) was compiled as such:

gcc -ggdb3 -O0 -std=c99 main.c -o main

and then I invoked valgrind:

valgrind --tool=callgrind ./main

which output the following:

==4770== Callgrind, a call-graph generating cache profiler
==4770== Copyright (C) 2002-2017, and GNU GPL'd, by Josef Weidendorfer et al.
==4770== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4770== Command: ./main
==4770== 
==4770== For interactive control, run 'callgrind_control -h'.
==4770== 
==4770== Events    : Ir
==4770== Collected : 121502
==4770== 
==4770== I   refs:      121,502

finally, I launched kcachegrind with the output file (callgrind.out.4789):

kcachegrind callgrind.out.4789

From the example I used, I expected the call graph to look like this:

The Expected Graph Output

Instead, I got something that looked like this:

The Actual Graph Output

What can I do to reveal my function names on the call graph?

Onofog
  • 443
  • 3
  • 15
  • `gcc -ggdb3 -O0` - Why bother testing a unoptimized build? Once you enable the optimizer things will change significantly and your results no longer matter. – Jesper Juhl May 04 '19 at 15:49
  • I used the flags provided in the example. It did not seem wise to change them since I was not able to successfully reproduce their results. – Onofog May 04 '19 at 15:51
  • 1
    KCacheGrind doesn't show all elements but only the most used from selected starting point and what is shown looks like what ld.so is doing before main. Either you search in the list on the left side for main or double click on dl_main. Your main will show up somewhere down. – user6556709 May 04 '19 at 16:45
  • 1
    My issue was that version 3.13 of Valgrind seems to have a bug where function names weren't showing up in callgrind output. Updating Valgrind to a newer version, 3.17 in my case, fixed my issue. Details here if anyone else is having a similar issue https://stackoverflow.com/questions/55770344/valgrind-shows-for-line-numbers-despite-g-flag, in short check for `ELF section outside all mapped regions` using `-v` option. – GeminiDakota Feb 02 '22 at 23:27

1 Answers1

1

Per the comment by @user6556709 , the loader and dynamic linking are what show in my graph output above. The referenced example does not indicate how to find your main, but it can be found in the 'Flat Profile' section of the GUI:

Digging for programs

It took some digging to find -- it can be grouped by source to show only .c/.h files, or use the Location heading to sort by location name and search for main.

Onofog
  • 443
  • 3
  • 15
  • What are the functions with location `(unknown)`? – user2023370 Sep 09 '20 at 14:19
  • What does this answer have to do with the problem of not seeing the function names? We are not only talking about main, are we – simplename Jul 19 '22 at 20:45
  • @simplename see the comments on the OP, you can click through into dl_main to search visually, and use the flat profile to determine where the functions you care about are so you know where to start. – Onofog Aug 17 '22 at 23:53