Auto-expanding objects when debugging

I am used to developing software using the C++ MFC framework and I especially like the fact that, when debugging, serialized info from objects can be seen in the debugging windows, namely the variables window and the watch window. That's cool as a mean to avoid to have to expand nodes by hand any time you are dealing with an object only to access a particular member.

serialized object info in the debugging windows
serialized object info in the debugging windows

Unfortunately, if you try and use this mechanism for your own classes, then it won't work by default as the pic below exemplifies, you are forced to manually expand the node over and over again.

serialized object info does not work by default
serialized object info does not work by default with your own classes

I haven't found any documentation over this, so I dived into the MFC classes in hope of finding more. I took a look to the CString class, which of course draws a natual attention on that matter, and was initially misled by the additional methods that it implements, namey ::Dump() and the << serialization operator, which are implemented by all MFC classes, and which made me believe they were the magic stuff.

//////////////////////////////////////////////////////////////////////////////
// Diagnostic support

#ifdef _DEBUG
friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,
                                       const CString& string)
{
  dc << string.m_pchData;
  return dc;
}
#endif

with a CDumpContent implementation that essentially writes content into a file.

After a while, I thought that regardless what that serialization implementation did, it was too awful to implement for my own classes, requiring an object to serialize itself is way too much implementation work!

So I ended the experiments somewhat frustrated.

It's only thanks to the re-reading of Debugging Windows Applications (Amazon) to learn more about advanced debugging techniques for another reason that I stump on that topic, given its tiny corner in the book.

Surprisingly enough, classes able to serialize themselves "intelligently" in debugging windows are explicitely declared in a DevStudio-specific environment file, which is reloaded any time a debugging session starts.

That file is autoexp.dat and can be found in the following folder : $Microsoft Visual Studio$ \ Common \ MSDev98 \ Bin if you are using DevStudio 6.0

Adding your own classes is a breaze, at least if the expected stuff to appear in the debugging windows is simple : number, string. There is no support for a callback or whatsoever that would, unlike I thought the previous MFC Dump stuff did (and I am wrong about it), allow to show complex stuff.

I have a AString class which mimics the MFC CString class, without any dependencies on MFC. ALl I have to do to have AString objects nicely auto-expanded while I am debugging is add the following line in autoexp.dat :

AString =text=<m_str,s>
where the syntax is as follows :
  • what's before the first equal sign is the class name. The space char doesn't matter.
  • what's matter is what's serialized and shown. There are litterals and functions
  • text= is a litteral, it's a prefix. Not mandatory, and only useful when you show more than one member
  • m_str is the class member being serialized. In my AString class, this member holds the underlying string
  • s is short for null-terminated string. It says that the member actual formatting.

The following table (excerpt from autoexp.dat) shows the available formatting options

Letter Description Sample Display
d,i Signed decimal integer 0xF000F065,d -268373915
u Unsigned decimal integer 0x0065,u 101
o Unsigned octal integer 0xF065,o 0170145
x,X Hexadecimal integer 61541,X 0X0000F065
l,h long or short prefix for d,i,u,o,x,X 00406042,hx 0x0c22
f Signed floating-point 3./2.,f 1.500000
e Signed scientific-notation 3./2.,e 1.500000e+000
g Shorter of e and f 3./2.,g 1.5
c Single character 0x0065,c 'e'
s Zero-terminated string 0x0012fde8,s "Hello world"
su Unicode string 0x007200c4,su "Hello world"
m, ma, mb, mw, md Memory dump (a:ascii, b:byte, w:word, d:double) 0x0012fde8,mb 4d 5a 90 00 03 00 00

 

Stéphane Rodriguez-
November 12, 2003.

Home
Blog