在WinDbg内存窗口中查看结构的语法?

时间:2019-06-14 01:39:50

标签: debugging windbg

因此,无论出于何种原因,我在编程时都不使用IDE。我编译程序并在命令行上生成其相应的PDB。因此,我不介意使用WinDbg进行调试。无论如何,请考虑以下数据:

typedef struct _ex
{
    char *ptr0;
    char *ptr1;
    char *ptr2;
} ex;

ex  *example; // assume example and members point to somewhere memory

如何在“内存”窗口中查看example结构的成员?我尝试过example->ptr0example.ptr0以及在此处和此处放置的*,但始终会遇到Unable to retrieve information错误。我有一个工作区设置,并希望有几个内存窗口在它们进入作用域时准备好一些结构值,而不必每次都复制粘贴地址,或更糟糕的是键入它们。有正确的语法可以做到这一点吗?

1 个答案:

答案 0 :(得分:3)

:>dir /b
disptype.cpp

:>type disptype.cpp
// compile with cl /Zi /W4 /O1 /analyze /nologo disptype.cpp /link /nologo /release
// either disable inlining or compile as debug
// optimisation will blow away stuffstruct function and load rcx,rdx,r8 with argv[]
// and call printf directly
#include <stdio.h>
#include <stdlib.h>
typedef struct _ex
{
    char *ptr0;
    char *ptr1;
    char *ptr2;
} ex;
__declspec(noinline) void stuffstruct (ex *myex,char *a,char *b,char *c) {
                myex->ptr0 = a;
                myex->ptr1 = b;
                myex->ptr2 = c;
                return;
}
int main (int argc, char * argv[])
{
        if(argc != 4)
        {
                printf( "usage %s how are you\n" , argv[0]);
                exit(0);
        }
        ex myex;
        stuffstruct(&myex,argv[1],argv[2],argv[3]);
        printf ("%s repeats 3 argv's \n%s\n%s\n%s\n",argv[0],myex.ptr0,myex.ptr1,myex.ptr2);
        return 0;
}

编译并链接和使用

:>cl /Zi /W4 /O1 /analyze /nologo disptype.cpp /link /nologo /release
disptype.cpp

:>disptype.exe
usage disptype.exe how are you

:>disptype.exe how are you
disptype.exe repeats 3 argv's
how
are
you

:>

windbg(使用cdb方便复制粘贴)

:>cdb disptype.exe how are you

Microsoft (R) Windows Debugger Version 10.0.17763.132 AMD64

$$ go to the relevent function

0:000> g disptype!stuffstruct 
disptype!stuffstruct:
00007ff6`3f541000 488911          mov     qword ptr [rcx],rdx ds:00000044`02cffe60=000000000000001f

$$ run until return so our struct is initialised
0:000> pt
disptype!stuffstruct+0xb:
00007ff6`3f54100b c3              ret

if you have src / private pdb you can look at locals using dv

0:000> dv
           myex = 0x00000044`02cffe60
              a = 0x0000019c`437f65b5 "how"
              b = 0x0000019c`437f65b9 "are"
              c = 0x0000019c`437f65bd "you"

C ++表达式评估器,dt和dx用法的示例

0:000> ?? myex
struct _ex * 0x00000044`02cffe60
   +0x000 ptr0             : 0x0000019c`437f65b5  "how"
   +0x008 ptr1             : 0x0000019c`437f65b9  "are"
   +0x010 ptr2             : 0x0000019c`437f65bd  "you"

you can also ask windbg to display type and coerce pointer 
either with dt or the new dx 

0:000> dt /v disptype!myex

Local var [AddrFlags c8  AddrOff 0000000000000000  Reg/Val rcx (3)] @ rcx Type _ex*

disptype!myex = 4402cffe60
struct _ex, 3 elements, 0x18 bytes
   +0x000 ptr0             : 0x0000019c`437f65b5  "how"
   +0x008 ptr1             : 0x0000019c`437f65b9  "are"
   +0x010 ptr2             : 0x0000019c`437f65bd  "you"


 0:000> dx (disptype!_ex *) @rcx
(disptype!_ex *) @rcx : 0x4402cffe60 [Type: _ex *]
    [+0x000] ptr0             : 0x19c437f65b5 : "how" [Type: char *]
    [+0x008] ptr1             : 0x19c437f65b9 : "are" [Type: char *]
    [+0x010] ptr2             : 0x19c437f65bd : "you" [Type: char *]
0:000>

将地址强制解释为我们的结构

0:000> dx (disptype!_ex *) @rax
(disptype!_ex *) @rax : 0x19c437ff290 [Type: _ex *]
    [+0x000] ptr0             : 0x19c437f6880 : "ALLUSERSPROFILE=C:\ProgramData" [Type: char *]
    [+0x008] ptr1             : 0x19c437f5dc0 : "APPDATA=C:\Users\xxxx\AppData\Roaming" [Type: char *]
    [+0x010] ptr2             : 0x19c437f5e20 : "CommandPromptType=Native" [Type: char *]
0:000>

您正在谈论GUI中的内存窗口(atl +5) 该窗口无法显示类型,它只能将数据显示为预定义类型,例如 位,字节,单词,dword,float,double,string等

设置本地人或手表(在我的拙见中,这两者都很麻烦,用不完的房地产会降低性能等等,但这是我的看法,如果您愿意的话,可以愉快地使用它们)

这是屏幕截图

enter image description here