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) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
00003    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>.
00004    This file is part of the GNU C Library.
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 <stddef.h>
00025 #include <stdlib.h>
00026 #include <unwind.h>
00027 
00028 /* This is a global variable set at program start time.  It marks the
00029    highest used stack address.  */
00030 extern void *__libc_stack_end;
00031 
00032 
00033 /* This is the stack layout we see for every non-leaf function.
00034            size                    offset
00035     %r15 ->    +------------------+
00036              4 | back chain       |  0
00037              4 | end of stack     |  4
00038              8 | glue             |  8
00039              8 | scratch          | 16
00040             40 | save area r6-r15 | 24
00041             16 | save area f4,f6  | 64
00042             16 | empty            | 80
00043                +------------------+
00044    r14 in the save area holds the return address.
00045 */
00046 
00047 struct layout
00048 {
00049   int back_chain;
00050   int end_of_stack;
00051   int glue[2];
00052   int scratch[2];
00053   int save_grps[10];
00054   int save_fp[4];
00055   int empty[2];
00056 };
00057 
00058 struct trace_arg
00059 {
00060   void **array;
00061   int cnt, size;
00062 };
00063 
00064 #ifdef SHARED
00065 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
00066 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
00067 
00068 static void
00069 init (void)
00070 {
00071   void *handle = __libc_dlopen ("libgcc_s.so.1");
00072 
00073   if (handle == NULL)
00074     return;
00075 
00076   unwind_backtrace = __libc_dlsym (handle, "_Unwind_Backtrace");
00077   unwind_getip = __libc_dlsym (handle, "_Unwind_GetIP");
00078   if (unwind_getip == NULL)
00079     unwind_backtrace = NULL;
00080 }
00081 #else
00082 # define unwind_backtrace _Unwind_Backtrace
00083 # define unwind_getip _Unwind_GetIP
00084 #endif
00085 
00086 static int
00087 __backchain_backtrace (void **array, int size)
00088 {
00089   /* We assume that all the code is generated with frame pointers set.  */
00090   struct layout *stack;
00091   int cnt = 0;
00092 
00093   asm ("LR  %0,%%r15" : "=d" (stack) );
00094   /* We skip the call to this function, it makes no sense to record it.  */
00095   stack = (struct layout *) stack->back_chain;
00096   while (cnt < size)
00097     {
00098       if (stack == NULL || (void *) stack > __libc_stack_end)
00099        /* This means the address is out of range.  Note that for the
00100           toplevel we see a frame pointer with value NULL which clearly is
00101           out of range.  */
00102        break;
00103 
00104       array[cnt++] = (void *) (stack->save_grps[8] & 0x7fffffff);
00105 
00106       stack = (struct layout *) stack->back_chain;
00107     }
00108 
00109   return cnt;
00110 }
00111 
00112 static _Unwind_Reason_Code
00113 backtrace_helper (struct _Unwind_Context *ctx, void *a)
00114 {
00115   struct trace_arg *arg = a;
00116 
00117   /* We are first called with address in the __backtrace function.
00118      Skip it.  */
00119   if (arg->cnt != -1)
00120     arg->array[arg->cnt] = (void *) unwind_getip (ctx);
00121   if (++arg->cnt == arg->size)
00122     return _URC_END_OF_STACK;
00123   return _URC_NO_REASON;
00124 }
00125 
00126 int
00127 __backtrace (void **array, int size)
00128 {
00129   struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
00130 #ifdef SHARED
00131   __libc_once_define (static, once);
00132 
00133   __libc_once (once, init);
00134 #endif
00135   if (unwind_backtrace == NULL)
00136     return __backchain_backtrace (array, size);
00137 
00138   if (size >= 1)
00139     unwind_backtrace (backtrace_helper, &arg);
00140 
00141   return arg.cnt != -1 ? arg.cnt : 0;
00142 }
00143 
00144 weak_alias (__backtrace, backtrace)
00145 libc_hidden_def (__backtrace)