1 00:00:05,400 --> 00:00:07,890 In this video, I'd like to go over the basics of using 2 00:00:07,890 --> 00:00:10,889 the CodeLite IDE's debugger. 3 00:00:11,389 --> 00:00:15,070 All the IDEs have some sort of debugger built in, and they 4 00:00:15,070 --> 00:00:16,429 all work more or less the same. 5 00:00:16,570 --> 00:00:19,099 There are some differences between them, but they all have break points. 6 00:00:19,100 --> 00:00:21,290 They all have watches, and they all let you see your local 7 00:00:21,300 --> 00:00:24,260 variables, which is really all we're interested in right now. 8 00:00:24,760 --> 00:00:27,259 So I'm in the section 12 workspace, and I've got a project 9 00:00:27,290 --> 00:00:30,330 called debugger, that's got some source code that i want to debug. 10 00:00:30,939 --> 00:00:32,819 So what do I do? 11 00:00:33,000 --> 00:00:35,980 Well, the first thing you want to do is, rather than build, 12 00:00:36,170 --> 00:00:37,095 we're going to do debug. 13 00:00:37,390 --> 00:00:38,490 So we're going to press F5. 14 00:00:38,540 --> 00:00:40,510 But we're not going to do that quite yet, I want to set 15 00:00:40,510 --> 00:00:41,960 up this environment first. 16 00:00:42,480 --> 00:00:45,150 Because right now, I'm on a single monitor system. 17 00:00:45,540 --> 00:00:49,139 And it's much nicer to work obviously with a dual monitor system so you 18 00:00:49,139 --> 00:00:52,160 can have your console in one area and the debugger in the other. 19 00:00:52,420 --> 00:00:57,570 But on a single monitor system, you need to make some room here. 20 00:00:57,580 --> 00:01:00,800 So what I'm going to do is I'm going to grab this and just move 21 00:01:00,800 --> 00:01:05,119 it over here so it fills up half the screen, move it over here 22 00:01:05,120 --> 00:01:06,589 so it fills up half the screen. 23 00:01:06,809 --> 00:01:10,360 Okay, then when I debug, I want the console window over here on 24 00:01:10,360 --> 00:01:13,389 the right, but I don't really want it to take up half the screen. 25 00:01:13,389 --> 00:01:15,290 I don't need it to take up half the screen, so I'm just going to 26 00:01:15,290 --> 00:01:18,800 move this guy over to about there, and that should allow me to see 27 00:01:18,800 --> 00:01:22,940 all the the icons that we can use up top to control the debugger. 28 00:01:23,450 --> 00:01:25,799 Okay, again, this is just a personal preference. 29 00:01:25,799 --> 00:01:28,139 You can do this whatever you like that works for you. 30 00:01:28,690 --> 00:01:31,095 The next thing we need to do is, we need to set a breakpoint. 31 00:01:31,400 --> 00:01:33,460 You need to set a breakpoint for the debugger. 32 00:01:33,460 --> 00:01:35,330 Now I'm going to do that right in main. 33 00:01:35,610 --> 00:01:39,870 If you look right next to line 16 here, not in this little gray gutter 34 00:01:39,870 --> 00:01:43,089 area right here where the arrow is, but right to the left of that you 35 00:01:43,090 --> 00:01:46,949 can click right in there, and you see that little red stop sign comes up. 36 00:01:46,980 --> 00:01:47,949 That's a break point. 37 00:01:48,350 --> 00:01:51,240 Okay, what's going to happen is the debugger is going to kick in. 38 00:01:51,950 --> 00:01:53,789 And it's going to start executing your code, then it's going 39 00:01:53,790 --> 00:01:55,460 to stop at that break point. 40 00:01:56,320 --> 00:01:59,990 From there, you can go line by line, you can go into functions, you can 41 00:01:59,990 --> 00:02:02,779 look at watches, you can do all kinds of stuff, which is pretty cool. 42 00:02:03,530 --> 00:02:04,670 So that's where we're at now. 43 00:02:05,020 --> 00:02:08,619 Now also notice up here in perspective, there's a debug layout. 44 00:02:08,940 --> 00:02:11,519 This is the layout that will automatically kick in 45 00:02:11,570 --> 00:02:12,880 when you start the debugger. 46 00:02:13,500 --> 00:02:16,409 So it may look different on your end than from then on what 47 00:02:16,410 --> 00:02:19,359 you're going to see on my end, but it's totally customizable. 48 00:02:19,660 --> 00:02:21,899 You do whatever you like, close windows, open windows. 49 00:02:21,900 --> 00:02:24,810 And when you're done, just go to perspective save current 50 00:02:24,820 --> 00:02:27,150 layout as debug layout again. 51 00:02:27,280 --> 00:02:29,820 So next time you run, it'll be in that perspective. 52 00:02:30,300 --> 00:02:31,940 All right, so let's debug. 53 00:02:32,080 --> 00:02:35,650 We're going to come up to the debugger and start continue 54 00:02:35,650 --> 00:02:37,150 debugger or you can press F5. 55 00:02:37,679 --> 00:02:40,879 Make sure you've got a break point set before you do this, otherwise 56 00:02:40,920 --> 00:02:41,939 you're not going to see much. 57 00:02:42,629 --> 00:02:45,559 So we're going to go to the debugger and start it, build and debug. 58 00:02:46,690 --> 00:02:47,980 Now you'll see the console. 59 00:02:47,980 --> 00:02:49,660 Now what I'm going to do with the console is just move it 60 00:02:49,660 --> 00:02:52,020 over to the right so it fills up the rest of that space. 61 00:02:52,080 --> 00:02:52,609 There you go. 62 00:02:53,300 --> 00:02:55,679 So all the output, I'm going to see over here on the right. 63 00:02:56,370 --> 00:02:59,829 Okay, you'll notice down here that I've got an output view, 64 00:02:59,900 --> 00:03:01,300 and I've got a debugger view. 65 00:03:01,880 --> 00:03:03,730 I really don't care about this output view. 66 00:03:03,730 --> 00:03:05,320 I just want to look at my debugger view. 67 00:03:05,320 --> 00:03:07,380 So you can close this site if you like, you can just 68 00:03:07,380 --> 00:03:08,590 click that x right there. 69 00:03:09,599 --> 00:03:11,390 And I've got only the debugger view. 70 00:03:12,469 --> 00:03:15,010 Now in the debugger view, there's a lot of stuff in here. 71 00:03:15,070 --> 00:03:17,310 All we're really interested in at this level right now are 72 00:03:17,310 --> 00:03:20,990 your local variables and any watches you might want, and I'll 73 00:03:20,990 --> 00:03:22,310 talk about watches in a second. 74 00:03:24,000 --> 00:03:28,230 Cool, okay, so you can see here are my local variables I, x and y. 75 00:03:28,480 --> 00:03:33,350 If you look at main, you'll see i., you'll see x and you'll see y. 76 00:03:33,360 --> 00:03:34,709 X and y right here on line 22. 77 00:03:35,029 --> 00:03:38,030 You'll also see that little break point right there the red. 78 00:03:38,310 --> 00:03:39,520 There's a little green arrow in there. 79 00:03:39,520 --> 00:03:41,609 I'm not sure if you'll be able to see it on the video, but you will 80 00:03:41,610 --> 00:03:44,000 be able to see it in a minute when I start walking through the code. 81 00:03:44,580 --> 00:03:47,430 Basically that's the line that's about to execute. 82 00:03:48,460 --> 00:03:51,959 Okay, you'll notice right now I X and j have garbage in them, that's 83 00:03:51,960 --> 00:03:54,329 because line 16 hasn't executed yet. 84 00:03:55,470 --> 00:03:59,990 Okay, so the debug icons up here, you can see them, you can show 85 00:03:59,990 --> 00:04:02,710 the current line, you can step in which is what we're going to 86 00:04:02,710 --> 00:04:06,350 use to go into a function or you can just go to the next line. 87 00:04:06,920 --> 00:04:08,080 Don't worry about step out. 88 00:04:08,090 --> 00:04:09,620 These are the two main ones we're going to use. 89 00:04:09,630 --> 00:04:11,949 Step in, next. 90 00:04:12,170 --> 00:04:14,810 And if you want to stop your debugging session, you can press 91 00:04:15,020 --> 00:04:17,170 that little stop button right here. 92 00:04:18,700 --> 00:04:20,539 Okay, so let's do this. 93 00:04:20,649 --> 00:04:24,270 And again, you may want to close your out your workspace view 94 00:04:24,280 --> 00:04:26,750 here if you don't have enough space on your monitor, but 95 00:04:26,750 --> 00:04:27,990 I've got plenty of space here. 96 00:04:29,029 --> 00:04:29,880 So let's start. 97 00:04:29,949 --> 00:04:32,520 Let's click on this button right here next. 98 00:04:32,640 --> 00:04:35,770 And you've got there's your debugger, you can also click next 99 00:04:35,770 --> 00:04:39,779 here you can press F10, whatever it is on a mac, I'm not sure. 100 00:04:40,090 --> 00:04:41,390 But you can toggle beak points. 101 00:04:41,390 --> 00:04:43,240 You've got a lot of different options here, but I'm just going 102 00:04:43,240 --> 00:04:46,120 to use these icons for now because it's easier to see what I'm doing. 103 00:04:47,359 --> 00:04:49,220 All right,s o we're here on line 16. 104 00:04:49,270 --> 00:04:52,060 Let's press that button right here next. 105 00:04:52,440 --> 00:04:54,730 Now when I do that, you'll notice that little green arrow will go 106 00:04:54,730 --> 00:04:58,219 to the next line, that means that this line executes, and we should 107 00:04:58,219 --> 00:05:00,439 see I down here change to 5. 108 00:05:00,710 --> 00:05:05,694 Okay, so I'm going to do that see the green arrow is now on line 17, that's 109 00:05:05,699 --> 00:05:07,450 the instruction that'll execute next. 110 00:05:07,920 --> 00:05:10,220 And also notice that I is now 5. 111 00:05:11,130 --> 00:05:14,830 Okay, sometimes you have to refresh this view down here, especially 112 00:05:14,830 --> 00:05:16,010 when you're working with pointers. 113 00:05:16,200 --> 00:05:19,009 If you need to do that, you just click that little button right 114 00:05:19,009 --> 00:05:21,640 here, and it refreshes that view. 115 00:05:22,980 --> 00:05:27,030 Okay, so now we're in this code while I is greater than equal to 0. 116 00:05:27,160 --> 00:05:28,500 We know i is 5, right. 117 00:05:29,380 --> 00:05:30,690 Sometimes it's pretty handy. 118 00:05:31,010 --> 00:05:35,109 You can just kind of select i right there and press 119 00:05:35,110 --> 00:05:39,219 ctrl and you can see i is 5. 120 00:05:39,219 --> 00:05:41,169 It pops up in this little tool tip right here. 121 00:05:41,650 --> 00:05:44,560 Okay, right now if I look at x, x is garbage because I 122 00:05:44,560 --> 00:05:45,820 haven't gotten to that line yet. 123 00:05:45,830 --> 00:05:48,750 So that can be pretty handy sometimes, but most of that information 124 00:05:48,760 --> 00:05:49,760 you'll be able to see here. 125 00:05:50,450 --> 00:05:51,909 Okay, so that's my locals. 126 00:05:52,330 --> 00:05:56,270 Suppose I want to do a watch, I want to watch I greater than 0. 127 00:05:56,530 --> 00:06:01,549 So I can come over here, and I can select the plus sign and type 128 00:06:01,550 --> 00:06:06,900 the entire expression, i greater than 0 and press okay, you can see 129 00:06:06,900 --> 00:06:10,400 it's a Boolean, and right now it's true because 5 is greater than 0. 130 00:06:10,400 --> 00:06:12,100 So you could switch between these two. 131 00:06:12,100 --> 00:06:15,359 You can obviously pull this pane out and have it in a dual monitor system. 132 00:06:15,770 --> 00:06:17,710 But right now, let's just keep going. 133 00:06:18,570 --> 00:06:22,790 So now i is 5 and we'll keep clicking on the next. 134 00:06:22,790 --> 00:06:25,570 You can see I'm in the body of the loop now. 135 00:06:25,700 --> 00:06:28,659 Now we have an output statement, so we should see 5 display 136 00:06:28,660 --> 00:06:30,190 out here on the console. 137 00:06:31,020 --> 00:06:32,960 And there you can see the 5 just display. 138 00:06:33,420 --> 00:06:36,379 Now we're going to decrement I, so I should go down to 4 down 139 00:06:36,379 --> 00:06:38,409 here in my local variables view. 140 00:06:39,870 --> 00:06:41,170 And there it is 4. 141 00:06:41,320 --> 00:06:43,050 And I'm just going to keep looping through here. 142 00:06:43,740 --> 00:06:45,289 All I'm doing is pressing next. 143 00:06:45,290 --> 00:06:46,860 You can see my code executing. 144 00:06:47,200 --> 00:06:49,429 It's very similar to what I was doing with my pencil 145 00:06:49,429 --> 00:06:51,420 and marker on the screen. 146 00:06:51,799 --> 00:06:54,289 But here you can see, it actually happening happening live. 147 00:06:54,730 --> 00:07:00,560 So at this point now, I becomes 0, the loop will fail, and I'm out of it. 148 00:07:01,360 --> 00:07:04,220 Okay, now I'm executing line 22. 149 00:07:04,260 --> 00:07:07,380 When I click next, those two variables, x and y, will be 150 00:07:07,380 --> 00:07:08,950 initialized right down here. 151 00:07:09,170 --> 00:07:10,349 So I'll click next. 152 00:07:11,119 --> 00:07:14,850 And you can see that they're 100 and 200, exactly what I expected. 153 00:07:15,420 --> 00:07:18,440 Now I'll execute lines 23 and 24, so we should display 154 00:07:18,440 --> 00:07:20,010 x and y to the console. 155 00:07:20,700 --> 00:07:22,719 There's x, there's y. 156 00:07:23,900 --> 00:07:24,970 Okay, so far so good. 157 00:07:25,420 --> 00:07:28,159 Now I'm calling the swap function that we wrote in this section, 158 00:07:28,440 --> 00:07:30,800 and I'm passing in the address of x and the address of y. 159 00:07:31,350 --> 00:07:33,940 If you just click next here, it'll just skip that and go 160 00:07:33,940 --> 00:07:35,080 right next to the next line. 161 00:07:35,110 --> 00:07:38,120 Well, actually it'll call the function but I want to go into that function. 162 00:07:38,590 --> 00:07:41,030 So you want to click this one right here called step in. 163 00:07:41,400 --> 00:07:45,310 So when I select that, now I go into the function, so I'm 164 00:07:45,310 --> 00:07:47,050 starting to execute that function. 165 00:07:47,050 --> 00:07:49,140 You can see the green arrow right here on line nine. 166 00:07:49,890 --> 00:07:51,800 Okay, and you can see a and b. 167 00:07:52,290 --> 00:07:54,570 Well, a and b are pointers, and they've already been set. 168 00:07:54,600 --> 00:07:55,810 You can see them right here. 169 00:07:55,930 --> 00:07:56,980 There's a, there's b. 170 00:07:56,990 --> 00:08:01,030 Those are the two addresses, and you can see their type, integer pointers. 171 00:08:01,740 --> 00:08:03,130 Temp is just a regular int. 172 00:08:03,549 --> 00:08:08,320 So if i expand a, it tells me what a is pointing to, which is a 100. 173 00:08:08,950 --> 00:08:11,370 Expand b, b is pointing to 200. 174 00:08:12,330 --> 00:08:14,010 Okay, so now let's walk through the swap. 175 00:08:14,260 --> 00:08:16,740 The first line says take what a is pointing to. 176 00:08:16,740 --> 00:08:21,150 Well, you know what a is pointing to right here, 100 and assign it to temp. 177 00:08:21,380 --> 00:08:23,039 So right now temp has garbage. 178 00:08:23,459 --> 00:08:24,570 So let's do that. 179 00:08:24,570 --> 00:08:28,190 We'll click next, and now you see temp has a 100 in it. 180 00:08:29,140 --> 00:08:32,490 Then we're taking what b is pointing to, which is 200 and putting 181 00:08:32,490 --> 00:08:33,808 it into where a was pointing. 182 00:08:34,570 --> 00:08:36,110 So now I'll go next. 183 00:08:37,280 --> 00:08:39,578 You see right here, you see how it didn't refresh, it doesn't 184 00:08:39,580 --> 00:08:40,760 do that with pointers sometimes. 185 00:08:40,760 --> 00:08:43,179 That's when you have to click this little refresh dial right there, 186 00:08:43,750 --> 00:08:46,739 and then just re-expand them, and you can see that it did do that now. 187 00:08:47,510 --> 00:08:53,119 We'll do the next and refresh one more time and you can see that they've 188 00:08:53,130 --> 00:08:55,039 been swapped, which is pretty cool. 189 00:08:55,840 --> 00:08:58,280 At this point, I click next, this function is done. 190 00:08:58,740 --> 00:09:02,010 Notice that my local variables now are a b and temp, that's because 191 00:09:02,010 --> 00:09:03,250 I'm in this function right here. 192 00:09:03,250 --> 00:09:04,580 Those are the local variables. 193 00:09:05,500 --> 00:09:07,890 My watch doesn't apply anymore, right. 194 00:09:08,040 --> 00:09:09,170 It's in the main. 195 00:09:09,429 --> 00:09:11,639 So here, I'll click next again. 196 00:09:12,170 --> 00:09:13,970 Now I'm back in main. 197 00:09:14,070 --> 00:09:15,690 I'm right here on line 28. 198 00:09:16,500 --> 00:09:18,680 And I'm going to display x and y again. 199 00:09:19,900 --> 00:09:24,209 There's x 200, there's y 100, and our program is done. 200 00:09:25,570 --> 00:09:28,179 Okay, so there's a lot of options that you can do. 201 00:09:28,179 --> 00:09:30,550 You can learn to play with the debugger. 202 00:09:30,550 --> 00:09:32,820 There's really -- debuggers are awesome. 203 00:09:34,039 --> 00:09:37,600 The best advice I could give you with a debugger is that they are -- they're 204 00:09:37,600 --> 00:09:41,219 called debuggers for a purpose because they really help you debug your code. 205 00:09:42,000 --> 00:09:46,039 I wouldn't suggest that you start using a debugger as so much of 206 00:09:46,040 --> 00:09:47,659 a learning aid at this moment. 207 00:09:47,969 --> 00:09:50,280 I think it's better just to do it with paper and pencil at the 208 00:09:50,280 --> 00:09:53,650 beginning because the debugger itself has a bit of a learning curve. 209 00:09:53,879 --> 00:09:55,910 So you're learning c++, and you're learning the IDE 210 00:09:57,260 --> 00:09:59,310 and now you're learning a debugger all at the same time. 211 00:09:59,580 --> 00:10:03,330 But in the context of what i just did here, it's very useful. 212 00:10:03,580 --> 00:10:06,919 Suppose you have a bug in your code and the bug was that, it 213 00:10:07,049 --> 00:10:10,540 was greater than or equal to 0 or you had a less than there. 214 00:10:10,779 --> 00:10:13,049 And maybe you didn't see it when you're looking at your code, 215 00:10:13,320 --> 00:10:15,730 well when you run it through the debugger bam, you'll see the 216 00:10:15,730 --> 00:10:17,230 error, you can fix it right away. 217 00:10:18,190 --> 00:10:21,450 Okay, so feel free to use the debugger, feel free to use the 218 00:10:21,450 --> 00:10:25,869 debugger in the challenge exercise for this section, it might come in handy.