-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from vein-lang/features/debug-terminal
add debug terminal
- Loading branch information
Showing
9 changed files
with
307 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
using Terminal.Gui; | ||
|
||
public static class IshtarSharedDebugData | ||
{ | ||
public static readonly object guarder = new object(); | ||
|
||
public static Queue<string> StdOut { get; } = new(); | ||
public static Queue<string> TraceOut { get; } = new(); | ||
|
||
private static IshtarState _state = new("UNK", "@entry", default, "NONE", 0); | ||
|
||
|
||
public static void SetState(IshtarState state) | ||
{ | ||
if (state.GetHashCode() == _state.GetHashCode()) | ||
return; | ||
|
||
lock (guarder) | ||
{ | ||
_state = state; | ||
} | ||
} | ||
|
||
public static IshtarState GetState() | ||
{ | ||
lock (guarder) | ||
{ | ||
return _state; | ||
} | ||
} | ||
|
||
|
||
public static bool StdOutHas() | ||
{ | ||
lock (guarder) | ||
{ | ||
return StdOut.Count != 0; | ||
} | ||
} | ||
public static bool TraceOutHas() | ||
{ | ||
lock (guarder) | ||
{ | ||
return TraceOut.Count != 0; | ||
} | ||
} | ||
|
||
public static string StdOutPop() | ||
{ | ||
lock (guarder) | ||
{ | ||
return StdOut.Dequeue(); | ||
} | ||
} | ||
|
||
public static void StdOutPush(string value) | ||
{ | ||
lock (guarder) | ||
{ | ||
StdOut.Enqueue(value); | ||
} | ||
} | ||
|
||
public static string TraceOutPop() | ||
{ | ||
lock (guarder) | ||
{ | ||
return TraceOut.Dequeue(); | ||
} | ||
} | ||
|
||
public static void TraceOutPush(string value) | ||
{ | ||
lock (guarder) | ||
{ | ||
TraceOut.Enqueue(value); | ||
} | ||
} | ||
|
||
public static void Setup() | ||
{ | ||
new Thread(_setup).Start(); | ||
Thread.Sleep(200); | ||
} | ||
private static void _setup() | ||
{ | ||
Application.Init(); | ||
|
||
|
||
var customColors = new ColorScheme | ||
{ | ||
Normal = Application.Driver.MakeAttribute(Color.White, Color.Black), | ||
Focus = Application.Driver.MakeAttribute(Color.Black, Color.Black), | ||
HotNormal = Application.Driver.MakeAttribute(Color.BrightYellow, Color.Black), | ||
HotFocus = Application.Driver.MakeAttribute(Color.Black, Color.Black), | ||
}; | ||
|
||
var top = Application.Top; | ||
|
||
var win = new Window | ||
{ | ||
X = 0, | ||
Y = 0, | ||
Width = Dim.Fill(), | ||
Height = Dim.Fill(), | ||
ColorScheme = customColors | ||
}; | ||
|
||
win.Border = new Border() | ||
{ | ||
BorderStyle = BorderStyle.None | ||
}; | ||
|
||
top.Add(win); | ||
|
||
var traceOutView = new TextView() | ||
{ | ||
Width = Dim.Fill(), | ||
Height = Dim.Fill(), | ||
ReadOnly = true, | ||
ColorScheme = customColors | ||
}; | ||
|
||
var mainTextViewFrame = new FrameView("Ishtar Trace") | ||
{ | ||
X = 0, | ||
Y = 0, | ||
Width = Dim.Percent(50), | ||
Height = Dim.Fill(), | ||
CanFocus = false, | ||
ColorScheme = customColors | ||
}; | ||
mainTextViewFrame.Add(traceOutView); | ||
|
||
var stdOutView = new TextView() | ||
{ | ||
Width = Dim.Fill(), | ||
Height = Dim.Fill(), | ||
ReadOnly = true, | ||
ColorScheme = customColors | ||
}; | ||
|
||
var upperTextViewFrame = new FrameView("StdOut") | ||
{ | ||
X = Pos.Percent(50) + 1, | ||
Y = 0, | ||
Width = Dim.Fill(), | ||
Height = Dim.Percent(50), | ||
CanFocus = false, | ||
ColorScheme = customColors | ||
}; | ||
upperTextViewFrame.Add(stdOutView); | ||
|
||
var stateView = new TextView() | ||
{ | ||
Width = Dim.Fill(), | ||
Height = Dim.Fill(), | ||
ReadOnly = true, | ||
ColorScheme = customColors | ||
}; | ||
|
||
var tableViewFrame = new FrameView("Ishtar State") | ||
{ | ||
X = Pos.Percent(50) + 1, | ||
Y = Pos.Percent(50) + 1, | ||
Width = Dim.Fill(), | ||
Height = Dim.Fill(), | ||
CanFocus = false, | ||
ColorScheme = customColors | ||
}; | ||
tableViewFrame.Add(stateView); | ||
|
||
win.Add(mainTextViewFrame, upperTextViewFrame, tableViewFrame); | ||
|
||
var updateThread = new Thread(() => | ||
{ | ||
while (true) | ||
{ | ||
bool isSignalled = StdOutHas() || TraceOutHas(); | ||
|
||
if (!isSignalled) | ||
{ | ||
Thread.Sleep(10); | ||
continue; | ||
} | ||
|
||
|
||
ManualResetEvent @event = new ManualResetEvent(true); | ||
Application.MainLoop.Invoke(() => | ||
{ | ||
while (StdOutHas()) | ||
{ | ||
var value = StdOutPop(); | ||
stdOutView.Text += value; | ||
stdOutView.Text += "\n"; | ||
stdOutView.MoveEnd(); | ||
} | ||
|
||
while (TraceOutHas()) | ||
{ | ||
var value = TraceOutPop(); | ||
traceOutView.Text += value; | ||
traceOutView.Text += "\n"; | ||
traceOutView.MoveEnd(); | ||
} | ||
|
||
|
||
stateView.Text = FormatState(GetState()); | ||
|
||
@event.Reset(); | ||
}); | ||
|
||
@event.WaitOne(); | ||
} | ||
}); | ||
|
||
updateThread.Start(); | ||
|
||
Application.Run(); | ||
} | ||
|
||
|
||
|
||
|
||
private static string FormatState(IshtarState state) => | ||
$"Instruction: {state.ip}\nMethod: {state.method}\nCycle: {state.cycleDelay.TotalMilliseconds - 1}ms\n" + | ||
$"StackType: {state.stackType}\nCallDepth: {state.invocationLevel}"; | ||
} | ||
|
||
|
||
public record IshtarState | ||
(string ip, string method, TimeSpan cycleDelay, string stackType, int invocationLevel); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Terminal.Gui" Version="1.17.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
namespace ishtar; | ||
|
||
using System.Diagnostics; | ||
|
||
internal struct IshtarTrace() | ||
{ | ||
private bool useConsole = Environment.GetCommandLineArgs().Contains("--sys::log::use-console=1"); | ||
private bool useFile = Environment.GetCommandLineArgs().Contains("--sys::log::use-file=1"); | ||
|
||
public void println(string s) | ||
[Conditional("DEBUG")] | ||
public void Setup() => IshtarSharedDebugData.Setup(); | ||
Check failure on line 11 in runtime/ishtar.vm/Trace.cs GitHub Actions / build_all (macos-latest, osx-x64, false)
|
||
|
||
[Conditional("DEBUG")] | ||
public void println(string s) => IshtarSharedDebugData.TraceOutPush(s); | ||
Check failure on line 14 in runtime/ishtar.vm/Trace.cs GitHub Actions / build_all (macos-latest, osx-x64, false)
|
||
|
||
|
||
[Conditional("DEBUG")] | ||
public void debug_stdout_write(string s) => IshtarSharedDebugData.StdOutPush(s); | ||
Check failure on line 18 in runtime/ishtar.vm/Trace.cs GitHub Actions / build_all (macos-latest, osx-x64, false)
|
||
|
||
[Conditional("DEBUG")] | ||
public unsafe void signal_state(OpCodeValue ip, CallFrame current, TimeSpan cycleDelay, stackval currentStack) | ||
=> IshtarSharedDebugData.SetState(new IshtarState($"{ip}", current.method->Name, cycleDelay, $"{currentStack.type}", current.level)); | ||
Check failure on line 22 in runtime/ishtar.vm/Trace.cs GitHub Actions / build_all (macos-latest, osx-x64, false)
Check failure on line 22 in runtime/ishtar.vm/Trace.cs GitHub Actions / build_all (macos-latest, osx-x64, false)
Check failure on line 22 in runtime/ishtar.vm/Trace.cs GitHub Actions / build_all (ubuntu-latest, linux-x64, true)
|
||
|
||
public void console_std_write(string s) | ||
{ | ||
//if (useConsole) | ||
{ | ||
Console.WriteLine(s); | ||
} | ||
#if DEBUG | ||
debug_stdout_write(s); | ||
#else | ||
Console.WriteLine(s); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.