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