Search code examples
c++visual-studiovisual-c++windbgwdk

windows cdb.exe how to display element in array?


I want to use cdb.exe to debug c++ program.

dv /t to display variables and type

0:000> dv /t
int argc = 0n2
char ** argv = 0x0000027f`b6e97310
char * help_text = 0x00007ff6`180a7600 "xxxxxx"

But how to display the elements of array "argv", like argv[0] or argv[1]?

I tried dv argv[0], not work.


Solution

  • Great! You already figured out how to set a breakpoint on main(). Here's how your starting point looks on my machine:

    0:000> dv /t
    int argc = 0n4
    char ** argv = 0x00a0e610
    

    Using C++ expression evaluation with MASM enabled

    The default expression is MASM. If you're in some other mode, you can switch to MASM using .expr:

    0:000> .expr /s masm
    Current expression evaluator: MASM - Microsoft Assembler expressions
    

    In MASM mode, use C++ expressions with ??:

    0:000> ?? argc
    int 0n4
    
    0:000> ?? argv[1]
    char * 0x00a0e65c
     "Test123"
    0:000> ?? argv[2]
    char * 0x00a0e664
     "Hello"
    0:000> ?? argv[3]
    char * 0x00a0e66a
     "Debugging"
    

    Or you can use the even more tedious @@c++ syntax with other commands like da or du for strings, ? for fundamental types, and dpfor pointers. Here are some examples:

    0:000> ? @@c++(argc)
    Evaluate expression: 4 = 00000004
    0:000> da @@c++(argv[1])
    00a0e65c  "Test123"
    0:000> da @@c++(argv[2])
    00a0e664  "Hello"
    0:000> da @@c++(argv[3])
    00a0e66a  "Debugging"
    

    Switching to C++ expressions

    In order to always use C++-like expressions, set them as default:

    0:000> .expr /s c++
    Current expression evaluator: C++ - C++ source expressions
    

    And then use the "normal" commands.

    0:000> ?  argc
    Evaluate expression: 4 = 00000004
    
    0:000> da argv[1]
    00a0e65c  "Test123"
    0:000> da argv[2]
    00a0e664  "Hello"
    0:000> da argv[3]
    00a0e66a  "Debugging"
    
    0:000> dp argv L argc
    00a0e610  00a0e624 00a0e65c 00a0e664 00a0e66a
    0:000> da 0x00a0e65c
    00a0e65c  "Test123"
    

    Is switching to C++ a good idea?

    Switching to C++ expressions may break some scripts you find on the Internet. Some of them are written with MASM in mind. There are subtle differences and I can't tell you all of them (simply because I don't know), but maybe one example is enough:

    0:000> .expr /s masm
    Current expression evaluator: MASM - Microsoft Assembler expressions
    0:000> dp 008ff950 L4
    008ff950  00a0e610 00a05868 00000004 00a0e610
    
    0:000> .expr /s c++
    Current expression evaluator: C++ - C++ source expressions
    0:000> dp 008ff950 L4
    00000000  ???????? ???????? ???????? ????????
    0:000> dp 0x008ff950 L4
    008ff950  00a0e610 00a05868 00000004 00a0e610