Fix pd -x and pD -x : It can be heavily reworked.

This commit is contained in:
pancake 2015-04-03 02:23:14 +02:00
parent 570b60104f
commit dbe0972610
3 changed files with 73 additions and 52 deletions

View File

@ -390,7 +390,8 @@ R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) {
r_asm_set_pc (a, a->pc + ret);
ret = r_asm_disassemble (a, &op, buf+idx, len-idx);
if (ret<1) {
eprintf ("disassemble error at offset %"PFMT64d"\n", idx);
// TODO: this warning is sometimes useful
// eprintf ("disassemble error at offset %"PFMT64d"\n", idx);
//ret = 1;
ret = 1;
//acode->buf_asm[0] = 0;

View File

@ -351,7 +351,9 @@ static int is_addr_in_range(ut64 start, ut64 end, ut64 start_range, ut64 end_ran
static int is_hit_inrange(RCoreAsmHit *hit, ut64 start_range, ut64 end_range){
int result = R_FALSE;
if (hit) {
result = is_addr_in_range(hit->addr, hit->addr + hit->len, start_range, end_range);
result = is_addr_in_range (hit->addr,
hit->addr + hit->len,
start_range, end_range);
}
return result;
}
@ -359,66 +361,87 @@ static int is_hit_inrange(RCoreAsmHit *hit, ut64 start_range, ut64 end_range){
R_API RList *r_core_asm_bwdisassemble (RCore *core, ut64 addr, int n, int len) {
RList *hits = r_core_asm_hit_list_new();
RAsmOp op;
// len = n * 32;
// if (n > core->blocksize) n = core->blocksize;
ut8 *buf = (ut8 *)malloc(len);
ut64 instrlen = 0, at = 0;
ut32 idx = 0, hit_count = 0;
int numinstr, asmlen, ii;
ut8 *buf;
ut64 buf_addr, instrlen = 0, at = 0;
ut32 idx = 0, hit_count = 0, buf_len = 0;
int numinstr, ii;
RAsmCode *c;
if (hits == NULL || buf == NULL ){
if (hits) {
r_list_purge (hits);
free (hits);
}
if (!hits)
return NULL;
buf_addr = addr - len;
buf_len = len;
buf = (ut8 *)malloc (buf_len);
if (!buf) {
r_list_free (hits);
return NULL;
}
if (r_io_read_at (core->io, buf_addr, buf, buf_len) != buf_len) {
r_list_free (hits);
free (buf);
return NULL;
}
if (!memcmp (buf, "\xff\xff\xff\xff", R_MIN (4, buf_len))) {
eprintf ("error reading at 0x%08"PFMT64x"\n", buf_addr);
r_list_free (hits);
free (buf);
return NULL;
}
if (r_io_read_at (core->io, addr-len, buf, len) != len) {
if (hits) {
r_list_purge (hits);
free (hits);
}
free (buf);
return NULL;
}
if (n<0) n = -n;
for (idx = 1; idx < len; ++idx) {
if (r_cons_singleton ()->breaked) break;
at = addr - idx; hit_count = 0;
c = r_asm_mdisassemble (core->assembler, buf+(len-idx), idx);
if (strstr(c->buf_asm, "invalid") || strstr(c->buf_asm, ".byte")) {
r_asm_code_free(c);
for (idx = 1; idx < len; idx++) {
if (r_cons_singleton ()->breaked)
break;
at = addr - idx;
hit_count = 0;
r_asm_set_pc (core->assembler, at);
// XXX: the disassemble errors are because of this line. mdisasm must not be used here
//c = r_asm_mdisassemble (core->assembler, buf+idx, buf_len-idx); //+buf_len-idx, idx);
c = r_asm_mdisassemble (core->assembler, buf+buf_len-idx, idx);
// XXX: relaying on string contents in the buf_asm is a bad idea
if (strstr (c->buf_asm, "invalid") || strstr (c->buf_asm, ".byte")) {
r_asm_code_free (c);
continue;
}
numinstr = 0;
asmlen = strlen(c->buf_asm);
for(ii = 0; ii < asmlen; ++ii) {
if (c->buf_asm[ii] == '\n') ++numinstr;
//eprintf ("-->(%x)(%s)\n", at, c->buf_asm);
for (numinstr = ii = 0; c->buf_asm[ii] ; ii++) {
if (c->buf_asm[ii] == '\n')
numinstr++;
}
r_asm_code_free(c);
//eprintf ("mdisasm worked! for 0x%llx with %d\n", addr-len+idx, numinstr);
r_asm_code_free (c);
if (numinstr >= n || idx > 32 * n) {
//eprintf ("idx = %d len = %d ninst = %d n = %d\n", idx, len, numinstr, n);
break;
}
//eprintf ("idx = %d len = %d\n", idx, len);
}
at = addr - idx;
hit_count = 0;
r_asm_set_pc (core->assembler, at);
at = addr-idx;
for ( hit_count = 0; hit_count < n; hit_count++) {
instrlen = r_asm_disassemble (core->assembler, &op, buf+(len-(addr-at)), addr-at);
add_hit_to_hits(hits, at, instrlen, R_TRUE);
for (hit_count = 0; hit_count < n; hit_count++) {
if (r_cons_singleton ()->breaked)
break;
r_asm_set_pc (core->assembler, at);
instrlen = r_asm_disassemble (core->assembler,
&op, buf+buf_len-(addr-at), addr-at); //addr-at);
// eprintf ("INST LEN = %d\n", instrlen);
if (instrlen<1) {
eprintf ("dissasm failed at %llx\n", at);
instrlen = 1;
// break;
}
add_hit_to_hits (hits, at, instrlen, R_TRUE);
at += instrlen;
}
r_asm_set_pc (core->assembler, addr);
free (buf);
return hits;
}
static RList * r_core_asm_back_disassemble_all(RCore *core, ut64 addr, ut64 len, ut64 max_hit_count, ut32 extra_padding){
RList *hits = r_core_asm_hit_list_new ();
RCoreAsmHit dummy_value;
@ -612,7 +635,7 @@ static RList *r_core_asm_back_disassemble (RCore *core, ut64 addr, int len, ut64
if ( hit_count >= max_hit_count &&
(last_num_invalid >= max_invalid_b4_exit || last_num_invalid == 0))
break;
} while ( ((int) current_buf_pos >= 0) && (int)(len - current_buf_pos) >= 0 );
} while (((int) current_buf_pos >= 0) && (int)(len - current_buf_pos) >= 0);
r_asm_set_pc (core->assembler, addr);
free (buf);
@ -622,13 +645,13 @@ static RList *r_core_asm_back_disassemble (RCore *core, ut64 addr, int len, ut64
R_API RList *r_core_asm_back_disassemble_instr (RCore *core, ut64 addr, int len, ut32 hit_count, ut32 extra_padding){
// extra padding to allow for additional disassembly on border buffer cases
ut8 disassmble_each_addr = R_FALSE;
return r_core_asm_back_disassemble(core, addr, len, hit_count, disassmble_each_addr, extra_padding);
return r_core_asm_back_disassemble (core, addr, len, hit_count, disassmble_each_addr, extra_padding);
}
R_API RList *r_core_asm_back_disassemble_byte (RCore *core, ut64 addr, int len, ut32 hit_count, ut32 extra_padding){
// extra padding to allow for additional disassembly on border buffer cases
ut8 disassmble_each_addr = R_TRUE;
return r_core_asm_back_disassemble(core, addr, len, hit_count, disassmble_each_addr, extra_padding);
ut8 disassmble_each_addr = R_TRUE;
return r_core_asm_back_disassemble (core, addr, len, hit_count, disassmble_each_addr, extra_padding);
}
/* Compute the len and the starting address
@ -641,18 +664,14 @@ R_API ut32 r_core_asm_bwdis_len (RCore* core, int* instr_len, ut64* start_addr,
if (instr_len)
*instr_len = 0;
if (hits && r_list_length (hits) > 0) {
hit = r_list_get_bottom(hits);
hit = r_list_get_bottom (hits);
if (start_addr)
*start_addr = hit->addr;
r_list_foreach (hits, iter, hit)
instr_run += hit->len;
if (instr_len)
*instr_len = instr_run;
}
r_list_free (hits);
return instr_run;
}

View File

@ -1955,12 +1955,13 @@ static int cmd_print(void *data, const char *input) {
if (bw_disassemble) {
block = malloc (core->blocksize);
l = -l;
if (l<0)
l = -l;
if (block) {
if (*input == 'D'){ //pD
r_core_read_at (core, addr-l, block, core->blocksize);
r_core_read_at (core, addr-l, block, l); //core->blocksize);
core->num->value = r_core_print_disasm (core->print,
core, addr-l, block, R_MIN (l, core->blocksize), l, 0, 1);
core, addr-l, block, l, l, 0, 1);
} else { //pd
const int bs = core->blocksize;
int instr_len;