55

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

4 Answers 4

70

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 :)

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way to also get rid of top split window? It's unncessary and takes space.
@BabkenVardanyan Try layout next. If it does not give you the desired layout try this command a few more times. This command keeps switching between layouts so you can keep the one you want.
Any way to also see the register as in layout regs?
51

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)

2 Comments

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.
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." -- sourceware.org/gdb/current/onlinedocs/gdb/Machine-Code.html
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.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.