Skip to content

Commit

Permalink
On rethrow, print VM's exception stack
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiaomao committed Jun 20, 2024
1 parent ce4c92d commit 5c257e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
34 changes: 32 additions & 2 deletions hld/Debugger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ typedef WatchPoint = {
var forReadWrite : Bool;
}

typedef StackRawInfo = { fidx : Int, fpos : Int, codePos : Int, ebp : hld.Pointer };

typedef StackInfo = { file : String, line : Int, ebp : Pointer, ?context : { obj : format.hl.Data.ObjPrototype, field : String } };

class Debugger {
Expand Down Expand Up @@ -49,9 +51,9 @@ class Debugger {

var breakPoints : Array<{ fid : Int, pos : Int, codePos : Int, oldByte : Int, condition : String }>;
var nextStep(default,set): Int = -1;
var currentStack : Array<{ fidx : Int, fpos : Int, codePos : Int, ebp : hld.Pointer }>;
var currentStack : Array<StackRawInfo>;
var watches : Array<WatchPoint>;
var threads : Map<Int,{ id : Int, stackTop : Pointer, exception : Pointer, name : String }>;
var threads : Map<Int,{ id : Int, stackTop : Pointer, exception : Pointer, ?exceptionStack: Array<StackRawInfo>, name : String }>;
var afterStep = false;

public var is64(get, never) : Bool;
Expand Down Expand Up @@ -245,6 +247,16 @@ class Debugger {
return eval.readVal(exc, HDyn);
}

public function getVMExceptionStack() {
var t = threads.get(currentThread);
if( t == null )
return null;
var stack = t.exceptionStack;
if( stack == null )
return null;
return stack.map(e -> stackInfo(e));
}

public function hasStack() {
return currentStack.length > 0;
}
Expand Down Expand Up @@ -420,6 +432,8 @@ class Debugger {
var tinfos = eval.readPointer(jit.threads.offset(8));
var flagsPos = jit.align.ptr * 6 + 8;
var excPos = jit.align.ptr * 5 + 8;
var excStackCountPos = flagsPos + 4;
var excStackPos = flagsPos + 8 + 256 + (jit.hlVersion >= 1.13 ? 128 : 0);
var namePos = jit.hlVersion >= 1.13 ? flagsPos + 8 : -1;
for( i in 0...count ) {
var tinf = eval.readPointer(tinfos.offset(jit.align.ptr * i));
Expand All @@ -440,6 +454,7 @@ class Debugger {
id : tid,
stackTop : eval.readPointer(tinf.offset(8)),
exception : flags & 4 == 0 ? null : tinf.offset(excPos),
exceptionStack : flags & 1 == 0 ? null : readVMExceptionStack(tinf.offset(excStackPos), eval.readI32(tinf.offset(excStackCountPos))),
name : name,
};
threads.set(tid, t);
Expand All @@ -448,6 +463,21 @@ class Debugger {
threads.set(currentThread,{ id : currentThread, stackTop: null, exception: null, name : null });
}

function readVMExceptionStack(base : Pointer, count : Int) : Array<StackRawInfo> {
var stack = [];
if( count <= 0 || count >= 256 || base.isNull() )
return stack;
for( i in 0...count ) {
var codePtr = eval.readPointer(base.offset(i * jit.align.ptr));
if( codePtr < jit.codeStart || codePtr > jit.codeEnd)
continue;
var codePos = codePtr.sub(jit.codeStart);
var e = jit.resolveAsmPos(codePos);
stack.push(e);
}
return [for( s in stack ) if( module.isValid(s.fidx, s.fpos) ) s];
}

function prepareStack( isWatchbreak=false ) {
currentStackFrame = 0;
currentStack = makeStack(currentThread, isWatchbreak);
Expand Down
3 changes: 3 additions & 0 deletions src/HLAdapter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ class HLAdapter extends DebugSession {
default: dbg.eval.valueStr(exc);
};
debug("Exception: " + str);
var bt = dbg.getVMExceptionStack();
if( bt != null )
trace("Rethrow from " + bt.map(f -> '${f.file}:${f.line}(${stackStr(f)})'));
}

syncThreads();
Expand Down

0 comments on commit 5c257e7

Please sign in to comment.