BLOG ARTICLE debug | 1 ARTICLE FOUND

  1. 2014.07.11 gdb로 STL 디버깅하기

@ GDB 로 STL 컨테이너들의 내용을 보여주는 gdb macro 파일을 다운 받아 설치합니다.

  • 다운로드 : gdb_stl_view
  • 설치 : 다운로드한 파일을 ~/.gdbinit 파일명으로 복사

 

@ 사용 방법

Data type GDB command
std::vector<T> pvector stl_variable
std::list<T> plist stl_variable T
std::map<T,T> pmap stl_variable
std::multimap<T,T> pmap stl_variable
std::set<T> pset stl_variable T
std::multiset<T> pset stl_variable
std::deque<T> pdequeue stl_variable
std::stack<T> pstack stl_variable
std::queue<T> pqueue stl_variable
std::priority_queue<T> ppqueue stl_variable
std::bitset<n>td> pbitset stl_variable
std::string pstring stl_variable
std::widestring pwstring stl_variable

 

@ 사용 예제

  • 예제 코드
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    string strSource;
    vector stNumList;
    map mapList;
    map::iterator itr;

    strSource = "SOURCE STRING";

    stNumList.push_back(1);
    stNumList.push_back(3);
    stNumList.push_back(4);

    mapList[2] = "Two";
    mapList[1] = "One";
    mapList[5] = "Five";
    mapList[10] = "Ten";
    mapList[7] = "Seven";

    cout << "Source string : " << strSource << endl;

    itr = mapList.find(7);
    if ( itr != mapList.end() ) {
        cout << "mapList(7) = " << itr->second << endl;
    }

    return 1;
}
  • gdb 실행 예제

# gdb test
(gdb) b 27
Breakpoint 1 at 0x8048cdf: file test.cpp, line 27.
(gdb) run

Breakpoint 1, main () at test.cpp:27
27 cout << "Source string : " << strSource << endl;

 

(gdb) pstring strSource
String = "SOURCE STRING"
String size/length = 13
String capacity = 13
String ref-count = 0

 

(gdb) pvector stNumList
elem[0]: $1 = 1
elem[1]: $2 = 3
elem[2]: $3 = 4
Vector size = 3
Vector capacity = 4
Element type = int *

 

(gdb) pmap mapList
Map type =
std::map<int,std::basic_string<char, std::char_traits<char>, std::allocator<char> >,std::less<int>,std::allocator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >
Use pmap <variable_name> <left_element_type> <right_element_type> to see the elements in the map.
Map size = 5
(gdb) pmap mapList int string
elem[0].left: $4 = 1
elem[0].right: No symbol "string" in current context.
(gdb) pmap mapList int char*
elem[0].left: $5 = 1
elem[0].right: $6 = 0x83980c4 "One"
elem[1].left: $7 = 2
elem[1].right: $8 = 0x839808c "Two"
elem[2].left: $9 = 5
elem[2].right: $10 = 0x83980fc "Five"
elem[3].left: $11 = 7
elem[3].right: $12 = 0x839816c "Seven"
elem[4].left: $13 = 10
elem[4].right: $14 = 0x8398134 "Ten"
Map size = 5

 

  • 사용법 예제

(gdb) pvector
Prints std::vector<T> information.
Syntax: pvector <vector> <idx1> <idx2>
Note: idx, idx1 and idx2 must be in acceptable range [0..<vector>.size()-1].
Examples:
pvector v - Prints vector content, size, capacity and T typedef
pvector v 0 - Prints element[idx] from vector
pvector v 1 2 - Prints elements in range [idx1..idx2] from vector
(gdb) plist
Prints std::list<T> information.
Syntax: plist <list> <T> <idx>: Prints list size, if T defined all elements or just element at idx
Examples:
plist l - prints list size and definition
plist l int - prints all elements and list size
plist l int 2 - prints the third element in the list (if exists) and list size
(gdb) pmap
Prints std::map<TLeft and TRight> or std::multimap<TLeft and TRight> information. Works for std::multimap as well.
Syntax: pmap <map> <TtypeLeft> <TypeRight> <valLeft> <valRight>: Prints map size, if T defined all elements or just element(s) with val(s)
Examples:
pmap m - prints map size and definition
pmap m int int - prints all elements and map size
pmap m int int 20 - prints the element(s) with left-value = 20 (if any) and map size
pmap m int int 20 200 - prints the element(s) with left-value = 20 and right-value = 200 (if any) and map size
(gdb) pdeque
Prints std::dequeue<T> information.
Syntax: pdequeue <dequeue>: Prints dequeue size, if T defined all elements
Deque elements are listed "left to right" (left-most stands for front and right-most stands for back)
Example:
pdequeue d - prints all elements and size of d
(gdb) pstack
Prints std::stack<T> information.
Syntax: pstack <stack>: Prints all elements and size of the stack
Stack elements are listed "top to buttom" (top-most element is the first to come on pop)
Example:
pstack s - prints all elements and the size of s

 

@ map iterator 를 gdb 로 보기 위해 추가한 매크로(.gdbinit 파일에 추가)

#
# std:map iterator
#

define pmapitr
if $argc == 0
help pmapitr
else
set $itr = $arg0
set $node = $itr._M_node
if $argc == 1
printf "map iterator "
whatis $itr
printf "Use pmapitr <variable_name> <TtypeLeft> <TypeRight>.\n"
end
if $argc == 3
set $value = (void *)($node + 1)
printf "elem.left: "
p *($arg1*)$value
set $value = $value + sizeof($arg1)
printf "elem.right: "
p *($arg2*)$value
end
end
end

 

document pmapitr
Prints std::map iterator value
Syntax: pmapitr <map>::iterator <TtypeLeft> <TypeRight>
Examples:
pmap_itr m int int - print iterator's element
end

 

  • gdb 실행 예제

# gdb test
(gdb) b 30
Breakpoint 1 at 0x8048d3d: file test.cpp, line 30.
(gdb) run
Source string : SOURCE STRING

Breakpoint 1, main () at test.cpp:30
30 if ( itr != mapList.end() ) {

(gdb) p itr
$1 = {_M_node = 0x82ad140}
(gdb) pmapitr itr
map iterator type =
std::_Rb_tree_iterator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >
Use pmapitr <variable_name> <TtypeLeft> <TypeRight>.

(gdb) pmapitr itr int char*
elem.left: $2 = 7
elem.right: $3 = 0x82ad16c "Seven"
(gdb)

 

 

@ 참고한 URL

http://www.yolinux.com/TUTORIALS/GDB-Commands.html#STLDEREF

AND