/robex/ - Function tree

Create a local function call tree in Linux

By /robex/, September 2018. Back to Articles and Guides

Code labyrinth

Have you ever downloaded some random project with a million files and lines of code in order to change a minor detail, just to end up with a headache while trying to find the code to tweak? I know I have, and in order to help me find it, I searched for tools that allowed me to create a function call tree so that I could locate the final code buried under a thousand abstractions.

Digging around, I found a "good enough" way to do it with systemtap. Its important to note that this is different from ltrace or strace as it does not show functions from the standard library or syscalls, that would completely clutter the output and make the process even more painful than just understanding the whole code.

Running the script

Install systemtap from your package manager (or build it from source) and then download this script (also found in systemtap examples). In order for the script to output meaningful names you must obviously recompile the source code with debug symbols (-g switch in gcc).

Afterwards, run the script as follows (root privileges are required for tracing):

If you want all function calls from within all functions:

sudo stap para-callgraph.stp 'process.function("*")' -c '/your/program --your-args'

If you want all function calls from main function:

sudo stap para-callgraph.stp 'process.function("main")' -c '/your/program --your-args'

Example output

Here is part of the output after running this on unrar source code:

 57 unrar(15621): ->main argc=0x3 argv=0x7ffe330d3ed8
 92 unrar(15621):  ->InitConsole 
 99 unrar(15621):  <-InitConsole 
101 unrar(15621):  ->SetSignalHandlers this=0x651990 Enable=0x1
105 unrar(15621):  <-SetSignalHandlers 
109 unrar(15621):  ->CommandData this=0x81ebf0
111 unrar(15621):   ->RAROptions this=0x81ebf0
114 unrar(15621):    ->SecPassword this=0x828c20
118 unrar(15621):     ->Set this=0x828c20 Psw=0x436248
121 unrar(15621):     <-Set 
121 unrar(15621):    <-SecPassword 
124 unrar(15621):    ->Init this=0x81ebf0
142 unrar(15621):     ->GetNumberOfThreads 
157 unrar(15621):     <-GetNumberOfThreads return=0x2
158 unrar(15621):    <-Init 
159 unrar(15621):   <-RAROptions 

/robex/ - Last edited: 2018-09-22 04:05:39