Methods and Principles of Printing Register Values in GDB Debugger

Nov 20, 2025 · Programming · 16 views · 7.8

Keywords: GDB Debugging | Register Printing | info registers Command

Abstract: This paper provides an in-depth exploration of various methods for printing register values in the GDB debugger, with a focus on the usage techniques of the info registers command and its variants. Through detailed code examples and explanations of architectural differences, it elucidates the distinctions in register naming between 32-bit and 64-bit systems, as well as the application scenarios of standard register aliases. The article also combines the impact of stack frame selection on register value display to explain the differences between virtual and raw formats, offering comprehensive technical guidance for program debugging.

Basic Commands for Register Printing in GDB

In the GDB debugging environment, viewing register values is a fundamental operation during the debugging process. The most commonly used command is info registers, which displays the names and values of all registers in the current stack frame, excluding floating-point and vector registers. This command supports the abbreviated form i r, significantly improving debugging efficiency.

Methods for Viewing Specific Registers

When needing to view a specific register, the format info registers regname can be used. For example, to view the value of the EAX register, simply enter info registers eax. It is important to note that when specifying register names, the % symbol should not be included, which is a characteristic of GDB syntax.

Impact of Architectural Differences on Register Naming

Different processor architectures use different register naming conventions. In 32-bit x86 systems, general-purpose registers start with e, such as eax, ebp, etc.; whereas in 64-bit x86-64 systems, register names start with r, such as rax, rbp. Incorrect use of register prefixes will cause GDB to fail to recognize the register.

Standard Register Alias System

GDB provides four standard register aliases that are available on most architectures: $pc (program counter), $sp (stack pointer), $fp (frame pointer), and $ps (processor status). These aliases correspond to architecture-specific canonical names; for example, in x86 systems, $ps is actually an alias for the EFLAGS register.

Using Register Values in Expressions

Register values can be used as variables in GDB expressions, with variable names starting with $. For example, p/x $pc can print the program counter value in hexadecimal format, and x/i $pc can display the next instruction to be executed. Register values can also be modified using expressions like set $sp += 4.

Special Handling of Register Data Formats

Some registers have both "raw" and "virtual" data formats. The info registers command displays both formats. For special registers containing packed data (such as SSE and MMX registers), GDB uses struct notation to display their contents; for example, print $xmm1 will show various data type views of that register.

Impact of Stack Frame Selection on Register Values

The display of register values is closely related to the currently selected stack frame. By default, the displayed register values are relative to the selected stack frame, meaning the values that the registers would contain if all deeper stack frames were exited and their saved registers restored. To view the actual hardware register contents, the innermost stack frame must be selected (using the frame 0 command).

Calling Conventions and Register Preservation

According to the Application Binary Interface (ABI) specifications, some registers are marked as caller-saved registers, also known as volatile registers. If a register is not saved by the callee and GDB determines through debug information or ABI knowledge that the register is caller-saved, it will display <not saved> as its value.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.