Starting the Lab
- Open the
lab02/folder in your preferred text editor. - Open the
lab02/folder in your preferred terminal. - Run
git pull skeleton mainto get the latest version of the skeleton code for this lab.
Debug Exercise 1
I’ve created an amazing program to do my math homework for me; unfortunately, it has a bug in it! It’s supposed to round each calculation to the nearest whole number, but sometimes it gives incorrect results.
Run cargo run --bin debug_exercise_1. You should notice an incorrect result; let’s figure out why.
- First, run
cargo build. This will build binaries/executables for every program in the current project. This step happens automatically whenever you runcargo run, but we’re doing it again for good measure. cargo buildputs the executables in thetarget/folder. Opentarget/debug/. You should see adebug_exercise_1file; that’s the executable we want to debug!- Run
rust-gdb target/debug/debug_exercise_1. This will open a GDB prompt. GDB is a powerful tool for debugging low-level programs: this cheatsheet lists several helpful commands you can use. If anything goes wrong in the next few steps, you can runquitin the GDB prompt to exit GDB, and re-run this command to start from here. - In the GDB prompt, run
layout src. This will let you see the source code fordebug_exercise_1as you debug, which will be very useful in the following steps. If things don’t look right, runrefresh, which often fixes graphical issues in GDB. - We should now set a breakpoint, which is a place where we want to pause the program. Run
break rounded_division, which will set a breakpoint at the beginning of therounded_divisionmethod. Alternatively, you can runbreak 24orb 24, which sets a breakpoint on line 24. - Now that we’ve set a breakpoint, we can start the program. Run
runin the GDB prompt. - The program should stop at the first line of
rounded_division. Let’s print some variables: Runprint topandprint bot. These will tell you the valies of thetopandbotvariables. Now try to runprint quotient: this should give an error, because line 24 has not run yet. - Run
next(orn) to advance to the next line. Then press enter again: GDB interprets a blank line as “run the last command again”, so it should advance again, to line 26. - Print the values of the
quotientandresultvariables. - Run
continue(orc) to resume the program; it should stop on line 24 again, the second timerounded_divisionis called. - Using the
nextandcontinuecommands, go to the point in execution where you think the bug is. Use theprintcommand to confirm your suspicions. - Once you are done debugging, run
quitto exit the GDB prompt (It may ask if you are sure; pressyto confirm).
Now you know how to use rust-gdb! Optionally, you can fix the bug in rounded_division, and re-run the program to see the (now correct) answers to my math homework.
Debug Exercise 2
I’ve created another amazing program, this time to take the sum of elementwise maxes of two arrays. However, this one also has a bug in it.
Run cargo run --bin debug_exercise_2. It will print out Ok(-17), even though the answer should be 15, not -17.
Using rust-gdb, locate the bug and fix it! Here are some GDB commands you might find useful:
break [line number]to set a breakpointrunto start the programnextto advance to the next line (skipping over function calls)stepto advance to the next line (stepping into function calls)continueto resume execution to the next breakpointprint [variable name]to print variablesfinishto jump to the end of the current function
Once you have fixed the bug, re-run the program and verify that it prints Ok(15).
Arithmetic
I’ve learned my lesson from the first two programs; now I always write tests to verify that my code is correct. Unfortunately, my code is not always correct.
Run cargo test arithmetic, which will run the tests in arithmetic.rs. Note that test_sum is failing; something might be wrong with the sum function. Let’s figure out why.
- At the top of the test output, there should be a line like
Running unittests src/lib.rs (target/debug/deps/lab02-0e9653381300a9a7)In parenthesis is the file name of the test binary; this the executable running under the hood when we run
cargo test. Copy that file name. - Run
rust-gdb [filename], using the file name you copied above (i.e.rust-gdb target/debug/deps/lab02-0e9653381300a9a7). This should open a familiar-looking GDB prompt. - In the GDB prompt, run
layout src, andrefreshif necessary. - Run
break test_sumto set a breakpoint, then runrunto start execution. - Debug the test!
- When you’ve located the bug, run
quitto exit GDB.
Fix the bug, and re-run cargo test arithmetic to verify that the tests pass.
IntList
Now that we know how to debug tests, it’s time to incorperate everyone’s favorite data structure: the linked list. I’ve been magically handed a bug-free IntList implementation (see Background), but unfortuately, the code I’ve written using IntLists is not quite so bug-free.
Run cargo test int_list, which will run all the tests in tests.rs. Your task is to fix the following methods in int_list_exercises.rs:
add_constantset_to_zero_if_max_felsquare_primes
Note that set_to_zero_if_max_fel uses several helper methods; the bug(s) may be in the helper methods. If a bug is in a helper method, you should fix the helper method.
Note that square_primes doesn’t fail any tests. Does this mean that it’s bug free? No, it means that there aren’t enough tests. Write a test that square_primes fails, then find and fix the bug in square_primes.