簡體   English   中英

Windbg C ++如何打印矢量內容

[英]Windbg c++ how to print vector contents

我有以下代碼片段

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個元素?

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

或者,如果您有可用的natvis javascript等更高版本的windbg,則只需使用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可以很好地擴展到所有場景

從windbg修改后的src和輸出以及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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM