1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
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:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
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. */
23
24 /* This illustrates the basics of using the unwind interface for
25 exception handling. */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include <libunwind.h>
36
37 #ifdef HAVE_IA64INTRIN_H
38 # include <ia64intrin.h>
39 #endif
40
41 #define panic(args...) \
42 { ++nerrors; fprintf (stderr, args); }
43
44 int nerrors = 0;
45 int verbose = 0;
46 int depth = 13;
47 volatile int got_here = 0;
48
49 extern void b (int);
50
51 void
raise_exception(void)52 raise_exception (void)
53 {
54 unw_cursor_t cursor;
55 unw_context_t uc;
56 int i;
57
58 unw_getcontext (&uc);
59 if (unw_init_local (&cursor, &uc) < 0)
60 {
61 panic ("unw_init_local() failed!\n");
62 return;
63 }
64
65 /* unwind to top-most frame a(), skipping over b() and raise_exception(): */
66 for (i = 0; i < depth + 2; ++i)
67 if (unw_step (&cursor) < 0)
68 {
69 panic ("unw_step() failed!\n");
70 return;
71 }
72 unw_resume (&cursor); /* transfer control to exception handler */
73 }
74
75 uintptr_t
get_bsp(void)76 get_bsp (void)
77 {
78 #if UNW_TARGET_IA64
79 # ifdef __INTEL_COMPILER
80 return __getReg (_IA64_REG_AR_BSP);
81 # else
82 return (uintptr_t) __builtin_ia64_bsp ();
83 # endif
84 #else
85 return 0;
86 #endif
87 }
88
89 int
a(int n)90 a (int n)
91 {
92 long stack;
93 int result = 99;
94
95 if (verbose)
96 printf ("a(n=%d): sp=%p bsp=0x%lx\n",
97 n, &stack, (unsigned long) get_bsp ());
98
99 if (n > 0)
100 a (n - 1);
101 else
102 b (16);
103
104 if (verbose)
105 {
106 printf ("exception handler: here we go (sp=%p, bsp=0x%lx)...\n",
107 &stack, (unsigned long) get_bsp ());
108 /* This call works around a bug in gcc (up-to pre3.4) which
109 causes invalid assembly code to be generated when
110 __builtin_ia64_bsp() gets predicated. */
111 getpid ();
112 }
113 if (n == depth)
114 {
115 result = 0;
116 got_here = 1;
117 }
118 return result;
119 }
120
121 void
b(int n)122 b (int n)
123 {
124 if ((n & 1) == 0)
125 {
126 if (verbose)
127 printf ("b(n=%d) calling raise_exception()\n", n);
128 raise_exception ();
129 }
130 panic ("FAILURE: b() returned from raise_exception()!!\n");
131 }
132
133 int
main(int argc,char ** argv)134 main (int argc, char **argv)
135 {
136 int result;
137
138 if (argc > 1)
139 {
140 ++verbose;
141 depth = atol (argv[1]);
142 if (depth < 1)
143 {
144 fprintf (stderr, "Usage: %s depth\n"
145 " depth must be >= 1\n", argv[0]);
146 exit (-1);
147 }
148 }
149
150 result = a (depth);
151 if (result != 0 || !got_here || nerrors > 0)
152 {
153 fprintf (stderr,
154 "FAILURE: test failed: result=%d got_here=%d nerrors=%d\n",
155 result, got_here, nerrors);
156 exit (-1);
157 }
158
159 if (verbose)
160 printf ("SUCCESS!\n");
161 return 0;
162 }
163