Home > OS >  Print elements of C string vector nicely in GDB
Print elements of C string vector nicely in GDB

Time:12-05

I want to view the content of std::vector<std::string> in GDB nicely

I can view it with just like in this suggestion

print *(myVector._M_impl._M_start)@myVector.size()

But it prints out all stuff that is part of the C STL and it is a bit difficult to view the "actual" content of the strings

Is there any way to view the elements nicely without displaying some part of the STL containers?

CodePudding user response:

Is there any way to view the elements nicely without displaying some part of the STL containers?

You either have a very old GDB, or some non-standard setup.

Here is what it looks like on a Fedora-34 system with default GDB installation:

(gdb) list
1       #include <string>
2       #include <vector>
3
4       int main()
5       {
6         std::vector<std::string> v;
7         v.push_back("abc");
8         v.push_back("abcdef");
9         v.push_back("ghi");
10      }
(gdb) b 9
Breakpoint 1 at 0x401378: file t.cc, line 9.
(gdb) run
Starting program: /tmp/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at t.cc:9
9         v.push_back("ghi");
(gdb) p v
$1 = std::vector of length 2, capacity 2 = {"abc", "abcdef"}
(gdb) n
10      }
(gdb) p v
$2 = std::vector of length 3, capacity 4 = {"abc", "abcdef", "ghi"}
(gdb) q
  • Related