Implement init rc script, similar to .gdbinit

This commit is contained in:
kamkow1
2025-03-09 18:32:10 +01:00
parent 3cd61ab871
commit 4b10d7ea4d
2 changed files with 52 additions and 0 deletions

13
.debugusrc.js Normal file
View File

@@ -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();

View File

@@ -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)