<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>assembly debugging Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/assembly-debugging/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/assembly-debugging/</link>
	<description></description>
	<lastBuildDate>Mon, 23 Jun 2025 02:42:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>assembly debugging Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/assembly-debugging/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Master CGDB: Boost Your Debugging with GDB’s Curses Interface in Terminal</title>
		<link>https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/</link>
					<comments>https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 15 Jun 2025 23:08:57 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly debugging]]></category>
		<category><![CDATA[C debugging]]></category>
		<category><![CDATA[c++ debugging]]></category>
		<category><![CDATA[CGDB]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[curses interface]]></category>
		<category><![CDATA[debugging tools]]></category>
		<category><![CDATA[GDB]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[terminal debugging]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=158</guid>

					<description><![CDATA[<p>Learn CGDB, a GDB curses layer for terminal debugging. View source code &#038; breakpoints in one window. Ideal for C++, C, &#038; assembly!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/">Master CGDB: Boost Your Debugging with GDB’s Curses Interface in Terminal</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master CGDB: Boost Your Debugging with GDB&#039;s Curses Interface in Terminal" width="1380" height="776" src="https://www.youtube.com/embed/grU1mA3W8K4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Hey coders! In this video, I’m diving into CGDB, a powerful curses layer on top of GDB that makes terminal debugging a breeze. See your source code and breakpoints side-by-side in one clean interface. I’ll walk you through setup, breakpoints, and why CGDB rocks for C++, C, and assembly projects. Watch my GDB video first for the basics (link in comments)! If you dig this, subscribe and drop a comment for more coding tips. Let’s debug smarter! #CGDB #GDB #Debugging #Coding</p>



<p>Introduction to CGDB 00:00:00<br>Recommendation to Watch GDB Video 00:00:05<br>Explanation of CGDB and Curses 00:00:23<br>Building and Running CGDB 00:01:03<br>CGDB Interface Overview 00:01:56<br>Setting Breakpoints in CGDB 00:02:18<br>Managing Breakpoints 00:02:53<br>Benefits of CGDB 00:03:47<br>Closing Remarks and Subscription Request 00:04:00<br>Call for Comments and Suggestions 00:05:30</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>Hey everybody! In this video I&#8217;m going to talk about CGDB which is just a debugging program.</p>



<p>If you haven&#8217;t seen my other video on GDB please watch that first because</p>



<p>I go over all the concepts of debugging an assembly program, a C++ program, a C program</p>



<p>and it&#8217;ll be much more useful than this video.</p>



<p>This video is just about CGDB which is really a curses layer on top of GDB.</p>



<p>on top of GDB. Curses is a library that allows programmers to kind of make</p>



<p>graphical interfaces in the terminal so the point of this video is just to show</p>



<p>you a slightly different way to use GDB that&#8217;s why I&#8217;m saying go check out the</p>



<p>previous GDB video first but you know I think you know sometimes people like CG</p>



<p>to be so what am I talking about for starters pretend that I have a program</p>



<p>I&#8217;ve got an assembly module a C++ module and a make file you don&#8217;t know</p>



<p>or C++, see my other videos.</p>



<p>This video is only about CGDB.</p>



<p>But I can do, you know, make build,</p>



<p>just to build my objects and my executable.</p>



<p>And then normally, if you know GDB,</p>



<p>you can go GDB main and just enter the GDB terminal, right?</p>



<p>This is great, you can do a lot of commands from here,</p>



<p>but it&#8217;s not very graphical.</p>



<p>It does have a graphical mode, to be honest,</p>



<p>but it seems to be kind of glitchy,</p>



<p>so I&#8217;m not really gonna talk about it.</p>



<p>going to talk about it anyway so instead of launching your program inside of gdb you can</p>



<p>launch your program with c gdb c standing for curses i don&#8217;t think this is installed on the</p>



<p>current system so i&#8217;ll probably have to install it right okay so you can go sudo apt install c gdb</p>



<p>and then just enter your password</p>



<p>Let me do clear and then we&#8217;ll do CGDB main.</p>



<p>And I just want you to see that it&#8217;s got two windows.</p>



<p>This is the whole point for me anyway.</p>



<p>The bottom window is actually GDB.</p>



<p>So anything you&#8217;ve learned there is going to apply here.</p>



<p>But the difference is that there&#8217;s a top panel that shows your source code.</p>



<p>So I don&#8217;t know.</p>



<p>Let&#8217;s suppose for the sake of argument, I&#8217;m going to hit escape so I can go up to the top panel and kind of scroll my source code.</p>



<p>Let&#8217;s suppose for the sake of argument, I want to break every time I hit line 36.</p>



<p>time I hit line 36 okay I&#8217;m kind of scrolling I just hit escape to get into the top panel</p>



<p>I&#8217;m gonna hit I for interactive mode which lets me go back down to the bottom panel</p>



<p>and then I&#8217;ll just say B for breakpoints main.cpp line 36 and then just double check that I entered</p>



<p>my breakpoints correctly and then I&#8217;ll run the program again if you don&#8217;t know GDB see my other</p>



<p>video but notice how I started to run the program and then it immediately hit a breakpoint and then</p>



<p>the top pointed to that line where it broke if you don&#8217;t believe me i&#8217;ll do another breakpoint</p>



<p>at line let&#8217;s see main.cpp line maybe uh 32 and then another one at uh 39 well maybe i should</p>



<p>have done 40. yeah i should have done 40 and then 33 so that means i probably want to uh</p>



<p>delete that one at 32 so i&#8217;m gonna delete whichever one shows up at 32 that&#8217;s number two</p>



<p>and then info breakpoints again okay so now if i continue the program also by the way notice how</p>



<p>the top panel is showing red line numbers for my breakpoints if i hit c for continue</p>



<p>and it hits the same breakpoint again if i hit enter to continue one more time it&#8217;ll do that</p>



<p>eventually it hits a different breakpoint and notice how the source code</p>



<p>moves down to the appropriate breakpoint so for me personally i kind of like to just regularly you</p>



<p>and then go back and forth between that and my code window.</p>



<p>But some people really love having both their code and GDB in the same terminal</p>



<p>so they don&#8217;t have to go back and forth between windows.</p>



<p>So if that&#8217;s you, then maybe CGDB is for you.</p>



<p>My little doggy wants to go pee.</p>



<p>Okay, that&#8217;s all I have for this video.</p>



<p>Thank you so much for watching,</p>



<p>and I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p>I&#8217;ll see you in the next video.</p>



<p>Hey, everybody.</p>



<p>Hey everybody!</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>If you could do me a please, a small little favor,</p>



<p>could you please subscribe and follow this channel or these videos</p>



<p>or whatever it is you do on the current social media website</p>



<p>that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos</p>



<p>and grow this community.</p>



<p>So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p>or just i&#8217;ll be able to keep making videos in general so please do do me a kindness and uh and</p>



<p>subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just wake up because i</p>



<p>know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s exactly what</p>



<p>happens every single time so you could do it as a nice favor to me or you could you control me if</p>



<p>you want to just wake me up in the middle of the night just subscribe and then i&#8217;ll i&#8217;ll just wake</p>



<p>up i promise that&#8217;s what will happen also uh if you look at the middle of the screen right now you</p>



<p>of the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p>which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p>main website where you can just kind of like see all the videos i published and the services and</p>



<p>tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p>clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p>up what&#8217;s going on you know just send me a comment whatever i also wake up for those in the middle of</p>



<p>the night i get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to</p>



<p>me i would really appreciate it so again thank you so much for watching this video and um enjoy the</p>



<p>cool music as as i fade into the darkness which is coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/">Master CGDB: Boost Your Debugging with GDB’s Curses Interface in Terminal</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master GNU Debugger: Debug C++ &#038; Assembly Programs with GDB Like a Pro</title>
		<link>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/</link>
					<comments>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 14 Jun 2025 23:53:13 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly debugging]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[breakpoints]]></category>
		<category><![CDATA[c++ debugging]]></category>
		<category><![CDATA[C++ programming]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[coding for beginners]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[debug symbols]]></category>
		<category><![CDATA[debugging tools]]></category>
		<category><![CDATA[GDB]]></category>
		<category><![CDATA[GNU Debugger]]></category>
		<category><![CDATA[null pointer]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=155</guid>

					<description><![CDATA[<p>Master GDB to debug C++ &#038; assembly! Learn breakpoints, call stacks, &#038; crash fixes in this fun guide for coders.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/">Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro" width="1380" height="776" src="https://www.youtube.com/embed/lPcwsvSWak0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Hey everybody! Ready to crush those pesky bugs in your C++ and assembly programs? In this video, we’re diving deep into the GNU Debugger (GDB) – the ultimate tool for tracking down crashes, inspecting variables, and mastering your code. From setting up debug symbols to navigating call stacks, setting breakpoints, and even debugging assembly registers, this fun and relatable guide has you covered. Whether you’re a beginner coder or a seasoned dev, you’ll learn practical tips to level up your debugging game. We’ll also tackle null pointers, create GDB scripts, and explore why your program might be misbehaving. Stick around for a few laughs and some serious skills! Don’t forget to subscribe, hit that like button, and check out my website for more coding tutorials. Let’s debug like pros together!</p>



<p>Introduction to GNU Debugger 00:00:00<br>What is a Debugger 00:00:20<br>Debug Symbols Explanation 00:02:08<br>Compiling with Debug Symbols 00:03:13<br>File Size Comparison 00:05:30<br>Installing and Launching GDB 00:06:21<br>GDB Console Basics 00:06:55<br>Attaching GDB to Program 00:08:12<br>Running Program in GDB 00:09:09<br>Handling Program Crash 00:13:02<br>Understanding Call Stack 00:14:05<br>Navigating Call Stack 00:18:13<br>Inspecting Variables 00:19:08<br>Null Pointer Issues 00:21:25<br>Using GDB Script 00:25:10<br>Setting Breakpoints 00:27:04<br>Managing Breakpoints 00:28:37<br>Continuing Execution 00:30:41<br>Debugging Assembly 00:33:24<br>Global Variables in Assembly 00:35:43<br>Advanced GDB Commands 00:38:31<br>Conclusion and Call to Action 00:40:22</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>Hey everybody! In this video I&#8217;m going to show you how to use the GNU Debugger to</p>



<p>debug your C++ and assembly programs.</p>



<p>You can also use it for C and some other stuff but we&#8217;re just going to focus on</p>



<p>C++ and assembly. What is the GNU Debugger and what is a debugger in the</p>



<p>first place? So for starters the GNU Debugger it&#8217;s like it&#8217;s called GDB and</p>



<p>the db you can imagine that stands for debugger something like that and the g stands for gnu i</p>



<p>personally don&#8217;t know how to uh pronounce gnu so i just say gnu because it it feels more fun to do</p>



<p>it that way it&#8217;s more interesting i think probably some people just say new but i don&#8217;t like that</p>



<p>because the gdb debugger is really old and robust and powerful it&#8217;s not a new tool it&#8217;s it&#8217;s an old</p>



<p>new debugger someone&#8217;s always going to ask what was the old debugger and then I say this is the</p>



<p>old debugger the new debugger is the old debugger anyway so we&#8217;re going to use the GNU debugger</p>



<p>if you haven&#8217;t yet watched my video on caveman debugging you probably want to watch that first</p>



<p>just for like some basics of you know why you might want to look at information in your program</p>



<p>but for now we&#8217;ll just assume that you already know what&#8217;s going on with just like why you would</p>



<p>know how to make make files see my other videos but here i just have a basic make file all it&#8217;s</p>



<p>really going to do is just sort of compile a very simple program and add debug symbols into it so</p>



<p>let&#8217;s double check that i&#8217;m actually doing that here yeah there we go okay so the first thing</p>



<p>that i should probably show you is uh we are compiling a c plus plus source module so i&#8217;ve</p>



<p>actually using c but i i have that left over in there anyway and then we&#8217;re assembling something</p>



<p>in yasm we&#8217;re linking with g plus plus because we want to have a hybrid program and we&#8217;ve got</p>



<p>all these flags here those are just variables and so uh so now before i can debug with gdb</p>



<p>i&#8217;ve got to talk about something called debug symbols what are debug symbols imagine just a</p>



<p>bunch of extra information explaining all of the design and variables and everything inside of your</p>



<p>about your code stuffed directly into the executable.</p>



<p>So imagine, you know, when we compile a C++ source module</p>



<p>into an object file, usually that&#8217;s just, you know,</p>



<p>assembly at that point.</p>



<p>But when we add debug symbols, we&#8217;ll have a bunch of information</p>



<p>that can help the debugger figure out where we are in the program,</p>



<p>what the variables look like, you know,</p>



<p>a bunch of extra information to help the debugger pretty much.</p>



<p>The assembler will put debug symbols into the object code</p>



<p>symbols into the object code that you generate the compilers will do that the linker will do that</p>



<p>and so it&#8217;s important to understand if you don&#8217;t generate debug symbols when you&#8217;re compiling and</p>



<p>linking your code then your debugger won&#8217;t actually be able to do anything it won&#8217;t understand what&#8217;s</p>



<p>going on you can debug it but like you&#8217;re not going to see anything that&#8217;s very uh that&#8217;s useful</p>



<p>so let me show you real fast i&#8217;m going to do make clean and then i&#8217;m going to say make build</p>



<p>that I do not have debug symbols.</p>



<p>All right, okay, if you don&#8217;t know how to compile and link from the command line, see</p>



<p>my other videos, but for now it looks like we&#8217;re not generating debug symbols.</p>



<p>So if I list the contents of my directory here, just note the size is real fast.</p>



<p>I have an assembly module here that I&#8217;m calling ASI.asm, ASI for assembly, and then we&#8217;re</p>



<p>assembling it down to an object file.</p>



<p>Notice how it&#8217;s just 656 bytes, very small.</p>



<p>plus plus module that I&#8217;m creating here is compiling to an object file that is</p>



<p>about four and a half kilobytes so you know not too big the final executable is</p>



<p>called main and it&#8217;s about 17 kilobytes so this is without debug symbols when</p>



<p>you&#8217;re compiling something on your own you need to add extra switches to enable</p>



<p>debug symbols so you can see here in this make file that I&#8217;ve made and again if</p>



<p>you don&#8217;t know how to make a make file or compile or link or anything see my</p>



<p>But for now, I&#8217;m just going to assume you kind of know your way around to makefile,</p>



<p>or at least you can kind of infer what&#8217;s going on.</p>



<p>So I&#8217;ve commented out a different version of this variable for the flags of my C++ compiler.</p>



<p>If I uncomment this and then comment out the other one,</p>



<p>you&#8217;ll notice the only real difference is this part right here, dash G dwarf 2.</p>



<p>Usually in C++ compilation, it&#8217;ll just be dash G to generate debug symbols,</p>



<p>and then the symbol format will default to a format called dwarf2 so I don&#8217;t know for these</p>



<p>tutorials I like to just specify the full format but you can just say dash g in your c++ compiler</p>



<p>and linker we do the same thing for the linker down here so I&#8217;m going to uncomment this</p>



<p>and comment that and the only difference is just g dwarf2 so I&#8217;m just telling my linker that I also</p>



<p>want debug symbols in there too then we do the same thing for the assembler so I&#8217;m going to</p>



<p>So I&#8217;m going to uncomment that and comment this other one.</p>



<p>And the only difference is that in the assembler,</p>



<p>we kind of have to specify it a little bit differently.</p>



<p>Instead of G dwarf dash two,</p>



<p>it&#8217;s just G dwarf two as just one string.</p>



<p>And I think with Yasm, you have to specify the debug symbol format.</p>



<p>So just keep that in mind.</p>



<p>So now I&#8217;ve turned on all the debug symbols.</p>



<p>I&#8217;m going to do real fast, make clean and make build one more time.</p>



<p>And then if I list everything,</p>



<p>list everything let me just double check that it&#8217;s building the symbols now right okay good</p>



<p>notice that the files are bigger so this asio um file is now 1.8 kilobytes instead of you know</p>



<p>half a kilobyte the main.o object file is now 36 kilobytes instead of only 4.5 and the executable</p>



<p>main is 36 kilobytes instead of just 17. so you can feel it already right like there&#8217;s a bunch</p>



<p>extra information sitting inside the object files and the binary which allows us to debug our program</p>



<p>okay so now that we&#8217;ve generated debug symbols in our program we can actually think about starting</p>



<p>to use gdb so gdb is a program just gdb by the command line if you don&#8217;t have it in your system</p>



<p>already you can go sudo apt install gdb or depending on what system you&#8217;re on maybe a or</p>



<p>or whatever your package management program is,</p>



<p>but we just need to install GDB.</p>



<p>On Ubuntu and Debian, I think,</p>



<p>there is a meta package called build essential,</p>



<p>which will give you a bunch of compilation tools</p>



<p>and make file stuff and whatever.</p>



<p>So I&#8217;m just going to assume at this point</p>



<p>you already have GDB on your system.</p>



<p>To launch GDB and go into its console,</p>



<p>just type GDB by itself.</p>



<p>So now we&#8217;re inside of the GDB console or terminal.</p>



<p>console or terminal. We&#8217;re no longer inside of a bash terminal. We can&#8217;t type normal commands.</p>



<p>Let me show you real fast. Here&#8217;s the first command you want to learn on GDB. It&#8217;s called</p>



<p>quit. Never quit. Never surrender. So, you know, normally if you&#8217;re at the command line,</p>



<p>you can do something like cat, etc. OS release just to see what operating system you&#8217;re using.</p>



<p>So you can see for this virtual machine that I&#8217;m using right now, I&#8217;ve got like Ubuntu 22 on there.</p>



<p>If we go inside of GDB, we can&#8217;t use regular bash commands anymore or regular operating</p>



<p>system commands.</p>



<p>We can&#8217;t say cat, et cetera, OS release because we can now only use GDB commands.</p>



<p>We&#8217;re inside of GDB already.</p>



<p>Notice how it says undefined catch command.</p>



<p>I don&#8217;t know what you&#8217;re talking about, dude.</p>



<p>Okay.</p>



<p>So remember we do a Q or quit to get out.</p>



<p>You can actually just type Q.</p>



<p>A lot of commands that I&#8217;m going to be talking about.</p>



<p>going to be talking about you can abbreviate them with one or two or three letters and it totally</p>



<p>works just for speed so now that we know how to enter gdb and now that we also know that we have</p>



<p>already compiled our program with debug symbols we can attach gdb to our program i mean what&#8217;s the</p>



<p>whole point of a debugger we most of the time we will just have our debugger attached to our program</p>



<p>and launch it or i guess launch it while it&#8217;s attached you can also attach to a running program</p>



<p>but we&#8217;re going to launch a program and stay attached to it.</p>



<p>And then we can sort of catch crashes and see what&#8217;s going on.</p>



<p>Or we could also tell the debugger to stop the program at certain points in time,</p>



<p>like when we hit a certain line number or when a variable changes or something like that.</p>



<p>And then we can look around.</p>



<p>We can look at all the variables, look at the state of the program,</p>



<p>even change things if we wanted to.</p>



<p>We can look at all the registers if you&#8217;re coding in assembly.</p>



<p>So it&#8217;s pretty useful.</p>



<p>Anyway, I&#8217;m going to go gdb and the next command we&#8217;re going to learn is file, which just tells</p>



<p>gdb to attach to a program so that we can run it.</p>



<p>So, you know, the name of our program here that we compiled is just called main.</p>



<p>And so if I type file main, then gdb should load up that file and try to parse its debug</p>



<p>symbols so it can begin debugging for you.</p>



<p>Notice how it says reading symbols from main.</p>



<p>you know if we did a typo or we had something with no debug symbols it will give us some sort</p>



<p>of an error message so for now i&#8217;m just going to quit and then go back in just to make sure that</p>



<p>it&#8217;s all clean and we&#8217;re going to do file main now it attaches once it&#8217;s attached that&#8217;s not i guess</p>



<p>it&#8217;s not technically attached because the program&#8217;s not running but once it has loaded the debug</p>



<p>symbols for that program we can run the program with the run command in gdb also kind of interesting</p>



<p>If you have debug symbols that are generated separately from your executable,</p>



<p>you could tell GDB to load the symbols file and then run your program.</p>



<p>That happens a lot in Linux where people release the non-debug friendly version of a program</p>



<p>and then they&#8217;ll release the debug symbols separately if you&#8217;re interested.</p>



<p>Anyway, so I&#8217;m going to type run and it&#8217;ll run the program.</p>



<p>Notice how GDB kind of tells us that it&#8217;s starting up now.</p>



<p>here&#8217;s like a thread debugger that&#8217;s being enabled and it says we&#8217;re using this library to do it and</p>



<p>then it says hello you know here&#8217;s the hello message so the program is actually running that&#8217;s</p>



<p>not my name i wish it was and then it prints the final result and then gdb says your program exited</p>



<p>normally on newer versions of gdb there&#8217;s a setting i think by default where it&#8217;ll ask you</p>



<p>if you want to start a debug daemon a debug info daemon or something yes or no you can usually just</p>



<p>without actually having to type n.</p>



<p>It&#8217;s a little bit faster.</p>



<p>So the whole program ran and finished.</p>



<p>If I type run again, it does the same thing.</p>



<p>In GDB, if you hit enter without typing anything,</p>



<p>it ends up repeating the previous command.</p>



<p>So I&#8217;m not going to type run.</p>



<p>If I just hit enter, are you serious?</p>



<p>GDB made me into a liar.</p>



<p>Well, most of the commands will repeat if you hit enter.</p>



<p>I guess not the run command.</p>



<p>So I&#8217;ll type run again.</p>



<p>And okay, I mean, the program is pretty good, right?</p>



<p>So if you watched my…</p>



<p>It&#8217;s not pretty good.</p>



<p>watched my it&#8217;s not pretty good it&#8217;s a nonsense program I mean it it doesn&#8217;t crash is what I&#8217;m</p>



<p>saying if you watched my previous video which you should on caveman debugging I had this uh you know</p>



<p>nonsense portion of code that just sort of updated a variable and we pretended that we were confused</p>



<p>and we didn&#8217;t understand what was happening and uh we used gdb to debug it so what I&#8217;m going to do</p>



<p>either let&#8217;s say we&#8217;ll start off with throwing an exception and then we&#8217;ll try to use a no pointer</p>



<p>and then we&#8217;ll see a crash and then after that I&#8217;m going to start using breakpoints where we can stop</p>



<p>automatically inside of our program to print the state of the program print the variables</p>



<p>and then I&#8217;m going to do that first in C++ and then hopefully I&#8217;ll remember to do that in</p>



<p>assembly right after it&#8217;s basically the same thing only with assembly you don&#8217;t really have</p>



<p>you know a bunch of variables everywhere you just kind of have registers and maybe some globals</p>



<p>globals but it&#8217;s going to be the same deal okay so let&#8217;s pretend that uh we are gonna have a crash</p>



<p>okay so this program just kind of runs later on we&#8217;ll figure out why the result is wrong we&#8217;ll</p>



<p>just pretend that it&#8217;s wrong but for now we&#8217;ll just say how about we uncomment this line right</p>



<p>here and what will happen is when we&#8217;re running the main loop at some point in the loop we get</p>



<p>When F gets called, the input will be looked at here.</p>



<p>And if the input is more than one,</p>



<p>which is definitely gonna happen like right away,</p>



<p>then we&#8217;ll call on the G function.</p>



<p>Then the G function will just throw an exception.</p>



<p>We&#8217;ll just pretend that something really bad happened.</p>



<p>You crashed or you actually did throw</p>



<p>and you didn&#8217;t catch your throw,</p>



<p>or maybe the system threw something at you</p>



<p>or another library threw something at you</p>



<p>and you didn&#8217;t catch it.</p>



<p>So just basically a program crash.</p>



<p>Let me run this real fast.</p>



<p>So make run just to show you that the program crashes.</p>



<p>Make run at this point is the same thing as just kind of executing the program.</p>



<p>But you can see that it says terminate called after throwing an instance of runtime error.</p>



<p>And then there&#8217;s the message, oh my gosh, and then aborted core dumped.</p>



<p>That&#8217;s not good.</p>



<p>So now let&#8217;s try to understand why.</p>



<p>Why did that crash?</p>



<p>Pretend you don&#8217;t know that it happened in G. You know, you&#8217;re trying to investigate.</p>



<p>So we could type GDB and then we could type a file to load the debug symbols, but it&#8217;s a little bit faster just to type GDB main and just sort of name the file that you want to load right away.</p>



<p>So if I do that, notice how it automatically tries to load the debug symbols from main.</p>



<p>Then I can just type run and then the program tries to run and notice how it actually catches the crash.</p>



<p>If you look down here, terminate called after throwing an instance of runtime error.</p>



<p>throwing an instance of runtime error so that&#8217;s what the program thinks is going on but then the</p>



<p>program is trying to terminate but then down here gdb is like wait wait wait i just caught you know</p>



<p>a termination uh getting invoked here and you can see this is sort of system code like we did not</p>



<p>create this source file p thread kill we didn&#8217;t write that that&#8217;s the standard library and there&#8217;s</p>



<p>a function here called p thread kill implementation with those you know fun c underscore prefixes</p>



<p>prefixes everywhere and so this is like some sort of a c source file and we don&#8217;t have that file on</p>



<p>the system so we can&#8217;t really debug that file lucky for you most of these standard libraries</p>



<p>work all the time so you don&#8217;t really need to debug them you need to debug your code instead</p>



<p>of the standard library code so the question really is how did i get here how did i get to</p>



<p>this crash so the next command we&#8217;ll learn in gdb is just the where command the where command will</p>



<p>I&#8217;m going to release a video in the future that kind of explains call stacks, but basically</p>



<p>imagine a abstract data type, a data structure called a stack. And every time you want to add</p>



<p>data to the stack, it just sort of stacks up on top of the last piece of data. So like if I add</p>



<p>something, let&#8217;s say I want to add the number five, then I would just add the number five on top of</p>



<p>the stack, right? If I wanted to add the number 11, then I would just add the number 11 onto the</p>



<p>off of the stack and the stack is a really good data structure to uh sort of give you a trail of</p>



<p>breadcrumbs to know where you got where did you come from so we call this the call stack because</p>



<p>what&#8217;s happening is every single time you call a function then the new scope and then you know the</p>



<p>new function that you&#8217;re invoking um it has something called a call frame full of information</p>



<p>current address is and all that stuff.</p>



<p>And we&#8217;ll just call that chunk of data that belongs to one instance of a function call.</p>



<p>We&#8217;ll just call that a call frame.</p>



<p>And so if you&#8217;re stacking call frames one on top of the other,</p>



<p>then we call that whole entire thing the call stack.</p>



<p>And this is what allows us to use recursion and all these complicated call graphs and everything.</p>



<p>It&#8217;s really simple, but it&#8217;s really powerful.</p>



<p>So you&#8217;re looking at the call stack.</p>



<p>Each one of these lines is the call frame or is a call frame.</p>



<p>And so for example, if you just look at number zero here, you&#8217;re looking at call frame zero,</p>



<p>which is the closest frame to wherever the crash occurred, which is if you look at it,</p>



<p>that&#8217;s exactly what we were looking at when we saw that original message, right? Call frame zero.</p>



<p>So that&#8217;s not really useful. You want to go down to higher numbered call frames or I guess</p>



<p>physically lowered number call frames and just kind of scan it until you find some code that</p>



<p>raise no abort no none of this stuff is ours live standard c plus plus no the first frame that</p>



<p>contains code that is ours is call frame number nine or i guess like nine away from our current</p>



<p>position so i guess find the physically highest call frame that is your code or the lowest numbered</p>



<p>call frame that is your code and it&#8217;s telling us right here all right we uh we actually seem to</p>



<p>So if I go to 65, right there, it tells you exactly where the crash happened.</p>



<p>If you&#8217;re still trying to figure out, well, how did I get to that crash?</p>



<p>I don&#8217;t know how that actually happened.</p>



<p>Just keep looking down further in the call frames.</p>



<p>The next one, call frame 10, says, well, we were inside of function f with input equals</p>



<p>three at main.cpb line 46.</p>



<p>So if I just go to 46, I can get another clue.</p>



<p>I can go, all right, so for some reason we called g.</p>



<p>Imagine again, this program is much more complicated.</p>



<p>And then if we&#8217;re still confused, we just look down at another one and it&#8217;s like, well, this all this whole mess started at CPP main dot CPP line 28.</p>



<p>So if we look at line 28, it&#8217;s like, well, we were inside of this loop and we we called the F function and then the F function called the G function.</p>



<p>And then G just kind of did a throw. So that gives us a lot of information.</p>



<p>All right. Next thing we can do is we can sort of move up and down the call stack, because right now we&#8217;re sitting at call frame zero, which is just the system code.</p>



<p>which is just the system code, but maybe we don&#8217;t understand why the throw actually happened at,</p>



<p>let&#8217;s say, line 65, right? So we can use up to go up in the call stack to a different call frame.</p>



<p>So if I hit up, notice how it mentions that we&#8217;re now in frame number one instead of zero.</p>



<p>And if I do up again, we&#8217;re now in frame two, which is a little bit closer. We can use the</p>



<p>trick finally where we hit the enter key instead of repeating the command. So I&#8217;m just going to</p>



<p>and hit enter a couple times until we eventually get to our code,</p>



<p>which I think was supposed to be at frame nine.</p>



<p>So I&#8217;m just going to hit it a bunch of times until we get to frame nine.</p>



<p>Now we&#8217;re looking sort of back in time at that point</p>



<p>because all these other frames actually did get invoked,</p>



<p>but we don&#8217;t really care about what was going on inside of them</p>



<p>because they probably did their job correctly.</p>



<p>We&#8217;re looking at our code, so we&#8217;re going back in time</p>



<p>right when we were doing this throw.</p>



<p>Oh no, we threw. Why did we throw?</p>



<p>well there&#8217;s another command in gdb called info locals</p>



<p>okay the info locals uh there&#8217;s there&#8217;s really not much to this maybe maybe let me do this again</p>



<p>a equals five and uh then i&#8217;ll just say a plus plus just so that we have a local variable</p>



<p>i forgot that gdb doesn&#8217;t consider the incoming arguments as locals so there was nothing there</p>



<p>real fast uh gdp oh let&#8217;s do make build and then gdp main and then run and then we have a throw</p>



<p>and then i say where and then i go up up up up to a frame you can also use the keyword down if you</p>



<p>go too far if you want to go you know back down in the call stack but i&#8217;m just going to use up</p>



<p>okay so now we have it here now finally i think i can do info locals just to see the local variables</p>



<p>notice how the a variable is there now because I just added a local variable a a moment ago</p>



<p>you can also print things directly you can say print the input incoming variable if you&#8217;re in</p>



<p>assembly you can also print registers you can say info registers like that and you&#8217;ll get a nice</p>



<p>print out of all the registers you could print individual registers you can say let&#8217;s print</p>



<p>register r12 you can print register r12 as binary by saying I think it&#8217;s p slash t</p>



<p>or print slash t yeah we can probably just do print slash t also</p>



<p>t stands for two because they&#8217;re it&#8217;s a base two number system</p>



<p>does that mean there&#8217;s a p slash h or a p slash f for 15 or 16 s for 16 i don&#8217;t know i haven&#8217;t</p>



<p>tried that is it going to work hang on p slash s bar 12 it&#8217;s not going to work nope didn&#8217;t work</p>



<p>i think it thinks it&#8217;s a character point or something but anyway so we can print a lot of</p>



<p>So we can print a lot of stuff.</p>



<p>And like I said before, we can go up and down the call stack.</p>



<p>So I can go down a couple times and then go back up again.</p>



<p>And now we can assume that we&#8217;ve kind of debugged, you know, where the source of the error was.</p>



<p>So I&#8217;m going to comment that call out to G and run the program again one more time with a different error.</p>



<p>So I&#8217;ve commented this code out from before, but just imagine now that we&#8217;re going to use some pointers.</p>



<p>We&#8217;re going to start off with a pointer called P.</p>



<p>a pointer called p we&#8217;re going to set it to null at the beginning and then we&#8217;re going to just</p>



<p>kind of allocate it and then we&#8217;re going to set you know a value and then we&#8217;re going to dereference</p>



<p>it and dereferencing a pointer just kind of sets the first value in an array you can do that and</p>



<p>then we&#8217;re going to deallocate the pointer and then how about right after that we do something</p>



<p>really really naughty we say p at some index is equal to a five this should be this should be a</p>



<p>a program crash because we deallocated p already at that point we would be dealing with junk data</p>



<p>if we tried to dereference p but to make it even more clear that it&#8217;s naughty we&#8217;ll set it to a no</p>



<p>pointer and then we&#8217;ll try to actually use it so let me let me just restart the program real fast</p>



<p>i&#8217;m gonna do quit yes and then we&#8217;ll do make a debug just so you know my make debug is just a</p>



<p>Let&#8217;s see, where&#8217;s that? Where&#8217;s that? Where&#8217;s that? Do I not have a debug shortcut yet?</p>



<p>Oh dear. I need to add that before this video is over. You know what? I&#8217;ll add it right now.</p>



<p>We&#8217;ll do this run and we&#8217;ll say debug, make debug, and it needs to build first. And then I&#8217;ll say</p>



<p>debugging the program. And then we will do the GDB binary and give it an argument of</p>



<p>the executable and then later we can we can upgrade this to take a little script of commands</p>



<p>because the commands are going to start getting out of control so now if i do make debug if i</p>



<p>didn&#8217;t screw it up now it at least goes in there and i&#8217;ll do run and then we have a seg fault and</p>



<p>notice how it tells us right away hey you have a seg fault right here you&#8217;re accessing invalid</p>



<p>memory let&#8217;s do info locals just to see what&#8217;s up oh the p is actually a zero that&#8217;s a null pointer</p>



<p>explicitly oh whoops you know oh what have I done but it&#8217;s a really good idea to set your</p>



<p>pointers to null after you deallocate because if you don&#8217;t you might end up with undefined</p>



<p>behavior where sometimes the program doesn&#8217;t crash while you&#8217;re overriding junk data or reading junk</p>



<p>data but sometimes it does and you&#8217;re not really sure like why does it work sometimes and why does</p>



<p>it not work sometimes so let me comment this out and see if it&#8217;ll crash uh let&#8217;s just let&#8217;s just</p>



<p>make debug and then i&#8217;ll do run and then if we do info locals oh yeah okay so this is uh</p>



<p>it did seg fault so that&#8217;s good uh if we were unlucky then the program wouldn&#8217;t have crashed</p>



<p>it would have just started behaving strangely and um when we print out the value of p notice how it</p>



<p>looks like a pointer still like if we were to debug the program and print the pointer value</p>



<p>then it doesn&#8217;t really it looks the same it looks the same after we deallocated it right so that</p>



<p>this is called a dangling pointer meaning you deallocated the pointer but you forgot to set</p>



<p>it to null afterwards so then later when you&#8217;re trying to debug the program it&#8217;s a little confusing</p>



<p>because you&#8217;re like that looks like a regular pointer what did i do wrong so um it&#8217;s really</p>



<p>good practice to set your pointers to null right after you deallocate them that way when you&#8217;re</p>



<p>debugging later because something crashed instead of seeing a memory location on p you&#8217;ll see</p>



<p>On P, you&#8217;ll see something that looks definitely like a null pointer and print P.</p>



<p>Then it&#8217;s way easier to realize, oh, whoops, I was trying to use a dead pointer, a pointer that I deallocated.</p>



<p>Hopefully that makes sense so far.</p>



<p>Next thing I want to do is a small upgrade.</p>



<p>I can&#8217;t remember if it&#8217;s dash X or dash EX.</p>



<p>I think it&#8217;s dash X.</p>



<p>But you can write a little script.</p>



<p>I have a little script here called Good Doggo.</p>



<p>called good doggo you can just make any file name it anything you want i&#8217;m so i&#8217;m just naming it</p>



<p>good doggo.txt because my dog&#8217;s taking a little nap behind me as usual um but you could name this</p>



<p>gdb.txt or whatever it is you want so you can put inside of this script file any command that you</p>



<p>want gdb to execute when it launches so let&#8217;s just comment this stuff out real fast and i&#8217;ll do</p>



<p>I&#8217;ll just do run maybe for starters.</p>



<p>Because whenever I&#8217;ve been launching for debug,</p>



<p>I launch with GDB and then I type run manually, right?</p>



<p>So I want to save a little time.</p>



<p>So good doggo is the name of the script.</p>



<p>So when I invoke GDB, I am going to,</p>



<p>where is it right there?</p>



<p>Okay, it&#8217;s GDB.</p>



<p>And then I&#8217;m going to go dot, I think it&#8217;s X.</p>



<p>And then I&#8217;m going to say good doggo.txt.</p>



<p>If it&#8217;s not dash x, then it&#8217;s dash ex.</p>



<p>One of those executes the command that comes after,</p>



<p>and one of those executes the command script that comes after.</p>



<p>Let me just double check that this actually is going to work.</p>



<p>So make debug.</p>



<p>All right, okay, so it was dash x.</p>



<p>Dash x specifies a script that will be run.</p>



<p>Dash ex just specifies a command that you can put right in there.</p>



<p>Like I could have said dash ex run, and it would have ran right away.</p>



<p>But the script is more convenient, right?</p>



<p>Because we can save typing.</p>



<p>So notice how it ran right away and then there&#8217;s a seg fault.</p>



<p>Okay, so now let&#8217;s go a little bit deeper.</p>



<p>Let&#8217;s change this to false again now that we understand like null pointers and all that</p>



<p>stuff.</p>



<p>So we can just basically deal a little bit more with only what the main loop is doing.</p>



<p>Suppose we just still don&#8217;t really understand what the loop is doing, why our answer is</p>



<p>you know, good or bad.</p>



<p>So maybe I want to do a breakpoint at the top of that for loop.</p>



<p>loop gets to the top of its iteration, I want the program to pause so I can look around.</p>



<p>So I&#8217;m going to set up a breakpoint on line 22. And here&#8217;s how you set up breakpoints</p>



<p>in GDB. I&#8217;m going to go ahead and maybe do the terminal for now. And then I&#8217;ll stick that into</p>



<p>the script next. I will not remember that it&#8217;s line 22. That&#8217;s okay. I&#8217;m going to do quit.</p>



<p>And I&#8217;m going to do make build. Then I&#8217;m going to go GDB main just to jump in there.</p>



<p>and then i&#8217;m going to say break or just the letter b just to make a break point you type the name of</p>



<p>the source code file that you want to break in and then a colon and then the line number where</p>



<p>you want it to break so i think i think it actually was line 22 oh i can see it and then</p>



<p>maybe i want to have it break at line i don&#8217;t know maybe right here line 25 right before it increases</p>



<p>And then so I&#8217;m going to do an up arrow to just repeat the command so I can edit it real fast.</p>



<p>And then I&#8217;ll break again at line 28 and then maybe 30 and then 32.</p>



<p>So I&#8217;m going to go 28, 30 and 32.</p>



<p>30 and I&#8217;m just making up break points.</p>



<p>I just want to inspect the program and then maybe I&#8217;ll break at line 36 so I can see what the final answer is.</p>



<p>So I just have a bunch of break points set up.</p>



<p>Remember when the program is running now under GDB, every time it hits one of those lines,</p>



<p>every time it hits one of those lines the program will pause and i&#8217;ll have a chance to look around</p>



<p>at the variables and things so i&#8217;m not sure if i&#8217;ve set up the breakpoints correctly i can say</p>



<p>info breakpoints and it&#8217;ll show me all of my breakpoints notice how you can enable them and</p>



<p>disable them so let&#8217;s say for the sake of argument i want to disable the breakpoint at line 30 because</p>



<p>maybe i don&#8217;t really need that right now but i kind of want to keep it in the system for later</p>



<p>disable four because it&#8217;s number four.</p>



<p>It&#8217;s breakpoint number four.</p>



<p>So I can say disable four.</p>



<p>Then if I do info breakpoints again,</p>



<p>notice how there&#8217;s a little N on there,</p>



<p>meaning breakpoint four is disabled.</p>



<p>So I could re-enable it with a four</p>



<p>and then go info breakpoints again.</p>



<p>So now they&#8217;re all enabled.</p>



<p>I am going to maybe copy this into the script file real fast</p>



<p>so I don&#8217;t have to keep typing this over and over and over again.</p>



<p>Imagine if every single time you changed your program and recompiled it you had to type all of the breakpoints from scratch</p>



<p>That&#8217;s why I want you to use this little breakpoint script</p>



<p>So we&#8217;re going to do B to break at main.cpp line 22 and 25 and</p>



<p>28 and</p>



<p>30 and 32. I should have copy pasted the three dang it 36</p>



<p>And after we&#8217;re done setting up all the breakpoints</p>



<p>show me the breakpoints just so I can have a visual reminder. Keep in mind if you change too</p>



<p>much of your source code then you&#8217;re probably going to have to update these lines and that&#8217;s</p>



<p>okay but it&#8217;s a good idea to just at least you know kind of look at them a little bit so that&#8217;s</p>



<p>why I&#8217;m saying info breakpoints. And so then remember again the makefile all it&#8217;s doing when</p>



<p>I type make debug is it&#8217;s just launching this command right here it&#8217;s just saying gdb with the</p>



<p>with my gdb script so let me quit to get out of this and then i&#8217;m going to say clear and make</p>



<p>debug and notice how it did all that stuff for me it added the breakpoints it showed me the break</p>



<p>points and then it ran the program like i told it to and then it already broke on a breakpoint it</p>



<p>already break it break breakened it&#8217;s broken i don&#8217;t know i don&#8217;t think it&#8217;s broken uh so we&#8217;re</p>



<p>So we&#8217;re on breakpoint number one on line 22.</p>



<p>And here&#8217;s the for loop.</p>



<p>I can type all of my inspection stuff.</p>



<p>I can say info locals.</p>



<p>I can say print I, I can say print A, I can print whatever I want.</p>



<p>And then when I&#8217;m done kind of looking around and inspecting things,</p>



<p>I want to continue the program.</p>



<p>I don&#8217;t want to just stop it here.</p>



<p>So I&#8217;m just going to use the command continue.</p>



<p>And it goes right to the next breakpoint.</p>



<p>If I want to, you know, continue again, I can hit C.</p>



<p>continue again i can hit c or i can just hit enter you know to just sort of keep continuing as i</p>



<p>inspect and print things let&#8217;s see when can i get to break point number one is that what&#8217;s going on</p>



<p>here hang on a second what is break point number one oh i know what to do info break points</p>



<p>break point number one is that main.cpp line 22 is that actually going to get hit oh it&#8217;s</p>



<p>having issues here with my GUI. Okay. Line 22. Okay. So it&#8217;s not really hitting that anymore.</p>



<p>I guess it&#8217;s not going to think that it hits the top of the forelip. Okay. So I guess breakpoint</p>



<p>two is where we&#8217;re always going to be hitting. So let&#8217;s just suppose that I&#8217;m continuing until</p>



<p>I hit breakpoint number two. Suppose that the next 10 times I see breakpoint two, I don&#8217;t</p>



<p>actually want to stop and break on it. I just want to skip the next 10 iterations for some reason.</p>



<p>some reason you can say continue 10 and it&#8217;ll skip the next 10 times that it hits the number two</p>



<p>notice how if i hit continue a bunch of times we&#8217;re not really seeing the break point number</p>



<p>two anymore we&#8217;re seeing other break points and then eventually if i hit it enough times</p>



<p>we&#8217;ll probably see two again oh my god oh my god are we seeing it yet no</p>



<p>Okay, wait, are we seeing I&#8217;m freaking myself out.</p>



<p>Okay, so let&#8217;s continue 10 times on breakpoint three, then we&#8217;ll only see</p>



<p>breakpoint four, I guess.</p>



<p>So we can do continue 10 times on breakpoint four.</p>



<p>What is going on here?</p>



<p>Did I write this program in a weird way?</p>



<p>Hmm.</p>



<p>Well, trust me on this.</p>



<p>Let&#8217;s continue 100 on breakpoint three and then continue 100 on breakpoint four.</p>



<p>breakpoint 4. Okay, so now we&#8217;re just done with the whole entire program. I guess there must have</p>



<p>been a 2 there that I missed. So now we&#8217;re finally on breakpoint 6, which is when everything is</p>



<p>finished. Let me show you real fast that we can add more breakpoints in assembly just to prove</p>



<p>that we can debug assembly real fast. So if I want to debug assembly, I just have to type the name of</p>



<p>that source code file. If it was nested in a folder, like if you had like a complicated hierarchy of</p>



<p>of source code files you would just need to type the relative path but for now I&#8217;m just going to</p>



<p>be able to type the name of the file so assy.asm and let&#8217;s suppose that I want to break at line</p>



<p>16 just so I can see the registers that I set up so I&#8217;m going to do 16 and then I&#8217;m going to break</p>



<p>at assy.asm line 16 and then I&#8217;m going to run and that actually never gets called does it no no it</p>



<p>called does it no no it gets called I think I have a call up here somewhere</p>



<p>nope I don&#8217;t have a call okay let&#8217;s make a call this is gonna throw off all of my</p>



<p>C++ breakpoints so I&#8217;ll just comment them all out right now comment them all</p>



<p>out so I don&#8217;t have to redo them and this is a hybrid program so I&#8217;m gonna go</p>



<p>extern what is it extern C make a block where I name the function the reason we</p>



<p>name mangling so the AC symbol is going to show up as just its regular function</p>



<p>name rather than a bunch of extra stuff indicating the prototype so that we can</p>



<p>do overloading so we&#8217;re just going to disable that and then at the very top of</p>



<p>the program I&#8217;m just going to call it this should hopefully work let me just</p>



<p>do a regular make run just to make sure the whole program compiles okay now</p>



<p>debug script we&#8217;re gonna break on line 16 which is going to be letting us see</p>



<p>the registers okay so I&#8217;m gonna go make debug notice how it hit the breakpoint</p>



<p>in the assembly just the same as the C++ and we can print whatever we want now so</p>



<p>I&#8217;m just gonna say info registers and if we look carefully we can kind of see</p>



<p>that we modified racks REX is 15 so that&#8217;s expected RDI is 20 which is</p>



<p>R12 should be a giant number. Where&#8217;s R12? Right there. So you can see the state of your program at any point in time just by breaking on it.</p>



<p>I&#8217;m breaking on a certain line. So this is tremendously useful for debugging not just for higher level languages but also assembly.</p>



<p>What else can I do? Oh let&#8217;s put a global in the data section. So I&#8217;m gonna do, I don&#8217;t know, my thing or something like that and we&#8217;ll call it a byte array and I&#8217;ll just go hello.</p>



<p>I&#8217;ll say we have a thing one and a thing two.</p>



<p>Something like that.</p>



<p>And maybe thing two is a null terminated string,</p>



<p>whereas thing one is a regular thing.</p>



<p>We should be able to print those symbols.</p>



<p>If that&#8217;s not true, I&#8217;ll come back at a later time</p>



<p>and show you how to do it.</p>



<p>Anyway, so we&#8217;ll do make debug</p>



<p>to just kind of assemble everything again.</p>



<p>And we know how to do info registers.</p>



<p>We can print, you know, one register in particular,</p>



<p>print R12 if we wanted to.</p>



<p>History has not reached.</p>



<p>History has not reached 12.</p>



<p>Oh, I got to put R12.</p>



<p>Yeah, then we can get the, wait a minute, wait a minute.</p>



<p>Oh, I threw off the line numbers because I added the variables up here.</p>



<p>So now we&#8217;re going to break at 21.</p>



<p>Let me just fix that.</p>



<p>21.</p>



<p>We&#8217;ll do a quit.</p>



<p>Make debug again.</p>



<p>And now we&#8217;re at 21.</p>



<p>If I say info registers, we can see R12 is that big value.</p>



<p>you, but we can also print R12 directly. And we should be able to print my thing one, hopefully,</p>



<p>has an unknown type, oh, we have to cast it. So we can do like basic casting in GDB. So I&#8217;m going</p>



<p>to say this is a character pointer. Remember, all of the assembly symbols are basically pointers,</p>



<p>unless it&#8217;s an EQU, EQU variable. In that case, it&#8217;s more of a define. So I&#8217;m going to print</p>



<p>and then it says error cannot access the memory add address</p>



<p>what the heck did i do wrong how about that cannot access that all right i&#8217;ll come back in</p>



<p>another video because this wasn&#8217;t something that i prepared for just to print strings in globals</p>



<p>i&#8217;ll come back at some later date in the future like five years from now and i&#8217;ll just show you</p>



<p>how to print globals but i hope if you&#8217;re in assembly you probably don&#8217;t need to worry about</p>



<p>You just have globals, but the real problem for you is going to be what&#8217;s inside of your registers and so forth.</p>



<p>You can also deref, I think.</p>



<p>Let me see.</p>



<p>Okay, I&#8217;m going to move on from that.</p>



<p>So anyway, we got a script and we got a lot of basic GDB commands.</p>



<p>Is there anything else that I wanted to show you?</p>



<p>Let me just look at my notes real fast here to make sure I&#8217;m not forgetting to say anything.</p>



<p>make sure I&#8217;m not forgetting to say anything.</p>



<p>So we&#8217;re going to do debug symbols, console, quit, attach to binary,</p>



<p>launching the program, uncaught exception, breakpoints, info, delete,</p>



<p>enable, disable, run, continue, end times, info registers, printing.</p>



<p>Oh, we can print a register in binary.</p>



<p>Forgot to show that.</p>



<p>So we are at a breakpoint, I think.</p>



<p>And if we printed R12 with the regular print, we can also do P slash T to just</p>



<p>p slash t to just sort of print it in binary if you want to i think i might have said that already</p>



<p>either that or i said that like a couple days ago to some and then we can do info locals and</p>



<p>then print a variable and then we get the script file and then we can quit okay so i think this is</p>



<p>pretty much everything that i wanted to show you you now have a basic idea of what we can do inside</p>



<p>of gdb but keep in mind there is so much more you can do in gdb than i can fit in one video</p>



<p>can type help and you can see a list of other major you know areas that gdb can handle um you</p>



<p>can do uh let&#8217;s see i think it&#8217;s help and then like we&#8217;ve got information on aliases breakpoints</p>



<p>data you know whatever we can do i think help breakpoints to see more information on breakpoint</p>



<p>commands yeah so all of the things you can do with breakpoints is like all of these commands here</p>



<p>one page we have to enter to see one more page or see to see all of the results without using</p>



<p>paging anymore so i&#8217;m just hit c and so there&#8217;s like you know we can save trace points we can</p>



<p>try to catch exceptions we can enable disable break points there&#8217;s like unwinders which i&#8217;ve</p>



<p>never even used we can bookmark things we can you know start tracing memory we can rethrow</p>



<p>variables so just keep in mind gdp is incredibly powerful this video barely scratches the surface</p>



<p>but for a programmer who is new to debugging i think this will be very useful to you i hope it is</p>



<p>anyway thanks for watching this video i hope you learned a little bit of stuff and had a</p>



<p>little bit of fun um i will see you in the next video rest yourself and take a nap and have fun</p>



<p>and hug your your loved ones okay I&#8217;m out see you later hey everybody thanks</p>



<p>for watching this video again from the bottom of my heart I really appreciate</p>



<p>it I do hope you did learn something and have some fun if you could do me a</p>



<p>please a small little favor could you please subscribe and follow this channel</p>



<p>channel or these videos or whatever it is you do on the current social media</p>



<p>website that you&#8217;re looking at right now it would really mean the world to me</p>



<p>and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p>more videos longer videos better videos or just I&#8217;ll be able to keep making</p>



<p>videos in general so please do do me a kindness and and subscribe you know</p>



<p>sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I</p>



<p>know somebody subscribed or followed it just wakes me up and I get filled with</p>



<p>That&#8217;s exactly what happens every single time.</p>



<p>So you could do it as a nice favor to me,</p>



<p>or you could troll me if you want to just wake me up in the middle of the night.</p>



<p>Just subscribe, and then I&#8217;ll just wake up.</p>



<p>I promise that&#8217;s what will happen.</p>



<p>Also, if you look at the middle of the screen right now,</p>



<p>you should see a QR code, which you can scan in order to go to the website,</p>



<p>which I think is also named somewhere at the bottom of this video.</p>



<p>And it&#8217;ll take you to my main website,</p>



<p>where you can just kind of like see all the videos I published,</p>



<p>and the services and tutorials and things that I offer,</p>



<p>and all that good stuff.</p>



<p>things that I offer and all that good stuff. And, uh, if you have a suggestion for, uh, uh,</p>



<p>clarifications or errata, or just future videos that you want to see, please leave a comment.</p>



<p>Or if you just want to say, Hey, what&#8217;s up, what&#8217;s going on? You know, just send me a comment,</p>



<p>whatever. I also wake up for those in the middle of the night. I get, I wake up in a cold sweat</p>



<p>and I&#8217;m like, it would really, it really mean the world to me. I would really appreciate it. So</p>



<p>watching this video and enjoy the cool music as I fade into the darkness which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/">Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!</title>
		<link>https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/</link>
					<comments>https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Wed, 11 Jun 2025 19:56:00 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly debugging]]></category>
		<category><![CDATA[beginner coding]]></category>
		<category><![CDATA[c++ debugging]]></category>
		<category><![CDATA[c++ tutorial]]></category>
		<category><![CDATA[caveman debugging]]></category>
		<category><![CDATA[code troubleshooting]]></category>
		<category><![CDATA[coding hacks]]></category>
		<category><![CDATA[coding tips]]></category>
		<category><![CDATA[debug c++]]></category>
		<category><![CDATA[debug code fast]]></category>
		<category><![CDATA[fix code bugs]]></category>
		<category><![CDATA[print statement debugging]]></category>
		<category><![CDATA[programming for beginners]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[software development]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=151</guid>

					<description><![CDATA[<p>Master caveman debugging! Easy C++ trick to fix code fast with print statements. Clear tips for beginners &#038; pros. Watch now to squash bugs!</p>
<p>The post <a href="https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/">Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!" width="1380" height="776" src="https://www.youtube.com/embed/hsGaRukvpF8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Hey coders! Ever hit a wall with a program that just won’t work? In this video, I’m spilling the beans on caveman debugging—a super simple, no-fuss way to hunt down bugs in your C++ code (or any language, from assembly to Python)! No need to wrestle with complex debuggers; this method is all about using print statements to trace what your code is doing, step by step.</p>



<p>I’ll show you a real C++ program where things go wrong (think integer overflows and mystery crashes) and walk you through how to sprinkle in cout statements to spot the problem fast. You’ll learn how to label decision points like loops and if-statements, break down complex expressions with temporary variables, and format your debug output so it’s easy to read. Plus, I share a neat trick called short circuiting to toggle your debug prints on or off without deleting them—saving you tons of time!</p>



<p>Whether you’re a beginner just starting out or a pro debugging a massive codebase, caveman debugging is a lifesaver. It’s quick, it’s intuitive, and it fits my “let’s figure this out” vibe perfectly. Stick around to see how I catch a sneaky bug in a for-loop and fix it in minutes.</p>



<p>Why watch?</p>



<ul class="wp-block-list">
<li>Learn a beginner-friendly debugging technique that works in any language.</li>



<li>See real code examples with clear, relatable explanations.</li>



<li>Get tips to make your debug output readable and avoid hours of frustration.</li>



<li>Discover how to short circuit print statements for reusable debugging.</li>
</ul>



<p>If you’re ready to level up your coding game and squash bugs like a champ, smash that play button! Don’t forget to subscribe and hit the bell for more coding tutorials, from beginner hacks to advanced tricks. Drop a comment if you’ve ever used caveman debugging or have a bug you need help with—I read every one! Check out my next video on using a proper debugger for even more debugging goodness.</p>



<p>Scan the QR code on-screen or visit my website (linked below) for more tutorials, services, and coding resources. Let’s keep the coding community growing—your support means the world! #CavemanDebugging #CodingTips #DebuggingHacks</p>



<p>Hey everybody! In this video I&#8217;m gonna try to familiarize you with a concept</p>



<p>called caveman debugging. It&#8217;s just as bad as it sounds but it&#8217;s really really</p>



<p>really useful. So suppose for the sake of argument you&#8217;re writing a program I&#8217;m</p>



<p>gonna show a C++ program here but this this concept is not just for C++</p>



<p>debugging it&#8217;s also for assembly and any other kind of language that you can come</p>



<p>other kind of language that you can come up with but suppose I have a program and I&#8217;m trying to</p>



<p>figure out why it&#8217;s not working so if you look at this program right here that I&#8217;ve kind of written</p>



<p>up already we have a main function here and we have like a little hello message that&#8217;s not my</p>



<p>name I love that name though and we have a variable a and then we just sort of do some nonsense to it</p>



<p>to try and compute a value for a when I say nonsense I mean literally I&#8217;m just I just made</p>



<p>I&#8217;m just I just made up instructions the point of this video is not to show you some advanced program</p>



<p>It&#8217;s just to show you that you know</p>



<p>This is how you can possibly debug a program that has gone awry</p>



<p>So you can imagine at home that this code you&#8217;re seeing right here might be your larger project your larger</p>



<p>Code base whatever it is that you&#8217;re doing that is not working for some reason and you&#8217;re not sure why</p>



<p>So, um, let me run the program real fast</p>



<p>It basically is just like a little for loop and then it just sort of</p>



<p>of you know not randomly but it just kind of increases the value of a for no reason</p>



<p>a bunch of times and then it calls a function that will crash later</p>



<p>so let me show you what happens when we try to run this program okay do do do</p>



<p>okay i want to go clear and make run if you don&#8217;t know how to do a make file or compile</p>



<p>or link or anything like that uh then go see my other videos so clear and make run</p>



<p>What did I do wrong?</p>



<p>TempResult was not declared in this scope.</p>



<p>Okay, well, this is now a video on reading compiler errors.</p>



<p>Line 59, I probably left something out.</p>



<p>Oh, because I just changed it.</p>



<p>I forgot to put it back in there.</p>



<p>Return tempResult, we should just return input times two.</p>



<p>And I&#8217;m just making this up.</p>



<p>It&#8217;s not really like a valid algorithm.</p>



<p>Okay, so we run our program.</p>



<p>We print the final result and we realize for some reason the final result is wrong.</p>



<p>result is wrong. So I don&#8217;t, I mean, I just made this up. So I don&#8217;t really know what the right</p>



<p>answer is. But just imagine that you are looking at some sort of an output, some sort of a final</p>



<p>state, maybe a crash. And you realize, oh, my program doesn&#8217;t work, but it&#8217;s too complicated</p>



<p>to figure out. Caveman debugging to the rescue. Caveman debugging kind of is as bad as it sounds.</p>



<p>It&#8217;s just really, really simple. But it&#8217;s also really useful, especially if you just want to do</p>



<p>on you don&#8217;t want to dive deep into a full-on debugger or do something more advanced it literally</p>



<p>is just printing a lot of things while your program is running i do it all the time i usually</p>



<p>don&#8217;t even go to a debugger unless i really get in trouble with like some huge mess um okay so</p>



<p>the first thing i&#8217;m going to say is in this for loop we uh you know we&#8217;re kind of like iterating</p>



<p>And so probably this is a good idea to announce that we&#8217;re iterating in the for loop.</p>



<p>So I&#8217;m just going to go Cout.</p>



<p>In C++, you know, we have the Cout object that we can use to stream texts to standard output,</p>



<p>which usually goes to the terminal.</p>



<p>So use whatever construct you&#8217;re using for your particular language.</p>



<p>So we&#8217;re going to do Cout.</p>



<p>And I should say also, if you&#8217;re programming in assembly,</p>



<p>if you are programming in assembly,</p>



<p>it&#8217;s a little bit more complicated than just writing a Cout.</p>



<p>little bit more complicated than just writing a C out you probably have to have some pre-computed</p>



<p>messages and then you can use a library to print a number to the screen or use printf to print stuff</p>



<p>to the screen but you can do it just takes a little bit longer so anyway conceptually I&#8217;m</p>



<p>just going to say this is the first iteration of my loop so I&#8217;m going to sort of announce it I&#8217;m</p>



<p>going to say main and then I&#8217;m going to call it main four or how about primary four or first four</p>



<p>but I want to try to label the for loop.</p>



<p>And maybe instead of leaving those parentheses blank,</p>



<p>I could say iteration number i right there,</p>



<p>or maybe I could do iteration i.</p>



<p>That would be pretty good.</p>



<p>Then I just have to replace the i with the actual variable i</p>



<p>so that the i keeps increasing.</p>



<p>So I&#8217;m just going to like do another stream operator here</p>



<p>and I&#8217;m going to say i.</p>



<p>Then I&#8217;ll print a new line.</p>



<p>we&#8217;ve got, you know, one iteration of our loop.</p>



<p>Maybe I should also print what is the current value of A</p>



<p>because the point of this, you know, weird nonsense code</p>



<p>is we&#8217;re coming up with a value for A, right?</p>



<p>So I should just print what A is at the top of each iteration of the for loop.</p>



<p>So I&#8217;m going to say A is equal to, and then do another A at the end of it.</p>



<p>And then maybe at the bottom of the for loop,</p>



<p>when the for loop is actually ending one of its iterations,</p>



<p>I can just say we&#8217;re at the bottom of the iteration so maybe I&#8217;ll put to end</p>



<p>iteration and then print the final value of a and then at the top I&#8217;ll say begin</p>



<p>iteration at this point you might start to feel like hey aren&#8217;t you using too</p>



<p>many words here like why can&#8217;t I just print a bunch of numbers I mean you</p>



<p>could but when you&#8217;re coding you need all the brainpower you can get to not</p>



<p>just code but to debug coding is already hard enough without making it harder</p>



<p>harder for yourself than you need it to be. So, you know, I see people all the time, they&#8217;re</p>



<p>trying to debug their code and they just like start printing numbers. And after not very long</p>



<p>at all, they&#8217;re looking at the debug output and they&#8217;re just like, what did that number mean?</p>



<p>Where is the number I&#8217;m looking for? It&#8217;s just a bunch of numbers. It&#8217;s harder to debug when you</p>



<p>do it that way. Do it the nice and pretty way. It just costs you a couple of extra keystrokes to do,</p>



<p>you know, nice letters and new lines and things, nice words, labels. Isn&#8217;t that still a lot better</p>



<p>Isn&#8217;t that still a lot better than debugging for an extra six hours?</p>



<p>Because you can&#8217;t really understand what you&#8217;re seeing.</p>



<p>Anyway, let me show you what this looks like so far with just those two lines added.</p>



<p>Now we can kind of see, alright, alright, on iteration number 61, where I was 61, then</p>



<p>this was the first value of A and then after that this was the second value of A, you know,</p>



<p>when we were finished with the iteration.</p>



<p>It seems to have gone up by a certain amount.</p>



<p>you know it was like a negative blah blah blah 836 and then negative blah blah blah 708 so it</p>



<p>kind of increased in value all right that&#8217;s a little bit helpful now it&#8217;s a really good uh</p>



<p>place to uh print our decision points so anytime in your code where you have an if or an else or</p>



<p>a function call or anything where you are kind of like you know making a decision or processing your</p>



<p>data in some way it&#8217;s probably a good idea to announce your decisions so i could say up here i</p>



<p>say up here i can go maybe i&#8217;ll say uh main first four and then i&#8217;ll call it uh maybe i&#8217;ll do a</p>



<p>double colon there and i&#8217;ll say top if just to make it easy to read that i&#8217;m kind of like at</p>



<p>the first if statement um maybe i&#8217;ll even put parentheses i&#8217;ll say a is more than 20 just to</p>



<p>kind of like name what i&#8217;m actually doing and then i could replace the a with the actual value of a</p>



<p>actual value of a so i could say a like that and maybe uh when i&#8217;m reading this later i might want</p>



<p>to kind of actually see the variable name so i could say uh you could just like put a as a string</p>



<p>and then maybe its value in parentheses or something like that do a new line um and then</p>



<p>maybe after that top thing i&#8217;ll say uh true to indicate that we&#8217;re inside of the the if statement</p>



<p>the if statement and then maybe at the bottom of that if statement we&#8217;ll just say</p>



<p>a is now true and we&#8217;ll say like a is now you know whatever the new value of a was so just</p>



<p>basically we&#8217;re announcing that we decided to go into this if block and then we announce what we</p>



<p>ended up changing a to and also you probably anytime you see a compound expression like this</p>



<p>You know B time something or a plus something or just you know a big part of multiple parts of the expression any expression</p>



<p>That&#8217;s more than just like one variable or one number. It&#8217;s probably a good idea if you break that up into temporary variables, too</p>



<p>and then</p>



<p>Print out each variable and I know that&#8217;s a lot of stuff to read but again it beats debugging for an extra six hours</p>



<p>I&#8217;m gonna leave that be for now though</p>



<p>leave that be and</p>



<p>I&#8217;m just gonna continue adding comments into the rest of the code. So I&#8217;m gonna do the second if block here</p>



<p>So I&#8217;m going to do the second if block here.</p>



<p>I&#8217;m going to say we&#8217;re in the first floor.</p>



<p>We&#8217;re in the bottom if more than 20 true.</p>



<p>And then we&#8217;ll print out what is the value of a after we&#8217;re finished.</p>



<p>We&#8217;ll say a is now is now whatever value for a.</p>



<p>And then I&#8217;ll do the same thing for this function call.</p>



<p>Because, you know, when I call this function, it&#8217;s going to change the value somehow.</p>



<p>So we&#8217;re going to do cout about to a plus equals f, you know, call to f.</p>



<p>And then inside the arguments, I&#8217;m obviously putting i plus 3.</p>



<p>But because i is a variable, we could just print that.</p>



<p>So I&#8217;m going to do quote, quote, and then just stick the actual i in there.</p>



<p>And then another new line.</p>



<p>a finished a plus equals f and then I&#8217;ll just say you know a is now you know a you know print the</p>



<p>new value of a you could also say a equal equal a and then print the value whatever you want but</p>



<p>the point is I&#8217;m just printing everything here so now let&#8217;s go down a little bit more because</p>



<p>there is a function call if you look at this function call down here here&#8217;s like some nonsense</p>



<p>being used here&#8217;s some other like you know disabled code really what i&#8217;m doing at the</p>



<p>very end is i&#8217;m just taking the input that the function received and i&#8217;m just multiplying it by</p>



<p>two and returning it so again imagine that your code is a lot more complicated than this</p>



<p>maybe input times two is actually input times two divided by three and then some other function</p>



<p>call and then some other this and some other that and whatever so a lot of people will just kind of</p>



<p>stuff like a really complicated expression in one line or one assignment or one return statement</p>



<p>And that&#8217;s super confusing because you can&#8217;t really be sure that every part of the expression is as you thought.</p>



<p>So it&#8217;s a really good idea to break it all up into parts and print each part.</p>



<p>So you could imagine that we have like a C out here and we&#8217;ll say C out, you know, begin F.</p>



<p>I&#8217;ll say like begin for F and then F receives an input of input.</p>



<p>Do an end L.</p>



<p>And then I&#8217;ll make a temporary variable here.</p>



<p>variable here I&#8217;ll say int temp results equals input times two and then I&#8217;ll</p>



<p>print that f and maybe I&#8217;ll do the input again</p>



<p>temp result is now and then say temp result so I&#8217;m just gonna print what the</p>



<p>temp result actually was and I could print something about I just multiplied</p>



<p>thing about I just multiplied the input by two and then you know the more</p>



<p>complicated your expression is the more temporary variables you want to use and</p>



<p>just kind of print every single one of them and print you know this new</p>



<p>variable is the result of dividing by two this new variable is the result of</p>



<p>calling some other function and so forth</p>



<p>so then we have like a basically you know basically a pretty good idea maybe</p>



<p>at the end though instead of multiplying by two again you want to make it more</p>



<p>want to make it more consistent with your debug output so like here temp result is obviously going</p>



<p>to be the result that i return why would you do the expression all over again for the return part</p>



<p>when you could easily get it wrong why not instead just return the temp result that you made right so</p>



<p>then that way your output completely matches what you&#8217;re actually returning and for now i think</p>



<p>that&#8217;s all we need to do let me run it one more time and then you&#8217;ll see a bunch of stuff now uh</p>



<p>Now, we have a lot of information that we can use to trace how our program was thinking and hopefully find the problem.</p>



<p>But isn&#8217;t this like a little bit starting to get a little bit hard to read, right?</p>



<p>You can see that there are kind of blocks happening whenever we iterate.</p>



<p>So, you know, for me, I always try to keep in mind you need all the brainpower you can get.</p>



<p>Why not just format it a little bit better so it&#8217;s really easy to just quickly look at one block and see, you know,</p>



<p>the couts kind of like belong together so i&#8217;m just going to add one more cout in the main for loop</p>



<p>here just at the very bottom i&#8217;m going to go couts and l just so that i get a new line and if i run</p>



<p>the program again notice how it&#8217;s easier to see the blocks like oh that&#8217;s clearly one iteration</p>



<p>right here you know your mind just grabs onto it faster and that increases your brain power and</p>



<p>makes it easier to debug and read the debug output so i can go all right all right okay so</p>



<p>all right okay so we&#8217;re right up here oh around uh iteration um number 83 uh a was this number</p>



<p>and then it got increased by uh this other number and the reason that that happened is because the</p>



<p>function returned 172 so it basically increased it by 172 and then it ended up being that number</p>



<p>oh okay i think i see what went wrong oh no right in this particular program if we scroll up just a</p>



<p>little bit we can probably see that the integer is overflowing right the integer was getting bigger</p>



<p>right the integer was getting bigger and bigger and bigger here and then eventually</p>



<p>it jumped into the negative at some point so right here we can see oh okay when did this</p>



<p>number become negative suppose we didn&#8217;t want it to be negative um what was this this is like</p>



<p>a million this is a billion no wait wait wait this is uh no that&#8217;s a million right there so it&#8217;s like</p>



<p>299 million and then it overflowed oh no no it didn&#8217;t it didn&#8217;t overflow it started at 299</p>



<p>it started at 299 then at the bottom it was sitting at 899 million that&#8217;s kind of close to a billion</p>



<p>and it did jump up from 300 million to 900 million so that&#8217;s a jump up of like 600 million</p>



<p>oh you know it&#8217;s like kind of jumping up faster with each iteration oh and i&#8217;m using a 32-bit</p>



<p>signed integer which has a maximum value of around 2 billion so now i could possibly realize at this</p>



<p>the number is just like it became too big and I need to change my data type or I need to change</p>



<p>my algorithm for some reason right so you can kind of get to the bottom of things pretty quickly</p>



<p>so here&#8217;s another problem that people encounter when they&#8217;re using caveman debugging</p>



<p>basically you you add all these cout statements or print statements or whatever you&#8217;re doing in</p>



<p>whatever language you have and then when you&#8217;re finished you&#8217;re kind of like well I guess uh</p>



<p>I need to delete all of these Couts because I don&#8217;t want all this you know</p>



<p>Junk being printed in my program after I fix the problem or maybe you&#8217;re trying to debug a different part of your program</p>



<p>So then you start commenting out all the print statements or you start</p>



<p>You know deleting them or whatever and then whoops later on you realize maybe that part of my program</p>



<p>Was not actually fixed and I have to add all the Couts all over again from scratch that costs time</p>



<p>So that&#8217;s not fun. So here&#8217;s a trick that I like to use. It&#8217;s called a short circuit</p>



<p>it&#8217;s called a short circuit it&#8217;s a sort of like a beginning C++ thing or just</p>



<p>when you&#8217;re first learning how to use logical operators for boolean</p>



<p>expressions so I&#8217;m gonna do a boolean and I&#8217;m just gonna name it after what</p>



<p>problem I&#8217;m trying to solve so we could just call this you know main C outs or</p>



<p>something like that let&#8217;s I don&#8217;t know primary problem I&#8217;ll call it primary</p>



<p>the boolean as true because then what will happen is uh we can then short circuit all of the cout</p>



<p>statements we can say primary problem couts and cout like that and the way the short circuit works</p>



<p>works is if you have a logical and expression you can see right here we have two sides</p>



<p>of that statement now one&#8217;s on the left which is just the boolean and one&#8217;s on the right which is</p>



<p>have a logical and then you know one and zero is equal to zero true and false is equal to false so</p>



<p>both things have to be true for the expression to evaluate to true which means if the first thing</p>



<p>is false there&#8217;s no point in even looking at the second thing at all because if i if i set that to</p>



<p>false then the second part the c out is it doesn&#8217;t matter the whole expression is going to evaluate</p>



<p>to false anyway so uh the logical operator itself will just you know block out the rest of the</p>



<p>you know, block out the rest of the statement.</p>



<p>That&#8217;s called a short circuit.</p>



<p>So I&#8217;m going to copy the short circuiting to all of my C out statements like this.</p>



<p>Okay.</p>



<p>And do that.</p>



<p>And then the C out there.</p>



<p>And I guess I&#8217;ll leave the final result there.</p>



<p>And for the moment, you know, this is just a regular function.</p>



<p>apply the short circuiting to this other f unless I do a separate boolean but</p>



<p>keep in mind if this was a class and you had two different methods that you</p>



<p>wanted to short circuit see outs inside of at the same time you could just use a</p>



<p>member variable and name it after the problem you&#8217;re currently trying to solve</p>



<p>and then just short circuit each see out in the appropriate method with that</p>



<p>boolean so I guess in this regular program that doesn&#8217;t have a class I</p>



<p>could move the boolean into the global if I wanted to but I&#8217;m not going to right</p>



<p>if I wanted to, but I&#8217;m not going to right now.</p>



<p>And if you&#8217;re writing assembly or some simpler language</p>



<p>and you&#8217;re thinking, hey, I can&#8217;t short circuit an assembly</p>



<p>or some other language, well, you could write a function</p>



<p>that just takes a couple arguments.</p>



<p>You could write a function that takes a string to print</p>



<p>and maybe a number to print or just, you know,</p>



<p>something simple like that.</p>



<p>And then in the function itself that prints,</p>



<p>you could call it debug print if you want.</p>



<p>Have it look at a global variable or define</p>



<p>that you&#8217;ve set up at the top of the assembly module.</p>



<p>set up at the top of the assembly module, which you can just turn on and off,</p>



<p>like just do a data byte and set it to a zero or set it to a one.</p>



<p>And then the function debug print will just look at that variable to decide</p>



<p>whether or not it&#8217;s going to actually print or not.</p>



<p>So, you know, it&#8217;s a little more complicated in assembly,</p>



<p>but you can do it basically the same concept.</p>



<p>Let&#8217;s run this again just to make sure it still works.</p>



<p>Okay, so it still works.</p>



<p>So now suppose I want to turn off all the Cout statements.</p>



<p>If I just change the true to false,</p>



<p>the true to false notice how it&#8217;s all gone except for the stuff inside of f which i told you i</p>



<p>wasn&#8217;t going to touch but notice how easy it was to mute it and then if i want to bring it back</p>



<p>later i just go true to unmute it of course if you want to do a little bit more typing you can</p>



<p>also do if statements like if this condition is true and that condition is true then print the</p>



<p>debug statement so then you can have a more complicated way of muting and not muting but</p>



<p>I&#8217;m going to leave it as is right now.</p>



<p>Let&#8217;s see, what else do we got here?</p>



<p>I think actually at this point, this is the basic idea of caveman debugging.</p>



<p>Just basically print everything.</p>



<p>Print all of your decision points.</p>



<p>Use pretty labels that are really, really easy for you to understand.</p>



<p>Use formatting so that your brain doesn&#8217;t have to struggle to understand what you&#8217;re seeing.</p>



<p>If you just print a bunch of numbers, you&#8217;re going to be shooting yourself in the foot.</p>



<p>you&#8217;re going to be shooting yourself in the foot and use short circuiting or if statements to</p>



<p>selectively mute them so you can do less typing and and debugging the debugging and so forth but</p>



<p>yeah i think i think this is all i really wanted to show you in this particular video</p>



<p>i hope you feel like an expert in caveman debugging i certainly am and it you know it</p>



<p>kind of fits my personality a little bit i&#8217;m like what&#8217;s going on um and like i said before</p>



<p>debugger it&#8217;s probably a better idea but I usually don&#8217;t unless I&#8217;m in big trouble</p>



<p>and I have a huge mess on my hands I usually just go directly to caveman</p>



<p>debugging we&#8217;ll look at the same piece of code in my next video that I&#8217;m going</p>



<p>to publish which is going to talk about using a proper debugger so watch the</p>



<p>next one also but for now I hope you&#8217;ve enjoyed this video thank you for</p>



<p>watching I hope you learned a little bit and had a little bit of fun see you in</p>



<p>video</p>



<p>whoops what the heck</p>



<p>hey everybody thanks for watching this video again from the bottom of my heart</p>



<p>i really appreciate it i do hope you did learn something and have</p>



<p>some fun uh if you could do me a please a small little favor could you please</p>



<p>subscribe and follow this channel or these videos</p>



<p>or whatever it is you do on the current social media website that you&#8217;re looking</p>



<p>at right now it would really mean the world to me and</p>



<p>more videos and grow this community so we&#8217;ll be able to do more videos longer</p>



<p>videos better videos or just I&#8217;ll be able to keep making videos in general so</p>



<p>please do do me a kindness and and subscribe you know sometimes I&#8217;m</p>



<p>sleeping in the middle of the night and I just wake up because I know somebody</p>



<p>subscribed or followed it just wakes me up and I get filled with joy that&#8217;s</p>



<p>exactly what happens every single time so you could do it as a nice favor to me</p>



<p>or you could you control me if you want to just wake me up in the middle of the</p>



<p>and then i&#8217;ll i&#8217;ll just wake up i promise that&#8217;s what will happen also uh if you look at the middle</p>



<p>of the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p>which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p>main website where you can just kind of like see all the videos i published and the services and</p>



<p>tutorials and things that i offer and all that good stuff and uh if you have a suggestion for</p>



<p>clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p>if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also</p>



<p>wake up for those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like</p>



<p>it would really it really mean the world to me i would really appreciate it so again thank you so</p>



<p>much for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



<p>us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/">Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
