* 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
This commit is contained in:
pancake 2009-04-16 20:49:18 +00:00
parent 161b7b745d
commit 7c27588c13
10 changed files with 69 additions and 52 deletions

View File

@ -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, <inmedate>
case 0xb9: // mov ecx, <inmedate>
case 0xba: // mov edx, <inmedate>
case 0xbb: // mov ebx, <inmedate>
case 0xbc: // mov esp, <inmedate>
case 0xbd: // mov esp, <inmedate>
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, <inmedate>
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, <inmedate>
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, <inmedate>
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, <inmedate>
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, <inmedate>
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, <inmedate>
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 */

View File

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

View File

@ -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", "");

View File

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

View File

@ -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;
}

View File

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

View File

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

View File

@ -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; i<dbg->regs->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;i<rs->nregs;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;
}

View File

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

View File

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