Skip to content

Commit b0cf7e3

Browse files
committed
Place breakpoint at luaD_throw only when break is expected there
This fixes slowdown of Lua coroutines and other uses of luaD_throw for non-error cases
1 parent b079aa5 commit b0cf7e3

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

LuaDkmDebuggerComponent/AttachmentHelpers.cs

+28-6
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ internal static ulong FindVariableAddress(DkmNativeModuleInstance nativeModuleIn
424424
return 0;
425425
}
426426

427-
internal static Guid? CreateTargetFunctionBreakpointAtAddress(DkmProcess process, DkmNativeModuleInstance moduleWithLoadedLua, string name, string desc, ulong address)
427+
internal static DkmRuntimeInstructionBreakpoint CreateTargetFunctionBreakpointObjectAtAddress(DkmProcess process, DkmNativeModuleInstance moduleWithLoadedLua, string name, string desc, ulong address, bool enabled)
428428
{
429429
if (address != 0)
430430
{
@@ -434,9 +434,10 @@ internal static ulong FindVariableAddress(DkmNativeModuleInstance nativeModuleIn
434434

435435
var breakpoint = DkmRuntimeInstructionBreakpoint.Create(Guids.luaSupportBreakpointGuid, null, nativeAddress, false, null);
436436

437-
breakpoint.Enable();
437+
if (enabled)
438+
breakpoint.Enable();
438439

439-
return breakpoint.UniqueId;
440+
return breakpoint;
440441
}
441442
else
442443
{
@@ -446,7 +447,17 @@ internal static ulong FindVariableAddress(DkmNativeModuleInstance nativeModuleIn
446447
return null;
447448
}
448449

449-
internal static Guid? CreateTargetFunctionBreakpointAtDebugStart(DkmProcess process, DkmNativeModuleInstance moduleWithLoadedLua, string name, string desc, out ulong breakAddress)
450+
internal static Guid? CreateTargetFunctionBreakpointAtAddress(DkmProcess process, DkmNativeModuleInstance moduleWithLoadedLua, string name, string desc, ulong address)
451+
{
452+
DkmRuntimeInstructionBreakpoint breakpoint = CreateTargetFunctionBreakpointObjectAtAddress(process, moduleWithLoadedLua, name, desc, address, true);
453+
454+
if (breakpoint != null)
455+
return breakpoint.UniqueId;
456+
457+
return null;
458+
}
459+
460+
internal static DkmRuntimeInstructionBreakpoint CreateTargetFunctionBreakpointObjectAtDebugStart(DkmProcess process, DkmNativeModuleInstance moduleWithLoadedLua, string name, string desc, out ulong breakAddress, bool enabled)
450461
{
451462
var address = TryGetFunctionAddressAtDebugStart(moduleWithLoadedLua, name, out string error);
452463

@@ -458,10 +469,11 @@ internal static ulong FindVariableAddress(DkmNativeModuleInstance nativeModuleIn
458469

459470
var breakpoint = DkmRuntimeInstructionBreakpoint.Create(Guids.luaSupportBreakpointGuid, null, nativeAddress, false, null);
460471

461-
breakpoint.Enable();
472+
if (enabled)
473+
breakpoint.Enable();
462474

463475
breakAddress = address.Value;
464-
return breakpoint.UniqueId;
476+
return breakpoint;
465477
}
466478
else
467479
{
@@ -472,6 +484,16 @@ internal static ulong FindVariableAddress(DkmNativeModuleInstance nativeModuleIn
472484
return null;
473485
}
474486

487+
internal static Guid? CreateTargetFunctionBreakpointAtDebugStart(DkmProcess process, DkmNativeModuleInstance moduleWithLoadedLua, string name, string desc, out ulong breakAddress)
488+
{
489+
DkmRuntimeInstructionBreakpoint breakpoint = CreateTargetFunctionBreakpointObjectAtDebugStart(process, moduleWithLoadedLua, name, desc, out breakAddress, true);
490+
491+
if (breakpoint != null)
492+
return breakpoint.UniqueId;
493+
494+
return null;
495+
}
496+
475497
internal static Guid? CreateTargetFunctionBreakpointAtDebugEnd(DkmProcess process, DkmNativeModuleInstance moduleWithLoadedLua, string name, string desc, out ulong breakAddress)
476498
{
477499
var address = TryGetFunctionAddressAtDebugEnd(moduleWithLoadedLua, name, out string error);

LuaDkmDebuggerComponent/LocalComponent.cs

+7-14
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ internal class LuaLocalProcessData : DkmDataItem
100100
public Guid breakpointLuaLoad;
101101

102102
// Two-stage Lua exception handling
103-
public bool captureNextThrow = false;
104103
public Guid breakpointLuaBreakError;
105104
public Guid breakpointLuaRuntimeError;
106105
public ulong breakpointLuaThrowAddress = 0;
107-
public Guid breakpointLuaThrow;
106+
public DkmRuntimeInstructionBreakpoint breakpointLuaThrow;
108107

109108
public Guid breakpointLuaHelperInitialized;
110109

@@ -2645,11 +2644,11 @@ void IDkmModuleInstanceLoadNotification.OnModuleInstanceLoad(DkmModuleInstance m
26452644
if (processData.luaLocations != null)
26462645
{
26472646
processData.breakpointLuaThrowAddress = processData.luaLocations.luaThrow;
2648-
processData.breakpointLuaThrow = AttachmentHelpers.CreateTargetFunctionBreakpointAtAddress(process, processData.moduleWithLoadedLua, "luaD_throw", "Lua script error", processData.luaLocations.luaThrow).GetValueOrDefault(Guid.Empty);
2647+
processData.breakpointLuaThrow = AttachmentHelpers.CreateTargetFunctionBreakpointObjectAtAddress(process, processData.moduleWithLoadedLua, "luaD_throw", "Lua script error", processData.luaLocations.luaThrow, false);
26492648
}
26502649
else
26512650
{
2652-
processData.breakpointLuaThrow = AttachmentHelpers.CreateTargetFunctionBreakpointAtDebugStart(process, processData.moduleWithLoadedLua, "luaD_throw", "Lua script error", out processData.breakpointLuaThrowAddress).GetValueOrDefault(Guid.Empty);
2651+
processData.breakpointLuaThrow = AttachmentHelpers.CreateTargetFunctionBreakpointObjectAtDebugStart(process, processData.moduleWithLoadedLua, "luaD_throw", "Lua script error", out processData.breakpointLuaThrowAddress, false);
26532652
}
26542653

26552654
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
@@ -3248,20 +3247,14 @@ DkmCustomMessage IDkmCustomMessageCallbackReceiver.SendHigher(DkmCustomMessage c
32483247

32493248
log.Debug("Enabling a trap at next luaD_throw");
32503249

3251-
processData.captureNextThrow = true;
3250+
if (processData.breakpointLuaThrow != null)
3251+
processData.breakpointLuaThrow.Enable();
32523252
}
3253-
else if (data.breakpointId == processData.breakpointLuaThrow)
3253+
else if (processData.breakpointLuaThrow != null && data.breakpointId == processData.breakpointLuaThrow.UniqueId)
32543254
{
32553255
log.Debug("Detected Lua exception throw");
32563256

3257-
if (!processData.captureNextThrow)
3258-
{
3259-
log.Debug("Capture is not enabled");
3260-
3261-
return null;
3262-
}
3263-
3264-
processData.captureNextThrow = false;
3257+
processData.breakpointLuaThrow.Disable();
32653258

32663259
var inspectionSession = EvaluationHelpers.CreateInspectionSession(process, thread, data, out DkmStackWalkFrame frame);
32673260

0 commit comments

Comments
 (0)