summaryrefslogtreecommitdiff
path: root/src/wrapper
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2018-02-22 18:40:15 +0200
committerJari Vetoniemi <mailroxas@gmail.com>2018-02-22 18:46:36 +0200
commitc4f2f8449ad09301e5725f69b7374d958cc5dff8 (patch)
tree084c8e916e09cd0cbd16bd6732b0fbff217fc81b /src/wrapper
parent952720c8def83d62bc0d328c2cc46ead8254703d (diff)
refactor commit
Diffstat (limited to 'src/wrapper')
-rw-r--r--src/wrapper/verbose.h16
-rw-r--r--src/wrapper/wrapper.c29
2 files changed, 34 insertions, 11 deletions
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 <stdarg.h>
+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 <dlfcn.h>
#include <assert.h>
#include <sys/mman.h>
+#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);