0

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?

  • 2
    Search for the pretty print option. – sweenish Dec 03 '21 at 04:43
  • Try https://github.com/cyrus-and/gdb-dashboard – Nimrod Dec 03 '21 at 08:05
  • gdb has pretty-printers for that. At least in linux distributions, gdb already comes with pretty-printers for stl containers and just `print myVector` will do just what you want. The vector pretty-printer nicely prints each element, which in turn uses the corresponding pretty-printer of that element type for the actual printing of the element. If that's not the case for you, then we need more information regarding how you installed gdb. – darcamo Dec 03 '21 at 21:57
  • Direct link to the pretty-printer method: [c++ - How to pretty-print STL containers in GDB? - Stack Overflow](https://stackoverflow.com/questions/11606048/how-to-pretty-print-stl-containers-in-gdb) – user202729 Dec 04 '21 at 07:22
  • @user202729 You should make that an answer or even better, a duplicate :-) – Klaus Dec 04 '21 at 08:54
  • Does this answer your question? [How to pretty-print STL containers in GDB?](https://stackoverflow.com/questions/11606048/how-to-pretty-print-stl-containers-in-gdb) – Klaus Dec 04 '21 at 08:55
  • @Klaus Not sure if it's really a duplicate, as that one seems to focus solely on using the pretty-printer while the "specific" questions like this one can have specific answers (also somewhat useful for old gdb version?) – user202729 Dec 04 '21 at 08:55
  • Similar to the question OP linked. The alternative option is to cast it to C array. – user202729 Dec 04 '21 at 08:58
  • @user202729 The duplicate link also solves printing std::string and is the standard method from gdb installations. Maybe also this one: https://stackoverflow.com/questions/4985414/how-to-enable-gdb-pretty-printing-for-c-stl-objects-in-eclipse-cdt Whatever the best answer is, all answers tell how to setup gdbinit and how to enable the python printer which is delivered with gdb. And this definitly solves the issue :-) – Klaus Dec 04 '21 at 09:00

1 Answers1

0

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
Employed Russian
  • 199,314
  • 34
  • 295
  • 362