Include capstone.js and libwabt.js for debugging

This commit is contained in:
Fabian 2017-12-21 11:36:58 -06:00
parent 02f75f476c
commit 136fad7e01
4 changed files with 93 additions and 0 deletions

View file

@ -249,3 +249,9 @@ node_modules/.bin/jshint:
jshint: node_modules/.bin/jshint
./node_modules/.bin/jshint --config=./.jshint.json src tests gen
build/capstone-x86.min.js:
wget -P build https://github.com/AlexAltea/capstone.js/releases/download/v3.0.5-rc1/capstone-x86.min.js
build/libwabt.js:
wget -P build https://raw.githubusercontent.com/WebAssembly/wabt/master/demo/libwabt.js

View file

@ -17,11 +17,14 @@
// jor1k stuff
LIB_FILES += " jor1k.js 9p.js filesystem.js marshall.js utf8.js";
var BUILD_FILES = "capstone-x86.min.js libwabt.js";
var to_load = [];
load_scripts(CORE_FILES, "src/");
load_scripts(BROWSER_FILES, "src/browser/");
load_scripts(LIB_FILES, "lib/");
load_scripts(BUILD_FILES, "build/");
function load_scripts(resp, path)
{

View file

@ -711,4 +711,72 @@ CPU.prototype.debug_init = function()
// this.debug.dump_regs_short();
//}
};
let capstone_decoder;
debug.dump_code = function(is_32, buffer, start)
{
if(!capstone_decoder)
{
if(typeof cs === "undefined")
{
dbg_log("Warning: Missing capstone library, disassembly not available");
return;
}
capstone_decoder = [
new cs.Capstone(cs.ARCH_X86, cs.MODE_16),
new cs.Capstone(cs.ARCH_X86, cs.MODE_32),
];
}
try
{
const instructions = capstone_decoder[is_32].disasm(buffer, start);
instructions.forEach(function (instr) {
dbg_log(h(instr.address) + ": " +
v86util.pads(instr.bytes.map(x => h(x, 2).slice(-2)).join(" "), 20) + " " +
instr.mnemonic + " " + instr.op_str);
});
dbg_log("");
}
catch(e)
{
dbg_log("Could not disassemble: " + Array.from(buffer).map(x => h(x, 2)).join(" "));
}
};
debug.dump_wasm = function(buffer)
{
if(typeof wabt === "undefined")
{
dbg_log("Warning: Missing libwabt, wasm dump not available");
return;
}
// Need to make a small copy otherwise libwabt goes nuts trying to copy
// the whole underlying buffer
buffer = buffer.slice();
try
{
var module = wabt.readWasm(buffer, { readDebugNames: false });
module.generateNames();
module.applyNames();
const result = module.toText({ foldExprs: true, inlineExport: true });
dbg_log(result);
}
catch(e)
{
console.log(e.toString());
}
finally
{
if(module)
{
module.destroy();
}
}
};
};

View file

@ -1,3 +1,5 @@
"use strict";
var performance = {};
@ -16,3 +18,17 @@ var module = {};
var WebAssembly = { Memory() {}, Table() {}, instantiate() {}, compile() {}, Instance() {}, Module() {} };
WebAssembly.Module.customSections = function(module, section) {};
var wabt = {
readWasm: function(buf, opt) {},
generateNames: function() {},
applyNames: function() {},
toText: function() {},
};
var cs = {
Capstone: function() {},
ARCH_X86: 0,
MODE_16: 0,
MODE_32: 0,
disasm: { bytes: "", mnemonic: "", op_str: "", },
};