Skip to content

Commit

Permalink
Handle CTRL+C press in debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed Jun 2, 2024
1 parent ebd5013 commit dd04b2e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
51 changes: 34 additions & 17 deletions Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,21 @@ public class Debugger
AllowFullyQualifiedBaseOpcodes = true
};

public Debugger(bool inReplMode, ulong entryPoint = 0,
bool useV1CallStack = false, bool mapStack = true, bool autoEcho = false)
{
InReplMode = inReplMode;
DebuggingProcessor = new Processor(Program.DefaultMemorySize, entryPoint, useV1CallStack, mapStack, autoEcho);
DebuggingProcessor.Registers.CopyTo(replPreviousRegisters, 0);
}

public Debugger(bool inReplMode, ulong memorySize, ulong entryPoint = 0,
bool useV1CallStack = false, bool mapStack = true, bool autoEcho = false)
{
InReplMode = inReplMode;
DebuggingProcessor = new Processor(memorySize, entryPoint, useV1CallStack, mapStack, autoEcho);
DebuggingProcessor.Registers.CopyTo(replPreviousRegisters, 0);
}
private bool forceBreak;
private bool currentlyAwaitingInput = false;

public Debugger(bool inReplMode, Processor processorToDebug)
{
InReplMode = inReplMode;
DebuggingProcessor = processorToDebug;
DebuggingProcessor.Registers.CopyTo(replPreviousRegisters, 0);
Console.CancelKeyPress += Console_CancelKeyPress;
}

public Debugger(bool inReplMode, ulong memorySize = Program.DefaultMemorySize, ulong entryPoint = 0,
bool useV1CallStack = false, bool mapStack = true, bool autoEcho = false)
: this(inReplMode, new Processor(memorySize, entryPoint, useV1CallStack, mapStack, autoEcho)) { }

public void LoadDebugFile(string debugFilePath)
{
try
Expand Down Expand Up @@ -195,7 +187,11 @@ public void StartDebugger()
try
{
bool breakForDebug;
if (InReplMode)
if (forceBreak)
{
breakForDebug = true;
}
else if (InReplMode)
{
// If we're acting as a REPL, only ask user for an instruction if there isn't one already to execute.
breakForDebug = DebuggingProcessor.Memory[DebuggingProcessor.Registers[(int)Register.rpo]] == 0;
Expand Down Expand Up @@ -237,11 +233,16 @@ public void StartDebugger()
DisplayDebugInfo();
}
}
currentlyAwaitingInput = true;
bool endCommandEntryLoop = false;
while (!endCommandEntryLoop && breakForDebug)
{
Console.Write(InReplMode ? Strings_Debugger.REPL_Command_Prompt : Strings_Debugger.Command_Prompt);
string userInput = Console.ReadLine()!;
string? userInput = Console.ReadLine();
if (userInput is null)
{
continue;
}
if (InReplMode)
{
if (ProcessReplInput(userInput))
Expand Down Expand Up @@ -308,6 +309,7 @@ public void StartDebugger()
}
}
}
currentlyAwaitingInput = false;
if (DebuggingProcessor.Execute(false) && !InReplMode)
{
Program.PrintWarning(Strings_Debugger.Warning_HLT_Reached);
Expand Down Expand Up @@ -912,5 +914,20 @@ private bool ProcessReplInput(string userInput)
#endif
}
}

private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e)
{
forceBreak = true;
// Only kill the process if user presses CTRL+C while being prompted for input,
// otherwise return them to the input prompt
if (currentlyAwaitingInput)
{
Environment.Exit(0);
}
else
{
e.Cancel = true;
}
}
}
}
2 changes: 1 addition & 1 deletion Program.Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace AssEmbly
{
internal static partial class Program
{
public static readonly ulong DefaultMemorySize = 8192; // 8KB
public const ulong DefaultMemorySize = 8192; // 8KB

// Shared methods that are used by multiple commands
public static AAPFile? LoadAAPFile(string appPath, bool ignoreNewerVersion)
Expand Down

0 comments on commit dd04b2e

Please sign in to comment.