blob: 9309076c275b659d9fba40e4dc76f8d00dfd49ea [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>
35
David Mosberger-Tang5f3d2952007-05-16 13:19:46 -060036static const int nerrors_max = 100;
37
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000038struct itimerval interval =
39 {
40 .it_interval = { .tv_sec = 0, .tv_usec = 0 },
41 .it_value = { .tv_sec = 0, .tv_usec = 1000 }
42 };
43
44int verbose;
45int nerrors;
46int sigcount;
47
48#define panic(args...) \
49 { ++nerrors; fprintf (stderr, args); return; }
50
51static void
52do_backtrace (int may_print, int get_proc_name)
53{
54 char buf[512], name[256];
55 unw_cursor_t cursor;
56 unw_word_t ip, sp, off;
57 unw_context_t uc;
58 int ret;
Jan Kratochvila72abd42007-05-16 13:16:31 -060059 int depth = 0;
mostang.com!davidm4471c1e2004-04-01 08:11:21 +000060
61 unw_getcontext (&uc);
62 if (unw_init_local (&cursor, &uc) < 0)
63 panic ("unw_init_local failed!\n");
64
65 do
66 {
67 unw_get_reg (&cursor, UNW_REG_IP, &ip);
68 unw_get_reg (&cursor, UNW_REG_SP, &sp);
69
70 buf[0] = '\0';
71 if (get_proc_name || (may_print && verbose))
72 {
73 if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0)
74 {
75 if (off)
76 snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off);
77 else
78 {
79 size_t len = strlen (name);
80 buf[0] = '<';
81 memcpy (buf + 1, name, len);
82 buf[len + 1] = '>';
83 buf[len + 2] = '\0';
84 }
85 }
86 }
87
88 if (may_print && verbose)
89 printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
90
91 ret = unw_step (&cursor);
92 if (ret < 0)
93 {
94 unw_get_reg (&cursor, UNW_REG_IP, &ip);
95 panic ("FAILURE: unw_step() returned %d for ip=%lx\n",
96 ret, (long) ip);
97 }
Jan Kratochvila72abd42007-05-16 13:16:31 -060098 if (depth++ > 100)
99 {
100 panic ("FAILURE: unw_step() looping over %d iterations\n", depth);
101 break;
102 }
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000103 }
104 while (ret > 0);
105}
106
107void
108sighandler (int signal)
109{
110 if (verbose)
111 printf ("sighandler(signal=%d, count=%d)\n", signal, sigcount);
112
113 do_backtrace (1, 1);
114
115 ++sigcount;
116
117 if (sigcount == 100)
118 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
119 else if (sigcount == 200)
120 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600121 else if (sigcount == 300 || nerrors > nerrors_max)
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000122 {
Jan Kratochvila72abd42007-05-16 13:16:31 -0600123 if (nerrors > nerrors_max)
124 panic ("Too many errors (%d)\n", nerrors);
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000125 if (nerrors)
126 {
127 fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
128 exit (-1);
129 }
130 if (verbose)
131 printf ("SUCCESS.\n");
132 exit (0);
133 }
134 setitimer (ITIMER_VIRTUAL, &interval, NULL);
135}
136
137int
138main (int argc, char **argv)
139{
140 struct sigaction act;
141 long i = 0;
142
143 if (argc > 1)
144 verbose = 1;
145
146 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
147
148 memset (&act, 0, sizeof (act));
149 act.sa_handler = sighandler;
150 act.sa_flags = SA_SIGINFO;
151 sigaction (SIGVTALRM, &act, NULL);
152
153 setitimer (ITIMER_VIRTUAL, &interval, NULL);
154
155 while (1)
156 {
157 if (0 && verbose)
158 printf ("%s: starting backtrace\n", __FUNCTION__);
159 do_backtrace (0, (i++ % 100) == 0);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600160 if (nerrors > nerrors_max)
161 {
162 panic ("Too many errors (%d)\n", nerrors);
163 exit (-1);
164 }
mostang.com!davidm4471c1e2004-04-01 08:11:21 +0000165 }
166}