mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2002-2003 Hewlett-Packard Co |
| 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
| 5 | This file is part of libunwind. |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining |
| 8 | a copy of this software and associated documentation files (the |
| 9 | "Software"), to deal in the Software without restriction, including |
| 10 | without limitation the rights to use, copy, modify, merge, publish, |
| 11 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | permit persons to whom the Software is furnished to do so, subject to |
| 13 | the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be |
| 16 | included in all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 25 | |
| 26 | /* This file tests unwinding from a constructor from within an |
| 27 | atexit() handler. */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <libunwind.h> |
Tommi Rantala | 6b55e0a | 2012-09-19 13:50:59 +0300 | [diff] [blame] | 34 | #include "compiler.h" |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 35 | |
hp.com!davidm | 587bba5 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 36 | int verbose, errors; |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 37 | |
| 38 | #define panic(args...) \ |
| 39 | { ++errors; fprintf (stderr, args); return; } |
| 40 | |
| 41 | class Test_Class { |
| 42 | public: |
| 43 | Test_Class (void); |
| 44 | }; |
| 45 | |
| 46 | static Test_Class t; |
| 47 | |
| 48 | static void |
Tommi Rantala | 6470a67 | 2012-08-28 16:51:41 +0300 | [diff] [blame] | 49 | do_backtrace (void) |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 50 | { |
| 51 | char name[128], off[32]; |
| 52 | unw_word_t ip, offset; |
| 53 | unw_cursor_t cursor; |
| 54 | unw_context_t uc; |
| 55 | int ret, count = 0; |
| 56 | |
| 57 | unw_getcontext (&uc); |
| 58 | unw_init_local (&cursor, &uc); |
| 59 | |
| 60 | do |
| 61 | { |
| 62 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 63 | name[0] = '\0'; |
| 64 | off[0] = '\0'; |
| 65 | if (unw_get_proc_name (&cursor, name, sizeof (name), &offset) == 0 |
Tommi Rantala | ffbe299 | 2012-08-28 16:55:23 +0300 | [diff] [blame] | 66 | && offset > 0) |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 67 | snprintf (off, sizeof (off), "+0x%lx", (long) offset); |
hp.com!davidm | 587bba5 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 68 | if (verbose) |
| 69 | printf (" [%lx] <%s%s>\n", (long) ip, name, off); |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 70 | if (++count > 32) |
| 71 | panic ("FAILURE: didn't reach beginning of unwind-chain\n"); |
| 72 | } |
| 73 | while ((ret = unw_step (&cursor)) > 0); |
| 74 | |
| 75 | if (ret < 0) |
| 76 | panic ("FAILURE: unw_step() returned %d\n", ret); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | b (void) |
| 81 | { |
Tommi Rantala | 6470a67 | 2012-08-28 16:51:41 +0300 | [diff] [blame] | 82 | do_backtrace(); |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void |
| 86 | a (void) |
| 87 | { |
hp.com!davidm | 587bba5 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 88 | if (verbose) |
Tommi Rantala | 6470a67 | 2012-08-28 16:51:41 +0300 | [diff] [blame] | 89 | printf ("do_backtrace() from atexit()-handler:\n"); |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 90 | b(); |
| 91 | if (errors) |
| 92 | abort (); /* cannot portably call exit() from an atexit() handler */ |
| 93 | } |
| 94 | |
| 95 | Test_Class::Test_Class (void) |
| 96 | { |
hp.com!davidm | 587bba5 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 97 | if (verbose) |
Tommi Rantala | 6470a67 | 2012-08-28 16:51:41 +0300 | [diff] [blame] | 98 | printf ("do_backtrace() from constructor:\n"); |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 99 | b(); |
| 100 | } |
| 101 | |
| 102 | int |
Tommi Rantala | 6b55e0a | 2012-09-19 13:50:59 +0300 | [diff] [blame] | 103 | main (int argc, char **argv UNUSED) |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 104 | { |
hp.com!davidm | 587bba5 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 105 | verbose = argc > 1; |
mostang.com!davidm | a4ab109 | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 106 | return atexit (a); |
| 107 | } |