• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "compiler.h"
28 
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 #include <libunwind.h>
38 
39 #ifdef HAVE_IA64INTRIN_H
40 # include <ia64intrin.h>
41 #endif
42 
43 #define panic(args...)				\
44 	{ ++nerrors; fprintf (stderr, args); }
45 
46 int nerrors = 0;
47 int verbose = 0;
48 int depth = 13;
49 volatile int got_here = 0;
50 
51 extern void b (int);
52 
53 void
raise_exception(void)54 raise_exception (void)
55 {
56   unw_cursor_t cursor;
57   unw_context_t uc;
58   int i;
59   unw_word_t ip;
60   unw_word_t sp;
61 
62   unw_getcontext (&uc);
63   if (unw_init_local (&cursor, &uc) < 0)
64     {
65       panic ("unw_init_local() failed!\n");
66       return;
67     }
68 
69   if (verbose)
70     {
71       printf ("raise_exception(): sp=%p\n", (void*) &sp);
72       unw_get_reg (&cursor, UNW_REG_IP, &ip);
73       unw_get_reg (&cursor, UNW_REG_SP, &sp);
74       printf ("\t #%-3d ip=%p sp=%p\n", 0, (void*) ip, (void*) sp);
75     }
76 
77   /* unwind to top-most frame a(), skipping over b() and raise_exception(): */
78   for (i = 0; i < depth + 2; ++i)
79     if (unw_step (&cursor) < 0)
80       {
81 	panic ("unw_step() failed!\n");
82 	return;
83       }
84     else if (verbose)
85       {
86 	unw_get_reg (&cursor, UNW_REG_IP, &ip);
87 	unw_get_reg (&cursor, UNW_REG_SP, &sp);
88 	printf ("\t #%-3d ip=%p sp=%p\n", i + 1, (void*) ip, (void*) sp);
89       }
90   unw_resume (&cursor);	/* transfer control to exception handler */
91 }
92 
93 uintptr_t
get_bsp(void)94 get_bsp (void)
95 {
96 #if UNW_TARGET_IA64
97 # ifdef __INTEL_COMPILER
98   return __getReg (_IA64_REG_AR_BSP);
99 # else
100   return (uintptr_t) __builtin_ia64_bsp ();
101 # endif
102 #else
103   return 0;
104 #endif
105 }
106 
107 int NOINLINE
a(int n)108 a (int n)
109 {
110   long stack;
111   int result = 99;
112 
113   if (verbose)
114     printf ("a(n=%d): sp=%p bsp=0x%lx\n",
115 	    n, &stack, (unsigned long) get_bsp ());
116 
117   if (n > 0)
118     a (n - 1);
119   else
120     b (16);
121 
122   if (verbose)
123     {
124       printf ("exception handler: here we go (sp=%p, bsp=0x%lx)...\n",
125 	      &stack, (unsigned long) get_bsp ());
126       /* This call works around a bug in gcc (up-to pre3.4) which
127 	 causes invalid assembly code to be generated when
128 	 __builtin_ia64_bsp() gets predicated.  */
129       getpid ();
130     }
131   if (n == depth)
132     {
133       result = 0;
134       got_here = 1;
135     }
136   return result;
137 }
138 
139 void NOINLINE
b(int n)140 b (int n)
141 {
142   if ((n & 1) == 0)
143     {
144       if (verbose)
145 	printf ("b(n=%d) calling raise_exception()\n", n);
146       raise_exception ();
147     }
148   panic ("FAILURE: b() returned from raise_exception()!!\n");
149 }
150 
151 int
main(int argc,char ** argv)152 main (int argc, char **argv)
153 {
154   int result;
155 
156   if (argc > 1)
157     {
158       ++verbose;
159       depth = atol (argv[1]);
160       if (depth < 1)
161 	{
162 	  fprintf (stderr, "Usage: %s depth\n"
163 		   "  depth must be >= 1\n", argv[0]);
164 	  exit (-1);
165 	}
166     }
167 
168   result = a (depth);
169   if (result != 0 || !got_here || nerrors > 0)
170     {
171       fprintf (stderr,
172 	       "FAILURE: test failed: result=%d got_here=%d nerrors=%d\n",
173 	       result, got_here, nerrors);
174       exit (-1);
175     }
176 
177   if (verbose)
178     printf ("SUCCESS!\n");
179   return 0;
180 }
181