# Linux Applications Debugging Techniques/The interposition library

The dynamic liker allows for interception of any function call an application makes to any shared library it uses. As such, interposition is a powerful technique allowing to tune performance, collect runtime statistics, or debug the application without having to instrument the code of that application.
As an example, we can use an interposition library to trace calls, with arguments' values and return codes.

### Call tracing

Note that part of code below is 32-bit x86 and gcc 4.1/4.2 specific.

##### Code instrumentation

In the library, we want to address the following points:
* when a function/method is entered and exited.
* what were the call arguments when the function is entered.
* what was the return code when the function is exited.
* optionally, where was the function called from.
The first one is easy: if requested, the compiler will instrument functions and methods so that when a function/method is entered, a call to an instrumentation function is made and when the function is exited, a similar instrumentation call is made:
This is achieved by compiling the code with the -finstrument-functions flag. The above two functions can be used for instance to collect data for coverage; or for profiling. We will use them to print a trace of function calls. Furthermore, we can isolate these two functions and the supporting code in an interposition library of our own. This library can be loaded when and if needed, thus leaving the application code basically unchanged.
Now when the function is entered we can get the arguments of the call:
And when the function is exited, we get the return value:
Since these two instrumentation functions are aware of addresses and we actually want the trace to be readable by humans, we need also a way to resolve symbol addresses to symbol names: this is what libtrace_resolve does.

##### Binutils and libbfd

First, we have to have the symbols information handy. To achieve this, we compile our application with the -g flag. Then, we can map addresses to symbol names and this would normally require writing some code knowledgeable of the ELF format.
Luckily, the there is the binutils package which comes with a library that does just that: libbfd; and with a tool: addr2line. addr2line is a good example on how to use libbfd and I have simply used it to wrap around libbfd. The result is the libtrace_resolve function.
Since the instrumentation functions are isolated in a stand-alone module, we tell this module the name of the instrumented executable through an environment variable (CTRACE_PROGRAM) that we set before running the program. This is needed to properly init libbfd to search for symbols.

##### Stack layout

To address the first point the work has been architecture-agnostic (actually libbfd is aware of the architecture, but things are hidden behind its API). However, to retrieve function arguments and return values we have to look at the stack, write a bit of architecture-specific code and exploit some gcc quirks. Again, the compilers I have used were gcc 4.1 and 4.2; later or previous versions might work differently. In short:
* x86 dictates that stack grows down.
* GCC dictates how the stack is used - a "typical" stack is depicted below.
* each function has a stack frame marked by the ebp (base pointer) and esp (stack pointer) registers.
* normally, we expect the eax register to contain the return code
In an ideal world, the code the compiler generates would make sure that upon instrumenting the exit of a function: the return value is set, then CPU registers pushed on the stack (to ensure the instrumentation function does not affects them), then call the instrumentation function and then pop the registers. This sequence of code would ensure we always get access to the
Also, in practice, many of gcc's flags affect the stack layout and registers usage. The most obvious ones are:
* -fomit-frame-pointer. This flag affects the stack offset where the arguments are to be found.
* The optimization flags: -Ox; each of these flags aggregates a number of optimizations. These flags did not affected the stack, and, quite amazingly, arguments were always passed to functions through the stack, regardless of the optimization level. One would have expected that some arguments would pe passed through registers - in which case getting these arguments would have proven to be difficult to impossible. However, these flags did complicated recovering the return code. However, on some architectures, these flags will suck in the -fomit-frame-pointer optimization.
* In any case, be wary: other flags you use to compile your application may reserve surprises.

##### Function arguments

In my tests with the compilers, all arguments were invariably passed through the stack. Hence this is trivial business, affected to a small extent by the -fomit-frame-pointer flag - this flag will change the offset at which arguments start.
How many arguments a function has, how many arguments are on the stack? One way to infer somehow the number of arguments is based on its signature (for C++, beware of the 'this' hidden argument) and this is the technique used in __cyg_profile_func_enter.
Once we know the offset where arguments start on the stack and how many of them there are, we just walk the stack to retrieve their values:

##### Function return values

Obtaining the return value proved to be possible only when using the -O0 flag.
Let's look what happens when this method
is instrumented with -O0:
Note how the return code is moved into the ebx register - a bit unexpected, since, traditionally, the eax register is used for return codes - and then the instrumentation function is called. Good to retrieve the return value but to avoid that the ebx register gets clobbered in the instrumentation function, we save it upon entering the function and we restore it upon exit.
When the compilation is done with some degree of optimization (-O1...3; shown here is -O2), the code changes:
Note how the instrumentation function gets called first and only then the eax register is set with the return value. Thus, if we absolutely want the return code, we are forced to compile with -O0.

##### Sample output

Finally, below are the results. At at shell prompt type:
Note how libbfd fails to resolve some addresses when the function gets inlined.

##### Resources

* Code
* Overview of GCC on x86 platforms
* The Intel stack
* etrace: a tool to generate the run-time function call tree with gcc
* ELF details