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.
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.
int answer(void)
{
return 42;
}gdb -batch -ex 'disassemble answer' ./a.outDump 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.