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?
- 347,512
- 102
- 1,199
- 985
- 10,165
- 18
- 66
- 97
-
1Cast it to an array type, or use the `x` command. – n. m. could be an AI Jan 24 '13 at 13:22
-
Related for assembly code without types: https://stackoverflow.com/questions/32300718/printing-array-from-bss-in-gdb – Ciro Santilli OurBigBook.com Jul 20 '18 at 00:07
5 Answers
See here. In short you should do:
p *array@len
- 4,271
- 8
- 34
- 56
- 69,226
- 18
- 123
- 176
-
3I have to know the number of len, is it possible to just use something like `command array`? – CodyChan Jun 23 '17 at 07:37
-
1@CodyChan When a is passed as parameter to a function you can't do that as gdb will not know the size. – Ivaylo Strandjev Jun 23 '17 at 07:39
-
2@CodyChan You can pass a number somewhat larger, and it _probably_ won't crash. Just so you can get an idea of what's going on. – Samie Bencherif Apr 22 '19 at 03:45
-
-
1@GabrielStaples thank you for reporting this. It now points to similar resource – Ivaylo Strandjev Sep 25 '20 at 06:53
-
1@IvayloStrandjev, glad to help! I've also added an answer, demonstrating some nice formatting options, and showing various samples of gdb output, here: https://stackoverflow.com/a/64055978/4561887. – Gabriel Staples Sep 25 '20 at 07:00
*(T (*)[N])p where T is the type, N is the number of elements and p is the pointer.
- 208,859
- 35
- 376
- 711
-
2It first casts `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. – R.. GitHub STOP HELPING ICE Jan 26 '13 at 01:26
-
8
-
Use the x command.
(gdb) x/100w a
- 70,891
- 9
- 107
- 161
-
1
-
3It's an overly specific solution. Whilst it will work for the OP's usecase with `int`s, it won't help e.g. if array contains `struct`s. – Hi-Angel Dec 05 '18 at 09:36
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 small master's thesis in my new 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
- 36,492
- 15
- 194
- 265
-
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`](https://man7.org/linux/man-pages/man7/environ.7.html)? – Abdull Aug 14 '23 at 10:32
-
1@Abdull, yes, there are many ways I just figured out. See my small master's thesis in my new answer here: [How to print the entire `environ` variable, containing strings of all of your C or C++ program's environment variables, in GDB](https://stackoverflow.com/a/76903706/4561887). – Gabriel Staples Aug 15 '23 at 06:21
(int[100])*pointer worked for me thanks to suggestion in the comments by @Ruslan
- 6,333
- 2
- 43
- 33