Call tracing code without adding statements

Learning the sequence of calls in an application is either tedious or time consuming or both. And when there are callbacks involved it becomes frustrating.

The typical solution used to be adding trace statements are the entry and exit of functions. But this reuiqres a lot of editing in existing programs along with adding code space and execution penalties in production code.

Along comes a fascinating article from IBM, written in 2005. The solution is to use existing  functionality with a bit of glue code. The key is  a feature of gcc, -finstrument-functions. By adding this to CFLAGS, you cause a tracing function to be called upon entry to a function and  on  exit of that function. The tracing  function writes the function pointer to a log file, along with an Entry or Xit indicator. Using another existing tool, addr2line,  you  can get the file and line number of that function. The details are beautifully documented here. The article  provides the simple source to add to your program as well as the code  for pvtrace.

To use this approach under scratchbox,  you need to install pvtrace using fakeroot inside your target. You will also ned to adjust the value of MAX_FUNCTIONS in symbols.h. For Navit I use  4000. You also need to install graphviz but this does  not need to be  done inside scratchbox.

A  typical run would be:

  1. Add the insturmentation code to your program, either in main() or in a separate file
  2. run configure with CFLAGS=”-g -finstrument-functions”
  3. make all
  4. run your program
  5. pvtrace <program name>
  6. dot -Tjpg -o graph.jpg

The resulting output is graph.jpg. For  complex programs this might be difficult to read, in which  case you  can  add the -finstrument-functions flag in a module Makefile. The  resulting output will be localized and thus smaller, probably printable.

This is an example of starting Navit but not having a map displayed. You  will need to save this file to your computer and then open it using some tool that lets you scroll around in the image as well as zoom in and out.

A better example is this trace from Gui/internal, changing the Country to CA and  then entering a partial street address.

dwould has adapted the approach to produce indented call trace logs.


Call tracing code without adding statements — 1 Comment

  1. Pingback: Getting call stack tracing out of navit « Daniel Would’s Weblog