Skip to content

Commit

Permalink
use StringBuilder; cancel catch at result stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Hecate2 committed Oct 10, 2024
1 parent 70b571d commit febd01d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
39 changes: 19 additions & 20 deletions Fairy.Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System.Text;

namespace Neo.Plugins
{
Expand Down Expand Up @@ -125,45 +126,43 @@ private JObject DumpDebugResultJson(JObject json, FairyEngine newEngine, BreakRe
json["exception"] = GetExceptionMessage(newEngine.FaultException);
if (json["exception"] != null)
{
string traceback = $"CallingScriptHash={newEngine.CallingScriptHash}\r\nCurrentScriptHash={newEngine.CurrentScriptHash}\r\nEntryScriptHash={newEngine.EntryScriptHash}\r\n";
traceback += newEngine.FaultException.StackTrace;
StringBuilder traceback = new();
traceback.Append($"CallingScriptHash={newEngine.CallingScriptHash}\r\nCurrentScriptHash={newEngine.CurrentScriptHash}\r\nEntryScriptHash={newEngine.EntryScriptHash}\r\n");
traceback.Append(newEngine.FaultException.StackTrace);
foreach (Neo.VM.ExecutionContext context in newEngine.InvocationStack.Reverse())
{
UInt160 contextScriptHash = context.GetScriptHash();
//try
{
if (contractScriptHashToAllInstructionPointerToSourceLineNum.ContainsKey(contextScriptHash) && contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash].ContainsKey((uint)context.InstructionPointer))
{
string sourceCodeTraceback = "";
SourceFilenameAndLineNum sourceCode = contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash][(uint)context.InstructionPointer];
sourceCodeTraceback += $"\r\nFile {sourceCode.sourceFilename}, line {sourceCode.lineNum}: {sourceCode.sourceContent}";
traceback += sourceCodeTraceback;
traceback.Append($"\r\nFile {sourceCode.sourceFilename}, line {sourceCode.lineNum}: {sourceCode.sourceContent}");
}
}
//catch (Exception _) {; }
traceback += $"\r\n\tScriptHash={contextScriptHash}, InstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length}";
traceback.Append($"\r\n\tScriptHash={contextScriptHash}, InstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length}");
}
traceback += $"\r\n{json["exception"]!.GetString()}";
traceback.Append($"\r\n{json["exception"]!.GetString()}");

if (!logs.IsEmpty)
{
traceback += $"\r\n-------Logs-------({logs.Count})";
}
traceback.Append($"\r\n-------Logs-------({logs.Count})");

foreach (LogEventArgs log in logs)
{
string contractName = NativeContract.ContractManagement.GetContract(newEngine.Snapshot, log.ScriptHash).Manifest.Name;
traceback += $"\r\n[{log.ScriptHash}] {contractName}: {log.Message}";
traceback.Append($"\r\n[{log.ScriptHash}] {contractName}: {log.Message}");
}
json["traceback"] = traceback;
}
try
{
json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems)));
}
catch (InvalidOperationException)
{
json["stack"] = "error: invalid operation";
json["traceback"] = traceback.ToString();
}
//try
//{
json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems)));
//}
//catch (InvalidOperationException)
//{
// json["stack"] = "error: invalid operation";
//}
return json;
}

Expand Down
42 changes: 21 additions & 21 deletions Fairy.Tester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Neo.Wallets;
using System.Collections.Concurrent;
using System.Numerics;
using System.Text;

namespace Neo.Plugins
{
Expand Down Expand Up @@ -144,11 +145,11 @@ private JObject GetInvokeResultWithSession(string session, bool writeSnapshot, b
json["exception"] = GetExceptionMessage(newEngine.FaultException);
if (json["exception"] != null)
{
string traceback = "";
try { if (newEngine.CallingScriptHash != null) traceback += $"CallingScriptHash={newEngine.CallingScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CallingScriptHash)?.Manifest.Name}]\r\n"; } catch { }
try { traceback += $"CurrentScriptHash={newEngine.CurrentScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CurrentScriptHash)?.Manifest.Name}]\r\n"; } catch { }
try { traceback += $"EntryScriptHash={newEngine.EntryScriptHash}\r\n"; } catch { }
traceback += newEngine.FaultException.StackTrace;
StringBuilder traceback = new();
try { if (newEngine.CallingScriptHash != null) traceback.Append($"CallingScriptHash={newEngine.CallingScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CallingScriptHash)?.Manifest.Name}]\r\n"); } catch { }
try { traceback.Append($"CurrentScriptHash={newEngine.CurrentScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CurrentScriptHash)?.Manifest.Name}]\r\n"); } catch { }
try { traceback.Append($"EntryScriptHash={newEngine.EntryScriptHash}\r\n"); } catch { }
traceback.Append(newEngine.FaultException.StackTrace);
foreach (Neo.VM.ExecutionContext context in newEngine.InvocationStack.Reverse())
{
UInt160 contextScriptHash = context.GetScriptHash();
Expand All @@ -160,33 +161,32 @@ private JObject GetInvokeResultWithSession(string session, bool writeSnapshot, b
string sourceCodeTraceback = "";
SourceFilenameAndLineNum sourceCode = contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash][(uint)context.InstructionPointer];
sourceCodeTraceback += $"\r\nFile {sourceCode.sourceFilename}, line {sourceCode.lineNum}: {sourceCode.sourceContent}";
traceback += sourceCodeTraceback;
traceback.Append(sourceCodeTraceback);
}
}
//catch (Exception _) {; }
traceback += $"\r\n\tInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length} {contextScriptHash}[{contextContractName}]";
traceback.Append($"\r\n\tInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length} {contextScriptHash}[{contextContractName}]");
}
traceback += $"\r\n{json["exception"]!.GetString()}";
traceback.Append($"\r\n{json["exception"]!.GetString()}");

if (!logs.IsEmpty)
{
traceback += $"\r\n-------Logs-------({logs.Count})";
}
traceback.Append($"\r\n-------Logs-------({logs.Count})");

foreach (LogEventArgs log in logs)
{
string? contractName = NativeContract.ContractManagement.GetContract(newEngine.Snapshot, log.ScriptHash)?.Manifest.Name;
traceback += $"\r\n[{log.ScriptHash}] {contractName}: {log.Message}";
traceback.Append($"\r\n[{log.ScriptHash}] {contractName}: {log.Message}");
}
json["traceback"] = traceback;
}
try
{
json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems)));
}
catch (InvalidOperationException)
{
json["stack"] = "error: invalid operation";
json["traceback"] = traceback.ToString();
}
//try
//{
json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems)));
//}
//catch (InvalidOperationException)
//{
// json["stack"] = "error: invalid operation";
//}
if (newEngine.State != VMState.FAULT)
{
if (witnesses == null)
Expand Down

0 comments on commit febd01d

Please sign in to comment.