87

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
CDT
  • 10,165
  • 18
  • 66
  • 97

5 Answers5

172

See here. In short you should do:

p *array@len
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
28

*(T (*)[N])p where T is the type, N is the number of elements and p is the pointer.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 2
    It 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
    Simpler: `(T[N])*p`. GDB is a bit more able than C ;) – Ruslan Mar 17 '18 at 08:09
  • print/x *(char(*)[20])str – NikitQa Oct 08 '19 at 11:01
18

Use the x command.

(gdb) x/100w a
aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 1
    This yields too much output. Ivaylo's seems better. – CDT Jan 26 '13 at 01:22
  • 3
    It'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
13

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:

  1. print/x *my_array@10 = hex
  2. print/d *my_array@10 = signed integer
  3. print/u *my_array@10 = unsigned integer
  4. print/<format> *my_array@10 = print according to the general printf()-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

  1. How to view a pointer like an array in GDB?
  2. Official GDB documentation: Ch. 10.4 Artificial Arrays: https://sourceware.org/gdb/onlinedocs/gdb/Arrays.html
  3. Official GDB online user manual: https://sourceware.org/gdb/onlinedocs/gdb/
  4. Official GDB online user manual (all on one page, for easy searching!): https://sourceware.org/gdb/current/onlinedocs/gdb

See also

  1. My answer on gdb: show typeinfo of some data
  2. My answer on How to print the entire environ variable, containing strings of all of your C or C++ program's environment variables, in GDB
Gabriel Staples
  • 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
1

(int[100])*pointer worked for me thanks to suggestion in the comments by @Ruslan

Ali80
  • 6,333
  • 2
  • 43
  • 33