2009-10-23 28 views
7

Người dùng gdb ở đây nghĩ gì về khả năng của nó liên quan đến việc gỡ lỗi mã với các mẫu và STL?gỡ lỗi mã C++ với các mẫu và STL với gdb

Bạn có sử dụng bất kỳ thủ thuật nào để gỡ lỗi đơn giản hơn không? Có lẽ một số kịch bản Python? Hoặc bạn có hài lòng với cách nó hiện diện trong gdb (ver 6.x, chưa thử 7.x chưa)?

Cảm ơn.

+0

Cảm ơn, Amro và dirkgently. Tôi mong đợi để có được GDB 7.0 trong máy Debian của tôi (chạy thử nghiệm) trong một vài ngày. Trong khi đó, tôi đang cố gắng để xem làm thế nào để làm các thiết lập trong .gdbinit chỉ là đúng cách. Trân trọng. – user193272

Trả lời

5

Tôi giả định rằng bạn có nghĩa là hình dung mã STL tốt hơn (và không phải là debug mode đó cho lặp an toàn và kiểm tra bổ sung khi chạy). Tôi không chắc chắn nếu bạn đã xem xét những bài viết:

Sử dụng gdb

Bắt đầu với phiên bản 7.0, GDB bao gồm hỗ trợ cho các văn bản khá-in bằng Python . Máy in đẹp cho các lớp STL được phân phối với GCC từ phiên bản 4.5.0. Phiên bản mới nhất của các máy in này luôn được tìm thấy trong kho libstdC++ svn. Để bật các máy in, check-out máy in mới nhất vào một thư mục địa phương:

Ngoài ra, hãy thử sử dụng KDevelop/DDD nếu có thể - họ làm giúp đỡ.

1

Cách yêu thích của tôi để sử dụng GDB là chế độ GDB trong các emacs. Bạn nhận được đầy đủ hình ảnh/nguồn cấp gỡ lỗi, cửa sổ chủ đề, cửa sổ ngăn xếp (vv) ... Hãy thử nó ra, bạn sẽ không phải thất vọng.

Điều đó nói rằng, GDB xử lý gỡ lỗi của STL container chỉ tốt với không add ons đặc biệt ... Chỉ cần chắc chắn rằng bạn đang xây dựng VỚI -g, và không có -on (dưới mọi hình thức) ...

0

ddd là tuyệt vời quá - yêu thích của tôi!

1

Tôi không chắc liệu bạn có được phép thêm mã hay bạn chỉ đang gỡ lỗi mã, Xin lỗi. Tôi đã viết một chức năng tiện ích đơn giản từ một thời gian, tôi hy vọng bạn thấy nó hữu ích. Bạn có thể in nội dung của các thùng chứa tiêu chuẩn dễ dàng. Không có mã cụ thể cho nền tảng, ví dụ về cách sử dụng (trình điều khiển thử nghiệm thực sự):

#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <string> 

#include <vector> 
#include <list> 
#include <stack> 
#include <queue> 
#include <deque> 
#include <set> 
#include <map> 

#include <boost/array.hpp> 
#include <boost/assign.hpp> 
#include "streamer.hpp" 


const std::size_t consoleWidth = 80; 

std::ostream& newline_if_not_console(std::ostream& outputstream) 
{ 
    if(&outputstream != & std::cout) 
    { 
     outputstream << std::endl; 
    } 

    return outputstream; 
} 

void STL_test_ostream(std::ostream& out) 
{ 
    using namespace boost::assign; 
    using namespace streamer; 

    double iDoubleArray[] = {0.1, 1.2, 2.3, 3.4, 4.5}; // It could be of any type! 
    std::vector<int>    iVec; 
    std::list<int>     iList; 
    std::deque<int>     iDeque; 
    std::stack<int>     iStack; 
    std::queue<int>     iQueue; 
    std::priority_queue<int>  iPriorityQueue; 
    std::set<int>     iSet; 
    std::map<int, std::string>  iMap; 

    iVec   += 0, 1, 2, 3, 4, 5; 
    iList   += 0, 1, 2, 3, 4, 5; 
    iDeque   += 0, 1, 2, 3, 4, 5; 
    iStack   += 0, 1, 2, 3, 4, 5; 
    iQueue   += 0, 1, 2, 3, 4, 5; 
    iPriorityQueue += 0, 1, 2, 3, 4, 5; 
    iSet   += 0, 1, 2, 3, 4, 5; 
    insert(iMap) 
     ( 1 , "one" ) 
     ( 2 , "two" ) 
     ( 3 , "three") 
     ( 4 , "four" ) 
     ( 5 , "five" ); 

    out << std::string(consoleWidth, '=') << newline_if_not_console 
     << "STL Test..." << std::endl 
     << std::string(consoleWidth, '=') << newline_if_not_console; 

    out << "Native Array = " << iDoubleArray << std::endl; 
    out << "vector   = " << iVec   << std::endl; 
    out << "list   = " << iList   << std::endl; 
    out << "deque   = " << iDeque   << std::endl; 
    out << "queue   = " << iQueue   << std::endl; 
    out << "stack   = " << iStack   << std::endl; 
    out << "priority_queue = " << iPriorityQueue << std::endl; 
    out << "set   = " << iSet   << std::endl; 
    out << "map   = " << iMap   << std::endl; 

    out << std::string(consoleWidth, '=') << std::endl; 
} 

void Boost_test_ostream(std::ostream& out) 
{ 
    out << std::string(consoleWidth, '=') << newline_if_not_console 
    << "Boost Test..." << std::endl 
    << std::string(consoleWidth, '=') << newline_if_not_console; 

} 

int main() 
{ 
    std::ofstream stl("STL_test_ostream.txt"), 
       boost("Boost_test_ostream.txt"); 

    STL_test_ostream(std::cout); 
    Boost_test_ostream(std::cout); 

    STL_test_ostream(stl); 
    Boost_test_ostream(boost); 
} 

Tôi chưa viết mã cho vùng chứa Tăng cường. Hy vọng rằng, tôi sẽ làm điều đó đôi khi :)

Tất cả những gì bạn phải làm là bao gồm tập tin này [ "streamer.hpp"]:

#ifndef DATASTRUCTRE_STREAMER 
#define DATASTRUCTRE_STREAMER 

#include <stack> 
#include <queue> 
#include <boost/array.hpp> 
#include <functional> 
#include <memory> 

namespace streamer 
{ 

    // one-value data structure streaming function 
    template <class Container, class Stream> 
    Stream& printOneValueContainer(Stream& outputstream, const Container& container) 
    { 
     Container::const_iterator beg = container.begin(); 

     outputstream << "["; 

     while(beg != container.end()) 
     { 
      outputstream << " " << *beg++; 
     } 

     outputstream << " ]"; 

     return outputstream; 
    } 

    // pair-value data structure streaming function 
    template <class Container, class Stream> 
    Stream& printPairValueContainer(Stream& outputstream, const Container& container) 
    { 
     Container::const_iterator beg = container.begin(); 

     outputstream << "["; 

     while(beg != container.end()) 
     { 
      outputstream << " " << "<" << beg->first << " , " << beg->second << ">"; 
      beg++; 
     } 

     outputstream << " ]"; 

     return outputstream; 
    } 



    /* 
    ************************************************************* 
    C++ Standard Library 
    ************************************************************* 
    */ 

    // Sequence Containers. 

    // vector, list, deque 
    template 
    < class Type 
    , template<class Type, class Allocator = std::allocator<Type> > class Container 
    , class Stream 
    > 
    Stream& operator<<(Stream& outputstream, const Container<Type>& container) 
    { 
     return printOneValueContainer(outputstream, container); 
    } 

    // Associative Containers. 

    // set, multiset 
    template 
     < class Key 
     , template<class KeyType, class Traits = std::less<KeyType>, class Allocator = std::allocator<KeyType> > class Container 
     , class Stream 
     > 
    Stream& operator<<(Stream& outputstream, const Container<Key>& container) 
    { 
     return printOneValueContainer(outputstream, container); 
    } 

    // map, multimap 
    template 
     < class Key, class Value 
     , template<class KeyType, class ValueType, class Traits = std::less<KeyType>, class Allocator = std::allocator<std::pair<const KeyType, ValueType> > > class Container 
     , class Stream 
     > 
    Stream& operator<<(Stream& outputstream, const Container<Key, Value>& container) 
    { 
     return printPairValueContainer(outputstream, container); 
    } 

    // Adapters. 

    // stack, queue 
    template < class Type, class Container > 
    const Container& container(const std::stack<Type, Container>& stack) 
    { 
     struct HackedStack : private std::stack<Type, Container> 
     { 
      static const Container& container(const std::stack<Type, Container>& stack) 
      { 
       return stack.*&HackedStack::c; 
      } 
     }; 

     return HackedStack::container(stack); 
    } 

    template < class Type, class Container > 
    const Container& container(const std::queue<Type, Container>& queue) 
    { 
     struct HackedQueue : private std::queue<Type, Container> 
     { 
      static const Container& container(const std::queue<Type, Container>& queue) 
      { 
       return queue.*&HackedQueue::c; 
      } 
     }; 

     return HackedQueue::container(queue); 
    } 

    template 
     < class Type 
     , template <class Type, class Container = std::deque<Type> > class Adapter 
     , class Stream 
     > 
    Stream& operator<<(Stream& outputstream, const Adapter<Type>& adapter) 
    { 
     return printOneValueContainer(outputstream, container(adapter)); 
    } 

    // priority_queue 
    template < class Type, class Container, class Compare > 
    const Container& container(const std::priority_queue<Type, Container, Compare>& priorityQue) 
    { 
     struct HackedProiorityQueue : private std::priority_queue<Type, Container, Compare> 
     { 
      static const Container& container(const std::priority_queue<Type, Container, Compare>& priorityQue) 
      { 
       return priorityQue.*&HackedProiorityQueue::c; 
      } 
     }; 

     return HackedProiorityQueue::container(priorityQue); 
    } 

    template < class Type, class Container, class Compare, class Stream > 
    Stream& operator<<(Stream& outputstream, const std::priority_queue<Type, Container, Compare>& adapter) 
    { 
     return printOneValueContainer(outputstream, container(adapter)); 
    } 

    /* 
    ************************************************************* 
    C++ Native Arrays 
    ************************************************************* 
    */ 

    template <class Type, std::size_t size, class Stream> 
    Stream& operator<<(Stream& outputstream, Type (&array)[size]) 
    { 
     outputstream << "["; 

     for(std::size_t i = 0; i < size; ++i) 
     { 
      outputstream << " " << array[i]; 
     } 

     outputstream << " ]"; 

     return outputstream; 
    } 

    /* 
    ************************************************************* 
     Boost 
    ************************************************************* 
    */ 
} 

#endif 
+0

Cảm ơn mẫu mã và ví dụ. Hiện tại tôi đang cố gắng không sử dụng bất kỳ mã bên ngoài nào cho việc này.Lý tưởng nhất, tôi muốn gdb để làm điều đó tự nhiên (khá in ấn đã được đề cập) hoặc thực hiện một kịch bản cho gdb giúp nó để làm như vậy. Từ các câu trả lời khác, tôi đang ngân hàng trên phiên bản mới của gdb để được tốt hơn về mặt này. Và, btw, tôi đang viết mã của riêng tôi. Trân trọng. – user193272

Các vấn đề liên quan