Inspecting Standard Container (Std::Map) Contents with Gdb

Inspecting standard container (std::map) contents with gdb

I think there isn't, at least not if your source is optimized etc. However, there are some macros for gdb that can inspect STL containers for you:

http://sourceware.org/ml/gdb/2008-02/msg00064.html

However, I don't use this, so YMMV

Having GDB print a big std::map fully while debugging


Do you know any simpler way to print everything ?

(gdb) set print elements 0

Documentation.

How to inspect an individual element in a c++ map using GDB and access members of an object pointed by the element in the map


Can you please let me know how this is done in GDB?

You could always do this: (gdb) p *(Parent*) 0xa85ed0.

How can I get specific std::map value with indexing while using gdb for debuggin c++ code?

You first write the below code in the ".gdbinit" file.

define newstr
set ($arg0)=(std::string*)malloc(sizeof(std::string))
call ($arg0)->basic_string()
# 'assign' returns *this; casting return to void avoids printing of the
struct.
call (void)( ($arg0)->assign($arg1) )
end

define delstr
call ($arg0)->~basic_string(0)
# ^
call free($arg0)
set ($arg0)=(void*)0
end

And then write the below function in your c++ code.

void indexing_map( map<string,[type]> & m, string i )
{
cout << m[i] << "\n";
}

Now, all things are ready. You just execute gdb for debugging your code with the

newstr $foo [any string of map of the key]

For example,

example.cpp

#include <iostream>
#include <unordered_map>
#include <string>

using std::cout;
using std::unordered_map;
using std::string;

void indexing_map(unordered_map<string, int> &m, string i)
{
cout << m[i] << "\n";
}

int main()
{
unordered_map<string, int> st_in;
st_in["1"] = 1;
st_in["2"] = 2;
st_in["3"] = 3;

indexing_map(st_in, "1");

return 0;
}

and gdb

(gdb) b 25
Breakpoint 1 at 0x28d6: file ctest.cpp, line 25.
(gdb) r
Starting program: /home/user/ctest
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, main () at ctest.cpp:25
25 st_in["3"] = 3;
(gdb) newstr $foo "1"
(gdb) call indexing_map(st_in , *$foo)
1

How do I examine the contents of an std::vector in gdb, using the icc compiler?

Not sure this will work with your vector, but it worked for me.

#include <string>
#include <vector>

int main() {
std::vector<std::string> vec;
vec.push_back("Hello");
vec.push_back("world");
vec.push_back("!");
return 0;
}

gdb:

(gdb) break source.cpp:8
(gdb) run
(gdb) p vec.begin()
$1 = {
_M_current = 0x300340
}
(gdb) p $1._M_current->c_str()
$2 = 0x3002fc "Hello"
(gdb) p $1._M_current +1
$3 = (string *) 0x300344
(gdb) p $3->c_str()
$4 = 0x30032c "world"


Related Topics



Leave a reply



Submit