Disassemble in the middle of an instruction with gdb?

Viewed 129

I believe this worked perfectly previously, but maybe I just forgot the correct syntax.

(gdb) disas main
Dump of assembler code for function main:
   0x0000000000001125 <+0>: push   rbp
   0x0000000000001126 <+1>: mov    rbp,rsp
   0x0000000000001129 <+4>: mov    DWORD PTR [rbp-0x4],edi
   0x000000000000112c <+7>: mov    QWORD PTR [rbp-0x10],rsi
   0x0000000000001130 <+11>:    mov    eax,0x0
   0x0000000000001135 <+16>:    pop    rbp
   0x0000000000001136 <+17>:    ret    

Now I want to disassemble at 0x0000000000001127, which is 1 byte into the first mov instruction:

(gdb) disas 0x0000000000001127
Dump of assembler code for function main:
   0x0000000000001125 <+0>: push   rbp
   0x0000000000001126 <+1>: mov    rbp,rsp
   0x0000000000001129 <+4>: mov    DWORD PTR [rbp-0x4],edi
   0x000000000000112c <+7>: mov    QWORD PTR [rbp-0x10],rsi
   0x0000000000001130 <+11>:    mov    eax,0x0
   0x0000000000001135 <+16>:    pop    rbp
   0x0000000000001136 <+17>:    ret    

It still starts the disassembly at the top of main.

I've also tried things such as main+1, disas /r, etc. Did gdb's behavior change somehow? I thought perhaps it was related to this being a PIE binary, but when I recompile it with -no-pie I still have this problem for something so simple.

What is the correct syntax?

1 Answers

It still starts the disassembly at the top of main.

When you give disas a single argument, it finds the enclosing function, and disassembles that entire function. This has been the behavior since forever.

If you give disas two arguments instead, then it will disassemble just the given range:

(gdb) disas &main
Dump of assembler code for function main:
   0x00000000000005fa <+0>: push   %rbp
   0x00000000000005fb <+1>: mov    %rsp,%rbp
   0x00000000000005fe <+4>: mov    $0x0,%eax
   0x0000000000000603 <+9>: pop    %rbp
   0x0000000000000604 <+10>:    retq   
End of assembler dump.

(gdb) disas &main+3,&main+11
Dump of assembler code from 0x5fd to 0x605:
   0x00000000000005fd <main+3>: in     $0xb8,%eax
   0x00000000000005ff <main+5>: add    %al,(%rax)
   0x0000000000000601 <main+7>: add    %al,(%rax)
   0x0000000000000603 <main+9>: pop    %rbp
   0x0000000000000604 <main+10>:    retq   
End of assembler dump.

You could also use x/i:

(gdb) x/4i &main+3
   0x5fd <main+3>:  in     $0xb8,%eax
   0x5ff <main+5>:  add    %al,(%rax)
   0x601 <main+7>:  add    %al,(%rax)
   0x603 <main+9>:  pop    %rbp
Related