ebook img

The Art of Debugging: with GDB, DDD, and Eclipse PDF

280 Pages·2008·9.347 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Art of Debugging: with GDB, DDD, and Eclipse

GG AA PPRRAACCTTIICCAALL DD AAPPPPRROOAACCHH TTOO TT HHEE AA RR TT OOFF BB DDEEBBUUGGGGIINNGG DD EE BB UU GG GG II NN GG TT HH WW Debugging is crucial to successful software develop- • Use features like catchpoints, convenience variables, ment, but even many experienced programmers find and artificial arrays IIEE WWIITTHH GGDDBB,, DDDDDD,, TT it challenging. Sophisticated debugging tools are • Avoid common debugging pitfalls HH A A available, yet it may be difficult to determine which features are useful in which situations. The Art of Real world examples of coding errors help to clarify the GGRR AANNDD EECCLLIIPPSSEE DD Debugging is your guide to making the debugging authors’ guiding principles, and coverage of complex TT BB process more efficient and effective. topics like thread, client-server, GUI, and parallel The Art of Debugging illustrates the use three of the most programming debugging will make you even more , D, D O O proficient. You’ll also learn how to prevent errors in the popular debugging tools on Linux/Unix platforms: GDB, DDFF first place with text editors, compilers, error reporting, N O R M A N M A T L O F F A N D P E T E R J AY S A L Z M A N DDD, and Eclipse. The text-command based GDB (the DD and static code checkers. DD GNU Project Debugger) is included with most distributions. ,, DDD is a popular GUI front end for GDB, while Eclipse Whether you dread the thought of debugging your A AEE provides a complete integrated development environment. programs or simply want to improve your current NNBB debugging efforts, you’ll find a valuable ally in The DD In addition to offering specific advice for debugging with Art of Debugging. UU each tool, authors Norm Matloff and Pete Salzman cover EE general strategies for improving the process of finding ABOUT THE AUTHORS CCGG and fixing coding errors, including how to: LLGG Norman Matloff, a computer science professor at UC II PP • Inspect variables and data structures Davis, is the author of several popular public-domain II SS software packages and online tutorials. NN GG • Understand segmentation faults and core dumps EE Peter Jay Salzman received his doctorate in theoretical GG • Know why your program crashes or throws exceptions physics at UC Davis and founded the Linux Users Group DD of Davis. BB THE FINEST IN GEEK ENTERTAINMENT™ www.nostarch.com SA M LZ AN ATL “I LAY FLAT.” $39.95 ($39.95 CDN) M D O A F This book uses RepKover—a durable binding that won’t snap shut. PROGRAMMSHELVE IN: N F ING/C www.it-ebooks.info THE ART OF DEBUGGING www.it-ebooks.info THE ART OF DEBUGGING with GDB, DDD, and Eclipse by Norman Matloff and Peter Jay Salzman SanFrancisco www.it-ebooks.info THEARTOFDEBUGGINGWITHGDB,DDD,ANDECLIPSE.Copyright©2008byNormanMatloffandPeter JaySalzman. Allrightsreserved.Nopartofthisworkmaybereproducedortransmittedinanyformorbyanymeans,electronic ormechanical,includingphotocopying,recording,orbyanyinformationstorageorretrievalsystem,withoutthe priorwrittenpermissionofthecopyrightownerandthepublisher. 1211100908 123456789 ISBN-10:1-59327-002-X ISBN-13:978-1-59327-174-9 Publisher:WilliamPollock ProductionEditor:MeganDunchak CoverandInteriorDesign:OctopodStudios DevelopmentalEditor:TylerOrtman TechnicalReviewer:DanielJacobowitz Copyeditor:NeilChing Compositor:RileyHoffman Proofreader:RachelKai Indexer:FredBrown,AllegroTechnicalIndexing Forinformationonbookdistributorsortranslations,pleasecontactNoStarchPress,Inc.directly: NoStarchPress,Inc. 555DeHaroStreet,Suite250,SanFrancisco,CA94107 phone:415.863.9900;fax:415.863.9950;[email protected];www.nostarch.com LibraryofCongressCataloging-in-PublicationData Matloff, Norman S. The art of debugging with GDB, DDD, and Eclipse / Norman Matloff and P.J. Salzman. p. cm. ISBN-13: 978-1-59327-002-5 ISBN-10: 1-59327-002-X 1. Debugging in computer science. 2. Computer software-Quality control. I. Salzman, P.J. II. Title. QA76.9.D43M35 2008 005.1’4-dc22 2003017566 NoStarchPressandtheNoStarchPresslogoareregisteredtrademarksofNoStarchPress,Inc.Otherproductand companynamesmentionedhereinmaybethetrademarksoftheirrespectiveowners.Ratherthanuseatrademark symbolwitheveryoccurrenceofatrademarkedname,weareusingthenamesonlyinaneditorialfashionandtothe benefitofthetrademarkowner,withnointentionofinfringementofthetrademark. Theinformationinthisbookisdistributedonan“AsIs”basis,withoutwarranty.Whileeveryprecautionhasbeen takeninthepreparationofthiswork,neithertheauthorsnorNoStarchPress,Inc.shallhaveanyliabilitytoany personorentitywithrespecttoanylossordamagecausedorallegedtobecauseddirectlyorindirectlybytheinfor- mationcontainedinit. www.it-ebooks.info BRIEF CONTENTS Preface............................................................................. xi Chapter1:SomePreliminariesforBeginnersandPros................................... 1 Chapter2:StoppingtoTakeaLookAround............................................ 47 Chapter3:InspectingandSettingVariables............................................ 95 Chapter4:WhenaProgramCrashes .................................................117 Chapter5:DebugginginaMultiple-ActivitiesContext...................................145 Chapter6:SpecialTopics............................................................185 Chapter7:OtherTools...............................................................205 Chapter8:UsingGDB/DDD/EclipseforOtherLanguages...............................235 Index...............................................................................259 www.it-ebooks.info CONTENTS IN DETAIL PREFACE xi 1 SOME PRELIMINARIES FOR BEGINNERS AND PROS 1 1.1 DebuggingToolsUsedinThisBook .......................................... 1 1.2 ProgrammingLanguageFocus ............................................... 2 1.3 ThePrinciplesofDebugging................................................. 2 1.3.1 TheEssenceofDebugging: ThePrincipleofConfirmation........... 2 1.3.2 OfWhatValueIsaDebuggingToolforthePrincipleofConfirmation? 3 1.3.3 OtherDebuggingPrinciples...................................... 4 1.4 Text-Basedvs. GUI-BasedDebuggingTools,andaCompromiseBetweenThem... 5 1.4.1 BriefComparisonofInterfaces ................................... 5 1.4.2 Compromises................................................... 12 1.5 MainDebuggerOperations ................................................. 14 1.5.1 SteppingThroughtheSourceCode............................... 14 1.5.2 InspectingVariables ............................................ 15 1.5.3 Issuingan“AllPointsBulletin”forChangestoaVariable ........... 17 1.5.4 MovingUpandDowntheCallStack ............................. 17 1.6 OnlineHelp................................................................ 19 1.7 IntroductoryDebuggingSession.............................................. 19 1.7.1 TheGDBApproach............................................. 22 1.7.2 TheSameSessioninDDD ....................................... 36 1.7.3 TheSessioninEclipse........................................... 38 1.8 UseofStartupFiles ......................................................... 43 2 STOPPING TO TAKE A LOOK AROUND 47 2.1 MechanismsforPause ...................................................... 47 2.2 OverviewofBreakpoints .................................................... 48 2.3 KeepingTrackofBreakpoints................................................ 49 2.3.1 BreakpointListsinGDB.......................................... 49 2.3.2 BreakpointListsinDDD.......................................... 50 2.3.3 BreakpointListsinEclipse........................................ 51 2.4 SettingBreakpoints ......................................................... 51 2.4.1 SettingBreakpointsinGDB ...................................... 51 2.4.2 SettingBreakpointsinDDD ...................................... 55 2.4.3 SettingBreakpointsinEclipse .................................... 56 2.5 ExtendedGDBExample..................................................... 56 www.it-ebooks.info 2.6 PersistenceofBreakpoints ................................................... 59 2.7 DeletingandDisablingBreakpoints .......................................... 60 2.7.1 DeletingBreakpointsinGDB..................................... 61 2.7.2 DisablingBreakpointsinGDB.................................... 62 2.7.3 DeletingandDisablingBreakpointsinDDD ....................... 62 2.7.4 DeletingandDisablingBreakpointsinEclipse ..................... 63 2.7.5 “Moving”BreakpointsinDDD.................................... 64 2.7.6 Undoing/RedoingBreakpointActionsinDDD ..................... 66 2.8 MoreonViewingBreakpointAttributes ....................................... 67 2.8.1 GDB........................................................... 67 2.8.2 DDD........................................................... 69 2.8.3 Eclipse......................................................... 69 2.9 ResumingExecution......................................................... 69 2.9.1 InGDB ........................................................ 70 2.9.2 InDDD ........................................................ 78 2.9.3 InEclipse ...................................................... 79 2.10 ConditionalBreakpoints..................................................... 79 2.10.1 GDB........................................................... 80 2.10.2 DDD........................................................... 83 2.10.3 Eclipse......................................................... 84 2.11 BreakpointCommandLists................................................... 85 2.12 Watchpoints ............................................................... 89 2.12.1 SettingWatchpoints............................................. 90 2.12.2 Expressions .................................................... 93 3 INSPECTING AND SETTING VARIABLES 95 3.1 OurMainExampleCode.................................................... 95 3.2 AdvancedInspectionandSettingofVariables................................. 98 3.2.1 InspectioninGDB .............................................. 98 3.2.2 InspectioninDDD...............................................102 3.2.3 InspectioninEclipse.............................................103 3.2.4 InspectingDynamicArrays ......................................104 3.2.5 WhatAboutC++?..............................................109 3.2.6 MonitoringLocalVariables ......................................112 3.2.7 ExaminingMemoryDirectly......................................112 3.2.8 AdvancedOptionsforPrintandDisplay ..........................112 3.3 SettingVariablesfromWithinGDB/DDD/Eclipse..............................113 3.4 GDB’sOwnVariables ......................................................113 3.4.1 MakingUseoftheValueHistory .................................114 3.4.2 ConvenienceVariables..........................................114 4 WHEN A PROGRAM CRASHES 117 4.1 BackgroundMaterial: MemoryManagement..................................118 viii ContentsinDetail www.it-ebooks.info 4.1.1 WhyDoesaProgramCrash?....................................118 4.1.2 ProgramLayoutinMemory ......................................118 4.1.3 TheNotionofPages ............................................121 4.1.4 DetailsontheRoleofthePageTable .............................122 4.1.5 ASlightMemory-AccessBugMightNotCauseaSegFault .........124 4.1.6 SegFaultsandUnixSignals .....................................125 4.1.7 OtherTypesofExceptions .......................................128 4.2 CoreFiles..................................................................129 4.2.1 HowCoreFilesAreCreated.....................................129 4.2.2 YourShellMaySuppresstheCreationofaCoreFile...............130 4.3 ExtendedExample..........................................................131 4.3.1 FirstBug .......................................................135 4.3.2 Don’tLeaveGDBDuringaDebuggingSession ....................137 4.3.3 SecondandThirdBugs..........................................137 4.3.4 FourthBug .....................................................139 4.3.5 FifthandSixthBugs.............................................141 5 DEBUGGING IN A MULTIPLE-ACTIVITIES CONTEXT 145 5.1 DebuggingClient/ServerNetworkPrograms..................................145 5.2 DebuggingThreadedCode..................................................151 5.2.1 ReviewofProcessesandThreads.................................151 5.2.2 BasicExample..................................................153 5.2.3 AVariation ....................................................159 5.2.4 GDBThreadsCommandSummary ...............................161 5.2.5 ThreadsCommandsinDDD......................................161 5.2.6 ThreadsCommandsinEclipse ...................................161 5.3 DebuggingParallelApplications .............................................163 5.3.1 Message-PassingSystems........................................164 5.3.2 Shared-MemorySystems.........................................170 5.4 ExtendedExample..........................................................171 5.4.1 OpenMPOverview .............................................171 5.4.2 OpenMPExampleProgram......................................172 6 SPECIAL TOPICS 185 6.1 WhatIfItDoesn’tEvenCompileorLoad?.....................................185 6.1.1 PhantomLineNumbersinSyntaxErrorMessages ..................185 6.1.2 MissingLibraries................................................190 6.2 DebuggingGUIPrograms...................................................194 6.2.1 DebuggingCursesPrograms.....................................194 7 OTHER TOOLS 205 ContentsinDetail ix www.it-ebooks.info 7.1 MakingGoodUseofaTextEditor ...........................................206 7.1.1 SyntaxHighlighting .............................................206 7.1.2 MatchingBrackets ..............................................208 7.1.3 VimandMakefiles..............................................209 7.1.4 MakefilesandCompilerWarnings ...............................210 7.1.5 FinalThoughts onaTextEditorasanIDE .........................211 7.2 MakingGoodUseoftheCompiler...........................................212 7.3 ErrorReportinginC ........................................................213 7.3.1 Usingerrno ....................................................213 7.4 BetterLivingwithstraceandltrace ...........................................217 7.5 StaticCodeCheckers: lintandFriends........................................219 7.5.1 HowtoUsesplint...............................................221 7.5.2 LastWords.....................................................221 7.6 DebuggingDynamicallyAllocatedMemory ...................................221 7.6.1 StrategiesforDetectingDAMProblems ...........................224 7.6.2 ElectricFence ..................................................225 7.6.3 DebuggingDAMProblemswithGNUCLibraryTools ..............228 8 USING GDB/DDD/ECLIPSE FOR OTHER LANGUAGES 235 8.1 Java.......................................................................236 8.1.1 DirectUseofGDBforDebuggingJava............................238 8.1.2 UsingDDDwithGDBtoDebugJava..............................240 8.1.3 UsingDDDasaGUIforJDB.....................................241 8.1.4 DebuggingJavainEclipse.......................................241 8.2 Perl .......................................................................242 8.2.1 DebuggingPerlviaDDD ........................................244 8.2.2 DebuggingPerlinEclipse........................................246 8.3 Python.....................................................................247 8.3.1 DebuggingPythoninDDD.......................................249 8.3.2 DebuggingPythoninEclipse.....................................250 8.4 DebuggingSWIGCode ....................................................251 8.5 AssemblyLanguage ........................................................255 INDEX 259 x ContentsinDetail www.it-ebooks.info PREFACE “Hey, this thing really works!” So said one of our stu- dents, Andrew, after he made serious use of a debug- ging tool for the first time. He had learned about de- bugging tools three years earlier in his freshman pro- gramming courses, but he had dismissed them as just something to learn for the final exam. Now as a fourth- year student, Andrew’s professor urged him to stop us- ing print statements for debugging and make use of formal debugging tools. To his delight and surprise, he found that he could greatly reduce his debugging time by making use of the proper tools. Therearemany“Andrews”outthere,amongstudentsandamongwork- ingprogrammers,andwehopethisbookwilltriggeran“Andrew-like”epiphany forthem. Butevenmoreso,wehopetoreachthemanypeoplewhodouse debuggingtoolsbutarenotsurewhatcanbedoneinspecializedsituations andwhowouldliketolearnmoreaboutdebuggingtoolsandthephilosophy behindthem. www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.