This video provides an introduction to using the GNU Debugger (GDB) for debugging C programs. The speaker explains the importance of debugging, demonstrates various GDB commands such as start, step, continue, list, info locals, info args, where (or bt), break, and watch. The video also addresses common C programming pitfalls like uninitialized variables and stack memory management, using a practical example to illustrate how GDB can help identify and resolve these issues.
-g flag (e.g., gcc -g) is essential for GDB to access debugging symbols, which include function and variable names, making the debugging process more understandable.where command (or bt) helps visualize the call stack, showing the sequence of function calls leading to the current execution point.gcc -Wall -Wextra) can help catch potential issues like uninitialized variables early in the development process.Here is the transcript of the video:
[{"start_ms":"160","end_ms":"6000","text":"week all right so debugging","start_time_text":"0:00"},{"start_ms":"3760","end_ms":"7839","text":"is going to give us the ability to","start_time_text":"0:03"},{"start_ms":"6000","end_ms":"8559","text":"introspect our programs while they're","start_time_text":"0:06"},{"start_ms":"7839","end_ms":"11040","text":"running","start_time_text":"0:07"},{"start_ms":"8559","end_ms":"11759","text":"before we get into how we debug using","start_time_text":"0:08"},{"start_ms":"11040","end_ms":"13280","text":"gdb","start_time_text":"0:11"},{"start_ms":"11759","end_ms":"15519","text":"i want to give you a chance to think","start_time_text":"0:11"},{"start_ms":"13280","end_ms":"18800","text":"through a challenge problem","start_time_text":"0:13"},{"start_ms":"15519","end_ms":"22640","text":"right and oops i meant to","start_time_text":"0:15"},{"start_ms":"18800","end_ms":"26160","text":"unhide the slide so there's a slide here","start_time_text":"0:18"},{"start_ms":"22640","end_ms":"28080","text":"that is not in the slide deck posted on","start_time_text":"0:22"},{"start_ms":"26160","end_ms":"29279","text":"sakai so this is the first time","start_time_text":"0:26"},{"start_ms":"28080","end_ms":"30080","text":"everybody is seeing this i'm going to","start_time_text":"0:28"},{"start_ms":"29279","end_ms":"32239","text":"make this much","start_time_text":"0:29"},{"start_ms":"30080","end_ms":"33120","text":"larger what i'd like you to do i'm going","start_time_text":"0:30"},{"start_ms":"32239","end_ms":"35280","text":"to open up some","start_time_text":"0:32"},{"start_ms":"33120","end_ms":"36880","text":"questions in just a moment on pull","start_time_text":"0:33"},{"start_ms":"35280","end_ms":"39920","text":"everywhere and we'll come back to q","start_time_text":"0:35"},{"start_ms":"36880","end_ms":"42960","text":"a after this but i'd like you to try","start_time_text":"0:36"},{"start_ms":"39920","end_ms":"47600","text":"reasoning through the program you see on","start_time_text":"0:39"},{"start_ms":"42960","end_ms":"50640","text":"the left hand side of this slide and","start_time_text":"0:42"},{"start_ms":"47600","end_ms":"52079","text":"responding to this and we'll spend about","start_time_text":"0:47"},{"start_ms":"50640","end_ms":"54559","text":"three or four minutes trying to think","start_time_text":"0:50"},{"start_ms":"52079","end_ms":"55920","text":"through what you expect is printed here","start_time_text":"0:52"},{"start_ms":"54559","end_ms":"57600","text":"and then we'll come back and discuss","start_time_text":"0:54"},{"start_ms":"55920","end_ms":"60640","text":"what's actually happening and play","start_time_text":"0:55"},{"start_ms":"57600","end_ms":"60640","text":"around with this ourselves","start_time_text":"0:57"},{"start_ms":"62000","end_ms":"65920","text":"uh 80 percent of the class 86 percent of","start_time_text":"1:02"},{"start_ms":"65439","end_ms":"69200","text":"you","start_time_text":"1:05"},{"start_ms":"65920","end_ms":"69680","text":"said that only in 2020 would not be","start_time_text":"1:05"},{"start_ms":"69200","end_ms":"73200","text":"printed","start_time_text":"1:09"},{"start_ms":"69680","end_ms":"75520","text":"all right so um let's just put down","start_time_text":"1:09"},{"start_ms":"73200","end_ms":"77520","text":"let's just uh make some remarks here so","start_time_text":"1:13"},{"start_ms":"75520","end_ms":"83119","text":"uh","start_time_text":"1:15"},{"start_ms":"77520","end_ms":"83119","text":"86 percent of you said this is false","start_time_text":"1:17"},{"start_ms":"83520","end_ms":"87280","text":"right meaning that this print statement","start_time_text":"1:23"},{"start_ms":"85680","end_ms":"90000","text":"wouldn't be printed","start_time_text":"1:25"},{"start_ms":"87280","end_ms":"92640","text":"and then another uh let's see let me","start_time_text":"1:27"},{"start_ms":"90000","end_ms":"95520","text":"change back to my mouse here","start_time_text":"1:30"},{"start_ms":"92640","end_ms":"96560","text":"another 70 percent said that this one","start_time_text":"1:32"},{"start_ms":"95520","end_ms":"99520","text":"wouldn't be printed so there's","start_time_text":"1:35"},{"start_ms":"96560","end_ms":"100000","text":"there's obviously some mistrust here and","start_time_text":"1:36"},{"start_ms":"99520","end_ms":"103200","text":"and the","start_time_text":"1:39"},{"start_ms":"100000","end_ms":"105040","text":"the lack of trust uh is","start_time_text":"1:40"},{"start_ms":"103200","end_ms":"106240","text":"when you're working in c is not","start_time_text":"1:43"},{"start_ms":"105040","end_ms":"109280","text":"unwarranted","start_time_text":"1:45"},{"start_ms":"106240","end_ms":"111840","text":"right so just about everybody says","start_time_text":"1:46"},{"start_ms":"109280","end_ms":"114399","text":"in no world should either of these two","start_time_text":"1:49"},{"start_ms":"111840","end_ms":"116240","text":"print statements be evaluated before","start_time_text":"1:51"},{"start_ms":"114399","end_ms":"118560","text":"you try and run this i want to just give","start_time_text":"1:54"},{"start_ms":"116240","end_ms":"119920","text":"you a demonstration of","start_time_text":"1:56"},{"start_ms":"118560","end_ms":"121840","text":"what's going on here so i'm going to","start_time_text":"1:58"},{"start_ms":"119920","end_ms":"123600","text":"open up and let me make this a little","start_time_text":"1:59"},{"start_ms":"121840","end_ms":"126640","text":"bit bigger","start_time_text":"2:01"},{"start_ms":"123600","end_ms":"128879","text":"i'm going to open up the debug.c program","start_time_text":"2:03"},{"start_ms":"126640","end_ms":"130399","text":"which has this exact same code in it","start_time_text":"2:06"},{"start_ms":"128879","end_ms":"132000","text":"right so this is just what we were","start_time_text":"2:08"},{"start_ms":"130399","end_ms":"135360","text":"looking at um","start_time_text":"2:10"},{"start_ms":"132000","end_ms":"137280","text":"we've got our max function and this is","start_time_text":"2:12"},{"start_ms":"135360","end_ms":"138319","text":"exactly verbatim what what the slide was","start_time_text":"2:15"},{"start_ms":"137280","end_ms":"138800","text":"started with so i'm going to go ahead","start_time_text":"2:17"},{"start_ms":"138319","end_ms":"141440","text":"and","start_time_text":"2:18"},{"start_ms":"138800","end_ms":"143520","text":"compile this so gcc i'm going to use the","start_time_text":"2:18"},{"start_ms":"141440","end_ms":"144959","text":"g flag which gives us debugging symbols","start_time_text":"2:21"},{"start_ms":"143520","end_ms":"147280","text":"which we'll use later","start_time_text":"2:23"},{"start_ms":"144959","end_ms":"148319","text":"and i'm going to use the standard c11","start_time_text":"2:24"},{"start_ms":"147280","end_ms":"150720","text":"i'm not going to print anything with","start_time_text":"2:27"},{"start_ms":"148319","end_ms":"154160","text":"warnings yet we'll come back to that","start_time_text":"2:28"},{"start_ms":"150720","end_ms":"156959","text":"and debug.c all right so","start_time_text":"2:30"},{"start_ms":"154160","end_ms":"159280","text":"program compiled no problem uh i look","start_time_text":"2:34"},{"start_ms":"156959","end_ms":"161680","text":"and i've got an a dot out here so","start_time_text":"2:36"},{"start_ms":"159280","end_ms":"162400","text":"let's see what happens so i'm going to","start_time_text":"2:39"},{"start_ms":"161680","end_ms":"165519","text":"run this","start_time_text":"2:41"},{"start_ms":"162400","end_ms":"168080","text":"all right and uh i want you to look at","start_time_text":"2:42"},{"start_ms":"165519","end_ms":"168080","text":"this output","start_time_text":"2:45"},{"start_ms":"168480","end_ms":"174480","text":"what even is life is printed","start_time_text":"2:48"},{"start_ms":"171519","end_ms":"175200","text":"and you're probably thinking what is","start_time_text":"2:51"},{"start_ms":"174480","end_ms":"177360","text":"going on","start_time_text":"2:54"},{"start_ms":"175200","end_ms":"178239","text":"the world is on fire this can't be","start_time_text":"2:55"},{"start_ms":"177360","end_ms":"180800","text":"happening","start_time_text":"2:57"},{"start_ms":"178239","end_ms":"182159","text":"what is happening here because this is a","start_time_text":"2:58"},{"start_ms":"180800","end_ms":"184400","text":"statement that means there's something","start_time_text":"3:00"},{"start_ms":"182159","end_ms":"186720","text":"wrong with our max function right and if","start_time_text":"3:02"},{"start_ms":"184400","end_ms":"189599","text":"we go back and we look at it","start_time_text":"3:04"},{"start_ms":"186720","end_ms":"191519","text":"it sure looks like well if the first one","start_time_text":"3:06"},{"start_ms":"189599","end_ms":"192239","text":"doesn't print if only in 2020 didn't","start_time_text":"3:09"},{"start_ms":"191519","end_ms":"193840","text":"print","start_time_text":"3:11"},{"start_ms":"192239","end_ms":"195360","text":"then what is it that would cause what","start_time_text":"3:12"},{"start_ms":"193840","end_ms":"197760","text":"even is life to print","start_time_text":"3:13"},{"start_ms":"195360","end_ms":"199440","text":"that seems very strange now i want to","start_time_text":"3:15"},{"start_ms":"197760","end_ms":"201760","text":"make one more small modification to this","start_time_text":"3:17"},{"start_ms":"199440","end_ms":"203920","text":"program before we continue on","start_time_text":"3:19"},{"start_ms":"201760","end_ms":"205280","text":"and show you one other variation of this","start_time_text":"3:21"},{"start_ms":"203920","end_ms":"208319","text":"all right so","start_time_text":"3:23"},{"start_ms":"205280","end_ms":"211120","text":"vim debug.c","start_time_text":"3:25"},{"start_ms":"208319","end_ms":"214159","text":"and uh i'm going to add another print","start_time_text":"3:28"},{"start_ms":"211120","end_ms":"217440","text":"statement here so printf","start_time_text":"3:31"},{"start_ms":"214159","end_ms":"218640","text":"uh what is happening right","start_time_text":"3:34"},{"start_ms":"217440","end_ms":"220799","text":"so i'm going to add another print","start_time_text":"3:37"},{"start_ms":"218640","end_ms":"223120","text":"statement we know that this print","start_time_text":"3:38"},{"start_ms":"220799","end_ms":"225040","text":"statement is definitely going to run","start_time_text":"3:40"},{"start_ms":"223120","end_ms":"226879","text":"and we would expect that okay what is","start_time_text":"3:43"},{"start_ms":"225040","end_ms":"229040","text":"happening prints and then","start_time_text":"3:45"},{"start_ms":"226879","end_ms":"230640","text":"for whatever reason what even is life is","start_time_text":"3:46"},{"start_ms":"229040","end_ms":"231920","text":"printing let's see what happens when we","start_time_text":"3:49"},{"start_ms":"230640","end_ms":"234319","text":"compile this","start_time_text":"3:50"},{"start_ms":"231920","end_ms":"235439","text":"uh and then go run it so i'm gonna do my","start_time_text":"3:51"},{"start_ms":"234319","end_ms":"238840","text":"compilation","start_time_text":"3:54"},{"start_ms":"235439","end_ms":"241840","text":"and dot slash a dot out all right","start_time_text":"3:55"},{"start_ms":"238840","end_ms":"241840","text":"and","start_time_text":"3:58"},{"start_ms":"242400","end_ms":"248560","text":"what is happening only in 2020","start_time_text":"4:02"},{"start_ms":"245519","end_ms":"250799","text":"what even is life","start_time_text":"4:05"},{"start_ms":"248560","end_ms":"252959","text":"and hopefully at this point you're","start_time_text":"4:08"},{"start_ms":"250799","end_ms":"255120","text":"sitting there thinking like","start_time_text":"4:10"},{"start_ms":"252959","end_ms":"256320","text":"what in the world is going on because","start_time_text":"4:12"},{"start_ms":"255120","end_ms":"258639","text":"this is not how","start_time_text":"4:15"},{"start_ms":"256320","end_ms":"259680","text":"our programs should work right this is","start_time_text":"4:16"},{"start_ms":"258639","end_ms":"262320","text":"nothing about this","start_time_text":"4:18"},{"start_ms":"259680","end_ms":"263600","text":"feels good as a software engineer we","start_time_text":"4:19"},{"start_ms":"262320","end_ms":"265840","text":"added a print statement","start_time_text":"4:22"},{"start_ms":"263600","end_ms":"267360","text":"and now we're seeing different behavior","start_time_text":"4:23"},{"start_ms":"265840","end_ms":"269040","text":"in that first print f","start_time_text":"4:25"},{"start_ms":"267360","end_ms":"270960","text":"right so we're going to get to the","start_time_text":"4:27"},{"start_ms":"269040","end_ms":"273120","text":"bottom of what actually is happening","start_time_text":"4:29"},{"start_ms":"270960","end_ms":"275280","text":"here but in order to better understand","start_time_text":"4:30"},{"start_ms":"273120","end_ms":"276400","text":"where our problems are originating from","start_time_text":"4:33"},{"start_ms":"275280","end_ms":"279759","text":"we're going to learn a little bit about","start_time_text":"4:35"},{"start_ms":"276400","end_ms":"279759","text":"debugging in the process","start_time_text":"4:36"},{"start_ms":"280240","end_ms":"286080","text":"so what i'd like you to try doing is","start_time_text":"4:40"},{"start_ms":"283440","end_ms":"287759","text":"pulling today's lecture materials uh and","start_time_text":"4:43"},{"start_ms":"286080","end_ms":"289199","text":"if for some reason you have trouble","start_time_text":"4:46"},{"start_ms":"287759","end_ms":"289919","text":"moving along with today's lecture","start_time_text":"4:47"},{"start_ms":"289199","end_ms":"292000","text":"materials","start_time_text":"4:49"},{"start_ms":"289919","end_ms":"293280","text":"uh just getting uh going back to the","start_time_text":"4:49"},{"start_ms":"292000","end_ms":"295199","text":"previous announcement","start_time_text":"4:52"},{"start_ms":"293280","end_ms":"297040","text":"that was shown in the first slide on the","start_time_text":"4:53"},{"start_ms":"295199","end_ms":"299759","text":"slide deck and getting this","start_time_text":"4:55"},{"start_ms":"297040","end_ms":"300960","text":"repository cloned will be fine but once","start_time_text":"4:57"},{"start_ms":"299759","end_ms":"302639","text":"you start up your","start_time_text":"4:59"},{"start_ms":"300960","end_ms":"304800","text":"container you should be able to change","start_time_text":"5:00"},{"start_ms":"302639","end_ms":"306320","text":"your directory in to be inside of that","start_time_text":"5:02"},{"start_ms":"304800","end_ms":"308560","text":"container","start_time_text":"5:04"},{"start_ms":"306320","end_ms":"310800","text":"to in the lecture directory which is our","start_time_text":"5:06"},{"start_ms":"308560","end_ms":"312160","text":"repository for our exercises that we'll","start_time_text":"5:08"},{"start_ms":"310800","end_ms":"314560","text":"be doing in class","start_time_text":"5:10"},{"start_ms":"312160","end_ms":"315520","text":"uh get pull origin master we'll pull","start_time_text":"5:12"},{"start_ms":"314560","end_ms":"318240","text":"down the latest","start_time_text":"5:14"},{"start_ms":"315520","end_ms":"319840","text":"and this will give you an ls18 directory","start_time_text":"5:15"},{"start_ms":"318240","end_ms":"321440","text":"which i've already changed into so if i","start_time_text":"5:18"},{"start_ms":"319840","end_ms":"326479","text":"were to go back to here","start_time_text":"5:19"},{"start_ms":"321440","end_ms":"326479","text":"and look at my my working directory uh","start_time_text":"5:21"},{"start_ms":"327600","end_ms":"330160","text":"ignore","start_time_text":"5:27"},{"start_ms":"330800","end_ms":"336800","text":"uh right i i cloned this in the wrong","start_time_text":"5:30"},{"start_ms":"334240","end_ms":"338720","text":"directory so i'm actually deeper into my","start_time_text":"5:34"},{"start_ms":"336800","end_ms":"340080","text":"you should ignore all this the point is","start_time_text":"5:36"},{"start_ms":"338720","end_ms":"343199","text":"uh from lecture","start_time_text":"5:38"},{"start_ms":"340080","end_ms":"345520","text":"ls18 gdb that's where you want to be","start_time_text":"5:40"},{"start_ms":"343199","end_ms":"347600","text":"i i actually cloned this earlier today","start_time_text":"5:43"},{"start_ms":"345520","end_ms":"349360","text":"and in my labs directory of another","start_time_text":"5:45"},{"start_ms":"347600","end_ms":"351240","text":"place i'll have to fix that afterwards","start_time_text":"5:47"},{"start_ms":"349360","end_ms":"353600","text":"but you should see a directory ls18","start_time_text":"5:49"},{"start_ms":"351240","end_ms":"355440","text":"ls18-gdb show up","start_time_text":"5:51"},{"start_ms":"353600","end_ms":"358240","text":"and i'm going to give you all a minute","start_time_text":"5:53"},{"start_ms":"355440","end_ms":"358240","text":"to work through","start_time_text":"5:55"},{"start_ms":"358319","end_ms":"362000","text":"these instructions here and we're not","start_time_text":"5:58"},{"start_ms":"360319","end_ms":"363600","text":"yet going to use gdb but i want you to","start_time_text":"6:00"},{"start_ms":"362000","end_ms":"364639","text":"try pulling this seeing if you can","start_time_text":"6:02"},{"start_ms":"363600","end_ms":"366880","text":"compile","start_time_text":"6:03"},{"start_ms":"364639","end_ms":"368240","text":"debug.c and i just am going to open up a","start_time_text":"6:04"},{"start_ms":"366880","end_ms":"370160","text":"separate poll in just a moment while","start_time_text":"6:06"},{"start_ms":"368240","end_ms":"373199","text":"you're working on this to be sure that","start_time_text":"6:08"},{"start_ms":"370160","end_ms":"375440","text":"most of us at least are caught up here","start_time_text":"6:10"},{"start_ms":"373199","end_ms":"378240","text":"and if we uh if if some of you aren't","start_time_text":"6:13"},{"start_ms":"375440","end_ms":"381520","text":"that's totally fine","start_time_text":"6:15"},{"start_ms":"378240","end_ms":"384960","text":"all right i'm seeing about 90","start_time_text":"6:18"},{"start_ms":"381520","end_ms":"386720","text":"of responses are we're able to compile","start_time_text":"6:21"},{"start_ms":"384960","end_ms":"388560","text":"um so i'm going to talk just a little","start_time_text":"6:24"},{"start_ms":"386720","end_ms":"391840","text":"bit more about what's happening here","start_time_text":"6:26"},{"start_ms":"388560","end_ms":"395520","text":"and then we'll move into using gdb next","start_time_text":"6:28"},{"start_ms":"391840","end_ms":"397120","text":"so the gcc compiler has many different","start_time_text":"6:31"},{"start_ms":"395520","end_ms":"399039","text":"options one that we've been using","start_time_text":"6:35"},{"start_ms":"397120","end_ms":"400319","text":"that you've seen before is this dash g","start_time_text":"6:37"},{"start_ms":"399039","end_ms":"402000","text":"option and today we're going to see why","start_time_text":"6:39"},{"start_ms":"400319","end_ms":"404479","text":"it's important","start_time_text":"6:40"},{"start_ms":"402000","end_ms":"405600","text":"normally when you compile a program if","start_time_text":"6:42"},{"start_ms":"404479","end_ms":"407919","text":"you don't specify","start_time_text":"6:44"},{"start_ms":"405600","end_ms":"409360","text":"like this dash g option which means i","start_time_text":"6:45"},{"start_ms":"407919","end_ms":"411680","text":"compile this program with","start_time_text":"6:47"},{"start_ms":"409360","end_ms":"413199","text":"debugging symbols where symbols are","start_time_text":"6:49"},{"start_ms":"411680","end_ms":"415120","text":"things like the names of our functions","start_time_text":"6:51"},{"start_ms":"413199","end_ms":"418160","text":"the names of our variables","start_time_text":"6:53"},{"start_ms":"415120","end_ms":"419680","text":"most of those naming concerns","start_time_text":"6:55"},{"start_ms":"