zamotivator

~/.gdbinit

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