From 4b10d7ea4d3196eecbdfa1324e03a06feba6497e Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 9 Mar 2025 18:32:10 +0100 Subject: [PATCH] Implement init rc script, similar to .gdbinit --- .debugusrc.js | 13 +++++++++++++ debugus.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .debugusrc.js diff --git a/.debugusrc.js b/.debugusrc.js new file mode 100644 index 0000000..b9fdfb8 --- /dev/null +++ b/.debugusrc.js @@ -0,0 +1,13 @@ +log_inf("Loading init script for program ./test"); +log_err("Test error message"); + +var main_offset = "0x0000000000001135"; // objdump -d ./test + +// Testing... +print_file(); +print_dbg_pid(); +print_program_load_offset(); +mk_brk_addr(main_offset); +list_brks(); +cont(); + diff --git a/debugus.c b/debugus.c index 55ce61e..c9efa02 100644 --- a/debugus.c +++ b/debugus.c @@ -23,6 +23,8 @@ #define shift(argc, argv, msg, ...) (argc <= 0 ? (LOG_ERR(msg, ##__VA_ARGS__), exit(EXIT_FAILURE)) : (void)0, argc--, *argv++) +#define INIT_SCRIPT ".debugusrc.js" + // How breakpoints work? // We can enable/disable breakpoints by putting/removing an int 3 instruction // into/from the executed program. int 3 will trigger a SIGTRAP, which we can @@ -160,6 +162,20 @@ void dbg_js_list_brks(js_State *js) js_pushundefined(js); } +void dbg_js_log_inf(js_State *js) +{ + const char *str = js_tostring(js, 1); + LOG_INF("%s\n", str); + js_pushundefined(js); +} + +void dbg_js_log_err(js_State *js) +{ + const char *str = js_tostring(js, 1); + LOG_ERR("%s\n", str); + js_pushundefined(js); +} + void dbg_init_js(Dbg *dbg) { dbg->js = js_newstate(NULL, NULL, JS_STRICT); @@ -178,6 +194,8 @@ void dbg_init_js(Dbg *dbg) make_js_func(list_brks, 0); make_js_func(set_program_load_offset, 1); make_js_func(print_program_load_offset, 0); + make_js_func(log_inf, 1); + make_js_func(log_err, 1); #undef make_js_func } @@ -201,6 +219,25 @@ void dbg_init_load_offset(Dbg *dbg) pmparser_free(&maps_iter); } +void dbg_load_init_script(Dbg *dbg, const char *script_path) +{ + FILE *script = fopen(script_path, "r"); + if (script == NULL) { + return; + } + + fseek(script, 0L, SEEK_END); + long size = ftell(script); + rewind(script); + char *script_buf = malloc(size+1); + fread(script_buf, size, 1, script); + script_buf[size] = '\0'; + + js_dostring(dbg->js, script_buf); + + fclose(script); +} + void dbg_init(Dbg *dbg, const char *file, pid_t pid) { memset(dbg, 0, sizeof(*dbg)); @@ -209,6 +246,8 @@ void dbg_init(Dbg *dbg, const char *file, pid_t pid) dbg_init_js(dbg); dbg_init_load_offset(dbg); hashtable_init(&dbg->brks, MAX_BRKS); + + dbg_load_init_script(dbg, INIT_SCRIPT); } void dbg_deinit(Dbg *dbg)