| 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 | |
| 36 | struct itimerval interval = |
| 37 | { |
| 38 | .it_interval = { .tv_sec = 0, .tv_usec = 0 }, |
| 39 | .it_value = { .tv_sec = 0, .tv_usec = 1000 } |
| 40 | }; |
| 41 | |
| 42 | int verbose; |
| 43 | int nerrors; |
| 44 | int sigcount; |
| 45 | |
| 46 | #define panic(args...) \ |
| 47 | { ++nerrors; fprintf (stderr, args); return; } |
| 48 | |
| 49 | static void |
| 50 | do_backtrace (int may_print, int get_proc_name) |
| 51 | { |
| 52 | char buf[512], name[256]; |
| 53 | unw_cursor_t cursor; |
| 54 | unw_word_t ip, sp, off; |
| 55 | unw_context_t uc; |
| 56 | int ret; |
| 57 | |
| 58 | unw_getcontext (&uc); |
| 59 | if (unw_init_local (&cursor, &uc) < 0) |
| 60 | panic ("unw_init_local failed!\n"); |
| 61 | |
| 62 | do |
| 63 | { |
| 64 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 65 | unw_get_reg (&cursor, UNW_REG_SP, &sp); |
| 66 | |
| 67 | buf[0] = '\0'; |
| 68 | if (get_proc_name || (may_print && verbose)) |
| 69 | { |
| 70 | if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0) |
| 71 | { |
| 72 | if (off) |
| 73 | snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off); |
| 74 | else |
| 75 | { |
| 76 | size_t len = strlen (name); |
| 77 | buf[0] = '<'; |
| 78 | memcpy (buf + 1, name, len); |
| 79 | buf[len + 1] = '>'; |
| 80 | buf[len + 2] = '\0'; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (may_print && verbose) |
| 86 | printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp); |
| 87 | |
| 88 | ret = unw_step (&cursor); |
| 89 | if (ret < 0) |
| 90 | { |
| 91 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 92 | panic ("FAILURE: unw_step() returned %d for ip=%lx\n", |
| 93 | ret, (long) ip); |
| 94 | } |
| 95 | } |
| 96 | while (ret > 0); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | sighandler (int signal) |
| 101 | { |
| 102 | if (verbose) |
| 103 | printf ("sighandler(signal=%d, count=%d)\n", signal, sigcount); |
| 104 | |
| 105 | do_backtrace (1, 1); |
| 106 | |
| 107 | ++sigcount; |
| 108 | |
| 109 | if (sigcount == 100) |
| 110 | unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL); |
| 111 | else if (sigcount == 200) |
| 112 | unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD); |
| 113 | else if (sigcount == 300) |
| 114 | { |
| 115 | if (nerrors) |
| 116 | { |
| 117 | fprintf (stderr, "FAILURE: detected %d errors\n", nerrors); |
| 118 | exit (-1); |
| 119 | } |
| 120 | if (verbose) |
| 121 | printf ("SUCCESS.\n"); |
| 122 | exit (0); |
| 123 | } |
| 124 | setitimer (ITIMER_VIRTUAL, &interval, NULL); |
| 125 | } |
| 126 | |
| 127 | int |
| 128 | main (int argc, char **argv) |
| 129 | { |
| 130 | struct sigaction act; |
| 131 | long i = 0; |
| 132 | |
| 133 | if (argc > 1) |
| 134 | verbose = 1; |
| 135 | |
| 136 | unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE); |
| 137 | |
| 138 | memset (&act, 0, sizeof (act)); |
| 139 | act.sa_handler = sighandler; |
| 140 | act.sa_flags = SA_SIGINFO; |
| 141 | sigaction (SIGVTALRM, &act, NULL); |
| 142 | |
| 143 | setitimer (ITIMER_VIRTUAL, &interval, NULL); |
| 144 | |
| 145 | while (1) |
| 146 | { |
| 147 | if (0 && verbose) |
| 148 | printf ("%s: starting backtrace\n", __FUNCTION__); |
| 149 | do_backtrace (0, (i++ % 100) == 0); |
| 150 | } |
| 151 | } |