chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / ia64 / backtrace.c
1 /* Return backtrace of current program state.
2    Copyright (C) 2003, 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <bits/libc-lock.h>
22 #include <dlfcn.h>
23 #include <execinfo.h>
24 #include <stdlib.h>
25 #include <unwind.h>
26
27 struct trace_arg
28 {
29   void **array;
30   int cnt, size;
31 };
32
33 #ifdef SHARED
34 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
35 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
36 static void *libgcc_handle;
37
38 static void
39 init (void)
40 {
41   libgcc_handle = __libc_dlopen ("libgcc_s.so.1");
42
43   if (libgcc_handle == NULL)
44     return;
45
46   unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
47   unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
48   if (unwind_getip == NULL)
49     unwind_backtrace = NULL;
50 }
51 #else
52 # define unwind_backtrace _Unwind_Backtrace
53 # define unwind_getip _Unwind_GetIP
54 #endif
55
56 static _Unwind_Reason_Code
57 backtrace_helper (struct _Unwind_Context *ctx, void *a)
58 {
59   struct trace_arg *arg = a;
60
61   /* We are first called with address in the __backtrace function.
62      Skip it.  */
63   if (arg->cnt != -1)
64     {
65       arg->array[arg->cnt] = (void *) unwind_getip (ctx);
66
67       /* Check whether we make any progress.  */
68       if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt])
69         return _URC_END_OF_STACK;
70     }
71   if (++arg->cnt == arg->size)
72     return _URC_END_OF_STACK;
73   return _URC_NO_REASON;
74 }
75
76 int
77 __backtrace (array, size)
78      void **array;
79      int size;
80 {
81   struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
82 #ifdef SHARED
83   __libc_once_define (static, once);
84
85   __libc_once (once, init);
86   if (unwind_backtrace == NULL)
87     return 0;
88 #endif
89
90   if (size >= 1)
91     unwind_backtrace (backtrace_helper, &arg);
92
93   /* _Unwind_Backtrace on IA-64 seems to put NULL address above
94      _start.  Fix it up here.  */
95   if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
96     --arg.cnt;
97   return arg.cnt != -1 ? arg.cnt : 0;
98 }
99 weak_alias (__backtrace, backtrace)
100 libc_hidden_def (__backtrace)
101
102
103 #ifdef SHARED
104 /* Free all resources if necessary.  */
105 libc_freeres_fn (free_mem)
106 {
107   unwind_backtrace = NULL;
108   if (libgcc_handle != NULL)
109     {
110       __libc_dlclose (libgcc_handle);
111       libgcc_handle = NULL;
112     }
113 }
114 #endif