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>
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 /* Verify that unw_resume() restores the signal mask at proper time. */
32 #include <libunwind.h>
40 #ifdef HAVE_IA64INTRIN_H
41 # include <ia64intrin.h>
44 #define panic(args...) \
45 do { fprintf (stderr, args); ++nerrors; } while (0)
49 int got_usr1, got_usr2;
56 # ifdef __INTEL_COMPILER
57 return __getReg (_IA64_REG_AR_BSP);
59 return (uintptr_t) __builtin_ia64_bsp ();
66 #ifdef TEST_WITH_SIGINFO
70 void *ucontext UNUSED)
82 // The test rely on SIGUSR2 mask to be cleared when the handler returns.
83 // For local context from the signal handler, there doesn't seem to be a way
84 // currently to set it so just clear the whole struct to make sure the signal mask is cleared.
85 // This should probably be fixed to avoid signal mask being set to random values
86 // by `unw_resume` if the context was not pre-zeroed.,
87 // Using the signal ucontext direction should also work automatically but currently doesn't
88 // on ARM/AArch64 (or any other archs that doesn't have a proper sigreturn implementation)
89 memset(&uc, 0x0, sizeof(uc));
93 printf ("bsp = %llx\n", (unsigned long long) get_bsp ());
97 printf ("got signal %d\n", sig);
105 sigaddset (&mask, SIGUSR2);
106 sigprocmask (SIG_BLOCK, &mask, NULL);
107 kill (getpid (), SIGUSR2); /* pend SIGUSR2 */
109 signal (SIGUSR1, SIG_IGN);
111 if ((ret = unw_getcontext (&uc)) < 0)
112 panic ("unw_getcontext() failed: ret=%d\n", ret);
113 if ((ret = unw_init_local (&c, &uc)) < 0)
114 panic ("unw_init_local() failed: ret=%d\n", ret);
116 if ((ret = unw_step (&c)) < 0) /* step to signal trampoline */
117 panic ("unw_step(1) failed: ret=%d\n", ret);
119 if ((ret = unw_step (&c)) < 0) /* step to kill() */
120 panic ("unw_step(2) failed: ret=%d\n", ret);
122 #if defined(UNW_TARGET_TILEGX)
123 if ((ret = unw_step (&c)) < 0) /* step to signal trampoline */
124 panic ("unw_step(2) failed: ret=%d\n", ret);
127 if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0)
128 panic ("unw_get_reg(IP) failed: ret=%d\n", ret);
130 printf ("resuming at 0x%lx, with SIGUSR2 pending\n",
134 else if (sig == SIGUSR2)
140 printf ("OK: stack still at %p\n", &foo);
142 signal (SIGUSR2, SIG_IGN);
145 panic ("Got unexpected signal %d\n", sig);
149 main (int argc, char **argv UNUSED)
158 memset (&sa, 0, sizeof(sa));
159 #ifdef TEST_WITH_SIGINFO
160 sa.sa_sigaction = handler;
161 sa.sa_flags = SA_SIGINFO;
163 sa.sa_handler = handler;
166 if (sigaction (SIGUSR1, &sa, NULL) != 0 ||
167 sigaction (SIGUSR2, &sa, NULL) != 0)
169 fprintf (stderr, "sigaction() failed: %s\n", strerror (errno));
173 /* Use the FPU a bit; otherwise we get spurious errors should the
174 signal handler need to use the FPU for any reason. This seems to
182 return -1; /* can't happen, but don't tell the compiler... */
185 printf ("sending SIGUSR1\n");
186 kill (getpid (), SIGUSR1);
189 panic ("failed to get SIGUSR2\n");
193 fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
198 printf ("SUCCESS\n");