Debugging, Doubly-Linked Lists, Project 1
Chapter 16
Debugging with GDB
What is GDB?
GDB stands for GNU Project Debugger and was first written in 1986.
A debugging tool for C++ (and other languages).
Lets you step through the code one instruction at a time to see what is happening.
Loads executable files that were compiled with debug information (
-g
).
Compiling a Program for Debugging
g++ -g <source> -o <executable>
Launching and Exiting GDB
gdb
Start GDB.gdb <program>
Start GDB.gdb --args
Start GDB and pass arguments to executable.quit
Exit GDB.
Run a Program to be Debugged
run
Run the executable that was previously set.file
Set the executable to run.
Breakpoints
break <where>
Set a new breakpoint.enable <breakpoint#>
Enable a disabled breakpointdisable <breakpoint#>
Disable a breakpoint.delete <breakpoint#>
Remove a breakpoint.clear
Delete all breakpoints.
Stepping through Lines of Code
step
Go to next instruction (source line), diving into function calls.next
Go to next instruction (source line) in this function.continue
Continue execution until the next breakpoint.finish
Continue until the current function returns.
Variables and Memory
print
Print content of variable/memory location.display
Likeprint
but shows the information with each step.undisplay
Remove thedisplay
with the given number.info locals
Print the local variables or arguments in the current stack frame.
GDB in VS Code
Prerequisites to GDB in VS Code
If running in WSL, install the WSL extension.
Install the C++ extension.
Open the lab/project folder (not just a source file or the whole repository).
Open a C++ file.
Setup Project for GDB in VS Code
Open a C++ File in the Folder.
Click on the “Run” menu at the top.
Click on the “Add Configuration” menu item and select “G++ (GDB/LLDB)”.
Click “Add Configuration” on the bottom right.
Select “C/C++: (gdb) Launch”.
Modify the “name”, “program”, “args”, and “cwd” fields as desired.
https://code.visualstudio.com/docs/cpp/launch-json-reference
Use .gitignore
files
There are often files you do not want to share (executable, debug configuration).
Git will not track files and folders specified in a .gitignore
file.
.vscode/*
obj/*
main
test-runner
Doubly-Linked List
What if we want to traverse the list in reverse?
Three Options:
We can use recursion to iterate to the end and then perform the operation when we “unwind.”
We can use a helper data structure (a stack).
We can implement a doubly-linked list type, where every node has a next and previous pointer.
Example of a doubly-linked list. Example of a doubly-linked list.
Prepending a Doubly-Linked List
Step 0:
Step 1:
Step 2:
pNewNode->pNext
to mpHead
.pNewNode->pNext
to mpHead
.Step 3:
mpHead->pPrev
to pNewNode
.mpHead->pPrev
to pNewNode
.Step 4:
mpHead
to pNewNode
.mpHead
to pNewNode
.Appending a Doubly-Linked List
Step 0:
Step 1:
Step 2:
pNewNode->pNext
to mpTail
.pNewNode->pNext
to mpTail
.Step 3:
mpTail->pNext
to pNewNode
.mpTail->pNext
to pNewNode
.Step 4:
mpTail
to pNewNode
.mpTail
to pNewNode
.Intro. to the Standard Template Library (SLT)
Go here for lecture notes on the STL.
Homework
Lab 9: Doubly-Linked Lists
Project 1: Large Map