Freigeben über


Windbg and managed code. What does, for example, a null value look like?

Sometimes when debugging I realize I forget what different values (NULL, TRUE, FALSE, empty strings etc.) looks like when debugging managed code.

If this happens for you as well, here is a quick reference.

 

The application (simple C# console application):

 

namespace Values

{

    class Program

    {

        static bool boolTrue = true;

        static bool boolFalse = false;

        static bool? nullableBoolTrue = true;

        static bool? nullableBoolFalse = false;

        static bool? nullableBoolNull = null;

        static byte byteValue = 123;

        static char charValue = 'X';

        static decimal decimalValue = 12.34m;

        static float floatValue = 12.34f;

        static string stringEmpty = "";

        static string stringValue = "abc123"; // Basically the same as an empty string.

        static string stringNull = null;

        static int intValue = 123;

        static int? nullableIntNull = null;

        static int? nullableIntValue = 123;

        static AClass referenceNull = null;

        static AClass referenceNotNull = new AClass();

       

        static Point structValue = new Point(1, 1);

        static List<AClass> populatedList = new List<AClass>() { new AClass { AProp = 100 } };

        static void Main(string[] args)

        {

            Program p = new Program();

            Console.WriteLine("Paused, break in with WinDbg");

            Console.ReadKey();

        }

    }

    public struct Point

    {

        public int x, y;

        public Point(int p1, int p2)

        {

            x = p1;

            y = p2;

   }

    }

    public class AClass

    {

        public int AProp { get; set; }

    }

}

 

Build it and run it. Then attach the debugger (32 bit in this example) and load SOS.dll and list out the Program class to see the values:

 

0:004> .load C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll

0:004> !do 024ab340

Name: Values.Program

MethodTable: 002939e8

EEClass: 0029155c

Size: 12(0xc) bytes

File: C:\Temp\Values\Values\bin\Debug\Values.exe

Fields:

      MT Field Offset Type VT Attr Value Name

6cf16788 4000001       24 System.Boolean 1 static 1 boolTrue

6cf16788 4000002       28 System.Boolean 1 static 0 boolFalse

6cf16700 4000003        4 ...olean, mscorlib]] 1 static 034a325c nullableBoolTrue

6cf16700 4000004        8 ...olean, mscorlib]] 1 static 034a3260 nullableBoolFalse

6cf16700 4000005        c ...olean, mscorlib]] 1 static 034a3264 nullableBoolNull

6cf12748 4000006       2c System.Byte 1 static 123 byteValue

6cf11f24 4000007       30 System.Char 1 static 58 charValue

6cf0a5b0 4000008       10 System.Decimal 1 static 034a3268 decimalValue

6cf0a424 4000009       34 System.Single 1 static 12.340000 floatValue

6cf0fb08 400000a       20 System.String 0 static 024a1228 stringEmpty

6cf0fb08 400000b       24 System.String 0 static 024ab264 stringValue

6cf0fb08 400000c       28 System.String 0 static 00000000 stringNull

6cf12ad4 400000d       38 System.Int32 1 static 123 intValue

6ceff768 400000e       14 ...Int32, mscorlib]] 1 static 034a326c nullableIntNull

6ceff768 400000f       18 ...Int32, mscorlib]] 1 static 034a3270 nullableIntValue

00293a74 4000010       2c Values.AClass 0 static 00000000 referenceNull

00293a74 4000011       30 Values.AClass 0 static 024ab280 referenceNotNull

002939a4 4000012       1c Values.Point 1 static 034a3274 structValue

00293ac8 4000013       34 ....AClass, Values]] 0 static 024ab28c populatedList

 

So here we for example, can see that an empty string (024a1228) and a null string (00000000) is different.
We can also see that a nullable int and an ordinary int are different in the Type department which could be a good clue when debugging.

 

You can just add other types as you want to, hopefully this serves as a start.