How to Use a Debugger - Debugger Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome back to the YouTube video so in today's video what I'm gonna be doing is teaching you how to use a debugger now I'll be using the vyas code to bug her here and I'll be working in a Python script but don't worry this video will apply to any debugger for any development environment that you use and if you're not sure what a debugger is or where you even access it you'll need to be using an IDE to get access to a debugger there's some tools that aren't IDs that have it but in most cases an IDE which is an integrated development environment will come with some kind of debugging tool and that'd be debugging tool will look very similar to the one I'm about to show you here and will definitely have the same four or five commands and I'm gonna be aiming to teach you about how they work so with that being said let's go ahead and get started now what is a debugger a debugger is really a tool to help you look at what's going on in your program usually when people are debugging what they'll do is they'll do something called print debugging so if you've never seen a debugger before you're not using some fancy tool or environment your way of probably checking what's going on is printing things out to the console or printing things to your terminal or wherever you're running your program so you might do that by just going in here and say okay well I want to print what numbers is just have a look at that here and see if things are what I expect them to be that's totally fine you can do that and for simple programs and for you know simple scripts and whatever you have that's totally fine and I do that all the time as well but as soon as you get into something that's pretty complicated where you might not know the values you actually want to look at maybe there's a lot of files there's a lot of different states that you want to examine it can get really messy if you're just printing values out into the console so instead you use a tool like a debugger now right here I actually have my debugger active it's currently running and I'll explain what all of this is doing later but you can see that I'm actually able to look at all of the different values in my program so I have a breakpoint right here which essentially tells the debugger hey I want to stop pause and have a look at what's going on at this point in time on line 25 the one that's highlight yellow on the left-hand side here you can see that it shows me locals and Global's I can click into these and I can actually have a look at all of the variables that are associated with the current scope that I'm in so here for example average is a variable that stores the kind of pointer to this function right here generate numbers oh well that's a variable that stores the pointer to this function right here highest running average same thing random oh what's that that's the variable associated with this import so you can actually go in and look at all of the different values associated with each of these variables now in this case it's not super useful because these are just functions but I will show you throughout this video of course how we can look at actual code and not just function pointers and all of that but this is just to give you an idea of what a debugger can really do so you place these little breakpoints which I'll show you how to do again and then what you can do is use some of the commands that are up here in this menu bar that I'm moving around which I'm gonna leave up here to actually step and navigate through your code so there's three main tools here that you want to know how to use step over step into and step out of now they each have shortcuts you can see the keyboard shortcuts right here I'm gonna be showing you how to use these but I just want to again give you an example of what they can actually do so look here when I click step over what this does is actually move this little pointer here to the next line of code so you can see that now what I actually have is a variable called num that stores all of the nums that were generated from this line so I actually go ahead and have a look at what all of these different numbers are I can see the properties for example the length of that list so there's a hundred different numbers and anything else that's related to Nub's I can go ahead and I can actually look at that which is great what I can do next is I can step over again so I can step over that note on that line right here nothing's changed because I haven't updated any variables or anything into this scope and then what I could do is actually step out of and what that will do is take me outside of the current scope which in this case will end the program so now that I've given you a little bit of an example let's go into some more detail and show how we can really use this debugger effectively so I figured that before I actually go into a full example and illustrate the debugger I should probably show you how to open the debugger and what the core features of it are just so you know while we go through this video so it's gonna be different to open a debugger in whatever environment you're working and so whatever IDE you have usually what you're gonna want to look for though is something that has a little bug icon and a kind of play sign like this or play logo so in vs code to access the debugger what you do is on the little left hand menu bar here you press on this icon that I'm pressing on here this will open up the debugger and right now I currently have mine active but if I stop it by hitting the stop button you'll see that it shows run in debug like this and it shows you where all of your breakpoints are in this little pain down here so this is great this is a good way to organize your breakpoints you can see I've added a bunch here and it shows me where they are by line number and I can simply deactivate them by just unchecking that button it Gray's it out and what that means is I'm not gonna remove that breakpoint but I just want you to ignore it for now it's almost like commenting the breakpoint out if you want to think of it like that so once you're ready to actually run your code you can click run and debug it's gonna be different in whatever environment in this case you have to select what file you want so I'll click python file here we go ahead and we start debugging and then we hit the most recent breakpoint and then here we are now the other two features that I kind of want to discuss here is this watch pane in this call stack so I won't be using these in the examples because we don't really need to but say there's a certain expression that you want to watch or say there's a certain value you want to look at that isn't directly written inside of this code so I've already showed you and I actually I'll show you here you can highlight over top of specific values if they've already been defined and you can get actually what the value is but let's say there's no you want to create your kind of own value to look at what you do is use this watch tab and you can go ahead and type in variables that you want to look at so let me just step over here quickly and go to generate numbers let's just keep stepping for one second I'm gonna step there so say we're here and we're at this point and what I want to actually do is look at a slice of nums so rather than looking at what all of the numbers are I just want to look at slice of it what I can actually do is write numbs and then I could say like : 5 and hit enter and what that will do is let me watch exactly the expression I type in here so I can watch what a slice of up to 5 of the numbers is from this watch tab so that's really useful you can add as many things to watch as you want you can literally do something like you know seven less than five you can watch that that's just always going to be false any expression you want you can put in here it will evaluate it after every time you step and you'll be able to watch that right if that makes sense the call stack is kind of a little bit complicated so I'm not really going to explain it but this will show you what you're gonna be returning to if you're calling in to a specific function so I'll show really quickly if I go step into here you can see the call stack now has module and then running average what this is telling you is that if you step back you're gonna go from running average to module and actually let me just step over here a little bit let me step into and you can see now we have three things on the call stack so average running average module this means if you step out of average you'll go to running average if you step out of running average you'll go back to module so that is the basics those are the main features now we'll get into a full example and really talk about how all these tools work so again you want to use the debugger when things are complicated and when you want to take kind of a snapshot of your code at a certain point and look at what's going on in a specific place the way you do that is by placing breakpoints so breakpoints these apply to all different debuggers and typically what they are is a red circle you can see I'm placing a ton here in what's called the gutter beside all of these line numbers so there's different ways to plague replace breakpoints in different editors and there's different ways of seeing them and displaying them but usually you place them in the gutter so that's either on the left or right hand side of wherever your code is sometimes you might have to right-click and it says place breakpoint there might be a shortcut for that but find how to place a breakpoint and essentially praise place them where you want to have a snapshot and take a look at your code now you can place as many breakpoints as you want you place as few as you want but if you want the debugger to actually run and be active you need to place at least one the reason for that is because the debugger does not stop running the program until it sees a breakpoint so if I go here and click run and debug and I run my Python file notice that we don't get any lines the debugger doesn't activate we don't highlight anything we don't see any variables in the left-hand side the reason for that is because I didn't place any breakpoints so if I go ahead now and I place a breakpoint inside of average here like this so I'll place one right now what's gonna happen is every single time that my program hits this breakpoint we're going to pause and have a look at what's going on so let's see when I click run and debug what happens when I place a breakpoint right here so immediately the first place I get into here is average so it did whatever else in the code we had to do and as soon as a verage was called boom it stopped and it shows me the local variables associated with this function so you can see it's showing me D and n so 246 and 1 and not saying the denominator is 246 and the numerator is 1 I might have messed up the way that those should go but either way you see them right here and now what I can do is use some of these tools here to step through my code and see what else is going on so the three tools that we want to learn is step over step into and step out the first one is the easiest it's step over all that does is move you to the next line of code so if you press step over what it's gonna do is just go down to the next line that you see in your code that you would assume that you would go to if you call step into what this will do is step into any method calls function calls anything that's happening on the current line so on this line here let's say I called another function and I called running average like that what this would do if I called step into is actually bring me into this function here and then we'll start running again where we're gonna start from the top and continue and we use these commands here to continue navigating through the code if I press step out of what this does is take me out of the current scope so we'll look for the last return statement inside of our is it lost return statement yes I believe it's the last return statement inside of whatever function we're at or whatever returned statement a hit sorry and then it will simply exit and go back to where it was called from so if I call step out it will bring me to where we called this average function from and then the next line after that so let me show you what step out does because it is definitely the most complicated will call step out and you can see it brings me to current average which is where we called this average function from that is what step out does now what I'm gonna do is call step into we're gonna move to the next line because there was nothing to step into there and we'll step into again now since there was nothing to step into right there was no method calls we move to the next line now I can press step over or step into because there's nothing to step into here actually let's see what step into does it just moves us the next line will step into that and now let's step into here what that does is bring me up to the average function because I called the average function on this line so we stepped into that function call now we're back here again and we can do whatever we want in fact something that we can do is press play so if we press continue which is f5 what this will do is move the program along until we hit another breakpoint so say okay we don't want to step around anymore we don't need to navigate let's just keep moving so let's go continue and boom where does it bring us back right into the average function because that was the next place that our program went to the next breakpoint that it hit and if I keep playing you can see on the left hand side here these local variables are being updated because the average function keeps getting called so I can keep pressing play and I can look at all of the different values inside of this average function let's say we're done we don't want to debug anymore what we can do next is we can hit the pause boom we'll be done and we stopped at debug now that is the basics of using a debugger and that's all you really need to know is what those few different commands do so now let's pray place a few more breakpoints and maybe some more interesting places so a place one on the averages up here we'll place one actually on return averages and then we maybe will place one on down here so after we generate the numbers okay so let's run this now and let's see what happens when we have more breakpoints in our program all right so the first breakpoint is right here so right after we generate our numbers we can have a look at the numbers here I'm not too you know concerned about stepping through this anymore so what I'll do is press play and notice it brings us to our next breakpoint which was up here you can see where we are based on this little kind of house shaped yellow thing around the break point and the highlight of the line this will move down on the line to show you where you currently are so we have numbers which is a local variable associated with this parameter and notice when I highlight this I can actually go ahead and see the value of all of these numbers that's a cool feature inside of es code next I'm going to step over that will bring us to total now I can highlight over averages and we can see that it's length 0 and simply an empty array let's click play again or actually let's click step over we're gonna step over here what I'm gonna do is I'm actually going to step over this as well so we get into total boom look total is now updated to 279 because we added the value of num which what is num let's highlight over it to 79 let's step over again we can see now current average is 279 the return value from average was 279 so it's showing us that and now average doesn't have anything in it yet let's step over again and notice average now gets updated with 279 so that is a very useful thing to be able to do because you can step through a for loop and see exactly what's happening let's say I'm done with this for loop I don't want to do any more let's press play now we can see all these values are updated the last number we were out was 287 the current average was 156 and all the averages are stored inside of the running averages list right here awesome so now let's see where this return goes to so we can go ahead and go step out or we can just step over and that will bring us back so let's go step out and step out brings us to here because this was where it was called from running average and now let's go step over alright and let's step into this now because I want to step into any function calls that happen on this line which should be highest running average so we're gonna step into brings us to highest running average which calls running average let's step into there again that brings me into running average because it brought me into any function calls on line let's step out that brings me to the return statement or actually sorry where the other breakpoint was let me remove that let's go step out now we're here okay let's just step over step over and we are done there was no more breakpoints and we finished the program so that is pretty much how a debugger works those are the three main tools you have your pause so 5 main tool sorry your pause your step over step in to step out and I guess your continue or whatever the other button is was gonna be and that really is all you need to know to effectively use a debugger when you're working on something you want to figure out what a certain piece of code looks like in a certain area what all the values of everything are what the state is you simply place a breakpoint you can place multiple breakpoints if you want and then you press play you get to that breakpoint it will pause you can step through your code however you want you can press play when you're done and you can continue from there so with that being said and that has been my tutorial on how to use a debugger again some of these things may vary depending on what environment you're using what like what editor you have how much code you have how many files I will note that if you're working with multiple files the debugger does work and it will go between the different files when you step into something or step out which is definitely really useful and yeah that's pretty much all I can say about this so with that being said if you guys enjoyed make sure you leave a like subscribe to the channel and I will see you guys in another YouTube video
Info
Channel: Tech With Tim
Views: 272,654
Rating: undefined out of 5
Keywords: tech with tim, debugger tutorial, debugger, how to use a debugger, debugger in visual studio code, using a debugger, debugger guide, debugging, debugging in visual studio code, debugging vscode, debugging python, python debugging, code debugging, python debugger, debug, x64 debugging tutorial, debugging in windows 10, what is debugging, software development, how to debug
Id: 7qZBwhSlfOo
Channel Id: undefined
Length: 17min 0sec (1020 seconds)
Published: Mon Jul 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.