From c4f2f8449ad09301e5725f69b7374d958cc5dff8 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Thu, 22 Feb 2018 18:40:15 +0200 Subject: refactor commit --- src/wrapper/verbose.h | 16 ++++++++++++++++ src/wrapper/wrapper.c | 29 ++++++++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 src/wrapper/verbose.h (limited to 'src/wrapper') diff --git a/src/wrapper/verbose.h b/src/wrapper/verbose.h new file mode 100644 index 0000000..719edc7 --- /dev/null +++ b/src/wrapper/verbose.h @@ -0,0 +1,16 @@ +#pragma once + +#ifdef VERBOSE_FUNCTIONS +# include +static void +verbose(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fputc('\n', stderr); +} +#else +# define verbose(...) +#endif diff --git a/src/wrapper/wrapper.c b/src/wrapper/wrapper.c index 07d3239..3acef77 100644 --- a/src/wrapper/wrapper.c +++ b/src/wrapper/wrapper.c @@ -8,8 +8,10 @@ #include #include #include +#include "verbose.h" -#ifdef ANDROID_X86_LINKER +#ifdef VERBOSE_FUNCTIONS +# ifdef ANDROID_X86_LINKER __asm__( "wrapper_start: nop\n" "wrapper_symbol: pushl $0xFAFBFCFD\n" @@ -17,33 +19,38 @@ __asm__( "wrapper_call: movl $0xFAFBFCFD, %eax\njmp *%eax\n" "wrapper_end: nop\n" ); -# define WRAPPER_TRACE -#else -# warning "no wrapper asm for this platform, function tracing is not available" +# define WRAPPER_TRACE +# else +# warning "no wrapper asm for this platform, function tracing is not available" +# endif #endif #ifdef WRAPPER_TRACE extern char wrapper_start, wrapper_symbol, wrapper_trace, wrapper_call, wrapper_end; -static char* (*__cxa_demangle)(const char *mangled_name, char *output_buffer, size_t *length, int *status); + +static union { + void *ptr; + char* (*fun)(const char *mangled_name, char *output_buffer, size_t *length, int *status); +} __cxa_demangle; static void trace(const char *const symbol) { - if (__cxa_demangle) { + if (__cxa_demangle.ptr) { // >If output_buffer is not long enough, it is expanded using realloc // Holy fuck gcc what the fuck? Guess we don't use stack then, thanks int status; char *demangled; static __thread char *data; static __thread size_t size; - if ((demangled = __cxa_demangle(symbol, data, &size, &status))) { + if ((demangled = __cxa_demangle.fun(symbol, data, &size, &status))) { data = (data != demangled ? demangled : data); - printf("trace: %s\n", demangled); + verbose("trace: %s", demangled); return; } } - printf("trace: %s\n", symbol); + verbose("trace: %s", symbol); } #endif @@ -51,8 +58,8 @@ void* wrapper_create(const char *const symbol, void *function) { #ifdef WRAPPER_TRACE - if (!__cxa_demangle) - __cxa_demangle = dlsym(RTLD_DEFAULT, "__cxa_demangle"); + if (!__cxa_demangle.ptr) + __cxa_demangle.ptr = dlsym(RTLD_DEFAULT, "__cxa_demangle"); const size_t sz = &wrapper_end - &wrapper_start; unsigned char *fun = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); -- cgit v1.2.3-70-g09d2