1
0
Fork 0
mirror of https://github.com/tobast/libunwind-eh_elf.git synced 2024-11-13 03:58:11 +01:00
libunwind-eh_elf/tests/test-dyn1.c
2002-12-03 08:19:58 +00:00

146 lines
3.1 KiB
C

#include <libunwind.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#define panic(args...) \
{ fprintf (stderr, args); exit (-1); }
typedef void (*template_t) (int, void (*)(),
int (*)(const char *, ...), const char *,
const char **);
static const char *strarr[] =
{
"i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", NULL
};
#ifdef __ia64__
struct fdesc
{
long code;
long gp;
};
# define get_fdesc(fdesc,func) (fdesc = *(struct fdesc *) &(func))
# define get_funcp(fdesc) ((template_t) &(fdesc))
#else
struct fdesc
{
long code;
};
# define get_fdesc(fdesc,func) (fdesc.code = (long) &(func))
# define get_funcp(fdesc) ((template_t) (fdesc).code)
#endif
static void
flush_cache (void *addr, unsigned long len)
{
void *end = (char *) addr + len;
#ifdef __ia64__
while (addr < end)
{
asm volatile ("fc %0" :: "r"(addr));
addr = (char *) addr + 32;
}
asm volatile (";;sync.i;;srlz.i;;");
#endif
}
void
template (int i, template_t self,
int (*printer)(const char *, ...), const char *fmt, const char **arr)
{
(*printer) (fmt, arr[11 - i][0], arr[11 - i] + 1);
if (i > 0)
(*self) (i - 1, self, printer, fmt, arr);
}
static void
sighandler (int signal)
{
unw_cursor_t cursor;
unw_word_t ip;
unw_context_t uc;
int count = 0;
printf ("caught signal %d\n", signal);
unw_getcontext (&uc);
unw_init_local (&cursor, &uc);
while (!unw_is_signal_frame (&cursor))
if (unw_step (&cursor) < 0)
panic ("failed to find signal frame!\n");
unw_step (&cursor);
do
{
unw_get_reg (&cursor, UNW_REG_IP, &ip);
printf ("ip = %lx\n", (long) ip);
++count;
}
while (unw_step (&cursor) > 0);
if (count != 13)
panic ("FAILURE: expected 13, not %d frames below signal frame\n", count);
printf ("SUCCESS\n");
exit (0);
}
int
main (int argc, char *argv[])
{
unw_dyn_region_info_t *region;
unw_dyn_info_t di;
struct fdesc fdesc;
template_t funcp;
void *mem;
int ret;
mem = malloc (getpagesize ());
get_fdesc (fdesc, template);
printf ("old code @ %p, new code @ %p\n", (void *) fdesc.code, mem);
memcpy (mem, (void *) fdesc.code, 256);
mprotect ((void *) ((long) mem & ~(getpagesize () - 1)),
2*getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC);
flush_cache (mem, 256);
signal (SIGSEGV, sighandler);
/* register the new function: */
region = alloca (_U_dyn_region_info_size (2));
region->next = NULL;
region->insn_count = 256;
region->op_count = 2;
_U_dyn_op_alias (&region->op[0], 0, -1, fdesc.code);
_U_dyn_op_stop (&region->op[1]);
di.start_ip = (long) mem;
di.end_ip = (long) mem + 256;
di.gp = fdesc.gp;
di.format = UNW_INFO_FORMAT_DYNAMIC;
di.u.pi.name = "<copy of template()>";
di.u.pi.handler = 0;
di.u.pi.flags = 0;
di.u.pi.regions = region;
ret = _U_dyn_register (&di);
if (ret < 0)
panic ("call to _U_dyn_register() failed: ret=%d\n", ret);
/* call new function: */
fdesc.code = (long) mem;
funcp = get_funcp (fdesc);
(*funcp) (10, funcp, printf, "iteration %c%s\n", strarr);
return -1;
}