Suppose while debugging our program with GDB, we want to view the memory contents at a specific address of our interest. The x
command of the GDB debugger provides this functionality. It can be used in three different ways:
x [Address]
x/[Format] [Address]
x/[Length][Format] [Address]
[Address]
is an address expression that starts with the prefix, 0x
, following a specific byte sequence, a register/pseudo-register, or even a C/C++ expression that simplifies to an address expression.
[Format]
allows for setting the output format for x
. Some valid options for the [Format]
parameter are o - Octal
, x-Hexadecimal
, and d-Decimal
.
[Length]
specifies the number of elements that should be shown as output. There are only four values for [Length]
that are supported, which are: b
byte, h
half-word (16-bit), w
word (32-bit), g
giant word (64-bit).
We can demonstrate some of these commands by debugging the following simple snippet of code in GDB using the x
command:
#include <iostream>using namespace std;int main () {int arr[3] = {1, 2, 3};}
GDB will have the following results:
x/c arr
: This returns 0xbdddef7b : 40 '1'
, which shows the first character of the array. The /c
is the [Format]
used in the command. By default, the length is 1
.
x/2c arr
: This returns 0xbdddef7b: 40 '1' 41 '2'
. The length is changed to 2
and it is showing the values that are stored in a total of two bytes in memory.
x/wx arr
: This returns 0xbdddef7b: 0x53226134
, which shows the memory location of the first element of the array, arr
.
Free Resources