homeip.net!davidm | 95dea64 | 2004-08-19 13:40:08 +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 | This file is part of libunwind. |
| 6 | |
| 7 | Copyright (c) 2002 Hewlett-Packard Co. |
| 8 | |
| 9 | Permission is hereby granted, free of charge, to any person obtaining |
| 10 | a copy of this software and associated documentation files (the |
| 11 | "Software"), to deal in the Software without restriction, including |
| 12 | without limitation the rights to use, copy, modify, merge, publish, |
| 13 | distribute, sublicense, and/or sell copies of the Software, and to |
| 14 | permit persons to whom the Software is furnished to do so, subject to |
| 15 | the following conditions: |
| 16 | |
| 17 | The above copyright notice and this permission notice shall be |
| 18 | included in all copies or substantial portions of the Software. |
| 19 | |
| 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 24 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 25 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 26 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 27 | |
| 28 | /* This test simply verifies that unw_init_remote() can be used in |
| 29 | lieu of unw_init_local(). This was broken for a while on ia64. */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | # include "config.h" |
| 33 | #endif |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <libunwind.h> |
| 39 | |
| 40 | #define panic(args...) \ |
| 41 | { fprintf (stderr, args); exit (-1); } |
| 42 | |
| 43 | int verbose; |
| 44 | |
| 45 | static int |
| 46 | do_backtrace (void) |
| 47 | { |
| 48 | char buf[512], name[256]; |
| 49 | unw_word_t ip, sp, off; |
| 50 | unw_cursor_t cursor; |
| 51 | unw_context_t uc; |
| 52 | int ret; |
| 53 | |
| 54 | unw_getcontext (&uc); |
| 55 | if (unw_init_remote (&cursor, unw_local_addr_space, &uc) < 0) |
| 56 | panic ("unw_init_remote failed!\n"); |
| 57 | |
| 58 | do |
| 59 | { |
| 60 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 61 | unw_get_reg (&cursor, UNW_REG_SP, &sp); |
| 62 | buf[0] = '\0'; |
| 63 | if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0) |
| 64 | { |
| 65 | if (off) |
| 66 | snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off); |
| 67 | else |
| 68 | snprintf (buf, sizeof (buf), "<%s>", name); |
| 69 | } |
| 70 | if (verbose) |
| 71 | printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp); |
| 72 | |
| 73 | ret = unw_step (&cursor); |
| 74 | if (ret < 0) |
| 75 | { |
| 76 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 77 | printf ("FAILURE: unw_step() returned %d for ip=%lx\n", |
| 78 | ret, (long) ip); |
| 79 | return -1; |
| 80 | } |
| 81 | } |
| 82 | while (ret > 0); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int |
| 88 | foo (void) |
| 89 | { |
| 90 | return do_backtrace (); |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | main (int argc, char **argv) |
| 95 | { |
| 96 | verbose = (argc > 1); |
| 97 | |
| 98 | if (verbose) |
| 99 | printf ("Normal backtrace:\n"); |
| 100 | return foo (); |
| 101 | } |