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 | |
| 5 | This file is part of libunwind. |
| 6 | |
| 7 | libunwind is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2, or (at your option) |
| 10 | any later version. |
| 11 | |
| 12 | libunwind is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unwind.h> |
| 20 | |
| 21 | #define panic(args...) \ |
| 22 | { fprintf (stderr, args); exit (-1); } |
| 23 | |
| 24 | static void |
| 25 | init_state (unw_context_t *ucp) |
| 26 | { |
| 27 | int i; |
| 28 | |
| 29 | ucp->uc_mcontext.sc_flags = 0; |
| 30 | |
| 31 | ucp->uc_mcontext.sc_ar_ccv = random (); |
| 32 | ucp->uc_mcontext.sc_ar_lc = random (); |
| 33 | ucp->uc_mcontext.sc_pr = random (); |
| 34 | |
| 35 | #if 0 |
| 36 | ucp->uc_mcontext.sc_ip = xxx; |
| 37 | ucp->uc_mcontext.sc_cfm = xxx; |
| 38 | ucp->uc_mcontext.sc_um = xxx; |
| 39 | ucp->uc_mcontext.sc_ar_rsc = xxx; |
| 40 | ucp->uc_mcontext.sc_ar_bsp = xxx; |
| 41 | ucp->uc_mcontext.sc_ar_rnat = xxx; |
| 42 | ucp->uc_mcontext.sc_ar_unat = xxx; |
| 43 | ucp->uc_mcontext.sc_ar_fpsr = xxx; |
| 44 | ucp->uc_mcontext.sc_ar_pfs = xxx; |
| 45 | #endif |
| 46 | |
| 47 | /* initialize static registers without trashing gp (r1), sp (r12), |
| 48 | or tp (r13). */ |
| 49 | for (i = 2; i < 32; ++i) |
| 50 | { |
| 51 | if (i != 12 && i != 13) |
| 52 | { |
| 53 | ucp->uc_mcontext.sc_gr[i] = random (); |
| 54 | ucp->uc_mcontext.sc_nat |= (random () & 1) << i; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #if 0 |
| 59 | /* initialize stacked registers: */ |
| 60 | for (i = 32; i < 128; ++i) |
| 61 | { |
| 62 | xxx; |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | for (i = 0; i < 8; ++i) |
| 67 | ucp->uc_mcontext.sc_br[i] = random (); |
| 68 | |
| 69 | for (i = 0; i < 128; ++i) |
| 70 | { |
| 71 | ucp->uc_mcontext.sc_fr[i].u.bits[0] = random (); |
| 72 | ucp->uc_mcontext.sc_fr[i].u.bits[0] = random (); |
| 73 | } |
| 74 | #if 0 |
| 75 | ucp->uc_mcontext.sc_rbs_base = xxx; |
| 76 | ucp->uc_mcontext.sc_loadrs = xxx; |
| 77 | ucp->uc_mcontext.sc_ar25 = xxx; |
| 78 | ucp->uc_mcontext.sc_ar26 = xxx; |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | check_state (ucontext_t *orig_state, unw_cursor_t *c) |
| 84 | { |
| 85 | unw_word_t val; |
| 86 | |
| 87 | unw_get_reg (c, UNW_REG_IP, &val); |
| 88 | printf ("IP: orig=%016lx now=%016lx\n", orig_state->uc_mcontext.sc_ip, val); |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | setup_context (ucontext_t *unwind_ucp) |
| 93 | { |
| 94 | asm volatile ("mov ar.fpsr = %0" :: "r"(0x9804c8a70033f)); |
| 95 | |
| 96 | init_state (unwind_ucp); |
| 97 | setcontext (unwind_ucp); |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | check (ucontext_t *unwind_ucp, ucontext_t *setup_ucp, |
| 102 | void (*doit) (ucontext_t *)) |
| 103 | { |
| 104 | swapcontext (unwind_ucp, setup_ucp); |
| 105 | (*doit) (unwind_ucp); |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | test1 (ucontext_t *orig_state) |
| 110 | { |
| 111 | unw_cursor_t cursor; |
| 112 | ucontext_t uc; |
| 113 | |
| 114 | getcontext (&uc); |
| 115 | if (unw_init_local (&cursor, &uc) < 0) |
| 116 | panic ("unw_init_local failed\n"); |
| 117 | |
| 118 | if (unw_step (&cursor) < 0) |
| 119 | panic ("unw_step failed\n"); |
| 120 | |
| 121 | check_state (orig_state, &cursor); |
| 122 | } |
| 123 | |
| 124 | int |
| 125 | main (int argc, char **argv) |
| 126 | { |
| 127 | ucontext_t unwind_uc, setup_uc; |
| 128 | unsigned char stack_mem[256*1024]; |
| 129 | |
| 130 | setup_uc.uc_stack.ss_sp = stack_mem; |
| 131 | setup_uc.uc_stack.ss_flags = 0; |
| 132 | setup_uc.uc_stack.ss_size = sizeof (stack_mem); |
| 133 | makecontext (&setup_uc, (void (*) (void)) setup_context, |
| 134 | 2, &setup_uc, &unwind_uc); |
| 135 | check (&unwind_uc, &setup_uc, test1); |
| 136 | return 0; |
| 137 | } |