In this video, I'd like to go over the basics of using
the CodeLite IDE's debugger.
All the IDEs have some sort of debugger built in, and they
all work more or less the same.
There are some differences between them, but they all have break points.
They all have watches, and they all let you see your local
variables, which is really all we're interested in right now.
So I'm in the section 12 workspace, and I've got a project
called debugger, that's got some source code that i want to debug.
So what do I do?
Well, the first thing you want to do is, rather than build,
we're going to do debug.
So we're going to press F5.
But we're not going to do that quite yet, I want to set
up this environment first.
Because right now, I'm on a single monitor system.
And it's much nicer to work obviously with a dual monitor system so you
can have your console in one area and the debugger in the other.
But on a single monitor system, you need to make some room here.
So what I'm going to do is I'm going to grab this and just move
it over here so it fills up half the screen, move it over here
so it fills up half the screen.
Okay, then when I debug, I want the console window over here on
the right, but I don't really want it to take up half the screen.
I don't need it to take up half the screen, so I'm just going to
move this guy over to about there, and that should allow me to see
all the the icons that we can use up top to control the debugger.
Okay, again, this is just a personal preference.
You can do this whatever you like that works for you.
The next thing we need to do is, we need to set a breakpoint.
You need to set a breakpoint for the debugger.
Now I'm going to do that right in main.
If you look right next to line 16 here, not in this little gray gutter
area right here where the arrow is, but right to the left of that you
can click right in there, and you see that little red stop sign comes up.
That's a break point.
Okay, what's going to happen is the debugger is going to kick in.
And it's going to start executing your code, then it's going
to stop at that break point.
From there, you can go line by line, you can go into functions, you can
look at watches, you can do all kinds of stuff, which is pretty cool.
So that's where we're at now.
Now also notice up here in perspective, there's a debug layout.
This is the layout that will automatically kick in
when you start the debugger.
So it may look different on your end than from then on what
you're going to see on my end, but it's totally customizable.
You do whatever you like, close windows, open windows.
And when you're done, just go to perspective save current
layout as debug layout again.
So next time you run, it'll be in that perspective.
All right, so let's debug.
We're going to come up to the debugger and start continue
debugger or you can press F5.
Make sure you've got a break point set before you do this, otherwise
you're not going to see much.
So we're going to go to the debugger and start it, build and debug.
Now you'll see the console.
Now what I'm going to do with the console is just move it
over to the right so it fills up the rest of that space.
There you go.
So all the output, I'm going to see over here on the right.
Okay, you'll notice down here that I've got an output view,
and I've got a debugger view.
I really don't care about this output view.
I just want to look at my debugger view.
So you can close this site if you like, you can just
click that x right there.
And I've got only the debugger view.
Now in the debugger view, there's a lot of stuff in here.
All we're really interested in at this level right now are
your local variables and any watches you might want, and I'll
talk about watches in a second.
Cool, okay, so you can see here are my local variables I, x and y.
If you look at main, you'll see i., you'll see x and you'll see y.
X and y right here on line 22.
You'll also see that little break point right there the red.
There's a little green arrow in there.
I'm not sure if you'll be able to see it on the video, but you will
be able to see it in a minute when I start walking through the code.
Basically that's the line that's about to execute.
Okay, you'll notice right now I X and j have garbage in them, that's
because line 16 hasn't executed yet.
Okay, so the debug icons up here, you can see them, you can show
the current line, you can step in which is what we're going to
use to go into a function or you can just go to the next line.
Don't worry about step out.
These are the two main ones we're going to use.
Step in, next.
And if you want to stop your debugging session, you can press
that little stop button right here.
Okay, so let's do this.
And again, you may want to close your out your workspace view
here if you don't have enough space on your monitor, but
I've got plenty of space here.
So let's start.
Let's click on this button right here next.
And you've got there's your debugger, you can also click next
here you can press F10, whatever it is on a mac, I'm not sure.
But you can toggle beak points.
You've got a lot of different options here, but I'm just going
to use these icons for now because it's easier to see what I'm doing.
All right,s o we're here on line 16.
Let's press that button right here next.
Now when I do that, you'll notice that little green arrow will go
to the next line, that means that this line executes, and we should
see I down here change to 5.
Okay, so I'm going to do that see the green arrow is now on line 17, that's
the instruction that'll execute next.
And also notice that I is now 5.
Okay, sometimes you have to refresh this view down here, especially
when you're working with pointers.
If you need to do that, you just click that little button right
here, and it refreshes that view.
Okay, so now we're in this code while I is greater than equal to 0.
We know i is 5, right.
Sometimes it's pretty handy.
You can just kind of select i right there and press
ctrl and you can see i is 5.
It pops up in this little tool tip right here.
Okay, right now if I look at x, x is garbage because I
haven't gotten to that line yet.
So that can be pretty handy sometimes, but most of that information
you'll be able to see here.
Okay, so that's my locals.
Suppose I want to do a watch, I want to watch I greater than 0.
So I can come over here, and I can select the plus sign and type
the entire expression, i greater than 0 and press okay, you can see
it's a Boolean, and right now it's true because 5 is greater than 0.
So you could switch between these two.
You can obviously pull this pane out and have it in a dual monitor system.
But right now, let's just keep going.
So now i is 5 and we'll keep clicking on the next.
You can see I'm in the body of the loop now.
Now we have an output statement, so we should see 5 display
out here on the console.
And there you can see the 5 just display.
Now we're going to decrement I, so I should go down to 4 down
here in my local variables view.
And there it is 4.
And I'm just going to keep looping through here.
All I'm doing is pressing next.
You can see my code executing.
It's very similar to what I was doing with my pencil
and marker on the screen.
But here you can see, it actually happening happening live.
So at this point now, I becomes 0, the loop will fail, and I'm out of it.
Okay, now I'm executing line 22.
When I click next, those two variables, x and y, will be
initialized right down here.
So I'll click next.
And you can see that they're 100 and 200, exactly what I expected.
Now I'll execute lines 23 and 24, so we should display
x and y to the console.
There's x, there's y.
Okay, so far so good.
Now I'm calling the swap function that we wrote in this section,
and I'm passing in the address of x and the address of y.
If you just click next here, it'll just skip that and go
right next to the next line.
Well, actually it'll call the function but I want to go into that function.
So you want to click this one right here called step in.
So when I select that, now I go into the function, so I'm
starting to execute that function.
You can see the green arrow right here on line nine.
Okay, and you can see a and b.
Well, a and b are pointers, and they've already been set.
You can see them right here.
There's a, there's b.
Those are the two addresses, and you can see their type, integer pointers.
Temp is just a regular int.
So if i expand a, it tells me what a is pointing to, which is a 100.
Expand b, b is pointing to 200.
Okay, so now let's walk through the swap.
The first line says take what a is pointing to.
Well, you know what a is pointing to right here, 100 and assign it to temp.
So right now temp has garbage.
So let's do that.
We'll click next, and now you see temp has a 100 in it.
Then we're taking what b is pointing to, which is 200 and putting
it into where a was pointing.
So now I'll go next.
You see right here, you see how it didn't refresh, it doesn't
do that with pointers sometimes.
That's when you have to click this little refresh dial right there,
and then just re-expand them, and you can see that it did do that now.
We'll do the next and refresh one more time and you can see that they've
been swapped, which is pretty cool.
At this point, I click next, this function is done.
Notice that my local variables now are a b and temp, that's because
I'm in this function right here.
Those are the local variables.
My watch doesn't apply anymore, right.
It's in the main.
So here, I'll click next again.
Now I'm back in main.
I'm right here on line 28.
And I'm going to display x and y again.
There's x 200, there's y 100, and our program is done.
Okay, so there's a lot of options that you can do.
You can learn to play with the debugger.
There's really -- debuggers are awesome.
The best advice I could give you with a debugger is that they are -- they're
called debuggers for a purpose because they really help you debug your code.
I wouldn't suggest that you start using a debugger as so much of
a learning aid at this moment.
I think it's better just to do it with paper and pencil at the
beginning because the debugger itself has a bit of a learning curve.
So you're learning c++, and you're learning the IDE
and now you're learning a debugger all at the same time.
But in the context of what i just did here, it's very useful.
Suppose you have a bug in your code and the bug was that, it
was greater than or equal to 0 or you had a less than there.
And maybe you didn't see it when you're looking at your code,
well when you run it through the debugger bam, you'll see the
error, you can fix it right away.
Okay, so feel free to use the debugger, feel free to use the
debugger in the challenge exercise for this section, it might come in handy.