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