49

Do we have a way to view assembly and c code both using gdb.

disassemble function_name shows only assembly, I was trying to find a way to easliy map c code to assembly. Thanks

Tectrendz
  • 1,354
  • 2
  • 17
  • 36

4 Answers4

66

You can run gdb in Text User Interface (TUI) mode:

gdb -tui <your-binary>
(gdb) b main
(gdb) r
(gdb) layout split

The layout split command divides the window into two parts - one of them displaying the source code, the other one the corresponding assembly. A few others tricks:

  • set disassembly-flavor intel - if your prefer intel notation
  • set print asm-demangle - demangles C++ names in assembly view
  • ni - next instruction
  • si - step instruction

If you do not want to use the TUI mode (e.g. your terminal does not like it), you can always do:

x /12i $pc

which means print 12 instructions from current program counter address - this also works with the tricks above (demangling, stepping instructions, etc.).

The "x /12i $pc" trick works in both gdb and cgdb, whereas "layout split" only works in gdb.

Enjoy :)

Community
  • 1
  • 1
sirgeorge
  • 6,331
  • 1
  • 28
  • 33
44

Try disassemble /m.

Refer to http://sourceware.org/gdb/current/onlinedocs/gdb/Machine-Code.html#Machine-Code

The format is similar to that of objdump -S, and intermixes source with disassembly. Sample output excerpt:

10      int i = 0;
=> 0x0000000000400536 <+9>: movl   $0x0,-0x14(%rbp)

11      while (1) {
12          i++;
   0x000000000040053d <+16>:    addl   $0x1,-0x14(%rbp)
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Yorkwar
  • 1,204
  • 1
  • 11
  • 27
  • 4
    I didn't want the other solutions because I don't want to restart gdb and get it set up the same way again and the .o file's references to relocated sections make it hard to understand. So I thought this was just the ticket. Imagine my horror, then, when I eventually realized that disassemble/m silently omits some instructions, like after the first, three byte instruction: ` 0x000000000442f038 <+24>: mov %rsi,%rbx 0x000000000442f043 <+35>: sub $0x38,%rsp` That's `GNU gdb (GDB) 7.4.1-debian`. – Martin Dorey Oct 23 '17 at 18:40
  • 3
    The gdb docs discourage `/m` and suggest `/s` instead: "The /m option is deprecated as its output is not useful when there is either inlined code or re-ordered code. The /s option is the preferred choice. Here is an example for AMD x86-64 showing the difference between /m output and /s output. This example has one inline function defined in a header file, and the code is compiled with ‘-O2’ optimization. Note how the /m output is missing the disassembly of several instructions that are present in the /s output." -- https://sourceware.org/gdb/current/onlinedocs/gdb/Machine-Code.html – thakis Jan 04 '21 at 16:37
9

For your purpose, try

objdump -S <your_object_file>

from man objdump:

-S
--source
 Display source code intermixed with disassembly, if possible.
 Implies -d.
chris
  • 91
  • 1
  • 1
2

The fastest way to obtain this is to press the key combination ctrl-x 2 after launching gdb.

This will give you immediately a split window with source code and assembly in Text User Interface Mode (described in accepted answer).

Just another tooltip: keyboard arrows in this mode are used for navigate up and down through the source code, to use them to access commands history you can use ctrl-x o that will refocus on gdb shell window.

piertoni
  • 1,933
  • 1
  • 18
  • 30