Click here to Skip to main content
13,249,087 members (39,175 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 14 Dec 2013

External Functions Call Tracer for Executable Linked Shared Libraries

, 15 Jan 2014
Rate this:
Please Sign up or sign in to vote.
External functions call tracer for executable linked shared libraries


This utility which is composed of a script and a small shared library code enables the user to apply the GCC provided functions instrumentation feature to trace all functions call in a process in the order they were called without making any modifications in the original source code which is being debugged/traced.

As a small example - given the code:

extern void goo(); //Implemented in a shared object linked with this exe...
void foo(){...}	//implemented in the exe...
int main() {
    return 0;

The output from running the program will be a trace.out file, given the file as an input to the script, the script will print the following:

Thread 70431040 --> main at 2013-12-07T15:36:30+0000, called from __libc_start_main (??:0)
Thread 70431040 --> foo() at 2013-12-07T15:36:30+0000, called from main (main.cpp:86)
Thread 70431040 <-- foo() at 2013-12-07T15:36:30+0000
Thread 70431040 --> goo() at 2013-12-07T15:36:30+0000, called from main (main.cpp:88)
Thread 70431040 <-- goo() at 2013-12-07T15:36:30+0000
Thread 70431040 <-- main at 2013-12-07T15:36:30+0000

The utility will handle coherently functions called and defined both in the executable file and in any shared objects it's linked against.

The user can control the trace granularity by adding -finstrument-functions-exclude-file-list=file,file,... compilation flag.

The function names in the output are demangled.

P.S.: This is a modification of

Using the Code

To use the utility, the following steps are to be done:

  1. Build the trace library from the source tracer.cpp file with the following command: g++ -g -shared -fpic -pthread tracer.cpp -o -ldl
  2. Compile the traced code (executable or shared libraries) with g -rdynamic -finstrument-functions flags.
  3. Link everything including the traced library, insert the tracer library link command after any of the traced libraries.

P.S.: The tracer functionality has a heavy performance impact, mainly due to the dladdr function use and secondly the frpintf (checked with valgrand's callgrind tool. I still have no clue how to improve it and will be happy to get any advice. 

Due to this issue, the tracer code can be turned on/off during runtime by setting/unsetting RUN_TRACE environment variable - this is accomplished with GDB attaching to the running traced process just before the scenario that needs to be debugged -  

P.P.S The -g flag which adds debug symbols to the tracee code inccures a big increase in resultting obj/exe/so files size. The utility will work fine without it, just  the source line numbers won't be there. 

P.P.P.S Notice that if in the final output you get a lot of same symbol repeating itself and it's clearly not
part of any library you trace, try to add this symbol as another OR clause to the script in row 44 - 

"if test "${MANGALEDCNAME}" = "(null)" -o "${MANGALEDCNAME}" = "__gxx_personality_v0""  

I suspect this might be a result of dladdr mistakenly resolving an executable's text section address to some adjacent (irrelevant) symbol. 

Another update - In case you don't see the trace.out file grow in size (stays zerro size) try to define this variable in your env - LD_PRELOAD="path to"  




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Andrey Grodzovsky
Software Developer (Senior) spielo
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
skyformat99@gmail.com15-Jan-14 20:28
memberskyformat99@gmail.com15-Jan-14 20:28 
QuestionAndrey it's a great utility! Pin
Volynsky Alex17-Dec-13 14:39
memberVolynsky Alex17-Dec-13 14:39 
AnswerRe: Andrey it's a great utility! Pin
Andrey Grodzovsky17-Dec-13 14:48
memberAndrey Grodzovsky17-Dec-13 14:48 
GeneralRe: Andrey it's a great utility! Pin
Volynsky Alex17-Dec-13 14:53
memberVolynsky Alex17-Dec-13 14:53 
GeneralRe: Andrey it's a great utility! Pin
Andrey Grodzovsky17-Dec-13 15:55
memberAndrey Grodzovsky17-Dec-13 15:55 
QuestionNo download? Pin
Mika Wendelius16-Dec-13 8:30
mvpMika Wendelius16-Dec-13 8:30 
AnswerRe: No download? Pin
Andrey Grodzovsky16-Dec-13 8:36
memberAndrey Grodzovsky16-Dec-13 8:36 
GeneralRe: No download? Pin
Mika Wendelius16-Dec-13 9:23
mvpMika Wendelius16-Dec-13 9:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 15 Jan 2014
Article Copyright 2013 by Andrey Grodzovsky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid