From 5644a0c456a452866295a88f844422e3fcac45d3 Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Thu, 20 Jan 2022 21:35:14 -0500 Subject: [PATCH] Starting to include debug info --- cc.c | 21 ++++++++++++++++++++- cc_env.c | 26 ++++++++++++++++++++++++++ cc_globals.c | 2 ++ cc_globals.h | 2 ++ cc_spawn.c | 9 ++++++--- makefile | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 4 deletions(-) diff --git a/cc.c b/cc.c index b3ed7a5..0980471 100644 --- a/cc.c +++ b/cc.c @@ -48,7 +48,10 @@ int main(int argc, char** argv, char** envp) char* name; char* hold; int DUMP_MODE = FALSE; - int DIRTY_MODE = FALSE; + DIRTY_MODE = FALSE; + + /* Assume no debugging by default */ + DEBUG_LEVEL = 0; int i = 1; while(i <= argc) @@ -72,6 +75,15 @@ int main(int argc, char** argv, char** envp) DIRTY_MODE = TRUE; i+= 1; } + else if(match(argv[i], "--debug-mode")) + { + hold = argv[i+1]; + DEBUG_LEVEL = strtoint(hold); + fputs("DEBUG_LEVEL set to: ", stderr); + fputs(hold, stderr); + fputc('\n', stderr); + i+= 2; + } else if(match(argv[i], "-f") || match(argv[i], "--file")) { if(NULL == hold_string) @@ -153,6 +165,8 @@ int main(int argc, char** argv, char** envp) } } + if(1 <= DEBUG_LEVEL) fputs("READ all files\n", stderr); + /* Deal with special case of wanting to read from standard input */ if(stdin == in) { @@ -166,13 +180,18 @@ int main(int argc, char** argv, char** envp) fputs("Either no input files were given or they were empty\n", stderr); exit(EXIT_FAILURE); } + + if(1 <= DEBUG_LEVEL) fputs("Start to reverse list\n", stderr); global_token = reverse_list(global_token); + if(1 <= DEBUG_LEVEL) fputs("List reversed\n", stderr); /* Get the environmental bits */ + if(1 <= DEBUG_LEVEL) fputs("Starting to setup Environment\n", stderr); populate_env(envp); setup_env(envp); M2LIBC_PATH = env_lookup("M2LIBC_PATH"); if(NULL == M2LIBC_PATH) M2LIBC_PATH = "./M2libc"; + if(1 <= DEBUG_LEVEL) fputs("Environment setup\n", stderr); if(DUMP_MODE) { diff --git a/cc_env.c b/cc_env.c index e41763d..80b61cd 100644 --- a/cc_env.c +++ b/cc_env.c @@ -24,9 +24,19 @@ char* env_lookup(char* variable); void setup_env() { + if(2 <= DEBUG_LEVEL) fputs("Starting setup_env\n", stderr); char* ARCH = NULL; struct utsname* unameData = calloc(1, sizeof(struct utsname)); + require(NULL != unameData, "unameData calloc failed\n"); uname(unameData); + if(3 <= DEBUG_LEVEL) fputs("obtained architecture details\n", stderr); + if(4 <= DEBUG_LEVEL) + { + fputs("utsname details: ", stderr); + fputs(unameData->machine, stderr); + fputc('\n', stderr); + } + if(match("i386", unameData->machine) || match("i486", unameData->machine) || match("i586", unameData->machine) || @@ -34,11 +44,18 @@ void setup_env() match("i686-pae", unameData->machine)) ARCH = "x86"; else if(match("x86_64", unameData->machine)) ARCH = "amd64"; else ARCH = unameData->machine; + if(3 <= DEBUG_LEVEL) + { + fputs("Architecture selected: ", stderr); + fputs(ARCH, stderr); + fputc('\n', stderr); + } /* Check for override */ char* hold = env_lookup("ARCHITECTURE_OVERRIDE"); if(NULL != hold) ARCH = hold; + if(3 <= DEBUG_LEVEL) fputs("override?\n", stderr); /* Set desired architecture */ WORDSIZE = 32; @@ -95,6 +112,7 @@ void setup_env() fputs(" know values are: knight-native, knight-posix, x86, amd64, armv7l, aarch64 and riscv64\n", stderr); exit(EXIT_FAILURE); } + if(2 <= DEBUG_LEVEL) fputs("setup_env successful\n", stderr); } struct Token @@ -161,9 +179,11 @@ char* env_lookup(char* variable) void populate_env(char** envp) { + if(2 <= DEBUG_LEVEL) fputs("populate_env started\n", stderr); /* You can't populate a NULL environment */ if(NULL == envp) { + if(3 <= DEBUG_LEVEL) fputs("NULL envp\n", stderr); return; } @@ -172,6 +192,7 @@ void populate_env(char** envp) if(0 == max) { + if(3 <= DEBUG_LEVEL) fputs("Empty envp\n", stderr); return; } @@ -185,6 +206,7 @@ void populate_env(char** envp) int k; char* envp_line; + if(3 <= DEBUG_LEVEL) fputs("starting env loop\n", stderr); for(i = 0; i < max; i = i + 1) { n->var = calloc(MAX_STRING, sizeof(char)); @@ -237,16 +259,20 @@ void populate_env(char** envp) require(n->next != NULL, "Memory initialization of n->next in population of env failed\n"); n = n->next; } + if(3 <= DEBUG_LEVEL) fputs("env loop successful\n", stderr); /* Get rid of node on the end */ n = NULL; /* Also destroy the n->next reference */ n = env; + require(NULL != n, "can't have an empty environment from the creation of a non-null environment\n"); + require(NULL != n->next, "should have an extra node at the end of the env\n"); while(n->next->var != NULL) { n = n->next; } n->next = NULL; + if(2 <= DEBUG_LEVEL) fputs("populate_env successful\n", stderr); } diff --git a/cc_globals.c b/cc_globals.c index b231f21..a719178 100644 --- a/cc_globals.c +++ b/cc_globals.c @@ -47,3 +47,5 @@ int STDIO_USED; /* So we don't shoot ourself in the face */ int FUZZING; +int DIRTY_MODE; +int DEBUG_LEVEL; diff --git a/cc_globals.h b/cc_globals.h index a7adbd4..0bc6c32 100644 --- a/cc_globals.h +++ b/cc_globals.h @@ -48,3 +48,5 @@ extern int STDIO_USED; /* So we don't shoot ourself in the face */ extern int FUZZING; +extern int DIRTY_MODE; +extern int DEBUG_LEVEL; diff --git a/cc_spawn.c b/cc_spawn.c index a3e5abd..f32f0a3 100644 --- a/cc_spawn.c +++ b/cc_spawn.c @@ -396,14 +396,17 @@ void spawn_processes(int debug_flag, char* preprocessed_file, char* destination, } /* We no longer need the M2-Planet tempfile output */ - remove(M2_output); + if(!DIRTY_MODE) remove(M2_output); /* Nor the blood-elf output anymore if it exists */ - if(!match("", blood_output)) remove(blood_output); + if(!match("", blood_output)) + { + if(!DIRTY_MODE) remove(blood_output); + } /* Build the final binary */ i = spawn_hex2(M1_output, destination, Architecture, envp, debug_flag); if(0 != i) exit(EXIT_FAILURE); /* clean up after ourselves*/ - remove(M1_output); + if(!DIRTY_MODE) remove(M1_output); } diff --git a/makefile b/makefile index 9a17780..237e8ec 100644 --- a/makefile +++ b/makefile @@ -22,6 +22,10 @@ PACKAGE = m2-mesoplanet # C compiler settings CC?=gcc CFLAGS:=$(CFLAGS) -D_GNU_SOURCE -O0 -std=c99 -ggdb +ARCH:=$(shell get_machine) +BLOOD_FLAG:=$(shell get_machine --blood) +ENDIAN_FLAG:=$(shell get_machine --endian) +BASE_ADDRESS:=$(shell get_machine --hex2) all: M2-Mesoplanet @@ -39,12 +43,60 @@ M2-Mesoplanet: bin results cc.h cc_reader.c cc_core.c cc_macro.c cc_env.c cc_spa gcc_req.h \ -o bin/M2-Mesoplanet +M2-boot: bin results cc.h cc_reader.c cc_core.c cc_macro.c cc_env.c cc_spawn.c cc.c cc_globals.c cc_globals.h + echo $(ARCH) + echo $(BLOOD_FLAG) + echo $(ENDIAN_FLAG) + echo $(BASE_ADDRESS) + M2-Planet --architecture ${ARCH} \ + -f M2libc/sys/types.h \ + -f M2libc/stddef.h \ + -f M2libc/${ARCH}/linux/fcntl.c \ + -f M2libc/${ARCH}/linux/unistd.c \ + -f M2libc/${ARCH}/linux/sys/stat.c \ + -f M2libc/stdlib.c \ + -f M2libc/stdio.c \ + -f M2libc/string.c \ + -f M2libc/bootstrappable.c \ + -f cc.h \ + -f cc_globals.c \ + -f cc_env.c \ + -f cc_reader.c \ + -f cc_spawn.c \ + -f cc_core.c \ + -f cc_macro.c \ + -f cc.c \ + --debug \ + -o ./bin/M2-Mesoplanet-1.M1 + blood-elf ${ENDIAN_FLAG} ${BLOOD_FLAG} -f ./bin/M2-Mesoplanet-1.M1 -o ./bin/M2-Mesoplanet-1-footer.M1 + M1 --architecture ${ARCH} \ + ${ENDIAN_FLAG} \ + -f M2libc/${ARCH}/${ARCH}_defs.M1 \ + -f M2libc/${ARCH}/libc-full.M1 \ + -f ./bin/M2-Mesoplanet-1.M1 \ + -f ./bin/M2-Mesoplanet-1-footer.M1 \ + -o ./bin/M2-Mesoplanet-1.hex2 + hex2 --architecture ${ARCH} \ + ${ENDIAN_FLAG} \ + --base-address ${BASE_ADDRESS} \ + -f ../M2libc/${ARCH}/ELF-${ARCH}-debug.hex2 \ + -f ./bin/M2-Mesoplanet-1.hex2 \ + -o ./bin/M2-Mesoplanet + + # Clean up after ourselves .PHONY: clean clean: rm -rf bin/ # ./test/test0000/cleanup.sh +.PHONY: clean-temp +clean-tmp: + rm -vf /tmp/M2-Mesoplanet-* + rm -vf /tmp/M2-Planet-* + rm -vf /tmp/M1-macro-* + rm -vf /tmp/blood-elf-* + # Directories bin: mkdir -p bin