Emulate 'futimesat' when __NR_futimesat is not available.
[uclibc-ng.git] / libubacktrace / backtrace.c
blob08a7010e7ed689e4d5f688f3317c3878b5efd7f9
1 /*
2 * Perform stack unwinding by using the _Unwind_Backtrace.
4 * User application that wants to use backtrace needs to be
5 * compiled with -fasynchronous-unwind-tables option and -rdynamic to get full
6 * symbols printed.
8 * Copyright (C) 2009, 2010 STMicroelectronics Ltd.
10 * Author(s): Giuseppe Cavallaro <peppe.cavallaro@st.com>
11 * - Initial implementation for glibc
13 * Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
14 * - Reworked for uClibc
15 * - use dlsym/dlopen from libdl
16 * - rewrite initialisation to not use libc_once
17 * - make it available in static link too
19 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
23 #include <libgcc_s.h>
24 #include <execinfo.h>
25 #include <dlfcn.h>
26 #include <stdlib.h>
27 #include <unwind.h>
28 #include <assert.h>
29 #include <stdio.h>
31 struct trace_arg
33 void **array;
34 int cnt, size;
37 #ifdef SHARED
38 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
39 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
41 static void backtrace_init (void)
43 void *handle = dlopen (LIBGCC_S_SO, RTLD_LAZY);
45 if (handle == NULL
46 || ((unwind_backtrace = dlsym (handle, "_Unwind_Backtrace")) == NULL)
47 || ((unwind_getip = dlsym (handle, "_Unwind_GetIP")) == NULL)) {
48 printf(LIBGCC_S_SO " must be installed for backtrace to work\n");
49 abort();
52 #else
53 # define unwind_backtrace _Unwind_Backtrace
54 # define unwind_getip _Unwind_GetIP
55 #endif
57 static _Unwind_Reason_Code
58 backtrace_helper (struct _Unwind_Context *ctx, void *a)
60 struct trace_arg *arg = a;
62 assert (unwind_getip != NULL);
64 /* We are first called with address in the __backtrace function. Skip it. */
65 if (arg->cnt != -1)
66 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
67 if (++arg->cnt == arg->size)
68 return _URC_END_OF_STACK;
69 return _URC_NO_REASON;
73 * Perform stack unwinding by using the _Unwind_Backtrace.
76 int backtrace (void **array, int size)
78 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
80 #ifdef SHARED
81 if (unwind_backtrace == NULL)
82 backtrace_init();
83 #endif
85 if (size >= 1)
86 unwind_backtrace (backtrace_helper, &arg);
88 return arg.cnt != -1 ? arg.cnt : 0;