MP0
Getting set up
Due dates may be found on the schedule.

This is not a full MP; it is primarily testing your setup and ensuring you are ready for subsequent MPs. It is dramatically easier than other MPs. Don’t plan your time on other MPs based on this one.

1 Set up your environment

Follow the MP page directions to set up VSCode and class-specific github.

2 Fetch starter code

We have some initial code for your mp0 for you to get started. Use the following command in your cs340/netid directory to merge our initial mp0 directory into your local repository:

git fetch release
git merge release/mp0 --allow-unrelated-histories -m "Merging release repository"

3 Use Visual Studio Code

A goal of this MP is to help you feel comfortable with using Visual Studio Code. You should try out the following:

Extra setup for M1/M2 Macs

Some users of M1 and M2 Macs report that they are unable to run the visual debugger with the provided launch.json.

Sample Error Output
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
ERROR: Unable to start debugging. Unexpected LLDB output from command "-exec-run". Command 'exec-run'. Invalid process during debug session
The program ‘/path/to/file/mp0/main' has exited with code 42 (0x0000002a).

To fix this

  1. install the CodeLLDB extension on VSCode
  2. update the type field in your launch.json to reflect this: "type": "lldb"

You may need to make similar changes to the launch.json of subsequent MPs as well.

4 Debug the Code

The purpose of this MP is to get comfortable with Visual Studio Code and viewing C code. The gif.c file provided is based off of Marcel Rodrigues’ ultra-small GIF encoding and decoding library, but then modified to allow us to Illinify any GIF animation.

Unfortunately, there are several bugs in the code that prevent it from running. You will need to use the Visual Studio Code debugger to help find the bugs.

Fixing the bug is a tool to help you learn to use the debugger. To optimize your learning, use the follow common debugging patterns:

4.1 Common Debugging Pattern #1: Segmentation Faults

When you run the provided code, you will immediately have a segmentation fault. When using an interactive debugger, you will see the exact position in code when the segmentation fault occurs.

A segmentation fault is an access to a memory address that is not allocated to your process. Often, the line of code where the segmentation fault occurs is valid code. You may need to inspect the code executed in the lines prior to the segmentation fault.

Other errors in the code, like infinite recursion, can cause a segmentation fault when the process runs out of available memory allocated to the process to add a new stack frame.

You will fix several segmentation faults throughout mp0.

4.2 Common Debugging Pattern #2: Terminal Messages on Abort

A segmentation fault is only one type of exception that can occur in your code. Other exceptions will provide additional information in stdout or stderr reported on your integrated VS Code terminal.

In this bug you will find that the debugger reports *** stack smashing detected ***:

Example stack smashing abort message

Stack smashing occurs when you write onto stack frame of a function, which is the memory immediately following the local variables to the function. Check the code to find out where we might have written a few too many bytes. (This is the one and only place in mp0 where you don’t comment a line to fix a bug, but modify it.)

You will find additional output in the terminal when finding other bugs throughout mp0.

macOS Specific Information The default debugger on macOS, lldb, was not able to give a useable stack trace on this stack smashing bug. To fix this bug, figure out the error on Line 61 – where read is called inside of the read_num function.

4.3 Common Debugging Pattern #3: Examining the Call Stack

When your program is paused (due to an exception, you pressing pause, a breakpoint you set, or any other reason), the Run and Debug window shows a wealth of useful debugging information.

One panel is labeled Call Stack. It will list the function calls that were called leading to where your program is currently paused. You can click on them to jump to their location in code.

Example call stack

In a call stack, the top is the most recent function called. Specifically, the above call stack shows the read_image function located in gif.c was called and the current line being executed is Line 449. This function was invoked by the function below (gd_get_frame), which itself was invoked by the function below that (main), which is the starting point of this execution.

The call stack is very useful for finding infinite recursion if you see the same function name again and again and again. You will want to reference the call stack for several bugs in mp0.

4.4 Common Debugging Pattern #4: Using Pause and Code Stepping

A common programming error is in accidental infinite loop, where no exception occurs but your program is stuck. A common debugging pattern is to pause the execution and examine the code where your program is stuck.

When Visual Studio Code is running your code, you will see the control window:

Pause button and other debugged execution controls

Pressing the left-most Pause button will pause the execution of the program and provide you information about the current point of execution. When the program is paused:

4.5 Common Debugging Pattern #5: Setting Breakpoints

Instead of pausing the program manually, programmers will set a breakpoint to have the program automatically pause immediately before running the line of code where the breakpoint is set.

To set a breakpoint, click on the space immediately to the left of the line number. A bright red dot will appear to indicate that an active breakpoint is set:

Marker indicating a break point set on line 835

In the example above, execution will pause after running Line 834 but before running Line 835. Since it’s paused, you can inspect all of the variables at the exact moment before running Line 835. If you resume the program and the breakpoint is encountered again, it will pause again.

5 Discovering a Hidden Message

Once you have all the bugs debugged, the program will complete and process all of the GIF frames and produce tay-illinify.png – you have just done an Illinify transformation of a Taylor Swift GIF!

Original tay.gif Partial Illinification
tay.gif tay-illinify.gif

Nope.

That’s not enough Illini.

Did it even work?

5.1 Final Fix: A Hidden Message

As your final debugging task, I hid a message for you in the source code. Use your knowledge of Visual Studio Code debugging, find the value stored inside of the global variable message at any time after the _illinify function is called. (Hint: Set a breakpoint; I purposefully made sure the message would not show up if you just use printf.)

Follow the instructions stored in the variable message I hid for you, run the program, and check out how tay-illinify.gif looks now!

6 Course Discord

The primary CS 340 virtual space is the CS 340 Discord server. I want to see your favorite GIF, and make sure you’re all set up with Discord so you can be part of the CS 340 community.

7 Submission and Grading

7.1 Testing Suite

When you are finished working on the MP, you can run a local copy of the same test suite that you will use for grading. To run the test suite:

7.2 Submission

Once you have locally passed all the tests, you will need to submit and grade your code. First commit using the standard git commands:

git add -A
git commit -m "MP submission"
git push

7.3 Grading

The initial grading is done via a manual GitHub Action. You MUST complete this step before the deadline to earn any points for the MP:

7.4 Weight

The test suit is given 75% weight in this MP

Posting an Illinified gif to Discord is given 25% weight for this MP

Frequently-asked questions

What kind of personal access token should I use? We have multiple reports of classic tokens working. Remember to grant it repo-related powers and set it to expire after the semester ends!
What should I do if I get the error re-authorize the OAuth Application GitHub CLI? Run gh auth login.
Do I have to keep typing my personal access token in as a password? No; see our github page for one way around this.