From 5138f5b58600a58b60f2fcffc69cd3f5fd97af64 Mon Sep 17 00:00:00 2001 From: roblabla Date: Wed, 20 Dec 2017 19:19:24 +0100 Subject: [PATCH] Super debugging redux --- Makefile | 7 +++++- lib/crt0_common.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++- pthread/Makefile | 4 +-- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index ced6d1e7..b0219efd 100644 --- a/Makefile +++ b/Makefile @@ -51,12 +51,17 @@ $(LIBTRANSISTOR_HOME)/build/test/%.o: $(LIBTRANSISTOR_HOME)/test/%.c # Disable stack protector for crt0_common $(LIBTRANSISTOR_HOME)/build/lib/crt0_common.o: $(LIBTRANSISTOR_HOME)/lib/crt0_common.c + mkdir -p $(@D) + $(CC) $(CC_FLAGS) $(WARNINGS) -fno-stack-protector -Ipthread/ -Ipthread/sys/switch -c -o $@ $< + +# Don't instrument ipc.c, it might mess up the state +$(LIBTRANSISTOR_HOME)/build/lib/ipc.o: $(LIBTRANSISTOR_HOME)/lib/ipc.c mkdir -p $(@D) $(CC) $(CC_FLAGS) $(WARNINGS) -fno-stack-protector -c -o $@ $< $(LIBTRANSISTOR_HOME)/build/lib/%.o: $(LIBTRANSISTOR_HOME)/lib/%.c mkdir -p $(@D) - $(CC) $(CC_FLAGS) $(WARNINGS) -c -o $@ $< + $(CC) $(CC_FLAGS) $(WARNINGS) -finstrument-functions -c -o $@ $< $(LIBTRANSISTOR_HOME)/build/lib/%.o: $(LIBTRANSISTOR_HOME)/lib/%.S mkdir -p $(@D) diff --git a/lib/crt0_common.c b/lib/crt0_common.c index e78f1dcd..3c9e0c5e 100644 --- a/lib/crt0_common.c +++ b/lib/crt0_common.c @@ -159,7 +159,7 @@ static int bsslog_write(struct _reent *reent, void *v, const char *ptr, int len) static jmp_buf exit_jmpbuf; static int exit_value; - +static void *global_aslr_base; int _libtransistor_start(libtransistor_context_t *ctx, void *aslr_base) { if(relocate(aslr_base)) { return -4; @@ -170,6 +170,7 @@ int _libtransistor_start(libtransistor_context_t *ctx, void *aslr_base) { char *argv_default[] = {"contextless", NULL}; char **argv = argv_default; + global_aslr_base = aslr_base; int argc = 1; if(ctx != NULL) { @@ -243,7 +244,9 @@ int _libtransistor_start(libtransistor_context_t *ctx, void *aslr_base) { stdout = &bsslog_stdout; stderr = &bsslog_stdout; } + dbg_printf("set up stdout"); + printf("ASLR base %p\n", aslr_base); if(init_array != NULL) { if(init_array_size == -1) { @@ -295,3 +298,61 @@ void _exit(int ret) { exit_value = ret; longjmp(exit_jmpbuf, 1); } + +char *ft_itoa(char buf[16], uintmax_t n, char *base) +{ + int i; + size_t base_len; + + i = 0; + base_len = strlen(base); + while (n > 0) + { + buf[i++] = base[n % base_len]; + n /= base_len; + } + i = 0; + while (i < 16 / 2) { + char c = buf[i]; + buf[i] = buf[15 - i]; + buf[15 - i] = c; + i++; + } + return (buf); +} + +static int in_cyg = 0; +void __cyg_profile_func_enter(void *des, void *src_call) { + if (in_cyg) + return; + in_cyg = 1; + + + char msg[] = "\nThread 0x0000000000000000 Entering function 0x0000000000000000 from 0x0000000000000000\n"; + + void *tid = get_tls(); + ft_itoa(msg + strlen("\nThread 0x"), tid, "0123456789ABCDEF"); + ft_itoa(msg + strlen("\nThread 0x0000000000000000 Entering function 0x"), des - global_aslr_base, "0123456789ABCDEF"); + ft_itoa(msg + strlen("\nThread 0x0000000000000000 Entering function 0x0000000000000000 from 0x"), src_call - global_aslr_base, "0123456789ABCDEF"); + if (bsd_get_object().object_id != 0 && libtransistor_context.has_bsd && libtransistor_context.std_socket > 0) { + bsd_send(libtransistor_context.std_socket, msg, strlen(msg), 0); + } + in_cyg = 0; +} + +void __cyg_profile_func_exit(void *des, void *src_call) { + if (in_cyg) + return; + in_cyg = 1; + + char msg[] = "\nThread 0x0000000000000000 Exit function 0x0000000000000000 to 0x0000000000000000\n"; + + void *tid = get_tls(); + ft_itoa(msg + strlen("\nThread 0x"), tid, "0123456789ABCDEF"); + ft_itoa(msg + strlen("\nThread 0x0000000000000000 Exit function 0x"), des - global_aslr_base, "0123456789ABCDEF"); + ft_itoa(msg + strlen("\nThread 0x0000000000000000 Exit function 0x0000000000000000 to 0x"), src_call - global_aslr_base, "0123456789ABCDEF"); + if (bsd_get_object().object_id != 0 && libtransistor_context.has_bsd && libtransistor_context.std_socket > 0) { + bsd_send(libtransistor_context.std_socket, msg, strlen(msg), 0); + } + in_cyg = 0; +} diff --git a/pthread/Makefile b/pthread/Makefile index e00f579d..e1982fc8 100644 --- a/pthread/Makefile +++ b/pthread/Makefile @@ -3,9 +3,9 @@ NAME=libpthread.a #CFLAGS+=-Wall -g -Werror -Wshadow -#CFLAGS+=-Werror-implicit-function-declaration +CC_FLAGS += -Werror-implicit-function-declaration -finstrument-functions #CFLAGS+=-Wsign-compare -CC_FLAGS+=-Isys/switch/ +CC_FLAGS += -Isys/switch/ # TODO: Let's cheat CC_FLAGS := -isystem . $(CC_FLAGS)