diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/events.c | 12 | ||||
| -rw-r--r-- | src/hashmap.c | 15 | ||||
| -rw-r--r-- | src/main.c | 34 | ||||
| -rw-r--r-- | src/module.c | 23 | ||||
| -rw-r--r-- | src/parser.c | 16 | ||||
| -rw-r--r-- | src/pdb.c | 8 | ||||
| -rw-r--r-- | src/stack_walk.c | 15 | ||||
| -rw-r--r-- | src/utils.c | 20 |
8 files changed, 76 insertions, 67 deletions
diff --git a/src/events.c b/src/events.c index 4f2115a..7492597 100644 --- a/src/events.c +++ b/src/events.c @@ -1,3 +1,12 @@ +#include <Windows.h> + +#include "utils.h" +#include "pdb.h" + +u8 expect_single_step = FALSE; + +extern DEBUG_EVENT dbg_event; + const char* dummy_name = "Function_dummy_name"; void read_name_uni(const void* name_addr, const char* name, size_t name_size) { @@ -24,6 +33,7 @@ void read_name_uni(const void* name_addr, const char* name, size_t name_size) { NULL ); } + void read_name_ansi(const void* name_addr, const char* name, size_t name_size) { u8* addr = NULL; if (!read_memory(name_addr, (void*)&addr, 8U)) { @@ -362,6 +372,8 @@ void handle_create_thread() { } void handle_exception() { + extern DWORD continue_status; + print("%s : ", "ExceptionDebugEvent"); if (dbg_event.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP && expect_single_step) { diff --git a/src/hashmap.c b/src/hashmap.c index 5b2d5a0..4a59e15 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,17 +1,8 @@ -struct element { - u64 name; - u32 start; - u32 end; - u32 unwind_offset; - u32 psl; -}; +#include "hashmap.h" -struct hashmap { - u32 capacity; - u32 size; +#include <Windows.h> - struct element* arr; -}; +#include "utils.h" static const u32 initial_size = 4096U * 100U; static const u32 empty_psl = (u32)-1; @@ -4,38 +4,24 @@ #include "types.h" -#include "utils.c" -#include "hashmap.c" +#include "utils.h" +#include "events.h" +#include "parser.h" extern u32 _fltused = TRUE; -static HANDLE h_input = NULL; -static HANDLE h_output = NULL; -static u8 is_open = TRUE; -static u8 process_commands = TRUE; -static u8 expect_single_step = FALSE; -static DWORD continue_status = DBG_EXCEPTION_NOT_HANDLED; -static DEBUG_EVENT dbg_event; -static PROCESS_INFORMATION proc_info = { 0 }; +u8 is_open = TRUE; +u8 process_commands = TRUE; +HANDLE h_input = NULL; +HANDLE h_output = NULL; +DEBUG_EVENT dbg_event; +PROCESS_INFORMATION proc_info = { 0 }; +DWORD continue_status = DBG_EXCEPTION_NOT_HANDLED; // TODO: Perfect hashing table ? u8* string_stack_base = NULL; u8* string_stack_current = NULL; -/* Read debugged process memory at address */ -u8 read_memory(const void* addr, void* buffer, size_t size) { - size_t b_read = 0U; - HRESULT hr = ReadProcessMemory(proc_info.hProcess, addr, buffer, size, &b_read); - - return hr > 0 && b_read == size; -} - -#include "module.c" -#include "stack_walk.c" -#include "parser.c" -#include "pdb.c" -#include "events.c" - void dbg_loop() { while(is_open) { WaitForDebugEventEx(&dbg_event, INFINITE); diff --git a/src/module.c b/src/module.c index 9148aa8..ad50d7a 100644 --- a/src/module.c +++ b/src/module.c @@ -1,23 +1,6 @@ -struct module { - void* base_addr; - u8** functions_name; - u32* functions_start; - u32* functions_end; - u32* functions_unwind; - u32 functions_count; - u32 functions_capacity; - // u32 functions_name_capacity; - // u32 function_start_capacity; - // u32 function_end_capacity; - // u32 function_unwind_capacity; - - IMAGE_DOS_HEADER dos_header; - IMAGE_NT_HEADERS64 nt_header; -#ifdef HASHMAP - struct hashmap symbols; -#endif - u8 name[256U]; -}; +#include "module.h" + +#include "utils.h" struct module modules[64U] = { 0U }; u64 modules_list = ~0ULL; diff --git a/src/parser.c b/src/parser.c index ab9bde1..1731c70 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,3 +1,14 @@ +#include "parser.h" + +#include <Windows.h> + +#include "utils.h" +#include "module.h" +#include "stack_walk.h" + +extern u8 expect_single_step; +extern DWORD continue_status; + enum TOKEN_TYPE { // Commands TOKEN_CONTINUE, @@ -194,6 +205,10 @@ u64 eval_number(const struct ast_node* node) { } void run(struct ast_node* root) { + extern DEBUG_EVENT dbg_event; + extern u8 process_commands; + extern u8 is_open; + struct ast_node cur_node = root[0]; CONTEXT ctx = { .ContextFlags = CONTEXT_ALL, @@ -225,7 +240,6 @@ void run(struct ast_node* root) { } } else if (cur_node.token->type == TOKEN_PRINT_REG) { print("RIP : %xu\n", ctx.Rip); - print("RAX : %xu\n", ctx.Rax); print("RCX : %xu\n", ctx.Rcx); print("RDX : %xu\n", ctx.Rdx); print("RBX : %xu\n", ctx.Rbx); @@ -1,5 +1,10 @@ +#include "pdb.h" + +#include <strsafe.h> #include "cvinfo.h" +#include "utils.h" + enum STREAM_INDEX { OLD_DIRECTORY, PDB, @@ -8,6 +13,9 @@ enum STREAM_INDEX { IPI, }; +extern u8* string_stack_base; +extern u8* string_stack_current; + void open_pdb(const char* pdb_path, struct module* module) { HANDLE pdb_file = CreateFileA(pdb_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); diff --git a/src/stack_walk.c b/src/stack_walk.c index bb4f4be..88a21db 100644 --- a/src/stack_walk.c +++ b/src/stack_walk.c @@ -1,3 +1,10 @@ +#include "stack_walk.h" + +#include <Windows.h> + +#include "module.h" +#include "utils.h" + // enum UNWIND_INFO_FLAGS { // UNW_FLAG_EHANDLER, // UNW_FLAG_UHANDLER, @@ -130,10 +137,10 @@ void print_reg(enum UNWIND_OP_INFO_TYPE op_info) { } } -void walk_stack(CONTEXT* ctx) { - u64 addr = ctx->Rip; - u64 rsp = ctx->Rsp; - u64 rbp = ctx->Rbp; +void walk_stack(void* ctx) { + u64 addr = ((CONTEXT*)ctx)->Rip; + u64 rsp = ((CONTEXT*)ctx)->Rsp; + u64 rbp = ((CONTEXT*)ctx)->Rbp; struct module* module = NULL; struct UNWIND_INFO* u_info = VirtualAlloc(NULL, 4096U, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); while (addr) { diff --git a/src/utils.c b/src/utils.c index de719e9..440b9fb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,9 +1,7 @@ -#if defined (_DEBUG) -#define assert(x) if (!(x)) __debugbreak(); -#else -#define assert(x) (x) -#endif -void print(const char* format, ...); +#include "utils.h" + +#include <Windows.h> +#include <strsafe.h> u8 is_alpha(char c) { return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); @@ -155,3 +153,13 @@ void print(const char* format, ...) { StringCchLengthA(formatted_str, sizeof(formatted_str), &length); SUCCEEDED(WriteFile(standard_err, formatted_str, (DWORD)length, NULL, NULL)); } + +/* Read debugged process memory at address */ +u8 read_memory(const void* addr, void* buffer, size_t size) { + extern PROCESS_INFORMATION proc_info; + + size_t b_read = 0U; + HRESULT hr = ReadProcessMemory(proc_info.hProcess, addr, buffer, size, &b_read); + + return hr > 0 && b_read == size; +} |
