Imported Upstream version 1.1
[platform/upstream/libunwind.git] / tests / ia64-test-sig.c
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 test uses the unwind interface to modify the IP in an ancestor
25    frame while still returning to the parent frame.  */
26
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <libunwind-ia64.h>
32
33 #define panic(args...)                          \
34         { fprintf (stderr, args); exit (-1); }
35
36 int verbose;
37
38 static void
39 sighandler (int signal)
40 {
41   unw_cursor_t cursor, cursor2;
42   unw_word_t ip;
43   unw_context_t uc;
44
45   if (verbose)
46     printf ("caught signal %d\n", signal);
47
48   unw_getcontext (&uc);
49   if (unw_init_local (&cursor, &uc) < 0)
50     panic ("unw_init() failed!\n");
51
52   /* get cursor for caller of sighandler: */
53   if (unw_step (&cursor) < 0)
54     panic ("unw_step() failed!\n");
55
56   cursor2 = cursor;
57   while (!unw_is_signal_frame (&cursor2))
58     if (unw_step (&cursor2) < 0)
59       panic ("failed to find signal frame!\n");
60
61   if (unw_step (&cursor2) < 0)
62     panic ("unw_step() failed!\n");
63
64   if (unw_get_reg (&cursor2, UNW_REG_IP, &ip) < 0)
65     panic ("failed to get IP!\n");
66
67   /* skip faulting instruction (doesn't handle MLX template) */
68   ++ip;
69   if ((ip & 0x3) == 0x3)
70     ip += 13;
71
72   if (unw_set_reg (&cursor2, UNW_REG_IP, ip) < 0)
73     panic ("failed to set IP!\n");
74
75   unw_resume (&cursor); /* update context & return to caller of sighandler() */
76
77   panic ("unexpected return from unw_resume()!\n");
78 }
79
80 static void
81 doit (volatile char *p)
82 {
83   int ch;
84
85   ch = *p;      /* trigger SIGSEGV */
86
87   if (verbose)
88     printf ("doit: finishing execution!\n");
89 }
90
91 int
92 main (int argc, char **argv)
93 {
94   if (argc > 1)
95     verbose = 1;
96
97   signal (SIGSEGV, sighandler);
98   doit (0);
99   if (verbose)
100     printf ("SUCCESS\n");
101   return 0;
102 }