7

Using the gdb debbuger what command can I execute to single step and display the next instruction that will be executed? I'm familiar with windbg where this operation is pretty straight forward.

So for example I have the following function and as I step into the code via si I want to display the next instruction that will be executed without having to do a full disassembly via disassemble. How can I accomplish this?

Dump of assembler code for function isEven:
   0x0000000100000f20 <+0>: push   %rbp
   0x0000000100000f21 <+1>: mov    %rsp,%rbp
   0x0000000100000f24 <+4>: mov    $0x2,%eax
   0x0000000100000f29 <+9>: mov    %edi,-0x4(%rbp)
=> 0x0000000100000f2c <+12>:    mov    -0x4(%rbp),%edi
   0x0000000100000f2f <+15>:    mov    %eax,-0xc(%rbp)
   0x0000000100000f32 <+18>:    mov    %edi,%eax
   0x0000000100000f34 <+20>:    cltd
   0x0000000100000f35 <+21>:    mov    -0xc(%rbp),%edi
   0x0000000100000f38 <+24>:    idiv   %edi
   0x0000000100000f3a <+26>:    cmp    $0x0,%edx
   0x0000000100000f3d <+29>:    jne    0x100000f4f <isEven+47>
   0x0000000100000f43 <+35>:    movl   $0x1,-0x8(%rbp)
   0x0000000100000f4a <+42>:    jmpq   0x100000f56 <isEven+54>
   0x0000000100000f4f <+47>:    movl   $0x0,-0x8(%rbp)
   0x0000000100000f56 <+54>:    mov    -0x8(%rbp),%eax
   0x0000000100000f59 <+57>:    pop    %rbp
   0x0000000100000f5a <+58>:    retq
End of assembler dump.
(gdb)
dcrearer
  • 1,972
  • 4
  • 24
  • 48
  • So what about the documentation or built-in help did you not understand? What did you find by a simple online-search? – too honest for this site Nov 06 '16 at 01:47
  • @Olaf I actually have the GNU Source level debugger manual and I've used to determine how to single step etc. However I didn't see any information on how to display the next instruction. – dcrearer Nov 06 '16 at 01:52
  • @Olaf thanks for the motivation I found show disassemble-next-line and set disassemble-next-line on. – dcrearer Nov 06 '16 at 01:56
  • I honestly hope the slight kick did not hurt too much:-) Downvoted your question, but I'll UV your self-answer - It might be useful for mee, too. (Just was never that important to me to search for it - I rarely debug extensively at assembler-level). – too honest for this site Nov 06 '16 at 07:20

1 Answers1

15

I found that the following sequence of instructions accomplishes my objective.

(gdb) show disassemble-next-line
Debugger's willingness to use disassemble-next-line is off.
(gdb) set disassemble-next-line on
(gdb) show disassemble-next-line
Debugger's willingness to use disassemble-next-line is on.

Thanks Olaf!

(gdb) si
0x0000000100000f32  27      if(num % 2 == 0 )
   0x0000000100000f2c <isEven+12>:  8b 7d fc    mov    -0x4(%rbp),%edi
   0x0000000100000f2f <isEven+15>:  89 45 f4    mov    %eax,-0xc(%rbp)
=> 0x0000000100000f32 <isEven+18>:  89 f8   mov    %edi,%eax
   0x0000000100000f34 <isEven+20>:  99  cltd
   0x0000000100000f35 <isEven+21>:  8b 7d f4    mov    -0xc(%rbp),%edi
   0x0000000100000f38 <isEven+24>:  f7 ff   idiv   %edi
   0x0000000100000f3a <isEven+26>:  83 fa 00    cmp    $0x0,%edx
   0x0000000100000f3d <isEven+29>:  0f 85 0c 00 00 00   jne    0x100000f4f <isEven+47>
dcrearer
  • 1,972
  • 4
  • 24
  • 48