Basic - Debugging Fall 2026
- Submission Instructions
- Preface
- Debugging with GDB (or LLDB)
- Finding memory issues with Valgrind
- Conclusion
Submission Instructions
This homework will be submitted as a repository on the EECS GitLab
server. The repository you submit will be a private Project with
the name/path eecs201-basic-debug with akamil added as a Reporter. The branch trunk
will be the one that is checked.
To finish the submission process and get your submission score, you will need to run the autograder. You will need to SSH into the course server (see Basic - Intro):
local$ ssh uniqname@peritia.eecs.umich.edu
(the local$
referring to a shell prompt on your system)
and then run eecs201-test:
peritia$ eecs201-test basic-debug
(the peritia$
referring to a shell prompt on the server)
Preface
In this homework you’ll be provided yet another zipped archive containing some starter files.
https://www.eecs.umich.edu/courses/eecs201/fa2026/files/assignments/basic-debug.tar.gz
Note that this assignment is to be submitted on a remote branch called
trunk. Initialize a
Git repository inside of the extracted
basic-debug
directory; as noted above
git init -b <branch name>
or
git init --initial-branch <branch name>
can initialize the repo with a different branch name (e.g.
trunk as per the
submission instructions). If your version of Git is too old for these
options, you could create the
trunk branch
afterwards after your first commit, or you can set the local branch’s
tracking information manually. Create a file called
report.txt in this
directory.
Add all of the present files and commit them.
Create a private project named
eecs201-basic-debug
on the EECS GitLab
(gitlab.eecs.umich.edu)
and add the instructor
akamil as a
Reporter. Set this EECS GitLab project as your remote: you’ll be
pushing to it in order to submit.
For this assignment we’ll be utilizing an interactive command-line debugger, your preference of GDB (associated with the GNU project that brings us GCC) or LLDB (associated with the LLVM project that brings us Clang), and Valgrind. If you are on macOS, you might want to look at LLDB since it’s better integrated with macOS. Unfortunately, Valgrind can be a bit of a hassle for macOS: there’s ways to install it (e.g. with Homebrew and the patched version for macOS), but if your Mac has an Apple Silicon chip you might be out of luck, and as of this writing the patched Valgrind don’t support macOS 11 and later when it comes to leak detection due to changes in macOS internals. However, in any case you are free to use the course server (peritia.eecs.umich.edu) to complete this assignment: it has GDB, LLDB, and Valgrind already installed, where these tools will work as expected.
The package names for Ubuntu are
gdb,
lldb, and
valgrind. If you
haven’t already installed
g++, the GNU C++
compiler, you can install the
g++ package.
Debugging with GDB (or LLDB)
-
cdinto the1directory. -
Use
maketo build theseqmeanprogram. What this program does is compute the average of a sequence of integers from 1 to n, where n is a command-line argument. -
Run
$ ./seqmean 3. Note that a segfault occurs. -
Run
$ gdb seqmeanor$ lldb seqmean. This will run GDB or LLDB and it will load theseqmeanexecutable. -
Look at the line above the
(gdb)(if you’re running GDB): notice how it mentions that there are no debugging symbols. Debugging symbols are what GDB and LLDB uses to be able to have a sense of what code “lines” and other things are. -
Let’s get some debugging symbols: exit GDB with
(gdb) quitor(gdb) q, or exit LLDB with(lldb) quitor(lldb) q. From now on, the(gdb)prompt will indicate commands you can enter for GDB and the(lldb)prompt will indicate commands you can enter for LLDB. -
Open the
Makefilein an editor. Set theCFLAGSvariable so that it contains-g. The-gflag will causegccto compile with debugging symbols. -
Use
maketo clean and rebuild the program, and then open the program in GDB or LLDB again. -
To run the program, run
(gdb) run 3,(gdb) r 3,(lldb) run 3, or(lldb) r 3. This will run the loaded program with the given arguments: in this case3. -
GDB/LLDB will catch the program at the segfault, allowing us to investigate the state of the program. It’s kind enough to tell us what line of what file it happened at.
-
With
(gdb) backtrace,(gdb) bt(or(gdb) where), or(lldb) btwe can get a backtrace, which shows the function calls and their stack frames that lead up to the current place. Frame #0 represents the current function and frame #1 represents the caller of that function (and frame #2 would be its caller and so on). -
(gdb) frame,(gdb) f, or(lldb) fwill show the current frame and the line of code it’s at. -
Segfaults (“segmentation faults”) come about due to accessing memory that doesn’t “exist” (or unmapped for you OS folks ;)) or if there is a permission access violation (e.g. writing to a read-only spot). In this line, the only operation that performs a risky memory operation is the struct pointer member access operation (
->). -
That operation looks risky: let’s print out the value of the pointer variable (which is the argument of the function):
(gdb) print d,(gdb) p d,(lldb) print d, or(lldb) p d. (Note:(gdb) frameand(lldb) falso shows the arguments and their values that passed into the function). -
Note that the value of
dis0x0/NULL, a null pointer. Null pointer dereferences are some common ways a segfault can happen. But where did it come from? Look at your backtrace again. -
Let’s switch to the frame of the calling function: run
(gdb) frame 1or(lldb) f 1to investigate it. -
frame/fconveniently prints out the line of code: we can see that at line 30 ofmain.c, aNULL(i.e. null pointer) is passed into the function instead of a pointer to adatastructure. -
Now that we’ve identified the bug, let’s fix it. Exit GDB or LLDB and open
main.cfor editing. Fix line 30 so thatdata_calc_statstakes in a pointer to thedvariable, which is an instance of thedatastructure. -
Rebuild the program and run
$ ./seqmean 3again. There should not be a segfault this time. -
Add and commit the
main.cfile.
Note how the result is incorrect: \(\frac{1 + 2 + 3}{3}\) should equal \(2\), not \(1\). Let’s figure out what’s wrong and fix the bug.
-
Open the program in GDB or LLDB again, and run
(gdb) run 3,(gdb) r 3,(lldb) run 3,(lldb) r 3. -
You’ll see that GDB/LLDB will run the program to completion. GDB/LLDB will remember the arguments you pass along with the run:
(gdb) run/(lldb) runwill cause it to run with3as the argument again. -
It’s not particularly interesting if it runs to completion. Let’s set a breakpoint at
main()to have it stop there: Run(gdb) break main,(gdb) b main,(lldb) breakpoint set --name main,
(lldb) breakpoint set -n main, or(lldb) b main.
You can list out the breakpoints with(gdb) info breakpoint,(gdb) info b,(lldb) breakpoint list. -
Run the program again. It should stop at
main(). -
Let step through the code: run
(gdb) next,(gdb) n,(lldb) next,(lldb) n, or(lldb) thread step-overto step over a line. -
Hit Enter/Return without typing out any command. Entering an empty command will run the previously entered command.
-
Place a breakpoint at line 30 with
(gdb) break 30,(gdb) b 30,
(lldb) b 30,(lldb) breakpoint set --line 30,(lldb) breakpoint set -l 30. Implicitly, without specifying a file, the current file that the current line is in will be referred to when it comes to line numbers and function names.(gdb) break main.c:30,(gdb) break main.c:main,(lldb) b main.c:30, and(lldb) b main.c:mainare examples of explicitly referring to a file. -
Continue execution with
(gdb) continue,(gdb) c,(lldb) continue, or(lldb) c. -
This is line that calls the function that calculates the mean. Step into the function with
(gdb) step,(gdb) s,(lldb) step,(lldb) s,(lldb) thread step-in. -
Let’s see if the sum is calculated properly: place a breakpoint at line 24. Continue so that this breakpoint gets triggered.
-
Print out the
sumvariable. It’s 3! \(1 + 2 + 3 \neq 3\)! -
That’s curious: maybe the data was bad. Let’s investigate the
buffermember ofd: try(gdb) p d->bufferor(lldb) p d->buffer. -
Unsurprisingly, GDB/LLDB prints out the actual pointer value that
bufferholds. We can dereference it:(gdb) p *d->bufferor(lldb) p *d->buffer. -
Well, that gives us only the 0th element. We can get GDB/LLDB to give us elements after it as well:
(gdb) p *d->buffer@3or(lldb) parray 3 d->buffer(or if we’re being really fancy
(gdb) p *d->buffer@d->sizeor(lldb) parray `d->size` d->buffer). -
Now we can see the issue. The buffer should be a sequence of 3 integers starting at 1, not 0: the buffer’s contents should be
{1, 2, 3}. -
I’ll let you figure out the fix for this :) Investigate the C source and header files, and take advantage of whatever debugging knowledge you have.
-
When you’re done with the fix, add and commit the files associated with the fix. Make sure to remove any debug prints that you make!
Finding memory issues with Valgrind
-
cdinto the2directory. -
Build and run the program. You should see
n = 5and0.2 0.4 0.6 0.4 0.2. (Some people’s systems may result in a segfault; we’ll deal with it soon :) ). -
While it may be functionally correct, there may be bad memory usage lurking about which may cause issues if this were a more complicated program.
-
We can use Valgrind (more specifically its Memcheck utility) to analyze the memory usage and access of your program. It’s easy: just run
$ valgrind ./conv. -
You’ll see a lot printed out (which by default goes to
stderr). There is a lot of good info here: we get info about bad writes and reads and there’s a memory leak summary as well. -
First, let’s deal with the memory leaks since those can be fairly easy to deal with. Take a note of the message that says “Rerun with –leak-check=full to see details of leaked memory”, and do that.
-
The resulting heap summary will list out where memory is being lost and is nice enough to tell us the function call stack that associated with the allocations. We can see that the buffer allocated by Signal constructor is never getting deallocated.
-
Fix this memory leak by adding the necessary
delete[]toSignal’s destructor. -
Rebuild and rerun the program with Valgrind. The memory leak should be gone. Now onto the invalid write and read.
-
First, let’s look at the invalid write which occurs in
main.cpp. Note how in theforloop,x[3]is written to due to theforloop condition, writing past the end of the allocated buffer. Change theforloop’s condition to fix this. -
Rebuild and rerun with Valgrind. The invalid write should be gone now. The invalid read is still there, however. Let’s look at the associated line in
conv.cpp. -
The line points to an accumulation:
sum += x[xI] * h[hI]. The culprits of this invalid read would either be thex[xI]orh[hI], as we could be indexing out of the bounds of the buffers for theseSignals. -
If we investigate the code a bit, the
forloop that immediately contains theifstatement checks thatxIis less thanxN, the size ofx’s buffer. Since in C/C++ indexing is 0-indexed the indexing is correct forxsince the last element is at indexxN - 1. -
If we look at the
ifthat controls the accumulation, we can see that it’s checking ifhIis in the bounds ofh. Notice anything wrong here? -
Fix that issue in the
ifcondition. When you rebuild and rerun, you should get a clean report from Valgrind. -
Now that it’s leak and memory error free, add and commit the files that you made fixes for.
Conclusion
-
Add and commit any changes you intend to submit.
-
Fill out the
report.txtfile in the following steps: -
On the first line provide an integer time in minutes of how long it took for you to complete this assignment. It should just be an integer: no letters or words.
-
On the second line and beyond, write down what you learned while doing this assignment. If you already knew how to do all of this, put down “N/A”.
-
Commit your
report.txtfile and push your commits to your remote.s -
Run the autograder!