Windbg c++ how to print vector contents

2

I have the below code snippet

Class Filters {    vector<int> numbers; }

Filters f1;

I need to debug the content inside 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>

How do I print the 7 elements inside?

c++
debugging
vector
windbg
asked on Stack Overflow Feb 22, 2018 by sparco • edited Feb 22, 2018 by sparco

1 Answer

6
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: }

in 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

step 2 lines to execute both the push backs to see the rest

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

or use this expression

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

or if you have later versions of windbg where natvis javascript etc are available you can simply use dx command

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]

the expression will appear to fail or you may need to modify the expression substantially to print a string vector or a double vector

dx will scale nicely to all scenerios

The modified src and output and dx output from windbg

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

in 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> >]

assuming you have a vector of structs declared thus

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 will neatly drill down to the last member of every structure in the vector

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]
answered on Stack Overflow Feb 23, 2018 by blabb • edited Feb 23, 2018 by blabb

User contributions licensed under CC BY-SA 3.0