How do you use the feature like step into, step out

Thread Starter

King2

Joined Jul 17, 2022
163
I'm trying to understand how to use debugger tools to test c program. I am using gcc compiler and gdb debugger on linux desktop computer.

I have read the manual of the debugger but I still don't fully understand how to use its feature like continue, step over, step into, step out.

Screenshot from 2022-08-13 15-09-31.png

I want to understand when and how you use these four options?
 
Last edited:

BobTPH

Joined Jun 5, 2013
8,814
When you are debugging a program, the debugger will stop the program at places in the program you specify via setting breakpoints. Or, it might stop the program when an exception occurs.

When stopped, the debugger lets you enter commands. You can look at data and even change it.

Eventually, you want the program to go on.

That is what all of the commands you are asking about do.

“continue” will just start the program running and continue until another breakpoint or other event stops it.

“step into” executes s single line of code, stopping after it completes. If the line is a function call, it will stop at the first line if the function.

“step over” contnurs the orogram until the next line in the current function, not stopping in any function that us called.
 

Thread Starter

King2

Joined Jul 17, 2022
163
"step into” executes s single line of code, stopping after it completes. If the line is a function call, it will stop at the first line if the function.
step into (F11) button

When I run the debug button, the cursor is on line number 4.
When I press step into (F11) button it goes to line number 5
When I press the F11 button again, then it goes to line number 7
It stays on this line when I press the button again but value of i doesn't change

I was expecting that every single line is getting executed then the value of the variable should also change in the for loop but it is not happening
 

Thread Starter

King2

Joined Jul 17, 2022
163
hi K2,
Does your compiler create a background text listing, which you could examine with a text editor.?
I don't really get what it is your question
When I compile the program. I get output on terminal. When I debug the program I can see the value of variable as shown in this picture

1660409195850.png
 

DickCappels

Joined Aug 21, 2008
10,152
The exact actions may vary slightly from debugger to debugger. The documentation that came with the debugger should quell all doubts.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi K2,
What other files of the same Project name are in the same Folder as the Compiled Code, look in the Folder and post a clip of the file contents.
E
 

WBahn

Joined Mar 31, 2012
29,979
step into (F11) button

When I run the debug button, the cursor is on line number 4.
When I press step into (F11) button it goes to line number 5
When I press the F11 button again, then it goes to line number 7
It stays on this line when I press the button again but value of i doesn't change

I was expecting that every single line is getting executed then the value of the variable should also change in the for loop but it is not happening
Based on the screen capture you are showing, it looks like i changed because it is showing that i is equal to 1, whereas when the loop started it was equal to 0.

It would help us help you if you showed the output window as well, and then did a screen capture after it prints out, say, 2 or 3, so that we can see what i is at that point.
 

Thread Starter

King2

Joined Jul 17, 2022
163
It would help us help you if you showed the output window as well, and then did a screen capture after it prints out, say, 2 or 3, so that we can see what i is at that point.
When I run the debug button, the cursor is on line number 4.

1660439875823.png

When I press step into (F11) button it goes to line number 5

1660439976951.png

When I press the F11 button again, then it goes to line number 7

1660440153051.png

When I press the F11 button again, then it stay on line number 7

1660440237417.png

When I press the F11 button again, then it stay on line number 7

1660440365356.png

When I press the F11 button again, then it stay on line number 7

1660440477615.png


When I press the F11 button again, then it stay on line number 7
1660440569428.png
 

WBahn

Joined Mar 31, 2012
29,979
I don't see anywhere in those screenshots where 1/n was printed to the terminal window (and note that your printf() statement is printing the second argument, the value of i, using %d following by a forward slash followed by the character 'n'). If you want to print a newline character, then you need to use '\n' and not '/n'.

Why is the program crashing? Is that because you are doing something to kill it, or because it is simply crashing? Try replacing your format string with "%i\n" and see if it makes a difference.
 

Thread Starter

King2

Joined Jul 17, 2022
163
I don't see anywhere in those screenshots where 1/n was printed to the terminal window (and note that your printf() statement is printing the second argument,
When I press F10 step out button Then I can see value of i increase in watches window

Why is the program crashing? Is that because you are doing something to kill it, or because it is simply crashing? Try replacing your format string with "%i\n" and see if it makes a difference.
No I don't kill It is crashing

EDIT
I got it when I press F10 instead of F11
1660442606905.png
 

Attachments

Last edited:

WBahn

Joined Mar 31, 2012
29,979
It's possible that you are attempting to step into the printf() function, but the IDE can't show you that code because it doesn't have the source code for it, just the object code.

I'm not familiar with the detailed behavior of the Visual Studio debugger, especially if it's configured to use the gcc compiler and the gdb debugger.
 

Thread Starter

King2

Joined Jul 17, 2022
163
I'm not familiar with the detailed behavior of the Visual Studio debugger, especially if it's configured to use the gcc compiler and the gdb debugger.
Which IDE, compiler ant debugger do you use?

What's the difference between step into and step out?

When you do use step into or step out?
 

WBahn

Joined Mar 31, 2012
29,979
Which IDE, compiler ant debugger do you use?

What's the difference between step into and step out?

When you do use step into or step out?
I've use a few different IDEs, but I came up when you didn't have an IDE or a debugger. You typed your code into a text file and then sent it off to a compiler and got an executable file that you then ran. At one point, when I was first in college, you sat at a terminal to type your program and submitted your program for compilation and execution to the computer operator and collected your printout of the run a couple hours later. I even did a few runs where you generated punched cards and handed the stack to the operator who then loaded the program source code, compiled, and ran it. Fortunately, it was only a few programs that I had to do that way, but that was the norm for all students just a few years earlier.

Because I learned to program in that environment, I learned to look my code over very thoroughly myself -- what was often referred to as "desk checking" it. I also learned to put print statements into my code to help track down and debug it when it wasn't working properly. I still tend to do that today. While I do sometimes use IDE debuggers to step through code execution, I seldom use the more sophisticated tools that modern debuggers offer.

What step in, step over, and step out refer to, primarily, are what to do with function calls. Say you wrote a function named fred() and you have a line of code that calls it. If you are running the debugger reach that line of code. Should the debugger execute fred() and move onto the next line of code? Or should it push into the code for fred() and execute it step by step? The former is "step into" and the latter is "step over". Once you are inside fred(), you might decide that you have seen enough to ascertain that fred() isn't the problem and you don't want to continue stepping through the remaining lines of fred(), so you can choose to "step out" and have the debugger stop just after fred() returns.
 

Thread Starter

King2

Joined Jul 17, 2022
163
What step in, step over, and step out refer to, primarily, are what to do with function calls. Say you wrote a function named fred() and you have a line of code that calls it. If you are running the debugger reach that line of code. Should the debugger execute fred() and move onto the next line of code? Or should it push into the code for fred() and execute it step by step? The former is "step into" and the latter is "step over". Once you are inside fred(), you might decide that you have seen enough to ascertain that fred() isn't the problem and you don't want to continue stepping through the remaining lines of fred(), so you can choose to "step out" and have the debugger stop just after fred() returns.
Thank you so much. I understood how to use step in and step out.

Screenshot from 2022-08-14 16-35-19.png

I set a breakpoint on line number 14 and hit the step in button, I see only one line of the function executed.

I set a breakpoint on line number 14 and hit the step out button, I got the output of the function
 

WBahn

Joined Mar 31, 2012
29,979
Keeping in mind that how the debugger works varies from tool to tool, what I would expect to happen is that when you hit the breakpoint and hit 'step in' that it would take you into your sleep function and from there you could step through the loop one pass at a time. But if you hit the breakpoint and then hit 'step out' that you are then telling the debugger that you want to step out of the current function, which is main(), and so it will execute the rest of the program and quit.
 

Thread Starter

King2

Joined Jul 17, 2022
163
Keeping in mind that how the debugger works varies from tool to tool, what I would expect to happen is that when you hit the breakpoint and hit 'step in' that it would take you into your sleep function and from there you could step through the loop one pass at a time.
I am focusing on 'step in' right now. When I hit 'step in' again and again the value of the variable is not changing in the loop, this is the main problem for me.

I've been trying to fix this problem but haven't had any success yet
 

WBahn

Joined Mar 31, 2012
29,979
Perhaps the problem is that you are telling it to do something that it can't. The first time you hit 'step in' it steps into the sleep() function. But after that, you are telling it to step into your for() statement and there is nothing there to step into -- there's no function call. There is usually just a "step" command, though sometimes the "step over" command is the same thing. What happens if you hit step into once and then start hitting step over?
 
Top