So in this lecture, we are going to go over a kind of offshoot side topic called debugging.
So you may have been doing this already, which I hope you have.
But for those of you that know what debugging is, it's just solving errors in your code.
So if you run into an error, you know whether it be a compile time as a compiler area or at runtime,
or maybe you have some type of logical error when you're running your program and you're trying to figure
out why you got a value that you didn't expect as output or something like that.
Fixing all these errors is considered debugging and just kind of a fun fact, and I'm not a hundred
percent sure if this is true or not.
But what I heard was where debugging came from was way back in the day when there was gigantic computers
that were just like the size of, you know, small apartment or something.
There was a bug that had literally lodged itself in one of these computers, and I believe it was Grace
Hopper, a famous computer scientist that had to remove this bug.
I think it was a moth or something and fixed the error in the computer because there was some bug in
there, like shorting something now.
And so that kind of stuck.
And since then, they've referred to fixing errors as debugging.
So in honor of censure, if that's true, but just kind of an interesting story I thought I'd share.
But.
Yeah, what we're going to be doing is figuring out how to solve errors, so hopefully if you encountered
errors up to this point, you have figured out how to solve them.
I'm going to go over three different ways of debugging.
And the first one is something that you probably have been doing if you've been solving errors on your
own.
And what this is is using see out statements.
So this is kind of most primitive way of debugging.
So I have hidden a couple of errors in here and we are going to debug them.
So if you haven't noticed already, this is Project two that I'm using, so an implementation of Project
two, where we have a binary search, we read in from a file, we saw the file and we use that binary
search to search for some names in this like quote unquote database, which is really just stored in
vectors.
So we expect the program to start and ask us for a name to be searched in the database and then we,
of course, have the option to quit as well.
So if this program wants to run normally, it would have this normal exit code and it would it would
exit fine.
So but it's not going to exit fine.
In fact, it's not going to ask us for a name to be searched in the database.
So if I go ahead and run the program right now here and sea lion, I notice that it doesn't ask me anything.
And just as process finished with exit code, this big number, which is not good, that's not what
you want to see.
You want a normal, you know, exit code like zero or something.
So that is not good.
How are we going to debug this?
So the first thing we're going to do is use C out to figure out how far our program is making it, and
therefore we can narrow down where it is getting stuck and where it's crashing.
So what we can do is kind of put this out in different places, so I could be like, well, it's not
making it to here.
Why don't I just put it?
Right here, I'll just see out the word test and see if that actually prints out.
So I go here and I run it.
And I say, huh, OK, that didn't print out there, so my program is not making it to the line 90.
So I go ahead and I get rid of that and I say, OK, well, maybe I put it before the force or so I
thought maybe it was happening and sort, but I guess it's not.
So let's put it there and run it.
Oh, it's still not printing out.
OK, well, let's see, I'll just keep moving it up, then I guess I'll put it right here.
And put it here.
Oh, it actually prints out, so our program actually makes it to here, but it doesn't make it to line
eighty nine.
That means that the problem must be right here.
So I see out my Vic six, that's kind of random.
Why is that not OK?
Let's go up here.
What is my vector?
So let's see what that is.
Well, looks like we just declare my verdict, we don't do anything, we don't read into it, we read
into names that we push back to name of.
We push back to bank, but we don't put anything in my vict.
Well, this is kind of one of the areas that I had.
That was just a stupid error.
You know, I put some vector in here that's just has no size here, and I'm trying to read out of thin
air, which is a blatant access violation.
So I'm going to just get rid of this altogether.
And that will fix my hair, so, you know, so you can just move this test around in the code to see
where the problem is.
So that's one type of debugging is just trying to see where the programs crashing.
If your program is crashing, you can just use this see out statement with a little test or something,
and then you can narrow down where the program is crashing.
So I'm going to go ahead and remove this and then let's go ahead and run our program.
OK, so now this is for a name to search in the database, so let's pull up some, let's look at some
names.
So what names do I have in here?
Well, I've got Dylan.
I should be able to search that name.
So I go in here and I search Dylan.
Oh, that's weird, though, since the name Dylan does not exist in the database, but it should exist
in a database.
OK, that's a little weird.
So we're going to have to narrow this down, right?
Why does it not exist in the database?
So now we should.
Try and see kind of like what's going on and why it why it doesn't exist in the database so.
We're reading in some names, right?
And then we're sorting some names, so let's just see, first off, if our names get kind of read into
here correctly, so maybe you want to print out what's in the vector.
So I might just make some code here that I can copy and paste it.
In fact, I might just make a function where I can print out the vector.
So I'm going to put it right here.
I'm just going to make some function.
It's just void and it just says that and it just takes a vector of type
string and I'm just going to call that V and I'm actually going to pass it by a reference.
Or actually, I want fast answers, but it's by copy value.
So it'll take this Vector V and then I'll just say four and I'm going to do a range based loop here.
And I'm just going to say for high or auto, high, the sea out I and space.
And then we will just close and find.
OK, I was just kind of earning their.
First, and I can't reference.
So I can this changes to a cost reference to make it a go with the suggestion there, but it's just
a simple oops conference, everything out in the factory.
So now I have this nice little foil loop, they can print it out.
So I'm going to say, well, I read everything into the vector.
Let's go ahead and to print fact and say name that.
And let's see, let's see what it says.
Let's see if it actually reads everything into the vector, OK?
So I'm going to run this.
OK, so it looks like it's doing Dylan, Jeff and Elliott Gould Frances, so it is reading Dylan into
their.
So that looks like it should be fine, and it looks like it all at lowercase them all, I came in wanting
it to.
So what seems to be the.
Seems to be the issue.
So, you know, that makes it lower.
I don't really see what the problem is, so let's see what happens after this, because if I type Dylan
now, you know, it still says the name does not exist in the database, so.
Maybe it's something to do with the source, so I could copy this and I could put it here after this
sort.
So I'll go ahead and print it out once, before and once after the.
Maybe it's a problem with the sword, maybe the sword is doing something and that's making it not have
Dylan in that.
Maybe it's changing up the vector and then I can see if they if that is the problem that I can go to
sort of debug sort.
Oh, OK, well, that definitely shows us there's a problem, right?
It says we saw Dylan was in here originally when we printed it out before the sword, but then we did.
The sword only printed out the vector out after the sword and says, and Francis Francis, it doesn't
have Dylan or Jeff or Leopold in there.
So that's definitely a problem.
Now we know the problem is in our sort.
So you can see that we're able to use printing stuff out to help us quite a bit.
So if I head over to sort.
I can go in here and, you know, you can kind of look at this and see if you see anything that's wrong
and maybe you immediately see something that's wrong.
But what we can do is also print some stuff out like.
We expect Dylan to be in the front of the victor, right?
So.
We you think about how it's going to swap initially, you know, with our sorting algorithm, or we
could just go ahead and print it out.
I might just opt to just print it out and just see kind of what happens so.
How about after each iteration of the loop, while we're doing like these swaps and things, I just
choose to see out?
Can I just say that there are those that to say the typing is really becoming a struggle lately and
there is so.
And then I say, how about I say?
Which, in fact, is going to be an issue.
So I'll have to put this above here.
Let me talk to you this.
All right, so I can say preemptive act and I can say, what is this, uh, the string victories fee?
So I can say, in fact, we.
So they don't print out everything on that right after this vector in a space each time is going to
print that out and then it can kind of kind of help me out.
So let's just put a little comment here and say
debug.
So now we can see what's happening to the vector each time, right?
Go ahead and it'll print out there.
So here I deleted these things that we're printing out the vector, the calls to print vector.
Now I'm just going to check what happens when we're sorting it.
So it's going to print each time for us.
OK, so Vector starts out as Dylan Jeff and Leopold Frances.
Then it is Dylan, Anna and Leopold Francis, then it's Anna Leopold Francis.
OK, yeah, that's definitely a problem, huh?
So it looks like what it's doing is it's not actually swapping things correctly because it's just replacing
names like it looks like Anna was just here and it wanted to swap with Jeff.
But then it just over it.
Just put Anna here, but it didn't put Jeff over here.
And then when he wanted to swap again as we're just moving it right all the way to the left with this
sort, you know, this is an insertion sort.
So it's just like moving through this moving left as much as it can tell us in the sort of position.
And it looks like it just keeps overwriting as it goes over.
So I think that there's not something being swapped and look right here.
You know, this might have been really obvious to you, but this is accidentally commented out.
So it's not actually swapping the.
Temporary one, so we're trying to do a swap here, but it doesn't finish the swap.
It only swaps one position, so it takes.
Whatever is at this sort of position plus one and puts it in a sort of position, but it doesn't take
the Syrian position what whatever was there which just saved into Tampa doesn't put that into this sort
of position plus one.
So we go ahead and we can comment that.
And now we will go ahead and run it.
And it looks like it actually supports things in a correct manner.
So let's say if I type Dylan, it actually knows now.
The age for Dylan is 20 and that is correct.
OK, so that's kind of how you can use printing out things with the sea, our statement to help you
debug code.
So I'm actually going to delete that and now I'm going to show you two other ways that we can debug
the code.
So let's say we had the same error here and I wanted to see what was inside of the vector.
So I wanted to know, like, oh, you know, what is the what's the issue?
Why is there, you know, why is it not being sorted or something like that?
The air is not here anymore because I can't get out, but what I can do is I could actually examine
this zone right here, so I could say, All right, I want to stop when I'm here and I want to take
a look at the vector and I want to step through the code and watch as things change, watch as the vector
values move around and why, how, you know, variables change and all of that.
This is something that we can do with a debugger, and it's actually a tool that is Bill tend to see
Lion.
So I'm going to go ahead and clear this, and I'm going to show you how to use the C line to bugger,
so to use the sea lion debugger.
You first need to place some points in which you want the code to stop on so you can examine what's
happening in your code.
So if let's say that we want to stop here and kind of slowly steps through these lines and see what's
going on in our code, what I can do is click in this zone right here on the line that I want to stop
on, so I click notice like this.
This is what I'm talking about this gray zone right here.
So I click right here.
You see a red dot appear in this becomes highlighted.
This is something that we call a breakpoint, and what it means is that it's going to basically break
out of the running of the program and stop right here and let you control as you step through the program.
So now we have our break point, but how do we start this debugging process of our code?
Well, you see up here in the right, we have this button that says Run Project two and this is what
we've been clicking if we want to run the project, when we click that, it opens up the console down
here and it lets us type something right?
So you notice now this exits, finds you, by the way, it has exit code zero.
That's what we want to see.
So that's if we run our program normally.
But now we want to debug.
So if I go one to the right, here you see it, says Debug Project two and you actually notice this
little symbol is like a little bug, right?
Has little antennas and feet.
So instead of running Project two right here, I'm going to click the debug project to and it's going
to basically run my program until this line happens, right until this line gets executed and it's going
to stop there on that line.
So I click that button and you notice now this is highlighted in blue.
What this is telling us is that the program has ran up until this point.
So what happened is it ran it started here.
I mean, it went through all of this in Maine right here.
It saw this the call to sort the sort function that we made it hit.
This line 94 called sort jumped up here to the start function, started running code inside this sort
function, and it got down all the way to here where we put the little red dot right.
And now it's stopped here.
And what we can do now is we can go down here where our console would normally be.
This is the console notice that says Console here.
If I click this right here, it says Debugger and the cool thing right here is now.
It gives us a view into all of these different variables in here.
Which is pretty cool, so it says, you know, insertion item right here, which is a variable.
It tells us what that currently is when we're on this line in our program, so.
As our as we execute line by line, it's like updating the code, right, like what we're doing is we're
running the code line by line now and we can actually step through you notice insertions item has been
modified right here.
So this is the line.
That modified insertion item that is reflected right here, so when it said insertion item equals VII,
it basically, you know, put the name Dylan into this variable insertion item.
And that is why when we stop right here, we notice nothing else in between here and here.
Nothing else has happened to insert an item.
That's why we know that since it says Dylan here, that was a result of this line.
Fifty one.
We can also look at the rest of our variable.
So right now.
We're interested in is our vector of names, which is v right?
We've called it v right.
Here we go down to main.
We notice that we passed name vectors the first argument.
So if we scroll back up here, what's the first argument?
What's the first parameter?
It's this one right v and it's passed by reference.
So I'm stopped here, I can examine everything that is within the scope of this function.
And that's all this stuff right here, so we see temp agencies tonight and we see V, this is a vector
right here.
If I click this little arrow, it's going to drop down more little arrows here.
And what is actually showing us is everything that's in the vector and what position is.
So what's position zero in the vector is Dylan was position one in the vector, it's Jeff and so on
and so forth.
So it's pretty cool.
You can check out all this stuff, drop down, see what's in each thing and examine it.
And so let's go ahead and step through our program and see how things change inside this vector.
So this is where these buttons come into play.
These let you move through your code in a certain way.
This right here is step over.
This right here is step into.
We're interested in stepping over when we just want to move to the next line.
So if we want to go from this line to this line, this is a comment.
So it's not going to consider this line 50.
If we go to the next line, it's going to go to the next line that can be executed, which is line fifty
nine.
So this can go from here to here.
The step into is meant for going into a function call or something like that.
So let's say that instead of this blue highlighted line, we'll put a breakpoint on this highlighted
line or something.
If I wanted to go, if I could step over when we were on this line, it would go to here.
If I did step into which is this, it would go into the call to sort and it would bring us all the way
up here to the sort of function.
Right now, we want to go from here to here, we're not going into anything right.
We don't want to go into anything here, so we're just going to go over.
So I press over.
And actually, it looks like it jumped us right back up to there instead of going into here.
That's because this must have not been true.
I sort of position.
It's only going to jump.
It's only you go from here to here.
It's this evaluated to true, right?
Otherwise it's not going to go in the wild.
So what happens is that it goes to the end of the while loop, which is right here.
And then you notice the for loop ends on this bracket right here.
So it goes right back up to the top of the for loop so we can continue stepping.
So I step here a step here, a step here.
Let's see.
Let's try and predict if it will step in the loop.
What is sorted position sort of position is zero, so well sort of position is greater than or equal
to zero.
It's zero.
So it's not going to go in there.
So we go back to the top.
Let's go back down sort of position this one that's greater than or equal to zero.
And we sorted position is greater than insertion item.
What is insertion item?
It's Anna.
Hmm.
What is it we sort of position sort of position as one, so what's that v one?
Well, here's v here's v one.
It's Jeff.
So is Jeff greater than insertion item on certain items, and that's alphabetically less than Jeff.
So this is true, right?
One is greater than equal to zero.
And Anna, I'm sorry, Jeff is greater than Anna, right?
As far as alphabetical Lexa graphical order.
So this should go to here now.
And it does, so it goes in the loop.
So let's look how things change.
We see temp name equals v Soviet position, so let's check this out.
So a phone call right there.
So now we are going to go ahead and step through to right here, so I'm going to go down here and see
what happens to 10th names.
So we see temp name is an empty string.
But what happens when we go like this now?
Temp name is Jeff.
So pretty interesting as we step through here.
So let's continue on and see how the things inside the vector change.
So if we keep stepping over, I'm going to go ahead and drop down what's in the vector here.
So we noticed his name became just like we were just talking about.
So let's see what happens in the vector when we go over these line.
60 in line 61.
So we have sort of position equals the sort of position plus one.
So if we check it out here, sort of position is one.
So we're going to look at this one.
So we have Jeff right here, right?
And then so name because Jeff, but we're saying that now this is going to be sort of position plus
one.
So sorry, position plus one would be two.
So it would be moving Anna to here.
So let's go ahead and do that.
So after we step over, you notice we have copy Anna from here to here, and I go ahead and I'm going
to step over this one, we say we sort of position plus one equals temp name.
We've stored Jeff and temp name, so we should now be having one plus one is two.
The of two is right here and we two should become Jeff.
And we noticed that it does.
So this is pretty cool.
You can use this debugger to kind of step through your code, look at values as you step through your
code, you can do the step into when there is a function call.
And this is actually continue or resume programs, so I can show you that as well.
So let's say that we want to see where in our.
Sort right here, and maybe we want to continue the program until somewhere in our binary search right
here, so I'll say like right here this, if you like that, it's right there.
So you notice that.
After this sort here, so I'll go ahead and take this break point away down here is where we're actually
calling binary search, right?
You were in sort.
So this initial function call to sort.
So I actually what I could do is.
I could put a breakpoint right here and then I could actually show how we can step into how about that?
So I'll go ahead and remove this breakpoint, but I'm going to we're right here on this line right now,
and what I'm doing is I'm putting in breakpoint right here because what we did was we hit this sort
online 94, and that jumped us up into this sort of function where we were sorting everything.
So now we're going to go past this line 94 and to here.
So when I do continue, it'll keep running the program.
So it's actually this called resume program, so it'll resume the program and keep running until it
hits the next breakpoint that we've set.
So I'm going to go ahead and do that.
And actually, you notice it doesn't necessarily necessarily hit this right away, because first, it
needs to get this 998 seat inquiry, so you notice that this little arrow came up in the console here.
So that means we need to jump over to the console and we need to type some name to be searched.
I was going to say Anna.
And now, once I press enter, it should go past this user input line for scene and go to here and stop
here.
So I do that, and we notice now that we are down here at ninety nine.
So let's go back to the debugger, we see that little line in the debugger.
So now what I can do is I could.
Step over a couple times.
So until I get to this binary search, so I step over if query equals quit, well, what is query queries
and it's not equal to quiz.
So let's jump over this and go here to one of three.
Now we're on binary search on one, two, three, so now what we can do is use the step into command
rather than step over.
So instead of going to the next line, we're going to jump into the function call and.
That is going to bring us up here to a binary search.
So let's go ahead and do that.
So step into and it takes us to the first line inside the binary search function, and now we can step
through the binary search function.
We can continue, you know, I can put a breakpoint here on this list if in press continue and now it
would continue and tell this line got executed right here.
OK, so I think that that's probably enough to cover the basics of going through this debugger is really
powerful debugger here and sea lion, it's it's great this visual kind of the ability to drop down these
little things and examine everything.
It's just so, so good.
You can also set like watch points and things, and they have this memory of you.
GDB is something that we're going to talk about next.
You can use TDB in here, but I'm going to show you how to use it in the terminal, but there's a lot
of other stuff you can do.
I'm not going to go over everything for this, but I just wanted to kind of point out to you, there's
things so you can, like I said, you can set.
New watch like, oh, you can watch a variable and it will kind of keep it up at the top for you and
show you how that variable changes, you won't have to like, drop down and examine it.
You know, if you went into another function or something like that or it was a global variable, you
know, it can show you how it changes across the program as you're moving around, you know, or I'm
like, Yeah, so you can experiment with that setting watch points.
I think I would not like to tell you too much more about it because I think it's really important for
people to learn how to go online and look at documentation on how to use debunkers and just documentation
in general.
So if you like to know more about it, you should go online and try and find documentation from jet
brains, the people that make it seem like and try and find the specific documentation for the sea lion
debugger so you can look at that.
There's also, you know, tutorials and things.
It's really good to be able to find some stuff on your own online.
Can't just get it.
Have it all given to you.
So with that, I'm going to bail out of this.
Well, actually, I'll stop this right now, and then I'm going to head over to the terminal to show
you GDB.
You notice there is something called GDB here.
GDB steps for a new debugger.
So it's kind of related to like mini GW, you know, that kind of had to do with the new compiler collection
for Windows minimum compiler collection for Windows.
This is going to be the new debugger.
So as although it's available here and sea lion, I would like to show you how to use it in the terminal
because the main point is you've seen the sea lion debugger in here and how powerful it is.
But maybe you don't have sea lion.
Maybe you don't have access to sea lion, and maybe you're just using them in the terminal and or you're
using GPS code, which I know you can set up a debugger for s code as well, but I'd like to show you
the GDB debugger.