blob: 2ce8b4bb711d03642ab0f581dbd8a99b264a1e07 [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
Konstantin Belousov459b2a52010-04-04 17:09:33 +030026#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
Tommi Rantala6b55e0a2012-09-19 13:50:59 +030030#include "compiler.h"
31
Konstantin Belousov459b2a52010-04-04 17:09:33 +030032#include <signal.h>
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000033#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include <sys/time.h>
39
40#define UNW_LOCAL_ONLY
41#include <libunwind.h>
42
David Mosberger-Tang5f3d2952007-05-16 13:19:46 -060043static const int nerrors_max = 100;
44
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000045struct itimerval interval =
46 {
47 .it_interval = { .tv_sec = 0, .tv_usec = 0 },
48 .it_value = { .tv_sec = 0, .tv_usec = 1000 }
49 };
50
51int verbose;
52int nerrors;
53int sigcount;
54
Paul Pluzhnikovb7e34442009-09-25 14:17:35 -070055#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. */
60int recurcount;
61#endif
62
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000063#define panic(args...) \
64 { ++nerrors; fprintf (stderr, args); return; }
65
66static void
67do_backtrace (int may_print, int get_proc_name)
68{
69 char buf[512], name[256];
70 unw_cursor_t cursor;
71 unw_word_t ip, sp, off;
72 unw_context_t uc;
73 int ret;
Jan Kratochvila72abd42007-05-16 13:16:31 -060074 int depth = 0;
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000075
Paul Pluzhnikovb7e34442009-09-25 14:17:35 -070076#ifndef CONFIG_BLOCK_SIGNALS
77 if (recurcount > 0)
78 return;
79 recurcount += 1;
80#endif
81
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000082 unw_getcontext (&uc);
83 if (unw_init_local (&cursor, &uc) < 0)
84 panic ("unw_init_local failed!\n");
85
86 do
87 {
88 unw_get_reg (&cursor, UNW_REG_IP, &ip);
89 unw_get_reg (&cursor, UNW_REG_SP, &sp);
90
91 buf[0] = '\0';
92 if (get_proc_name || (may_print && verbose))
93 {
Tommi Rantala848ad532012-08-16 10:25:56 +030094 ret = unw_get_proc_name (&cursor, name, sizeof (name), &off);
95 if (ret == 0 && (may_print && verbose))
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000096 {
97 if (off)
98 snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off);
99 else
100 {
101 size_t len = strlen (name);
102 buf[0] = '<';
103 memcpy (buf + 1, name, len);
104 buf[len + 1] = '>';
105 buf[len + 2] = '\0';
106 }
107 }
108 }
109
110 if (may_print && verbose)
111 printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
112
113 ret = unw_step (&cursor);
114 if (ret < 0)
115 {
116 unw_get_reg (&cursor, UNW_REG_IP, &ip);
117 panic ("FAILURE: unw_step() returned %d for ip=%lx\n",
118 ret, (long) ip);
119 }
Jan Kratochvila72abd42007-05-16 13:16:31 -0600120 if (depth++ > 100)
121 {
122 panic ("FAILURE: unw_step() looping over %d iterations\n", depth);
123 break;
124 }
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000125 }
126 while (ret > 0);
Paul Pluzhnikovb7e34442009-09-25 14:17:35 -0700127
128#ifndef CONFIG_BLOCK_SIGNALS
129 recurcount -= 1;
130#endif
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000131}
132
133void
134sighandler (int signal)
135{
136 if (verbose)
137 printf ("sighandler(signal=%d, count=%d)\n", signal, sigcount);
138
139 do_backtrace (1, 1);
140
141 ++sigcount;
142
143 if (sigcount == 100)
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);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600147 else if (sigcount == 300 || nerrors > nerrors_max)
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000148 {
Jan Kratochvila72abd42007-05-16 13:16:31 -0600149 if (nerrors > nerrors_max)
150 panic ("Too many errors (%d)\n", nerrors);
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000151 if (nerrors)
152 {
153 fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
154 exit (-1);
155 }
156 if (verbose)
157 printf ("SUCCESS.\n");
158 exit (0);
159 }
160 setitimer (ITIMER_VIRTUAL, &interval, NULL);
161}
162
163int
Tommi Rantala6b55e0a2012-09-19 13:50:59 +0300164main (int argc, char **argv UNUSED)
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000165{
166 struct sigaction act;
167 long i = 0;
168
169 if (argc > 1)
170 verbose = 1;
171
172 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
173
174 memset (&act, 0, sizeof (act));
175 act.sa_handler = sighandler;
176 act.sa_flags = SA_SIGINFO;
177 sigaction (SIGVTALRM, &act, NULL);
178
179 setitimer (ITIMER_VIRTUAL, &interval, NULL);
180
181 while (1)
182 {
183 if (0 && verbose)
184 printf ("%s: starting backtrace\n", __FUNCTION__);
185 do_backtrace (0, (i++ % 100) == 0);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600186 if (nerrors > nerrors_max)
187 {
Tommi Rantala9a6d9212012-08-03 16:40:27 +0300188 fprintf (stderr, "Too many errors (%d)\n", nerrors);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600189 exit (-1);
190 }
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000191 }
Konstantin Belousov459b2a52010-04-04 17:09:33 +0300192 return (0);
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000193}