Skip to content
systemcoding.dev
Assembly

Disassemble one function from the shell with gdb -batch

Print the disassembly of one function from a shell script with gdb -batch and -ex, without starting a debugging session.

1 min read
Summarize in AI

GDB can disassemble a function from the command line without starting the program: -batch runs the commands given with -ex and exits, and no banner is printed. That makes it usable in scripts and in a quick terminal check. The example assumes the file was built with GCC 14 using gcc -O1 -fcf-protection=none -g answer.c.

answer.cc
int answer(void)
{
    return 42;
}
Shell
gdb -batch -ex 'disassemble answer' ./a.out
Text
Dump of assembler code for function answer:
   0x0000000000001119 <+0>:	mov    $0x2a,%eax
   0x000000000000111e <+5>:	ret
End of assembler dump.

The addresses depend on the linker, but the offsets in angle brackets do not. Add -ex 'set disassembly-flavor intel' before the disassemble command for Intel syntax. The command accepts /r for raw bytes and /s to interleave source lines when the file has debug info. The advantage over objdump is that GDB understands symbols from separate debug files and can disassemble a function in a shared library once the process has loaded it, while objdump works on the file as it sits on disk. See the GDB manual on machine code.

Also inDebugging
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.