Skip to content

Commit

Permalink
Added 'Lua Debugger' section to 'Tools' menu where auto-attach on pro…
Browse files Browse the repository at this point in the history
…cess launch can be disabled
  • Loading branch information
WheretIB committed May 30, 2020
1 parent b836b41 commit b690718
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 4 deletions.
6 changes: 6 additions & 0 deletions LuaDkmDebugger/LuaDkmDebugger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Design" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.SDK" Version="16.0.204" ExcludeAssets="runtime" />
Expand All @@ -78,6 +79,11 @@
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
</ItemGroup>
<ItemGroup>
<VSCTCompile Include="LuaDkmDebugger.vsct">
<ResourceName>LuaDkmDebuggerMenus.ctmenu</ResourceName>
</VSCTCompile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LuaDkmDebuggerComponent\LuaDkmDebuggerComponent.csproj">
<Project>{671c8b4f-93f5-4aec-82e6-6c65a80798e7}</Project>
Expand Down
44 changes: 44 additions & 0 deletions LuaDkmDebugger/LuaDkmDebugger.vsct
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Extern href="stdidcmd.h"/>
<Extern href="vsshlids.h"/>

<Commands package="guidLuaDkmDebuggerPackage">
<Menus>
<Menu guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaMainExtensionMenu" priority="0x0002" type="Menu">
<Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS"/>
<Strings>
<ButtonText>Lua Debugger</ButtonText>
<CommandName>Lua Debugger</CommandName>
</Strings>
</Menu>
</Menus>

<Groups>
<Group guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaMenuGroup" priority="0x0600">
<Parent guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaMainExtensionMenu"/>
</Group>
</Groups>

<Buttons>
<Button guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaEnableBreakpointsCommandId" priority="0x0100" type="Button">
<Parent guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaMenuGroup" />
<CommandFlag>DontCache</CommandFlag>
<Strings>
<ButtonText>Attach on Launch</ButtonText>
<CommandName>LuaEnableBreakpointsCommandId</CommandName>
</Strings>
</Button>
</Buttons>
</Commands>

<Symbols>
<GuidSymbol name="guidLuaDkmDebuggerPackage" value="{99662a30-53ec-42a6-be5d-80aed0e1e2ea}" />

<GuidSymbol name="guidLuaDkmDebuggerPackageCmdSet" value="{6EB675D6-C146-4843-990E-32D43B56706C}">
<IDSymbol name="LuaMainExtensionMenu" value="0x1021" />
<IDSymbol name="LuaMenuGroup" value="0x1010" />
<IDSymbol name="LuaEnableBreakpointsCommandId" value="0x0120" />
</GuidSymbol>
</Symbols>
</CommandTable>
76 changes: 76 additions & 0 deletions LuaDkmDebugger/LuaDkmDebuggerPackage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell.Settings;
using Task = System.Threading.Tasks.Task;

namespace LuaDkmDebugger
Expand All @@ -24,16 +28,26 @@ namespace LuaDkmDebugger
/// </para>
/// </remarks>
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[ProvideAutoLoad(UIContextGuids80.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
[Guid(LuaDkmDebuggerPackage.PackageGuidString)]
[ProvideMenuResource("LuaDkmDebuggerMenus.ctmenu", 1)]
public sealed class LuaDkmDebuggerPackage : AsyncPackage
{
/// <summary>
/// LuaDkmDebuggerPackage GUID string.
/// </summary>
public const string PackageGuidString = "99662a30-53ec-42a6-be5d-80aed0e1e2ea";

public const int CommandId = 0x0120;
public static readonly Guid CommandSet = new Guid("6EB675D6-C146-4843-990E-32D43B56706C");

private IServiceProvider ServiceProvider => this;

#region Package Members

public static bool attachOnLaunch = true;
private WritableSettingsStore configurationSettingsStore = null;

/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
Expand All @@ -46,6 +60,68 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
// When initialized asynchronously, the current thread may be a background thread at this point.
// Do any initialization that requires the UI thread after switching to the UI thread.
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

try
{
SettingsManager settingsManager = new ShellSettingsManager(ServiceProvider);

configurationSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

configurationSettingsStore.CreateCollection("LuaDkmDebugger");

attachOnLaunch = configurationSettingsStore.GetBoolean("LuaDkmDebugger", "AttachOnLaunch", true);

LuaDkmDebuggerComponent.LocalComponent.attachOnLaunch = attachOnLaunch;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Failed to setup setting with " + e.Message);
}

OleMenuCommandService commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

if (commandService != null)
{
CommandID menuCommandID = new CommandID(CommandSet, CommandId);

OleMenuCommand menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID);

menuItem.BeforeQueryStatus += OnBeforeQueryStatus;

menuItem.Checked = attachOnLaunch;

commandService.AddCommand(menuItem);
}
}

private void MenuItemCallback(object sender, EventArgs args)
{
if (sender is OleMenuCommand command)
{
attachOnLaunch = !attachOnLaunch;

try
{
if (configurationSettingsStore != null)
configurationSettingsStore.SetBoolean("LuaDkmDebugger", "AttachOnLaunch", attachOnLaunch);

LuaDkmDebuggerComponent.LocalComponent.attachOnLaunch = attachOnLaunch;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Failed to setup setting with " + e.Message);
}

command.Checked = attachOnLaunch;
}
}

private void OnBeforeQueryStatus(object sender, EventArgs args)
{
if (sender is OleMenuCommand command)
{
command.Checked = attachOnLaunch;
}
}

#endregion
Expand Down
9 changes: 9 additions & 0 deletions LuaDkmDebuggerComponent/LocalComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ internal class LuaResolvedDocumentItem : DkmDataItem

public class LocalComponent : IDkmCallStackFilter, IDkmSymbolQuery, IDkmSymbolCompilerIdQuery, IDkmSymbolDocumentCollectionQuery, IDkmLanguageExpressionEvaluator, IDkmSymbolDocumentSpanQuery, IDkmModuleInstanceLoadNotification, IDkmCustomMessageCallbackReceiver, IDkmLanguageInstructionDecoder
{
public static bool attachOnLaunch = true;

#if DEBUG
public static Log log = new Log(Log.LogLevel.Debug);
#else
Expand Down Expand Up @@ -1967,6 +1969,13 @@ void IDkmModuleInstanceLoadNotification.OnModuleInstanceLoad(DkmModuleInstance m

if (nativeModuleInstance.FullName != null && nativeModuleInstance.FullName.EndsWith(".exe"))
{
if (!attachOnLaunch)
{
log.Warning("Lua attach on launch is disabled, skip search for Lua");

return;
}

log.Debug("Check if Lua library is loaded");

// Check if Lua library is loaded
Expand Down
18 changes: 14 additions & 4 deletions LuaDkmDebuggerComponent/RemoteComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ DkmCustomMessage IDkmCustomMessageForwardReceiver.SendLower(DkmCustomMessage cus

void UpdateBreakpoints(DkmProcess process, LuaRemoteProcessData processData)
{
// Can't update breakpoints if we don't have the hook attached
if (processData.locations == null)
return;

int count = processData.activeBreakpoints.Count;

if (count > 256)
Expand Down Expand Up @@ -350,10 +354,13 @@ void IDkmRuntimeMonitorBreakpointHandler.DisableRuntimeBreakpoint(DkmRuntimeBrea

void ClearStepperData(DkmProcess process, LuaRemoteProcessData processData)
{
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperStepOverAddress, 0);
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperStepIntoAddress, 0);
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperStepOutAddress, 0);
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperSkipDepthAddress, 0);
if (processData.locations != null)
{
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperStepOverAddress, 0);
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperStepIntoAddress, 0);
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperStepOutAddress, 0);
DebugHelpers.TryWriteIntVariable(process, processData.locations.helperSkipDepthAddress, 0);
}

processData.activeStepper = null;
}
Expand All @@ -374,6 +381,9 @@ bool IDkmRuntimeStepper.OwnsCurrentExecutionLocation(DkmRuntimeInstance runtimeI
// Stepping can be performed if we are inside the debug helper or inside luaV_execute
var instructionAddress = stepper.StartingAddress.CPUInstructionPart.InstructionPointer;

if (processData.locations == null)
return false;

if (instructionAddress >= processData.locations.helperStartAddress && instructionAddress < processData.locations.helperEndAddress)
return true;

Expand Down

0 comments on commit b690718

Please sign in to comment.