1 /* Return backtrace of current program state.
2 Copyright (C) 2003-2005, 2007, 2009, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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.
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.
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
21 #include <bits/libc-lock.h>
36 static _Unwind_Reason_Code (*unwind_backtrace
) (_Unwind_Trace_Fn
, void *);
37 static _Unwind_Ptr (*unwind_getip
) (struct _Unwind_Context
*);
38 static _Unwind_Word (*unwind_getcfa
) (struct _Unwind_Context
*);
39 static void *libgcc_handle
;
42 /* Dummy version in case libgcc_s does not contain the real code. */
44 dummy_getcfa (struct _Unwind_Context
*ctx
__attribute__ ((unused
)))
53 libgcc_handle
= __libc_dlopen ("libgcc_s.so.1");
55 if (libgcc_handle
== NULL
)
58 unwind_backtrace
= __libc_dlsym (libgcc_handle
, "_Unwind_Backtrace");
59 unwind_getip
= __libc_dlsym (libgcc_handle
, "_Unwind_GetIP");
60 if (unwind_getip
== NULL
)
61 unwind_backtrace
= NULL
;
62 unwind_getcfa
= (__libc_dlsym (libgcc_handle
, "_Unwind_GetCFA")
66 # define unwind_backtrace _Unwind_Backtrace
67 # define unwind_getip _Unwind_GetIP
68 # define unwind_getcfa _Unwind_GetCFA
71 static _Unwind_Reason_Code
72 backtrace_helper (struct _Unwind_Context
*ctx
, void *a
)
74 struct trace_arg
*arg
= a
;
76 /* We are first called with address in the __backtrace function.
80 arg
->array
[arg
->cnt
] = (void *) unwind_getip (ctx
);
82 /* Check whether we make any progress. */
83 _Unwind_Word cfa
= unwind_getcfa (ctx
);
85 if (arg
->cnt
> 0 && arg
->array
[arg
->cnt
- 1] == arg
->array
[arg
->cnt
]
87 return _URC_END_OF_STACK
;
90 if (++arg
->cnt
== arg
->size
)
91 return _URC_END_OF_STACK
;
92 return _URC_NO_REASON
;
96 __backtrace (array
, size
)
100 struct trace_arg arg
= { .array
= array
, .cfa
= 0, .size
= size
, .cnt
= -1 };
102 __libc_once_define (static, once
);
104 __libc_once (once
, init
);
105 if (unwind_backtrace
== NULL
)
110 unwind_backtrace (backtrace_helper
, &arg
);
112 /* _Unwind_Backtrace on IA-64 seems to put NULL address above
113 _start. Fix it up here. */
114 if (arg
.cnt
> 1 && arg
.array
[arg
.cnt
- 1] == NULL
)
116 return arg
.cnt
!= -1 ? arg
.cnt
: 0;
118 weak_alias (__backtrace
, backtrace
)
119 libc_hidden_def (__backtrace
)
123 /* Free all resources if necessary. */
124 libc_freeres_fn (free_mem
)
126 unwind_backtrace
= NULL
;
127 if (libgcc_handle
!= NULL
)
129 __libc_dlclose (libgcc_handle
);
130 libgcc_handle
= NULL
;