次の方法で共有


How to look at value of structure object who is member of Class object while debugging in WinDbg??

Sometimes when you debug applications, it happens that you have got the pointer to class which has few member variables including structures and another class objects! How to look at values of those inner class/structure variable's members?? So, here is the tip.

Following is the code being used for demonstrating commands.

    1:  struct testStruct
    2:  {
    3:      int i;
    4:      char ch;
    5:      long l;
    6:      char* str;
    7:  };
    8:   
    9:  class myClass2
   10:  {
   11:  public:
   12:      myClass2();
   13:      ~myClass2();
   14:      testStruct structObj;
   15:  };
   16:   
   17:  myClass2::myClass2()
   18:  {
   19:      //Do Nothing..
   20:      char *pStr = new char[100];
   21:      sprintf(pStr, "Test Message");
   22:      structObj.i = 101; structObj.l = 1001; structObj.ch = 'j'; structObj.str = pStr;
   23:  }
   24:   
   25:  myClass2::~myClass2()
   26:  {
   27:      delete structObj.str;
   28:  }
   29:   
   30:  int test2(myClass2* classObject)
   31:  {
   32:      return classObject->structObj.i;
   33:  }
   34:   
   35:  void CWinDbgDlg::OnBnClickedButton2()
   36:  {
   37:      // TODO: Add your control notification handler code here
   38:      myClass2 *ptr = new myClass2();
   39:      int retVal = test2(ptr);
   40:  }

So, what we want to do is, while we break @ test2 function, we want to see in WinDbg whats the value of structObj.str of that instance.

Here are steps which needs to be done in Windbg. Attach the process to WinDbg, break on the function test2 by putting simple breakpoint. Hit kb command to find out the first parameter to test2 function (which is pointer to class object for myClass2).

SHOT0028

First parameter to function test2 is, 0x006ab520 (see the third column in first row of call stack above). As you might know, if you want to see member variable of class object pointer, you can use dt command.

SHOT0029

But, our goal is to find out whats inside structObj! Well, you can give following command to get that detail,

SHOT0030

Notice the difference between last command and one prior to that. We appended "structObj." at the end. That's how we can find members inside that structure. Mission Accomplished!!

I know, its really difficult to explain what exactly we are doing, and I think I am trying my best to explain it with the help of snapshot and coding, if you think it can be improved, please let me know..

Stay tuned.. Wave