Skip to content

Commit

Permalink
Merge pull request #253 from vein-lang/features/debug-terminal
Browse files Browse the repository at this point in the history
add debug terminal
  • Loading branch information
0xF6 authored Jun 30, 2024
2 parents 2a61399 + 806a50b commit 543e14d
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 44 deletions.
232 changes: 232 additions & 0 deletions lib/ishtar.debug.console/IshtarSharedDebugData.cs
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);
14 changes: 14 additions & 0 deletions lib/ishtar.debug.console/ishtar.debug.console.csproj
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>
9 changes: 5 additions & 4 deletions runtime/ishtar.vm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@

if (!frame->exception.IsDefault())
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"unhandled exception '{frame->exception.value->clazz->Name}' was thrown. \n" +
vm.trace.println($"unhandled exception '{frame->exception.value->clazz->Name}' was thrown. \n" +
$"{frame->exception.GetStackTrace()}");
Console.ForegroundColor = ConsoleColor.White;
}

watcher.Stop();
Console.WriteLine($"Elapsed: {watcher.Elapsed}");
vm.trace.println($"Elapsed: {watcher.Elapsed}");
frame->Dispose();
vm.Dispose();

vm.trace.println($"Press ENTER to exit...");

Console.ReadKey();
return 0;
}
27 changes: 22 additions & 5 deletions runtime/ishtar.vm/Trace.cs
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

View workflow job for this annotation

GitHub Actions / build_all (macos-latest, osx-x64, false)

The name 'IshtarSharedDebugData' does not exist in the current context

Check failure on line 11 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (ubuntu-latest, linux-x64, true)

The name 'IshtarSharedDebugData' does not exist in the current context

[Conditional("DEBUG")]
public void println(string s) => IshtarSharedDebugData.TraceOutPush(s);

Check failure on line 14 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (macos-latest, osx-x64, false)

The name 'IshtarSharedDebugData' does not exist in the current context

Check failure on line 14 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (ubuntu-latest, linux-x64, true)

The name 'IshtarSharedDebugData' does not exist in the current context


[Conditional("DEBUG")]
public void debug_stdout_write(string s) => IshtarSharedDebugData.StdOutPush(s);

Check failure on line 18 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (macos-latest, osx-x64, false)

The name 'IshtarSharedDebugData' does not exist in the current context

Check failure on line 18 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (ubuntu-latest, linux-x64, true)

The name 'IshtarSharedDebugData' does not exist in the current context

[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

View workflow job for this annotation

GitHub Actions / build_all (macos-latest, osx-x64, false)

The name 'IshtarSharedDebugData' does not exist in the current context

Check failure on line 22 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (macos-latest, osx-x64, false)

The type or namespace name 'IshtarState' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 22 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (ubuntu-latest, linux-x64, true)

The name 'IshtarSharedDebugData' does not exist in the current context

Check failure on line 22 in runtime/ishtar.vm/Trace.cs

View workflow job for this annotation

GitHub Actions / build_all (ubuntu-latest, linux-x64, true)

The type or namespace name 'IshtarState' could not be found (are you missing a using directive or an assembly reference?)

public void console_std_write(string s)
{
//if (useConsole)
{
Console.WriteLine(s);
}
#if DEBUG
debug_stdout_write(s);
#else
Console.WriteLine(s);
#endif
}
}
26 changes: 24 additions & 2 deletions runtime/ishtar.vm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public static VirtualMachine Create(string name)
vm.Config = new VMConfig();
vm.Vault = new AppVault(vm, name);
vm.trace = new IshtarTrace();

vm.trace.Setup();

vm.Types = IshtarTypes.Create(vm.Vault);
vm.GC = new IshtarGC(vm);

Expand Down Expand Up @@ -159,7 +162,15 @@ public void FastFail(bool assert, WNE type, string msg, CallFrame* frame)
[Conditional("DEBUG")]
public void println(string str) => trace.println(str);

public void halt(int exitCode = -1) => Environment.Exit(exitCode);
public void halt(int exitCode = -1)
{
#if DEBUG
trace.println($"exit code is {exitCode}");
trace.println("Press ENTER to exit...");
while (System.Console.ReadKey().Key != ConsoleKey.Enter) Thread.Sleep(1);
#endif
Environment.Exit(exitCode);
}


public void exec_method_external_native(CallFrame* frame)
Expand Down Expand Up @@ -324,7 +335,9 @@ void ForceThrow(RuntimeIshtarClass* clazz)
sp->data.p = (nint)GC.AllocObject(clazz, invocation);
sp->type = TYPE_CLASS;
}


var stopwatch = new Stopwatch();

while (true)
{
vm_cycle_start:
Expand All @@ -340,6 +353,15 @@ void ForceThrow(RuntimeIshtarClass* clazz)
if (!assert_violation_zone_writes(invocation, stack, STACK_VIOLATION_LEVEL_SIZE))
continue;

Thread.Sleep(1);

if (stopwatch.IsRunning)
{
stopwatch.Stop();
trace.signal_state(invocation->last_ip, *invocation, stopwatch.Elapsed, *sp);
}

stopwatch.Restart();

switch (invocation->last_ip)
{
Expand Down
Loading

0 comments on commit 543e14d

Please sign in to comment.