How do I print the full value of a long string in gdb?
set print elements 0
From the GDB manual:
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 theset 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.
Print whole string verbatim in gdb
set print repeats 0
Example:
(gdb) p "How to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in Gdbaaaaaa"
$6 = 'a' <repeats 30 times>
(gdb) set print repeats 0
(gdb) p "How to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in Gdbaaaaaa"
$7 = "How to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in Gdbaaaaaa"
(gdb) set print repeats 10
(gdb) p "How to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in GdbHow to Print the Full Value of a Long String in Gdbaaaaaa"
$8 = 'a' <repeats 30 times>
GDB print all values in char array
You can use x/999bc
, where 999
is the size of your array, for instance:
paul@thoth:~/src/sandbox$ gdb ./str
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/paul/src/sandbox/str...done.
(gdb) list
1 int main(void) {
2 char * p = "hello\0world\0hahaha";
3 return 0;
4 }
5
(gdb) b 3
Breakpoint 1 at 0x4004b8: file str.c, line 3.
(gdb) run
Starting program: /home/paul/src/sandbox/str
Breakpoint 1, main () at str.c:3
3 return 0;
(gdb) print p
$1 = 0x40056c "hello"
(gdb) x/19bc p
0x40056c: 104 'h' 101 'e' 108 'l' 108 'l' 111 'o' 0 '\000' 119 'w' 111 'o'
0x400574: 114 'r' 108 'l' 100 'd' 0 '\000' 104 'h' 97 'a' 104 'h' 97 'a'
0x40057c: 104 'h' 97 'a' 0 '\000'
(gdb)
Gdb printing long values in hex without having to guess the length
A way to display long (as well as ll & ull) values is with the g modifier. For example if we have a program which just stores
unsigned long long int a = 1234567891234567898;
int b = 23;
unsigned long long int c = 1111111111111111111;
by typing x/20xg $rsp (after the values have been moved to the stack) we get
0x7fffffffdd90: 0x0000000000000000 0x0000001755555040
0x7fffffffdda0: 0x112210f4c023b6da 0x0f6b75ab2bc471c7
0x7fffffffddb0: 0x0000000000000000 0x00007ffff7e08b25
...
With the long numbers in [rsp+0x10] & [rsp+0x18] being a & c respectively, and that 0x17 in [rsp+0xc] being b.
Print elements of C++ string vector nicely in GDB
Is there any way to view the elements nicely without displaying some part of the STL containers?
You either have a very old GDB, or some non-standard setup.
Here is what it looks like on a Fedora-34 system with default GDB installation:
(gdb) list
1 #include <string>
2 #include <vector>
3
4 int main()
5 {
6 std::vector<std::string> v;
7 v.push_back("abc");
8 v.push_back("abcdef");
9 v.push_back("ghi");
10 }
(gdb) b 9
Breakpoint 1 at 0x401378: file t.cc, line 9.
(gdb) run
Starting program: /tmp/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Breakpoint 1, main () at t.cc:9
9 v.push_back("ghi");
(gdb) p v
$1 = std::vector of length 2, capacity 2 = {"abc", "abcdef"}
(gdb) n
10 }
(gdb) p v
$2 = std::vector of length 3, capacity 4 = {"abc", "abcdef", "ghi"}
(gdb) q
How can I print the string value and not the hex in GDB debugger?
argv
isn't a string, it's a char**
- a pointer to the first of possibly multiple C strings.
I think you're looking for:
print argv[0]
print argv[1]
...
Or if you want to use printf:
printf "%s\n", argv[0]
But there's really no reason to in such a simple case, since gdb does know how to print char*
strings.
Or, if you want to be fancy, this works:
print *argv@argc
The syntax FOO@NUM
tells it to print an array of NUM
elements starting at FOO
. And I have no idea why the dereferencing works, but it does - I guess gdb is just nice like that. Someone enlighten me?
Related Topics
Class Type Non-Type Template Parameter Initialization Does Not Compile
Building Qt5 with Visual Studio 2012/Visual Studio 2013, and Integrating with the Ide
How to Pretty-Print Stl Containers in Gdb
How to Change the Variable to Which a C++ Reference Refers
Thread Safe Implementation of Circular Buffer
On Local and Global Static Variables in C++
Does Casting to an Int After Std::Floor Guarantee the Right Result
How to Have Static Data Members in a Header-Only Library
How to Make an Array with a Dynamic Size? General Usage of Dynamic Arrays (Maybe Pointers Too)
Understanding the Example on Lvalue-To-Rvalue Conversion
Vector::At VS. Vector::Operator[]
C++ Access Derived Class Member from Base Class Pointer
Initialise Eigen::Vector with Std::Vector
Why Can't "Transform(S.Begin(),S.End(),S.Begin(),Tolower)" Be Complied Successfully
C++: When (And How) Are C++ Global Static Constructors Called