1
0
Fork 0
mirror of https://github.com/tobast/libunwind-eh_elf.git synced 2024-12-23 20:03:43 +01:00
libunwind-eh_elf/tests/test-async-sig.c

152 lines
3.7 KiB
C
Raw Normal View History

2004-04-01 10:11:21 +02:00
/* libunwind - a platform-independent unwind library
Copyright (C) 2004 Hewlett-Packard Co
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. */
/* Check whether basic unwinding truly is async-signal safe. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#define UNW_LOCAL_ONLY
#include <libunwind.h>
struct itimerval interval =
{
.it_interval = { .tv_sec = 0, .tv_usec = 0 },
.it_value = { .tv_sec = 0, .tv_usec = 1000 }
};
int verbose;
int nerrors;
int sigcount;
#define panic(args...) \
{ ++nerrors; fprintf (stderr, args); return; }
static void
do_backtrace (int may_print, int get_proc_name)
{
char buf[512], name[256];
unw_cursor_t cursor;
unw_word_t ip, sp, off;
unw_context_t uc;
int ret;
unw_getcontext (&uc);
if (unw_init_local (&cursor, &uc) < 0)
panic ("unw_init_local failed!\n");
do
{
unw_get_reg (&cursor, UNW_REG_IP, &ip);
unw_get_reg (&cursor, UNW_REG_SP, &sp);
buf[0] = '\0';
if (get_proc_name || (may_print && verbose))
{
if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0)
{
if (off)
snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off);
else
{
size_t len = strlen (name);
buf[0] = '<';
memcpy (buf + 1, name, len);
buf[len + 1] = '>';
buf[len + 2] = '\0';
}
}
}
if (may_print && verbose)
printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
ret = unw_step (&cursor);
if (ret < 0)
{
unw_get_reg (&cursor, UNW_REG_IP, &ip);
panic ("FAILURE: unw_step() returned %d for ip=%lx\n",
ret, (long) ip);
}
}
while (ret > 0);
}
void
sighandler (int signal)
{
if (verbose)
printf ("sighandler(signal=%d, count=%d)\n", signal, sigcount);
do_backtrace (1, 1);
++sigcount;
if (sigcount == 100)
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_GLOBAL);
else if (sigcount == 200)
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
else if (sigcount == 300)
{
if (nerrors)
{
fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
exit (-1);
}
if (verbose)
printf ("SUCCESS.\n");
exit (0);
}
setitimer (ITIMER_VIRTUAL, &interval, NULL);
}
int
main (int argc, char **argv)
{
struct sigaction act;
long i = 0;
if (argc > 1)
verbose = 1;
unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_NONE);
memset (&act, 0, sizeof (act));
act.sa_handler = sighandler;
act.sa_flags = SA_SIGINFO;
sigaction (SIGVTALRM, &act, NULL);
setitimer (ITIMER_VIRTUAL, &interval, NULL);
while (1)
{
if (0 && verbose)
printf ("%s: starting backtrace\n", __FUNCTION__);
do_backtrace (0, (i++ % 100) == 0);
}
}