Skip to content

Commit

Permalink
Add missing instructions to ProcDecoder (#1866)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Jul 7, 2024
1 parent b4b9dfe commit 5613d62
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 58 deletions.
4 changes: 2 additions & 2 deletions DMCompiler/Bytecode/DreamProcOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,12 @@ public override string ToString() {
case Type.Global:
case Type.Argument:
case Type.GlobalProc:
return $"{RefType} {Index}";
return $"{RefType}({Index})";

case Type.SrcField:
case Type.Field:
case Type.SrcProc:
return $"{RefType} \"{Name}\"";
return $"{RefType}(\"{Name}\")";

default: return RefType.ToString();
}
Expand Down
24 changes: 1 addition & 23 deletions OpenDreamRuntime/Procs/DebugAdapter/DebugAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using System.Net.Sockets;
using System.Net.Sockets;
using OpenDreamRuntime.Procs.DebugAdapter.Protocol;

namespace OpenDreamRuntime.Procs.DebugAdapter;
Expand All @@ -9,38 +8,17 @@ public sealed class DebugAdapter {

public event OnClientConnectedHandler? OnClientConnected;

private TcpListener? _listener;
private readonly List<DebugAdapterClient> _clients = new();

public DebugAdapter() {
}

public bool AnyClientsConnected() => _clients.Count > 0;

public void StartListening(string? host = null, int? port = null) {
if (!IPAddress.TryParse(host, out IPAddress? hostAddress)) {
hostAddress = IPAddress.Any;
}
_listener = new TcpListener(hostAddress, port ?? 25567);
_listener.Start();
}

public void ConnectOut(string? host = null, int? port = null) {
var client = new DebugAdapterClient(new TcpClient(host ?? "127.0.0.1", port ?? 25567));
_clients.Add(client);
OnClientConnected?.Invoke(client);
}

public void HandleMessages() {
if (_listener != null) {
while (_listener.Pending()) {
var client = new DebugAdapterClient(_listener.AcceptTcpClient());

_clients.Add(client);
OnClientConnected?.Invoke(client);
}
}

for (int i = 0; i < _clients.Count; i++) {
DebugAdapterClient client = _clients[i];

Expand Down
4 changes: 2 additions & 2 deletions OpenDreamRuntime/Procs/DebugAdapter/DebugAdapterClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private sealed class ProtocolHeader {
int contentLength = -1;

string? headerLine;
while ((headerLine = _netReader.ReadLine()) != String.Empty && headerLine != null) {
while ((headerLine = _netReader.ReadLine()) != string.Empty && headerLine != null) {
string[] split = headerLine.Split(": ");
string field = split[0];
string value = split[1];
Expand Down Expand Up @@ -144,7 +144,7 @@ private sealed class ProtocolHeader {
return null;
}

string content = new String(buffer);
string content = new string(buffer);
_sawmill.Log(LogLevel.Verbose, $"Received {contentLength} {content}");

return content;
Expand Down
40 changes: 23 additions & 17 deletions OpenDreamRuntime/Procs/DebugAdapter/DreamDebugManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ internal sealed class DreamDebugManager : IDreamDebugManager {
// Setup
private DebugAdapter? _adapter;
private string RootPath => _resourceManager.RootPath ?? throw new Exception("No RootPath yet!");
private bool _stopOnEntry = false;
private bool _stopOnEntry;

// State
private bool _stopped = false;
private bool _terminated = false;
private bool _stopped;
private bool _terminated;

public enum StepMode {
StepOver, // aka "next"
Expand All @@ -43,16 +43,12 @@ public struct ThreadStepMode {
private const string ExceptionFilterRuntimes = "runtimes";
private bool _breakOnRuntimes = true;

private sealed class FileBreakpointSlot {
private sealed class FileBreakpointSlot(DMProc proc) {
public IReadOnlyList<ActiveBreakpoint> Breakpoints => _breakpoints;
public readonly DMProc Proc;
public readonly DMProc Proc = proc;

private readonly List<ActiveBreakpoint> _breakpoints = new();

public FileBreakpointSlot(DMProc proc) {
Proc = proc;
}

public void ClearBreakpoints() {
// Set all the opcodes back to their original
foreach (var breakpoint in _breakpoints) {
Expand Down Expand Up @@ -96,8 +92,9 @@ private struct ActiveBreakpoint {

private readonly Dictionary<int, WeakReference<ProcState>> _stackFramesById = new();

private int _variablesIdCounter = 0;
private int _variablesIdCounter;
private readonly Dictionary<int, Func<RequestVariables, IEnumerable<Variable>>> _variableReferences = new();

private int AllocVariableRef(Func<RequestVariables, IEnumerable<Variable>> func) {
int id = ++_variablesIdCounter;
_variableReferences[id] = func;
Expand All @@ -110,7 +107,6 @@ public void Initialize(int port) {
_adapter = new DebugAdapter();

_adapter.OnClientConnected += OnClientConnected;
//_adapter.StartListening();
_adapter.ConnectOut(port: port);
}

Expand Down Expand Up @@ -542,9 +538,11 @@ private void HandleRequestThreads(DebugAdapterClient client, RequestThreads reqT
foreach (var thread in InspectThreads().Distinct()) {
threads.Add(new Thread(thread.Id, thread.Name));
}

if (!threads.Any()) {
threads.Add(new Thread(0, "Nothing"));
}

reqThreads.Respond(client, threads);
}

Expand Down Expand Up @@ -636,6 +634,7 @@ private void HandleRequestStackTrace(DebugAdapterClient client, RequestStackTrac
output.Add(outputFrame);
_stackFramesById[frame.Id] = new WeakReference<ProcState>(frame);
}

reqStackTrace.Respond(client, output, output.Count);
}

Expand Down Expand Up @@ -673,6 +672,7 @@ private void HandleRequestScopes(DebugAdapterClient client, RequestScopes reques
PresentationHint = Scope.PresentationHintRegisters,
});
}

scopes.Add(new Scope {
Name = "Arguments",
PresentationHint = Scope.PresentationHintArguments,
Expand All @@ -695,7 +695,9 @@ private IEnumerable<Variable> ExpandArguments(RequestVariables req, DMProcState
if (dmFrame.Proc.OwningType != _objectTree.Root) {
yield return DescribeValue("src", new(dmFrame.Instance));
}

yield return DescribeValue("usr", new(dmFrame.Usr));

foreach (var (name, value) in dmFrame.DebugArguments()) {
yield return DescribeValue(name, value);
}
Expand All @@ -721,6 +723,7 @@ private IEnumerable<Variable> ExpandStack(RequestVariables req, ReadOnlyMemory<D

private Variable DescribeValue(string name, DreamValue value) {
var varDesc = new Variable { Name = name, Value = value.ToString() };

if (value.TryGetValueAsDreamList(out var list)) {
if (list.GetLength() > 0) {
varDesc.VariablesReference = AllocVariableRef(req => ExpandList(req, list));
Expand All @@ -730,10 +733,10 @@ private Variable DescribeValue(string name, DreamValue value) {
varDesc.VariablesReference = AllocVariableRef(req => ExpandObject(req, obj));
varDesc.NamedVariables = obj.ObjectDefinition?.Variables.Count;
}

return varDesc;
}


private IEnumerable<Variable> ExpandList(RequestVariables req, DreamList list) {
if (list.IsAssociative) {
var assoc = list.GetAssociativeValues();
Expand All @@ -752,6 +755,7 @@ private IEnumerable<Variable> ExpandList(RequestVariables req, DreamList list) {
private IEnumerable<Variable> ExpandObject(RequestVariables req, DreamObject obj) {
foreach (var name in obj.GetVariableNames().OrderBy(k => k)) {
Variable described;

try {
described = DescribeValue(name, obj.GetVariable(name));
} catch (Exception ex) {
Expand All @@ -762,6 +766,7 @@ private IEnumerable<Variable> ExpandObject(RequestVariables req, DreamObject obj
Value = $"<error: {ex.Message}>",
};
}

yield return described;
}
}
Expand All @@ -786,6 +791,7 @@ private void HandleRequestDisassemble(DebugAdapterClient client, RequestDisassem
} else {
requestDisassemble.Respond(client, Enumerable.Repeat(LowInstruction, requestDisassemble.Arguments.InstructionCount));
}

return;
}

Expand All @@ -794,9 +800,9 @@ private void HandleRequestDisassemble(DebugAdapterClient client, RequestDisassem
DisassembledInstruction? previousInstruction = null;
int previousOffset = 0;
foreach (var (offset, instruction) in new ProcDecoder(_objectTree.Strings, proc.Bytecode).Disassemble()) {
if (previousInstruction != null) {
/*if (previousInstruction != null) {
previousInstruction.InstructionBytes = BitConverter.ToString(proc.Bytecode, previousOffset, offset - previousOffset).Replace("-", " ").ToLowerInvariant();
}
}*/

previousOffset = offset;
previousInstruction = new DisassembledInstruction {
Expand All @@ -811,9 +817,9 @@ private void HandleRequestDisassemble(DebugAdapterClient client, RequestDisassem
output.Add(previousInstruction);
}

if (previousInstruction != null) {
/*if (previousInstruction != null) {
previousInstruction.InstructionBytes = BitConverter.ToString(proc.Bytecode, previousOffset).Replace("-", " ").ToLowerInvariant();
}
}*/

// ... and THEN strip everything outside the requested range.
int requestedPoint = output.FindIndex(di => di.Address == requestDisassemble.Arguments.MemoryReference);
Expand Down Expand Up @@ -841,7 +847,7 @@ private string EncodeInstructionPointer(DMProc proc, int pc) {
// Otherwise it will think they're all the same and never refetch.
int procId = proc.GetHashCode();
_disassemblyProcs[procId] = proc;
ulong ip = ((ulong)(uint)procId << 32) | (ulong)(uint)pc;
ulong ip = ((ulong)(uint)procId << 32) | (uint)pc;
return "0x" + ip.ToString("x");
}

Expand Down
Loading

0 comments on commit 5613d62

Please sign in to comment.