Back to index

glibc  2.9
backtrace.c
Go to the documentation of this file.
00001 /* Return backtrace of current program state.
00002    Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 <bits/libc-lock.h>
00022 #include <dlfcn.h>
00023 #include <execinfo.h>
00024 #include <stdlib.h>
00025 #include <unwind.h>
00026 
00027 struct trace_arg
00028 {
00029   void **array;
00030   int cnt, size;
00031 };
00032 
00033 #ifdef SHARED
00034 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
00035 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
00036 static void *libgcc_handle;
00037 
00038 static void
00039 init (void)
00040 {
00041   libgcc_handle = __libc_dlopen ("libgcc_s.so.1");
00042 
00043   if (libgcc_handle == NULL)
00044     return;
00045 
00046   unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
00047   unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
00048   if (unwind_getip == NULL)
00049     unwind_backtrace = NULL;
00050 }
00051 #else
00052 # define unwind_backtrace _Unwind_Backtrace
00053 # define unwind_getip _Unwind_GetIP
00054 #endif
00055 
00056 static _Unwind_Reason_Code
00057 backtrace_helper (struct _Unwind_Context *ctx, void *a)
00058 {
00059   struct trace_arg *arg = a;
00060 
00061   /* We are first called with address in the __backtrace function.
00062      Skip it.  */
00063   if (arg->cnt != -1)
00064     arg->array[arg->cnt] = (void *) unwind_getip (ctx);
00065   if (++arg->cnt == arg->size)
00066     return _URC_END_OF_STACK;
00067   return _URC_NO_REASON;
00068 }
00069 
00070 int
00071 __backtrace (array, size)
00072      void **array;
00073      int size;
00074 {
00075   struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
00076 #ifdef SHARED
00077   __libc_once_define (static, once);
00078 
00079   __libc_once (once, init);
00080   if (unwind_backtrace == NULL)
00081     return 0;
00082 #endif
00083 
00084   if (size >= 1)
00085     unwind_backtrace (backtrace_helper, &arg);
00086 
00087   /* _Unwind_Backtrace on IA-64 seems to put NULL address above
00088      _start.  Fix it up here.  */
00089   if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
00090     --arg.cnt;
00091   return arg.cnt != -1 ? arg.cnt : 0;
00092 }
00093 weak_alias (__backtrace, backtrace)
00094 libc_hidden_def (__backtrace)
00095 
00096 
00097 #ifdef SHARED
00098 /* Free all resources if necessary.  */
00099 libc_freeres_fn (free_mem)
00100 {
00101   unwind_backtrace = NULL;
00102   if (libgcc_handle != NULL)
00103     {
00104       __libc_dlclose (libgcc_handle);
00105       libgcc_handle = NULL;
00106     }
00107 }
00108 #endif