Multiple Methods and Practical Guide for Displaying Current Assembly Instructions in GDB

Nov 21, 2025 · Programming · 13 views · 7.8

Keywords: GDB Debugging | Assembly Instructions | layout asm | display command | Disassembly

Abstract: This article comprehensively explores three main methods for displaying current assembly instructions in the GDB debugger: using the layout asm command to enter assembly layout mode, employing the display/i $pc command for automatic instruction display, and utilizing the x/i $pc command for manual inspection. Through rich code examples and practical debugging scenario analysis, the article provides an in-depth comparison of the advantages and disadvantages of various approaches, along with advanced techniques such as mixed source-assembly display and disassembler option configuration. Drawing from GDB official documentation, it systematically introduces the various parameter usages and display effects of the disassemble command, offering comprehensive technical reference for assembly-level debugging.

Introduction

In low-level system programming and reverse engineering, assembly-level debugging is an indispensable skill. GDB (GNU Debugger), as a powerful debugging tool, provides multiple methods for viewing and tracking assembly instructions. However, many developers often face a common practical challenge: how to conveniently view the currently executing assembly instruction without repeatedly entering the disassemble command.

Assembly Layout Mode: layout asm

GDB's TUI (Text User Interface) mode offers the most intuitive way to view assembly instructions. Through the layout asm command, users can enter a dedicated assembly layout interface:

(gdb) layout asm

After executing this command, the GDB interface divides into multiple windows, with one specifically displaying the assembly code of the current function. The currently executing instruction is clearly marked with a > symbol, making instruction tracking straightforward. This layout mode is particularly suitable for debugging scenarios requiring continuous single-stepping through assembly instructions.

In practical use, the assembly layout window displays content similar to:

   ┌───────────────────────────────────────────────────────────────────────────┐
   │0x7ffff740d756 <__libc_start_main+214>  mov    0x39670b(%rip),%rax        #│
   │0x7ffff740d75d <__libc_start_main+221>  mov    0x8(%rsp),%rsi              │
   │0x7ffff740d762 <__libc_start_main+226>  mov    0x14(%rsp),%edi             │
  >│0x7ffff740d766 <__libc_start_main+230>  mov    (%rax),%rdx                 │
   │0x7ffff740d769 <__libc_start_main+233>  callq  *0x18(%rsp)                 │
   │0x7ffff740d76d <__libc_start_main+237>  mov    %eax,%edi                   │
   └───────────────────────────────────────────────────────────────────────────┘

Automatic Instruction Display: display Command

For users who prefer the traditional command-line interface, the display command provides an automated solution for showing assembly instructions:

(gdb) display/i $pc

This command configures GDB to automatically display the instruction pointed to by the program counter ($pc) every time the program stops. The /i parameter here specifies displaying memory content in instruction format. The advantage of this method is that it maintains command-line simplicity without requiring interface mode switching.

Starting from GDB 7.0, users can also employ the set disassemble-next-line on command:

(gdb) set disassemble-next-line on

This setting causes GDB to display not only the next source line (if available) but also the corresponding assembly instructions when the program stops. When source code is unavailable, GDB automatically shows the disassembly of the next instruction.

Manual Instruction Inspection: x Command

The most fundamental method for viewing assembly instructions is using the x (examine) command:

(gdb) x/i $pc

This command displays the instruction pointed to by the current program counter. Although manual execution is required, it can be set up as a frequently used command through GDB's configuration mechanism or bound to specific keyboard shortcuts.

Advanced Disassembly Features

GDB's disassemble command offers rich parameter options supporting various display formats:

(gdb) disassemble /s main

The /s parameter displays mixed source and assembly code, which is currently the recommended approach, especially when dealing with inline functions and optimized code. In contrast, the /m parameter has been deprecated due to its incomplete output in optimized code.

For scenarios requiring inspection of raw instruction bytes, the /r or /b parameters can be used:

(gdb) disassemble /r 0x400281,+10

Both parameters display instruction bytes in hexadecimal format but with different ordering: /b shows bytes in memory order, while /r displays them in instruction order (considering endianness).

Practical Tips and Best Practices

In actual debugging workflows, combining multiple methods yields optimal results. For example, use layout asm when detailed function logic analysis is needed, and employ x/i for quick checks at specific locations.

For x86 architecture users, disassembly style can be switched using:

(gdb) set disassembly-flavor intel

This changes the display style from the default AT&T syntax to Intel syntax, accommodating different user preferences.

Additionally, the info line command is particularly useful for mapping source code lines to program addresses:

(gdb) info line main

After executing this command, the default address for x/i is set to the starting address of the corresponding line, simplifying subsequent machine code inspection.

Conclusion

GDB provides multiple methods for viewing assembly instructions, ranging from simple to complex, catering to different scenarios and user preferences. layout asm is ideal for complex debugging tasks requiring continuous instruction execution tracking, display/i $pc offers automated convenience, and x/i $pc serves as the most fundamental manual inspection method. By combining the advanced features of the disassemble command with various configuration options, developers can establish efficient assembly-level debugging workflows.

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.