mostang.com!davidm | 716484f | 2003-11-20 01:10:03 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2003 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 | /* Verify that multi-threaded concurrent unwinding works as expected. */ |
| 25 | |
| 26 | #ifdef HAVE_CONFIG_H |
| 27 | # include "config.h" |
| 28 | #endif |
| 29 | |
| 30 | #include <libunwind.h> |
| 31 | #include <pthread.h> |
| 32 | #include <signal.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #define NTHREADS 128 |
| 38 | |
| 39 | #define panic(args...) \ |
| 40 | do { fprintf (stderr, args); ++nerrors; } while (0) |
| 41 | |
| 42 | int verbose; |
| 43 | int nerrors; |
| 44 | int got_usr1, got_usr2; |
| 45 | char *sigusr1_sp; |
| 46 | |
| 47 | void |
| 48 | handler (int sig) |
| 49 | { |
| 50 | unw_word_t ip; |
| 51 | unw_context_t uc; |
| 52 | unw_cursor_t c; |
| 53 | int ret; |
| 54 | |
| 55 | unw_getcontext(&uc); |
| 56 | unw_init_local(&c, &uc); |
| 57 | do |
| 58 | { |
| 59 | unw_get_reg(&c, UNW_REG_IP, &ip); |
| 60 | if (verbose) |
hp.com!davidm | 94f198b | 2003-11-24 21:37:22 +0000 | [diff] [blame] | 61 | printf ("%lx: IP=%lx\n", (long) pthread_self (), (unsigned long) ip); |
mostang.com!davidm | 716484f | 2003-11-20 01:10:03 +0000 | [diff] [blame] | 62 | } |
| 63 | while ((ret = unw_step(&c)) > 0); |
| 64 | |
| 65 | if (ret < 0) |
| 66 | panic ("unw_step() returned %d\n", ret); |
| 67 | } |
| 68 | |
| 69 | void * |
| 70 | worker (void *arg) |
| 71 | { |
| 72 | signal (SIGUSR1, handler); |
| 73 | |
| 74 | if (verbose) |
| 75 | printf ("sending SIGUSR1\n"); |
| 76 | pthread_kill (pthread_self (), SIGUSR1); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | doit (void) |
| 82 | { |
| 83 | pthread_t th[NTHREADS]; |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < NTHREADS; ++i) |
| 87 | pthread_create (th + i, NULL, worker, NULL); |
| 88 | |
| 89 | for (i = 0; i < NTHREADS; ++i) |
| 90 | pthread_join (th[i], NULL); |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | main (int argc, char **argv) |
| 95 | { |
| 96 | if (argc > 1) |
| 97 | verbose = 1; |
| 98 | |
| 99 | if (verbose) |
| 100 | printf ("Caching: none\n"); |
| 101 | unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE); |
| 102 | doit (); |
| 103 | |
| 104 | if (verbose) |
| 105 | printf ("Caching: global\n"); |
| 106 | unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL); |
| 107 | |
| 108 | if (verbose) |
| 109 | printf ("Caching: per-thread\n"); |
| 110 | unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD); |
| 111 | |
| 112 | if (nerrors) |
| 113 | fprintf (stderr, "FAILURE: detected %d errors\n", nerrors); |
| 114 | |
| 115 | if (verbose) |
| 116 | printf ("SUCCESS\n"); |
| 117 | return 0; |
| 118 | } |