How do I print the full value of a long string in gdb?
Solution 1
set print elements 0
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. When GDB starts, this limit is set to 200. Setting number-of-elements to zero means that the printing is unlimited.
Solution 2
The printf
command will print the complete strings:
(gdb) printf "%s\n", string
Solution 3
There is a third option: the x command, which allows you to set a different limit for the specific command instead of changing a global setting. To print the first 300 characters of a string you can use x/300s your_string
. The output might be a bit harder to read. For example printing a SQL query results in:
(gdb) x/300sb stmt.c_str() 0x9cd948: "SELECT article.r"... 0x9cd958: "owid FROM articl"... ..
Solution 4
Just to complete it:
(gdb) p (char[10]) *($ebx)
$87 = "asdfasdfe\n"
You must give a length, but may change the representation of that string:
(gdb) p/x (char[10]) *($ebx)
$90 = {0x61,
0x73,
0x64,
0x66,
0x61,
0x73,
0x64,
0x66,
0x65,
0xa}
This may be useful if you want to debug by their values
Solution 5
Using set elements ...
isn't always the best way. It would be useful if there were a distinct set string-elements ...
.
So, I use these functions in my .gdbinit:
define pstr
ptype $arg0._M_dataplus._M_p
printf "[%d] = %s\n", $arg0._M_string_length, $arg0._M_dataplus._M_p
end
define pcstr
ptype $arg0
printf "[%d] = %s\n", strlen($arg0), $arg0
end
Caveats:
- The first is c++ lib dependent as it accesses members of std::string, but is easily adjusted.
- The second can only be used on a running program as it calls strlen.
John Carter
Updated on July 08, 2022Comments
-
John Carter almost 2 years
I want to print the full length of a C-string in GDB. By default it's being abbreviated, how do I force GDB to print the whole string?
-
Rob W over 10 yearsI was wondering what "x/300sb" meant. With the help of this cheat sheet (pdf), I've translated "x/300sb cstr" as "eXamine 300 units (Bytes) of memory at address cstr, interpreted as a NULL-terminated string (S).". If your string has length 100, then you will see lots of garbage, because all 300 bytes are printed, whether they make sense or not. +1 nevertheless for introducing me to
x
! -
UmNyobe almost 7 yearssorry but this is not true
-
Mark Lakata over 6 yearsThis seems to respect the
set print elements nnn
limit, and will not print the complete string unless you doset print elements 0
. -
John Lindgren about 6 yearsNowadays you might also need "set print repeats 0", otherwise GDB will omit repeated elements of the string/array.
-
Trevor Boyd Smith about 6 yearsthis also applies to array types too
-
Aaron Swan over 5 yearsYou may also need to "set max-value-size unlimited".
-
Philipp Ludwig over 5 yearsWhen I try this I only get: "Value can't be converted to integer."
-
Paul Childs over 4 yearsfor std::string you need string.c_str() in order to avoid the "Value can't be converted to integer" error