fix #1731 and #1733. Remove all workarounds for the agj non structured output, and fixes some minor bugs

This commit is contained in:
Alvaro Muñoz 2014-12-05 13:29:33 +01:00 committed by pancake
parent 9638759289
commit cbdf4d051b
9 changed files with 142 additions and 238 deletions

View File

@ -386,10 +386,14 @@ static void r_core_anal_graph_nodes(RCore *core, RAnalFunction *fcn, int opts) {
r_cons_printf (",\"jump\":%"PFMT64d, bbi->jump);
if (bbi->fail != -1)
r_cons_printf (",\"fail\":%"PFMT64d, bbi->fail);
if ((str = r_core_anal_graph_label (core, bbi, opts))) {
str = r_str_replace (str, "\\ ", "\\\\ ", 1);
r_cons_printf (",\"code\":\"%s\"", str);
free (str);
r_cons_printf (",\"ops\":");
{
ut8 *buf = malloc (bbi->size);
if (buf) {
r_io_read_at (core->io, bbi->addr, buf, bbi->size);
r_core_print_disasm_json (core, bbi->addr, buf, bbi->size, 0);
free (buf);
} else eprintf ("cannot allocate %d bytes\n", bbi->size);
}
r_cons_printf ("}");
continue;

View File

@ -1600,7 +1600,7 @@ static void handle_print_ptr (RCore *core, RDisasmState *ds, int len, int idx) {
}
r_mem_copyendian ((ut8*)&n, (ut8*)&n, ds->analop.refptr, !core->assembler->big_endian);
n32 = n;
handle_comment_align (core, ds);
if (ds->show_color) {
r_cons_printf (ds->pal_comment);
@ -1723,7 +1723,7 @@ addr addr+size
static void handle_print_relocs (RCore *core, RDisasmState *ds) {
RBinReloc *rel = getreloc (core, ds->at, ds->analop.size);
if (rel) {
if (rel->import)
if (rel->import)
r_cons_printf (" ; RELOC %d %s", rel->type, rel->import->name);
else if (rel->symbol)
r_cons_printf (" ; RELOC %d %s", rel->type, rel->symbol->name);
@ -2281,9 +2281,21 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte
}
r_anal_op (core->anal, &analop, at, buf+i, nb_bytes-i);
RDisasmState *ds;
ds = handle_init_ds (core);
if (ds->pseudo) r_parse_parse (core->parser, asmop.buf_asm, asmop.buf_asm);
RAnalFunction *f = r_anal_get_fcn_in (core->anal, at, R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM);
oplen = r_asm_op_get_size (&asmop);
r_cons_printf (i>0? ",{": "{");
r_cons_printf ("\"offset\":%"PFMT64d, at);
if (f) {
r_cons_printf (",\"fcn_addr\":%"PFMT64d, f->addr);
r_cons_printf (",\"fcn_last\":%"PFMT64d, f->addr + f->size - oplen);
} else {
r_cons_printf (",\"fcn_addr\":0");
r_cons_printf (",\"fcn_last\":0");
}
r_cons_printf (",\"size\":%d", oplen);
escaped_str = r_str_escape(asmop.buf_asm);
r_cons_printf (",\"opcode\":\"%s\"", escaped_str);
@ -2351,7 +2363,7 @@ R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int nb_byte
r_cons_printf (",\"xrefs\":[");
r_list_foreach (xrefs, iter, ref) {
r_cons_printf ("%s{\"addr\":%"PFMT64d",\"type\":\"%s\"}",
iter->p?",":"", ref->addr,
iter->p?",":"", ref->addr,
r_anal_xrefs_type_tostring (ref->type));
}
r_cons_printf ("]");

File diff suppressed because one or more lines are too long

View File

@ -116,7 +116,7 @@ function render_graph(x) {
} catch (e) {
console.log("Cannot parse JSON data");
}
// try {
try {
if (obj[0] === undefined) return false;
if (obj[0].blocks === undefined) return false;
var graph = new BBGraph();
@ -124,31 +124,16 @@ function render_graph(x) {
var bb = obj[0].blocks[bn];
if (bb.length === 0) continue;
var addr = bb.offset;
var lines = bb.code.split('\n');
var cnt = lines.length;
var cnt = bb.ops.length;
var idump = "";
for (var i = 0; i < cnt; i++) {
var line = lines[i];
if (line !== "") {
// Prepare an instruction object
var ins = {};
ins.offset = line.split(' ')[0];
if (ins.offset.indexOf("0x") !== 0) continue;
ins.comment = "";
ins.bytes = "";
ins.type = "mov";
var opcode_idx = line.indexOf(' ');
var colon_idx = line.indexOf(';');
if (colon_idx > 0) {
ins.opcode = line.substring(opcode_idx,colon_idx).trim();
ins.comment = line.substring(colon_idx + 1).trim();
} else {
ins.opcode = line.substring(opcode_idx).trim();
}
if (ins.opcode === "") continue;
idump += html_for_instruction(ins);
for (var i in bb.ops) {
var ins = bb.ops[i];
ins.offset = "0x" + ins.offset.toString(16);
if (ins.comment === undefined || ins.comment === null) ins.comment = "";
else {
ins.comment = atob(ins.comment);
}
idump += html_for_instruction(ins);
}
var dom = document.createElement('div');
dom.id = "bb_" + addr;
@ -166,9 +151,10 @@ function render_graph(x) {
}
graph.render();
return true;
// } catch (e) {
// console.log("Error generating bb graph");
// }
} catch (e) {
console.log("Error generating bb graph");
return false;
}
}
function render_instructions(instructions) {
@ -333,17 +319,9 @@ function render_instructions(instructions) {
function getOffsetRect(elem) {
var box = elem.getBoundingClientRect();
var bar = document.getElementById("radareApp_mp_toolbar");
var barrect = bar.getBoundingClientRect();
var body = document.body;
var docElem = document.documentElement;
var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
var clientTop = docElem.clientTop || body.clientTop || 0;
var top = box.top - barrect.bottom - 10; //+ scrollTop - clientTop;
var bottom = box.bottom - barrect.bottom - 10;//+ scrollTop - clientTop;
var offset = $('#gbox').offset().top;
var top = box.top - offset;
var bottom = box.bottom - offset;
return {top: Math.round(top), bottom: Math.round(bottom)};
}
@ -371,6 +349,9 @@ function html_for_instruction(ins) {
var asm_xrefs = (r2.settings["asm.xrefs"]);
var asm_cmtright = (r2.settings["asm.cmtright"]);
if (ins.offset === "0x"+ins.fcn_addr.toString(16) && r2ui._dis.display == "flat") {
idump += '<div class="ec_flow">; -----------------------------------------------------------</div>';
}
if (asm_flags) {
var flags;
if (ins.flags !== undefined && ins.flags !== null) {
@ -400,7 +381,6 @@ function html_for_instruction(ins) {
idump += '<span class="insaddr datainstruction ec_offset addr addr_' + address_canonicalize(ins.offset) + '">' + address + '</span> ';
if (asm_bytes) {
if (ins.bytes !== undefined && ins.bytes !== null && ins.bytes !== "") {
var dorep = function(a) {
@ -434,12 +414,7 @@ function html_for_instruction(ins) {
}
var math = ["add", "sub", "mul", "imul", "div", "idiv", "neg", "adc", "sbb", "inc", "dec", ".byte"];
var invalid = [".byte", "insb", "outsd"];// this is just as a workaround for the agj problem
var bin = ["xor", "and", "or", "not"];
var jmps = ["jmp"];
var cjmps = ["je", "jne", "jg", "jge", "jl", "jle", "ja", "jae", "jb", "jbe", "jo", "jno", "jc", "jnc", "js", "jns", "jz", "jnz"];
var calls = ["call"];
var movs = ["mov", "lea"];
var regs = ["EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI", "EIP", "RAX", "RCX", "RDX", "RBX", "RSP", "RBP", "RSI", "RDI", "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", "RIP"];
var escapeHTML = (function () {
@ -501,39 +476,7 @@ function highlight_instruction(line, instruction) {
return reps[a];
}
}
ret = ret.replace(new RegExp(re, "g"), dorep);
// highlight opcode
if (instruction) {
var j = 0;
for (j = 0; j < ret.length; j++) {
if (ret[j] == ' ' || ret[j] == '\t') {
break;
}
}
var op = ret.substr(0, j);
var rest = ret.substr(j);
// WORKAROUND until agj returns structured data:
var klass = "";
if (contains(math, op)) klass = "ec_math";
if (contains(bin, op)) klass = "ec_bin";
if (contains(invalid, op)) klass = "invalid";
if (contains(jmps, op)) klass = "ec_jmp";
if (contains(cjmps, op)) klass = "ec_cjmp";
if (contains(calls, op)) klass = "ec_call";
if (contains(movs, op)) klass = "ec_mov";
if (op == "push") klass = "ec_push";
if (op == "pop") klass = "ec_pop";
if (op == "nop") klass = "ec_nop";
if (op == "ret") klass = "ec_ret";
if (op == "cmp") klass = "ec_cmp";
// WORKAROUND
ret = '<span class="op ' + klass + '">' + op + '</span>' + rest;
}
return ret;
return ret.replace(new RegExp(re, "g"), dorep);
}
function hex2(a) {
@ -576,14 +519,8 @@ function get_address_from_class(t, type) {
}
function rehighlight_iaddress(address) {
var elements = document.getElementsByClassName("autohighlighti");
for (var i = 0; i < elements.length; i++) {
removeClass("autohighlighti", elements[i]);
}
elements = document.getElementsByClassName("addr_" + address);
for (var j = 0; j < elements.length; j++) {
addClass("autohighlighti", elements[j]);
}
$('.autohighlighti').removeClass('autohighlighti');
$('.addr_' + address).addClass('autohighlighti');
}
function get_element_by_address(address) {
@ -611,23 +548,6 @@ function scroll_to_element(element) {
r2ui._dis.scrollTo(0,top);
}
function addClass( classname, element ) {
var cn = element.className;
if( cn.indexOf( classname ) != -1 ) {
return;
}
if( cn !== '' ) {
classname = ' '+classname;
}
element.className = cn+classname;
}
function removeClass( classname, element ) {
var cn = element.className;
var rxp = new RegExp( "\\s?\\b"+classname+"\\b", "g" );
cn = cn.replace( rxp, '' );
element.className = cn;
}
function address_canonicalize(s) {
s = s.substr(2);
@ -650,6 +570,63 @@ function handleInputTextChange() {
r2ui._dis.handleInputTextChange();
}
function get_offset_flag(offset) {
var old_value = "";
r2.cmdj("fs offsets;fj", function(x) {
for (var i in x) {
if ("0x" + x[i].offset.toString(16) == offset) {
old_value = x[i].name;
break;
}
}
});
return old_value;
}
function get_symbol_flag(symbol) {
var full_name = symbol;
var found = false;
r2.cmdj("fs symbols;fj", function(x) {
for (var i in x) {
if (x[i].name == symbol) {
found = true;
break;
}
}
if (!found) {
for (var i in x) {
if (x[i].name == "sym." + symbol) {
full_name = "sym." + symbol;
break;
}
}
}
});
return full_name;
}
function get_reloc_flag(reloc) {
var full_name = reloc;
var found = false;
r2.cmdj("fs relocs;fj", function(x) {
for (var i in x) {
if (x[i].name == reloc) {
found = true;
break;
}
}
if (!found) {
for (var i in x) {
if (x[i].name == "reloc." + reloc) {
full_name = "reloc." + reloc;
break;
}
}
}
});
return full_name;
}
// Cookies
function createCookie(name,value,days) {
@ -675,4 +652,4 @@ function readCookie(name) {
function eraseCookie(name) {
createCookie(name,"",-1);
}
}

View File

@ -127,6 +127,7 @@ enyo.kind ({
// n Rename
if (key === 110) {
if (this.renaming === null && this.selected !== null && (this.selected.className.indexOf(" addr ") ) -1) {
var address = get_address_from_class(this.selected);
this.renaming = this.selected;
this.renameOldValue = this.selected.innerHTML;
this.rbox = document.createElement('input');
@ -134,7 +135,14 @@ enyo.kind ({
this.rbox.setAttribute("id", "rename");
this.rbox.setAttribute("style", "border-width: 0;padding: 0;");
this.rbox.setAttribute("onChange", "handleInputTextChange()");
this.rbox.setAttribute("value", "");
if (this.selected.className.indexOf("insaddr") > -1) {
var value = get_offset_flag(address);
this.rbox.setAttribute("value",value);
this.rbox.setSelectionRange(value.length, value.length);
} else {
this.rbox.setAttribute("value", this.renameOldValue);
this.rbox.setSelectionRange(this.renameOldValue.length, this.renameOldValue.length);
}
this.renaming.innerHTML = "";
this.renaming.appendChild(this.rbox);
this.rbox.focus();
@ -237,10 +245,6 @@ enyo.kind ({
});
if (new_value) {
var cmd = "fs functions;f-@" + this.selected_offset + ";f+" + new_value + "@" + this.selected_offset + ";";
// labels = new_value.split(";");
// for (var i in labels) {
// if (labels[i] !== "") cmd += "f+" + labels[i] + "@$$;";
// }
r2.cmd(cmd, function() {});
} else {
r2.cmd("f-@" + this.selected_offset, function() {});
@ -299,26 +303,21 @@ enyo.kind ({
},
seek: function(addr, scroll) {
var text = this.$.text;
var error = false;
if (this.display === "graph") {
var display = "graph";
text.setContent("");
r2.store_asm_config();
r2.cmd("e asm.bytes = false; e asm.flags = false; e asm.functions = false; e asm.lines = false; e asm.xrefs = false; e asm.cmtright = true; e asm.pseudo = false", function (x) {
r2.cmd ("agj " + addr, function(x) {
text.setContent("<div id='bb_canvas' class='bbcanvas enyo-selectable ec_background'></div>");
// If render fails (address does not belong to function) then switch to flat view
if (render_graph(x) === false) display = "flat";
});
r2.cmd ("agj " + addr, function(x) {
text.setContent("<div id='bb_canvas' class='bbcanvas enyo-selectable ec_background'></div>");
// If render fails (address does not belong to function) then switch to flat view
if (render_graph(x) === false) error = true;
});
this.display = display;
r2.restore_asm_config();
}
else if (this.display === "flat") {
if (error) this.display_flat();
if (this.display === "flat") {
this.min = this.max = 0;
r2.get_disasm_before_after(addr, -0.5*this.block, this.block, function(x) {
text.setContent("<div id='flat_canvas' class='flatcanvas enyo-selectable ec_background'></div>");
render_instructions(x);
// text.setContent(x);
});
}
this.selected = get_element_by_address(addr);

View File

@ -120,10 +120,9 @@ r2ui.seek = function (addr, push, scroll) {
r2.cmd ("s " + addr, function () {
r2ui._dis.seek(addr, scroll);
//r2ui._dis.scrollTo (0, 0);
r2ui._hex.seek(addr, scroll);
// r2ui._hex.scrollTo(0, 0);
});
return addr;
}
r2ui.seek_in_graph = function (addr, push) {

View File

@ -5,7 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title>Radare2</title>
<title>radare2</title>
<link rel="stylesheet" type="text/css" href="./lib/css/layout-default-latest.css" />
<link rel="stylesheet" type="text/css" href="./lib/css/jquery.ui.tabs.css" />
@ -37,8 +37,8 @@
<body>
<!-- NORTH -->
<div id="radareApp_mp_toolbar" class="ui-layout-north ui-widget-content" style="display: none;background-color:rgb(20,20,20);color: white;text-align: center">
radare 2
<div id="radareApp_mp_toolbar" class="ui-layout-north ui-widget-content" style="display: none;background-color:rgb(20,20,20);color: white;text-align: center;font-family: monospace;">
radare2
</div>
<!-- SOUTH -->

View File

@ -15,22 +15,17 @@ var DisasmPanel = function () {
};
DisasmPanel.prototype.seek = function(addr, scroll) {
var panel = this.panel;
var error = false;
if (this.display === "graph") {
var display = "graph";
panel.innerHTML = "";
r2.store_asm_config();
r2.cmd("e asm.bytes = false; e asm.flags = false; e asm.functions = false; e asm.lines = false; e asm.xrefs = false; e asm.cmtright = true; e asm.pseudo = false", function (x) {
r2.cmd ("agj " + addr, function(x) {
panel.innerHTML = "<div id='bb_canvas' class='bbcanvas enyo-selectable ec_background'></div>";
// If render fails (address does not belong to function) then switch to flat view
if (render_graph(x) === false) display = "flat";
});
r2.cmd("agj " + addr, function(x) {
panel.innerHTML = "<div id='bb_canvas' class='bbcanvas enyo-selectable ec_background'></div>";
// If render fails (address does not belong to function) then switch to flat view
if (render_graph(x) === false) error = true;
});
this.display = display;
r2.restore_asm_config();
}
else if (this.display === "flat") {
if (error) this.display_flat();
if (this.display === "flat") {
this.min = this.max = 0;
r2.get_disasm_before_after(addr, -0.5*this.block, this.block, function(x) {
panel.innerHTML = "<div id='flat_canvas' class='flatcanvas enyo-selectable ec_background'></div>";

View File

@ -190,19 +190,6 @@ function scroll_to_last_offset() {
if (r2ui._dis.scroll_offset !== null) $('#center_panel').scrollTo(r2ui._dis.scroll_offset, {axis: 'y'});
}
function rehighlight_iaddress(address) {
$('.autohighlighti').removeClass('autohighlighti');
$('.addr_' + address).addClass('autohighlighti');
}
function getOffsetRect(elem) {
var box = elem.getBoundingClientRect();
var offset = $('#gbox').offset().top;
var top = box.top - offset;
var bottom = box.bottom - offset;
return {top: Math.round(top), bottom: Math.round(bottom)};
}
// key handler
function handleKeypress(inEvent) {
var key = inEvent.keyCode || inEvent.charCode || inEvent.which || 0;
@ -221,7 +208,7 @@ function handleKeypress(inEvent) {
if (address !== undefined && address !== null) {
if (r2ui._dis.display === "flat") r2ui._dis.display_graph();
else if (r2ui._dis.display === "graph") r2ui._dis.display_flat();
r2ui.seek(address, true);
address = r2ui.seek(address, true);
scroll_to_address(address);
inEvent.preventDefault();
document.getElementById("canvas").focus();
@ -300,7 +287,7 @@ function handleKeypress(inEvent) {
if (key === 117) do_undefine(r2ui._dis.selected);
// g Go to address
if (key === 103) do_goto();
if (key === 103) do_jumpto(prompt('Go to'));
// ; Add comment
if (key === 59) do_comment(r2ui._dis.selected);
@ -334,23 +321,17 @@ function do_jumpto(address) {
var element = $('.insaddr.addr_' + address);
if (element.length > 0) {
r2ui.history_push(address);
render_history();
r2ui._dis.selected = element;
r2ui._dis.selected_offset = address;
render_history();
} else {
r2ui.seek(address, true);
address = r2ui.seek(address, true);
}
rehighlight_iaddress(address);
scroll_to_address(address);
document.getElementById("canvas").focus();
}
function do_goto() {
r2ui.opendis(prompt('Go to'));
scroll_to_element(r2ui._dis.selected);
document.getElementById("canvas").focus();
}
function do_rename(element, inEvent) {
var address = get_address_from_class(element);
if ($(element).hasClass("addr") && $(element).hasClass("flag")) {
@ -457,63 +438,6 @@ function rename(offset, old_value, new_value, space) {
r2.update_flags();
}
function get_offset_flag(offset) {
var old_value = "";
r2.cmdj("fs offsets;fj", function(x) {
for (var i in x) {
if ("0x" + x[i].offset.toString(16) == offset) {
old_value = x[i].name;
break;
}
}
});
return old_value;
}
function get_symbol_flag(symbol) {
var full_name = symbol;
var found = false;
r2.cmdj("fs symbols;fj", function(x) {
for (var i in x) {
if (x[i].name == symbol) {
found = true;
break;
}
}
if (!found) {
for (var i in x) {
if (x[i].name == "sym." + symbol) {
full_name = "sym." + symbol;
break;
}
}
}
});
return full_name;
}
function get_reloc_flag(reloc) {
var full_name = reloc;
var found = false;
r2.cmdj("fs relocs;fj", function(x) {
for (var i in x) {
if (x[i].name == reloc) {
found = true;
break;
}
}
if (!found) {
for (var i in x) {
if (x[i].name == "reloc." + reloc) {
full_name = "reloc." + reloc;
break;
}
}
}
});
return full_name;
}
function handleClick(inEvent) {
if ($(inEvent.target).hasClass('addr')) {
var address = get_address_from_class(inEvent.target);
@ -538,10 +462,7 @@ function handleDoubleClick (inEvent) {
document.getElementById("canvas").focus();
}
// METHOD to UPDATE all LATERAL Information
function update_binary_details() {
// <div id="symbols"></div>
r2.cmdj("isj", function(x) {
render_symbols(x);
@ -574,15 +495,12 @@ function update_binary_details() {
render_history();
}
function render_functions(functions) {
// TODO: Sometimes undefined is printed
var imports = null;
r2.cmdj("iij", function(x) {
imports = x;
});
var fcn_data = [];
for (var i in functions) {
var f = functions[i];