|
| 1 | +# @file pmdb - A gdb-like JavaScript debugger interface |
| 2 | +# @author Tom Tang <[email protected]> |
| 3 | +# @date July 2023 |
| 4 | + |
| 5 | +import pythonmonkey as pm |
| 6 | + |
| 7 | +def debuggerInput(prompt: str): |
| 8 | + try: |
| 9 | + return input(prompt) # blocking |
| 10 | + except KeyboardInterrupt: |
| 11 | + print("\b\bQuit") # to match the behaviour of gdb |
| 12 | + return "" |
| 13 | + except Exception as e: |
| 14 | + print(e) |
| 15 | + return "" |
| 16 | + |
| 17 | +def enable(debuggerGlobalObject = pm.eval("debuggerGlobal")): |
| 18 | + if debuggerGlobalObject._pmdbEnabled: |
| 19 | + return # already enabled, skipping |
| 20 | + |
| 21 | + debuggerGlobalObject._pmdbEnabled = True |
| 22 | + debuggerGlobalObject.eval("""(debuggerInput, _pythonPrint, _pythonExit) => { |
| 23 | + const dbg = new Debugger() |
| 24 | + const mainDebuggee = dbg.addDebuggee(mainGlobal) |
| 25 | + dbg.uncaughtExceptionHook = (e) => { |
| 26 | + _pythonPrint(e) |
| 27 | + } |
| 28 | +
|
| 29 | + function makeDebuggeeValue (val) { |
| 30 | + if (val instanceof Debugger.Object) { |
| 31 | + return dbg.adoptDebuggeeValue(val) |
| 32 | + } else { |
| 33 | + // See https://firefox-source-docs.mozilla.org/js/Debugger/Debugger.Object.html#makedebuggeevalue-value |
| 34 | + return mainDebuggee.makeDebuggeeValue(val) |
| 35 | + } |
| 36 | + } |
| 37 | +
|
| 38 | + function print (...args) { |
| 39 | + const logger = makeDebuggeeValue(mainGlobal.console.log) |
| 40 | + logger.apply(logger, args.map(makeDebuggeeValue)) |
| 41 | + } |
| 42 | + |
| 43 | + function printErr (...args) { |
| 44 | + const logger = makeDebuggeeValue(mainGlobal.console.error) |
| 45 | + logger.apply(logger, args.map(makeDebuggeeValue)) |
| 46 | + } |
| 47 | + |
| 48 | + function printSource (frame, location) { |
| 49 | + const src = frame.script.source.text |
| 50 | + const line = src.split('\\n').slice(location.lineNumber-1, location.lineNumber).join('\\n') |
| 51 | + print(line) |
| 52 | + print(" ".repeat(location.columnNumber) + "^") // indicate column position |
| 53 | + } |
| 54 | + |
| 55 | + function getCommandInputs () { |
| 56 | + const input = debuggerInput("(pmdb) > ") // blocking |
| 57 | + const [_, command, rest] = input.match(/\\s*(\\w+)?(?:\\s+(.*))?/) |
| 58 | + return { command, rest } |
| 59 | + } |
| 60 | +
|
| 61 | + function enterDebuggerLoop (frame, checkIsBreakpoint = false) { |
| 62 | + const metadata = frame.script.getOffsetMetadata(frame.offset) |
| 63 | + if (checkIsBreakpoint && !metadata.isBreakpoint) { |
| 64 | + // This bytecode offset does not qualify as a breakpoint, skipping |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + blockingLoop: while (true) { |
| 69 | + const { command, rest } = getCommandInputs() // blocking |
| 70 | + switch (command) { |
| 71 | + case "b": |
| 72 | + case "break": { |
| 73 | + // Set breakpoint on specific line number |
| 74 | + const lineNum = Number(rest) |
| 75 | + if (!lineNum) { |
| 76 | + print(`"break <lineNumber>" command requires a valid line number argument.`) |
| 77 | + continue blockingLoop; |
| 78 | + } |
| 79 | +
|
| 80 | + // find the bytecode offset for possible breakpoint location |
| 81 | + const bp = frame.script.getPossibleBreakpoints({ line: lineNum })[0] |
| 82 | + if (!bp) { |
| 83 | + print(`No possible breakpoint location found on line ${lineNum}`) |
| 84 | + continue blockingLoop; |
| 85 | + } |
| 86 | +
|
| 87 | + // add handler |
| 88 | + frame.script.setBreakpoint(bp.offset, (frame) => enterDebuggerLoop(frame)) |
| 89 | +
|
| 90 | + // print breakpoint info |
| 91 | + print(`Breakpoint set on line ${bp.lineNumber} column ${bp.columnNumber+1} in "${frame.script.url}" :`) |
| 92 | + printSource(frame, bp) |
| 93 | +
|
| 94 | + continue blockingLoop; |
| 95 | + } |
| 96 | + case "c": |
| 97 | + case "cont": |
| 98 | + // Continue execution until next breakpoint or `debugger` statement |
| 99 | + frame.onStep = undefined // clear step next handler |
| 100 | + break blockingLoop; |
| 101 | + case "n": |
| 102 | + case "next": |
| 103 | + // Step next |
| 104 | + frame.onStep = function () { enterDebuggerLoop(this, /*checkIsBreakpoint*/ true) } // add handler |
| 105 | + break blockingLoop; |
| 106 | + case "bt": |
| 107 | + case "backtrace": |
| 108 | + // Print backtrace of current execution frame |
| 109 | + // FIXME: we currently implement this using Error.stack |
| 110 | + print(frame.eval("(new Error).stack.split('\\\\n').slice(1).join('\\\\n')").return) |
| 111 | + continue blockingLoop; |
| 112 | + case "l": |
| 113 | + case "line": { |
| 114 | + // Print current line |
| 115 | + printSource(frame, metadata) |
| 116 | + continue blockingLoop; |
| 117 | + } |
| 118 | + case "p": |
| 119 | + case "exec": |
| 120 | + case "print": { |
| 121 | + // Execute an expression in debugging script's context and print its value |
| 122 | + if (!rest) { |
| 123 | + print(`"print <expr>" command requires an argument.`) |
| 124 | + continue blockingLoop; |
| 125 | + } |
| 126 | + const result = frame.eval(rest) |
| 127 | + if (result.throw) printErr(result.throw) // on error |
| 128 | + else print(result.return) // on success |
| 129 | + continue blockingLoop; |
| 130 | + } |
| 131 | + case "q": |
| 132 | + case "quit": |
| 133 | + case "kill": |
| 134 | + // Force exit the program |
| 135 | + _pythonExit(127) |
| 136 | + break blockingLoop; |
| 137 | + case "h": |
| 138 | + case "help": |
| 139 | + // Print help message |
| 140 | + // XXX: keep this in sync with the actual implementation |
| 141 | + print([ |
| 142 | + "List of commands:", |
| 143 | + "• b <lineNumber>/break <lineNumber>: Set breakpoint on specific line", |
| 144 | + "• c/cont: Continue execution until next breakpoint or debugger statement", |
| 145 | + "• n/next: Step next", |
| 146 | + "• bt/backtrace: Print backtrace of current execution frame", |
| 147 | + "• l/line: Print current line", |
| 148 | + "• p <expr>/print <expr>/exec <expr>: Execute an expression in debugging script's context and print its value", |
| 149 | + "• q/quit/kill: Force exit the program", |
| 150 | + "• h/help: Print help message", |
| 151 | + ].join("\\n")) |
| 152 | + continue blockingLoop; |
| 153 | + case "": |
| 154 | + case undefined: |
| 155 | + // no-op |
| 156 | + continue blockingLoop; |
| 157 | + default: |
| 158 | + print(`Undefined command: "${command}". Try "help".`) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +
|
| 163 | + // Enter debugger on uncaught exceptions |
| 164 | + dbg.onExceptionUnwind = (frame, err) => { |
| 165 | + const isUncaught = !frame.script.isInCatchScope(frame.offset) // not in a catch block |
| 166 | + && frame.older == null // this is the outermost frame |
| 167 | + if (isUncaught) { |
| 168 | + printErr("Uncaught exception:") |
| 169 | + printErr(err) |
| 170 | + enterDebuggerLoop(frame) |
| 171 | + } |
| 172 | + } |
| 173 | +
|
| 174 | + // Enter debugger on `debugger;` statement |
| 175 | + dbg.onDebuggerStatement = (frame) => enterDebuggerLoop(frame) |
| 176 | +
|
| 177 | + }""")(debuggerInput, print, lambda status: exit(int(status))) |
0 commit comments