Debugging std::string argument

When a function takes a parameter of “std::string const&”, it would be a little trickier to debug than the simple “char const*” case.

#include <string>

extern void print(const std::string& text);

int main(int argc, char** argv){
    print("Hello World. This is a long string!");
    return 0;

When you set a break point in print function, the stack trace would be like this:

ChildEBP RetAddr  Args to Child
0018ff0c 00401043 0018ff1c a6ea238e 0018ff24 big_mem!print+0x2
0018ff44 004011e5 00000001 006179f0 006f1c58 big_mem!main+0x43
0018ff88 768e3677 7efde000 0018ffd4 776e9d72 big_mem!__tmainCRTStartup+0x10f
0018ff94 776e9d72 7efde000 77239523 00000000 kernel32!BaseThreadInitThunk+0xe
0018ffd4 776e9d45 0040132d 7efde000 00000000 ntdll!__RtlUserThreadStart+0x70
0018ffec 00000000 0040132d 7efde000 00000000 ntdll!_RtlUserThreadStart+0x1b

The first argument of the print function is 0x18ff1c. To see the content of the argument, you can dump the memory content as a type of std::string. Since std::string is merely typedef-ed type, you have to specify the real type name. Also note that you have to enclose the type name with (…) since the type name includes a space.

0:000> dt 0018ff1c (std::basic_string<char,std::char_traits<char>,std::allocator<char> >) -r
big_mem!std::basic_string<char,std::char_traits<char>,std::allocator<char> >
   +0x000 _Alval           : std::allocator<char>
   =00400000 npos             : 0x905a4d
   +0x004 _Bx              : std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
      +0x000 _Buf             : [16]  "???"
      +0x000 _Ptr             : 0x006f28e0  "Hello World. This is a long string!"
   +0x014 _Mysize          : 0x23
   +0x018 _Myres           : 0x2f

Interestingly, MSVCP90.DLL (not MSVCP90D.DLL) has a exposed type information called “string”. Therefore, if your program was compiled as release mode, you can specify “string” as a type name instead of the long basic_string type name.

0:000> dt 18ff1c string -r
   +0x000 _Alval           : std::allocator<char>
   =71df132c std::basic_string<char,std::char_traits<char>,std::allocator<char> >::npos : 0xffffffff
   +0x004 _Bx              : std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Bxty
      +0x000 _Buf             : [16]  "???"
      +0x000 _Ptr             : 0x002c28e0  "Hello World. This is a long string!"
   +0x014 _Mysize          : 0x23
   +0x018 _Myres           : 0x2f


About Moto

Engineer who likes coding
This entry was posted in Advanced Debugging. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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