From d423024d2530b6a20da43ead4b2b60e6506ffc89 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Sat, 22 Sep 2018 13:16:12 +0300 Subject: [PATCH] Minor fixes to dump_bytecode.py and duk_debug.js * Add script path relative resolution for duk_opcodes.py so that one can run tools/dump_bytecode.py from repo root. * Use os.path.join() rather than hardcoded separators. * Use wider opcode name (12 chars) in duk_debug.js. * Use matching format for opcodes, arguments, and comments in dump_bytecode.py. --- debugger/duk_debug.js | 4 ++-- tools/dump_bytecode.py | 47 +++++++++++++++++++----------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/debugger/duk_debug.js b/debugger/duk_debug.js index 9205953d..e2d0bdab 100755 --- a/debugger/duk_debug.js +++ b/debugger/duk_debug.js @@ -1138,9 +1138,9 @@ Debugger.prototype.decodeBytecodeFromBuffer = function (buf, consts, funcs) { } if (args.length > 0) { - str = sprintf('%05d %08x %-10s %s', pc, ins, op.name, args.join(', ')); + str = sprintf('%05d %08x %-12s %s', pc, ins, op.name, args.join(', ')); } else { - str = sprintf('%05d %08x %-10s', pc, ins, op.name); + str = sprintf('%05d %08x %-12s', pc, ins, op.name); } if (comments.length > 0) { str = sprintf('%-44s ; %s', str, comments.join(', ')); diff --git a/tools/dump_bytecode.py b/tools/dump_bytecode.py index b8796ecf..b12709a5 100644 --- a/tools/dump_bytecode.py +++ b/tools/dump_bytecode.py @@ -3,11 +3,15 @@ # Utility to dump bytecode into a human readable form. # -import struct +import os +import sys import optparse +import struct import yaml -from os.path import isfile +script_path = sys.path[0] # http://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python + +ops = None def decode_string(buf, off): strlen, = struct.unpack('>L', buf[off:off + 4]) @@ -17,7 +21,6 @@ def decode_string(buf, off): return off, strdata - def sanitize_string(val): # Don't try to UTF-8 decode, just escape non-printable ASCII. def f(c): @@ -28,16 +31,14 @@ def sanitize_string(val): return "'" + ''.join(map(f, val)) + "'" - def decode_sanitize_string(buf, off): off, val = decode_string(buf, off) return off, sanitize_string(val) - def dump_ins(ins, x): global ops - if not ops: + if ops is None: return '' pc = x / 4 @@ -146,17 +147,15 @@ def dump_ins(ins, x): if ins & f['mask']: comments.append(f['name']) - if len(args): - res = '{} {}'.format(op['name'], ', '.join(args)) + if len(args) > 0: + res = '%-12s %s' % (op['name'], ', '.join(args)) else: res = op['name'] - - if len(comments): - res = '{} ; {}'.format(res, ', '.join(comments)) + if len(comments) > 0: + res = '%-44s ; %s' % (res, ', '.join(comments)) return res - def dump_function(buf, off, ind): count_inst, count_const, count_funcs = struct.unpack('>LLL', buf[off:off + 12]) off += 12 @@ -233,7 +232,6 @@ def dump_function(buf, off, ind): return off - def dump_bytecode(buf, off, ind): sig, = struct.unpack('B', buf[off:off + 1]) print('%sSignature byte: 0x%02x' % (ind, sig)) @@ -247,22 +245,20 @@ def dump_bytecode(buf, off, ind): return off - def main(): global ops - ops = None - yamlpath = None - if isfile('duk_opcodes.yaml'): - yamlpath = 'duk_opcodes.yaml' - elif isfile('../debugger/duk_opcodes.yaml'): - yamlpath = '../debugger/duk_opcodes.yaml' - else: - print('WARN: duk_opcodes.yaml NOT found! Unable to show opcodes!') + for ops_path in [ '.', + os.path.join('..', 'debugger'), + script_path, + os.path.join(script_path, '..', 'debugger') ]: + fn = os.path.join(ops_path, 'duk_opcodes.yaml') + if os.path.isfile(fn): + with open(fn) as f: + ops = yaml.load(f)['opcodes'] - if yamlpath: - y = yaml.load(open(yamlpath)) - ops = y['opcodes'] + if ops is None: + print('WARN: duk_opcodes.yaml not found, unable do dump opcodes!') parser = optparse.OptionParser() parser.add_option('--hex-decode', dest='hex_decode', default=False, action='store_true', @@ -276,6 +272,5 @@ def main(): d = d.decode('hex') dump_bytecode(d, 0, '') - if __name__ == '__main__': main()