Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between nexti and stepi in gdb?

Tags:

c++

c

debugging

gdb

While debugging an executable using gdb, there are two commands which we can use to step through the execution:

  • stepi and
  • nexti

What is/are the difference/s between these two and why would anyone choose one over the other?

using help in gdb says:

stepi: Step one instruction exactly.

nexti: Step one instruction, but proceed through subroutine calls.

since we are dealing with instructions and machine code here (the smallest part of a program in execution) I can't figure out what the subroutine calls are.

like image 815
Kobayashi Avatar asked Aug 26 '18 08:08

Kobayashi


People also ask

What is Stepi in gdb?

The simplest GDB execution command is the stepi command to step the target a single machine instruction. The RSP packet exchanges to implement the GDB stepi command are shown as a sequence diagram in Figure 3.4. In this example the instruction at address 0x100 is executed.

What's the difference between gdb commands step and Stepi?

The gdb terms (and commands) are step and next and the difference is that step continues to run until it changes line of source code, while next doesn't trace into a subroutine, but rather skips over it. The stepi and nexti commands are similar but operate at the machine instruction level rather than source code level.

How do I get past breakpoints in gdb?

Just press c. It will continue execution until the next breakpoint. You can also disable intermediate breakpoints by using disable #breakpointnumber as stated here.

How do I skip a function call in gdb?

Functions may be skipped by providing either a function name, linespec (see Specify Location), regular expression that matches the function's name, file name or a glob -style pattern that matches the file name.


2 Answers

The difference is how call is treated:

  • stepi dives into call
  • nexti runs call but doesn't walk you through its code

Hence here's the semantical rule to remember it better: you are to step if you need to walk through

like image 190
poige Avatar answered Sep 22 '22 16:09

poige


stepi is more detailed than nexti. if you call sum() from main() function then doing stepi reaches you inside the sum() function, but nexti doesn't.

Below is the screenshot when you call stepi when you were at call of sum() instruction (i.e., => 0x08048403 <+40>: call 0x8048419 <sum>). The stepi instuction routes you inside the sum().

enter image description here

If you do nexti when you were at call of sum() instruction (i.e., => 0x08048403 <+40>: call 0x8048419 <sum>) then it uses the returned value from sum method and goes to the next instruction of main method, screenshot as below.

enter image description here

Conclusion: Use stepi if you want to see every machine instructions that happened in your processor. Use nexti if you wanna see only the machine instructions executed at the main().

like image 44
Uddhav P. Gautam Avatar answered Sep 21 '22 16:09

Uddhav P. Gautam