add changes
[platform/upstream/libunwind.git] / tests / Gtest-resume-sig.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003-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 /*  Verify that unw_resume() restores the signal mask at proper time.  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "compiler.h"
31
32 #include <libunwind.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39
40 #ifdef HAVE_IA64INTRIN_H
41 # include <ia64intrin.h>
42 #endif
43
44 #define panic(args...)                                          \
45         do { fprintf (stderr, args); ++nerrors; } while (0)
46
47 int verbose;
48 int nerrors;
49 int got_usr1, got_usr2;
50 char *sigusr1_sp;
51
52 uintptr_t
53 get_bsp (void)
54 {
55 #if UNW_TARGET_IA64
56 # ifdef __INTEL_COMPILER
57   return __getReg (_IA64_REG_AR_BSP);
58 # else
59   return (uintptr_t) __builtin_ia64_bsp ();
60 # endif
61 #else
62   return 0;
63 #endif
64 }
65
66 #ifdef TEST_WITH_SIGINFO
67 void
68 handler (int sig,
69          siginfo_t *si UNUSED,
70          void *ucontext UNUSED)
71 #else
72 void
73 handler (int sig)
74 #endif
75 {
76   unw_word_t ip;
77   sigset_t mask, oldmask;
78   unw_context_t uc;
79   unw_cursor_t c;
80   char foo;
81   int ret;
82
83 #if UNW_TARGET_IA64
84   if (verbose)
85     printf ("bsp = %llx\n", (unsigned long long) get_bsp ());
86 #endif
87
88   if (verbose)
89     printf ("got signal %d\n", sig);
90
91   if (sig == SIGUSR1)
92     {
93       ++got_usr1;
94       sigusr1_sp = &foo;
95
96       sigemptyset (&mask);
97       sigaddset (&mask, SIGUSR2);
98       sigprocmask (SIG_BLOCK, &mask, &oldmask);
99       kill (getpid (), SIGUSR2);        /* pend SIGUSR2 */
100
101       signal (SIGUSR1, SIG_IGN);
102
103       if ((ret = unw_getcontext (&uc)) < 0)
104         panic ("unw_getcontext() failed: ret=%d\n", ret);
105 #if UNW_TARGET_X86_64
106       /* unw_getcontext() doesn't save signal mask to avoid a syscall */
107       uc.uc_sigmask = oldmask; 
108 #endif
109       if ((ret = unw_init_local (&c, &uc)) < 0)
110         panic ("unw_init_local() failed: ret=%d\n", ret);
111
112       if ((ret = unw_step (&c)) < 0)            /* step to signal trampoline */
113         panic ("unw_step(1) failed: ret=%d\n", ret);
114
115       if ((ret = unw_step (&c)) < 0)            /* step to kill() */
116         panic ("unw_step(2) failed: ret=%d\n", ret);
117
118 #if defined(UNW_TARGET_TILEGX)
119       if ((ret = unw_step (&c)) < 0)            /* step to signal trampoline */
120         panic ("unw_step(2) failed: ret=%d\n", ret);
121 #endif
122
123       if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0)
124         panic ("unw_get_reg(IP) failed: ret=%d\n", ret);
125       if (verbose)
126         printf ("resuming at 0x%lx, with SIGUSR2 pending\n",
127                 (unsigned long) ip);
128       unw_resume (&c);
129     }
130   else if (sig == SIGUSR2)
131     {
132       ++got_usr2;
133       if (got_usr1)
134         {
135           if (verbose)
136             printf ("OK: stack still at %p\n", &foo);
137         }
138       signal (SIGUSR2, SIG_IGN);
139     }
140   else
141     panic ("Got unexpected signal %d\n", sig);
142 }
143
144 int
145 main (int argc, char **argv UNUSED)
146 {
147   struct sigaction sa;
148   float d = 1.0;
149   int n = 0;
150
151   if (argc > 1)
152     verbose = 1;
153
154   memset (&sa, 0, sizeof(sa));
155 #ifdef TEST_WITH_SIGINFO
156   sa.sa_sigaction = handler;
157   sa.sa_flags = SA_SIGINFO;
158 #else
159   sa.sa_handler = handler;
160 #endif
161
162   if (sigaction (SIGUSR1, &sa, NULL) != 0 ||
163       sigaction (SIGUSR2, &sa, NULL) != 0)
164     {
165       fprintf (stderr, "sigaction() failed: %s\n", strerror (errno));
166       return -1;
167     }
168
169   /* Use the FPU a bit; otherwise we get spurious errors should the
170      signal handler need to use the FPU for any reason.  This seems to
171      happen on x86-64.  */
172   while (d > 0.0)
173     {
174       d /= 2.0;
175       ++n;
176     }
177   if (n > 9999)
178     return -1;  /* can't happen, but don't tell the compiler... */
179
180   if (verbose)
181     printf ("sending SIGUSR1\n");
182   kill (getpid (), SIGUSR1);
183
184   if (!got_usr2)
185     panic ("failed to get SIGUSR2\n");
186
187   if (nerrors)
188     {
189       fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
190       exit (-1);
191     }
192
193   if (verbose)
194     printf ("SUCCESS\n");
195   return 0;
196 }