1
00:00:01,670 --> 00:00:07,640
In the terminals, so or command prompt, so it works easier for everybody.

2
00:00:07,700 --> 00:00:08,600
It's more accessible.

3
00:00:09,380 --> 00:00:17,990
So right now, I am actually in a folder that just has a little test file for for this.

4
00:00:18,530 --> 00:00:20,900
So I am actually going to.

5
00:00:23,290 --> 00:00:27,520
Hey, you can check it out if you would like it just as test us copy.

6
00:00:28,490 --> 00:00:34,070
So I just have like this really basic programming here where I have like a string says Hello world,

7
00:00:34,070 --> 00:00:35,300
I print out some stuff.

8
00:00:35,690 --> 00:00:40,820
And what we're going to do is just kind of step through this really basic program, and I'm just going

9
00:00:40,820 --> 00:00:47,870
to show you a few things on basic commands and GDB so you can start debugging that as well.

10
00:00:49,280 --> 00:00:55,190
And I might go ahead and change something in here and try and see if I can show you like an error that

11
00:00:55,190 --> 00:00:57,500
you might encounter, just like previously.

12
00:00:58,580 --> 00:01:01,100
So we we're out of here.

13
00:01:01,700 --> 00:01:04,680
And so you notice this isn't compiled yet.

14
00:01:04,700 --> 00:01:09,110
We just have this test does copy here.

15
00:01:09,740 --> 00:01:11,840
We don't have an executable in here yet.

16
00:01:11,840 --> 00:01:14,930
So I'm going to go ahead and compile a way to compile it.

17
00:01:14,930 --> 00:01:18,320
Because if you want to debug, what can you debug?

18
00:01:18,320 --> 00:01:21,980
Are you going to have to change the command for G Plus plus?

19
00:01:22,430 --> 00:01:23,810
We're going to just need to add something.

20
00:01:23,810 --> 00:01:29,700
So we're going do G Plus Plus and rather than just saying test, copy and dash.

21
00:01:29,780 --> 00:01:36,730
So we're actually going to add another kind of similar flag like Dash.

22
00:01:36,740 --> 00:01:38,120
Oh, it's a flag, right?

23
00:01:38,150 --> 00:01:41,750
It's a a thing that says we want to specify an output file.

24
00:01:41,750 --> 00:01:43,730
We're not actually going to add a dash and G.

25
00:01:43,730 --> 00:01:48,860
And what this stands for is to include something called a debug symbols.

26
00:01:49,220 --> 00:01:56,900
And that just makes it so we can actually kind of look at stuff inside of our program when we are using

27
00:01:56,900 --> 00:01:57,930
the new debugger.

28
00:01:57,950 --> 00:02:08,630
So we're going to do G Plus plus just gasbag test CP Dash O Test stuff out here right now, right?

29
00:02:11,060 --> 00:02:18,050
So went ahead and compiled that now if we do it, their command, we can see that there is a test study

30
00:02:18,470 --> 00:02:18,800
here.

31
00:02:20,740 --> 00:02:22,360
So what do we do now?

32
00:02:22,370 --> 00:02:24,430
Well, to invoke GDB?

33
00:02:24,460 --> 00:02:32,480
What we're going to do is just tap GDP and then do the executable, so test not exceed.

34
00:02:34,910 --> 00:02:41,050
So this brings up the GDP debugger here, so says the new GDP.

35
00:02:41,390 --> 00:02:48,180
Here's the version number, you know, talks a little bit about the licensing and stuff like that.

36
00:02:48,200 --> 00:02:54,530
It says this GDB was configured as many GW 30 to 32 bit.

37
00:02:54,920 --> 00:02:58,250
And here it has the website.

38
00:02:58,250 --> 00:03:05,030
If you would like to go check it out and this is for reporting bugs, but you shouldn't be too concerned

39
00:03:05,030 --> 00:03:05,900
about that right now.

40
00:03:06,260 --> 00:03:07,610
So how do we start using this?

41
00:03:07,640 --> 00:03:13,970
Well, it's just like the other debugger and we go ahead and maximize this now and.

42
00:03:15,470 --> 00:03:18,770
I think that hopefully this will be big enough for you to.

43
00:03:19,810 --> 00:03:21,130
To see everything.

44
00:03:21,940 --> 00:03:24,640
Wondering if I need to increase the.

45
00:03:25,970 --> 00:03:27,890
The size, I think it will be OK.

46
00:03:30,410 --> 00:03:35,260
So let's go ahead and you know what, I.

47
00:03:35,390 --> 00:03:36,320
Let me see.

48
00:03:37,870 --> 00:03:39,040
Properties.

49
00:03:40,520 --> 00:03:44,570
I'm actually going to try and increase the status a little bit about that.

50
00:03:45,350 --> 00:03:46,580
I think that's a little bit better.

51
00:03:47,510 --> 00:03:50,070
So we need to set some break points, right?

52
00:03:50,090 --> 00:03:52,250
So think back to the debugger we just looked at.

53
00:03:52,700 --> 00:03:57,470
We were setting some break points in there by clicking around the side and making a little red dot.

54
00:03:57,800 --> 00:04:04,190
This is a terminal based debugger, so it's not going to have that nice graphical nature where we can

55
00:04:04,190 --> 00:04:07,430
click on little drop down arrows and examine things.

56
00:04:07,790 --> 00:04:12,110
It's all going to be based in the command line with little like text commands.

57
00:04:12,590 --> 00:04:19,970
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

58
00:04:19,970 --> 00:04:22,370
say be for a shortcut.

59
00:04:22,910 --> 00:04:26,990
And I'm going to say I can actually just provide a breakpoint at a function.

60
00:04:26,990 --> 00:04:28,730
So I know there's a main function, right?

61
00:04:28,730 --> 00:04:34,820
So I can actually just say break main and it will set a breakpoint at main.

62
00:04:34,820 --> 00:04:38,450
What it says right here, though, is just a hex address.

63
00:04:38,450 --> 00:04:40,210
You notice this is hexadecimal right.

64
00:04:40,970 --> 00:04:46,880
So what it's really doing is is just setting a breakpoint at a specific memory address, you know?

65
00:04:46,940 --> 00:04:51,120
So we talked a little bit about sections of memory, right?

66
00:04:51,140 --> 00:04:53,120
There's like the the text segment.

67
00:04:53,420 --> 00:04:57,770
We have our code in memory and also, you know, we have the stack and all of that stuff.

68
00:04:58,160 --> 00:05:02,990
So this is just to address a memory where it's setting this breakpoint and it's going to stop there

69
00:05:02,990 --> 00:05:03,680
that address.

70
00:05:03,680 --> 00:05:06,160
But it just so happens to be main because we said, break me.

71
00:05:07,220 --> 00:05:13,190
So what we can do now is just type R to run the program and it will stop at our breakpoint.

72
00:05:15,060 --> 00:05:20,580
So you notice that says Breakpoint, one main at Tostart CP Line six.

73
00:05:21,000 --> 00:05:27,990
So this call six means Line six and you notice it shows us the line number and then it also says it's

74
00:05:27,990 --> 00:05:29,070
string as equals.

75
00:05:29,070 --> 00:05:29,620
Hello, world.

76
00:05:29,640 --> 00:05:33,390
So that was really the first line in our program.

77
00:05:34,380 --> 00:05:40,770
So if we want to kind of see more information about where we are, what zone we are in, we can actually

78
00:05:40,800 --> 00:05:46,290
type list and press enter and that will show us a little bit more.

79
00:05:46,300 --> 00:05:48,300
So you see the line numbers here.

80
00:05:48,330 --> 00:05:50,370
So Line six is where we're at, right?

81
00:05:50,760 --> 00:05:51,810
That's where we start.

82
00:05:52,530 --> 00:05:55,560
And then it shows this kind of a little bit of what comes after that.

83
00:05:56,640 --> 00:05:59,220
Right here on line seven, eight, nine and 10.

84
00:06:00,300 --> 00:06:07,440
So to move through the program like a step over command, what we're actually going to do is say next,

85
00:06:08,400 --> 00:06:09,900
or you can just type in.

86
00:06:10,310 --> 00:06:13,440
Sellers can type in and that will go to the next line.

87
00:06:13,440 --> 00:06:15,570
You notice we went from line six to line seven.

88
00:06:15,570 --> 00:06:19,260
We're now so see out s if I go.

89
00:06:19,380 --> 00:06:23,130
So when I'm on this line that says See out, outguess.

90
00:06:24,280 --> 00:06:26,950
It hasn't yet executed this line.

91
00:06:27,340 --> 00:06:29,900
It has stopped right before executing it.

92
00:06:29,920 --> 00:06:37,930
So another interesting thing that I can do is if I press enter, it will run the last command and I

93
00:06:37,930 --> 00:06:39,850
ran, which was in right, which was next.

94
00:06:39,860 --> 00:06:41,560
So I don't have to type in again.

95
00:06:41,560 --> 00:06:45,400
I can just press enter and it will do the same command that I did last.

96
00:06:45,790 --> 00:06:53,440
And you'll notice that when I do next, it will then print out as it will then print out this line because

97
00:06:53,440 --> 00:06:54,610
it hasn't executed it.

98
00:06:54,610 --> 00:06:56,950
Like I said, it stopped right before execution.

99
00:06:56,950 --> 00:07:01,390
So when I press the next, it will execute Line seven and then go to the next line.

100
00:07:02,530 --> 00:07:03,910
So you see that at what it did.

101
00:07:03,910 --> 00:07:05,480
Is it printed out, right?

102
00:07:06,290 --> 00:07:09,660
Printing it to the console console out, as is Hello world.

103
00:07:09,670 --> 00:07:14,980
So that's why we see Hello World print here before it now telling us that we've moved from line seven

104
00:07:14,980 --> 00:07:15,790
to line eight.

105
00:07:17,260 --> 00:07:17,560
Right.

106
00:07:18,220 --> 00:07:24,340
So another interesting thing, if you want to examine something that's in the program, you can actually

107
00:07:24,670 --> 00:07:27,780
say use the Pekerman for print.

108
00:07:27,790 --> 00:07:30,700
You can say print as well, but you can shortcut it with P.

109
00:07:31,450 --> 00:07:35,140
And so what if I want to say P and then s?

110
00:07:35,920 --> 00:07:38,500
So it tells me, like this huge thing?

111
00:07:38,500 --> 00:07:45,160
It's like, Oh, this is like some huge, huge string, you know, it's like this allocator you notice

112
00:07:45,160 --> 00:07:47,050
here it says, Hello, world here.

113
00:07:48,870 --> 00:07:55,320
But it's going to show you like a lot of stuff to do with the object in memory, so no strings.

114
00:07:55,620 --> 00:08:02,160
I don't know if I mentioned that much, but strings are objects, something that we might have not talked

115
00:08:02,160 --> 00:08:03,150
about yet in the course.

116
00:08:03,630 --> 00:08:08,250
But since it's an object, it's something that a little more complicated than just a basic data type

117
00:08:08,250 --> 00:08:09,810
is going to show all kinds of stuff here.

118
00:08:09,810 --> 00:08:16,350
But you can also see the Helloworld world right here, so you can try printing out different things.

119
00:08:16,770 --> 00:08:23,310
You might be able to print out something like this, you know, like s one, it says that it can't find

120
00:08:23,310 --> 00:08:24,150
this operator.

121
00:08:24,150 --> 00:08:28,710
So there's some things you can do, some things you can't do in GDP.

122
00:08:28,950 --> 00:08:31,950
You can kind of just experiment to see what you can do.

123
00:08:31,980 --> 00:08:34,770
We can also print out something else if I go to the next line.

124
00:08:35,040 --> 00:08:38,730
I have this into a equals four plus seven.

125
00:08:39,270 --> 00:08:46,890
So let me go to the next line, and it looks like it actually went and printed out this, either there,

126
00:08:46,900 --> 00:08:47,430
in fact.

127
00:08:48,270 --> 00:08:52,320
So I said print as sub one, it's taken off by an operator.

128
00:08:52,320 --> 00:08:55,410
But then I went to next and it actually printed out the E and Hello world.

129
00:08:55,410 --> 00:08:58,260
So if I go next again.

130
00:08:59,880 --> 00:09:06,480
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

131
00:09:07,290 --> 00:09:08,470
that it's 11, right?

132
00:09:08,490 --> 00:09:10,390
Seven plus four is 11.

133
00:09:10,980 --> 00:09:14,400
So I'm able to print out a is a primitive type.

134
00:09:14,400 --> 00:09:19,560
So it didn't show like all of this information here, that it had with the string.

135
00:09:21,440 --> 00:09:28,400
So pretty cool if you want to step into something, I don't really have a function call here, but if

136
00:09:28,400 --> 00:09:34,440
you want to step into something rather than standing in for next, it's actually s first step.

137
00:09:34,490 --> 00:09:38,960
And you can, I think you can just say step as well, but it's easier to just put it in.

138
00:09:39,150 --> 00:09:43,400
So if you want to step into a function, call it just like we did in the other debugger.

139
00:09:43,670 --> 00:09:48,380
You can use the S command once you're on that line.

140
00:09:48,380 --> 00:09:52,790
So if you have stopped on a line as a function call, this is not a function call, of course.

141
00:09:53,150 --> 00:09:55,320
But if you are out of line, that's a function call.

142
00:09:55,340 --> 00:09:58,700
You could just type as to step into that function.

143
00:10:01,150 --> 00:10:02,800
So pretty cool.

144
00:10:03,340 --> 00:10:08,650
You know, there's a lot more you can do in GDB, you can actually set watch points, just like I said

145
00:10:08,650 --> 00:10:10,420
in the other debugger and.

146
00:10:11,960 --> 00:10:20,570
You know, you can reference things like pointers and all that, so it's really powerful, and if you

147
00:10:20,570 --> 00:10:26,540
would like to know more about it, just like I said with the other debugger and sea lion, I would go

148
00:10:26,540 --> 00:10:29,510
ahead and look up some documentation on GDP.

149
00:10:30,110 --> 00:10:32,780
Just scour the internet to look for some things.

150
00:10:32,780 --> 00:10:37,570
It's good practice because most of the time in your program, you're going to need to look stuff up,

151
00:10:37,580 --> 00:10:37,880
right?

152
00:10:38,480 --> 00:10:42,680
If you don't know how to do something, you'll have to look at the syntax, look up the command, et

153
00:10:42,680 --> 00:10:43,130
cetera.

154
00:10:43,850 --> 00:10:49,490
So I think it would be a good idea for you to look more into the GDP documentation to see how you can

155
00:10:49,490 --> 00:10:50,620
do some other things.

156
00:10:50,630 --> 00:10:54,680
I'm going to show you, or at least try to show you one more thing in GDP.

157
00:10:54,690 --> 00:11:01,370
So one one kind of intermediate thing is if you want to quit out and grab you, press Q and then it

158
00:11:01,370 --> 00:11:11,690
asks you if you're sure and you'll say Yes, OK, I've jumped over to bash to show you one last thing

159
00:11:11,960 --> 00:11:14,960
to do with that has to do with GDP.

160
00:11:15,470 --> 00:11:20,090
So this is basically on how to debug a segmentation fault.

161
00:11:20,570 --> 00:11:24,800
So I was just doing it in the command prompt, but.

162
00:11:25,890 --> 00:11:31,770
I was actually having a hard time creating segmentation fault, believe it or not, some of the compilers

163
00:11:32,910 --> 00:11:38,670
kind of handle things a certain way, so it's kind of sometimes hard to guarantee a segmentation fault.

164
00:11:40,770 --> 00:11:45,900
So what do we have here is a modified the test CPA file a little bit.

165
00:11:45,900 --> 00:11:51,480
You notice here I create a pointer to be and pointer.

166
00:11:53,140 --> 00:11:58,820
Important to tap into and I make it zero, so it's basically a null pointer, and then here I try and

167
00:11:59,060 --> 00:12:04,480
reference a null pointer, which is a problem.

168
00:12:04,630 --> 00:12:09,910
So that will lead to a segmentation fault.

169
00:12:10,210 --> 00:12:17,470
So if we go ahead and clear this, I have already compiled it with debug symbols.

170
00:12:17,470 --> 00:12:22,390
So I ran a command that was like this to compile it and then I'm going to run.

171
00:12:22,390 --> 00:12:27,230
It just called out and you notice it says segmentation fault.

172
00:12:27,280 --> 00:12:29,350
So segmentation fault.

173
00:12:29,590 --> 00:12:35,200
You probably already encountered some honestly yourself, but they're kind of one of the most annoying

174
00:12:35,200 --> 00:12:41,890
things because if it just if you run your program and it just says cauda, you know, horse segmentation,

175
00:12:41,890 --> 00:12:44,440
fault or whatever bad access, something like that.

176
00:12:46,010 --> 00:12:50,060
It's pretty hard, it seems pretty ambiguous for you to figure out like what's going on.

177
00:12:50,540 --> 00:12:55,970
Of course, you can go that primitive method and just put a bunch of see out statements to see how far

178
00:12:55,970 --> 00:13:00,380
your program actually makes it when it runs before it crashes.

179
00:13:01,250 --> 00:13:09,260
But there is a kind of more straightforward way of solving these problems, and that is using GDP.

180
00:13:09,530 --> 00:13:18,110
So we'll use GDP again with this eight out sound of GDP eight, out nine and bash.

181
00:13:18,680 --> 00:13:21,380
So I'll go ahead and run this.

182
00:13:21,530 --> 00:13:25,580
And then what I'm going to do is just straight up press run our four run.

183
00:13:26,980 --> 00:13:34,120
And you notice it says program received Signal Sigue SAG, which is a segmentation fault that says here

184
00:13:34,660 --> 00:13:39,760
and it says where it actually received the signal is said B equals seven.

185
00:13:41,560 --> 00:13:42,300
You're like, huh?

186
00:13:42,900 --> 00:13:43,470
OK.

187
00:13:43,920 --> 00:13:49,020
You know, that's interesting, I wonder what's going on with this of tell you the line, this problem,

188
00:13:49,020 --> 00:13:55,290
and then you can kind of go back and look at the code and see where B is coming from.

189
00:13:55,290 --> 00:13:56,220
You know, what is B?

190
00:13:56,220 --> 00:14:00,840
Why is it not OK to D reference B and have it be seven?

191
00:14:01,830 --> 00:14:07,470
You also can use a need function called backtrace, which you can actually type out the word backtrace,

192
00:14:07,470 --> 00:14:12,510
or you can just put b t and that will show you where it came from.

193
00:14:12,510 --> 00:14:16,080
But all we came from as far as a function call was made.

194
00:14:16,380 --> 00:14:22,950
So we don't really have functions to back trace into, but we could make an example of that.

195
00:14:22,950 --> 00:14:29,190
So let's go ahead and let's go ahead and see.

196
00:14:30,440 --> 00:14:36,740
See what happens when we only do that, so I'm going to go ahead and go into here.

197
00:14:37,790 --> 00:14:39,220
I can make another function.

198
00:14:39,230 --> 00:14:42,890
We also can change this to, you know, explicitly.

199
00:14:45,590 --> 00:14:47,630
So I'll just put some other function here.

200
00:14:47,630 --> 00:14:55,730
Just call try something like that and I'll just say,

201
00:14:58,820 --> 00:14:59,690
see out.

202
00:15:02,250 --> 00:15:05,490
Well, let's actually just put this stuff in here, so I'll say.

203
00:15:10,110 --> 00:15:11,900
We made this point, right?

204
00:15:11,920 --> 00:15:18,790
There's no point here, so let's go ahead and make it into winter here, so to say endpoint.

205
00:15:22,080 --> 00:15:37,710
And he and we'll just say people seven and do that, so I'll get rid of this line and then down here,

206
00:15:37,710 --> 00:15:40,680
I'll just say that we call my.

207
00:15:43,390 --> 00:15:47,290
So it's hard to say so.

208
00:15:48,680 --> 00:15:54,050
Just save my funk and we'll pass, be here.

209
00:15:56,940 --> 00:16:03,960
And then, yeah, let's just let's just save that, so I'm going to clear this and then.

210
00:16:05,460 --> 00:16:08,220
We'll go ahead and run this command again to compile it.

211
00:16:09,940 --> 00:16:17,710
And then we can do the TV out, or actually let's go ahead and just run again, just to be sure that

212
00:16:17,710 --> 00:16:18,700
it safe.

213
00:16:18,850 --> 00:16:24,340
So now it prints Hello World any, and then it's also like, Oh gosh, where's the snowfall coming from?

214
00:16:24,340 --> 00:16:31,240
So we can run TV out and we will run it again, and it will segue fault here.

215
00:16:31,820 --> 00:16:34,240
And so shows us what the signal is on.

216
00:16:34,240 --> 00:16:37,030
But we're kind of like, Oh, why is this a problem?

217
00:16:37,030 --> 00:16:43,810
You know what if we list like, Oh, here's our function here, and it just so happens that we can see

218
00:16:43,810 --> 00:16:49,000
what he was set to right now because the list just happened to get this last line in here.

219
00:16:49,300 --> 00:16:52,900
But you might not have seen this if it was far further down in the code.

220
00:16:53,260 --> 00:16:56,920
Maybe you wouldn't see and be in star vehicles, you know?

221
00:16:57,100 --> 00:17:00,490
And so what you could do is just say backtrace.

222
00:17:01,480 --> 00:17:10,330
So we've backtrace here and it shows us that we came from Maine and it just tells us exactly where we

223
00:17:10,330 --> 00:17:15,040
came from in the main function that got us to where we were here.

224
00:17:15,520 --> 00:17:22,120
So the call from my phone specifically, so it says where we last came from was in Maine, the main

225
00:17:22,120 --> 00:17:26,590
function at Test IACP and 15.

226
00:17:27,910 --> 00:17:33,400
So if I actually list right here, I mean, this is not something that you might normally do to see

227
00:17:33,400 --> 00:17:35,830
this line, but I'm just going to list because it's right there.

228
00:17:35,830 --> 00:17:43,060
But you can see blatantly why I'm here on Line 15 that I closed my phone and that was the call that

229
00:17:43,060 --> 00:17:44,560
led us to jump into my thumb.

230
00:17:44,560 --> 00:17:50,970
So you can actually, you know, back Trace is tracing back in the code to see where you came from,

231
00:17:50,980 --> 00:17:52,770
so you can figure out what the problem was.

232
00:17:52,780 --> 00:17:56,350
And of course, you know, we have this null pointer as got passed here and went up to there.

233
00:17:57,520 --> 00:18:02,590
But then that kind of helps you to, you know, if you know where you came from, you can just trace

234
00:18:02,590 --> 00:18:07,300
back and you can keep kind of running it and checking it again and testing out the values and seeing

235
00:18:07,300 --> 00:18:08,200
what the problem is.

236
00:18:10,280 --> 00:18:16,730
All right, so that's actually all I really have for debugging, so hopefully this was enough to get

237
00:18:16,730 --> 00:18:22,310
you going and help you get through some of the errors that you might have had.

238
00:18:22,670 --> 00:18:28,730
I felt like this was an important video to show because it's not immediately obvious how to debug things

239
00:18:30,230 --> 00:18:33,350
from now on, and you have a couple of tools to be able to debug.

240
00:18:33,620 --> 00:18:38,000
And of course, there are others, you know, I'm not going to show debug your have all the IDs, but

241
00:18:38,000 --> 00:18:41,170
you might be using another idea that has a nice built-in debugger.

242
00:18:41,180 --> 00:18:47,060
So of course, you know, I would implore you to look at the documentation for that specific debugger

243
00:18:47,060 --> 00:18:49,790
that your specific ID that you're using.

244
00:18:51,260 --> 00:18:51,980
All right, cool.

245
00:18:52,010 --> 00:18:54,200
So with that, I'll see you in the next lecture.
