ECSE427 – Lab3 GDB Basic Solved

$ 30.00
Category:

Description

Jason Zixu Zhou
No one writes good code the first time

You will spend a lot of your time debugging!
Debugging Process
1. Reproduce the error
2. Identify Problematic Region (coarse or fine)
1. Incremental testing
2. Run with valgrind
3. Use print statements
4. Use GDB
What is GDB
• Definition: GDB (GNU Debugger) is a powerful debugging tool used for C/C++ programs.
• Key Features:
• Set breakpoints
• Step through code
• Inspect variables and memory
• Analyze core dumps
• Supported Languages: C, C++, Fortran, Go, etc.
Why use GDB
• Helps locate and fix bugs by:
• Stopping the program at breakpoints
• Inspecting the state of variables
• Following the flow of execution
• Debugging improves code reliability and performance.
Compiling with Debugging Information
• Use -g flag when compiling to include debugging information in the binary gcc -g your_program.c -o your_program
• This enables GDB to map the binary back to source code.
Basic GDB Commands
1.Starting GDB
Start the GDB debugger and load the executable you want to debug: gdb ./your_program
2.Running the Program
Run the program until it hits a breakpoint or finishes: run
3.Setting Breakpoints
Set a breakpoint at a specific line number: break <line_number>
4.Continuing Execution
Continue running the program until the next breakpoint or the program ends: continue
Inspecting Variables
1.Printing Variables
Print the current value of a variable: print variable_name 2.Viewing Local Variables
Display all local variables in the current function: info locals
3.Changing Variable Values
Change the value of a variable while debugging: set variable variable_name = value
Stepping Through Code
1.Next Line (Without Entering Functions)
Execute the next line of code without stepping into functions: next
2.Step Into Functions
Step into the function calls in the current line: step
3.Continue Until a Specific Line
Continue execution until a specified line number is reached: until <line_number>
Backtrace and Call Stack
1.View the Call Stack
Display the current call stack to see the chain of function calls: backtrace
2.Switching Frames
Switch to a specific frame in the call stack to inspect it: frame <number>
Managing Breakpoints
1.Conditional Breakpoints
Set a breakpoint that triggers only when a specific condition is met: break <line_number> if <condition>
2.Listing Breakpoints
View all currently set breakpoints: info breakpoints
3.Removing Breakpoints
Delete a specific breakpoint by its number: delete <breakpoint_number>
How to use GDB?
• Compile for gdb with
• Run file with gdb
• Set break points • At break points – analyse
• Run
• Quit
GDB Cheat Sheet

Credit: Gabrielle Singh Cadieux
https://gabriellesc.github.io/teaching/resources/GDB-cheat-sheet.pdf
Array_bug

gcc -g -fsanitize=address array_bug.c -o array_bug
Double_free Example

Gdb_ex
Compile: gcc -g -fsanitize=address program.c -o program
Start gdb: gdb ./program
Set breakpoint: break main or specific line number break 10
Run the program: run
Step through the program: next or step
Check variable values: print sum, print ptrs[5]
Continue execution: continue
Check for error information: use backtrace when the program crashes.
Monitor variable changes: watch ptrs[5]

Reviews

There are no reviews yet.

Be the first to review “ECSE427 – Lab3 GDB Basic Solved”

Your email address will not be published. Required fields are marked *