-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Adding variable tracking throughout assembly printout #2067
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a2312f5
Decorate ReadyToRun dissassembly with NativeVarInfo
edkazcarlson 28a1382
Some changes I wanted
cshung 005135b
Merge pull request #2 from cshung/patches
edkazcarlson 8b49cc0
backup
edkazcarlson ef96d17
change from tuple to valuetuple
edkazcarlson eabe8f9
Update ILSpy.ReadyToRun/ReadyToRunLanguage.cs
edkazcarlson f9562d6
Fix build.
siegfriedpammer 16585ee
Merge branch 'master' into debugInfo
siegfriedpammer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,10 +103,85 @@ public override void WriteCommentLine(ITextOutput output, string comment) | |
| output.WriteLine("; " + comment); | ||
| } | ||
|
|
||
| private Dictionary<VarLocType, HashSet<(DebugInfo debugInfo, NativeVarInfo varLoc)>> WriteDebugInfo(ReadyToRunMethod readyToRunMethod, ITextOutput output) | ||
| { | ||
| Dictionary<VarLocType, HashSet<(DebugInfo debugInfo, NativeVarInfo varLoc)>> debugInfoDict = new Dictionary<VarLocType, HashSet<(DebugInfo debugInfo, NativeVarInfo varLoc)>>(); | ||
| IReadOnlyList<RuntimeFunction> runTimeList = readyToRunMethod.RuntimeFunctions; | ||
| foreach (RuntimeFunction runtimeFunction in runTimeList) { | ||
| DebugInfo debugInfo = runtimeFunction.DebugInfo; | ||
| if (debugInfo != null && debugInfo.BoundsList.Count > 0) { | ||
| for (int i = 0; i < debugInfo.VariablesList.Count; ++i) { | ||
| var varLoc = debugInfo.VariablesList[i]; | ||
| try { | ||
| var typeSet = new HashSet<ValueTuple<DebugInfo, NativeVarInfo>>(); | ||
| bool found = debugInfoDict.TryGetValue(varLoc.VariableLocation.VarLocType, out typeSet); | ||
| if (found) { | ||
| (DebugInfo debugInfo, NativeVarInfo varLoc) newTuple = (debugInfo, varLoc); | ||
| typeSet.Add(newTuple); | ||
| } else { | ||
| typeSet = new HashSet<ValueTuple<DebugInfo, NativeVarInfo>>(); | ||
| debugInfoDict.Add(varLoc.VariableLocation.VarLocType, typeSet); | ||
| (DebugInfo debugInfo, NativeVarInfo varLoc) newTuple = (debugInfo, varLoc); | ||
| typeSet.Add(newTuple); | ||
| } | ||
| } catch (ArgumentNullException) { | ||
| output.WriteLine("Failed to find hash set of Debug info type"); | ||
| } | ||
|
|
||
| if (varLoc.VariableLocation.VarLocType != VarLocType.VLT_REG && varLoc.VariableLocation.VarLocType != VarLocType.VLT_STK | ||
| && varLoc.VariableLocation.VarLocType != VarLocType.VLT_STK_BYREF) { | ||
| output.WriteLine($" Variable Number: {varLoc.VariableNumber}"); | ||
| output.WriteLine($" Start Offset: 0x{varLoc.StartOffset:X}"); | ||
| output.WriteLine($" End Offset: 0x{varLoc.EndOffset:X}"); | ||
| output.WriteLine($" Loc Type: {varLoc.VariableLocation.VarLocType}"); | ||
| switch (varLoc.VariableLocation.VarLocType) { | ||
| case VarLocType.VLT_REG: | ||
| case VarLocType.VLT_REG_FP: | ||
| case VarLocType.VLT_REG_BYREF: | ||
| output.WriteLine($" Register: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}"); | ||
| break; | ||
| case VarLocType.VLT_STK: | ||
| case VarLocType.VLT_STK_BYREF: | ||
| output.WriteLine($" Base Register: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}"); | ||
| output.WriteLine($" Stack Offset: {varLoc.VariableLocation.Data2}"); | ||
| break; | ||
| case VarLocType.VLT_REG_REG: | ||
| output.WriteLine($" Register 1: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}"); | ||
| output.WriteLine($" Register 2: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data2)}"); | ||
| break; | ||
| case VarLocType.VLT_REG_STK: | ||
| output.WriteLine($" Register: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}"); | ||
| output.WriteLine($" Base Register: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data2)}"); | ||
| output.WriteLine($" Stack Offset: {varLoc.VariableLocation.Data3}"); | ||
| break; | ||
| case VarLocType.VLT_STK_REG: | ||
| output.WriteLine($" Stack Offset: {varLoc.VariableLocation.Data1}"); | ||
| output.WriteLine($" Base Register: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data2)}"); | ||
| output.WriteLine($" Register: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data3)}"); | ||
| break; | ||
| case VarLocType.VLT_STK2: | ||
| output.WriteLine($" Base Register: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}"); | ||
| output.WriteLine($" Stack Offset: {varLoc.VariableLocation.Data2}"); | ||
| break; | ||
| case VarLocType.VLT_FPSTK: | ||
| output.WriteLine($" Offset: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}"); | ||
| break; | ||
| case VarLocType.VLT_FIXED_VA: | ||
| output.WriteLine($" Offset: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}"); | ||
| break; | ||
| default: | ||
| output.WriteLine("WRN: Unexpected variable location type"); | ||
| break; | ||
| } | ||
| output.WriteLine(""); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return debugInfoDict; | ||
| } | ||
|
|
||
| private Dictionary<ulong, UnwindCode> WriteUnwindInfo(RuntimeFunction runtimeFunction, ITextOutput output) | ||
|
|
||
| { | ||
| Dictionary<ulong, UnwindCode> unwindCodes = new Dictionary<ulong, UnwindCode>(); | ||
| if (runtimeFunction.UnwindInfo is UnwindInfo amd64UnwindInfo) { | ||
|
|
@@ -129,27 +204,32 @@ private Dictionary<ulong, UnwindCode> WriteUnwindInfo(RuntimeFunction runtimeFun | |
| WriteCommentLine(output, $"FrameRegister: {((amd64UnwindInfo.FrameRegister == 0) ? "none" : amd64UnwindInfo.FrameRegister.ToString())}"); | ||
| for (int unwindCodeIndex = 0; unwindCodeIndex < amd64UnwindInfo.CountOfUnwindCodes; unwindCodeIndex++) { | ||
| unwindCodes.Add((ulong)(amd64UnwindInfo.UnwindCodeArray[unwindCodeIndex].CodeOffset), amd64UnwindInfo.UnwindCodeArray[unwindCodeIndex]); | ||
|
|
||
| } | ||
| } | ||
| return unwindCodes; | ||
| } | ||
|
|
||
| private void Disassemble(PEFile currentFile, ITextOutput output, ReadyToRunReader reader, ReadyToRunMethod readyToRunMethod, RuntimeFunction runtimeFunction, int bitness, ulong address, bool showMetadataTokens, bool showMetadataTokensInBase10) | ||
| { | ||
| // TODO: Decorate the disassembly with GCInfo | ||
| WriteCommentLine(output, readyToRunMethod.SignatureString); | ||
|
|
||
| Dictionary<ulong, UnwindCode> unwindInfo = null; | ||
| if (ReadyToRunOptions.GetIsShowUnwindInfo(null) && bitness == 64) { | ||
| unwindInfo = WriteUnwindInfo(runtimeFunction, output); | ||
| } | ||
|
|
||
| bool isShowDebugInfo = ReadyToRunOptions.GetIsShowDebugInfo(null); | ||
| Dictionary<VarLocType, HashSet<ValueTuple<DebugInfo, NativeVarInfo>>> debugInfo = null; | ||
| if (isShowDebugInfo) { | ||
| debugInfo = WriteDebugInfo(readyToRunMethod, output); | ||
| } | ||
|
|
||
| byte[] codeBytes = new byte[runtimeFunction.Size]; | ||
| for (int i = 0; i < runtimeFunction.Size; i++) { | ||
| codeBytes[i] = reader.Image[reader.GetOffset(runtimeFunction.StartAddress) + i]; | ||
| } | ||
|
|
||
| // TODO: Decorate the disassembly with GC and debug info | ||
| var codeReader = new ByteArrayCodeReader(codeBytes); | ||
| var decoder = Decoder.Create(bitness, codeReader); | ||
| decoder.IP = address; | ||
|
|
@@ -174,7 +254,7 @@ private void Disassemble(PEFile currentFile, ITextOutput output, ReadyToRunReade | |
| ulong baseInstrIP = instructions[0].IP; | ||
| foreach (var instr in instructions) { | ||
| int byteBaseIndex = (int)(instr.IP - address); | ||
| if (runtimeFunction.DebugInfo != null) { | ||
| if (isShowDebugInfo && runtimeFunction.DebugInfo != null) { | ||
| foreach (var bound in runtimeFunction.DebugInfo.BoundsList) { | ||
| if (bound.NativeOffset == byteBaseIndex) { | ||
| if (bound.ILOffset == (uint)DebugInfoBoundsType.Prolog) { | ||
|
|
@@ -191,14 +271,17 @@ private void Disassemble(PEFile currentFile, ITextOutput output, ReadyToRunReade | |
| output.Write(instr.IP.ToString("X16")); | ||
| output.Write(" "); | ||
| int instrLen = instr.Length; | ||
| for (int i = 0; i < instrLen; i++) | ||
| for (int i = 0; i < instrLen; i++) { | ||
| output.Write(codeBytes[byteBaseIndex + i].ToString("X2")); | ||
| } | ||
| int missingBytes = 10 - instrLen; | ||
| for (int i = 0; i < missingBytes; i++) | ||
| for (int i = 0; i < missingBytes; i++) { | ||
| output.Write(" "); | ||
| } | ||
| output.Write(" "); | ||
| output.Write(tempOutput.ToStringAndReset()); | ||
| DecorateUnwindInfo(output, unwindInfo, baseInstrIP, instr); | ||
| DecorateDebugInfo(output, instr, debugInfo, baseInstrIP); | ||
| DecorateCallSite(currentFile, output, reader, showMetadataTokens, showMetadataTokensInBase10, instr); | ||
| } | ||
| output.WriteLine(); | ||
|
|
@@ -213,6 +296,66 @@ private static void DecorateUnwindInfo(ITextOutput output, Dictionary<ulong, Unw | |
| } | ||
| } | ||
|
|
||
| private static void DecorateDebugInfo(ITextOutput output, Instruction instr, Dictionary<VarLocType, HashSet<(DebugInfo debugInfo, NativeVarInfo varLoc)>> debugInfoDict, ulong baseInstrIP) | ||
| { | ||
| if (debugInfoDict != null) { | ||
| InstructionInfoFactory factory = new InstructionInfoFactory(); | ||
| InstructionInfo info = factory.GetInfo(instr); | ||
| HashSet<ValueTuple<DebugInfo, NativeVarInfo>> stkSet = new HashSet<ValueTuple<DebugInfo, NativeVarInfo>>(); | ||
| if (debugInfoDict.ContainsKey(VarLocType.VLT_STK)) { | ||
| stkSet.UnionWith(debugInfoDict[VarLocType.VLT_STK]); | ||
| } | ||
| if (debugInfoDict.ContainsKey(VarLocType.VLT_STK_BYREF)) { | ||
| stkSet.UnionWith(debugInfoDict[VarLocType.VLT_STK_BYREF]); | ||
| } | ||
| if (stkSet != null) { | ||
| foreach (UsedMemory usedMemInfo in info.GetUsedMemory()) { //for each time a [register +- value] is used | ||
| foreach ((DebugInfo debugInfo, NativeVarInfo varLoc) tuple in stkSet) { //for each VLT_STK variable | ||
| var debugInfo = tuple.debugInfo; | ||
| var varInfo = tuple.varLoc; | ||
| int stackOffset = varInfo.VariableLocation.Data2; | ||
| ulong adjOffset; | ||
| bool negativeOffset; | ||
| if (stackOffset < 0) { | ||
| int absValue = -1 * stackOffset; | ||
| adjOffset = ulong.MaxValue - (ulong)absValue + 1; | ||
| negativeOffset = true; | ||
| } else { | ||
| adjOffset = (ulong)stackOffset; | ||
| negativeOffset = false; | ||
| } | ||
| if (varInfo.StartOffset < instr.IP - baseInstrIP && varInfo.EndOffset > instr.IP - baseInstrIP && | ||
| DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varInfo.VariableLocation.Data1) == usedMemInfo.Base.ToString() && | ||
| adjOffset == usedMemInfo.Displacement) { | ||
| output.Write($"; [{usedMemInfo.Base.ToString()}{(negativeOffset ? '-' : '+')}{Math.Abs(stackOffset)}] = {varInfo.Variable.Type} {varInfo.Variable.Index}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| HashSet<ValueTuple<DebugInfo, NativeVarInfo>> regSet = new HashSet<ValueTuple<DebugInfo, NativeVarInfo>>(); | ||
| if (debugInfoDict.ContainsKey(VarLocType.VLT_REG)) { | ||
| regSet.UnionWith(debugInfoDict[VarLocType.VLT_REG]); | ||
| } | ||
| if (debugInfoDict.ContainsKey(VarLocType.VLT_REG_BYREF)) { | ||
| regSet.UnionWith(debugInfoDict[VarLocType.VLT_REG_BYREF]); | ||
| } | ||
| if (debugInfoDict.ContainsKey(VarLocType.VLT_REG_FP)) { | ||
| regSet.UnionWith(debugInfoDict[VarLocType.VLT_REG_FP]); | ||
| } | ||
| if (regSet != null) { | ||
| foreach (UsedRegister usedMemInfo in info.GetUsedRegisters()) { | ||
| foreach ((DebugInfo debugInfo, NativeVarInfo varLoc) tuple in regSet) { | ||
| var debugInfo = tuple.debugInfo; | ||
| var varInfo = tuple.varLoc; | ||
| if (varInfo.StartOffset < instr.IP - baseInstrIP && varInfo.EndOffset > instr.IP - baseInstrIP && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to double-check with this, I think the |
||
| DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varInfo.VariableLocation.Data1) == usedMemInfo.Register.ToString()) { | ||
| output.Write($"; {usedMemInfo.Register.ToString()} = {varInfo.Variable.Type} {varInfo.Variable.Index}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| private static void DecorateCallSite(PEFile currentFile, ITextOutput output, ReadyToRunReader reader, bool showMetadataTokens, bool showMetadataTokensInBase10, Instruction instr) | ||
| { | ||
| int importCellAddress = (int)instr.IPRelativeMemoryAddress; | ||
|
|
@@ -246,7 +389,6 @@ private static void DecorateCallSite(PEFile currentFile, ITextOutput output, Rea | |
| output.WriteLine(reader.ImportCellNames[importCellAddress]); | ||
| break; | ||
| } | ||
|
|
||
| output.WriteLine(); | ||
| } else { | ||
| output.WriteLine(); | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to show hex for
stackOffsetto be consistent with the disassembly.