1. #
    2. # STL GDB evaluators/views/utilities - 1.03
    3. #
    4. # The new GDB commands:
    5. # are entirely non instrumental
    6. # do not depend on any "inline"(s) - e.g. size(), [], etc
    7. # are extremely tolerant to debugger settings
    8. #
    9. # This file should be "included" in .gdbinit as following:
    10. # source stl-views.gdb or just paste it into your .gdbinit file
    11. #
    12. # The following STL containers are currently supported:
    13. #
    14. # std::vector<T> -- via pvector command
    15. # std::list<T> -- via plist or plist_member command
    16. # std::map<T,T> -- via pmap or pmap_member command
    17. # std::multimap<T,T> -- via pmap or pmap_member command
    18. # std::set<T> -- via pset command
    19. # std::multiset<T> -- via pset command
    20. # std::deque<T> -- via pdequeue command
    21. # std::stack<T> -- via pstack command
    22. # std::queue<T> -- via pqueue command
    23. # std::priority_queue<T> -- via ppqueue command
    24. # std::bitset<n> -- via pbitset command
    25. # std::string -- via pstring command
    26. # std::widestring -- via pwstring command
    27. #
    28. # The end of this file contains (optional) C++ beautifiers
    29. # Make sure your debugger supports $argc
    30. #
    31. # Simple GDB Macros writen by Dan Marinescu (H-PhD) - License GPL
    32. # Inspired by intial work of Tom Malnar,
    33. # Tony Novac (PhD) / Cornell / Stanford,
    34. # Gilad Mishne (PhD) and Many Many Others.
    35. # Contact: dan_c_marinescu@yahoo.com (Subject: STL)
    36. #
    37. # Modified to work with g++ 4.3 by Anders Elton
    38. # Also added _member functions, that instead of printing the entire class in map, prints a member.
    39. #
    40. # std::vector<>
    41. #
    42. define pvector
    43. if $argc == 0
    44. help pvector
    45. else
    46. set $size = $arg0._M_impl._M_finish - $arg0._M_impl._M_start
    47. set $capacity = $arg0._M_impl._M_end_of_storage - $arg0._M_impl._M_start
    48. set $size_max = $size - 1
    49. end
    50. if $argc == 1
    51. set $i = 0
    52. while $i < $size
    53. printf "elem[%u]: ", $i
    54. p *($arg0._M_impl._M_start + $i)
    55. set $i++
    56. end
    57. end
    58. if $argc == 2
    59. set $idx = $arg1
    60. if $idx < 0 || $idx > $size_max
    61. printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max
    62. else
    63. printf "elem[%u]: ", $idx
    64. p *($arg0._M_impl._M_start + $idx)
    65. end
    66. end
    67. if $argc == 3
    68. set $start_idx = $arg1
    69. set $stop_idx = $arg2
    70. if $start_idx > $stop_idx
    71. set $tmp_idx = $start_idx
    72. set $start_idx = $stop_idx
    73. set $stop_idx = $tmp_idx
    74. end
    75. if $start_idx < 0 || $stop_idx < 0 || $start_idx > $size_max || $stop_idx > $size_max
    76. printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max
    77. else
    78. set $i = $start_idx
    79. while $i <= $stop_idx
    80. printf "elem[%u]: ", $i
    81. p *($arg0._M_impl._M_start + $i)
    82. set $i++
    83. end
    84. end
    85. end
    86. if $argc > 0
    87. printf "Vector size = %u\n", $size
    88. printf "Vector capacity = %u\n", $capacity
    89. printf "Element "
    90. whatis $arg0._M_impl._M_start
    91. end
    92. end
    93. document pvector
    94. Prints std::vector<T> information.
    95. Syntax: pvector <vector> <idx1> <idx2>
    96. Note: idx, idx1 and idx2 must be in acceptable range [0..<vector>.size()-1].
    97. Examples:
    98. pvector v - Prints vector content, size, capacity and T typedef
    99. pvector v 0 - Prints element[idx] from vector
    100. pvector v 1 2 - Prints elements in range [idx1..idx2] from vector
    101. end
    102. #
    103. # std::list<>
    104. #
    105. define plist
    106. if $argc == 0
    107. help plist
    108. else
    109. set $head = &$arg0._M_impl._M_node
    110. set $current = $arg0._M_impl._M_node._M_next
    111. set $size = 0
    112. while $current != $head
    113. if $argc == 2
    114. printf "elem[%u]: ", $size
    115. p *($arg1*)($current + 1)
    116. end
    117. if $argc == 3
    118. if $size == $arg2
    119. printf "elem[%u]: ", $size
    120. p *($arg1*)($current + 1)
    121. end
    122. end
    123. set $current = $current._M_next
    124. set $size++
    125. end
    126. printf "List size = %u \n", $size
    127. if $argc == 1
    128. printf "List "
    129. whatis $arg0
    130. printf "Use plist <variable_name> <element_type> to see the elements in the list.\n"
    131. end
    132. end
    133. end
    134. document plist
    135. Prints std::list<T> information.
    136. Syntax: plist <list> <T> <idx>: Prints list size, if T defined all elements or just element at idx
    137. Examples:
    138. plist l - prints list size and definition
    139. plist l int - prints all elements and list size
    140. plist l int 2 - prints the third element in the list (if exists) and list size
    141. end
    142. define plist_member
    143. if $argc == 0
    144. help plist_member
    145. else
    146. set $head = &$arg0._M_impl._M_node
    147. set $current = $arg0._M_impl._M_node._M_next
    148. set $size = 0
    149. while $current != $head
    150. if $argc == 3
    151. printf "elem[%u]: ", $size
    152. p (*($arg1*)($current + 1)).$arg2
    153. end
    154. if $argc == 4
    155. if $size == $arg3
    156. printf "elem[%u]: ", $size
    157. p (*($arg1*)($current + 1)).$arg2
    158. end
    159. end
    160. set $current = $current._M_next
    161. set $size++
    162. end
    163. printf "List size = %u \n", $size
    164. if $argc == 1
    165. printf "List "
    166. whatis $arg0
    167. printf "Use plist_member <variable_name> <element_type> <member> to see the elements in the list.\n"
    168. end
    169. end
    170. end
    171. document plist_member
    172. Prints std::list<T> information.
    173. Syntax: plist <list> <T> <idx>: Prints list size, if T defined all elements or just element at idx
    174. Examples:
    175. plist_member l int member - prints all elements and list size
    176. plist_member l int member 2 - prints the third element in the list (if exists) and list size
    177. end
    178. #
    179. # std::map and std::multimap
    180. #
    181. define pmap
    182. if $argc == 0
    183. help pmap
    184. else
    185. set $tree = $arg0
    186. set $i = 0
    187. set $node = $tree._M_t._M_impl._M_header._M_left
    188. set $end = $tree._M_t._M_impl._M_header
    189. set $tree_size = $tree._M_t._M_impl._M_node_count
    190. if $argc == 1
    191. printf "Map "
    192. whatis $tree
    193. printf "Use pmap <variable_name> <left_element_type> <right_element_type> to see the elements in the map.\n"
    194. end
    195. if $argc == 3
    196. while $i < $tree_size
    197. set $value = (void *)($node + 1)
    198. printf "elem[%u].left: ", $i
    199. p *($arg1*)$value
    200. set $value = $value + sizeof($arg1)
    201. printf "elem[%u].right: ", $i
    202. p *($arg2*)$value
    203. if $node._M_right != 0
    204. set $node = $node._M_right
    205. while $node._M_left != 0
    206. set $node = $node._M_left
    207. end
    208. else
    209. set $tmp_node = $node._M_parent
    210. while $node == $tmp_node._M_right
    211. set $node = $tmp_node
    212. set $tmp_node = $tmp_node._M_parent
    213. end
    214. if $node._M_right != $tmp_node
    215. set $node = $tmp_node
    216. end
    217. end
    218. set $i++
    219. end
    220. end
    221. if $argc == 4
    222. set $idx = $arg3
    223. set $ElementsFound = 0
    224. while $i < $tree_size
    225. set $value = (void *)($node + 1)
    226. if *($arg1*)$value == $idx
    227. printf "elem[%u].left: ", $i
    228. p *($arg1*)$value
    229. set $value = $value + sizeof($arg1)
    230. printf "elem[%u].right: ", $i
    231. p *($arg2*)$value
    232. set $ElementsFound++
    233. end
    234. if $node._M_right != 0
    235. set $node = $node._M_right
    236. while $node._M_left != 0
    237. set $node = $node._M_left
    238. end
    239. else
    240. set $tmp_node = $node._M_parent
    241. while $node == $tmp_node._M_right
    242. set $node = $tmp_node
    243. set $tmp_node = $tmp_node._M_parent
    244. end
    245. if $node._M_right != $tmp_node
    246. set $node = $tmp_node
    247. end
    248. end
    249. set $i++
    250. end
    251. printf "Number of elements found = %u\n", $ElementsFound
    252. end
    253. if $argc == 5
    254. set $idx1 = $arg3
    255. set $idx2 = $arg4
    256. set $ElementsFound = 0
    257. while $i < $tree_size
    258. set $value = (void *)($node + 1)
    259. set $valueLeft = *($arg1*)$value
    260. set $valueRight = *($arg2*)($value + sizeof($arg1))
    261. if $valueLeft == $idx1 && $valueRight == $idx2
    262. printf "elem[%u].left: ", $i
    263. p $valueLeft
    264. printf "elem[%u].right: ", $i
    265. p $valueRight
    266. set $ElementsFound++
    267. end
    268. if $node._M_right != 0
    269. set $node = $node._M_right
    270. while $node._M_left != 0
    271. set $node = $node._M_left
    272. end
    273. else
    274. set $tmp_node = $node._M_parent
    275. while $node == $tmp_node._M_right
    276. set $node = $tmp_node
    277. set $tmp_node = $tmp_node._M_parent
    278. end
    279. if $node._M_right != $tmp_node
    280. set $node = $tmp_node
    281. end
    282. end
    283. set $i++
    284. end
    285. printf "Number of elements found = %u\n", $ElementsFound
    286. end
    287. printf "Map size = %u\n", $tree_size
    288. end
    289. end
    290. document pmap
    291. Prints std::map<TLeft and TRight> or std::multimap<TLeft and TRight> information. Works for std::multimap as well.
    292. Syntax: pmap <map> <TtypeLeft> <TypeRight> <valLeft> <valRight>: Prints map size, if T defined all elements or just element(s) with val(s)
    293. Examples:
    294. pmap m - prints map size and definition
    295. pmap m int int - prints all elements and map size
    296. pmap m int int 20 - prints the element(s) with left-value = 20 (if any) and map size
    297. pmap m int int 20 200 - prints the element(s) with left-value = 20 and right-value = 200 (if any) and map size
    298. end
    299. define pmap_member
    300. if $argc == 0
    301. help pmap_member
    302. else
    303. set $tree = $arg0
    304. set $i = 0
    305. set $node = $tree._M_t._M_impl._M_header._M_left
    306. set $end = $tree._M_t._M_impl._M_header
    307. set $tree_size = $tree._M_t._M_impl._M_node_count
    308. if $argc == 1
    309. printf "Map "
    310. whatis $tree
    311. printf "Use pmap <variable_name> <left_element_type> <right_element_type> to see the elements in the map.\n"
    312. end
    313. if $argc == 5
    314. while $i < $tree_size
    315. set $value = (void *)($node + 1)
    316. printf "elem[%u].left: ", $i
    317. p (*($arg1*)$value).$arg2
    318. set $value = $value + sizeof($arg1)
    319. printf "elem[%u].right: ", $i
    320. p (*($arg3*)$value).$arg4
    321. if $node._M_right != 0
    322. set $node = $node._M_right
    323. while $node._M_left != 0
    324. set $node = $node._M_left
    325. end
    326. else
    327. set $tmp_node = $node._M_parent
    328. while $node == $tmp_node._M_right
    329. set $node = $tmp_node
    330. set $tmp_node = $tmp_node._M_parent
    331. end
    332. if $node._M_right != $tmp_node
    333. set $node = $tmp_node
    334. end
    335. end
    336. set $i++
    337. end
    338. end
    339. if $argc == 6
    340. set $idx = $arg5
    341. set $ElementsFound = 0
    342. while $i < $tree_size
    343. set $value = (void *)($node + 1)
    344. if *($arg1*)$value == $idx
    345. printf "elem[%u].left: ", $i
    346. p (*($arg1*)$value).$arg2
    347. set $value = $value + sizeof($arg1)
    348. printf "elem[%u].right: ", $i
    349. p (*($arg3*)$value).$arg4
    350. set $ElementsFound++
    351. end
    352. if $node._M_right != 0
    353. set $node = $node._M_right
    354. while $node._M_left != 0
    355. set $node = $node._M_left
    356. end
    357. else
    358. set $tmp_node = $node._M_parent
    359. while $node == $tmp_node._M_right
    360. set $node = $tmp_node
    361. set $tmp_node = $tmp_node._M_parent
    362. end
    363. if $node._M_right != $tmp_node
    364. set $node = $tmp_node
    365. end
    366. end
    367. set $i++
    368. end
    369. printf "Number of elements found = %u\n", $ElementsFound
    370. end
    371. printf "Map size = %u\n", $tree_size
    372. end
    373. end
    374. document pmap_member
    375. Prints std::map<TLeft and TRight> or std::multimap<TLeft and TRight> information. Works for std::multimap as well.
    376. Syntax: pmap <map> <TtypeLeft> <TypeRight> <valLeft> <valRight>: Prints map size, if T defined all elements or just element(s) with val(s)
    377. Examples:
    378. pmap_member m class1 member1 class2 member2 - prints class1.member1 : class2.member2
    379. pmap_member m class1 member1 class2 member2 lvalue - prints class1.member1 : class2.member2 where class1 == lvalue
    380. end
    381. #
    382. # std::set and std::multiset
    383. #
    384. define pset
    385. if $argc == 0
    386. help pset
    387. else
    388. set $tree = $arg0
    389. set $i = 0
    390. set $node = $tree._M_t._M_impl._M_header._M_left
    391. set $end = $tree._M_t._M_impl._M_header
    392. set $tree_size = $tree._M_t._M_impl._M_node_count
    393. if $argc == 1
    394. printf "Set "
    395. whatis $tree
    396. printf "Use pset <variable_name> <element_type> to see the elements in the set.\n"
    397. end
    398. if $argc == 2
    399. while $i < $tree_size
    400. set $value = (void *)($node + 1)
    401. printf "elem[%u]: ", $i
    402. p *($arg1*)$value
    403. if $node._M_right != 0
    404. set $node = $node._M_right
    405. while $node._M_left != 0
    406. set $node = $node._M_left
    407. end
    408. else
    409. set $tmp_node = $node._M_parent
    410. while $node == $tmp_node._M_right
    411. set $node = $tmp_node
    412. set $tmp_node = $tmp_node._M_parent
    413. end
    414. if $node._M_right != $tmp_node
    415. set $node = $tmp_node
    416. end
    417. end
    418. set $i++
    419. end
    420. end
    421. if $argc == 3
    422. set $idx = $arg2
    423. set $ElementsFound = 0
    424. while $i < $tree_size
    425. set $value = (void *)($node + 1)
    426. if *($arg1*)$value == $idx
    427. printf "elem[%u]: ", $i
    428. p *($arg1*)$value
    429. set $ElementsFound++
    430. end
    431. if $node._M_right != 0
    432. set $node = $node._M_right
    433. while $node._M_left != 0
    434. set $node = $node._M_left
    435. end
    436. else
    437. set $tmp_node = $node._M_parent
    438. while $node == $tmp_node._M_right
    439. set $node = $tmp_node
    440. set $tmp_node = $tmp_node._M_parent
    441. end
    442. if $node._M_right != $tmp_node
    443. set $node = $tmp_node
    444. end
    445. end
    446. set $i++
    447. end
    448. printf "Number of elements found = %u\n", $ElementsFound
    449. end
    450. printf "Set size = %u\n", $tree_size
    451. end
    452. end
    453. document pset
    454. Prints std::set<T> or std::multiset<T> information. Works for std::multiset as well.
    455. Syntax: pset <set> <T> <val>: Prints set size, if T defined all elements or just element(s) having val
    456. Examples:
    457. pset s - prints set size and definition
    458. pset s int - prints all elements and the size of s
    459. pset s int 20 - prints the element(s) with value = 20 (if any) and the size of s
    460. end
    461. #
    462. # std::dequeue
    463. #
    464. define pdequeue
    465. if $argc == 0
    466. help pdequeue
    467. else
    468. set $size = 0
    469. set $start_cur = $arg0._M_impl._M_start._M_cur
    470. set $start_last = $arg0._M_impl._M_start._M_last
    471. set $start_stop = $start_last
    472. while $start_cur != $start_stop
    473. p *$start_cur
    474. set $start_cur++
    475. set $size++
    476. end
    477. set $finish_first = $arg0._M_impl._M_finish._M_first
    478. set $finish_cur = $arg0._M_impl._M_finish._M_cur
    479. set $finish_last = $arg0._M_impl._M_finish._M_last
    480. if $finish_cur < $finish_last
    481. set $finish_stop = $finish_cur
    482. else
    483. set $finish_stop = $finish_last
    484. end
    485. while $finish_first != $finish_stop
    486. p *$finish_first
    487. set $finish_first++
    488. set $size++
    489. end
    490. printf "Dequeue size = %u\n", $size
    491. end
    492. end
    493. document pdequeue
    494. Prints std::dequeue<T> information.
    495. Syntax: pdequeue <dequeue>: Prints dequeue size, if T defined all elements
    496. Deque elements are listed "left to right" (left-most stands for front and right-most stands for back)
    497. Example:
    498. pdequeue d - prints all elements and size of d
    499. end
    500. #
    501. # std::stack
    502. #
    503. define pstack
    504. if $argc == 0
    505. help pstack
    506. else
    507. set $start_cur = $arg0.c._M_impl._M_start._M_cur
    508. set $finish_cur = $arg0.c._M_impl._M_finish._M_cur
    509. set $size = $finish_cur - $start_cur
    510. set $i = $size - 1
    511. while $i >= 0
    512. p *($start_cur + $i)
    513. set $i--
    514. end
    515. printf "Stack size = %u\n", $size
    516. end
    517. end
    518. document pstack
    519. Prints std::stack<T> information.
    520. Syntax: pstack <stack>: Prints all elements and size of the stack
    521. Stack elements are listed "top to buttom" (top-most element is the first to come on pop)
    522. Example:
    523. pstack s - prints all elements and the size of s
    524. end
    525. #
    526. # std::queue
    527. #
    528. define pqueue
    529. if $argc == 0
    530. help pqueue
    531. else
    532. set $start_cur = $arg0.c._M_impl._M_start._M_cur
    533. set $finish_cur = $arg0.c._M_impl._M_finish._M_cur
    534. set $size = $finish_cur - $start_cur
    535. set $i = 0
    536. while $i < $size
    537. p *($start_cur + $i)
    538. set $i++
    539. end
    540. printf "Queue size = %u\n", $size
    541. end
    542. end
    543. document pqueue
    544. Prints std::queue<T> information.
    545. Syntax: pqueue <queue>: Prints all elements and the size of the queue
    546. Queue elements are listed "top to bottom" (top-most element is the first to come on pop)
    547. Example:
    548. pqueue q - prints all elements and the size of q
    549. end
    550. #
    551. # std::priority_queue
    552. #
    553. define ppqueue
    554. if $argc == 0
    555. help ppqueue
    556. else
    557. set $size = $arg0.c._M_impl._M_finish - $arg0.c._M_impl._M_start
    558. set $capacity = $arg0.c._M_impl._M_end_of_storage - $arg0.c._M_impl._M_start
    559. set $i = $size - 1
    560. while $i >= 0
    561. p *($arg0.c._M_impl._M_start + $i)
    562. set $i--
    563. end
    564. printf "Priority queue size = %u\n", $size
    565. printf "Priority queue capacity = %u\n", $capacity
    566. end
    567. end
    568. document ppqueue
    569. Prints std::priority_queue<T> information.
    570. Syntax: ppqueue <priority_queue>: Prints all elements, size and capacity of the priority_queue
    571. Priority_queue elements are listed "top to buttom" (top-most element is the first to come on pop)
    572. Example:
    573. ppqueue pq - prints all elements, size and capacity of pq
    574. end
    575. #
    576. # std::bitset
    577. #
    578. define pbitset
    579. if $argc == 0
    580. help pbitset
    581. else
    582. p /t $arg0._M_w
    583. end
    584. end
    585. document pbitset
    586. Prints std::bitset<n> information.
    587. Syntax: pbitset <bitset>: Prints all bits in bitset
    588. Example:
    589. pbitset b - prints all bits in b
    590. end
    591. #
    592. # std::string
    593. #
    594. define pstring
    595. if $argc == 0
    596. help pstring
    597. else
    598. printf "String \t\t\t= \"%s\"\n", $arg0._M_data()
    599. printf "String size/length \t= %u\n", $arg0._M_rep()._M_length
    600. printf "String capacity \t= %u\n", $arg0._M_rep()._M_capacity
    601. printf "String ref-count \t= %d\n", $arg0._M_rep()._M_refcount
    602. end
    603. end
    604. document pstring
    605. Prints std::string information.
    606. Syntax: pstring <string>
    607. Example:
    608. pstring s - Prints content, size/length, capacity and ref-count of string s
    609. end
    610. #
    611. # std::wstring
    612. #
    613. define pwstring
    614. if $argc == 0
    615. help pwstring
    616. else
    617. call printf("WString \t\t= \"%ls\"\n", $arg0._M_data())
    618. printf "WString size/length \t= %u\n", $arg0._M_rep()._M_length
    619. printf "WString capacity \t= %u\n", $arg0._M_rep()._M_capacity
    620. printf "WString ref-count \t= %d\n", $arg0._M_rep()._M_refcount
    621. end
    622. end
    623. document pwstring
    624. Prints std::wstring information.
    625. Syntax: pwstring <wstring>
    626. Example:
    627. pwstring s - Prints content, size/length, capacity and ref-count of wstring s
    628. end
    629. #
    630. # C++ related beautifiers (optional)
    631. #
    632. set print pretty on
    633. set print object on
    634. set print static-members on
    635. set print vtbl on
    636. set print demangle on
    637. set demangle-style gnu-v3
    638. set print sevenbit-strings off
    639. set follow-fork-mode child
    640. set detach-on-fork off