Back to index

glibc  2.9
backtracesyms.c
Go to the documentation of this file.
00001 /* Return list with names for address in backtrace.
00002    Copyright (C) 1998,1999,2000,2001,2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <assert.h>
00022 #include <execinfo.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include <ldsodefs.h>
00028 
00029 #if __ELF_NATIVE_CLASS == 32
00030 # define WORD_WIDTH 8
00031 #else
00032 /* We assyme 64bits.  */
00033 # define WORD_WIDTH 16
00034 #endif
00035 
00036 
00037 char **
00038 __backtrace_symbols (array, size)
00039      void *const *array;
00040      int size;
00041 {
00042   Dl_info info[size];
00043   int status[size];
00044   int cnt;
00045   size_t total = 0;
00046   char **result;
00047 
00048   /* Fill in the information we can get from `dladdr'.  */
00049   for (cnt = 0; cnt < size; ++cnt)
00050     {
00051       status[cnt] = _dl_addr (array[cnt], &info[cnt], NULL, NULL);
00052       if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
00053        /* We have some info, compute the length of the string which will be
00054           "<file-name>(<sym-name>) [+offset].  */
00055        total += (strlen (info[cnt].dli_fname ?: "")
00056                 + (info[cnt].dli_sname
00057                    ? strlen (info[cnt].dli_sname) + 3 + WORD_WIDTH + 3
00058                    : 1)
00059                 + WORD_WIDTH + 5);
00060       else
00061        total += 5 + WORD_WIDTH;
00062     }
00063 
00064   /* Allocate memory for the result.  */
00065   result = (char **) malloc (size * sizeof (char *) + total);
00066   if (result != NULL)
00067     {
00068       char *last = (char *) (result + size);
00069 
00070       for (cnt = 0; cnt < size; ++cnt)
00071        {
00072          result[cnt] = last;
00073 
00074          if (status[cnt] && info[cnt].dli_fname
00075              && info[cnt].dli_fname[0] != '\0')
00076            {
00077              char buf[20];
00078 
00079              if (array[cnt] >= (void *) info[cnt].dli_saddr)
00080               sprintf (buf, "+%#lx",
00081                       (unsigned long)(array[cnt] - info[cnt].dli_saddr));
00082              else
00083               sprintf (buf, "-%#lx",
00084                       (unsigned long)(info[cnt].dli_saddr - array[cnt]));
00085 
00086              last += 1 + sprintf (last, "%s%s%s%s%s[%p]",
00087                                info[cnt].dli_fname ?: "",
00088                                info[cnt].dli_sname ? "(" : "",
00089                                info[cnt].dli_sname ?: "",
00090                                info[cnt].dli_sname ? buf : "",
00091                                info[cnt].dli_sname ? ") " : " ",
00092                                array[cnt]);
00093            }
00094          else
00095            last += 1 + sprintf (last, "[%p]", array[cnt]);
00096        }
00097       assert (last <= (char *) result + size * sizeof (char *) + total);
00098     }
00099 
00100   return result;
00101 }
00102 weak_alias (__backtrace_symbols, backtrace_symbols)