Tuesday, February 14, 2012

GDB: printing complete string

In GDB, generally to print the value of the variable ,we use print or just p. But for the strings or arrays of large size , it wont print whole string or array. We have to do some modifications. By default GDB will print upto 200 characters ,if the string is very large.To change this there is command in GDB

set print elements number-of-elements


Set a limit on how many elements of an array GDB will print. If GDB is printing a large array, it stops printing after it has printed the number of elements set by the set print elements command. This limit also applies to the display of strings. Setting number-of-elements to zero means that the printing is unlimited.

(gdb) set print element 20
(gdb)

next time it will print upto 20 characters

(gdb) set print element 50
(gdb)

next time it will print upto 50 characters

(gdb) set print element 0
(gdb)

next time it will print whole array or string

Example:
(gdb) p a
$1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr"...
(gdb) set print element 0
(gdb) p a
$2 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\000\000\000"
(gdb)

No comments:

Popular Posts