Skip to content

Commit

Permalink
Request runtime and module instance creation from IDkmModuleInstanceL…
Browse files Browse the repository at this point in the history
…oadNotification.OnModuleInstanceLoad.

When requested there, we don't have to wait for an unrelated runtime and won't get an exception.
  • Loading branch information
WheretIB committed May 31, 2020
1 parent 39c7e5f commit c1d5e5d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 58 deletions.
9 changes: 3 additions & 6 deletions LuaDkmDebuggerComponent/LocalComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,6 @@ DkmStackWalkFrame[] IDkmCallStackFilter.FilterNextFrame(DkmStackContext stackCon

if (processData.runtimeInstance == null)
{
// Request the RemoteComponent to create the runtime and a module
// NOTE: Due to issues with Visual Studio debugger, runtime and module were already created as soon as application launched
var message = DkmCustomMessage.Create(process.Connection, process, MessageToRemote.guid, MessageToRemote.createRuntime, null, null);

message.SendLower();

processData.runtimeInstance = process.GetRuntimeInstances().OfType<DkmCustomRuntimeInstance>().FirstOrDefault(el => el.Id.RuntimeType == Guids.luaRuntimeGuid);

if (processData.runtimeInstance == null)
Expand Down Expand Up @@ -1662,6 +1656,9 @@ void IDkmModuleInstanceLoadNotification.OnModuleInstanceLoad(DkmModuleInstance m

if (nativeModuleInstance.FullName != null && nativeModuleInstance.FullName.EndsWith(".exe"))
{
// Request the RemoteComponent to create the runtime and a module
DkmCustomMessage.Create(process.Connection, process, MessageToRemote.guid, MessageToRemote.createRuntime, null, null).SendLower();

if (!attachOnLaunch)
{
log.Warning("Lua attach on launch is disabled, skip search for Lua");
Expand Down
71 changes: 25 additions & 46 deletions LuaDkmDebuggerComponent/RemoteComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class LuaBreakpoint

internal class LuaRemoteProcessData : DkmDataItem
{
public bool unrelatedRuntimeLoaded = false;

public DkmLanguage language = null;
public DkmCompilerId compilerId;

Expand All @@ -49,63 +47,44 @@ internal class LuaRemoteProcessData : DkmDataItem
public Dictionary<ulong, LuaFunctionData> functionDataCache = new Dictionary<ulong, LuaFunctionData>();
}

public class RemoteComponent : IDkmProcessExecutionNotification, IDkmRuntimeInstanceLoadCompleteNotification, IDkmCustomMessageForwardReceiver, IDkmRuntimeBreakpointReceived, IDkmRuntimeMonitorBreakpointHandler, IDkmRuntimeStepper, IDkmLanguageConditionEvaluator
public class RemoteComponent : IDkmCustomMessageForwardReceiver, IDkmRuntimeBreakpointReceived, IDkmRuntimeMonitorBreakpointHandler, IDkmRuntimeStepper, IDkmLanguageConditionEvaluator
{
void IDkmProcessExecutionNotification.OnProcessPause(DkmProcess process, DkmProcessExecutionCounters processCounters)
DkmCustomMessage IDkmCustomMessageForwardReceiver.SendLower(DkmCustomMessage customMessage)
{
var process = customMessage.Process;
var processData = DebugHelpers.GetOrCreateDataItem<LuaRemoteProcessData>(process);

if (processData.language == null)
{
processData.compilerId = new DkmCompilerId(Guids.luaCompilerGuid, Guids.luaLanguageGuid);

processData.language = DkmLanguage.Create("Lua", processData.compilerId);
}

if (processData.runtimeInstance == null)
{
DkmRuntimeInstanceId runtimeId = new DkmRuntimeInstanceId(Guids.luaRuntimeGuid, 0);

processData.runtimeInstance = DkmCustomRuntimeInstance.Create(process, runtimeId, null);
}

if (processData.module == null)
if (customMessage.MessageCode == MessageToRemote.createRuntime)
{
DkmModuleId moduleId = new DkmModuleId(Guid.NewGuid(), Guids.luaSymbolProviderGuid);

processData.module = DkmModule.Create(moduleId, "lua.vm.code", processData.compilerId, process.Connection, null);
}
if (processData.language == null)
{
processData.compilerId = new DkmCompilerId(Guids.luaCompilerGuid, Guids.luaLanguageGuid);

// NOTE: For some reason, creating module instance from OnProcessPause _after_ OnRuntimeInstanceLoadComplete for some unrelated runtime allows us to create module instances
// Sadly, there is no way to find out if the application uses Lua and on-demand module instance creation from IDkmCustomMessageForwardReceiver.SendLower fails, so we always create a Lua module instance and hope that we don't mess up the debugger
if (processData.moduleInstance == null && processData.unrelatedRuntimeLoaded)
{
DkmDynamicSymbolFileId symbolFileId = DkmDynamicSymbolFileId.Create(Guids.luaSymbolProviderGuid);
processData.language = DkmLanguage.Create("Lua", processData.compilerId);
}

processData.moduleInstance = DkmCustomModuleInstance.Create("lua_vm", "lua.vm.code", 0, processData.runtimeInstance, null, symbolFileId, DkmModuleFlags.None, DkmModuleMemoryLayout.Unknown, 0, 1, 0, "Lua vm code", false, null, null, null);
if (processData.runtimeInstance == null)
{
DkmRuntimeInstanceId runtimeId = new DkmRuntimeInstanceId(Guids.luaRuntimeGuid, 0);

processData.moduleInstance.SetModule(processData.module, true); // Can use reload?
}
}
processData.runtimeInstance = DkmCustomRuntimeInstance.Create(process, runtimeId, null);
}

void IDkmProcessExecutionNotification.OnProcessResume(DkmProcess process, DkmProcessExecutionCounters processCounters)
{
}
if (processData.module == null)
{
DkmModuleId moduleId = new DkmModuleId(Guid.NewGuid(), Guids.luaSymbolProviderGuid);

void IDkmRuntimeInstanceLoadCompleteNotification.OnRuntimeInstanceLoadComplete(DkmRuntimeInstance runtimeInstance, DkmWorkList workList, DkmEventDescriptor eventDescriptor)
{
var processData = DebugHelpers.GetOrCreateDataItem<LuaRemoteProcessData>(runtimeInstance.Process);
processData.module = DkmModule.Create(moduleId, "lua.vm.code", processData.compilerId, process.Connection, null);
}

processData.unrelatedRuntimeLoaded = true;
}
if (processData.moduleInstance == null)
{
DkmDynamicSymbolFileId symbolFileId = DkmDynamicSymbolFileId.Create(Guids.luaSymbolProviderGuid);

DkmCustomMessage IDkmCustomMessageForwardReceiver.SendLower(DkmCustomMessage customMessage)
{
var processData = DebugHelpers.GetOrCreateDataItem<LuaRemoteProcessData>(customMessage.Process);
processData.moduleInstance = DkmCustomModuleInstance.Create("lua_vm", "lua.vm.code", 0, processData.runtimeInstance, null, symbolFileId, DkmModuleFlags.None, DkmModuleMemoryLayout.Unknown, 0, 1, 0, "Lua vm code", false, null, null, null);

if (customMessage.MessageCode == MessageToRemote.createRuntime)
{
// NOTE: It would be great to create Lua DkmCustomModuleInstance on demand when we find that application uses Lua, but if you attempt to call DkmCustomModuleInstance.Create here, you will receive E_WRONG_COMPONENT for no apparent reason
processData.moduleInstance.SetModule(processData.module, true); // Can use reload?
}
}
else if (customMessage.MessageCode == MessageToRemote.luaHelperDataLocations)
{
Expand Down
6 changes: 0 additions & 6 deletions LuaDkmDebuggerComponent/RemoteComponent.vsdconfigxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
<ManagedComponent ComponentId="guidLuaRemoteDebuggerComponent" ComponentLevel="40500" AssemblyName="LuaDkmDebuggerComponent">
<Class Name="LuaDkmDebuggerComponent.RemoteComponent">
<Implements>
<InterfaceGroup>
<NoFilter/>
<Interface Name="IDkmProcessExecutionNotification"/>
<Interface Name="IDkmRuntimeInstanceLoadCompleteNotification"/>
</InterfaceGroup>

<InterfaceGroup>
<Filter>
<SourceId RequiredValue="guidLuaMessageToRemote"/>
Expand Down

0 comments on commit c1d5e5d

Please sign in to comment.