0

Could Someone kindly tell me how can I profile single lines or blocks of code of a program in C with GNU profiler? I used gprof ./a.out gmon.out which gives me flat profile and Call graph. However, I would like to see lines that are more frequently accessed.

Thanks,

mOna
  • 2,341
  • 9
  • 36
  • 60
  • Extract those lines into their own function? The thought "did action A of function B take too long" makes me think that action A should be its own function in the first place. – Carl Sep 29 '14 at 18:17
  • This seems to be an answer though: http://stackoverflow.com/questions/3263573/is-there-a-profiler-for-c-gcc-to-profile-code-lines-separately?rq=1 – Carl Sep 29 '14 at 18:18

2 Answers2

1

This is probably one of those things that you just don't know the term you should've googled, so I'll answer it:

The term you are looking for is "annotation"-you want to annotate the source and see the line by line hits in the code.

Calling gprof with the -A flag will dump out the samples on each line that were caught.

See Also:

https://sourceware.org/binutils/docs/gprof/Annotated-Source.html

IdeaHat
  • 7,641
  • 1
  • 22
  • 53
  • But it won't give you inclusive percent by line. (If a line is a big slow function call, it won't see it.) Nor does it give you wall-clock time. (It's blind to I/O.) – Mike Dunlavey Sep 29 '14 at 18:44
  • Thanks, I found the answer here https://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html :) – mOna Sep 29 '14 at 20:08
0

Ok, I'll post this answer so if some newbie like me searched for it can find it faster :) here are the steps: source

  1. gcc -fprofile-arcs -ftest-coverage fourcefile.c (At the end of compilation files *.gcno will be produced)
  2. Run the executable.
  3. Run gcov: gcov sourcefile.c (At the end of running, a file (*.gcov) will be produced which contains which contains all the required info)
mOna
  • 2,341
  • 9
  • 36
  • 60