Suppose defined: int a[100] Type print a then gdb will automatically display it as an array:1, 2, 3, 4.... However, if a is passed to a function as a parameter, then gdb will treat it as a normal int pointer, type print a will display:(int *)0x7fffffffdaa0. What should I do if I want to view a as an array?
5 Answers
See here. In short you should do:
p *array@len
6 Comments
command array?*(T (*)[N])p where T is the type, N is the number of elements and p is the pointer.
3 Comments
p to a pointer-to-array type (instead of pointer-to-element type pointing to the first element), then dereferences that pointer to get an array object. In C, this would decay back to a pointer in most contexts except as the operand of & or sizeof, but gdb uses the array type directly to print the array.(T[N])*p. GDB is a bit more able than C ;)How to view or print any number of bytes from any array in any printf-style format using the gdb debugger
As @Ivaylo Strandjev says here, the general syntax is:
print *my_array@len
# OR the shorter version:
p *my_array@len
Example to print the first 10 bytes from my_array:
print *my_array@10
[Recommended!] Custom printf-style print formatting: however, if the commands above look like garbage since it tries to interpret the values as chars, you can force different formatting options like this:
print/x *my_array@10= hexprint/d *my_array@10= signed integerprint/u *my_array@10= unsigned integerprint/<format> *my_array@10= print according to the generalprintf()-style format string,<format>
Here are some real examples from my debugger to print 16 bytes from a uint8_t array named byteArray. Notice how ugly the first one is, with just p *byteArray@16:
(gdb) p *byteArray@16
$4 = "\000\001\002\003\004\005\006\a\370\371\372\373\374\375\376\377"
(gdb) print/x *byteArray@16
$5 = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}
(gdb) print/d *byteArray@16
$6 = {0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1}
(gdb) print/u *byteArray@16
$7 = {0, 1, 2, 3, 4, 5, 6, 7, 248, 249, 250, 251, 252, 253, 254, 255}
In my case, the best version, with the correct representation I want to see, is the last one where I print the array as unsigned integers using print/u, since it is a uint8_t unsigned integer array after-all:
(gdb) print/u *byteArray@16
$7 = {0, 1, 2, 3, 4, 5, 6, 7, 248, 249, 250, 251, 252, 253, 254, 255}
Going further
Comment question from @Abdull:
Is there a way to print the array without specifying the array length, for pointer-based arrays which mark the end of the array with a null pointer such as
extern char **environ?
Yes, there are many ways. See my long answer here: How to print the entire environ variable, containing strings of all of your C or C++ program's environment variables, in GDB.
References
- How to view a pointer like an array in GDB?
- Official GDB documentation: Ch. 10.4 Artificial Arrays: https://sourceware.org/gdb/onlinedocs/gdb/Arrays.html
- Official GDB online user manual: https://sourceware.org/gdb/onlinedocs/gdb/
- Official GDB online user manual (all on one page, for easy searching!): https://sourceware.org/gdb/current/onlinedocs/gdb
See also
2 Comments
extern char **environ?environ variable, containing strings of all of your C or C++ program's environment variables, in GDB.
xcommand.