天天看點

支援stl容器的gdb自定義指令

#

# 使用方法:

# 将以下内容追加到~/.gdbinit檔案的尾部,然後再啟動gdb,如果gdb已經啟動,則可以source ~/.gdbinit來立即生效。

##########################################

#                                        #

#   STL GDB evaluators/views/utilities   #

#   The new GDB commands:                                                         

#    are entirely non instrumental                                             

#    do not depend on any "inline"(s) - e.g. size(), [], etc

#       are extremely tolerant to debugger settings

#                                                                                 

#   This file should be "included" in .gdbinit as following:

#   source stl-views.gdb or just paste it into your .gdbinit file

#   The following STL containers are currently supported:

#       std::vector -- via pvector command

#       std::list -- via plist command

#       std::map -- via pmap command

#       std::multimap -- via pmap command

#       std::set -- via pset command

#       std::multiset -- via pset command

#       std::deque -- via pdequeue command

#       std::stack -- via pstack command

#       std::queue -- via pqueue command

#       std::priority_queue -- via ppqueue command

#       std::bitset -- via pbitset command

#       std::string -- via pstring command

#       std::widestring -- via pwstring command

#   The end of this file contains (optional) C++ beautifiers

##########################################################################

#                                                                        #

# CopyLefty @ 2008 - Dan C Marinescu - No Rights Reserved / GPL V3.      #

# Inspired by intial work of Tom Malnar and many others                 #

#  (do whatever you want with this code, hope it helps)                 #

#   Email: [email protected]                                     #

# std::vector

define pvector

if $argc == 0

help pvector

else

set $size = $arg0._M_impl._M_finish - $arg0._M_impl._M_start

set $capacity = $arg0._M_impl._M_end_of_storage - $arg0._M_impl._M_start

set $size_max = $size - 1

end

if $argc == 1

set $i = 0

while $i

printf "elem[%u]: ", $i

p *($arg0._M_impl._M_start + $i)

set $i++

if $argc == 2

set $idx = $arg1

if $idx $size_max

printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max

printf "elem[%u]: ", $idx

p *($arg0._M_impl._M_start + $idx)

if $argc == 3

 set $start_idx = $arg1

 set $stop_idx = $arg2

 if $start_idx > $stop_idx

   set $tmp_idx = $start_idx

   set $start_idx = $stop_idx

   set $stop_idx = $tmp_idx

 end

 if $start_idx $size_max || $stop_idx > $size_max

   printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max

 else

   set $i = $start_idx

if $argc > 0

printf "Vector size = %u\n", $size

printf "Vector capacity = %u\n", $capacity

printf "Element "

whatis $arg0._M_impl._M_start

document pvector

Prints std::vector information.

Syntax: pvector

Note: idx, idx1 and idx2 must be in acceptable range [0...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

end 

# std::list

define plist

help plist

set $head = &$arg0._M_impl._M_node

set $current = $arg0->_M_impl->_M_node->_M_next

set $size = 0

while $current != $head

printf "elem[%u]: ", $size

p *($arg1*)($current + 1)

if $size == $arg2

set $current = $current->_M_next

set $size++

printf "List size = %u \n", $size

printf "List "

whatis $arg0

printf "Use plist to see the elements in the list.\n"

document plist

Prints std::list information.

Syntax: plist : Prints list size, if T defined all elements or just element at idx

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

# std::map and std::multimap

define pmap

help pmap

set $tree = $arg0

set $node = $tree->_M_t->_M_impl->_M_header->_M_left

set $end = $tree->_M_t->_M_impl->_M_header

set $tree_size = $tree->_M_t->_M_impl->_M_node_count

printf "Map "

whatis $tree

printf "Use pmap to see the elements in the map.\n"

set $value = (void *)($node + 1)

printf "elem[%u]->left: ", $i

p *($arg1*)$value

set $value = $value + 4

printf "elem[%u]->right: ", $i

p *($arg2*)$value

if $node->_M_right != 0

set $node = $node->_M_right

while $node->_M_left != 0

set $node = $node->_M_left

set $tmp_node = $node->_M_parent

while $node == $tmp_node->_M_right

set $node = $tmp_node

set $tmp_node = $tmp_node->_M_parent

if $node->_M_right != $tmp_node

if $argc == 4

set $idx = $arg3

set $ElementsFound = 0

if *($arg1*)$value == $idx

set $ElementsFound++

printf "Number of elements found = %u\n", $ElementsFound

if $argc == 5

set $idx1 = $arg3

set $idx2 = $arg4

set $valueLeft = *($arg1*)$value

set $valueRight = *($arg2*)($value + 4)

if $valueLeft == $idx1 && $valueRight == $idx2

p $valueLeft

p $valueRight

printf "Map size = %u\n", $tree_size

document pmap

Prints std::map or std::multimap information. Works for std::multimap as well.

Syntax: pmap : Prints map size, if T defined all elements or just element(s) with val(s)

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

# std::set and std::multiset

define pset

help pset

printf "Set "

printf "Use pset to see the elements in the set.\n"

set $idx = $arg2

printf "Set size = %u\n", $tree_size

document pset

Prints std::set or std::multiset information. Works for std::multiset as well.

Syntax: pset : Prints set size, if T defined all elements or just element(s) having val

pset s - prints set size and definition

pset s int - prints all elements and the size of s

pset s int 20 - prints the element(s) with value = 20 (if any) and the size of s

# std::dequeue

define pdequeue

help pdequeue

set $start_cur = $arg0._M_impl._M_start._M_cur

set $start_last = $arg0._M_impl._M_start._M_last

set $start_stop = $start_last

while $start_cur != $start_stop

p *$start_cur

set $start_cur++

set $finish_first = $arg0._M_impl._M_finish._M_first

set $finish_cur = $arg0._M_impl._M_finish._M_cur

set $finish_last = $arg0._M_impl._M_finish._M_last

if $finish_cur

set $finish_stop = $finish_cur

set $finish_stop = $finish_last

while $finish_first != $finish_stop

p *$finish_first

set $finish_first++

printf "Dequeue size = %u\n", $size

document pdequeue

Prints std::dequeue information.

Syntax: pdequeue : 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

# std::stack

define pstack

help pstack

set $start_cur = $arg0.c._M_impl._M_start._M_cur

set $finish_cur = $arg0.c._M_impl._M_finish._M_cur

set $size = $finish_cur - $start_cur

        set $i = $size - 1

        while $i >= 0

            p *($start_cur + $i)

            set $i--

        end

printf "Stack size = %u\n", $size

document pstack

Prints std::stack information.

Syntax: pstack : 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)

pstack s - prints all elements and the size of s

# std::queue

define pqueue

help pqueue

        set $i = 0

        while $i

            set $i++

printf "Queue size = %u\n", $size

document pqueue

Prints std::queue information.

Syntax: pqueue : Prints all elements and the size of the queue

Queue elements are listed "top to bottom" (top-most element is the first to come on pop)

pqueue q - prints all elements and the size of q

# std::priority_queue

define ppqueue

help ppqueue

set $size = $arg0.c._M_impl._M_finish - $arg0.c._M_impl._M_start

set $capacity = $arg0.c._M_impl._M_end_of_storage - $arg0.c._M_impl._M_start

set $i = $size - 1

while $i >= 0

p *($arg0.c._M_impl._M_start + $i)

set $i--

printf "Priority queue size = %u\n", $size

printf "Priority queue capacity = %u\n", $capacity

document ppqueue

Prints std::priority_queue information.

Syntax: ppqueue : Prints all elements, size and capacity of the priority_queue

Priority_queue elements are listed "top to buttom" (top-most element is the first to come on pop)

ppqueue pq - prints all elements, size and capacity of pq

# std::bitset

define pbitset

help pbitset

        p /t $arg0._M_w

document pbitset

Prints std::bitset information.

Syntax: pbitset : Prints all bits in bitset

pbitset b - prints all bits in b

# std::string

define pstring

help pstring

printf "String \t\t\t= \"%s\"\n", $arg0._M_data()

printf "String size/length \t= %u\n", $arg0._M_rep()->_M_length

printf "String capacity \t= %u\n", $arg0._M_rep()->_M_capacity

printf "String ref-count \t= %d\n", $arg0._M_rep()->_M_refcount

document pstring

Prints std::string information.

Syntax: pstring

pstring s - Prints content, size/length, capacity and ref-count of string s

# std::wstring

define pwstring

help pwstring

call printf("WString \t\t= \"%ls\"\n", $arg0._M_data())

printf "WString size/length \t= %u\n", $arg0._M_rep()->_M_length

printf "WString capacity \t= %u\n", $arg0._M_rep()->_M_capacity

printf "WString ref-count \t= %d\n", $arg0._M_rep()->_M_refcount

document pwstring

Prints std::wstring information.

Syntax: pwstring

pwstring s - Prints content, size/length, capacity and ref-count of wstring s

# C++ related beautifiers

set print pretty on

set print object on

set print static-members on

set print vtbl on

set print demangle on

set demangle-style gnu-v3

set print sevenbit-strings off

繼續閱讀