Back to index

glibc  2.9
Defines | Functions
backtracesyms.c File Reference
#include <assert.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldsodefs.h>

Go to the source code of this file.

Defines

#define WORD_WIDTH   16

Functions

char ** __backtrace_symbols (void *const *array, int size)

Define Documentation

#define WORD_WIDTH   16

Definition at line 33 of file backtracesyms.c.


Function Documentation

char** __backtrace_symbols ( void *const array,
int  size 
)

Definition at line 38 of file backtracesyms.c.

{
  Dl_info info[size];
  int status[size];
  int cnt;
  size_t total = 0;
  char **result;

  /* Fill in the information we can get from `dladdr'.  */
  for (cnt = 0; cnt < size; ++cnt)
    {
      status[cnt] = _dl_addr (array[cnt], &info[cnt], NULL, NULL);
      if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
       /* We have some info, compute the length of the string which will be
          "<file-name>(<sym-name>) [+offset].  */
       total += (strlen (info[cnt].dli_fname ?: "")
                + (info[cnt].dli_sname
                   ? strlen (info[cnt].dli_sname) + 3 + WORD_WIDTH + 3
                   : 1)
                + WORD_WIDTH + 5);
      else
       total += 5 + WORD_WIDTH;
    }

  /* Allocate memory for the result.  */
  result = (char **) malloc (size * sizeof (char *) + total);
  if (result != NULL)
    {
      char *last = (char *) (result + size);

      for (cnt = 0; cnt < size; ++cnt)
       {
         result[cnt] = last;

         if (status[cnt] && info[cnt].dli_fname
             && info[cnt].dli_fname[0] != '\0')
           {
             char buf[20];

             if (array[cnt] >= (void *) info[cnt].dli_saddr)
              sprintf (buf, "+%#lx",
                      (unsigned long)(array[cnt] - info[cnt].dli_saddr));
             else
              sprintf (buf, "-%#lx",
                      (unsigned long)(info[cnt].dli_saddr - array[cnt]));

             last += 1 + sprintf (last, "%s%s%s%s%s[%p]",
                               info[cnt].dli_fname ?: "",
                               info[cnt].dli_sname ? "(" : "",
                               info[cnt].dli_sname ?: "",
                               info[cnt].dli_sname ? buf : "",
                               info[cnt].dli_sname ? ") " : " ",
                               array[cnt]);
           }
         else
           last += 1 + sprintf (last, "[%p]", array[cnt]);
       }
      assert (last <= (char *) result + size * sizeof (char *) + total);
    }

  return result;
}

Here is the call graph for this function: