Net Advanced Debugging Part 3: Introduction to type metadata (synchronized block table, type handle, method descriptor, etc.)
1. Introduction
Today is the third article of “Net Advanced Debugging”, and the pressure is still quite high. In the previous article, we briefly talked about how the CLR and Windows loader load Net assemblies and how to find the entry point of the program. With the previous foundation, let’s look at something more detailed today. Now that the Windows operating system has loaded the CLR, initialized the application domain, and loaded our Net program, let’s take a look at what the Net type looks like in memory. This article is still a bit difficult. When I watched the video for the first time, I didn’t know what it was talking about. Later, I watched “Net Advanced Debugging” and I didn’t understand it. If it doesn’t work once, then do it again. If it doesn’t work, then do it again. As the saying goes, if you read a book a thousand times, its meaning will appear by itself.
If there is no explanation, the test environment for all codes is Net Framewok 4.8. However, sometimes in order to view the source code, you may need to use Net Core projects. I will explain it in the project chapter. Okay, without further ado, let’s start our debugging work today.
I need to explain the debugging environment in case everyone is unclear. I have listed the specific circumstances.
Operating system: Windows Professional 10
Debugging tool: Windbg Preview (you can go to Microsoft Store to download)
Development tool: Visual Studio 2022
Net version: Net Framework 4.8
CoreCLR source code: source code Download
2. Related knowledge
We know the CLR, the JIT, and the Net In the compilation process, seeing is believing, and all the knowledge points are rooted. This time, I will study the type of things. Of course, this is something that is difficult to encounter when writing. Even if you don’t understand, you can still write Get something out. However, if you want to do it, you must know one and the other. These are just necessary to understand, and they are still very helpful for us to write efficient code. The following are the relevant knowledge points. I will explain them one by one. List them.
Stack stack (first in, last out) is a memory space allocated during compilation, so in your code The size of the stack must be clearly defined;
Heap (queue Priority, first in, first out) is the memory space dynamically allocated during the running of the program. You can determine the size of the heap memory to be allocated based on the running conditions of the program.
1. Introduction
Type is the basic programming unit in Net program, and type is Can be subdivided into: value types, reference types.
a), value type
boards in you in in in in inrenting types
and other simple types, such as: int, float, double, char, bool, etc. These types occupy a small space and are generally stored on the thread stack. Of course, they can also be stored in registers, managed heaps, or private heaps.
b), reference types
Interfaces, arrays, classes and our custom Class are all reference types. Such types generally occupy a relatively large space and they exist in the managed heap. , the GC is responsible for allocating memory and reclaiming memory to manage instances of these reference types.
2. Value type layout
over over
Local variables are stored on the current thread stack, which means that values of value types are directly stored on the thread stack.
3. Reference type layout
The class type is a reference type. The instance object allocates space in the managed heap and assigns the first address of the object to exists at the stack address.
4. Synchronized block table
ccogene through in in inâher’s That’s what it’s called. Each object on the managed heap has a synchronization block index in front of it, which points to the synchronization block table on the private heap in the CLR. The synchronization block table can contain a lot of information, such as: object hash code, lock information, application domain index.
<img src="https://img2023.cnblogs.com/blog/1048776/202310/1048776-20231030111316904-2105317722.png" alt="�I will clean it up.
1 0:006> !token2ee Example_3_1_5 06000001 2 Module: 00e64044 3 Assembly: Example_3_1_5 .exe 4 Token: 06000001 5 MethodDesc: 00e64d58 6 Name: Example_3_1_5 .Program.Main(System.String[]) 7 JITTED Code Address: 02990848
2.9. View the structure of EECLass.
“ clip
executable] menu to load the [Example_3_1_5.exe] project, run the program through the [g] command, the debugger will pause execution once it runs [Console.ReadLine()], and then we click the [break] button to enter the debugging state. Of course, we can use the [cls] command to clean up the excessive information displayed by the debugger. It is up to you to decide. I will clean it up.
1 0:006> !dumpheap -type Person 2 Address MT Size 3 029d2508 00e64e1c 16 4 5 Statistics: 6 MT Count TotalSize Class Name 7 00e64e1c 1 16 Example_3_1_5.Person 8 Total 1 objects 9 10 11 0:006> !do 029d2508 12 Name: Example_3_1_5 .Person 13 MethodTable: 00e64e1c 14 EEClass: 00e61318 15 Size: 16 (0x10) bytes 16 File: E:\Visual Studio 2022\Source\Projects\AdvancedDebug.NetFramework.Test\Example_3_1_5\bin\Debug\Example_3_1_5.exe 17 Fields: 18 MT Field Offset Type VT Attr Value Name 19 6fa424e4 4000001 4 System.String 0 instance 029d24c8 k__BackingField 20 6fa442a8 4000002 8 System.Int32 1 instance 20 k__BackingField 21 22 23 0:006> !DumpClass /d 00e61318 24 Class Name: Example_3_1_5.Person 25 mdToken: 02000003 26 File: E:\Visual Studio 2022\Source\Projects\AdvancedDebug.NetFramework.Test\Example_3_1_5\bin\Debug\Example_3_1_5.exe 27 Parent Class: 6fa315c8 28 Module: 00e64044 29 Method Table: 00e64e1c 30 Vtable Slots: 4 31 Total Method Slots: 5 span> 32 Class Attributes: 100001 33 Transparency: Critical 34 NumInstanceFields: 2 35 NumStaticFields: 0 36 MT Field Offset Type VT Attr Value Name 37 6fa424e4 4000001 4 System.String 0 instance k__BackingField 38 6fa442a8 4000002 8 System.Int32 1 instance k__BackingField
View Code
4. Summary
Finally it is completed. I have been writing this article for several days, looking at the underlying things. It takes patience and persistence. After finishing writing, I feel that I have gained a lot, and I know more about the underlying details of Net. Learning is an arduous process, quite time-consuming and labor-intensive. That’s it for writing this. Don’t forget your original intention and keep working hard.
All countries in the world can be equalized; honors and positions can be dismissed; swordsmanship can be practiced; moderation is impossible.ba(128, 0, 128, 1)”>0 instance 029d24c8 k__BackingField
20 6fa442a8 4000002 8 System.Int32 1 instance 20 k__BackingField
21
22
23 0:006> !DumpClass /d 00e61318
24 Class Name: Example_3_1_5.Person
25 mdToken: 02000003
26 File: E:\Visual Studio 2022\Source\Projects\AdvancedDebug.NetFramework.Test\Example_3_1_5\bin\Debug\Example_3_1_5.exe
27 Parent Class: 6fa315c8
28 Module: 00e64044
29 Method Table: 00e64e1c
30 Vtable Slots: 4
31 Total Method Slots: 5 span>
32 Class Attributes: 100001
33 Transparency: Critical
34 NumInstanceFields: 2
35 NumStaticFields: 0
36 MT Field Offset Type VT Attr Value Name
37 6fa424e4 4000001 4 System.String 0 instance k__BackingField
38 6fa442a8 4000002 8 System.Int32 1 instance k__BackingField
View Code
4. Summary
Finally it is completed. I have been writing this article for several days, looking at the underlying things. It takes patience and persistence. After finishing writing, I feel that I have gained a lot, and I know more about the underlying details of Net. Learning is an arduous process, quite time-consuming and labor-intensive. That’s it for writing this. Don’t forget your original intention and keep working hard.
All countries in the world can be equalized; honors and positions can be dismissed; swordsmanship can be practiced; moderation is impossible.