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?
6 Answers
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.

- 53,924
- 26
- 111
- 144
-
9Nowadays you might also need "set print repeats 0", otherwise GDB will omit repeated elements of the string/array. – John Lindgren Feb 23 '18 at 17:45
-
this also applies to array types too – Trevor Boyd Smith May 11 '18 at 19:48
-
1You may also need to "set max-value-size unlimited". – Aaron Swan Oct 30 '18 at 20:22
As long as your program's in a sane state, you can also call (void)puts(your_string)
to print it to stdout. Same principle applies to all functions available to the debugger, actually.
-
3This answer is even better than "set print elements 0" (for my purposes) because it respects the newline/carriage return chars instead of escaping them. – mhenry1384 Jun 22 '10 at 16:34
-
8
-
4Note: this option only works if you are debugging a live program. You can't use GDB's "call" command when you are debugging a core file. – Solomon Slow Feb 13 '15 at 22:33
-
1also requires gdb to be sane, which increasingly seems to NOT be the case (I get "No symbol "puts" in current context." on my Mac OS X machine) – Michael May 05 '15 at 02:14
-
I simply wrtie "puts(your_string)" in the output window of xcode – Regis St-Gelais Jun 28 '16 at 17:32
-
Also doesn't work when you debugging a program which doesn't have `puts()` and problematic when `puts()` uses a UART and the UART of your target requires a running CPU. – 12431234123412341234123 Nov 06 '18 at 17:11
The printf
command will print the complete strings:
(gdb) printf "%s\n", string
-
2
-
13This seems to respect the `set print elements nnn` limit, and will not print the complete string unless you do `set print elements 0`. – Mark Lakata Oct 13 '17 at 18:17
-
3When I try this I only get: "Value can't be converted to integer." – Philipp Ludwig Jan 28 '19 at 12:57
-
9for std::string you need string.c_str() in order to avoid the "Value can't be converted to integer" error – Paul Childs Oct 24 '19 at 04:53
-
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"... ..

- 4,918
- 2
- 23
- 30
-
4I was wondering what "x/300sb" meant. With the help of this [cheat sheet (pdf)](http://refcards.com/docs/peschr/gdb/gdb-refcard-a4.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`! – Rob W Feb 03 '14 at 16:34
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

- 955
- 9
- 20
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.

- 487
- 4
- 13