Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Super debugging redux
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Jan 4, 2018
1 parent 829f6d6 commit 5138f5b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 62 additions & 1 deletion lib/crt0_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions pthread/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 5138f5b

Please sign in to comment.