13

I have an array of characters with about 100k entries. It seems like when in the function I print *buffer it is getting cut off prematurely. is there anything I can do to lengthen the amount of characters gdb will print to console? If not, can I print this variable to file? I tried to use the dump commands, but couldnt seem to come up with the right thing.

7
  • From your description it sounds like your array might actually be a pointer. Did you specify a length to print when you gave that command? Commented Jan 30, 2013 at 17:04
  • Well it is passed into the function as **buffer from the calling function. Commented Jan 30, 2013 at 17:05
  • So how is GDB supposed to know how long it is? Commented Jan 30, 2013 at 17:05
  • 3
    I find it much easier to define functions in the code that perform the data dump, and just call them from gdb. (Wrap the definitions in #ifdef DEBUG) Commented Jan 30, 2013 at 17:06
  • Not easier if you're debugging production code. ;-) Commented Sep 13, 2013 at 6:47

4 Answers 4

28

I think you want something like this:

(gdb) dump binary memory ~/file.bin 0x100390f4c (0x100390f4c + 940)

The dump command is a little awkward to use. It takes a start an an end address, and expressions that indicate what to dump (you can use value instead of memory to specify an expression, if that works for you, but sometimes I'd rather be specific.) But it appears (as I've tested above) that you can use expressions anyway, as I've specified the end address above in terms of the start address I wanted to dump, plus the amount of bytes I wanted.

You can also do something like this (pass in the expression that results in the pointer value rather than the value of the pointer itself):

(gdb) dump binary memory ~/file.bin buf (buf + len)

For more information, see the documentation here.

Sign up to request clarification or add additional context in comments.

Comments

13
(gdb) help x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
  t(binary), f(float), a(address), i(instruction), c(char) and s(string).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format.

Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed
with this command or "print".
(gdb) x/8b array
0xbffd7670:     0       0       0       0       0       0       0       0
(gdb) x/16b array
0xbffd7670:     0       0       0       0       0       0       0       0
0xbffd7678:     0       0       0       0       0       0       0       0
(gdb) x/128b array
0xbffd7670:     0       0       0       0       0       0       0       0
0xbffd7678:     0       0       0       0       0       0       0       0
0xbffd7680:     0       0       0       0       0       0       0       0
0xbffd7688:     0       0       0       0       0       0       0       0
0xbffd7690:     0       0       0       0       0       0       0       0
0xbffd7698:     0       0       0       0       0       0       0       0
0xbffd76a0:     0       0       0       0       0       0       0       0
0xbffd76a8:     0       0       0       0       0       0       0       0
0xbffd76b0:     0       0       0       0       0       0       0       0
0xbffd76b8:     0       0       0       0       0       0       0       0
0xbffd76c0:     0       0       0       0       0       0       0       0
0xbffd76c8:     0       0       0       0       0       0       0       0
0xbffd76d0:     0       0       0       0       0       0       0       0
0xbffd76d8:     0       0       0       0       0       0       0       0
0xbffd76e0:     0       0       0       0       0       0       0       0
0xbffd76e8:     0       0       0       0       0       0       0       0
(gdb)

If you want the ASCII character symbols printed as well, use x/<size>c.

(gdb) set logging file ~/gdb_dump.txt
(gdb) set logging on
Copying output to /home/mminich/gdb_dump.txt.
(gdb) x/26c array
0xbfff4b20:     97 'a'  98 'b'  99 'c'  100 'd' 101 'e' 102 'f' 103 'g' 104 'h'
0xbfff4b28:     105 'i' 106 'j' 107 'k' 108 'l' 109 'm' 110 'n' 111 'o' 112 'p'
0xbfff4b30:     113 'q' 114 'r' 115 's' 116 't' 117 'u' 118 'v' 119 'w' 120 'x'
0xbfff4b38:     121 'y' 122 'z'
(gdb) set logging off
Done logging to /home/mminich/gdb_dump.txt.
(gdb)

BTW, I agree wholeheartedly with William Pursell's comment under your question: "I find it much easier to define functions in the code that perform the data dump, and just call them from gdb. (Wrap the definitions in #ifdef DEBUG)"

1 Comment

How about getting that printed to file?
3

To print unlimited characters to the console use

set print elements 0

Comments

3

I personally use embedded python for dumping data.For example:

(gdb) pi open("output_data.log","w").write(gdb.execute("print myarray@100000",to_string=True))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.