Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 2: Using Valgrind for Memory Debugging in Operating Systems - Prof. B. L. Barnett, Lab Reports of Operating Systems

A lab exercise for cmsc 321: operating systems, where students learn to use valgrind, a memory debugging tool, to identify and fix memory management errors in c/c++ programs. The lab includes three intentionally buggy example programs and instructions for using valgrind to find and correct the errors. Students are required to complete an answer sheet as they progress through the assignments.

Typology: Lab Reports

Pre 2010

Uploaded on 08/16/2009

koofers-user-o7c-1
koofers-user-o7c-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC321:OperatingSystems
Lab222January2007
UsingValgrind
Overview:Inthislab,youwilllearntouseValgrind,averyhandymemorydebuggingtool.Using
Valgrind,youcanfindmanymemorymanagementerrorsinyourprograms,includingmemoryleaks
(moreonmemoryleaksinamoment).SowhyshouldyouuseValgrind?Anentirelistofreasonsis
providedontheValgrindwebsite(http://valgrind.org/info/about.html),butthefollowingis,fromyour
perspective,themostimportant:
Valgrindwillsaveyouhoursofdebuggingtime.
Becausemanyofyouarenewto,oratleastrelativelyuncomfortablewith,dynamicmemory
managementinC/C++,youshouldmakefrequentuseofValgrind.
ExecutingtheLab:OntheAssignmentspageofthecoursewebsite,youwillfindthegzippedtarball
lab2-examples.tgzdownloadandunpackthefileintothedirectoryofyourchoice.Here’sa
quickprimeronhandlingtarfiles:
tarxvzflab2examples.tgzUnpackthegzippedtararchiveinthecurrentdirectory
tarczflab2examples.tgz*.cCreatethegzippedtararchiveofall.cfilesinthecurrent
directory
Tocreateuncompressed.tarfiles,leaveoutthezflag.Tarhasahugenumberofothercapabilities,
whichyoucanlearnaboutfromthetarmanpage.(Historicalnote:thepopular.zipformatisa
combinationoftarandtheUnixcompresscompressionutility.Gzipisamoreeffectivecompression
algorithmthatcontainsnopatentedsoftwaretechnology.NotealsothatJava.jararchivearereallytar
archives,andthecommandlineflagsforthetwotoolsareverysimilar.)
Nowlet’swalkthroughsomeintentionallybuggyexampleprograms,demonstratingtheuseofValgrind
tofindthebugsandhighlightingcommonmemorymistakesalongtheway.
Completeyourelectronicanswersheet(includedinthetarballaslab2Ans.txt)asyoucompletethe
assignmentsbelow.
Program#1:
1.Openprog1.cinyourfavoriteeditor.Carefullyreadthroughthecodeandlocate,butdonot
correct,theprogramerror.(Note:Theerrorisnotacompiletimeerror;itisasubtleruntimeerrorthat,
inthisprogram,won’tevenshowitselfwhenyouexecutetheprogram.)
2.Nowcompiletheprogram.Valgrindrequiresyoutocompilewitha‐gflagtoturndebugging
informationon,e.g.,
g++ -Wall -g -o prog1 prog1.c
pf3
pf4

Partial preview of the text

Download Lab 2: Using Valgrind for Memory Debugging in Operating Systems - Prof. B. L. Barnett and more Lab Reports Operating Systems in PDF only on Docsity!

CMSC 321: Operating Systems Lab 2 — 22 January 2007 Using Valgrind Overview: In this lab, you will learn to use Valgrind, a very handy memory debugging tool. Using Valgrind, you can find many memory management errors in your programs, including memory leaks (more on memory leaks in a moment). So why should you use Valgrind? An entire list of reasons is provided on the Valgrind web site (http://valgrind.org/info/about.html), but the following is, from your perspective, the most important: Valgrind will save you hours of debugging time. Because many of you are new to, or at least relatively uncomfortable with, dynamic‐memory management in C/C++, you should make frequent use of Valgrind. Executing the Lab: On the Assignments page of the course web site, you will find the gzipped tarball lab2-examples.tgz — download and unpack the file into the directory of your choice. Here’s a quick primer on handling tar files: tar xvzf lab2‐examples.tgz Unpack the gzipped tar archive in the current directory tar czf lab2‐examples.tgz *.c Create the gzipped tar archive of all .c files in the current directory To create uncompressed .tar files, leave out the z flag. Tar has a huge number of other capabilities, which you can learn about from the tar man page. (Historical note: the popular .zip format is a combination of tar and the Unix compress compression utility. Gzip is a more effective compression algorithm that contains no patented software technology. Note also that Java .jar archive are really tar archives, and the command line flags for the two tools are very similar.) Now let’s walk through some intentionally buggy example programs, demonstrating the use of Valgrind to find the bugs and highlighting common memory mistakes along the way. Complete your electronic answer sheet (included in the tarball as lab2‐Ans.txt) as you complete the assignments below. Program #1:

  1. Open prog1.c in your favorite editor. Carefully read through the code and locate, but do not correct, the program error. (Note: The error is not a compile‐time error; it is a subtle run‐time error that, in this program, won’t even show itself when you execute the program.)
  2. Now compile the program. Valgrind requires you to compile with a ‐g flag to turn debugging information on, e.g., g++ -Wall -g -o prog1 prog1.c
  1. Execute the program without using Valgrind. You will see that the error doesn’t rear its ugly head (but that doesn’t mean that in bigger, more complex programs it wouldn’t!).
  2. Now run the program using Valgrind: valgrind --tool=memcheck prog (Ask a friend about aliases if you don’t feel like typing ‐‐tool=memcheck every time.) Valgrind will find one error, listing the name of the program, the line on which the error occurred, and the type of error.
  3. Complete question 1 on your answer sheet.
  4. Now correct the program, recompile, and rerun using Valgrind to make sure the error is no longer present. Program #2:
  5. Open prog2.c and try to locate (but do not correct) the error. This error, although not a compile‐time error, can result in a very serious run‐time error.
  6. Compile the program and execute without Valgrind. It is possible that nothing disastrous happens — both arrays are initialized and print out exactly as expected. But this program will blow up just as it is on some systems. Mismanagement of memory (how’s that for alliteration?) is lurking beneath the hood, and can easily come back to bite you.
  7. If the program ran without error on your system, change the value of intArraySize from 10 to 100. Recompile and execute again. This should produce a segmentation fault.
  8. Now run the program again using Valgrind — still a segmentation fault, but at least you have more meaningful feedback. In the Valgrind output, find all references to prog2.c with associated line numbers.
  9. Complete question 2 on your answer sheet — do not just write the error(s) fed back by Valgrind; I want a code‐specific explanation. You may need to refer back to the original code.
  10. Now correct all errors in the program, recompile, and rerun using Valgrind to make sure no errors are present.
  11. Remove any existing core files present in your directory. There shouldn’t be any unless you have used the ulimit command to change the default behavior for your account, but check just in case – core files are big and can easily gobble up your disk quota if you aren’t careful about properly disposing of them. Program #3:
  12. Open prog3.c and locate the error.
  13. Compile the program and execute without Valgrind. You should not receive a runtime error; however, an error does exist in the program.

Submitting: Create a gzipped tarball of your modified files (following the instructions above), choosing a name that identifies you and the assignment, and drop the tarball in my netfiles inbox. Be sure to include your answer sheet as well. Summary: You have seen that Valgrind is capable of finding uninitialized variables, problems with allocating and deallocating memory, and memory leaks. Valgrind is capable of much more than we can learn in one lab — I encourage you to visit the Valgrind Web site for more information. Acknowledgments: Thanks to Will Jones in the ECE Department at Clemson University for graciously permitting us to use and modify the example programs he created. If you found these to be helpful, send Will an email to that effect: wjones@parl.clemson.edu. Thanks also to Barry Lawson for the original formulation of this lab exercise.