CS-211 Fall 2015 Lab 3 Debugging

Getting Started

After you have logged on to one of the Linux Lab machines, open a terminal window like you did last lab. (Go back to the first lab if you forgot how.) Then, download lab3.tar.gz and move it into you home directory. (Note... in some browsers, you may need to right click on the lab3.tar.gz hyperlink, and choose the Save Link As... option in order to download)

Once the tar file is in your home directory, type the command tar -xvzf lab3.tar.gz. This will unpack the tar file and make a new lab3 subdirectory of your home directory. From your home directory, cd to the lab3 directory, and type the command ls to list the files in the lab3 directory. There should be 5 files... one called Makefile, and then four program files called prog1.c, prog2.c, prog3.c, and prog4.c.

Each C file contains code that has bugs in it. This lab consists of using various techniques to find and fix those bugs.

What is a "bug"? A bug is anything in your code that doesn't do what it's "supposed" to do. Certainly, if you have code which generates compiler errors or warnings, that can be thought of as a "bug". However, not all bugs cause compiler errors or warnings! For instance, if you have the code "twoPlusTwo=5;", that code will compile with no errors. However, if you meant to type "twoPlusTwo=4;", then what you actually typed is a bug. You need to look at the descriptions of what the program is supposed to do in these instructions, and use some common sense... what would you expect the program to do in various situations. If the program doesn't do what it's supposed to do, there must be a bug somewhere. If it doesn't do what it ought to do based on common sense, there must be a bug somewhere.

Please keep track of what bugs you find in each program, and how you fixed those bugs. You will be asked to report on the bugs found and fixed in the lab report.

Using The Makefile

The Makefile file contains the recipes to build the C program associated with prog1, prog2, prog3, and prog4. To build an executable, use the make command, and tell it which program to build with a parameter. For instance, type make prog1 to compile prog1.c and build the prog1 binary.

Program 1

Program 1 should print out all the arguments you specify on the command line when you invoke it, then convert the first argument to a number, and, using several "if" statements, comment on the first argument.

Build prog1. Notice that there are compiler warning messages. Try running prog1 by typing ./prog1 10 11 and look at the output. Then edit prog1.c by typing gedit prog1.c&.

Can you fix any of the compiler warnings? Edit the prog1.c file, save it, and rerun make prog1 to test it.

Hint... the compiler warning that states:

prog1.c:9:43: warning: operation on 'i' may be undefined [-Wsequence-point]

... is quite confusing. (Note that this is the message you get an unchanged version of prog1.c. If you have made some changes already, the numbers might be different, but the concepts are the same.) The "9" in the message refers to line 9, which says

printf("Argument %d is : %s\n",i, argv[i++]);

The "43" refers to column 43, which refers to the "i++" part of that line. The compiler issues a warning because you MIGHT be confused about the "side effects" caused by both using "i" as an index into "argv", and (after we used it as an index), incrementing the value of "i". In fact, this code is correct and cannot cause any errors in this situation. In other situations, it might lead to errors. To get rid of the warning message, increment "i" in a new instruction rather than both incrementing "i" and using "i" as an index in the same instruction. Is this a "bug"? Probably not... it will always work in this situation, but it's worth fixing just to get rid of the error message. We will count this as a "bug" for this lab.

Notice that when we print the arguments out, we use the variable "i". The value of i in incremented (i.e. we add one to it), but what is the value of "i" when we start the program?

Notice that prog1 is printing out four arguments, including a null argument, when you run ./prog1 10 11. However, you only passed two arguments. C treats the command itself as an argument, so you should only see three things printed... not four. Can you figure out what is wrong and fix it?

Try various different values for the first parameter. Can you find the value to print out the comment First argument is really big? If not... why not? If you can't figure this out just by looking at the code, run prog1 under gdb. To do this, type gdb prog1. Then, when you get the (gdb) prompt, type break main to set a breakpoint at the beginning of the main function, and then type run 10 11 to start the program with parameters 10 and 11. GDB will stop at the first instruction in the "main" function. You might want to look at the value of i and argc by typing print i and/or print argc. Then type next to go on to line 9. Hit enter to go to the next line (enter repeats the previous gdb command.) and so on to step through the code. Fix any bugs you might find in the code, then quit to exit gdb, save your changes, and make prog1 again, and test your fixes.

Try running prog1 abc. Notice that the atoi function returns 0 for any character string that is not a string of numbers.

Try running prog1 with no parameters... just ./prog1. What happened? Notice that when a segmentation violation occurs, you get a message that says a segmentation fault occured, but no information about how or why a segmentation fault occured. Run under gdb with no breakpoints, and gdb will stop after the segmentation fault. Use the command where to find out where you are in your code when the segmentation fault occurs. Fix the code to print a message about this condition rather than allowing the segmentation fault.

The main part of this lab consists of debugging and fixing program 1. However, if you still have time, work on program 2, program 3, and program 4. Not only will you learn much more about debugging, but you can earn extra credit (up to a maximum of 10 points for the entire lab) if you describe the bugs you find in program 2, 3, and 4 in the lab report.

Program 2

Program 2 should take two parameters - the first is an increment value, and the second is a maximum how this should work. Then try ./prog2 2 10 and ./prog2 3 10. So far so good.

Next try ./prog2 20 400... still OK... what about ./prog2 150 1000? (If yout want to kill your program so it stops printing out garbage, hit Ctrl-C on the keyboard.) To figure out what is going on, run prog2 under gdb... gdb prog2. Then, when the prompt occurs, type break main followed by run 150 1000. Step through the code until you get to line 12. Then print the value of the inc variable. What happened? How can you fix it?

You may have already fixed this problem, but try running prog2 abc 100. If this causes an endless loop, fix this problem as well.

Now try ./prog2 -2 100. If this causes an endless loop, fix this problem as well.

Program 3

Program 3 has a constant character string, which in C is just an array of characters. Program 3 checks to make sure that you enterred at least one argument, and it treats the first letter of the first argument as a letter to change from, and the second letter as a letter to change to. Then, program 3 attempts to change every instance of the "from" letter to the "to" letter. For instance, if the string is This is a test., and the first parameter is ix, then program 3 should print out Thxs xs a test..

Build program 3 and fix any compile problems.

Does program 3 do what it is supposed to do? If not, run under gdb and see if you can figure out what program 3 is doing wrong. Fix these bugs, and see if you can get program 3 to work.

When program 3 is working correctly, you should get the following:

~/lab3> ./prog3 ix
Testval is now: Thxs xs a test... thxs xs only a test... for the next sxxty seco
nds thxs wxll be a test of the emergency broadcastxng system.

Program 4

Program 4 is supposed to count the number of words in a text string. A "word" consists of a string of adjacent non-blank characters. One or more spaces will appear after one word and before the next word.

If you build and run program 4 (it does not take any parameters), you will get The number of words in testval is 102. The number 102 is incorrect, it should be 24. See if you can figure out what the problem is here. (Hint... it can be fixed with a two character change to prog4.c)

Lab Report

Download and edit the following file: lab3_report.txt. Then submit your editted file on Blackboard under the main CS-211 class page (not the lab section page), under "Contents" in the Lab_03 report area.