NTSD and SOS basics

I wanted to take a very simple console application and use ntsd with SOS to debug it. The example demonstrated very simple operations as setting a break point, viewing the managed stack, stack variables, object instance etc.

 

We can go deeper into some of the in depth concepts in the next post.

 

Let us take a simple console application as below:

using System;

public class sample

{

    string str;

    public void MyMethod(string arg)

    {

        str = "Member Variable";

        Console.WriteLine("Argument: {0} - {1]", arg, str);

    }

    static void Main()

    {

        sample s = new sample();

        s.MyMethod("Hello");

    }

}

 

We would like to do the following as part of our debugging.

  1. Set a break point on MyMethod
  2. Watch the variables passed to the method
  3. Watch the instance of sample and its variables

 

Let us compile the application with debug enabled. Type

 

csc /Debug App.cs

 

This would generate App.exe and App.pdb. Now, let us start debugging this application:

 

C:\Blog>ntsd App.exe

 

0:000> .symfix

0:000> .sympath+ .

0:000> .reload

0:000> sxe –c “ “ clrn

0:000> g

0:000> .loadby sos mscorwks

0:000> !bpmd App.exe sample.MyMethod

0:000> g

0:000> !clrstack –a

OS Thread Id: 0x154c (0)

ESP EIP

001bf268 009700f0 sample.MyMethod(System.String)

    PARAMETERS:

        this = 0x01501964

        arg = 0x01501948

001bf26c 009700a9 sample.Main()

    LOCALS:

        <CLR reg> = 0x01501964

001bf490 79e8273b [GCFrame: 001bf490]

0:000> !DumpObj 0x01501948

Name: System.String

MethodTable: 790fc6cc

EEClass: 790fc62c

Size: 28(0x1c) bytes

 (C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)

String: Hello

Fields:

      MT Field Offset Type VT Attr Value Name

790ff7f0 4000096 4 System.Int32 0 instance 6 m_arrayLength

790ff7f0 4000097 8 System.Int32 0 instance 5 m_stringLength

790fe2dc 4000098 c System.Char 0 instance 48 m_firstChar

790fc6cc 4000099 10 System.String 0 shared static Empty >> Domain:Value 003507e8:790d7eb4 <<

7913cb00 400009a 14 System.Char[] 0 shared static WhitespaceChars >> Domain:Value 003507e8:01501548 <<

0:000> !DumpObj 0x01501964

Name: sample

MethodTable: 002a301c

EEClass: 002a1200

Size: 12(0xc) bytes

 (C:\blog\app.exe)

Fields:

      MT Field Offset Type VT Attr Value Name

790fc6cc 4000001 4 System.String 0 instance 00000000 str

// Type in a couple of “p” until you get to the place where the local variable is assgned the value “Member Variable”

0:000> !DumpObj 0x01501964

Name: sample

MethodTable: 000c301c

EEClass: 000c1200

Size: 12(0xc) bytes

 (C:\blog\app.exe)

Fields:

      MT Field Offset Type VT Attr Value Name

790fc6cc 4000001 4 System.String 0 instance 01411970 str

0:000>  !DumpObj 01411970

Name: System.String

MethodTable: 790fc6cc

EEClass: 790fc62c

Size: 48(0x30) bytes

 (C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)

String: Member Variable

Fields:

      MT Field Offset Type VT Attr Value Name

790ff7f0 4000096 4 System.Int32 0 instance 16 m_arrayLength

790ff7f0 4000097 8 System.Int32 0 instance 15 m_stringLength

790fe2dc 4000098 c System.Char 0 instance 4d m_firstChar

790fc6cc 4000099 10 System.String 0 shared static Empty >> Domain:Value 001007c8:790d7eb4 <<

7913cb00 400009a 14 System.Char[] 0 shared static WhitespaceChars >> Domain:Value 001007c8:01411548 <<