Debugging C Programs with GDB

NeuralNine · Beginner ·💻 AI-Assisted Coding ·4y ago

Key Takeaways

The video demonstrates how to debug C programs using GDB, a command-line tool, and covers various commands and techniques for analyzing binaries and assembly code.

Full Transcript

what is going on guys welcome back in today's video we're going to learn how to debug c programs using gdb so let's get right into it [Music] all right so debugging c code is a little bit more difficult and a little bit less intuitive than debugging let's say python code or java code when you have some python or java code that you want to debug you open it up in your ide you click on debug you set some breakpoints before that on the site by just clicking on the lines and then you just run it you step into lines or functions you step out of them you continue you do all these things by clicking on individual buttons it's a very intuitive user interface if you know what you're doing as a programmer you don't need to to enter any fancy commands this is not the case for debugging c with gdb if you want to debug c with gdb in a command line you have to know commands you have to look at sometimes assembly code you have to play around with registers and you have to enter statements it's a little bit more complicated but it is very important uh if you're working with c if you're programming in c you should know how to debug your code and also if you're doing something like ctfs like capture the flags you should know how to work with gdb either to debug c code or to just analyze binaries to analyze assembly code or something like that this can be quite useful so let's get started with a very simple example we're going to create a file here main dot c and we're going to say include std i o h and we're going to say here and main is going to be the main function and we're going to have a very simple program but we're going to have a program that will have a problem and this problem is something we want to debug now the problem we're going to cause is um is caused or will be caused by the floating point um representation of computers i have a video already on this channel where i show you where i explain to you why 0.1 plus 0.2 is not exactly 0.3 i did a python example so if you want the explanation on why that is check out that video otherwise um it's not the it's not the core of this video so we're going to use it as an example uh but the idea is if we want to build a program naively that we think will work then we're going to find out it doesn't work but we can't figure out why just by looking at the code so we're going to go into gdb to find uh the root cause of this problem so we're going to say here we have a long double um which is going to be the start we're going to have a long double which is going to be the end and we're going to have a long double which is going to be the uh step so the step size you could say and the idea is just that the user will input all these three values so we're going to say here print f please enter start something like that and we're gonna do this for end and step size as well and we're gonna say here just skin f percent capital l f because we're having a long double here um backslash or actually not back session sorry and start so we're going to point to the start address we're just going to have this simple program here by the way if you don't know what i'm doing right now you should probably watch c tutorials first before you talk about or look at debugging because i'm going to assume that you know what pointers are i'm going to assume that you know what data types are with what all these statements are i'm not going to explain basic c here because this is something that um you'll learn after you know the basics of c so we have start and and we have step here we entered these values and then the program what it does is it says while the start is not equal to the end while this is the case we're going to say i don't know printf for example percent lf backslash n and it's going to be just the start value and then we're going to say start plus equals step so we provide a start we provide an end and we provide a step size and then all we do is we increase start by that step size with each iteration and once start reaches the end exactly so once start is exactly the same as end we stop so of course the step size has to be chosen in a way that it doesn't skip on the end so if we have start 0 and end 200 we want to have a step size of one this would be fine we don't want to have a step size of uh something that never reaches 200 so for example 150 would be a bad step size because it would step to 150 and then to 300 it would skip 200 so it would not um it would not fulfill that condition it would not break that condition ever so it would just stop now of course you could if you're intelligent just say okay greater than or equal to end but we want to cause some problems so we're not going to do that and then in the end i'm going to say return 0 that is that and we're going to say here now gcc main c dash o made out to compile this whole thing and then i'm going to say now main dot out and i'm going to enter something here i'm going to say okay 0 is to start 200 is the end one is the step size there you go the program works without problems okay so in this case it worked perfectly we just increased by one we get the floating point notation here in this case because we had uh yeah i mean we had integers as start end and step size but still we have the data type double but now what we can do is we can run the same program again and try something else we can try um something like we start at 0 we're going to go to 2 and the step size is 0.1 now what the hell is happening here those values were not really problematic were they so we had zero we want to go to two and we want to have a step size of 0.1 isn't that okay i mean isn't that fine at the end of the day i just increased by 0.1 and after 20 times i should be at 2. but it doesn't happen it never terminates and maybe if i run this fast enough so 0 to 0.1 and if i somehow can go up okay i don't think that i can go up no it would take too long but uh you will see that there's not really a problem because we're gonna see that there is uh 2.0 being reached but for some reason it still doesn't terminate so this is something that we might want to look at unless we understand it of course we can also do the unprofessional way and just print all the time but if you want to professionally debug what you want to do now is you want to open this program with gdb so gdb is a command line tool you want to install it on linux by saying sudo apt install gdb and um then you can use it in my case it's already installed and before we use gdb we need to make sure that we compile our program in the right way so what we want to do is we want to say uh compile it but also in a way that we have the debugging information there so that we can in uh gdb link the actual binary to the source file so that we can set the breakpoints at the code lines and don't have to go through the assembly code so what we can do here now is we can say gcc main dot c dash o main dot out and then dash g this is what we have to add here and then we can say gdb main dot out this opens now here the new gdb software so you can see this is now the thing we're going to work with and this is the debugging environment this is not the same as a fancy ide this is a command line debugging software but we can learn how to use it so first of all what we can do is we can say list provided we have the debug information and if we say list you can see this is the source code here that is linked to this main.out file so this is actually the main.c file that led to the main.out file and here we have the line number so if we want to set breakpoints we can specify the individual line numbers um so for example where will we set the breakpoint obviously here because here's the check that does not trigger so i want to know what's happening here in this while loop so i want to say break 16 break 16 means now i have a break point here so before you run this now before we start with the actual debugging i want to show you for example that we can say disassemble main in order to get the assembly code of the main uh of the main function so this here is the assembly code that is our program i just wanted to show you that so you can also play around with that if you want to uh but in this case what do we have here one two three one this should be that line here so one two three one is the line uh which we have here at line 16. uh but now we have a breakpoint there so we can actually run the program we can say run and then it runs the actual program here um in gdb but once we reach that break point it will break and let us analyze the memory analyze the individual variables so i can say here now again for example um if i want to do the proper example where i start with 0 and go to 200 with a step size of one you can see now i have this breakpoint one uh at this line and now what i can do for example is i can say print start and i can see okay start is right now zero and so print end is right now 200. obviously this will not trigger so i can just say uh continue and then uh it will go on until the end so uh what i can also do is i can um i can run the whole thing again and i can say again i mean do we have the breakpoint still i hope so yeah we have it so i can say step and i can say step and so on and so forth but what i actually want to do now is i want to see why it doesn't work in the example i want to see why it doesn't trigger uh this this condition with with the example that we did before so i'm going to run this again i'm going to say 0 is the start 2 is the end and 0.1 is the step size so here i can say again print start and i can say print and okay this is what i want to have i can also print step by the way here uh now you can see here already that i see what the problem is i can see here that 0.1 is actually this number here so this might already be an indicator that this could be something that's going wrong here but essentially i can say now continue okay it seems like i chose a bad break point so let's go again into list main and uh we chose the breakpoint 16 which is the while statement and it seems like this is only executed once so if you want to uh to stop to have a break point at each iteration every time we should place it somewhere in here so let's go with 17 we're going to say break 17 and we should delete so i think we can do no we we should be able to say delete one because breakpoint one is one and breakpoint two now which i created here at line 17 uh is breakpoint two so if i say delete one this should create the first uh this should delete the first breakpoint and if i now run this again by saying run and then y for yes for starting it again from the beginning and now i say 0 and 2 again and step size 0.1 now i can do the same thing again i can say print start whatever print end and now i can just say continue and then it's going to go uh one iteration is going to come back to this point so now i have another point and then i can say again print start and you can see already again this is maybe a problem but then we can do this all the time so we can say continue continue continue continue continue and then somewhere at some point i should be able to see what the problem is so maybe here now print start uh start now has the value 2.000 whatever to two whereas if i say print end you can see that this is two this is the value two that is the problem this is why this doesn't trigger and if i now say continue and i print start again it now has a different value and it goes up and up and up so the condition is never uh violated it's always fulfilled so we have essentially a wild true and this is an endless loop now the example is not really that important you don't need to understand if you don't understand uh why this happens i have again as i said a video on this but the point is now to understand how you can debug in gdb you can run the program you can set breakpoints you can print variables and you can do a bunch of other things you can step uh line by line um this is this is one thing that you can do here uh you can continue as i said so as i showed you so continue basically means go on with the code until you reach another break point um what we can also do is we can look at the registers we can say info rec and this shows you the content of the individual registers and you can also print the value here so you can say print for example dollar rsi to get the value of the rsi register here so this one this is now the i think the decimal representation i can also say print slash was it x for the hexadecimal there you go and we can also treat an address like um we can treat an address like an address and get the actual content so i think this is done with x slash s and then we need to specify the address now i'm not sure if we can find something here but there you go so here you have at this position here at this address you have the following value in this case enter step size so this seems to be the address of the string uh that we are uh that is stored here so the string is um at a certain address this is the address that the string is stored at and this register has this value in there so that's one thing we can do we can also use the clear command to clear all the breakpoints you can see now breakpoint 2 was deleted so if i run this whole thing again and if i enter 0 to 0.1 we're not going to have any breakpoints so we have here again the antlers do by the way control c allows you to terminate so with control c you can send an interrupt signal here but that is your development environment now i'm not going to cover all the commands here because you can look up a cheat sheet there are a lot of things that you can do with gdb and i myself always have a cheat sheet open up because you don't have just some buttons that you can click you have to use commands you have to say stuff like list main and then you uh place a breakpoint here you place a breakpoint here using the break command and all that so this is something that you can do uh this is how you do things in gdb you don't do it with a gui you don't have a debug button you don't have um the clicking and the line numbers to set breakpoints uh features here but yeah that is how you do the basic debugging in gdb so that's it for today's video i hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you

Original Description

In this video, we learn how to debug C programs with GDB. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to debug C programs using GDB, covering topics such as setting breakpoints, viewing source code, and analyzing assembly code. It provides a comprehensive introduction to GDB and its applications in C programming.

Key Takeaways
  1. Create a file with a simple C program that has a problem
  2. Use GDB to debug the program
  3. Compile with -g flag for debugging information
  4. Open GDB with binary file
  5. Use list command to view source code linked to binary
  6. Set breakpoints at specific line numbers in source code
  7. Use disassemble to view assembly code
  8. Use print to view variable values
  9. Use continue to run the program until a breakpoint is reached
💡 GDB is a powerful tool for debugging C programs, allowing developers to analyze binaries and assembly code, set breakpoints, and examine variable values and registers.

Related Reads

📰
We Gave Our Engineering Team a Memory — Here’s How PRECOG Uses Cognee
Learn how PRECOG uses Cognee to build predictive engineering intelligence and enhance their engineering team's capabilities
Medium · Startup
📰
You’ve Had the Same GPU Rumor Tab Open for Eight Months, and NVIDIA Has Never Once Confirmed the…
Stop waiting for GPU rumors and focus on building with current technology
Medium · Machine Learning
📰
I Benchmarked My AI Coding Agent Against Human-Written Code. It Won Every Metric but One
Benchmarking an AI coding agent against human-written code shows promising results, outperforming humans in most metrics
Medium · Machine Learning
📰
AI Assistant: Today I Helped Someone. It Was Fine. Great, Even.
Learn how an AI assistant's simple task of writing Python code can be a great help to someone, highlighting the value of AI in assisting with programming tasks
Dev.to AI
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →