lab_debug

Disastrous Debugging

Preface: How Did We Get Here?

Your lab partner is writing an image recognition program. They are working on the tracing algorithm, which turns the image into a trace of the outlines in the image. After going through all the compiler errors (sketchify.cpp:33, etc), the program finally compiles! Overjoyed to have a program, you both decide to test it on a couple images.

Segmentation Fault

Ouch. What do we do now?

Getting Set Up

From your CS 225 git directory, run the following on EWS:

git fetch release
git merge release/lab_debug -m "Merging initial lab_debug files"

If you’re on your own machine, you may need to run:

git fetch release
git merge --allow-unrelated-histories release/lab_debug -m "Merging initial lab_debug files"

Upon a successful merge, your lab_debug files are now in your lab_debug directory.

Make this Yours!

Determining What’s Going Wrong

You could start and open sketchify.cpp and try to figure out what’s happening. This is good for logical bugs – when you only rotate half of your image, for example, or the image doesn’t rotate at all. Walking through what your code does to yourself or your partner is a good exercise in debugging bugs in your algorithm. However, this is often a poor choice for debugging runtime errors or general code bugs. In this case, you should attempt to use the following workflow to debug your code.

Basic Instrumentation: Print (std::cout) Statements!

The easiest way to debug your code is to add print statements. To do this, you can add comments at various points in your code, such as:

std::cout << "line " << __LINE__ << ": x = " << x << std::endl;

The above line prints out the current line number as well as the value of the variable x when that line number executes, for example:

line 32: x = 3

__LINE__ is a special compiler macro containing the current line number of the file.

If you’re getting compiler errors after trying to use std::cout statements, then you need to #include the iostream library like this:

#include <iostream>

Print statements work for debugging in (almost) any language and make repeated debug testing easy – to repeat debug testing with a new change, all you need to do is compile and run the program again. They also require nothing new to learn (smile).

Debugging Your Code

To make and run the code, type the following into your terminal:

make
cp in_01.png in.png
./sketchify

Your First Bug

As you can see, your code caused a Segmentation Fault, or segfault. This happens when you access memory that doesn’t belong to you – such as dereferencing a NULL or uninitialized pointer.

Try adding print statements to lines 39 and 43, before and after the calls to original->readFromFile(), width(), and height().

Use the std::cout statement below on lines 39 and 43.

std::cout << "Reached line " << __LINE__ << std::endl;

Now run sketchify again. You’ll see line 39 print out, but not line 43. This means the segfault occurred sometime between executing lines 39 and 43.

Work on getting your program to run to Line 43!

Bug 2

Once you’ve fixed the first bug, you’ll get another segfault. You’ll want to narrow down the line it’s occurring on and its cause by printing more information. Try putting std::cout statements at the beginning and end of the inner for loop.

More debugging!

Like almost most all code written, the code isn’t perfect. But fixing it is as simple as repeating the above to learn more about what the program is actually doing at runtime so that you can solve the issues. Good luck!

Checking Your Output

Once you think sketchify is working, you can compare your output (out.png) to the expected output by opening each image up using a graphical viewer. If you’re on EWS, you can run the following two commands:

xdg-open out.png &
xdg-open out_01.png &

If you’re working on your own machine, you can open the images using the photo viewer of your choice.

If the outputs differ, you’ve still got a bit more debugging to do – go back and add some print statements to figure out why the outputs differ!

You can also use the compare utility to generate a visual comparison of the images. For this to work, you’ll have to temporarily change your color’s hue to 280, since compare will expect the images to match exactly. Once you’ve recompiled and rerun with that new hue, run the following command:

compare out.png out_01.png comparison.png

Differences between your image and the expected image will be highlighted in red in comparison.png. If you’re on EWS, you can open that image like you did before:

xdg-open comparison.png &

If you’re working on your own machine, you can open comparison.png using the photo viewer of your choice.

Autograder Testing

You can run a subset of the test cases that will be used in the autograder with the following commands:

make test
./test

Submitting Your Work

The following files are used in grading:

  • sketchify.cpp

All other files including any testing files you have added will not be used for grading.

Additional Resources