-
Notifications
You must be signed in to change notification settings - Fork 383
Reimplement !verifyobj and !dumpruntimetypes #3811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1bd44ed
Update ClrMD Version
leculver b55f3cf
Add C# based VerifyHeap command
leculver 2193764
Fix verifyobj command impl
leculver b413de9
Add !sos dumpruntimetypes
leculver 4a7260b
Remove dead code
leculver c92ccc2
Remove object verification code
leculver 8b7644d
Remove last use of RefIterator
leculver 8354d26
Remove RefIterator
leculver e748791
Test updates
leculver 5eb6847
Update src/Microsoft.Diagnostics.ExtensionCommands/ExtensionMethodHel…
leculver eada317
Update src/Microsoft.Diagnostics.ExtensionCommands/VerifyObjectComman…
leculver c9f47b8
Fix managed commands
leculver 9d14053
Update soscommand.cpp
leculver 0670bb6
Merge branch 'main' into verifyObj
leculver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/Microsoft.Diagnostics.ExtensionCommands/DumpObjGCRefsHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // 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.Linq; | ||
| using System.Text; | ||
| using Microsoft.Diagnostics.DebugServices; | ||
| using Microsoft.Diagnostics.Runtime; | ||
| using static Microsoft.Diagnostics.ExtensionCommands.TableOutput; | ||
|
|
||
| namespace Microsoft.Diagnostics.ExtensionCommands | ||
| { | ||
| [Command(Name = "dumpobjgcrefs", Help = "A helper command to implement !dumpobj -refs")] | ||
| public sealed class DumpObjGCRefsHelper : CommandBase | ||
| { | ||
| [ServiceImport] | ||
| public ClrRuntime Runtime { get; set; } | ||
|
|
||
| [Argument(Name = "object")] | ||
| public string ObjectAddress { get; set; } | ||
|
|
||
| public override void Invoke() | ||
| { | ||
| if (!TryParseAddress(ObjectAddress, out ulong objAddress)) | ||
| { | ||
| throw new ArgumentException($"Invalid object address: '{ObjectAddress}'", nameof(ObjectAddress)); | ||
| } | ||
|
|
||
| ClrObject obj = Runtime.Heap.GetObject(objAddress); | ||
| if (!obj.IsValid) | ||
| { | ||
| Console.WriteLine($"Unable to walk object references, invalid object."); | ||
| return; | ||
| } | ||
|
|
||
| ClrReference[] refs = obj.EnumerateReferencesWithFields(carefully: false, considerDependantHandles: false).ToArray(); | ||
| if (refs.Length == 0 ) | ||
| { | ||
| Console.WriteLine("GC Refs: none"); | ||
| return; | ||
| } | ||
|
|
||
| Console.WriteLine("GC Refs:"); | ||
|
|
||
| int fieldNameLen = Math.Max(refs.Max(r => GetFieldName(r)?.Length ?? 0), 5); | ||
| int offsetLen = Math.Max(refs.Max(r => r.Offset.ToSignedHexString().Length), 6); | ||
|
|
||
| TableOutput output = new(Console, (fieldNameLen, ""), (offsetLen, ""), (16, "x12")); | ||
| output.WriteRow("Field", "Offset", "Object", "Type"); | ||
| foreach (ClrReference objRef in refs) | ||
| { | ||
| output.WriteRow(GetFieldName(objRef), objRef.Offset, new DmlDumpObj(objRef.Object), objRef.Object.Type?.Name); | ||
| } | ||
| } | ||
|
|
||
| private static string GetFieldName(ClrReference objRef) | ||
| { | ||
| if (objRef.Field is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (objRef.InnerField is null) | ||
| { | ||
| return objRef.Field?.Name; | ||
| } | ||
|
|
||
| StringBuilder sb = new(260); | ||
| bool foundOneFieldName = false; | ||
|
|
||
| for (ClrReference? curr = objRef; curr.HasValue; curr = curr.Value.InnerField) | ||
| { | ||
| if (sb.Length > 0) | ||
| { | ||
| sb.Append('.'); | ||
| } | ||
|
|
||
| string fieldName = curr.Value.Field?.Name; | ||
| if (string.IsNullOrWhiteSpace(fieldName)) | ||
| { | ||
| sb.Append("???"); | ||
| } | ||
| else | ||
| { | ||
| sb.Append(fieldName); | ||
| foundOneFieldName = true; | ||
| } | ||
| } | ||
|
|
||
| // Make sure we don't just return "???.???.???" | ||
| return foundOneFieldName ? sb.ToString() : null; | ||
| } | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
src/Microsoft.Diagnostics.ExtensionCommands/DumpRuntimeTypeCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Diagnostics.DebugServices; | ||
| using Microsoft.Diagnostics.Runtime; | ||
| using static Microsoft.Diagnostics.ExtensionCommands.TableOutput; | ||
|
|
||
| namespace Microsoft.Diagnostics.ExtensionCommands | ||
| { | ||
| [Command(Name = "dumpruntimetypes", Help = "Finds all System.RuntimeType objects in the GC heap and prints the type name and MethodTable they refer too.")] | ||
| public sealed class DumpRuntimeTypeCommand : CommandBase | ||
| { | ||
| [ServiceImport] | ||
| public ClrRuntime Runtime { get; set; } | ||
|
|
||
| public override void Invoke() | ||
| { | ||
| TableOutput output = null; | ||
|
|
||
| foreach (ClrObject runtimeType in Runtime.Heap.EnumerateObjects()) | ||
| { | ||
| Console.CancellationToken.ThrowIfCancellationRequested(); | ||
| if (!runtimeType.IsValid || !runtimeType.IsRuntimeType) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (!runtimeType.TryReadField("m_handle", out nuint m_handle)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| ClrAppDomain domain = null; | ||
| string typeName; | ||
| bool isMethodTable = (m_handle & 2) == 0; | ||
| if (isMethodTable) | ||
| { | ||
| // Only lookup the type if we have a MethodTable. | ||
| ClrType type = Runtime.GetTypeByMethodTable(m_handle); | ||
| typeName = type?.Name ?? $"methodtable: {m_handle:x}"; | ||
| domain = type?.Module?.AppDomain; | ||
| } | ||
| else | ||
| { | ||
| typeName = $"typehandle: {m_handle:x} (SOS does not support resolving typehandle names.)"; | ||
| } | ||
|
|
||
| if (output is null) | ||
| { | ||
| output = new(Console, (16, "x12"), (16, "x12"), (16, "x12")); | ||
| output.WriteRow("Address", "Domain", "MT", "Type Name"); | ||
| } | ||
|
|
||
| output.WriteRow(new DmlDumpObj(runtimeType.Address), | ||
| domain is not null ? new DmlDumpDomain(domain.Address) : null, | ||
| isMethodTable ? new DmlDumpMT(m_handle) : m_handle, | ||
| typeName); | ||
| } | ||
|
|
||
| if (output is null) | ||
| { | ||
| Console.WriteLine("No System.RuntimeType objects found."); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Microsoft.Diagnostics.ExtensionCommands/VerifyObjectCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // 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.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.Diagnostics.DebugServices; | ||
| using Microsoft.Diagnostics.Runtime; | ||
|
|
||
| namespace Microsoft.Diagnostics.ExtensionCommands | ||
| { | ||
| [Command(Name = "verifyobj", Help = "Checks the given object for signs of corruption.")] | ||
| public sealed class VerifyObjectCommand : CommandBase | ||
| { | ||
| [ServiceImport] | ||
| public ClrRuntime Runtime { get; set; } | ||
|
|
||
| [ServiceImport] | ||
| public IMemoryService Memory { get; set; } | ||
|
|
||
| [Argument(Name = "ObjectAddress", Help = "The object to verify.")] | ||
| public string ObjectAddress { get; set; } | ||
|
|
||
| public override void Invoke() | ||
| { | ||
| if (!TryParseAddress(ObjectAddress, out ulong objAddress)) | ||
| { | ||
| throw new ArgumentException($"Invalid object address: '{ObjectAddress}'", nameof(ObjectAddress)); | ||
| } | ||
|
|
||
| bool isNotCorrupt = Runtime.Heap.FullyVerifyObject(objAddress, out IEnumerable<ObjectCorruption> corruptionEnum); | ||
| if (isNotCorrupt) | ||
| { | ||
| Console.WriteLine($"object {objAddress:x} is a valid object"); | ||
leculver marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| ObjectCorruption[] corruption = corruptionEnum.OrderBy(r => r.Offset).ToArray(); | ||
| int offsetColWidth = Math.Max(6, corruption.Max(r => r.Offset.ToSignedHexString().Length)); | ||
| int kindColWidth = Math.Max(5, corruption.Max(ce => ce.Kind.ToString().Length)); | ||
|
|
||
| TableOutput output = new(Console, (offsetColWidth, ""), (kindColWidth, "")) | ||
| { | ||
| AlignLeft = true, | ||
| }; | ||
|
|
||
| output.WriteRow("Offset", "Issue", "Description"); | ||
| foreach (ObjectCorruption oc in corruption) | ||
| { | ||
| output.WriteRow(oc.Offset.ToSignedHexString(), oc.Kind, VerifyHeapCommand.GetObjectCorruptionMessage(Memory, Runtime.Heap, oc)); | ||
| } | ||
|
|
||
| Console.WriteLine(); | ||
| Console.WriteLine($"{corruption.Length:n0} error{(corruption.Length == 1 ? "" : "s")} detected."); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.