In the terminals, so or command prompt, so it works easier for everybody.
It's more accessible.
So right now, I am actually in a folder that just has a little test file for for this.
So I am actually going to.
Hey, you can check it out if you would like it just as test us copy.
So I just have like this really basic programming here where I have like a string says Hello world,
I print out some stuff.
And what we're going to do is just kind of step through this really basic program, and I'm just going
to show you a few things on basic commands and GDB so you can start debugging that as well.
And I might go ahead and change something in here and try and see if I can show you like an error that
you might encounter, just like previously.
So we we're out of here.
And so you notice this isn't compiled yet.
We just have this test does copy here.
We don't have an executable in here yet.
So I'm going to go ahead and compile a way to compile it.
Because if you want to debug, what can you debug?
Are you going to have to change the command for G Plus plus?
We're going to just need to add something.
So we're going do G Plus Plus and rather than just saying test, copy and dash.
So we're actually going to add another kind of similar flag like Dash.
Oh, it's a flag, right?
It's a a thing that says we want to specify an output file.
We're not actually going to add a dash and G.
And what this stands for is to include something called a debug symbols.
And that just makes it so we can actually kind of look at stuff inside of our program when we are using
the new debugger.
So we're going to do G Plus plus just gasbag test CP Dash O Test stuff out here right now, right?
So went ahead and compiled that now if we do it, their command, we can see that there is a test study
here.
So what do we do now?
Well, to invoke GDB?
What we're going to do is just tap GDP and then do the executable, so test not exceed.
So this brings up the GDP debugger here, so says the new GDP.
Here's the version number, you know, talks a little bit about the licensing and stuff like that.
It says this GDB was configured as many GW 30 to 32 bit.
And here it has the website.
If you would like to go check it out and this is for reporting bugs, but you shouldn't be too concerned
about that right now.
So how do we start using this?
Well, it's just like the other debugger and we go ahead and maximize this now and.
I think that hopefully this will be big enough for you to.
To see everything.
Wondering if I need to increase the.
The size, I think it will be OK.
So let's go ahead and you know what, I.
Let me see.
Properties.
I'm actually going to try and increase the status a little bit about that.
I think that's a little bit better.
So we need to set some break points, right?
So think back to the debugger we just looked at.
We were setting some break points in there by clicking around the side and making a little red dot.
This is a terminal based debugger, so it's not going to have that nice graphical nature where we can
click on little drop down arrows and examine things.
It's all going to be based in the command line with little like text commands.
So what we're going to do to set a break point is you can say you can type out break or you can just
say be for a shortcut.
And I'm going to say I can actually just provide a breakpoint at a function.
So I know there's a main function, right?
So I can actually just say break main and it will set a breakpoint at main.
What it says right here, though, is just a hex address.
You notice this is hexadecimal right.
So what it's really doing is is just setting a breakpoint at a specific memory address, you know?
So we talked a little bit about sections of memory, right?
There's like the the text segment.
We have our code in memory and also, you know, we have the stack and all of that stuff.
So this is just to address a memory where it's setting this breakpoint and it's going to stop there
that address.
But it just so happens to be main because we said, break me.
So what we can do now is just type R to run the program and it will stop at our breakpoint.
So you notice that says Breakpoint, one main at Tostart CP Line six.
So this call six means Line six and you notice it shows us the line number and then it also says it's
string as equals.
Hello, world.
So that was really the first line in our program.
So if we want to kind of see more information about where we are, what zone we are in, we can actually
type list and press enter and that will show us a little bit more.
So you see the line numbers here.
So Line six is where we're at, right?
That's where we start.
And then it shows this kind of a little bit of what comes after that.
Right here on line seven, eight, nine and 10.
So to move through the program like a step over command, what we're actually going to do is say next,
or you can just type in.
Sellers can type in and that will go to the next line.
You notice we went from line six to line seven.
We're now so see out s if I go.
So when I'm on this line that says See out, outguess.
It hasn't yet executed this line.
It has stopped right before executing it.
So another interesting thing that I can do is if I press enter, it will run the last command and I
ran, which was in right, which was next.
So I don't have to type in again.
I can just press enter and it will do the same command that I did last.
And you'll notice that when I do next, it will then print out as it will then print out this line because
it hasn't executed it.
Like I said, it stopped right before execution.
So when I press the next, it will execute Line seven and then go to the next line.
So you see that at what it did.
Is it printed out, right?
Printing it to the console console out, as is Hello world.
So that's why we see Hello World print here before it now telling us that we've moved from line seven
to line eight.
Right.
So another interesting thing, if you want to examine something that's in the program, you can actually
say use the Pekerman for print.
You can say print as well, but you can shortcut it with P.
And so what if I want to say P and then s?
So it tells me, like this huge thing?
It's like, Oh, this is like some huge, huge string, you know, it's like this allocator you notice
here it says, Hello, world here.
But it's going to show you like a lot of stuff to do with the object in memory, so no strings.
I don't know if I mentioned that much, but strings are objects, something that we might have not talked
about yet in the course.
But since it's an object, it's something that a little more complicated than just a basic data type
is going to show all kinds of stuff here.
But you can also see the Helloworld world right here, so you can try printing out different things.
You might be able to print out something like this, you know, like s one, it says that it can't find
this operator.
So there's some things you can do, some things you can't do in GDP.
You can kind of just experiment to see what you can do.
We can also print out something else if I go to the next line.
I have this into a equals four plus seven.
So let me go to the next line, and it looks like it actually went and printed out this, either there,
in fact.
So I said print as sub one, it's taken off by an operator.
But then I went to next and it actually printed out the E and Hello world.
So if I go next again.
No, I'm on line 10, so a has been set, so what I could do is they print a and it actually shows you
that it's 11, right?
Seven plus four is 11.
So I'm able to print out a is a primitive type.
So it didn't show like all of this information here, that it had with the string.
So pretty cool if you want to step into something, I don't really have a function call here, but if
you want to step into something rather than standing in for next, it's actually s first step.
And you can, I think you can just say step as well, but it's easier to just put it in.
So if you want to step into a function, call it just like we did in the other debugger.
You can use the S command once you're on that line.
So if you have stopped on a line as a function call, this is not a function call, of course.
But if you are out of line, that's a function call.
You could just type as to step into that function.
So pretty cool.
You know, there's a lot more you can do in GDB, you can actually set watch points, just like I said
in the other debugger and.
You know, you can reference things like pointers and all that, so it's really powerful, and if you
would like to know more about it, just like I said with the other debugger and sea lion, I would go
ahead and look up some documentation on GDP.
Just scour the internet to look for some things.
It's good practice because most of the time in your program, you're going to need to look stuff up,
right?
If you don't know how to do something, you'll have to look at the syntax, look up the command, et
cetera.
So I think it would be a good idea for you to look more into the GDP documentation to see how you can
do some other things.
I'm going to show you, or at least try to show you one more thing in GDP.
So one one kind of intermediate thing is if you want to quit out and grab you, press Q and then it
asks you if you're sure and you'll say Yes, OK, I've jumped over to bash to show you one last thing
to do with that has to do with GDP.
So this is basically on how to debug a segmentation fault.
So I was just doing it in the command prompt, but.
I was actually having a hard time creating segmentation fault, believe it or not, some of the compilers
kind of handle things a certain way, so it's kind of sometimes hard to guarantee a segmentation fault.
So what do we have here is a modified the test CPA file a little bit.
You notice here I create a pointer to be and pointer.
Important to tap into and I make it zero, so it's basically a null pointer, and then here I try and
reference a null pointer, which is a problem.
So that will lead to a segmentation fault.
So if we go ahead and clear this, I have already compiled it with debug symbols.
So I ran a command that was like this to compile it and then I'm going to run.
It just called out and you notice it says segmentation fault.
So segmentation fault.
You probably already encountered some honestly yourself, but they're kind of one of the most annoying
things because if it just if you run your program and it just says cauda, you know, horse segmentation,
fault or whatever bad access, something like that.
It's pretty hard, it seems pretty ambiguous for you to figure out like what's going on.
Of course, you can go that primitive method and just put a bunch of see out statements to see how far
your program actually makes it when it runs before it crashes.
But there is a kind of more straightforward way of solving these problems, and that is using GDP.
So we'll use GDP again with this eight out sound of GDP eight, out nine and bash.
So I'll go ahead and run this.
And then what I'm going to do is just straight up press run our four run.
And you notice it says program received Signal Sigue SAG, which is a segmentation fault that says here
and it says where it actually received the signal is said B equals seven.
You're like, huh?
OK.
You know, that's interesting, I wonder what's going on with this of tell you the line, this problem,
and then you can kind of go back and look at the code and see where B is coming from.
You know, what is B?
Why is it not OK to D reference B and have it be seven?
You also can use a need function called backtrace, which you can actually type out the word backtrace,
or you can just put b t and that will show you where it came from.
But all we came from as far as a function call was made.
So we don't really have functions to back trace into, but we could make an example of that.
So let's go ahead and let's go ahead and see.
See what happens when we only do that, so I'm going to go ahead and go into here.
I can make another function.
We also can change this to, you know, explicitly.
So I'll just put some other function here.
Just call try something like that and I'll just say,
see out.
Well, let's actually just put this stuff in here, so I'll say.
We made this point, right?
There's no point here, so let's go ahead and make it into winter here, so to say endpoint.
And he and we'll just say people seven and do that, so I'll get rid of this line and then down here,
I'll just say that we call my.
So it's hard to say so.
Just save my funk and we'll pass, be here.
And then, yeah, let's just let's just save that, so I'm going to clear this and then.
We'll go ahead and run this command again to compile it.
And then we can do the TV out, or actually let's go ahead and just run again, just to be sure that
it safe.
So now it prints Hello World any, and then it's also like, Oh gosh, where's the snowfall coming from?
So we can run TV out and we will run it again, and it will segue fault here.
And so shows us what the signal is on.
But we're kind of like, Oh, why is this a problem?
You know what if we list like, Oh, here's our function here, and it just so happens that we can see
what he was set to right now because the list just happened to get this last line in here.
But you might not have seen this if it was far further down in the code.
Maybe you wouldn't see and be in star vehicles, you know?
And so what you could do is just say backtrace.
So we've backtrace here and it shows us that we came from Maine and it just tells us exactly where we
came from in the main function that got us to where we were here.
So the call from my phone specifically, so it says where we last came from was in Maine, the main
function at Test IACP and 15.
So if I actually list right here, I mean, this is not something that you might normally do to see
this line, but I'm just going to list because it's right there.
But you can see blatantly why I'm here on Line 15 that I closed my phone and that was the call that
led us to jump into my thumb.
So you can actually, you know, back Trace is tracing back in the code to see where you came from,
so you can figure out what the problem was.
And of course, you know, we have this null pointer as got passed here and went up to there.
But then that kind of helps you to, you know, if you know where you came from, you can just trace
back and you can keep kind of running it and checking it again and testing out the values and seeing
what the problem is.
All right, so that's actually all I really have for debugging, so hopefully this was enough to get
you going and help you get through some of the errors that you might have had.
I felt like this was an important video to show because it's not immediately obvious how to debug things
from now on, and you have a couple of tools to be able to debug.
And of course, there are others, you know, I'm not going to show debug your have all the IDs, but
you might be using another idea that has a nice built-in debugger.
So of course, you know, I would implore you to look at the documentation for that specific debugger
that your specific ID that you're using.
All right, cool.
So with that, I'll see you in the next lecture.