WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:01.440 SPEAKER 1: Welcome, everyone. 00:00:01.440 --> 00:00:04.310 My name is Carter, and today I'll show you my secret program. 00:00:04.310 --> 00:00:07.410 You can run the program by typing ./secret into your terminal, 00:00:07.410 --> 00:00:10.760 and if you type in the right password, you should be able to see come on in. 00:00:10.760 --> 00:00:11.880 So let's try it out. 00:00:11.880 --> 00:00:16.940 I'll do ./secret, and maybe I don't know the secret phrase, so I'll just say, 00:00:16.940 --> 00:00:18.765 let me in. 00:00:18.765 --> 00:00:20.640 And at which point, I don't get any response. 00:00:20.640 --> 00:00:23.270 So let me try it again and do ./secret. 00:00:23.270 --> 00:00:26.490 Maybe password is the password here. 00:00:26.490 --> 00:00:27.350 But it's not. 00:00:27.350 --> 00:00:29.940 And I'll just reveal to you that the password is please. 00:00:29.940 --> 00:00:35.170 So if I type ./secret, and I type in please, I should see come on in. 00:00:35.170 --> 00:00:36.940 But I don't. 00:00:36.940 --> 00:00:38.950 And so what's the problem here? 00:00:38.950 --> 00:00:42.490 Well, maybe I should look into my code, and maybe step through it bit by bit. 00:00:42.490 --> 00:00:48.060 So I'll do code secret.c, and I can see the very top of my code, 00:00:48.060 --> 00:00:50.490 starting with this int main(void) function. 00:00:50.490 --> 00:00:52.710 To step through my code, remember we can use debug50 00:00:52.710 --> 00:00:55.350 to set a breakpoint, or a place to pause in our code. 00:00:55.350 --> 00:01:00.510 So I can go to this left side here, find a pale red circle, 00:01:00.510 --> 00:01:04.500 and turn it into a bright red circle as I click it, and set that pause point, 00:01:04.500 --> 00:01:06.460 or that breakpoint. 00:01:06.460 --> 00:01:11.688 Now I can do debug50 of secret, and then my code should boot up, 00:01:11.688 --> 00:01:14.230 and I should be able to pause right at that moment I'm asking 00:01:14.230 --> 00:01:17.240 the user for their secret phrase. 00:01:17.240 --> 00:01:19.840 Of course, I have to actually step over that line for it 00:01:19.840 --> 00:01:23.160 to actually run my program, so I'll then step over, 00:01:23.160 --> 00:01:26.530 and I can type in my secret phrase of please. 00:01:26.530 --> 00:01:27.780 OK, so that seems pretty good. 00:01:27.780 --> 00:01:30.840 I see that phrase becomes please, and correct is currently false, 00:01:30.840 --> 00:01:34.510 but it should change to true after I do this check phrase function. 00:01:34.510 --> 00:01:38.820 So I'll step over check phrase, and I see it's actually still false. 00:01:38.820 --> 00:01:43.130 And so of course, if I run past line 13, nothing will happen. 00:01:43.130 --> 00:01:44.730 I actually won't print "come on in." 00:01:44.730 --> 00:01:49.463 So at this point, debug50 seems to be not really helping me here. 00:01:49.463 --> 00:01:51.630 I've walked through my entire program, but I haven't 00:01:51.630 --> 00:01:54.180 seen what went wrong in my code. 00:01:54.180 --> 00:01:57.600 Well, debug50 has this option to step into a function, actually dive 00:01:57.600 --> 00:02:00.670 in and see what's happening underneath that hood of that function. 00:02:00.670 --> 00:02:07.840 So what I can do is go to debug50 again, and run debug50 with secret, 00:02:07.840 --> 00:02:11.030 wait for my code to go to that breakpoint again, 00:02:11.030 --> 00:02:16.460 and I'll walk through that first piece where I type in my phrase, please. 00:02:16.460 --> 00:02:20.000 But now, instead of stepping over this check phrase function, 00:02:20.000 --> 00:02:23.690 I actually want to dive into it and see what's going on underneath there. 00:02:23.690 --> 00:02:26.900 So I'll do step into in debug50, and then see, 00:02:26.900 --> 00:02:29.310 I'm setting my password to be please. 00:02:29.310 --> 00:02:33.860 So if I step over line 22, I should see that password becomes please, 00:02:33.860 --> 00:02:35.790 as it does on this left hand side. 00:02:35.790 --> 00:02:38.662 And now, when I check for if phrase is password, 00:02:38.662 --> 00:02:40.370 I should actually be able to return true, 00:02:40.370 --> 00:02:43.760 because it seems that phrase is the same as password. 00:02:43.760 --> 00:02:45.965 So I'll step over that, and see-- 00:02:45.965 --> 00:02:47.770 well actually, I get to line 29. 00:02:47.770 --> 00:02:50.360 And this is kind of subtle, but in C, you 00:02:50.360 --> 00:02:53.040 can't compare two strings using two equal signs. 00:02:53.040 --> 00:02:55.790 You actually could use a different function called string compare, 00:02:55.790 --> 00:02:58.260 or strcmp for short. 00:02:58.260 --> 00:03:02.690 So to use strcompare, I can use strcmp, and give it 00:03:02.690 --> 00:03:05.880 two arguments, phrase and password. 00:03:05.880 --> 00:03:08.120 And with these two inputs, strcompare will tell 00:03:08.120 --> 00:03:11.100 me are they exactly equivalent or not. 00:03:11.100 --> 00:03:13.310 If they are equivalent, strcmp-- 00:03:13.310 --> 00:03:17.100 or strcompare-- will give me 0 as a value back. 00:03:17.100 --> 00:03:20.373 So now my code is saying, if these strings are equivalent-- 00:03:20.373 --> 00:03:22.790 and I know they are because strcompare is giving me zero-- 00:03:22.790 --> 00:03:26.180 I can go ahead and return true and say this is the correct password. 00:03:26.180 --> 00:03:31.070 So with this new code here, I should be able to exit debug50 and go back 00:03:31.070 --> 00:03:32.240 to the terminal. 00:03:32.240 --> 00:03:39.787 And now I can make secret again, run secret, type in my password of please, 00:03:39.787 --> 00:03:40.620 and now there it is. 00:03:40.620 --> 00:03:42.000 It says, come on in. 00:03:42.000 --> 00:03:45.000 And with that, we've seen how debug50 can help us not only step over 00:03:45.000 --> 00:03:49.940 our code, but dive into functions and see the bugs inside of them.