Site Tools


wiki:gdb-examining-state

Table of Contents

GDB Examining State

Printing variables and state tells you what values the program holds. Print can show variables, expressions, array contents, and dereferenced pointers.

(gdb) print x                    # show value of x
(gdb) print x + y               # show expression
(gdb) print arr                  # show array (often prints just base address)
(gdb) print arr[0]              # show first element
(gdb) print *ptr                # dereference pointer
(gdb) print &variable           # address of variable
(gdb) print (int)ptr            # cast pointer to int
(gdb) info locals               # show all local variables
(gdb) info args                 # show function arguments
(gdb) backtrace (bt)            # show call stack (all frames)
(gdb) frame N                   # select frame N (0 is innermost)
(gdb) up                        # move to calling frame
(gdb) down                      # move to called frame

Print is the workhorse command. Abbreviate to p in GDB. It evaluates expressions in the current frame's scope—locals, globals, and dereferenced pointers are all available.

info locals lists all local variables and their values. Faster than printing individually when debugging a function with many locals. info args shows function arguments—useful when stepping into a function to confirm what it received.

backtrace shows the stack of function calls that led to the current point. Frame 0 is the currently executing function; frame 1 is its caller; higher numbers are older callers. frame N jumps to frame N so you can inspect its variables. up and down navigate one frame at a time.

Arrays print as a single address by default; print specific indices like arr[5] or use x command (see GDB Inspecting Memory) to dump multiple elements.

wiki/gdb-examining-state.md · Last modified: by 127.0.0.1