Visualizing MFC Containers in autoexp.dat

MFC containers are more or less officially deprecated in favor of STL. Even so, when navigating in legacy code the need often arises to watch CArrays, CLists, CMaps and the like. autoexp.dat provides only STL visualizers out of the box, but you can just paste the lines below into autoexp’s [Visualizer] section, and have a similar debugging experience with MFC code:

[Visualizer]
; This section contains visualizers for STL and ATL containers
; DO NOT MODIFY     (HAAAAAAAAA. -o.s.)

...

;---------------------------------------------------------------------
;  MFC Types
;---------------------------------------------------------------------
CArray<*,*> |CObArray|CByteArray|CDWordArray|CPtrArray|CStringArray|CWordArray|CUIntArray|CTypedPtrArray<*,*>{
 preview([$c,!])
 children(
            #(
              #array (
                  expr: $c.m_pData[$i],
                  size: $c.m_nSize
                     )
             )
         )
}

CList<*,*>|CObList|CPtrList|CStringList|CTypedPtrList<*,*>{
 preview([$c,!])
 children(
          #(
              #list  (
                  head: $c.m_pNodeHead,
                  next: pNext
                     ) : $e.data
               )
            )
}

CMap<*,*,*,*>::CAssoc|CMapPtrToWord::CAssoc|CMapPtrToPtr::CAssoc|CMapStringToOb::CAssoc|CMapStringToPtr::CAssoc|CMapStringToString::CAssoc|CMapWordToOb::CAssoc|CMapWordToPtr::CAssoc|CTypedPtrMap<*,*,*>::CAssoc{
preview(#("key= ",$e.key,", value= ", $e.value))
}

CMap<*,*,*,*>|CMapPtrToWord|CMapPtrToPtr|CMapStringToOb|CMapStringToPtr|CMapStringToString|CMapWordToOb|CMapWordToPtr|CTypedPtrMap<*,*,*>{
children (
    #(
        #if ($c.m_nHashTableSize >= 0 && $c.m_nHashTableSize <= 65535) (
            #array (
                expr : ($c.m_pHashTable)[$i],
                size : $c.m_nHashTableSize
                   ) : #list(
                             head : $e,
                             next : pNext
                            ) : $e
            ) #else (
             #(  __ERROR – Hash table too large!!!__: 1,Table size: $c.m_nHashTableSize)
         )
       )
)
}

[EDIT: CAssoc visualizer fix, thanks to @Gerald].

[EDIT: CMap visualizer fix, thanks to @avek]

Here’s what you’d get:

I’m aware of the formatting issues in the snippet above, but the autoexp parser is notoriously fragile and I didn’t want to risk extra spaces for proper line-wrapping.

And btw, unlike Avery (the original autoexp Jedi), I prefer to avoid cluttering the visualizers with ‘raw’ watch entries.  If you ever need to watch into, say, raw members of a CList, just postfix the variable with ‘,!’ , as in:

6 comments on “Visualizing MFC Containers in autoexp.dat

  1. Gerald says:

    if I insert the above text in the Visualizer section of autoexp.dat file, vs2008 sp1 hangs (is busy) if I step over a CMapStringToString declaration. Is there a solution?

    • Ofek Shilon says:

      Gerald – thanks! I posted a fix (I mistakenly pasted an old autoexp snippet, still with ‘raw’ entries and all). Please post whether it works.

      • Gerald says:

        Unfortunately it does not work. VS2008 still hangs as described above.

        Perhaps this information could help: If I uncomment the “CMap|CMapPtrToWord …”-part, the debugger does not hang anymore.

  2. avek says:

    To whom it’s still important: the CMap visualizer from here hangs when it evaluates a CMap instance before its constructor has been run. That is, it’s not working with 0xCCCCCCCC or similar garbage in CMap fields.

    Proper version would be like:
    children
    (
    #(
    #if ($c.m_nHashTableSize >= 0 && $c.m_nHashTableSize <= 65535) (
    #array (
    expr : ($c.m_pHashTable)[$i],
    size : $c.m_nHashTableSize
    ) : #list(
    head : $e,
    next : pNext
    ) : $e
    ) #else (
    #(
    __ERROR – Hash table too large!!!__: 1,
    Table size: $c.m_nHashTableSize
    )
    )
    )
    )

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s