mostang.com!davidm | 0660b2b | 2002-02-23 20:27:03 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2001-2002 Hewlett-Packard Co |
| 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
mostang.com!davidm | aca3843 | 2002-11-16 03:25:36 +0000 | [diff] [blame^] | 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: |
mostang.com!davidm | 0660b2b | 2002-02-23 20:27:03 +0000 | [diff] [blame] | 12 | |
mostang.com!davidm | aca3843 | 2002-11-16 03:25:36 +0000 | [diff] [blame^] | 13 | The above copyright notice and this permission notice shall be |
| 14 | included in all copies or substantial portions of the Software. |
mostang.com!davidm | 0660b2b | 2002-02-23 20:27:03 +0000 | [diff] [blame] | 15 | |
mostang.com!davidm | aca3843 | 2002-11-16 03:25:36 +0000 | [diff] [blame^] | 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. */ |
mostang.com!davidm | 0660b2b | 2002-02-23 20:27:03 +0000 | [diff] [blame] | 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unwind.h> |
| 27 | |
| 28 | #define panic(args...) \ |
| 29 | { fprintf (stderr, args); exit (-1); } |
| 30 | |
| 31 | static void |
| 32 | init_state (unw_context_t *ucp) |
| 33 | { |
| 34 | int i; |
| 35 | |
| 36 | ucp->uc_mcontext.sc_flags = 0; |
| 37 | |
| 38 | ucp->uc_mcontext.sc_ar_ccv = random (); |
| 39 | ucp->uc_mcontext.sc_ar_lc = random (); |
| 40 | ucp->uc_mcontext.sc_pr = random (); |
| 41 | |
| 42 | #if 0 |
| 43 | ucp->uc_mcontext.sc_ip = xxx; |
| 44 | ucp->uc_mcontext.sc_cfm = xxx; |
| 45 | ucp->uc_mcontext.sc_um = xxx; |
| 46 | ucp->uc_mcontext.sc_ar_rsc = xxx; |
| 47 | ucp->uc_mcontext.sc_ar_bsp = xxx; |
| 48 | ucp->uc_mcontext.sc_ar_rnat = xxx; |
| 49 | ucp->uc_mcontext.sc_ar_unat = xxx; |
| 50 | ucp->uc_mcontext.sc_ar_fpsr = xxx; |
| 51 | ucp->uc_mcontext.sc_ar_pfs = xxx; |
| 52 | #endif |
| 53 | |
| 54 | /* initialize static registers without trashing gp (r1), sp (r12), |
| 55 | or tp (r13). */ |
| 56 | for (i = 2; i < 32; ++i) |
| 57 | { |
| 58 | if (i != 12 && i != 13) |
| 59 | { |
| 60 | ucp->uc_mcontext.sc_gr[i] = random (); |
| 61 | ucp->uc_mcontext.sc_nat |= (random () & 1) << i; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #if 0 |
| 66 | /* initialize stacked registers: */ |
| 67 | for (i = 32; i < 128; ++i) |
| 68 | { |
| 69 | xxx; |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | for (i = 0; i < 8; ++i) |
| 74 | ucp->uc_mcontext.sc_br[i] = random (); |
| 75 | |
| 76 | for (i = 0; i < 128; ++i) |
| 77 | { |
| 78 | ucp->uc_mcontext.sc_fr[i].u.bits[0] = random (); |
| 79 | ucp->uc_mcontext.sc_fr[i].u.bits[0] = random (); |
| 80 | } |
| 81 | #if 0 |
| 82 | ucp->uc_mcontext.sc_rbs_base = xxx; |
| 83 | ucp->uc_mcontext.sc_loadrs = xxx; |
| 84 | ucp->uc_mcontext.sc_ar25 = xxx; |
| 85 | ucp->uc_mcontext.sc_ar26 = xxx; |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | check_state (ucontext_t *orig_state, unw_cursor_t *c) |
| 91 | { |
| 92 | unw_word_t val; |
| 93 | |
| 94 | unw_get_reg (c, UNW_REG_IP, &val); |
| 95 | printf ("IP: orig=%016lx now=%016lx\n", orig_state->uc_mcontext.sc_ip, val); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | setup_context (ucontext_t *unwind_ucp) |
| 100 | { |
| 101 | asm volatile ("mov ar.fpsr = %0" :: "r"(0x9804c8a70033f)); |
| 102 | |
| 103 | init_state (unwind_ucp); |
| 104 | setcontext (unwind_ucp); |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | check (ucontext_t *unwind_ucp, ucontext_t *setup_ucp, |
| 109 | void (*doit) (ucontext_t *)) |
| 110 | { |
| 111 | swapcontext (unwind_ucp, setup_ucp); |
| 112 | (*doit) (unwind_ucp); |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | test1 (ucontext_t *orig_state) |
| 117 | { |
| 118 | unw_cursor_t cursor; |
| 119 | ucontext_t uc; |
| 120 | |
| 121 | getcontext (&uc); |
| 122 | if (unw_init_local (&cursor, &uc) < 0) |
| 123 | panic ("unw_init_local failed\n"); |
| 124 | |
| 125 | if (unw_step (&cursor) < 0) |
| 126 | panic ("unw_step failed\n"); |
| 127 | |
| 128 | check_state (orig_state, &cursor); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | main (int argc, char **argv) |
| 133 | { |
| 134 | ucontext_t unwind_uc, setup_uc; |
| 135 | unsigned char stack_mem[256*1024]; |
| 136 | |
| 137 | setup_uc.uc_stack.ss_sp = stack_mem; |
| 138 | setup_uc.uc_stack.ss_flags = 0; |
| 139 | setup_uc.uc_stack.ss_size = sizeof (stack_mem); |
| 140 | makecontext (&setup_uc, (void (*) (void)) setup_context, |
| 141 | 2, &setup_uc, &unwind_uc); |
| 142 | check (&unwind_uc, &setup_uc, test1); |
| 143 | return 0; |
| 144 | } |