Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write an expression that deference a pointer in GDB?

Suppose EAX contains a pointer to some value (doubleword).

What I'd like to do is to examine this value, i.e. writing an expression like x /1wx [eax].

However, GDB complains when writing [eax] in expressions, saying that syntax is wrong.

How would I deference a pointer in GDB?

like image 298
Shuzheng Avatar asked Mar 23 '26 05:03

Shuzheng


2 Answers

As the other answer noted, you can the $eax to look at that specific register.

(gdb) x /1wx $eax
0x400523d <main>:  0xe5894855

'info reg' will give a register dump that contains lots of useful information.

You can also cast in gdb to dereference a pointer.

(gdb) print /x *(int*)0x400523d
$3 = 0xe5894855

I use this method often while debugging. It is useful when digging inside a complex class/structure for pointers. Following a chain of pointers is often simplified but just grabbing the address and looking at the dereference.

like image 176
Matthew Fisher Avatar answered Mar 25 '26 21:03

Matthew Fisher


You can refer to registers by common names using $ before. For example

print $rax

to print rax value, or

print *$rax

to dereference value in rax as pointer.

https://sourceware.org/gdb/onlinedocs/gdb/Registers.html

like image 39
user2807083 Avatar answered Mar 25 '26 19:03

user2807083