No pre-lab for this one.
In this lab, you'll get to look at some tools that can make your life as a
programmer easier. The first tool is lint - this is a tool for checking
your C code for problems before it explodes or produces bad results.
Here's a brief
tutorial on lint. It gives you a general idea of the sort of things that
the lint tool can do. We don't have a version of lint itself available on
the lab computers; instead, we have splint. If you enter
man splint
The next tool is probably more important - the gdb command line debugger. This is a pretty primitive tool, and there are a number of different wrappers around it to make it easier to use, but unfortunately, those tools don't appear to be installed on the lab machines. So you'll get a chance to see the basic tool.
Here's a quick tutorial on gdb. For a more complete reference, see the Gnu Debugging with GDB manual, or Peter Jay Salzman's Using Gnu's GDB Debugger manual. These are both very well organized, but they are too verbose for an introduction.
For the lab, first do the lint tutorial, using splint instead of lint. Generate the output, fix the errors identified by splint, and relint the code. Note that the command line parameters for splint differ from those of lint - the command line
splint -varuse -paramuse lint1.c
lint -v -x lint1.c
Answer the questions listed below about lint.
Next, go through the gdb tutorial - make sure you understand how the tool works.
NOTE: the gdb tutorial discusses loading a core file into gdb.
It appears that all user accounts on esus and on the lab machines are currently
set up so that when a program dies (i.e. with a segmentation fault), they do
not "dump core" (generate a core file). Luckily, this is simply due to a
ulimit setting that you can change. At the Unix command prompt, enter
ulimit -c
ulimit -c 500
Remember that this ulimit will be reset to zero each time you log in, so you will need to change it any time you want an executable to be able to create a core file. (You can put the ulimit command in your .bashrc or .profile if you want it to be in effect each time you log in.)
Once you have run through the tutorials, do the following exercise.
Create a link to the executable gdb_example in my CS440 directory; you can do this with the command
ln -s ~bwall/CS440/gdb_example .
Now make sure you've set your ulimit so you can generate a core file, and run the program with no arguments. It should generate a core file. Load the core into gdb and determine where and why it crashed. For the lab report, include a copy of the stack trace showing where the program died and show each of the gdb commands that you used that was helpful in pinpointing the cause of the problem. If you try a bunch of commands and they aren't helpful, you don't need to include them. But for example if you print the value of a variable and that helps you understand what went wrong, include the command and its output in the lab report.
After you've figured out why the program core dumped, quit gdb, and start it
running on gdb_example again. Run the program with the command line
arguments out_file123 5000000
. It should core dump again; figure
out why it died. Include a stack trace and the commands you used to figure out
what went wrong. After you have determined the cause of the error, set a
breakpoint right before the place where it crashes and restart the program
with the same arguments. When it reaches the breakpoint, use gdb to change
the value of an integer variable so that the program will run successfully
(a value of 50 will be fine - the variable to change should be apparent to you
once you figure out why it died). Continue execution of the program and
verify that it successfully runs to completion. Capture the commands you used
to set the breakpoint, restart the program, change the variable, and continue
execution.
Note that there are other capabilities of gdb, including commands to debug multithreaded programs. One of the handiest features when you are working on your network programs is the ability to attach to a running process. If you have started up your TCP server program, you can do something like this:
bwall@esus:~/CS440$ lab1srvr & [1] 12449 bwall@esus:~/CS440$ Server is listening on port 46112. ps x PID TTY STAT TIME COMMAND 2288 ? SN 0:03 sshd: bwall@pts/2 28302 pts/2 SNs 0:00 -bash 12961 ? SN 0:00 sshd: bwall@pts/25 9841 pts/25 SNs 0:00 -bash 12449 pts/16 SN 0:00 lab1srvr 20471 pts/16 RN+ 0:00 ps x bwall@esus:~/CS440$ gdb lab1srvr GNU gdb 6.3-debian Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-linux"...(no debugging symbols found) Using host libthread_db library "/lib/libthread_db.so.1". (gdb) attach 12449 Attaching to program: /students/bwall/CS440/lab1srvr, process 12449 Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done. Loaded symbols for /lib/libc.so.6 Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done. Loaded symbols for /lib/ld-linux.so.2 0x400f7276 in accept () from /lib/libc.so.6 (gdb) c Continuing. Connected to client address 127.0.0.1, port 43325. Receiving message: bd Completed successfully. Program exited normally. (gdb) quit [1]+ Done lab1srvr
There are a lot of other tools out there to assist you with programming -
programs to help find memory problems, tools to analyze the quality of
code (meter), etc. One that you might find helpful if you are working
on freeware code is a source code formatter called indent. This is
available on the lab machines; just do man indent
to see what
you can do. Beware; the program has about a billion different options, but
it can be invaluable if you are stuck maintaining sloppy code.
Your lab report should include answers to the following questions about splint:
For the gdb example, the lab report should include the following information:
Make sure that you are following Anthony's submission guidelines.
The lab write-up is due by the end of the day Wednesday (i.e. 11:59 PM) for the Tuesday lab section and by the end of the day Friday for the Thursday lab section.