1 00:00:00,000 --> 00:00:01,440 SPEAKER 1: Welcome, everyone. 2 00:00:01,440 --> 00:00:04,310 My name is Carter, and today I'll show you my secret program. 3 00:00:04,310 --> 00:00:07,410 You can run the program by typing ./secret into your terminal, 4 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. 5 00:00:10,760 --> 00:00:11,880 So let's try it out. 6 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, 7 00:00:16,940 --> 00:00:18,765 let me in. 8 00:00:18,765 --> 00:00:20,640 And at which point, I don't get any response. 9 00:00:20,640 --> 00:00:23,270 So let me try it again and do ./secret. 10 00:00:23,270 --> 00:00:26,490 Maybe password is the password here. 11 00:00:26,490 --> 00:00:27,350 But it's not. 12 00:00:27,350 --> 00:00:29,940 And I'll just reveal to you that the password is please. 13 00:00:29,940 --> 00:00:35,170 So if I type ./secret, and I type in please, I should see come on in. 14 00:00:35,170 --> 00:00:36,940 But I don't. 15 00:00:36,940 --> 00:00:38,950 And so what's the problem here? 16 00:00:38,950 --> 00:00:42,490 Well, maybe I should look into my code, and maybe step through it bit by bit. 17 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, 18 00:00:48,060 --> 00:00:50,490 starting with this int main(void) function. 19 00:00:50,490 --> 00:00:52,710 To step through my code, remember we can use debug50 20 00:00:52,710 --> 00:00:55,350 to set a breakpoint, or a place to pause in our code. 21 00:00:55,350 --> 00:01:00,510 So I can go to this left side here, find a pale red circle, 22 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, 23 00:01:04,500 --> 00:01:06,460 or that breakpoint. 24 00:01:06,460 --> 00:01:11,688 Now I can do debug50 of secret, and then my code should boot up, 25 00:01:11,688 --> 00:01:14,230 and I should be able to pause right at that moment I'm asking 26 00:01:14,230 --> 00:01:17,240 the user for their secret phrase. 27 00:01:17,240 --> 00:01:19,840 Of course, I have to actually step over that line for it 28 00:01:19,840 --> 00:01:23,160 to actually run my program, so I'll then step over, 29 00:01:23,160 --> 00:01:26,530 and I can type in my secret phrase of please. 30 00:01:26,530 --> 00:01:27,780 OK, so that seems pretty good. 31 00:01:27,780 --> 00:01:30,840 I see that phrase becomes please, and correct is currently false, 32 00:01:30,840 --> 00:01:34,510 but it should change to true after I do this check phrase function. 33 00:01:34,510 --> 00:01:38,820 So I'll step over check phrase, and I see it's actually still false. 34 00:01:38,820 --> 00:01:43,130 And so of course, if I run past line 13, nothing will happen. 35 00:01:43,130 --> 00:01:44,730 I actually won't print "come on in." 36 00:01:44,730 --> 00:01:49,463 So at this point, debug50 seems to be not really helping me here. 37 00:01:49,463 --> 00:01:51,630 I've walked through my entire program, but I haven't 38 00:01:51,630 --> 00:01:54,180 seen what went wrong in my code. 39 00:01:54,180 --> 00:01:57,600 Well, debug50 has this option to step into a function, actually dive 40 00:01:57,600 --> 00:02:00,670 in and see what's happening underneath that hood of that function. 41 00:02:00,670 --> 00:02:07,840 So what I can do is go to debug50 again, and run debug50 with secret, 42 00:02:07,840 --> 00:02:11,030 wait for my code to go to that breakpoint again, 43 00:02:11,030 --> 00:02:16,460 and I'll walk through that first piece where I type in my phrase, please. 44 00:02:16,460 --> 00:02:20,000 But now, instead of stepping over this check phrase function, 45 00:02:20,000 --> 00:02:23,690 I actually want to dive into it and see what's going on underneath there. 46 00:02:23,690 --> 00:02:26,900 So I'll do step into in debug50, and then see, 47 00:02:26,900 --> 00:02:29,310 I'm setting my password to be please. 48 00:02:29,310 --> 00:02:33,860 So if I step over line 22, I should see that password becomes please, 49 00:02:33,860 --> 00:02:35,790 as it does on this left hand side. 50 00:02:35,790 --> 00:02:38,662 And now, when I check for if phrase is password, 51 00:02:38,662 --> 00:02:40,370 I should actually be able to return true, 52 00:02:40,370 --> 00:02:43,760 because it seems that phrase is the same as password. 53 00:02:43,760 --> 00:02:45,965 So I'll step over that, and see-- 54 00:02:45,965 --> 00:02:47,770 well actually, I get to line 29. 55 00:02:47,770 --> 00:02:50,360 And this is kind of subtle, but in C, you 56 00:02:50,360 --> 00:02:53,040 can't compare two strings using two equal signs. 57 00:02:53,040 --> 00:02:55,790 You actually could use a different function called string compare, 58 00:02:55,790 --> 00:02:58,260 or strcmp for short. 59 00:02:58,260 --> 00:03:02,690 So to use strcompare, I can use strcmp, and give it 60 00:03:02,690 --> 00:03:05,880 two arguments, phrase and password. 61 00:03:05,880 --> 00:03:08,120 And with these two inputs, strcompare will tell 62 00:03:08,120 --> 00:03:11,100 me are they exactly equivalent or not. 63 00:03:11,100 --> 00:03:13,310 If they are equivalent, strcmp-- 64 00:03:13,310 --> 00:03:17,100 or strcompare-- will give me 0 as a value back. 65 00:03:17,100 --> 00:03:20,373 So now my code is saying, if these strings are equivalent-- 66 00:03:20,373 --> 00:03:22,790 and I know they are because strcompare is giving me zero-- 67 00:03:22,790 --> 00:03:26,180 I can go ahead and return true and say this is the correct password. 68 00:03:26,180 --> 00:03:31,070 So with this new code here, I should be able to exit debug50 and go back 69 00:03:31,070 --> 00:03:32,240 to the terminal. 70 00:03:32,240 --> 00:03:39,787 And now I can make secret again, run secret, type in my password of please, 71 00:03:39,787 --> 00:03:40,620 and now there it is. 72 00:03:40,620 --> 00:03:42,000 It says, come on in. 73 00:03:42,000 --> 00:03:45,000 And with that, we've seen how debug50 can help us not only step over 74 00:03:45,000 --> 00:03:49,940 our code, but dive into functions and see the bugs inside of them. 75 00:03:49,940 --> 00:03:52,000