1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2002-2003 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of libunwind.
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26 /* This file tests dynamic code-generation via function-cloning. */
28 #include "flush-cache.h"
32 #include <libunwind.h>
41 #define MAX_FUNC_SIZE 96 /* FIXME: arch/compiler dependent */
43 #define MAX_FUNC_SIZE 2048 /* max. size of cloned function */
46 #define panic(args...) \
47 { fprintf (stderr, args); exit (-1); }
49 typedef void (*template_t) (int, void (*)(),
50 int (*)(const char *, ...), const char *,
55 static const char *strarr[] =
57 "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", NULL
66 # define get_fdesc(fdesc,func) (fdesc = *(struct fdesc *) &(func))
67 # define get_funcp(fdesc) ((template_t) &(fdesc))
68 # define get_gp(fdesc) ((fdesc).gp)
75 /* Workaround GCC bug: https://bugs.launchpad.net/gcc-linaro/+bug/721531 */
76 # define get_fdesc(fdesc,func) ({long tmp = (long) &(func); \
77 (fdesc).code = (long) &(func) & ~0x1; \
78 (fdesc).is_thumb = tmp & 0x1;})
79 /*# define get_fdesc(fdesc,func) ({(fdesc).code = (long) &(func) & ~0x1; \
80 (fdesc).is_thumb = (long) &(func) & 0x1;})*/
81 # define get_funcp(fdesc) ((template_t) ((fdesc).code | (fdesc).is_thumb))
82 # define get_gp(fdesc) (0)
88 # define get_fdesc(fdesc,func) (fdesc.code = (long) &(func))
89 # define get_funcp(fdesc) ((template_t) (fdesc).code)
90 # define get_gp(fdesc) (0)
94 template (int i, template_t self,
95 int (*printer)(const char *, ...), const char *fmt, const char **arr)
97 (*printer) (fmt, arr[11 - i][0], arr[11 - i] + 1);
99 (*self) (i - 1, self, printer, fmt, arr);
103 sighandler (int signal)
106 char name[128], off[32];
107 unw_word_t ip, offset;
112 printf ("caught signal %d\n", signal);
114 unw_getcontext (&uc);
115 unw_init_local (&cursor, &uc);
118 while (!unw_is_signal_frame (&cursor))
120 if (unw_step (&cursor) < 0)
121 panic ("failed to find signal frame!\n");
125 panic ("Too many steps to the signal frame (%d)\n", count);
134 unw_get_reg (&cursor, UNW_REG_IP, &ip);
137 if (unw_get_proc_name (&cursor, name, sizeof (name), &offset) == 0
139 snprintf (off, sizeof (off), "+0x%lx", (long) offset);
141 printf ("ip = %lx <%s%s>\n", (long) ip, name, off);
146 panic ("Too many steps (%d)\n", count);
151 while (unw_step (&cursor) > 0);
154 panic ("FAILURE: expected 13, not %d frames below signal frame\n", count);
157 printf ("SUCCESS\n");
162 dev_null (const char *format UNUSED, ...)
168 main (int argc, char *argv[] UNUSED)
170 unw_dyn_region_info_t *region;
179 mem = malloc (getpagesize ());
181 get_fdesc (fdesc, template);
184 printf ("old code @ %p, new code @ %p\n", (void *) fdesc.code, mem);
186 memcpy (mem, (void *) fdesc.code, MAX_FUNC_SIZE);
187 mprotect ((void *) ((long) mem & ~(getpagesize () - 1)),
188 2*getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC);
190 flush_cache (mem, MAX_FUNC_SIZE);
192 signal (SIGSEGV, sighandler);
194 /* register the new function: */
195 region = alloca (_U_dyn_region_info_size (2));
197 region->insn_count = 3 * (MAX_FUNC_SIZE / 16);
198 region->op_count = 2;
199 _U_dyn_op_alias (®ion->op[0], 0, -1, fdesc.code);
200 _U_dyn_op_stop (®ion->op[1]);
202 memset (&di, 0, sizeof (di));
203 di.start_ip = (long) mem;
204 di.end_ip = (long) mem + 16*region->insn_count/3;
205 di.gp = get_gp (fdesc);
206 di.format = UNW_INFO_FORMAT_DYNAMIC;
207 di.u.pi.name_ptr = (unw_word_t) "copy_of_template";
208 di.u.pi.regions = region;
210 _U_dyn_register (&di);
212 /* call new function: */
213 fdesc.code = (long) mem;
214 funcp = get_funcp (fdesc);
217 (*funcp) (10, funcp, printf, "iteration %c%s\n", strarr);
219 (*funcp) (10, funcp, dev_null, "iteration %c%s\n", strarr);