Getting started with windbg: How to Use windbg to debug arraylist values

Source: Internet
Author: User
Tags mscorlib microsoft website
When developing in. net, the most basic debugging method is to use Visual Studio single-step debugging. However, in some special cases, especially when the CLR is involved, this method cannot be used.
You can use either of the following methods to view the memory usage, il code, and CLR information during running:
1. Use vs2005 + SOS. dll
2. Use windbg + SOS. dll
The second method is more powerful. Next I will show you how to use this method to obtain the value inside the arraylist during running.
Some may say: Is it faster for me to directly use Visual Studio for single-step debugging? Of course, this is just a demonstration. This demonstration will lay the foundation for future advanced debugging.

Before performing this operation, familiarize yourself with the basic knowledge:
A. Use vs2005 + SOS. DLL for debugging
1. You need to enable unmanaged code debugging in the project> Properties> debugging>
2. Open the debugging-> window-> instant
3. enter it in the real-time window! Load SOS load debugging Module
4. Input other debugging statements

B. Use windbg + SOS. dll
1. download the latest windbg from Microsoft website
2. Open windbg and enterSRV * c: \ symbols * http://msdl.microsoft.com/download/symbols
3. Run the program to be debugged and select the program you just run in file-> attach to process in windbg.
4. In the displayed command window, you can enter debugging statements.
5. common debugging statements:
Lm // view the loaded modules
. Load c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ SOS. dll // load the debugging Module
LD testclass // load the debugging symbol
! Name2ee testclass.exe testclass. program. Test // display the address related to the test method
! Dumpmt-MD 00976d48 // obtain the member function details of the class.
! Dumpil 00973028 // display the Il code after this method is compiled by the compiler
! Dumpheap-stat // This command displays the statistics of all objects in the program. The displayed size is the size of the object itself, excluding the nominal value of the object.
! Dumpheap-MT 790fcb30 // This command displays detailed information about methodtable 790fcb30
! Gcroot 012919b8 // to display the relationship of an instance
! Dumpobj (DO) 012a3904 // display the specific content of an object to see what is in the object and what is the value
! Objsize 012a1ba4 // actual size of the object in the memory
! Eeheap-GC // view the managed heap (including the size)
! Dumparray // view the array information

Next let's take a look at the specific debugging steps:
1. Our test code

Namespace testclass
{
Class Program
{
[Stathread]
Static void main (string [] ARGs)
{
Arraylist list = new arraylist ();
List. Add ("aaaa ");
List. Add ("BBBB ");
Console. Readline ();
}
}
}

It is very simple, it is an arraylist

Run this program (start execution, do not Debug), then enter windbg, attach to this process

2. View All stack information
0: 004>! Dumpheap-stat
Mt count totalsize Class Name
7910062c 1 12 system. Security. permissions. securitypermission
7918e284 1 16 system. Io. textreader + synctextreader
79102d10 1 20 Microsoft. win32.safehandles. safefilemappinghandle
79102cb4 1 20 Microsoft. win32.safehandles. safeviewoffilehandle
79101d30 1 20 system. Text. internalencoderbestfitfallback
79100a7c 1 20 Microsoft. win32.safehandles. safefilehandle
79105cd4 1 24 system. Collections. arraylist
......
7912ad90 11 9036 system. object []
790fcb30 2083 131492 system. String
Total 2202 objects
In addition to our arraylist, there is a lot of other system information, you don't need to worry about it

3. view the arraylist information.
0: 004>! Dumpheap-MT 79105cd4
Address Mt size
012a1b88 79105cd4 24
Total 1 objects
Statistics:
Mt count totalsize Class Name
79105cd4 1 24 system. Collections. arraylist
Total 1 objects

4. view the actual internal values of the corresponding address
0: 004>! Do 012a1b88
Name: system. Collections. arraylist
Methodtable: 79105cd4
Eeclass: 79105c28
Size: 24 (0x18) bytes
(C: \ WINDOWS \ Assembly \ gac_32 \ mscorlib \ 2.0.0.0 _ b77a5c561934e089 \ mscorlib. dll)
Fields:
MT field offset type vt attr Value Name
7912ad90 40008df 4 system. object [] 0 instance 012a1bb0 _ items
791018e0 40008e0 C system. int32 1 instance 2 _ size
791018e0 40008e1 10 system. int32 1 instance 2 _ version
790fc35c 40008e2 8 system. Object 0 instance 00000000 _ syncroot
7912ad90 40008e3 1c0 system. object [] 0 shared static emptyarray
> Domain: Value 00149c58: 012a1ba0 <
The size of arraylist is 2. The specific value is saved in address 012a1bb0, which is an array of the system. object [] type.

5. View array information
0: 004>! Dumparray 012a1bb0
Name: system. object []
Methodtable: 7912ad90
Eeclass: 7912b304
Size: 32 (0x20) bytes
Array: Rank 1, number of elements 4, type class
Element methodtable: 790fc35c
[0] 012a1b50
[1] 012a1b6c
[2] Null
[3] Null

6. view the values of objects in the array
0: 004>! Do 012a1b50
Name: system. String
Methodtable: 790fcb30
Eeclass: 790fca90
Size: 26 (0x1a) bytes
(C: \ WINDOWS \ Assembly \ gac_32 \ mscorlib \ 2.0.0.0 _ b77a5c561934e089 \ mscorlib. dll)
String: AAAA
Fields:
MT field offset type vt attr Value Name
791018e0 4000096 4 system. int32 1 instance 5 m_arraylength
791018e0 4000097 8 system. int32 1 instance 4 m_stringlength
790fe534 4000098 C system. Char 1 instance 61 m_firstchar
790fcb30 4000099 10 system. String 0 shared static empty
> Domain: Value 00149c58: 790d81bc <
7912b1d8 400009a 14 system. Char [] 0 shared static whitespacechars
> Domain: Value 00149c58: 012a16f0 <

In this way, we can get the arraylist runtime value through windbg.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.