debuggingidebreakpoints

What is the difference between Step Into and Step Over in a debugger


I want to debug the whole flow of a (Java) program. I see there are several options for stepping through my program. What is the difference between step into and step over?


Solution

  • Consider the following (Java) code with your current instruction pointer (the line that will be executed next, line #7, indicated by ->), having just been called by line #12:

    # 1     public class testprog {
    # 2       static void f(int x) {
    # 3         System.out.println("x=" + (x+0));  // STEP INTO
    # 4       }
    # 5
    # 6       static void g(int x) {
    # 7 ->      f(x);
    # 8         f(1);                             // STEP OVER
    # 9       }
    #10
    #11       public static void main(String args[]) {
    #12         g(2);
    #13         g(3);                             // STEP OUT OF
    #14       }
    #15     }
    

    If you step-into at that point, you would move to line #3, stepping into the function call. Basically, you step to the first line of any function that you're ready to call.

    However, if you step-over at that point, you would move to line #8, stepping over the f(x) function call. To be clear, this means executing the whole function before once again stopping, not skipping over the function entirely.

    Note that there is no real difference between step-into and step-over if the code you're about to execute is not a function call of some sort. For example, both of them would simply execute a statement like xyzzy = 42; and then move on to the next statement.

    Also note that the definition of a function call may include more complex things like:

    int twenty_one = add_one_to(multiply(10, 2))`
    

    Stepping into on that line (the first time) would enter multiply with the arguments 10 and 2. Then, when you return from multiply, you would still be on that line and step into would then enter add_one_to with whatever argument was returned from multiply. It's also like that you would still be on that line when you return the second time since you still have to assign the result to twenty_one.


    Another useful feature of debuggers is the step-out-of or step-return facility. In that case, it will basically run you through the remainder of the current function until you go back up one level.

    With our example code above, with line #7 being the next line to execute, it will step through both the f(x) and f(1) calls, then back out to the calling function to end up at line #13.