我的GDB有问题吗?

时间:2015-08-17 21:07:43

标签: c++ gdb

我的程序挂起很难(左近冻结我的整个计算机),我试图找出它发生的位置。我已经浏览了一些 gdb 教程和其他内容 - 并且很好地遵循了它们。然而,每当我看到 gdb 输出时,它有点干净和可读(也就是说,它的输出似乎至少与它正在调试的文件有关)。

这是我的gdb输出的块:

23  std::map<std::string, int> Item::getStats() { return enhancements_; };
(gdb) step
Python Exception <class 'ValueError'> Cannot find type const std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> > >::_Rep_type: 
map (__x=std::map with 2 elements, this=0x7fffffffd0c0)
    at src/item.cpp:23
23  std::map<std::string, int> Item::getStats() { return enhancements_; };
(gdb) step
_Rb_tree (__x=..., this=0x7fffffffd0c0) at src/item.cpp:23
23  std::map<std::string, int> Item::getStats() { return enhancements_; };
(gdb) step
_Rb_tree_impl (__a=<optimized out>, __comp=..., this=0x7fffffffd0c0)
    at /usr/include/c++/4.9/bits/stl_tree.h:474
474         _M_header(), _M_node_count(0)
(gdb) step
475       { _M_initialize(); }
(gdb) step
_M_initialize (this=0x7fffffffd0c0)
    at /usr/include/c++/4.9/bits/stl_tree.h:484
484         this->_M_header._M_left = &this->_M_header;
(gdb) 
485         this->_M_header._M_right = &this->_M_header;
(gdb) step
_Rb_tree (__x=..., this=0x7fffffffd0c0)
    at /usr/include/c++/4.9/bits/stl_tree.h:674
674     if (__x._M_root() != 0)
(gdb) step
_M_root (this=0x7fffffffd038)
    at /usr/include/c++/4.9/bits/stl_tree.h:498
498       { return this->_M_impl._M_header._M_parent; }

我的gdb是否存在问题,或者它通常是所谓的读取方式?我没有看到来自main.cpp之外的其他头文件或代码的任何内容。上面只有三行似乎与我的代码有关。

编辑: 我在Ubuntu 14.04 w / Python 2.7.6上使用G ++ 4.9.2和GDB 7.7.1

&#39;输出&#39;是:

(gdb) where
#0  _int_malloc (av=0x7ffff7840760 <main_arena>, bytes=48) at malloc.c:3775
#1  0x00007ffff75047b0 in __GI___libc_malloc (bytes=48) at malloc.c:2891
#2  0x00007ffff7aebf18 in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00000000004016f7 in allocate (__n=1, this=0x7fffffffd0c0) at /usr/include/c++/4.9/ext/new_allocator.h:104
#4  allocate (__n=1, __a=...) at /usr/include/c++/4.9/bits/alloc_traits.h:357
#5  _M_get_node (this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_tree.h:385
#6  _M_create_node<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> const&> (this=0x7fffffffd0c0)
    at /usr/include/c++/4.9/bits/stl_tree.h:417
#7  _M_clone_node (__x=0xa7b82fa0, this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_tree.h:445
#8  std::_Rb_tree<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::_M_copy (this=this@entry=0x7fffffffd0c0, __x=0xa7b82fa0, __p=0xa7b82fa0, 
    __p@entry=0x7fffffffd0c8) at /usr/include/c++/4.9/bits/stl_tree.h:1219
#9  0x0000000000401cc5 in _Rb_tree (__x=..., this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_tree.h:676
Python Exception <class 'ValueError'> Cannot find type const std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> > >::_Rep_type: 
#10 map (__x=std::map with 2 elements, this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_map.h:183
#11 Item::getStats (this=0x7fffffffd020) at src/item.cpp:23
#12 0x00000000004011a5 in main () at src/main.cpp:26

我可以在堆栈中上下移动,回溯,设置休息以及类似的东西 - 但所有输出的格式都相似。我期待看到一些更清洁的东西,与我调试的代码更相关。类似this教程中的内容:

Breakpoint 1, LinkedList<int>::remove (this=0x40160, 
    item_to_remove=@0xffbef014) at main.cc:52
52      Node<T> *marker = head_;
(gdb) step
53      Node<T> *temp = 0;  // temp points to one behind as we iterate
(gdb) 
55      while (marker != 0) {
(gdb) 
56        if (marker->value() == item_to_remove) {
(gdb) 
Node<int>::value (this=0x401b0) at main.cc:30
30    const T& value () const { return value_; }
(gdb) 
LinkedList<int>::remove (this=0x40160, item_to_remove=@0xffbef014)
    at main.cc:75
75        marker = 0;  // reset the marker
(gdb) 
76        temp = marker;
(gdb) 
77        marker = marker->next();
(gdb) 
Node<int>::next (this=0x0) at main.cc:28
28    Node<T>* next () const { return next_; }
(gdb) 

Program received signal SIGSEGV, Segmentation fault.
Node<int>::next (this=0x0) at main.cc:28
28    Node<T>* next () const { return next_; }
(gdb)

与此相比,雷似乎难以理解。

1 个答案:

答案 0 :(得分:0)

我不知道为什么你的程序会冻结整个计算机,但问题很明显就在这里:

75        marker = 0;  // reset the marker
76        temp = marker;
77        marker = marker->next();

如果忽略第76行,只是阅读代码,那么它似乎显然是错误的。也许你打算写:

marker = marker->next();