Added disassembler CoreDisTools to dump runtime functions (dotnet/coreclr#18180)
authorAmy <amycmyu@gmail.com>
Fri, 1 Jun 2018 20:44:28 +0000 (13:44 -0700)
committerZach Montoya <zamont@microsoft.com>
Fri, 1 Jun 2018 20:44:28 +0000 (13:44 -0700)
Remove obsolete targets

Remove unused NewDiffer function, fix error with uninitialized declaringTypeHandle

Commit migrated from https://github.com/dotnet/coreclr/commit/15f2475fc4ced34607f74830d406989d7cf5f9ca

src/coreclr/src/tools/r2rdump/CoreDisTools.cs [new file with mode: 0644]
src/coreclr/src/tools/r2rdump/NativeReader.cs
src/coreclr/src/tools/r2rdump/R2RDump.cs
src/coreclr/src/tools/r2rdump/R2RDump.csproj
src/coreclr/src/tools/r2rdump/R2RMethod.cs
src/coreclr/src/tools/r2rdump/R2RSection.cs

diff --git a/src/coreclr/src/tools/r2rdump/CoreDisTools.cs b/src/coreclr/src/tools/r2rdump/CoreDisTools.cs
new file mode 100644 (file)
index 0000000..52cb900
--- /dev/null
@@ -0,0 +1,62 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Reflection.PortableExecutable;
+using System.Runtime.InteropServices;
+
+namespace R2RDump
+{
+    class CoreDisTools
+    {
+        public enum TargetArch
+        {
+            Target_Host, // Target is the same as host architecture
+            Target_X86,
+            Target_X64,
+            Target_Thumb,
+            Target_Arm64
+        };
+
+        [DllImport("coredistools.dll")]
+        [return: MarshalAs(UnmanagedType.I8)]
+        public static extern long InitDisasm(TargetArch Target);
+
+        [DllImport("coredistools.dll")]
+        public static extern void DumpCodeBlock(long Disasm, ulong Address, IntPtr Bytes, int Size);
+
+        [DllImport("coredistools.dll")]
+        public static extern void FinishDisasm(long Disasm);
+
+        public unsafe static void DumpCodeBlock(long Disasm, int Address, int Offset, byte[] image, int Size)
+        {
+            fixed (byte* p = image)
+            {
+                IntPtr ptr = (IntPtr)(p + Offset);
+                DumpCodeBlock(Disasm, (ulong)Address, ptr, Size);
+            }
+        }
+
+        public static long GetDisasm(Machine machine)
+        {
+            TargetArch target = TargetArch.Target_Host;
+            switch (machine)
+            {
+                case Machine.Amd64:
+                    target = TargetArch.Target_X64;
+                    break;
+                case Machine.I386:
+                    target = TargetArch.Target_X86;
+                    break;
+                case Machine.Arm64:
+                    target = TargetArch.Target_Arm64;
+                    break;
+                case Machine.ArmThumb2:
+                    target = TargetArch.Target_Thumb;
+                    break;
+            }
+            return InitDisasm(target);
+        }
+    }
+}
index b3359f7632699dd8cf2c3a3ae342a3254f8caaa9..59fd2881a6b631fcb7eea2610361a6b6421ceb1e 100644 (file)
@@ -1,4 +1,8 @@
-using System;
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
 using System.Collections.Generic;
 using System.Text;
 
index 9aa80395f51571ec7c7921e7163b55dcea7c3aaa..f7fc729c1f5f0f90ea5990a1a00eca8638471476 100644 (file)
@@ -22,6 +22,7 @@ namespace R2RDump
         private IReadOnlyList<int> _runtimeFunctions = Array.Empty<int>();
         private IReadOnlyList<string> _sections = Array.Empty<string>();
         private bool _diff = false;
+        private long _disassembler;
         private TextWriter _writer;
 
         private R2RDump()
@@ -90,7 +91,7 @@ namespace R2RDump
 
         private void WriteSubDivider()
         {
-            _writer.WriteLine("------------------");
+            _writer.WriteLine("_______________________________________________");
             _writer.WriteLine();
         }
 
@@ -146,7 +147,15 @@ namespace R2RDump
         /// </summary>
         private void DumpRuntimeFunction(R2RReader r2r, RuntimeFunction rtf)
         {
-            _writer.WriteLine(rtf.ToString());
+            if (_disasm)
+            {
+                _writer.WriteLine($"Id: {rtf.Id}");
+                CoreDisTools.DumpCodeBlock(_disassembler, rtf.StartAddress, r2r.GetOffset(rtf.StartAddress), r2r.Image, rtf.Size);
+            }
+            else
+            {
+                _writer.Write($"{rtf}");
+            }
             if (_raw)
             {
                 DumpBytes(r2r, rtf.StartAddress, (uint)rtf.Size);
@@ -302,7 +311,7 @@ namespace R2RDump
                 QueryMethod(r2r, "R2R Methods by Keyword", _keywords, false);
             }
 
-            _writer.WriteLine("========================================================");
+            _writer.WriteLine("=============================================================");
             _writer.WriteLine();
         }
 
@@ -440,7 +449,17 @@ namespace R2RDump
                 foreach (string filename in _inputFilenames)
                 {
                     R2RReader r2r = new R2RReader(filename);
+                    if (_disasm)
+                    {
+                        _disassembler = CoreDisTools.GetDisasm(r2r.Machine);
+                    }
+
                     Dump(r2r);
+
+                    if (_disasm)
+                    {
+                        CoreDisTools.FinishDisasm(_disassembler);
+                    }
                 }
             }
             catch (Exception e)
index cb9e76fb4d8bd40002a82b6b24fc02799ecd37e4..e783e1698a64ace45096060423e9f1bec7783ec2 100644 (file)
@@ -10,7 +10,9 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="Microsoft.NETCore.CoreDisTools" Version="1.0.1-prerelease-00003" />
     <PackageReference Include="System.CommandLine" Version="0.1.0-e160119-1" />
+    <PackageReference Include="System.Reflection.Metadata" Version="1.7.0-preview1-26530-04" />
   </ItemGroup>
 
 </Project>
index 1b658df40ab317446f9806e7cb2d79ebde558b09..95cf8929c3f35b07cadf0dac1cdab16cd5b13144 100644 (file)
@@ -189,13 +189,14 @@ namespace R2RDump
 
             TypeDefinitionHandle declaringTypeHandle = _methodDef.GetDeclaringType();
             TypeDefinition declaringTypeDef;
-            while (!declaringTypeHandle.IsNil)
+            do
             {
                 declaringTypeDef = mdReader.GetTypeDefinition(declaringTypeHandle);
                 DeclaringType = mdReader.GetString(declaringTypeDef.Name) + "." + DeclaringType;
                 declaringTypeHandle = declaringTypeDef.GetDeclaringType();
             }
-            
+            while (!declaringTypeHandle.IsNil);
+
             NamespaceDefinitionHandle namespaceHandle = declaringTypeDef.NamespaceDefinition;
             while (!namespaceHandle.IsNil)
             {
index aef32d24ba9a3acc3d1eba16a7ac819e216d9266..8d6d7cf99c578e705bb61c90dddec2facf685a8e 100644 (file)
@@ -1,4 +1,8 @@
-using System;
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
 using System.Collections.Generic;
 using System.Text;