Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB single stepping assembly and displaying the next instruction which will be executed. [duplicate]

Tags:

c

debugging

gdb

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)
like image 958
dcrearer Avatar asked Feb 06 '23 18:02

dcrearer


1 Answers

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>
like image 94
dcrearer Avatar answered May 06 '23 17:05

dcrearer