diff --git a/src/tools/StressLogAnalyzer/src/InterestingStringFinder.cs b/src/tools/StressLogAnalyzer/src/InterestingStringFinder.cs index f8d5541908898a..4fbb8047358878 100644 --- a/src/tools/StressLogAnalyzer/src/InterestingStringFinder.cs +++ b/src/tools/StressLogAnalyzer/src/InterestingStringFinder.cs @@ -93,7 +93,7 @@ public bool IsInteresting(TargetPointer formatStringPointer, out WellKnownString StringCacheEntry CalculateEntryForString(TargetPointer address) { - string formatString = target.ReadZeroTerminatedUtf8String(address, 1024); + string formatString = target.ReadUtf8String(address); bool defaultInteresting = false; if (_knownStrings.TryGetValue(formatString, out WellKnownString wellKnown)) { @@ -144,7 +144,7 @@ int GetCacheIndex(TargetPointer address) public bool IsWellKnown(TargetPointer formatStringPointer, out WellKnownString wellKnownString) { - return _knownStrings.TryGetValue(target.ReadZeroTerminatedUtf8String(formatStringPointer, 1024), out wellKnownString); + return _knownStrings.TryGetValue(target.ReadUtf8String(formatStringPointer), out wellKnownString); } } } diff --git a/src/tools/StressLogAnalyzer/src/Output/StressMessageWriter.cs b/src/tools/StressLogAnalyzer/src/Output/StressMessageWriter.cs index 296484e6d82c11..fd838e6a2756bb 100644 --- a/src/tools/StressLogAnalyzer/src/Output/StressMessageWriter.cs +++ b/src/tools/StressLogAnalyzer/src/Output/StressMessageWriter.cs @@ -25,7 +25,7 @@ public async Task OutputMessageAsync(ThreadStressLogData thread, StressMsgData m if (writeFormatString) { - string format = target.ReadZeroTerminatedUtf8String(message.FormatString, 1024); + string format = target.ReadUtf8String(message.FormatString); await output.WriteAsync($"***|\"{format}\"|*** ").ConfigureAwait(false); } diff --git a/src/tools/StressLogAnalyzer/src/StressMessageFormatter.cs b/src/tools/StressLogAnalyzer/src/StressMessageFormatter.cs index 7c15c4ca2f2e41..0e05e0215f9f18 100644 --- a/src/tools/StressLogAnalyzer/src/StressMessageFormatter.cs +++ b/src/tools/StressLogAnalyzer/src/StressMessageFormatter.cs @@ -100,7 +100,7 @@ private void FormatAsciiString(TargetPointer ptr, PaddingFormat paddingFormat, S { try { - builder.Append(ReadZeroTerminatedString(ptr, maxLength: 256)); + builder.Append(ReadUtf8String(ptr)); } catch (InvalidOperationException) { @@ -112,7 +112,7 @@ private void FormatUtf16String(TargetPointer ptr, PaddingFormat paddingFormat, S { try { - builder.Append(ReadZeroTerminatedString(ptr, maxLength: 256)); + builder.Append(ReadUtf16String(ptr)); } catch (InvalidOperationException) { @@ -193,32 +193,10 @@ private static void FormatFloatingPoint(TargetPointer valueAsBits, PaddingFormat } } - private unsafe string ReadZeroTerminatedString(TargetPointer pointer, int maxLength) - where T : unmanaged, IBinaryInteger, IMinMaxValue - { - StringBuilder sb = new(); - for (T ch = _target.Read(pointer); - ch != T.Zero; - ch = _target.Read(pointer = new TargetPointer((ulong)pointer + (ulong)sizeof(T)))) - { - if (sb.Length > maxLength) - { - break; - } - - // char implements INumberBase explicitly, so we need to call the helper method to use CreateChecked. - sb.Append(MakeTruncatingHelper(ch)); - } - return sb.ToString(); - - static U MakeTruncatingHelper(T value) where U : INumberBase => U.CreateChecked(value); - } - public string GetFormattedMessage(StressMsgData stressMsg) { Debug.Assert(stressMsg.FormatString != TargetPointer.Null); - TargetPointer nextCharPtr = stressMsg.FormatString; - string formatString = ReadZeroTerminatedString(stressMsg.FormatString, maxLength: 256); + string formatString = ReadUtf8String(stressMsg.FormatString); int currentArg = 0; int startIndex = 0; StringBuilder sb = new(); diff --git a/src/tools/StressLogAnalyzer/src/TargetExtensions.cs b/src/tools/StressLogAnalyzer/src/TargetExtensions.cs deleted file mode 100644 index 96748042a9d986..00000000000000 --- a/src/tools/StressLogAnalyzer/src/TargetExtensions.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.Buffers; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.Diagnostics.DataContractReader; - -namespace StressLogAnalyzer; - -internal static class TargetExtensions -{ - public static unsafe string ReadZeroTerminatedUtf8String(this Target target, TargetPointer pointer, int maxLength) - { - byte[]? rented = null; - Span bytes = maxLength <= 1024 ? stackalloc byte[maxLength] : (rented = ArrayPool.Shared.Rent(maxLength)); - try - { - target.ReadBuffer(pointer, bytes.Slice(0, maxLength)); - return Encoding.UTF8.GetString(bytes.Slice(0, bytes.IndexOf([(byte)0]))); - } - finally - { - if (rented != null) - { - ArrayPool.Shared.Return(rented); - } - } - } -}