Back to index

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