blob: e4af5df82f55d9effd73caebd16ea15408fc7176 [file] [log] [blame]
mostang.com!davidm0660b2b2002-02-23 20:27:03 +00001/* 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!davidmaca38432002-11-16 03:25:36 +00005Permission is hereby granted, free of charge, to any person obtaining
6a copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sublicense, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
mostang.com!davidm0660b2b2002-02-23 20:27:03 +000012
mostang.com!davidmaca38432002-11-16 03:25:36 +000013The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
mostang.com!davidm0660b2b2002-02-23 20:27:03 +000015
mostang.com!davidmaca38432002-11-16 03:25:36 +000016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
mostang.com!davidm0660b2b2002-02-23 20:27:03 +000023
24#include <stdio.h>
25#include <stdlib.h>
26#include <unwind.h>
27
28#define panic(args...) \
29 { fprintf (stderr, args); exit (-1); }
30
31static void
32init_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
89static void
90check_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
98static void
99setup_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
107static void
108check (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
115static void
116test1 (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
131int
132main (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}