Back to index

glibc  2.9
backtracesymsfd.c
Go to the documentation of this file.
00001 /* Write formatted list with names for addresses in backtrace to a file.
00002    Copyright (C) 1998, 2000, 2003, 2005 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 <execinfo.h>
00022 #include <string.h>
00023 #include <sys/uio.h>
00024 
00025 #include <stdio-common/_itoa.h>
00026 #include <ldsodefs.h>
00027 
00028 #if __ELF_NATIVE_CLASS == 32
00029 # define WORD_WIDTH 8
00030 #else
00031 /* We assyme 64bits.  */
00032 # define WORD_WIDTH 16
00033 #endif
00034 
00035 
00036 void
00037 __backtrace_symbols_fd (array, size, fd)
00038      void *const *array;
00039      int size;
00040      int fd;
00041 {
00042   struct iovec iov[9];
00043   int cnt;
00044 
00045   for (cnt = 0; cnt < size; ++cnt)
00046     {
00047       char buf[WORD_WIDTH];
00048       Dl_info info;
00049       size_t last = 0;
00050 
00051       if (_dl_addr (array[cnt], &info, NULL, NULL)
00052          && info.dli_fname && info.dli_fname[0] != '\0')
00053        {
00054          /* Name of the file.  */
00055          iov[0].iov_base = (void *) info.dli_fname;
00056          iov[0].iov_len = strlen (info.dli_fname);
00057          last = 1;
00058 
00059          /* Symbol name.  */
00060          if (info.dli_sname != NULL)
00061            {
00062              char buf2[WORD_WIDTH];
00063              size_t diff;
00064 
00065              iov[1].iov_base = (void *) "(";
00066              iov[1].iov_len = 1;
00067              iov[2].iov_base = (void *) info.dli_sname;
00068              iov[2].iov_len = strlen (info.dli_sname);
00069 
00070              if (array[cnt] >= (void *) info.dli_saddr)
00071               {
00072                 iov[3].iov_base = (void *) "+0x";
00073                 diff = array[cnt] - info.dli_saddr;
00074               }
00075              else
00076               {
00077                 iov[3].iov_base = (void *) "-0x";
00078                 diff = info.dli_saddr - array[cnt];
00079               }
00080              iov[3].iov_len = 3;
00081 
00082              iov[4].iov_base = _itoa_word ((unsigned long int) diff,
00083                                        &buf2[WORD_WIDTH], 16, 0);
00084              iov[4].iov_len = &buf2[WORD_WIDTH] - (char *) iov[4].iov_base;
00085 
00086              iov[5].iov_base = (void *) ")";
00087              iov[5].iov_len = 1;
00088 
00089              last = 6;
00090            }
00091        }
00092 
00093       iov[last].iov_base = (void *) "[0x";
00094       iov[last].iov_len = 3;
00095       ++last;
00096 
00097       iov[last].iov_base = _itoa_word ((unsigned long int) array[cnt],
00098                                    &buf[WORD_WIDTH], 16, 0);
00099       iov[last].iov_len = &buf[WORD_WIDTH] - (char *) iov[last].iov_base;
00100       ++last;
00101 
00102       iov[last].iov_base = (void *) "]\n";
00103       iov[last].iov_len = 2;
00104       ++last;
00105 
00106       __writev (fd, iov, last);
00107     }
00108 }
00109 weak_alias (__backtrace_symbols_fd, backtrace_symbols_fd)
00110 libc_hidden_def (__backtrace_symbols_fd)