Skip to content

Instantly share code, notes, and snippets.

@hackcasual
Created March 8, 2016 02:15
Show Gist options
  • Save hackcasual/efb93d70c4c3e87cd1ee to your computer and use it in GitHub Desktop.
Save hackcasual/efb93d70c4c3e87cd1ee to your computer and use it in GitHub Desktop.
if (typeof(document) !== "undefined") {
$.ready(function(){
var saveHeap = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (fileName) {
var blob = new Blob([Module.HEAPU8], {type: 'application/octet-binary'}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
});
};
var Debugger = (function() {
var BASIC_TYPE = 0,
DERIVED_TYPE = 1,
COMPOSITE_TYPE = 2,
SUBROUTINE_TYPE = 3,
SUBRANGE_INFO = 4,
SUBPROGRAM_TYPE = 5,
ENUMERATOR_TYPE = 6,
STRING_REFERENCE = 10;
var DW_TAG_array_type = 0x01,
DW_TAG_class_type = 0x02,
DW_TAG_entry_point = 0x03,
DW_TAG_enumeration_type = 0x04,
DW_TAG_formal_parameter = 0x05,
DW_TAG_imported_declaration = 0x08,
DW_TAG_label = 0x0a,
DW_TAG_lexical_block = 0x0b,
DW_TAG_member = 0x0d,
DW_TAG_pointer_type = 0x0f,
DW_TAG_reference_type = 0x10,
DW_TAG_compile_unit = 0x11,
DW_TAG_string_type = 0x12,
DW_TAG_structure_type = 0x13,
DW_TAG_subroutine_type = 0x15,
DW_TAG_typedef = 0x16,
DW_TAG_union_type = 0x17,
DW_TAG_unspecified_parameters = 0x18,
DW_TAG_variant = 0x19,
DW_TAG_common_block = 0x1a,
DW_TAG_common_inclusion = 0x1b,
DW_TAG_inheritance = 0x1c,
DW_TAG_inlined_subroutine = 0x1d,
DW_TAG_module = 0x1e,
DW_TAG_ptr_to_member_type = 0x1f,
DW_TAG_set_type = 0x20,
DW_TAG_subrange_type = 0x21,
DW_TAG_with_stmt = 0x22,
DW_TAG_access_declaration = 0x23,
DW_TAG_base_type = 0x24,
DW_TAG_catch_block = 0x25,
DW_TAG_const_type = 0x26,
DW_TAG_constant = 0x27,
DW_TAG_enumerator = 0x28,
DW_TAG_file_type = 0x29,
DW_TAG_friend = 0x2a,
DW_TAG_namelist = 0x2b,
DW_TAG_namelist_item = 0x2c,
DW_TAG_packed_type = 0x2d,
DW_TAG_subprogram = 0x2e,
DW_TAG_template_type_parameter = 0x2f,
DW_TAG_template_value_parameter = 0x30,
DW_TAG_thrown_type = 0x31,
DW_TAG_try_block = 0x32,
DW_TAG_variant_part = 0x33,
DW_TAG_variable = 0x34,
DW_TAG_volatile_type = 0x35,
DW_TAG_dwarf_procedure = 0x36,
DW_TAG_restrict_type = 0x37,
DW_TAG_interface_type = 0x38,
DW_TAG_namespace = 0x39,
DW_TAG_imported_module = 0x3a,
DW_TAG_unspecified_type = 0x3b,
DW_TAG_partial_unit = 0x3c,
DW_TAG_imported_unit = 0x3d,
DW_TAG_condition = 0x3f,
DW_TAG_shared_type = 0x40,
DW_TAG_lo_user = 0x4080,
DW_TAG_hi_user = 0xffff;
var DW_ATE_address = 0x01,
DW_ATE_boolean = 0x02,
DW_ATE_complex_float = 0x03,
DW_ATE_float = 0x04,
DW_ATE_signed = 0x05,
DW_ATE_signed_char = 0x06,
DW_ATE_unsigned = 0x07,
DW_ATE_unsigned_char = 0x08,
DW_ATE_imaginary_float = 0x09,
DW_ATE_packed_decimal = 0x0a,
DW_ATE_numeric_string = 0x0b,
DW_ATE_edited = 0x0c,
DW_ATE_signed_fixed = 0x0d,
DW_ATE_unsigned_fixed = 0x0e,
DW_ATE_decimal_float = 0x0f,
DW_ATE_lo_user = 0x80,
DW_ATE_hi_user = 0xff;
var TYPE_ID_IDX = 0,
NAME_IDX = 1,
D_TAG_IDX = 2,
B_TAG_IDX = 2,
C_TAG_IDX = 3,
BASE_TYPE_IDX = 3,
D_OFFSET_IDX = 4,
D_SIZE_IDX = 5,
C_ELEMS_IDX = 6,
B_SIZE_IDX = 4;
var heap = {};
var cyberdwarf = undefined;
var heap_shift = {
"u8" : 0,
"u16": 1,
"u32": 2,
"i8" : 0,
"i16": 1,
"i32": 2,
"f32": 2,
"f64": 3
}
function install_heap($heap) {
heap = {
"u8" : new Uint8Array($heap),
"u16": new Uint16Array($heap),
"u32": new Uint32Array($heap),
"i8" : new Int8Array($heap),
"i16": new Int16Array($heap),
"i32": new Int32Array($heap),
"f32": new Float32Array($heap),
"f64": new Float64Array($heap)
};
}
var symbols = {};
function install_cyberdwarf(data_cd, data_symbols) {
cyberdwarf = JSON.parse(data_cd);
if (typeof(data_symbols) !== "undefined") {
var symbol_lines = data_symbols.split("\n");
for (var i in symbol_lines) {
symbol_lines[i] = symbol_lines[i].trim();
var slcp = symbol_lines[i].split(":");
if (slcp.length == 2) {
symbols[slcp[0]] = slcp[1].substring(1);
}
}
}
}
heaps_id = {
8: {
DW_ATE_unsigned: "u8",
DW_ATE_signed: "i8"
},
16: {
DW_ATE_unsigned: "u16",
DW_ATE_signed: "i16"
},
32: {
DW_ATE_unsigned: "u32",
DW_ATE_signed: "i32",
DW_ATE_float: "f32"
},
64: {
DW_ATE_signed: "f64"
}
};
function type_descriptor_to_heap_id(type_descriptor) {
var id = "";
switch (type_descriptor[B_TAG_IDX]) {
case DW_ATE_unsigned: id = "u"; break;
case DW_ATE_signed: id = "i"; break;
case DW_ATE_float: id = "f"; break;
}
id += type_descriptor[B_SIZE_IDX];
return id;
}
function decode_basic_type(val, type_descriptor, description, in_heap) {
//in_heap = false;
if (!in_heap) {
switch (type_descriptor[2]) {
case DW_ATE_unsigned:
case DW_ATE_float:
case DW_ATE_signed: {
return {"d":description, "v": val};
}; break;
}
} else {
var heap_id = type_descriptor_to_heap_id(type_descriptor);
return {"d":description, "v": heap[heap_id][val >> heap_shift[heap_id]]};
}
return ["missing", "decode_basic_type"];
}
function type_id_to_type_descriptor(type_id) {
if (!isNaN(+type_id)) {
return cyberdwarf["type_defs"][type_id];
}
if (typeof(type_id) === "string") {
type_id = cyberdwarf["type_names"][type_id];
}
return cyberdwarf["type_defs"][type_id];
}
function resolve_pointer(ptr, pointed_type_id, description) {
var td = type_id_to_type_descriptor(pointed_type_id);
switch (td[TYPE_ID_IDX]) {
case DERIVED_TYPE: {
return handle_derived_type(ptr, td, description);
} break;
case COMPOSITE_TYPE: {
return handle_composite_type(ptr, td, description);
}
case SUBROUTINE_TYPE: {
return ["ptrsub", "sub"];
}
default:
console.error("SUPPORT MISSING " + td);
return ["missing", "resolve_pointer"];
}
}
function handle_derived_type(val, type_descriptor, description) {
if (type_descriptor[NAME_IDX] !== "") {
description.push(type_descriptor[NAME_IDX]);
}
switch (type_descriptor[2]) {
case DW_TAG_pointer_type: {
description.push("*");
return follow_pointer(val, type_descriptor, description);
} break;
case DW_TAG_const_type: {
description.push("const");
return decode_variable(val, type_descriptor[BASE_TYPE_IDX], description, true);
} break;
case DW_TAG_inheritance: {
description.push(">");
return decode_variable(val, type_descriptor[BASE_TYPE_IDX], description, true);
} break;
case DW_TAG_member: {
description.push(":");
return decode_variable(val + (type_descriptor[D_OFFSET_IDX] / 8), type_descriptor[BASE_TYPE_IDX], description, true);
} break;
case DW_TAG_typedef: {
description.push("typedef");
return decode_variable(val, type_descriptor[BASE_TYPE_IDX], description, true);
} break;
case DW_TAG_volatile_type: {
description.push("volatile");
return decode_variable(val, type_descriptor[BASE_TYPE_IDX], description, true);
}
default:
console.error("Unimplemented " + type_descriptor);
}
return ["missing", "handle_derived_type " + JSON.stringify(type_descriptor)];
}
function handle_composite_type(val, type_descriptor, description) {
if (type_descriptor[NAME_IDX] !== "") {
description.push(type_descriptor[NAME_IDX]);
}
switch (type_descriptor[C_TAG_IDX]) {
case DW_TAG_structure_type: {
description.push("struct");
} break;
case DW_TAG_class_type: {
description.push("class");
} break;
default:
console.error("Unimplemented for composite " + type_descriptor);
return ["missing", "handle_composite_type"];
}
var elems = [];
for (var i in type_descriptor[C_ELEMS_IDX]) {
var deced = decode_variable(val, type_descriptor[C_ELEMS_IDX][i], []);
if (deced["v"] !== "sub") {
elems.push(deced);
}
}
return {"d": description, "v": elems};
}
function follow_pointer(val, type_descriptor, description) {
return resolve_pointer(val, type_descriptor[BASE_TYPE_IDX], description);
}
var current_function = "";
function set_current_function(func_name) {
if (typeof(symbols[func_name]) !== "undefined") {
func_name = symbols[func_name];
console.log("Got this func: " + func_name);
}
current_function = cyberdwarf["function_vars"][func_name];
console.log(current_function);
}
function clean_descriptor(res) {
if (typeof(res) === "object" && typeof(res.d) === "object" && res.d instanceof Array) {
res.d = res.d.reverse().join(" ");
}
if (typeof(res.v) === "object") {
if (res.v instanceof Array) {
for (var i in res.v) {
res.v[i] = clean_descriptor(res.v[i]);
}
} else {
res.v = clean_descriptor(res.v);
}
}
return res;
}
function wrapped_decoder(val, type_name) {
return clean_descriptor(decode_variable(val, current_function[type_name], [], false));
}
function wrapped_decoder_by_name(val, type_name) {
return clean_descriptor(decode_variable(val, type_name, [], false));
}
function decode_variable(val, type_id, description, in_heap) {
if (typeof(description) === "undefined") {
description = [];
}
if (typeof(in_heap) === "undefined") {
in_heap = false;
}
var td = type_id_to_type_descriptor(type_id);
switch (td[0]) {
case BASIC_TYPE: {
if (td[1] !== "") {
description.push(td[1]);
}
return decode_basic_type(val, td, description, in_heap)
} break;
case DERIVED_TYPE: {
return handle_derived_type(val, td, description);
}; break;
case COMPOSITE_TYPE: {
return handle_composite_type(val, td, description);
}; break;
case SUBROUTINE_TYPE: {
if (td[1] !== "") {
description.push(td[1]);
}
return {"d":description, "v": "sub"};
}; break;
case SUBPROGRAM_TYPE: {
if (td[1] !== "") {
description.push(td[1]);
}
return {"d":description, "v": "sub"};
}; break;
case STRING_REFERENCE: {
return decode_variable(val, td[1], description, in_heap);
}
default:
console.error("We don't yet support " + td[0]);
console.error(td);
return ["missing", "type_id_to_type_descriptor"];
}
return ["missing", "type_id_to_type_descriptor"];
}
return {
"decode_variable": wrapped_decoder,
"decode_by_name": wrapped_decoder_by_name,
"install_heap": install_heap,
"install_cyberdwarf": install_cyberdwarf,
"clean_descriptor": clean_descriptor,
"set_current_function": set_current_function,
"cyberdwarf": cyberdwarf
};
})();
if (typeof(exports) == "object") {
exports.decode_variable = Debugger.decode_variable;
exports.install_heap = Debugger.install_heap;
exports.install_cyberdwarf = Debugger.install_cyberdwarf;
exports.clean_descriptor = Debugger.clean_descriptor;
exports.cyberdwarf = Debugger.cyberdwarf;
exports.set_current_function = Debugger.set_current_function;
exports.decode_by_name = Debugger.decode_by_name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment