Tutorial #3: Debugging


Debugging

I. Prepare

First of all, make sure that the GCC tools has been installed on your machine. If them have not been installed, you could use apt-get command with the install option and package name build-essential to install them.

$ su
$ apt-get install build-essential
...

To learn more about them, you can read the manual by typing man command, like man gcc.

We provide the tut1 to help you to be familiar with the operations, which you need to use in the following labs.

Now you need to exit the root user and change to your top-level svn directory which should include the folder used in tutorial-2 (If you have not checkout your svn directory, please do it according to the SVN introduction in Tutorial-2). You could use the svn command with the update option under the svn top-level directory to get tut-debug folder.

$ svn update
A    tut-debug
A    tut-debug/gen_sort.c
A    tut-debug/error_report.txt
A    tut-debug/gen_sort_base
Updated to revision 361.

Change your current directory to tut-debug directory, and use the ls to show the content within it. The output should include three files, error_report.txt, gen_sort.c and gen_sort_base.

$ cd tut-debug
$ ls
error_report.txt  gen_sort.c  gen_sort_base

II. Debug C program

The purpose of this task is to help you to familiar with the development environment in Linux, and introduce you to the general bugs in C code.

Your task is to fix 10 bugs in a console program, ¡°gen_sort¡±, which is written in the source file named "gen_sort.c". ¡°gen_sort¡± generates a raw file with multiple 100 byte records. The first 10 bytes is a random key of 10 printable characters. The next 10 bytes is the record number, 0, 1, ... with leading blanks. The remaining 80 bytes contain varying padding characters.

The bugs include syntax and semantic. We recommend that you should understand the meaning of program before debugging, even though some bugs could be fixed in blind.

You could use the gcc command with the -Wall and -o option to compiling "gen_sort" from "ge_sort.c".

$ gcc -Wall -o gen_sort gen_sort.c
gen_sort.c:58: error: two or more data types in declaration specifiers
gen_sort.c:60: error: expected ¡®=¡¯, ¡®,¡¯, ¡®;¡¯, ¡®asm¡¯ or ¡®__attribute__¡¯ before ¡®static¡¯
gen_sort.c:114: error: expected ¡®)¡¯ before ¡®*¡¯ token
gen_sort.c: In function ¡®fill_buf¡¯:
gen_sort.c:145: error: ¡®record_t¡¯ undeclared (first use in this function)
...

After you fix all syntax bugs, you should further try to fix all semantic bugs. We provide the executable file ¡°gen_sort_base¡± to help you debugging, which is compiled from the correct implementation. The output should be the same since the key is not real random.

You must record the key reasons that help you to fix each bug in the file, named ¡°error_report.txt¡±. For example, if the bug is in syntax, you should record the error output from compiler (gcc). If the bug result crash of program, you should record the error output in console and your analysis to it. If the bug results in the misbehavior of program, you should record the evidence and how you find it. Please follow the structure of the file, and give the description, location and evidence for each bug you fixed like the following example.

/*
 * BUG#0
 * description:
 * miss semicolon
 * location:
 * int a = 10 (line 18)
 * evidence:
 * test.c:19: error: two or more data types in declaration specifiers
 */

After you finish the task, you need to submit your updated files (gen_sort.c and error_report.txt) to svn server using the svn command with the commit(ci) option and the file name. You also could add some comments to this commit operation using --message="blablabla". Note: you don't need to subimit the executable file gen_sort

$ svn ci gen_sort.c error_report.txt --message="fix all bugs in gen_sort.c"
Sending    error_report.txt
Sending    gen_sort.c
Transmitting file data .
Committed revision 362.

Congratulations!




Go to Top // ICS Home Page
Questions or comments regarding ICS course? Send e-mail to the course Staffs or TAs.
Last updated: Sun Mar. 10 2013