blob: eaa9ce9049014a3e34241767be7e81cc59fc08f0 [file] [log] [blame]
mostang.com!davidm4471c1e2004-04-01 08:11:21 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5Permission is hereby granted, free of charge, to any person obtaining
6a copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sublicense, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
12
13The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24/* Check whether basic unwinding truly is async-signal safe. */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31#include <sys/time.h>
32
33#define UNW_LOCAL_ONLY
34#include <libunwind.h>
Paul Pluzhnikovb7e34442009-09-25 14:17:35 -070035#include "config.h"
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000036
David Mosberger-Tang5f3d2952007-05-16 13:19:46 -060037static const int nerrors_max = 100;
38
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000039struct itimerval interval =
40 {
41 .it_interval = { .tv_sec = 0, .tv_usec = 0 },
42 .it_value = { .tv_sec = 0, .tv_usec = 1000 }
43 };
44
45int verbose;
46int nerrors;
47int sigcount;
48
Paul Pluzhnikovb7e34442009-09-25 14:17:35 -070049#ifndef CONFIG_BLOCK_SIGNALS
50/* When libunwind is configured with --enable-block-signals=no, the caller
51 is responsible for preventing recursion via signal handlers.
52 We use a simple global here. In a multithreaded program, one would use
53 a thread-local variable. */
54int recurcount;
55#endif
56
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000057#define panic(args...) \
58 { ++nerrors; fprintf (stderr, args); return; }
59
60static void
61do_backtrace (int may_print, int get_proc_name)
62{
63 char buf[512], name[256];
64 unw_cursor_t cursor;
65 unw_word_t ip, sp, off;
66 unw_context_t uc;
67 int ret;
Jan Kratochvila72abd42007-05-16 13:16:31 -060068 int depth = 0;
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000069
Paul Pluzhnikovb7e34442009-09-25 14:17:35 -070070#ifndef CONFIG_BLOCK_SIGNALS
71 if (recurcount > 0)
72 return;
73 recurcount += 1;
74#endif
75
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000076 unw_getcontext (&uc);
77 if (unw_init_local (&cursor, &uc) < 0)
78 panic ("unw_init_local failed!\n");
79
80 do
81 {
82 unw_get_reg (&cursor, UNW_REG_IP, &ip);
83 unw_get_reg (&cursor, UNW_REG_SP, &sp);
84
85 buf[0] = '\0';
86 if (get_proc_name || (may_print && verbose))
87 {
88 if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0)
89 {
90 if (off)
91 snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off);
92 else
93 {
94 size_t len = strlen (name);
95 buf[0] = '<';
96 memcpy (buf + 1, name, len);
97 buf[len + 1] = '>';
98 buf[len + 2] = '\0';
99 }
100 }
101 }
102
103 if (may_print && verbose)
104 printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
105
106 ret = unw_step (&cursor);
107 if (ret < 0)
108 {
109 unw_get_reg (&cursor, UNW_REG_IP, &ip);
110 panic ("FAILURE: unw_step() returned %d for ip=%lx\n",
111 ret, (long) ip);
112 }
Jan Kratochvila72abd42007-05-16 13:16:31 -0600113 if (depth++ > 100)
114 {
115 panic ("FAILURE: unw_step() looping over %d iterations\n", depth);
116 break;
117 }
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000118 }
119 while (ret > 0);
Paul Pluzhnikovb7e34442009-09-25 14:17:35 -0700120
121#ifndef CONFIG_BLOCK_SIGNALS
122 recurcount -= 1;
123#endif
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000124}
125
126void
127sighandler (int signal)
128{
129 if (verbose)
130 printf ("sighandler(signal=%d, count=%d)\n", signal, sigcount);
131
132 do_backtrace (1, 1);
133
134 ++sigcount;
135
136 if (sigcount == 100)
137 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
138 else if (sigcount == 200)
139 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600140 else if (sigcount == 300 || nerrors > nerrors_max)
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000141 {
Jan Kratochvila72abd42007-05-16 13:16:31 -0600142 if (nerrors > nerrors_max)
143 panic ("Too many errors (%d)\n", nerrors);
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000144 if (nerrors)
145 {
146 fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
147 exit (-1);
148 }
149 if (verbose)
150 printf ("SUCCESS.\n");
151 exit (0);
152 }
153 setitimer (ITIMER_VIRTUAL, &interval, NULL);
154}
155
156int
157main (int argc, char **argv)
158{
159 struct sigaction act;
160 long i = 0;
161
162 if (argc > 1)
163 verbose = 1;
164
165 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
166
167 memset (&act, 0, sizeof (act));
168 act.sa_handler = sighandler;
169 act.sa_flags = SA_SIGINFO;
170 sigaction (SIGVTALRM, &act, NULL);
171
172 setitimer (ITIMER_VIRTUAL, &interval, NULL);
173
174 while (1)
175 {
176 if (0 && verbose)
177 printf ("%s: starting backtrace\n", __FUNCTION__);
178 do_backtrace (0, (i++ % 100) == 0);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600179 if (nerrors > nerrors_max)
180 {
181 panic ("Too many errors (%d)\n", nerrors);
182 exit (-1);
183 }
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000184 }
185}