Tuesday, September 29, 2009

c/c++: call stack v.2

In previous c/c++: call stack article I wrote about obtaining function call stack. The method described in the article is good enough but it is linux-specific. There's no similar solutions out-of-box in other *nix-like operation systems as far as I know.
This time I would like to discuss more generic way. At list it is available for the code compiled with gcc.

gcc provides two built-in functions which could be used to obtain function call stack: __builtin_return_address and __builtin_frame_address. With __builtin_return_address it's possible to obtain return address of the current function, or of one of its callers. __builtin_frame_address returns the address of the function frame.

Both functions require constant argument - the number of frames to scan up.

To store function call struct call structure is used defined as following:

struct call {
 const void *address;
 const char *function;
 const char *object;

With __builtin_frame_address it is checked if the top of the stack has been reached - in this case aforementioned built-in returns zero. In the loop the return value of this function is compared with zero and the loop is terminated if this expression in compare statement turns into true - the top of the call stack has been reached.
Finally __builtin_return_address is used to get the return address of the function.

The resulting function to get the backtrace looks like:
#define _GNU_SOURCE
#include <dlfcn.h>

int backtrace(struct call trace[], int maxlen)
 Dl_info dlinfo;
 unsigned int i;

 for (i=0;i<maxlen;++i) {
  switch (i) {
   case 0:
     return i;
    trace[i].address = __builtin_return_address(0);
   case 1:
     return i;
    trace[i].address = __builtin_return_address(1);
   case 2:
     return i;
    trace[i].address = __builtin_return_address(2);
 /* SNIP */
 /* .... */
 /* SNIP */
   case 63:
     return i;
    trace[i].address = __builtin_return_address(63);
    return i;

  if (dladdr(trace[i].address, &dlinfo) != 0) {
   trace[i].function = dlinfo.dli_sname;
   trace[i].object = dlinfo.dli_fname;

 return i;
backtrace routine fills trace array with the call stack information and returns the depth of the function calls.

Following small example shows backtrace in action:
#include <stdio.h>


void f0()
 struct call trace[CALLSTACK_MAXLEN];
 int i;
 int depth;

 depth = backtrace(trace, CALLSTACK_MAXLEN);

 for (i=0;i<depth;++i)
  printf("%s: %s(%p)\n", trace[i].object, trace[i].function, trace[i].address);

void f1()

void f2()

void f3()

void f4()

int main(int argc, char **argv)

 return 0;
Again the application should be compiled with -rdynamic gcc flag needed for dladdr function and linked with dl library for the same purpose:
gcc backtrace.c -o backtrace -ldl -rdynamic
After execution the program should provide the following output:
./backtrace: f0(0x804b11c)
./backtrace: f1(0x804b1ac)
./backtrace: f2(0x804b1b9)
./backtrace: f3(0x804b1c6)
./backtrace: f4(0x804b1d3)
./backtrace: main(0x804b1e0)
/lib/libc.so.6: __libc_start_main(0x6ff58a9e)
Though it's gcc-specific method this compiler is available for most platforms and operation systems and is used almost everywhere as a default one.

Monday, September 21, 2009

c: double exclamation

Linux kernel is full of fascinating code. Not all of it is good but it's possible to find something interesting.
While reading the kernel code I've seen a lot of conditional expressions that contain double exclamations. Something like:

if (!!smth) {...}
I was curious what's the purpose of such expression.
And I've found that mostly this is used to make compiler happy and quite. Double exclamation or simply '!!' turns the expression value into binary representation: either '1' or '0'. Everything that could be treated as true results into '1' otherwise - '0'.
The simplest example to show it in action could be:
int i;

for (i = 0; i < 5; i++)
    printf("%d: %d\n", i, !!i);
This will print to the output:
0: 0
1: 1
2: 1
3: 1
4: 1
So, which compiler warnings this could help to suppress? That's very easy. Let's assume there's a function like following:
int function(void)
{                                                                                                     void *ptr;
    /* Useful calculations */
    return ptr;
The compiler, at least gcc, will warn that return expression makes integer from pointer without a cast. If function should return '1' on success and '0' on failure, double exclamation fits very well:
int function(void)
{                                                                                                     void *ptr;
    /* Useful calculations */
    return !!ptr;
Besides this simple example the double exclamation expression has indeed wide range of application.