101

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?

2

5 Answers 5

194

See here. In short you should do:

p *array@len
Sign up to request clarification or add additional context in comments.

6 Comments

I have to know the number of len, is it possible to just use something like command array?
@CodyChan When a is passed as parameter to a function you can't do that as gdb will not know the size.
@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.
@GabrielStaples thank you for reporting this. It now points to similar resource
@IvayloStrandjev, glad to help! I've also added an answer, demonstrating some nice formatting options, and showing various samples of gdb output, here: stackoverflow.com/a/64055978/4561887.
|
34

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

3 Comments

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.
Simpler: (T[N])*p. GDB is a bit more able than C ;)
print/x (char()[20])str
20

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 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

  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

2 Comments

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?
@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.
19

Use the x command.

(gdb) x/100w a

2 Comments

This yields too much output. Ivaylo's seems better.
It's an overly specific solution. Whilst it will work for the OP's usecase with ints, it won't help e.g. if array contains structs.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.