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>
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:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
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. */
24 /* This illustrates the basics of using the unwind interface for
25 exception handling. */
35 #include <libunwind.h>
37 #ifdef HAVE_IA64INTRIN_H
38 # include <ia64intrin.h>
41 #define panic(args...) \
42 { ++nerrors; fprintf (stderr, args); }
47 volatile int got_here = 0;
52 raise_exception (void)
59 if (unw_init_local (&cursor, &uc) < 0)
61 panic ("unw_init_local() failed!\n");
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)
69 panic ("unw_step() failed!\n");
72 unw_resume (&cursor); /* transfer control to exception handler */
79 # ifdef __INTEL_COMPILER
80 return __getReg (_IA64_REG_AR_BSP);
82 return (uintptr_t) __builtin_ia64_bsp ();
96 printf ("a(n=%d): sp=%p bsp=0x%lx\n",
97 n, &stack, (unsigned long) get_bsp ());
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. */
127 printf ("b(n=%d) calling raise_exception()\n", n);
130 panic ("FAILURE: b() returned from raise_exception()!!\n");
134 main (int argc, char **argv)
141 depth = atol (argv[1]);
144 fprintf (stderr, "Usage: %s depth\n"
145 " depth must be >= 1\n", argv[0]);
151 if (result != 0 || !got_here || nerrors > 0)
154 "FAILURE: test failed: result=%d got_here=%d nerrors=%d\n",
155 result, got_here, nerrors);
160 printf ("SUCCESS!\n");