From 7c27588c132a0755df93c0c81dec442cc13cdfdc Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 16 Apr 2009 20:49:18 +0000 Subject: [PATCH] * Sync x86 code analysis fixups from r1 * Fix some segfaults on the not-yet-used dbg.reg api * Now uses .radare2rc * Some R_APIzation in r_debug --- libr/anal/p/anal_x86.c | 36 ++++++++++-------------------------- libr/core/cmd.c | 8 ++++++-- libr/core/config.c | 1 + libr/core/core.c | 1 + libr/core/file.c | 4 ++++ libr/core/t/radare2.c | 8 ++++---- libr/debug/debug.c | 3 ++- libr/debug/reg.c | 17 +++++++++++------ libr/include/r_debug.h | 40 +++++++++++++++++++++++++++------------- libr/reg/README | 3 +++ 10 files changed, 69 insertions(+), 52 deletions(-) diff --git a/libr/anal/p/anal_x86.c b/libr/anal/p/anal_x86.c index 697285405a..fd4d088336 100644 --- a/libr/anal/p/anal_x86.c +++ b/libr/anal/p/anal_x86.c @@ -141,6 +141,16 @@ static int aop(struct r_anal_t *anal, struct r_anal_aop_t *aop, void *data) aop->length = 1; aop->type = R_ANAL_AOP_TYPE_SWI; break; + case 0xb8: // mov eax, + case 0xb9: // mov ecx, + case 0xba: // mov edx, + case 0xbb: // mov ebx, + case 0xbc: // mov esp, + case 0xbd: // mov esp, + case 0xbf: + aop->type = R_ANAL_AOP_TYPE_MOV; // bfdc054000 mov edi, 0x4005dc + aop->ref = buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24);//((unsigned long)((buf+2))+6); + break; case 0xcd: aop->length = 2; aop->type = R_ANAL_AOP_TYPE_SWI; @@ -329,32 +339,6 @@ static int aop(struct r_anal_t *anal, struct r_anal_aop_t *aop, void *data) //vm_arch_x86_regs[VM_X86_EAX] = anal->pc+buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24); //radare_read_at((u64)vm_arch_x86_regs[VM_X86_EAX], (unsigned char *)&(vm_arch_x86_regs[VM_X86_EAX]), 4); break; - - // roll to a switch range case - case 0xb8: // mov eax, - aop->type = R_ANAL_AOP_TYPE_MOV; - //vm_arch_x86_regs[VM_X86_EAX] = anal->pc+buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24); - break; - case 0xb9: // mov ecx, - aop->type = R_ANAL_AOP_TYPE_MOV; - //vm_arch_x86_regs[VM_X86_ECX] = anal->pc+buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24); - break; - case 0xba: // mov edx, - aop->type = R_ANAL_AOP_TYPE_MOV; - //vm_arch_x86_regs[VM_X86_EDX] = anal->pc+buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24); - break; - case 0xbb: // mov ebx, - aop->type = R_ANAL_AOP_TYPE_MOV; - //vm_arch_x86_regs[VM_X86_EBX] = anal->pc+buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24); - break; - case 0xbc: // mov esp, - aop->type = R_ANAL_AOP_TYPE_MOV; - //vm_arch_x86_regs[VM_X86_ESP] = anal->pc+buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24); - break; - case 0xbd: // mov esp, - aop->type = R_ANAL_AOP_TYPE_MOV; - //vm_arch_x86_regs[VM_X86_EBP] = anal->pc+buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24); - break; #if 0 case0xF /* conditional jump */ diff --git a/libr/core/cmd.c b/libr/core/cmd.c index b3d72f1c7c..ed8c8b53dc 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -1509,7 +1509,7 @@ int r_core_cmd(struct r_core_t *core, const char *command, int log) cmd[len-1]='\0'; strcpy(cmd, cmd+1); ret = r_cmd_call(&core->cmd, cmd); - free(cmd); + free(ocmd); return ret; } @@ -1655,7 +1655,11 @@ static int cmd_debug(void *data, const char *input) system("cat /proc/$PID/maps"); } break; case 'r': - r_core_cmd(core, "|reg", 0); // XXX +#if 0 + r_debug_reg_sync(&core->dbg, 0); + r_debug_reg_list(&core->dbg, NULL, input[1]=='*'); +#endif + r_core_cmd(core, "|reg", 0); break; case 'p': // TODO: Support PID and Thread diff --git a/libr/core/config.c b/libr/core/config.c index d71a03ac2b..d4c3b77799 100644 --- a/libr/core/config.c +++ b/libr/core/config.c @@ -82,6 +82,7 @@ R_API int r_core_config_init(struct r_core_t *core) r_config_set(cfg, "asm.reflinesstyle", "false"); r_config_set(cfg, "asm.reflineswide", "true"); r_config_set(cfg, "asm.comments", "true"); + r_config_set(cfg, "cmd.open", ""); r_config_set(cfg, "cmd.prompt", ""); r_config_set(cfg, "cmd.vprompt", ""); r_config_set(cfg, "cmd.hit", ""); diff --git a/libr/core/core.c b/libr/core/core.c index d950e3d025..0589694d4e 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -151,6 +151,7 @@ R_API int r_core_init(struct r_core_t *core) r_core_cmd_init(core); r_flag_init(&core->flags); r_debug_init(&core->dbg); + core->dbg.printf = r_cons_printf; r_debug_set_io(&core->dbg, &__dbg_read, &__dbg_write, core); r_core_config_init(core); // XXX fix path here diff --git a/libr/core/file.c b/libr/core/file.c index 0fa73999ca..d958d398cd 100644 --- a/libr/core/file.c +++ b/libr/core/file.c @@ -37,6 +37,10 @@ R_API struct r_core_file_t *r_core_file_open(struct r_core_t *r, const char *fil r_core_block_read(r, 0); + p = r_config_get(&r->config, "cmd.open"); + if (p && *p) + r_core_cmd(r, p, 0); + return fh; } diff --git a/libr/core/t/radare2.c b/libr/core/t/radare2.c index 5da4351577..b53eb438a0 100644 --- a/libr/core/t/radare2.c +++ b/libr/core/t/radare2.c @@ -15,7 +15,7 @@ static int main_help(int line) if (!line) printf( " -d use 'file' as a program to debug\n" " -w open file in write mode\n" - " -n do not run ~/.radarerc\n" + " -n do not run ~/.radare2rc\n" " -f block size = file size\n" " -s [addr] initial seek\n" " -b [size] initial block size\n" @@ -139,7 +139,7 @@ int main(int argc, char **argv) } if (run_rc) { - char *homerc = r_str_home (".radarerc"); + char *homerc = r_str_home (".radare2rc"); if (homerc) { r_core_cmd_file (&r, homerc); free (homerc); @@ -149,9 +149,9 @@ int main(int argc, char **argv) if (debug) { r_core_cmd (&r, "dh ptrace", 0); r_core_cmdf (&r, "dp %d", r.file->fd); - r_core_cmd (&r, ".dr*", 0); + //r_core_cmd (&r, ".dr*", 0); r_core_cmd (&r, "s eip", 0); - r_core_cmd (&r, "e cmd.prompt=.dr",0); + //r_core_cmd (&r, "e cmd.prompt=.dr",0); r_core_cmd (&r, "\"e cmd.vprompt=.dr\"",0); r_core_cmd (&r, "\"e cmd.visual=.dr\"",0); } diff --git a/libr/debug/debug.c b/libr/debug/debug.c index 9c4ff65bce..c362d80c83 100644 --- a/libr/debug/debug.c +++ b/libr/debug/debug.c @@ -9,13 +9,14 @@ R_API int r_debug_init(struct r_debug_t *dbg) dbg->swstep = 0; // software step dbg->newstate = 0; dbg->regs = dbg->oregs = NULL; + dbg->printf = printf; dbg->h = NULL; r_debug_handle_init(dbg); r_bp_init(&dbg->bp); return R_TRUE; } -R_API int r_debug_set_io(struct r_debug_t *dbg, +R_API void r_debug_set_io(struct r_debug_t *dbg, int (*_read)(void *user, int pid, u64 addr, u8 *buf, int len), int (*_write)(void *user, int pid, u64 addr, u8 *buf, int len), void *user) diff --git a/libr/debug/reg.c b/libr/debug/reg.c index b9f11e1971..09b8dd9d21 100644 --- a/libr/debug/reg.c +++ b/libr/debug/reg.c @@ -7,15 +7,18 @@ R_API int r_debug_reg_sync(struct r_debug_t *dbg, int write) { if (write) { - dbg->h->reg_write(dbg->pid, dbg->regs); + if (dbg && dbg->h && dbg->h->reg_write) { + dbg->h->reg_write(dbg->pid, dbg->regs); + } } else { /* read registers from debugger backend to dbg->regs */ - if (dbg->h && dbg->h->reg_read) { + if (dbg && dbg->h && dbg->h->reg_read) { free(dbg->oregs); dbg->oregs = dbg->regs; - dbg->h->reg_read(dbg->regs); + dbg->regs = dbg->h->reg_read(dbg->pid); } } + return (dbg->regs != NULL); } R_API struct r_debug_regset_t *r_debug_reg_diff(struct r_debug_t *dbg) @@ -33,7 +36,7 @@ R_API u64 r_debug_reg_get(struct r_debug_t *dbg, const char *name) if (dbg->regs) for(i=0; iregs->nregs; i++) { if (!strcmp(name, dbg->regs->regs[i].name)) - return &dbg->regs->regs[i].value; + return dbg->regs->regs[i].value; } return R_TRUE; } @@ -50,7 +53,7 @@ R_API int r_debug_reg_set(struct r_debug_t *dbg, const char *name, u64 value) return R_FALSE; } -R_API int r_debug_reg_list(struct r_debug_t *dbg, struct r_debug_regset_t *rs) +R_API int r_debug_reg_list(struct r_debug_t *dbg, struct r_debug_regset_t *rs, int rad) { int i =0; if (rs == NULL) @@ -58,8 +61,10 @@ R_API int r_debug_reg_list(struct r_debug_t *dbg, struct r_debug_regset_t *rs) if (rs) for(i=0;inregs;i++) { struct r_debug_reg_t *r = &rs->regs[i]; - printf("%d %s 0x%08llx\n", i, r->name, r->value); + if (rad) dbg->printf("f %s @ 0x%08llx\n", r->name, r->value); + else dbg->printf("%d %s 0x%08llx\n", i, r->name, r->value); /* TODO: add floating point support here */ + /* TODO: add packed registers support here */ } return R_TRUE; } diff --git a/libr/include/r_debug.h b/libr/include/r_debug.h index db1c691e28..328fac38ed 100644 --- a/libr/include/r_debug.h +++ b/libr/include/r_debug.h @@ -66,6 +66,7 @@ struct r_debug_t { struct r_bp_t bp; void *user; /* io */ + void (*printf)(const char *str, ...); int (*read)(void *user, int pid, u64 addr, u8 *buf, int len); int (*write)(void *user, int pid, u64 addr, u8 *buf, int len); struct r_debug_handle_t *h; @@ -94,27 +95,27 @@ struct r_debug_pid_t { struct list_head list; }; -int r_debug_handle_add(struct r_debug_t *dbg, struct r_debug_handle_t *foo); -int r_debug_handle_set(struct r_debug_t *dbg, const char *str); -int r_debug_handle_init(struct r_debug_t *dbg); -int r_debug_init(struct r_debug_t *dbg); +R_API int r_debug_handle_add(struct r_debug_t *dbg, struct r_debug_handle_t *foo); +R_API int r_debug_handle_set(struct r_debug_t *dbg, const char *str); +R_API int r_debug_handle_init(struct r_debug_t *dbg); +R_API int r_debug_init(struct r_debug_t *dbg); // TODO: -int r_debug_set_io(struct r_debug_t *dbg, +R_API void r_debug_set_io(struct r_debug_t *dbg, int (*read)(void *user, int pid, u64 addr, u8 *buf, int len), int (*write)(void *user, int pid, u64 addr, u8 *buf, int len), void *user); /* send signals */ -int r_debug_kill(struct r_debug_t *dbg, int pid, int sig); -int r_debug_step(struct r_debug_t *dbg, int steps); -int r_debug_continue(struct r_debug_t *dbg); -int r_debug_select(struct r_debug_t *dbg, int pid, int tid); +R_API int r_debug_kill(struct r_debug_t *dbg, int pid, int sig); +R_API int r_debug_step(struct r_debug_t *dbg, int steps); +R_API int r_debug_continue(struct r_debug_t *dbg); +R_API int r_debug_select(struct r_debug_t *dbg, int pid, int tid); /* handle.c */ -int r_debug_handle_init(struct r_debug_t *dbg); -int r_debug_handle_set(struct r_debug_t *dbg, const char *str); -int r_debug_handle_list(struct r_debug_t *dbg, const char *str); -int r_debug_handle_add(struct r_debug_t *dbg, struct r_debug_handle_t *foo); +R_API int r_debug_handle_init(struct r_debug_t *dbg); +R_API int r_debug_handle_set(struct r_debug_t *dbg, const char *str); +R_API int r_debug_handle_list(struct r_debug_t *dbg, const char *str); +R_API int r_debug_handle_add(struct r_debug_t *dbg, struct r_debug_handle_t *foo); /* breakpoints */ R_API int r_debug_bp_add(struct r_debug_t *dbg, u64 addr, int size); @@ -122,6 +123,19 @@ R_API int r_debug_bp_del(struct r_debug_t *dbg, u64 addr); R_API int r_debug_bp_enable(struct r_debug_t *dbg, u64 addr, int set); R_API int r_debug_bp_disable(struct r_debug_t *dbg); R_API int r_debug_bp_list(struct r_debug_t *dbg, int rad); + +/* registers */ +R_API int r_debug_reg_sync(struct r_debug_t *dbg, int write); +R_API u64 r_debug_reg_get(struct r_debug_t *dbg, const char *name); +R_API int r_debug_reg_set(struct r_debug_t *dbg, const char *name, u64 value); +R_API struct r_debug_regset_t *r_debug_reg_diff(struct r_debug_t *dbg); +R_API int r_debug_reg_list(struct r_debug_t *dbg, struct r_debug_regset_t *rs, int rad); + +/* regset */ +R_API struct r_debug_regset_t *r_debug_regset_diff(struct r_debug_regset_t *a, struct r_debug_regset_t *b); +R_API int r_debug_regset_set(struct r_debug_regset_t *r, int idx, const char *name, u64 value); +R_API struct r_debug_regset_t *r_debug_regset_new(int size); +R_API void r_debug_regset_free(struct r_debug_regset_t *r); #if 0 Missing callbacks ================= diff --git a/libr/reg/README b/libr/reg/README index 5df8143352..f7169cb3ef 100644 --- a/libr/reg/README +++ b/libr/reg/README @@ -1,3 +1,6 @@ + +** TO BE REMOVED ** THIS MODULE IS PLANED TO DISSAPEAR ** + * Library to handle register values for multiple architectures * Architecture register sets are handled as plugins? * Designed to be used from radare libr within the modules: