1 Title: Debugging C++ with less pain
2 Tags: en, gdb, c++, stl, debug, python
4 It turns out GDB__ 7.0+ can be extended through Python__ scripts, for instance,
5 to add pretty-printers. And it turns out GCC__ 4.5 comes with some good
6 pretty-printers for GDB.
8 __ http://www.gnu.org/software/gdb/
9 __ http://www.python.org/
10 __ http://gcc.gnu.org/
12 Do you want to see the result of that combination?
24 8 std::string s = "hello world";
25 9 std::vector<std::string> v;
27 11 v.push_back("nice");
28 12 std::map<std::string, std::vector<std::string> > m;
30 14 v.push_back("yeah");
38 Breakpoint 1 at 0x400f86: file p.cpp, line 16.
40 Starting program: /tmp/p
42 Breakpoint 1, main () at p.cpp:16
45 $1 = std::map with 2 elements = {
46 ["hello world"] = std::vector of length 2, capacity 2 = {"hello world", "nice"},
47 ["lala"] = std::vector of length 3, capacity 3 = {"hello world", "nice", "yeah"}
53 The only missing step is configuration, because most distribution don't do the
54 integration themselves yet (or don't have packages with the scripts).
56 Here are 3 quick steps to make it all work::
58 $ mkdir ~/.gdb # can be stored anywhere really
59 $ svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python ~/.gdb/python
60 $ cat << EOT > ~/.gdbinit
63 sys.path.insert(0, '/home/$HOME/.gdb/python')
64 from libstdcxx.v6.printers import register_libstdcxx_printers
65 register_libstdcxx_printers (None)
71 If like to suffer once in a while you can get the raw values using ``/r``::
75 _M_impl = {<std::allocator<std::_Rb_tree_node<std::pair<std::basic_string<char, std::char_traits<char>,
76 std::allocator<char> > const, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
77 std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >> =
78 {<__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char>
79 > const, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
80 std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >> = {<No data fields>}, <No
82 _M_key_compare = {<std::binary_function<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
83 std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool>> = {<No data fields>}, <No data fields>},
85 _M_color = std::_S_red, _M_parent = 0x6070b0, _M_left = 0x6070b0,
86 _M_right = 0x607190}, _M_node_count = 2}}}
88 Looks more familiar? I guess you won't miss it! =P
91 .. vim: set et sw=3 sts=3 :