Skip to content

Commit

Permalink
Add support for conditional breakpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiaomao committed Mar 26, 2024
1 parent cc855b1 commit a4e8d85
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
25 changes: 21 additions & 4 deletions hld/Debugger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Debugger {
var processExit : Bool;
var ignoredRoots : Map<String,Bool>;

var breakPoints : Array<{ fid : Int, pos : Int, codePos : Int, oldByte : Int }>;
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 watches : Array<WatchPoint>;
Expand Down Expand Up @@ -284,6 +284,7 @@ class Debugger {

function wait( onStep = false ) : Api.WaitResult {
var cmd = null;
var condition : String = null;
watchBreak = null;
while( true ) {
cmd = api.wait(customTimeout == null ? 1000 : Math.ceil(customTimeout * 1000));
Expand Down Expand Up @@ -319,6 +320,7 @@ class Debugger {
var codePos = getCodePos(tid) - 1;
for( b in breakPoints ) {
if( b.codePos == codePos ) {
condition = b.condition;
// restore code
setAsm(codePos, b.oldByte);
// move backward
Expand Down Expand Up @@ -379,6 +381,21 @@ class Debugger {

readThreads();
prepareStack(cmd.r == Watchbreak);

// if breakpoint has a condition, try to evaluate and do not actually break on false
if( condition != null ) {
try {
var value = getValue(condition);
if( value != null ) {
switch( value.v ) {
case VBool( b ) if( !b ): return Handled;
default:
}
}
} catch( e : Dynamic ) {
if( DEBUG ) trace("Can't evaluate condition for breakpoint: " + condition);
}
}
return cmd.r;
}

Expand Down Expand Up @@ -486,7 +503,7 @@ class Debugger {
if( lineChange || c == CRet || (mode == Into && c.match(CCall(_))) ) {
var codePos = jit.getCodePos(s.fidx, pos);
var old = getAsm(codePos);
var bp = { fid : lineChange ? -1 : (c == CRet ? -2 : -3), pos : pos, codePos : codePos, oldByte : old };
var bp = { fid : lineChange ? -1 : (c == CRet ? -2 : -3), pos : pos, codePos : codePos, oldByte : old, condition : null };
breakPoints.push(bp);
marked.set(pos, bp);
if( codePos == currentCodePos && onBreakPoint ) {
Expand Down Expand Up @@ -891,7 +908,7 @@ class Debugger {
throw "Failed to set register " + reg;
}

public function addBreakpoint( file : String, line : Int ) {
public function addBreakpoint( file : String, line : Int, condition : Null<String> ) {
var breaks = module.getBreaks(file, line);
if( breaks == null )
return -1;
Expand All @@ -910,7 +927,7 @@ class Debugger {
var codePos = jit.getCodePos(b.ifun, b.pos);
var old = getAsm(codePos);
setAsm(codePos, INT3);
breakPoints.push({ fid : b.ifun, pos : b.pos, oldByte : old, codePos : codePos });
breakPoints.push({ fid : b.ifun, pos : b.pos, oldByte : old, codePos : codePos, condition : condition });
set = true;
}
return breaks.line;
Expand Down
7 changes: 4 additions & 3 deletions hld/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ class Main {
var fileLine = nextArg().split(":");
var line = Std.parseInt(fileLine.pop());
var file = fileLine.join(":");
line = dbg.addBreakpoint(file, line);
var condition = args.shift();
line = dbg.addBreakpoint(file, line, condition);
if( line >= 0 ) {
breaks.push({file:file, line:line});
Sys.println("Breakpoint set line "+line);
breaks.push({file:file, line:line, condition:condition});
Sys.println("Breakpoint set line " + line + (condition == null ? "" : ", cond: " + condition));
} else
Sys.println("No breakpoint set");
case "p", "print", "global":
Expand Down
4 changes: 2 additions & 2 deletions src/HLAdapter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class HLAdapter extends DebugSession {

response.body.supportsConfigurationDoneRequest = true;
response.body.supportsFunctionBreakpoints = false;
//response.body.supportsConditionalBreakpoints = true;
response.body.supportsConditionalBreakpoints = true;
response.body.supportsEvaluateForHovers = true;
response.body.supportsStepBack = false;
response.body.supportsSetVariable = true;
Expand Down Expand Up @@ -500,7 +500,7 @@ class HLAdapter extends DebugSession {
for( bp in args.breakpoints ) {
var line = -1;
for( f in files ) {
line = dbg.addBreakpoint(f, bp.line);
line = dbg.addBreakpoint(f, bp.line, bp.condition);
if( line >= 0 ) break;
}
if( line >= 0 )
Expand Down

0 comments on commit a4e8d85

Please sign in to comment.