2010-02-26 20:00:03 +00:00
|
|
|
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
|
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_list.h>
|
|
|
|
#include <r_core.h>
|
|
|
|
|
2010-02-27 10:56:41 +00:00
|
|
|
static char *r_core_anal_graph_label (struct r_core_t *core, ut64 addr, ut64 size) {
|
|
|
|
char cmd[1024], *cmdstr = NULL, *str = NULL;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
snprintf (cmd, 1023, "pD %lli @ 0x%08llx", size, addr);
|
|
|
|
if ((cmdstr = r_core_cmd_str(core, cmd))) {
|
|
|
|
if (!(str = malloc(strlen(cmdstr)*2)))
|
|
|
|
return NULL;
|
|
|
|
for(i=j=0;cmdstr[i];i++,j++) {
|
|
|
|
switch(cmdstr[i]) {
|
2010-02-27 14:56:07 +00:00
|
|
|
case 0x1b:
|
2010-02-27 10:56:41 +00:00
|
|
|
/* skip ansi chars */
|
|
|
|
for(i++;cmdstr[i]&&cmdstr[i]!='m'&&cmdstr[i]!='H'&&cmdstr[i]!='J';i++);
|
|
|
|
j--;
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
str[j]='\\';
|
|
|
|
str[++j]='l';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str[j]=cmdstr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str[j]='\0';
|
|
|
|
free (cmdstr);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2010-02-27 18:12:06 +00:00
|
|
|
R_API int r_core_anal_bb (struct r_core_t *core, ut64 at, int depth) {
|
2010-03-01 15:50:37 +00:00
|
|
|
struct r_anal_bb_t *bb;
|
2010-02-27 18:12:06 +00:00
|
|
|
ut64 jump, fail;
|
2010-02-26 20:00:03 +00:00
|
|
|
ut8 *buf;
|
2010-03-01 15:50:37 +00:00
|
|
|
int ret, buflen, bblen = 0;
|
2010-02-26 20:00:03 +00:00
|
|
|
|
2010-02-27 18:12:06 +00:00
|
|
|
if (depth < 0)
|
|
|
|
return R_FALSE;
|
2010-02-26 20:00:03 +00:00
|
|
|
if (!(bb = r_anal_bb_new()))
|
|
|
|
return R_FALSE;
|
2010-03-01 15:50:37 +00:00
|
|
|
ret = r_anal_bb_split (&core->anal, bb, core->anal.bbs, at);
|
|
|
|
if (ret == R_ANAL_RET_DUP) { /* Dupped bb */
|
|
|
|
r_anal_bb_free (bb);
|
|
|
|
return R_FALSE;
|
|
|
|
} else if (ret == R_ANAL_RET_NEW) { /* New bb */
|
2010-02-27 14:56:07 +00:00
|
|
|
if (!(buf = malloc (core->blocksize)))
|
|
|
|
return R_FALSE;
|
2010-02-28 19:07:36 +00:00
|
|
|
do {
|
2010-03-01 15:50:37 +00:00
|
|
|
if ((buflen = r_io_read_at (&core->io, at+bblen, buf, core->blocksize)) == -1)
|
2010-02-28 19:07:36 +00:00
|
|
|
return R_FALSE;
|
2010-03-01 15:50:37 +00:00
|
|
|
bblen = r_anal_bb (&core->anal, bb, at+bblen, buf, buflen);
|
|
|
|
if (bblen == R_ANAL_RET_ERROR) { /* Error analyzing bb */
|
2010-02-28 19:07:36 +00:00
|
|
|
r_anal_bb_free (bb);
|
|
|
|
return R_FALSE;
|
2010-03-01 15:50:37 +00:00
|
|
|
} else if (bblen == R_ANAL_RET_END) { /* bb analysis complete */
|
|
|
|
if (r_anal_bb_overlap (&core->anal, bb, core->anal.bbs) == R_ANAL_RET_NEW) {
|
|
|
|
r_list_append (core->anal.bbs, bb);
|
|
|
|
fail = bb->fail;
|
|
|
|
jump = bb->jump;
|
|
|
|
if (fail != -1)
|
|
|
|
r_core_anal_bb (core, fail, depth-1);
|
|
|
|
if (jump != -1)
|
|
|
|
r_core_anal_bb (core, jump, depth-1);
|
|
|
|
}
|
2010-02-28 19:07:36 +00:00
|
|
|
}
|
2010-03-01 15:50:37 +00:00
|
|
|
} while (bblen != R_ANAL_RET_END);
|
2010-02-27 14:56:07 +00:00
|
|
|
free (buf);
|
2010-02-26 20:00:03 +00:00
|
|
|
}
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
2010-02-27 10:56:41 +00:00
|
|
|
|
|
|
|
R_API int r_core_anal_graph (struct r_core_t *core) {
|
|
|
|
struct r_anal_bb_t *bbi;
|
|
|
|
RListIter *iter;
|
|
|
|
char *str;
|
2010-02-27 18:12:06 +00:00
|
|
|
int reflines = r_config_get_i(&core->config, "asm.reflines");
|
2010-03-01 00:57:16 +00:00
|
|
|
int bytes = r_config_get_i(&core->config, "asm.bytes");
|
2010-02-27 10:56:41 +00:00
|
|
|
|
|
|
|
r_config_set_i(&core->config, "asm.reflines", 0);
|
2010-03-01 00:57:16 +00:00
|
|
|
r_config_set_i(&core->config, "asm.bytes", 0);
|
2010-02-27 10:56:41 +00:00
|
|
|
r_cons_printf ("digraph code {\n");
|
|
|
|
r_cons_printf ("\tgraph [bgcolor=white];\n");
|
|
|
|
r_cons_printf ("\tnode [color=lightgray, style=filled shape=box fontname=\"Courier\" fontsize=\"8\"];\n");
|
|
|
|
iter = r_list_iterator (core->anal.bbs);
|
|
|
|
while (r_list_iter_next (iter)) {
|
|
|
|
bbi = r_list_iter_get (iter);
|
|
|
|
if (bbi->jump != -1)
|
|
|
|
r_cons_printf ("\t\"0x%08llx\" -> \"0x%08llx\" [color=\"green\"];\n", bbi->addr, bbi->jump);
|
|
|
|
if (bbi->fail != -1)
|
|
|
|
r_cons_printf ("\t\"0x%08llx\" -> \"0x%08llx\" [color=\"red\"];\n", bbi->addr, bbi->fail);
|
|
|
|
r_cons_flush ();
|
|
|
|
if ((str = r_core_anal_graph_label (core, bbi->addr, bbi->size))) {
|
|
|
|
r_cons_printf (" \"0x%08llx\" [label=\"%s\"]\n", bbi->addr, str);
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_cons_printf ("}\n");
|
|
|
|
r_config_set_i(&core->config, "asm.reflines", reflines);
|
2010-03-01 00:57:16 +00:00
|
|
|
r_config_set_i(&core->config, "asm.bytes", bytes);
|
2010-02-27 10:56:41 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|