Windbg c ++如何打印矢量内容

时间:2018-02-22 22:04:52

标签: c++ debugging vector windbg

我有以下代码段

Class Filters {    vector<int> numbers; }

Filters f1;

我需要调试f1.numbers

中的内容
Windbg>
??f1.numbers
class std::vector<bool,std::allocator<bool> >
   +0x000 _Myvec           : std::vector<unsigned int,std::allocator<unsigned int> >
   +0x018 _Mysize          : 7

Windbg>
??f1.numbers._Myvec._Mypair._Myval2
class std::_Vector_val<std::_Simple_types<unsigned int> >
   +0x000 _Myfirst         : 0x00000089`006cf220  -> 0x7e
   +0x008 _Mylast          : 0x00000089`006cf224  -> 0x5c
   +0x010 _Myend           : 0x00000089`006cf224  -> 0x5c

Windbg>
!stl f1.numbers
<NULL>

如何在里面打印7个元素?

1 个答案:

答案 0 :(得分:3)

0:000> lsa .
     1: #include <iostream>
     2: #include <vector>
     3: using namespace std;
     4: int main() {
     5:     vector<int> v = {7, 5, 16, 8};
>    6:     v.push_back(25); 
     7:     v.push_back(13);
     8:     for(int n : v) { std::cout << n << '\n'; }
     9: }

在windbg中

0:000> ?? v._Mypair._Myval2._Myfirst
int * 0x0044abd8
0:000> ?? v._Mypair._Myval2._Myfirst[0]
int 0n7
0:000> ?? v._Mypair._Myval2._Myfirst[1]
int 0n5
0:000> ?? v._Mypair._Myval2._Myfirst[2]
int 0n16
0:000> ?? v._Mypair._Myval2._Myfirst[3]
int 0n8
0:000> ?? v._Mypair._Myval2._Myfirst[4]
int 0n-1414812757

第2步行执行两次回退以查看其余的

0:000> p
0:000> p
0:000> ?? v._Mypair._Myval2._Myfirst[4]
int 0n25
0:000> ?? v._Mypair._Myval2._Myfirst[5]
int 0n13
0:000> ?? v._Mypair._Myval2._Myfirst[6]
int 0n-1414812757

或使用此表达式

0:000> dd /c 1 @@c++(*(int *)&(v._Mypair._Myval2._Myfirst)) L? ((@@c++(*(int *)&(v._Mypair._Myval2._Mylast))) - (@@c++(*(int *)&(v._Mypair._Myval2._Myfirst))))/@@(sizeof(int)))
0044ac00  00000007
0044ac04  00000005
0044ac08  00000010
0044ac0c  00000008
0044ac10  00000019
0044ac14  0000000d

或者如果您有更高版本的windbg,其中natvis javascript等可用,您只需使用dx命令

0:000> dx v
v                 : { size=6 } [Type: std::vector<int,std::allocator<int> >]
    [<Raw View>]     [Type: std::vector<int,std::allocator<int> >]
    [capacity]       : 6
    [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<int>,std::_Vector_val<std::_Simple_types<int> >,1>]
    [0]              : 7 [Type: int]
    [1]              : 5 [Type: int]
    [2]              : 16 [Type: int]
    [3]              : 8 [Type: int]
    [4]              : 25 [Type: int]
    [5]              : 13 [Type: int]

表达式似乎会失败,或者您可能需要大幅修改表达式以打印字符串向量或双向量

dx可以很好地扩展到所有场景

修改后的src和输出以及来自windbg的输出dx

:\>vectie.exe
7
5
16
8
25
13
seven

在windbg中

0:000> lsa .
     1: #include <iostream>
     2: #include <vector>
     3: using namespace std;
     4: int main() {
     5:     vector<int> v = {7, 5, 16, 8};
     6:     v.push_back(25); 7:     v.push_back(13);
     8:     for(int n : v) { std::cout << n << '\n'; }
    10:     vector<string> w = {"seven", "five", "sixteen", "eight"};
    11:     w.push_back("twenty five"); 
    12:     w.push_back("thirteen");
>   13:     cout << w[0].c_str() << '\n';
    15: }
0:000> dx v ; dx w
v                  : { size=6 } [Type: std::vector<int,std::allocator<int> >]
    [<Raw View>]     [Type: std::vector<int,std::allocator<int> >]
    [capacity]       : 6
    [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<int>,std::_Vector_val<std::_Simple_types<int> >,1>]
    [0]              : 7 [Type: int]
    [1]              : 5 [Type: int]
    [2]              : 16 [Type: int]
    [3]              : 8 [Type: int]
    [4]              : 25 [Type: int]
    [5]              : 13 [Type: int]
w                 : { size=6 } [Type: std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >]
    [<Raw View>]     [Type: std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >]
    [capacity]       : 6
    [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >,1>]
    [0]              : "seven" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
    [1]              : "five" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
    [2]              : "sixteen" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
    [3]              : "eight" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
    [4]              : "twenty five" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
    [5]              : "thirteen" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]

假设你有一个声明的结构向量

typedef struct _MYSTRUCT {
    int intie;
    string stringie;
    double doblee;
}Mystruct,*PMystruct;

xxxxxx
    vector<Mystruct> y = {{1,"one",1.0}, {5,"five",5.0}, {16,"sixteen",16.0}};
    y.push_back({25,"twenty five",25.0}); 
    for(int i = 0 ; i < 3; i++){
    cout << "{ " << y[i].intie <<"  "<<y[i].stringie.c_str() << "   "<< y[i].doblee <<" } \n";
    }   

dx将整齐地向下钻取到向量

中每个结构的最后一个成员
0:000> lsp -a 4 ; lsa .
WARNING: Source line display is disabled
At the prompt, display 2 source lines before and 2 after
For lsa commands, display 2 source lines before
For ls and lsa commands, display 4 source lines
    28:     
    29:     vector<Mystruct> y = {{1,"one",1.0}, {5,"five",5.0}, {16,"sixteen",16.0}};
>   30:     y.push_back({25,"twenty five",25.0}); 
    31:     for(int i = 0 ; i < 3; i++){
0:000> dx -r4 y
y                 : { size=4 } [Type: std::vector<_MYSTRUCT,std::allocator<_MYSTRUCT> >]
    [<Raw View>]     [Type: std::vector<_MYSTRUCT,std::allocator<_MYSTRUCT> >]
    [capacity]       : 4
    [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<_MYSTRUCT>,std::_Vector_val<std::_Simple_types<_MYSTRUCT> >,1>]
        [<Raw View>]     [Type: std::_Compressed_pair<std::allocator<_MYSTRUCT>,std::_Vector_val<std::_Simple_types<_MYSTRUCT> >,1>]
    [0]              [Type: _MYSTRUCT]
        [+0x000] intie            : 1 [Type: int]
        [+0x004] stringie         : "one" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [<Raw View>]     [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [size]           : 0x3 [Type: unsigned int]
            [capacity]       : 0xf [Type: unsigned int]
            [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
                [<Raw View>]     [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
            [0]              : 111 'o' [Type: char]
            [1]              : 110 'n' [Type: char]
            [2]              : 101 'e' [Type: char]
        [+0x020] doblee           : 1.000000 [Type: double]
    [1]              [Type: _MYSTRUCT]
        [+0x000] intie            : 5 [Type: int]
        [+0x004] stringie         : "five" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [<Raw View>]     [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [size]           : 0x4 [Type: unsigned int]
            [capacity]       : 0xf [Type: unsigned int]
            [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
                [<Raw View>]     [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
            [0]              : 102 'f' [Type: char]
            [1]              : 105 'i' [Type: char]
            [2]              : 118 'v' [Type: char]
            [3]              : 101 'e' [Type: char]
        [+0x020] doblee           : 5.000000 [Type: double]
    [2]              [Type: _MYSTRUCT]
        [+0x000] intie            : 16 [Type: int]
        [+0x004] stringie         : "sixteen" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [<Raw View>]     [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [size]           : 0x7 [Type: unsigned int]
            [capacity]       : 0xf [Type: unsigned int]
            [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
                [<Raw View>]     [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
            [0]              : 115 's' [Type: char]
            [1]              : 105 'i' [Type: char]
            [2]              : 120 'x' [Type: char]
            [3]              : 116 't' [Type: char]
            [4]              : 101 'e' [Type: char]
            [5]              : 101 'e' [Type: char]
            [6]              : 110 'n' [Type: char]
        [+0x020] doblee           : 16.000000 [Type: double]
    [3]              [Type: _MYSTRUCT]
        [+0x000] intie            : 25 [Type: int]
        [+0x004] stringie         : "twenty five" [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [<Raw View>]     [Type: std::basic_string<char,std::char_traits<char>,std::allocator<char> >]
            [size]           : 0xb [Type: unsigned int]
            [capacity]       : 0xf [Type: unsigned int]
            [allocator]      : allocator [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
                [<Raw View>]     [Type: std::_Compressed_pair<std::allocator<char>,std::_String_val<std::_Simple_types<char> >,1>]
            [0]              : 116 't' [Type: char]
            [1]              : 119 'w' [Type: char]
            [2]              : 101 'e' [Type: char]
            [3]              : 110 'n' [Type: char]
            [4]              : 116 't' [Type: char]
            [5]              : 121 'y' [Type: char]
            [6]              : 32 ' ' [Type: char]
            [7]              : 102 'f' [Type: char]
            [8]              : 105 'i' [Type: char]
            [9]              : 118 'v' [Type: char]
            [10]             : 101 'e' [Type: char]
        [+0x020] doblee           : 25.000000 [Type: double]