Skip to content

Commit a840b34

Browse files
committed
Clean up stack traces by removing useless info and shortening paths
1 parent 346c842 commit a840b34

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Source/Client/Desyncs/StackTraceLogItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public override string StackTraceString
6464
if (!methodNameCache.TryGetValue(addr, out string method))
6565
methodNameCache[addr] = method = Native.MethodNameFromAddr(raw[i], false);
6666

67-
builder.AppendLine(method != null ? SyncCoordinator.MethodNameWithIL(method) : "Null");
67+
builder.AppendLine(method != null ? SyncCoordinator.CleanedMethodName(method) : "Null");
6868
}
6969

7070
return builder.ToString();

Source/Client/Desyncs/SyncCoordinator.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Text.RegularExpressions;
56
using RimWorld;
67
using Verse;
78
using Multiplayer.Client.Desyncs;
@@ -242,6 +243,44 @@ public void TryAddStackTraceForDesyncLogRaw(StackTraceLogItemRaw item, int depth
242243
OpinionInBuilding.desyncStackTraceHashes.Add(hash);
243244
}
244245

246+
private static readonly Regex RemoveUnknownSourceInfoRegex = new(@"in <([a-zA-Z0-9]+)>:0");
247+
public static string CleanedMethodName(string rawName)
248+
{
249+
// at (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.RimWorld.UniqueIDsManager.GetNextID_Patch2 ...
250+
// =>
251+
// at (wrapper dynamic-method) RimWorld.UniqueIDsManager.GetNextID_Patch2 ...
252+
rawName = rawName.Replace("MonoMod.Utils.DynamicMethodDefinition.", string.Empty);
253+
254+
// Performs the transformation only if the source file is unknown, meaning random id instead of a path
255+
// at Verse.AI.JobDriver.ReadyForNextToil () [0x00000] in <c847e073cda54790b59d58357cc8cf98>:0
256+
// =>
257+
// at Verse.AI.JobDriver.ReadyForNextToil () [0x00000]
258+
rawName = RemoveUnknownSourceInfoRegex.Replace(rawName, string.Empty);
259+
return ShortenSourceInfo(rawName);
260+
}
261+
262+
263+
private static string ShortenSourceInfo(string rawName)
264+
{
265+
// at Multiplayer.Client.Desyncs.DeferredStackTracing.Postfix () [0x00045] in /home/runner/work/Multiplayer/Multiplayer/Source/Client/Desyncs/DeferredStackTracing.cs:41
266+
// =>
267+
// at Multiplayer.Client.Desyncs.DeferredStackTracing.Postfix () [0x00045] in /Client/Desyncs/DeferredStackTracing.cs:41
268+
const string sourcePrefix = "in ";
269+
const string realSourceStartA = "Multiplayer/Source";
270+
const string realSourceStartB = "Multiplayer\\Source";
271+
var startSource2 = rawName.IndexOf(sourcePrefix, StringComparison.Ordinal);
272+
if (startSource2 != -1)
273+
{
274+
var offset = startSource2 + sourcePrefix.Length;
275+
var realSourceStartIndex = rawName.IndexOf(realSourceStartA, offset, StringComparison.Ordinal);
276+
if (realSourceStartIndex == -1)
277+
realSourceStartIndex = rawName.IndexOf(realSourceStartB, offset, StringComparison.Ordinal);
278+
if (realSourceStartIndex != -1)
279+
rawName = rawName.Remove(offset, realSourceStartIndex - offset + realSourceStartA.Length);
280+
}
281+
return rawName;
282+
}
283+
245284
public static string MethodNameWithIL(string rawName)
246285
{
247286
// Note: The names currently don't include IL locations so the code is commented out

0 commit comments

Comments
 (0)