If you have -O2 code the source line <-> PC do not map linearly, they "jump". objdump -dS displays the disassembly annotated by source lines. GDB disass/m displays the source lines annotated by disassembly. I find the objdump way better, in fact the only meaningful one. GDB dump below displays PCs: 0x0, 0x1, 0xc, 0x7, 0xf, 0x11, 0x12. ----------------------------------------------------------------------------- int f (void) { extern int j; int i = j + 1; extern void g (void); g(); return i; } ----------------------------------------------------------------------------- gcc -O2 -g (gcc-4.4.4-10.fc13.x86_64) ----------------------------------------------------------------------------- objdump -dS 0000000000000000 <f>: int f (void) { 0: 53 push %rbx extern int j; int i = j + 1; 1: 8b 1d 00 00 00 00 mov 0x0(%rip),%ebx # 7 <f+0x7> extern void g (void); g(); 7: e8 00 00 00 00 callq c <f+0xc> int f (void) { extern int j; int i = j + 1; c: 83 c3 01 add $0x1,%ebx extern void g (void); g(); return i; } f: 89 d8 mov %ebx,%eax 11: 5b pop %rbx 12: c3 retq ----------------------------------------------------------------------------- (gdb) disass/m f Dump of assembler code for function f: 3 { 0x0000000000000000 <+0>: push %rbx 4 extern int j; 5 int i = j + 1; 0x0000000000000001 <+1>: mov 0x0(%rip),%ebx # 0x7 <f+7> 0x000000000000000c <+12>: add $0x1,%ebx 6 7 extern void g (void); 8 g(); 0x0000000000000007 <+7>: callq 0xc <f+12> 9 return i; 10 } 0x000000000000000f <+15>: mov %ebx,%eax 0x0000000000000011 <+17>: pop %rbx 0x0000000000000012 <+18>: retq End of assembler dump.
The master branch has been updated by Doug Evans <devans@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6ff0ba5f7b8a2b10642bbb233a32043595c55670 commit 6ff0ba5f7b8a2b10642bbb233a32043595c55670 Author: Doug Evans <xdje42@gmail.com> Date: Fri Aug 14 21:45:54 2015 -0700 New /s modifier for the disassemble command. The "source centric" /m option to the disassemble command is often unhelpful, e.g., in the presence of optimized code. This patch adds a /s modifier that is better. For one, /m only prints instructions from the originating source file, leaving out instructions from e.g., inlined functions from other files. gdb/ChangeLog: PR gdb/11833 * NEWS: Document new /s modifier for the disassemble command. * cli/cli-cmds.c (disassemble_command): Add support for /s. (_initialize_cli_cmds): Update online docs of disassemble command. * disasm.c: #include "source.h". (struct deprecated_dis_line_entry): Renamed from dis_line_entry. All uses updated. (dis_line_entry): New struct. (hash_dis_line_entry, eq_dis_line_entry): New functions. (allocate_dis_line_table): New functions. (maybe_add_dis_line_entry, line_has_code_p): New functions. (dump_insns): New arg end_pc. All callers updated. (do_mixed_source_and_assembly_deprecated): Renamed from do_mixed_source_and_assembly. All callers updated. (do_mixed_source_and_assembly): New function. (gdb_disassembly): Handle /s (DISASSEMBLY_SOURCE). * disasm.h (DISASSEMBLY_SOURCE_DEPRECATED): Renamed from DISASSEMBLY_SOURCE. All uses updated. (DISASSEMBLY_SOURCE): New macro. * mi/mi-cmd-disas.c (mi_cmd_disassemble): New modes 4,5. gdb/doc/ChangeLog: * gdb.texinfo (Machine Code): Update docs for mixed source/assembly disassembly. (GDB/MI Data Manipulation): Update docs for new disassembly modes. gdb/testsuite/ChangeLog: * gdb.mi/mi-disassemble.exp: Update. * gdb.base/disasm-optim.S: New file. * gdb.base/disasm-optim.c: New file. * gdb.base/disasm-optim.h: New file. * gdb.base/disasm-optim.exp: New file.
Patch committed.