10

I'm working on an algorithm which uses a big map. I'm trying to follow the algorithm along with GDB while doing it on paper to see where it goes wrong. But as the map gets big, GDB abbreviates it and stop showing the next values, which I need:

(gdb) p R
$1 = std::map with 140 elements = {[0] = "", [1] = "e", [2] = "", [3] = "", [4] = "", [5] = "", [6] = "", [7] = "", [8] = "a", [9] = "a", [10] = "", [11] = "", [12] = "", [13] = "", [14] = "", 
  [15] = "", [16] = "a", [17] = "b", [18] = "", [19] = "", [20] = "", [21] = "", [22] = "b", [23] = "", [24] = "", [25] = "a", [26] = "", [27] = "", [28] = "", [29] = "", [30] = "", [31] = "b", 
  [32] = "", [33] = "a", [34] = "", [35] = "", [36] = "", [37] = "", [38] = "", [39] = "", [40] = "(b|a)", [41] = "e", [42] = "", [43] = "", [44] = "", [45] = "", [46] = "", [47] = "", [48] = "", 
  [49] = "", [50] = "", [51] = "a*.a", [52] = "", [53] = "", [54] = "", [55] = "", [56] = "", [57] = "", [58] = "", [59] = "", [60] = "", [61] = "", [62] = "", [63] = "", [64] = "", [65] = "a", 
  [66] = "b", [67] = "", [68] = "", [69] = "", [70] = "", [71] = "", [72] = "b.a*.a", [73] = "", [74] = "a", [75] = "", [76] = "", [77] = "", [78] = "", [79] = "", [80] = "b", [81] = "", [82] = "a", 
  [83] = "", [84] = "", [85] = "", [86] = "", [87] = "", [88] = "", [89] = "(b|a)", [90] = "e", [91] = "", [92] = "", [93] = "", [94] = "", [95] = "", [96] = "", [97] = "", [98] = "", [99] = ""...}

I've tried to access elements with p R[100] but GDB doesn't understand this syntax:

(gdb) p R[100]
Attempt to take address of value not located in memory.

I've heard that GDB uses pretty-printers which are python scripts to make a pretty display with the command print, but I'm not sure which pretty-printer exactly when I print my std::map and I'm not sure how to modify it. Also I don't really know how memory is handled (I didn't study allocators and stuff yet) in STL containers so I'm not sure I want to go into that code.

Do you know any simpler way to print everything? Or must I modify pretty-printers? (if so can you give me a hint on which file to modify and which commands to register the changes please?).

Floern
  • 33,559
  • 24
  • 104
  • 119
Harkan
  • 153
  • 2
  • 12

1 Answers1

10

Do you know any simpler way to print everything ?

(gdb) set print elements 0

Documentation.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Oh, that simple... I've seen it on posts but for some reason I thought it was only working on arrays, strings, etc. Thanks a lot ! – Harkan Dec 10 '17 at 20:46
  • @Harkan If the answer solves your problem, you should accept it. Re: arrays, strings, etc. -- the pretty-printers can (and are usually written to) also respect that setting. – Employed Russian Dec 10 '17 at 21:34
  • Some notes: 1) printing of huge maps can be prohibitively slow in GDB as compared to a compiled C++ for loop, redirection to file did not help: https://stackoverflow.com/questions/5941158/gdb-print-to-file-instead-of-stdout#comment65984744_5941271 2) trying to get individual elements may fail with "Cannot evaluate function maybe inlined ": https://stackoverflow.com/questions/40633787/c-stl-gdb-cannot-evaluate-function-maybe-inlined leaving me with no practical way to view the objec without recompile/rerunt. This is Hell. – Ciro Santilli OurBigBook.com Sep 24 '20 at 12:48