ChatGPT解决这个技术问题 Extra ChatGPT

Show current assembly instruction in GDB

I'm doing some assembly-level debugging in GDB. Is there a way to get GDB to show me the current assembly instruction in the same way that it shows the current source line? The default output after every command looks like this:

0x0001433f      990         Foo::bar(p);

This gives me the address of the current instruction, but I have to keep referring back to the output of disassemble in order to see which instruction I'm currently executing.


P
Peter Mortensen

You can switch to assembly layout in GDB:

(gdb) layout asm

See here for more information. The current assembly instruction will be shown in assembler window.

   ┌───────────────────────────────────────────────────────────────────────────┐
   │0x7ffff740d756 <__libc_start_main+214>  mov    0x39670b(%rip),%rax        #│
   │0x7ffff740d75d <__libc_start_main+221>  mov    0x8(%rsp),%rsi              │
   │0x7ffff740d762 <__libc_start_main+226>  mov    0x14(%rsp),%edi             │
   │0x7ffff740d766 <__libc_start_main+230>  mov    (%rax),%rdx                 │
   │0x7ffff740d769 <__libc_start_main+233>  callq  *0x18(%rsp)                 │
  >│0x7ffff740d76d <__libc_start_main+237>  mov    %eax,%edi                   │
   │0x7ffff740d76f <__libc_start_main+239>  callq  0x7ffff7427970 <exit>       │
   │0x7ffff740d774 <__libc_start_main+244>  xor    %edx,%edx                   │
   │0x7ffff740d776 <__libc_start_main+246>  jmpq   0x7ffff740d6b9 <__libc_start│
   │0x7ffff740d77b <__libc_start_main+251>  mov    0x39ca2e(%rip),%rax        #│
   │0x7ffff740d782 <__libc_start_main+258>  ror    $0x11,%rax                  │
   │0x7ffff740d786 <__libc_start_main+262>  xor    %fs:0x30,%rax               │
   │0x7ffff740d78f <__libc_start_main+271>  callq  *%rax                       │
   └───────────────────────────────────────────────────────────────────────────┘
multi-thre process 3718 In: __libc_start_main     Line: ??   PC: 0x7ffff740d76d
#3  0x00007ffff7466eb5 in _IO_do_write () from /lib/x86_64-linux-gnu/libc.so.6
#4  0x00007ffff74671ff in _IO_file_overflow ()
   from /lib/x86_64-linux-gnu/libc.so.6
#5  0x0000000000408756 in ?? ()
#6  0x0000000000403980 in ?? ()
#7  0x00007ffff740d76d in __libc_start_main ()
   from /lib/x86_64-linux-gnu/libc.so.6
(gdb)

@greatwolf, looks like you have no tui support in your gdb. See this question for more information: stackoverflow.com/q/6706838/72178.
Neat! Now can I have a similar window for the registers? Indeed I can: layout regs
See also gdb docs for other TUI commands, like tui reg vector to show the vector regs instead of the integer regs. (Not always very usable, though, because it doesn't let you pick just the .v8_int16 or something, so the display is a big mess.) See the x86 tag wiki for a quick tutorial to debugging asm.
That's about a useless feature and output. The C++ mangled names are too long, and everything I'm trying to view is off the screen on the right. What a stupid decision (not to display ASM by default when si), and what a useless feature (viewport that does not display the necessary information). There's no sense in down voting this answer since you're only the messenger...
similarly, ·layout src to see source code when debugging, and also worthing remembering exit this mode by CTRL+x+a
E
Employed Russian

You can do

display/i $pc

and every time GDB stops, it will display the disassembly of the next instruction.

GDB-7.0 also supports set disassemble-next-line on, which will disassemble the entire next line, and give you more of the disassembly context.


How do we enable this feature when using si (but not s)?
You can also use display/ni $pc here to display n instructions, like in the comment to stackoverflow.com/a/1902906/129550
b
bmargulies

The command

x/i $pc

can be set to run all the time using the usual configuration mechanism.


And x/ni $pc to view the next n instructions, which is often quite useful.
what is the configuration mechanism to run a command all the time?
J
Jakuje

Setting the following option:

set  disassemble-next-line on
show disassemble-next-line

Will give you results that look like this:

(gdb) stepi
0x000002ce in ResetISR () at startup_gcc.c:245
245 {
   0x000002cc <ResetISR+0>: 80 b5   push    {r7, lr}
=> 0x000002ce <ResetISR+2>: 82 b0   sub sp, #8
   0x000002d0 <ResetISR+4>: 00 af   add r7, sp, #0
(gdb) stepi
0x000002d0  245 {
   0x000002cc <ResetISR+0>: 80 b5   push    {r7, lr}
   0x000002ce <ResetISR+2>: 82 b0   sub sp, #8
=> 0x000002d0 <ResetISR+4>: 00 af   add r7, sp, #0

This option doesn't seem to exist in my installation. Has it been removed?
@fuz More likely, your gdb is old
@fuz present at least in GDB 8.1 in Ubuntu 18.04.
very useful skill
show disassemble-next-line is for test, to print the flag status, on or off
m
mohit

If you want the next few instructions to display automatically while stepping through the program you can use the display command as follows -

display /3i $pc

The above will display 3 instructions whenever a breakpoint is hit or when you single step the program.

More details at the blog entry here.


C
Ciro Santilli Путлер Капут 六四事

GDB Dashboard

https://github.com/cyrus-and/gdb-dashboard

This GDB configuration uses the official GDB Python API to show us whatever we want whenever GDB stops after for example next, much like TUI.

However I have found that this implementation is a more robust and configurable alternative to the built-in GDB TUI mode as explained at: gdb split view with code

For example, we can configure GDB Dashboard to show disassembly, source, registers and stack with:

dashboard -layout source assembly registers stack

Here is what it looks like if you enable all available views instead:

https://i.stack.imgur.com/mHC8f.png

Related questions:

Switching to assembly in gdb


@downvoters: please explain so I can learn and improve info. I believe that this is a superior alternative for the TUI currently accepted answer: stackoverflow.com/a/2015523/895245
Santilli This is a very useful tool. But when I use it, I can only look at the code near the current code location. Sometimes the code is executed to the nth line, but I want to see what the code is in other locations (such as a certain function)? Is there any way to make the position of the displayed code slide (if the assembly code can slide, it would be better)?
@cyj hi, do you want to show the disassembly of a specific function after every step? Or just after entering a given command manually from time to time?
It does not need to be displayed all the time. Just sometimes I want to see what the code is in other locations. So I can roughly know what the result is when debugging. I wonder if I can scroll the position of the displayed code, because when debugging, only a few lines of code near the code being executed are displayed, which makes me unable to understand it based on the context.
@cyj if it is the current file, I often use the edit command to open the code in vim: vi.stackexchange.com/questions/2046/… If it is in another file, I tend to just use Eclipse and jump to the definition of a nearby function with Ctrl + Shift + T :-) Not perfect, but good enough.
a
abhi

From within gdb press Ctrl x 2 and the screen will split into 3 parts.

First part will show you the normal code in high level language.

Second will show you the assembly equivalent and corresponding instruction Pointer.

Third will present you the normal gdb prompt to enter commands.

https://i.stack.imgur.com/tiVcg.png


I could not launch it with Ctrl-X 2, but it looks like gdb -tui mode, which is great.
This is also reachable with layout split from the gdb prompt.
T
Tom

There is a simple solution that consists in using stepi, which in turns moves forward by 1 asm instruction and shows the surrounding asm code.