1. Net CoreClr 2.0 compilation and debugging,. netcoreclr
Small goal: To compile a small console program test_core.dll with. Net Core 2.0 SDK, and then use VS2017 DebugCoreclr source code. You can debug and view it in detail.Test_core.dllHow is IL code converted from jit to Native code step by step.
1. download the latest coreclr source code and switch to the release/2.0.0 branch. The main purpose of this article is to learn the coreclr source code. Because the main Master code has been updated, many new problems are not fixed in time, using the release branch can avoid many compilation problems and be stable.
2. Prepare the coreclr compiling environment. This time, we use Win10 and VS2017 for compilation and debugging (the local VS2015 and VS2017 are both installed ).
3. You are ready to go to the coreclr directory and open the console. \ build skiptests. The compilation is complete as follows:
4. Configure Debug. There is a description on Github, but it is relatively old and not completely easy to use. Here I will add my personal understanding and reorganize it as follows:
- A. Find the coreclr \ bin \ obj \ Windows_NT.x64.Debug folder and enable CoreCLR. sln in VS2017.
- B. Set the INSTALL project to start the project.
- C. Set INSTALL project-> right-click properties-> Debugging.
- D. set Command =
$(SolutionDir)..\..\product\Windows_NT.$(Platform).$(Configuration)\corerun.exe。
- E. set Command Arguments =
<managed app you wish to run>(E.g. test_core.dll ).
- F. Set Directory =
$(SolutionDir)..\..\product\Windows_NT.$(Platform).$(Configuration)This folder contains the dll and pdb files generated by compiling CoreCLR.
- G. set Environment = CORE_LIBRARIES = C: \ Program Files \ dotnet \ shared \ Microsoft. NETCore. app \ 2.0.3 (this configuration tells CoreCLR to go to this path to find the basic Managed class library. If debug is not configured, an error will be thrown. In order to facilitate direct pointing to the local machine.. Net Core SDK folder ).
The complete configuration is as follows:
Summary:So far, a total of three-party dll (managed and unmanaged) has been involved ):
- We compile our own managed test_core.dll, which is also the main experimental object.
- . NET Core SDK hosting base class library System. Runtime. dll, System. Threading. dll, and so on (managed dependencies related to test_core.dll ).
- Our edited coreclris not pure dll, coreclr.dll?cl=it.dll=corerun.exe, and so on, which are our main debugging objects.
5. Compile test_core.dll
Use the. Net Core2.0 SDK to compile the following code.
1 using System; 2 using System.Runtime.CompilerServices; 3 4 namespace test_core 5 { 6 class Program 7 { 8 const int Pass = 100; 9 const int Fail = -1;10 [MethodImplAttribute(MethodImplOptions.NoInlining)]11 public static void DblRoots(double a, double b, double c, ref double r1, ref double r2)12 {13 r1 = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);14 r2 = (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a);15 return;16 }17 static int Main(string[] args)18 {19 double x1 = 0;20 double x2 = 0;21 DblRoots(1d, -5d, 6d, ref x1, ref x2);22 Console.WriteLine(x1 + "," + x2);23 if (System.Math.Abs(x1 - 3d) > Double.Epsilon) return Fail;24 if (System.Math.Abs(x2 - 2d) > Double.Epsilon) return Fail;25 string str = Console.ReadLine();26 return Pass;27 28 }29 }30 }
Copy all the compiled content to the coreclr \ bin \ Product \ Windows_NT.x64.Debug directory.
6. Set a breakpoint in VS2017 and run it to view the running result of test_core.dll.
7. The next step is to make full use of your imagination. You can refer to the GitHub coreclr document to debug the core code of ryujit at a critical breakpoint.
8. I hope this article will help you Debug CoreClr more easily. You are welcome to give some advice.