| mostang.com!davidm | 4471c1e | 2004-04-01 08:11:21 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2004 Hewlett-Packard Co |
| 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
| 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: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be |
| 14 | included in all copies or substantial portions of the Software. |
| 15 | |
| 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. */ |
| 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-Tang | 5f3d295 | 2007-05-16 13:19:46 -0600 | [diff] [blame^] | 36 | static const int nerrors_max = 100; |
| 37 | |
| mostang.com!davidm | 4471c1e | 2004-04-01 08:11:21 +0000 | [diff] [blame] | 38 | struct itimerval interval = |
| 39 | { |
| 40 | .it_interval = { .tv_sec = 0, .tv_usec = 0 }, |
| 41 | .it_value = { .tv_sec = 0, .tv_usec = 1000 } |
| 42 | }; |
| 43 | |
| 44 | int verbose; |
| 45 | int nerrors; |
| 46 | int sigcount; |
| 47 | |
| 48 | #define panic(args...) \ |
| 49 | { ++nerrors; fprintf (stderr, args); return; } |
| 50 | |
| 51 | static void |
| 52 | do_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 Kratochvil | a72abd4 | 2007-05-16 13:16:31 -0600 | [diff] [blame] | 59 | int depth = 0; |
| mostang.com!davidm | 4471c1e | 2004-04-01 08:11:21 +0000 | [diff] [blame] | 60 | |
| 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 Kratochvil | a72abd4 | 2007-05-16 13:16:31 -0600 | [diff] [blame] | 98 | if (depth++ > 100) |
| 99 | { |
| 100 | panic ("FAILURE: unw_step() looping over %d iterations\n", depth); |
| 101 | break; |
| 102 | } |
| mostang.com!davidm | 4471c1e | 2004-04-01 08:11:21 +0000 | [diff] [blame] | 103 | } |
| 104 | while (ret > 0); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | sighandler (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 Kratochvil | a72abd4 | 2007-05-16 13:16:31 -0600 | [diff] [blame] | 121 | else if (sigcount == 300 || nerrors > nerrors_max) |
| mostang.com!davidm | 4471c1e | 2004-04-01 08:11:21 +0000 | [diff] [blame] | 122 | { |
| Jan Kratochvil | a72abd4 | 2007-05-16 13:16:31 -0600 | [diff] [blame] | 123 | if (nerrors > nerrors_max) |
| 124 | panic ("Too many errors (%d)\n", nerrors); |
| mostang.com!davidm | 4471c1e | 2004-04-01 08:11:21 +0000 | [diff] [blame] | 125 | 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 | |
| 137 | int |
| 138 | main (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 Kratochvil | a72abd4 | 2007-05-16 13:16:31 -0600 | [diff] [blame] | 160 | if (nerrors > nerrors_max) |
| 161 | { |
| 162 | panic ("Too many errors (%d)\n", nerrors); |
| 163 | exit (-1); |
| 164 | } |
| mostang.com!davidm | 4471c1e | 2004-04-01 08:11:21 +0000 | [diff] [blame] | 165 | } |
| 166 | } |