View Both Assembly and C code

前端 未结 3 1029
情歌与酒
情歌与酒 2021-01-30 04:06

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.

相关标签:
3条回答
  • 2021-01-30 04:36

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

    0 讨论(0)
  • 2021-01-30 04:57

    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)
    
    0 讨论(0)
  • 2021-01-30 05:00

    For your purpose, try

    objdump -S <your_object_file>
    

    from man objdump:

    -S
    --source
     Display source code intermixed with disassembly, if possible.
     Implies -d.
    
    0 讨论(0)
提交回复
热议问题