1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 /* Check whether basic unwinding truly is async-signal safe. */
40 #define UNW_LOCAL_ONLY
41 #include <libunwind.h>
43 static const int nerrors_max = 100;
45 struct itimerval interval =
47 .it_interval = { .tv_sec = 0, .tv_usec = 0 },
48 .it_value = { .tv_sec = 0, .tv_usec = 1000 }
55 #ifndef CONFIG_BLOCK_SIGNALS
56 /* When libunwind is configured with --enable-block-signals=no, the caller
57 is responsible for preventing recursion via signal handlers.
58 We use a simple global here. In a multithreaded program, one would use
59 a thread-local variable. */
63 #define panic(args...) \
64 { ++nerrors; fprintf (stderr, args); return; }
67 do_backtrace (int may_print, int get_proc_name)
69 char buf[512], name[256];
71 unw_word_t ip, sp, off;
76 #ifndef CONFIG_BLOCK_SIGNALS
83 if (unw_init_local (&cursor, &uc) < 0)
84 panic ("unw_init_local failed!\n");
88 unw_get_reg (&cursor, UNW_REG_IP, &ip);
89 unw_get_reg (&cursor, UNW_REG_SP, &sp);
92 if (get_proc_name || (may_print && verbose))
94 ret = unw_get_proc_name (&cursor, name, sizeof (name), &off);
95 if (ret == 0 && (may_print && verbose))
98 snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off);
101 size_t len = strlen (name);
103 memcpy (buf + 1, name, len);
110 if (may_print && verbose)
111 printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
113 ret = unw_step (&cursor);
116 unw_get_reg (&cursor, UNW_REG_IP, &ip);
117 panic ("FAILURE: unw_step() returned %d for ip=%lx\n",
122 panic ("FAILURE: unw_step() looping over %d iterations\n", depth);
128 #ifndef CONFIG_BLOCK_SIGNALS
134 sighandler (int signal)
137 printf ("sighandler(signal=%d, count=%d)\n", signal, sigcount);
144 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
145 else if (sigcount == 200)
146 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
147 else if (sigcount == 300 || nerrors > nerrors_max)
149 if (nerrors > nerrors_max)
150 panic ("Too many errors (%d)\n", nerrors);
153 fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
157 printf ("SUCCESS.\n");
160 setitimer (ITIMER_VIRTUAL, &interval, NULL);
164 main (int argc, char **argv UNUSED)
166 struct sigaction act;
172 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
174 memset (&act, 0, sizeof (act));
175 act.sa_handler = sighandler;
176 act.sa_flags = SA_SIGINFO;
177 sigaction (SIGVTALRM, &act, NULL);
179 setitimer (ITIMER_VIRTUAL, &interval, NULL);
184 printf ("%s: starting backtrace\n", __FUNCTION__);
185 do_backtrace (0, (i++ % 100) == 0);
186 if (nerrors > nerrors_max)
188 fprintf (stderr, "Too many errors (%d)\n", nerrors);