2003-02-22 04:08:22 +01:00
|
|
|
/* libunwind - a platform-independent unwind library
|
2005-05-03 11:13:17 +02:00
|
|
|
Copyright (C) 2003-2004 Hewlett-Packard Co
|
2003-02-22 04:08:22 +01:00
|
|
|
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
|
|
|
/* Verify that unw_resume() restores the signal mask at proper time. */
|
|
|
|
|
2003-03-27 05:29:07 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-02-22 04:08:22 +01:00
|
|
|
#include <libunwind.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-08-10 09:54:09 +02:00
|
|
|
#include <string.h>
|
2003-02-22 04:08:22 +01:00
|
|
|
#include <unistd.h>
|
2012-08-10 09:54:09 +02:00
|
|
|
#include <errno.h>
|
2003-02-22 04:08:22 +01:00
|
|
|
|
2003-03-27 05:29:07 +01:00
|
|
|
#ifdef HAVE_IA64INTRIN_H
|
|
|
|
# include <ia64intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2003-02-22 04:08:22 +01:00
|
|
|
#define panic(args...) \
|
|
|
|
do { fprintf (stderr, args); ++nerrors; } while (0)
|
|
|
|
|
|
|
|
int verbose;
|
|
|
|
int nerrors;
|
|
|
|
int got_usr1, got_usr2;
|
|
|
|
char *sigusr1_sp;
|
|
|
|
|
2005-05-03 11:13:17 +02:00
|
|
|
uintptr_t
|
|
|
|
get_bsp (void)
|
|
|
|
{
|
|
|
|
#if UNW_TARGET_IA64
|
|
|
|
# ifdef __INTEL_COMPILER
|
|
|
|
return __getReg (_IA64_REG_AR_BSP);
|
|
|
|
# else
|
|
|
|
return (uintptr_t) __builtin_ia64_bsp ();
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-08-10 09:54:09 +02:00
|
|
|
#ifdef TEST_WITH_SIGINFO
|
|
|
|
void
|
|
|
|
handler (int sig,
|
|
|
|
siginfo_t *si __attribute__((unused)),
|
|
|
|
void *ucontext __attribute__((unused)))
|
|
|
|
#else
|
2003-02-22 04:08:22 +01:00
|
|
|
void
|
|
|
|
handler (int sig)
|
2012-08-10 09:54:09 +02:00
|
|
|
#endif
|
2003-02-22 04:08:22 +01:00
|
|
|
{
|
|
|
|
unw_word_t ip;
|
2010-03-11 07:52:12 +01:00
|
|
|
sigset_t mask, oldmask;
|
2003-02-22 04:08:22 +01:00
|
|
|
unw_context_t uc;
|
|
|
|
unw_cursor_t c;
|
|
|
|
char foo;
|
2005-05-03 11:13:17 +02:00
|
|
|
int ret;
|
2003-02-22 04:08:22 +01:00
|
|
|
|
|
|
|
#if UNW_TARGET_IA64
|
|
|
|
if (verbose)
|
2005-05-03 11:13:17 +02:00
|
|
|
printf ("bsp = %llx\n", (unsigned long long) get_bsp ());
|
2003-02-22 04:08:22 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf ("got signal %d\n", sig);
|
|
|
|
|
|
|
|
if (sig == SIGUSR1)
|
|
|
|
{
|
|
|
|
++got_usr1;
|
|
|
|
sigusr1_sp = &foo;
|
|
|
|
|
|
|
|
sigemptyset (&mask);
|
|
|
|
sigaddset (&mask, SIGUSR2);
|
2010-03-11 07:52:12 +01:00
|
|
|
sigprocmask (SIG_BLOCK, &mask, &oldmask);
|
2003-02-22 04:08:22 +01:00
|
|
|
kill (getpid (), SIGUSR2); /* pend SIGUSR2 */
|
|
|
|
|
|
|
|
signal (SIGUSR1, SIG_IGN);
|
|
|
|
|
2005-05-03 11:13:17 +02:00
|
|
|
if ((ret = unw_getcontext (&uc)) < 0)
|
|
|
|
panic ("unw_getcontext() failed: ret=%d\n", ret);
|
2010-03-11 07:52:12 +01:00
|
|
|
#if UNW_TARGET_X86_64
|
|
|
|
/* unw_getcontext() doesn't save signal mask to avoid a syscall */
|
|
|
|
uc.uc_sigmask = oldmask;
|
|
|
|
#endif
|
2005-05-03 11:13:17 +02:00
|
|
|
if ((ret = unw_init_local (&c, &uc)) < 0)
|
|
|
|
panic ("unw_init_local() failed: ret=%d\n", ret);
|
|
|
|
|
|
|
|
if ((ret = unw_step (&c)) < 0) /* step to signal trampoline */
|
|
|
|
panic ("unw_step(1) failed: ret=%d\n", ret);
|
|
|
|
|
2012-08-10 09:54:09 +02:00
|
|
|
if ((ret = unw_step (&c)) < 0) /* step to kill() */
|
2005-05-03 11:13:17 +02:00
|
|
|
panic ("unw_step(2) failed: ret=%d\n", ret);
|
|
|
|
|
|
|
|
if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0)
|
|
|
|
panic ("unw_get_reg(IP) failed: ret=%d\n", ret);
|
2003-02-22 04:08:22 +01:00
|
|
|
if (verbose)
|
2003-11-27 07:52:54 +01:00
|
|
|
printf ("resuming at 0x%lx, with SIGUSR2 pending\n",
|
|
|
|
(unsigned long) ip);
|
2005-05-03 11:13:17 +02:00
|
|
|
unw_resume (&c);
|
2003-02-22 04:08:22 +01:00
|
|
|
}
|
|
|
|
else if (sig == SIGUSR2)
|
|
|
|
{
|
|
|
|
++got_usr2;
|
|
|
|
if (got_usr1)
|
|
|
|
{
|
2010-03-11 07:52:12 +01:00
|
|
|
if (verbose)
|
2003-02-22 04:08:22 +01:00
|
|
|
printf ("OK: stack still at %p\n", &foo);
|
|
|
|
}
|
|
|
|
signal (SIGUSR2, SIG_IGN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
panic ("Got unexpected signal %d\n", sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-02 08:42:21 +02:00
|
|
|
main (int argc, char **argv __attribute__((unused)))
|
2003-02-22 04:08:22 +01:00
|
|
|
{
|
2012-08-10 09:54:09 +02:00
|
|
|
struct sigaction sa;
|
2005-05-03 11:13:17 +02:00
|
|
|
float d = 1.0;
|
|
|
|
int n = 0;
|
|
|
|
|
2003-02-22 04:08:22 +01:00
|
|
|
if (argc > 1)
|
|
|
|
verbose = 1;
|
|
|
|
|
2012-08-10 09:54:09 +02:00
|
|
|
memset (&sa, 0, sizeof(sa));
|
|
|
|
#ifdef TEST_WITH_SIGINFO
|
|
|
|
sa.sa_sigaction = handler;
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
#else
|
|
|
|
sa.sa_handler = handler;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (sigaction (SIGUSR1, &sa, NULL) != 0 ||
|
|
|
|
sigaction (SIGUSR2, &sa, NULL) != 0)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "sigaction() failed: %s\n", strerror (errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-22 04:08:22 +01:00
|
|
|
|
2005-05-03 11:13:17 +02:00
|
|
|
/* Use the FPU a bit; otherwise we get spurious errors should the
|
|
|
|
signal handler need to use the FPU for any reason. This seems to
|
|
|
|
happen on x86-64. */
|
|
|
|
while (d > 0.0)
|
|
|
|
{
|
|
|
|
d /= 2.0;
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
if (n > 9999)
|
|
|
|
return -1; /* can't happen, but don't tell the compiler... */
|
|
|
|
|
2003-02-22 04:08:22 +01:00
|
|
|
if (verbose)
|
|
|
|
printf ("sending SIGUSR1\n");
|
|
|
|
kill (getpid (), SIGUSR1);
|
|
|
|
|
2005-05-03 11:13:17 +02:00
|
|
|
if (!got_usr2)
|
|
|
|
panic ("failed to get SIGUSR2\n");
|
|
|
|
|
2003-02-22 04:08:22 +01:00
|
|
|
if (nerrors)
|
2005-05-03 11:13:17 +02:00
|
|
|
{
|
|
|
|
fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
|
|
|
|
exit (-1);
|
|
|
|
}
|
2003-02-22 04:08:22 +01:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf ("SUCCESS\n");
|
|
|
|
return 0;
|
|
|
|
}
|