+ All Categories
Home > Documents > THE ART OF DEBUGGING - GBV

THE ART OF DEBUGGING - GBV

Date post: 16-Jan-2022
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
5
THE ART OF DEBUGGING with GDB, DDD, and ЕсПрле by Norman Matloff and Peter Jay Salzman no starch press San Francisco
Transcript
Page 1: THE ART OF DEBUGGING - GBV

THE ART OF DEBUGGING

with GDB, DDD, and ЕсПрле

by Norman Matloff and Peter Jay Salzman

no starch press

San Francisco

Page 2: THE ART OF DEBUGGING - GBV

CONTENTS IN DETAIL

PREFACE x i

1 SOME PRELIMINARIES FOR BEGINNERS A N D PROS 1

1.1 Debugging Tools Used in This Book 1 1.2 Programming Language Focus 2 1.3 The Principles of Debugging 2

1.3.1 The Essence of Debugging: The Principle of Confirmation 2 1.3.2 Of What Value Is a Debugging Tool for the Principle of Confirmation? 3 1.3.3 Other Debugging Principles 4

1.4 Text-Based vs. GUI-Based Debugging Tools, and a Compromise Between Them... 5 1.4.1 Brief Comparison of Interfaces 5 1.4.2 Compromises 12

1.5 Main Debugger Operations 14 1.5.1 Stepping Through the Source Code 14 1.5.2 Inspecting Variables 15 1.5.3 Issuing an "All Points Bulletin" for Changes to a Variable 17 1.5.4 Moving Up and Down the Call Stack 17

1.6 Online Help 19 1.7 Introductory Debugging Session 19

1.7.1 The GDB Approach 22 1.7.2 The Same Session in DDD 36 1.7.3 The Session in Eclipse 38

1.8 Use of Startup Files 43

2 S T O P P I N G TO TAKE A L O O K A R O U N D 4 7

2.1 Mechanisms for Pause 47 2.2 Overview of Breakpoints 48 2.3 Keeping Track of Breakpoints 49

2.3.1 Breakpoint Lists in GDB 49 2.3.2 Breakpoint Lists in DDD 50 2.3.3 Breakpoint Lists in Eclipse 51

2.4 Setting Breakpoints 51 2.4.1 Setting Breakpoints in GDB 51 2.4.2 Setting Breakpoints in DDD 55 2.4.3 Setting Breakpoints in Eclipse 56

2.5 Extended GDB Example 56

Page 3: THE ART OF DEBUGGING - GBV

2.6 Persistence of Breakpoints 59 2.7 Deleting and Disabling Breakpoints 60

2.7.1 Deleting Breakpoints in GDB 61 2.7.2 Disabling Breakpoints in GDB 62 2.7.3 Deleting and Disabling Breakpoints in DDD 62 2.7.4 Deleting and Disabling Breakpoints in Eclipse 63 2.7.5 "Moving" Breakpoints in DDD 64 2.7.6 Undoing/Redoing Breakpoint Actions in DDD 66

2.8 More on Viewing Breakpoint Attributes 67 2.8.1 GDB 67 2.8.2 DDD 69 2.8.3 Eclipse 69

2.9 Resuming Execution 69 2.9.1 In GDB 70 2.9.2 In DDD 78 2.9.3 In Eclipse 79

2.10 Conditional Breakpoints 79 2.10.1 GDB 80 2.10.2 DDD 83 2.10.3 Eclipse 84

2.11 Breakpoint Command Lists 85 2.12 Watchpoints 89

2.12.1 Setting Watchpoints 90 2.12.2 Expressions 93

3 INSPECTING A N D SETTING VARIABLES 9 5

3.1 Our Main Example Code 95 3.2 Advanced Inspection and Setting of Variables 98

3.2.1 Inspection in GDB 98 3.2.2 Inspection in DDD 102 3.2.3 Inspection in Eclipse 103 3.2.4 Inspecting Dynamic Arrays 104 3.2.5 What About C++? 109 3.2.6 Monitoring Local Variables 112 3.2.7 Examining Memory Directly 112 3.2.8 Advanced Options for Print and Display 112

3.3 Setting Variables from Within GDB/DDD/Eclipse 113 3.4 GDB's Own Variables 113

3.4.1 Making Use of the Value History 114 3.4.2 Convenience Variables 114

4 W H E N A P R O G R A M CRASHES 1 1 7

4.1 Background Material: Memory Management 118

Viü Contents in Detail

Page 4: THE ART OF DEBUGGING - GBV

4.1.1 Why Does a Program Crash? 118 4.1.2 Program Layout in Memory 118 4.1.3 The Notion of Pages 121 4.1.4 Details on the Role of the Page Table 122 4.1.5 A Slight Memory-Access Bug Might Not Cause a Seg Fault 124 4.1.6 Seg Faults and Unix Signals 125 4.1.7 Other Types of Exceptions 128

4.2 Core Files 129 4.2.1 How Core Files Are Created 129 4.2.2 Your Shell May Suppress the Creation of a Core File 130

4.3 Extended Example 131 4.3.1 First Bug 135 4.3.2 Don't Leave GDB During a Debugging Session 137 4.3.3 Second and Third Bugs 137 4.3.4 Fourth Bug 1 39 4.3.5 Fifth and Sixth Bugs 141

5 D E B U G G I N G I N A MULTIPLE-ACTIVITIES CONTEXT 1 4 5

5.1 Debugging Client/Server Network Programs 145 5.2 Debugging Threaded Code 151

5.2.1 Review of Processes and Threads 151 5.2.2 Basic Example 153 5.2.3 A Variation 159 5.2.4 GDB Threads Command Summary 161 5.2.5 Threads Commands in DDD 161 5.2.6 Threads Commands in Eclipse 161

5.3 Debugging Parallel Applications 163 5.3.1 Message-Passing Systems 164 5.3.2 Shared-Memory Systems 170

5.4 Extended Example 171 5.4.1 OpenMP Overview 171 5.4.2 OpenMP Example Program 172

6 SPECIAL TOPICS 1 8 5

6.1 What If It Doesn't Even Compile or Load? 1 85 6.1.1 Phantom Line Numbers in Syntax Error Messages 1 85 6.1.2 Missing Libraries 190

6.2 Debugging GUI Programs 194 6.2.1 Debugging Curses Programs 194

7 OTHER TOOLS 2 0 5

Page 5: THE ART OF DEBUGGING - GBV

7.1 Making Good Use of a Text Editor 206 7.1.1 Syntax Highlighting 206 7.1.2 Matching Brackets 208 7.1.3 Vim and Makefiles 209 7.1.4 Makefiles and Compiler Warnings 210 7.1.5 Final Thoughts on a Text Editor as an IDE 211

7.2 Making Good Use of the Compiler 212 7.3 Error Reporting inC 213

7.3.1 Using errno 213 7.4 Better Living with strace and Itrace 217 7.5 Static Code Checkers: lint and Friends 219

7.5.1 How to Use splint 221 7.5.2 Last Words 221

7.6 Debugging Dynamically Allocated Memory 221 7.6.1 Strategies for Detecting DAM Problems 224 7.6.2 Electric Fence 225 7.6.3 Debugging DAM Problems with GNU С Library Tools 228

8 U S I N G GDB/DDD/ECL IPSE FOR OTHER L A N G U A G E S 2 3 5

8.1 Java 236 8.1.1 Direct Use of GDB for Debugging Java 238 8.1.2 Using DDD with GDB to Debug Java 240 8.1.3 Using DDD as a GUI for JDB 241 8.1.4 Debugging Java in Eclipse 241

8.2 Perl 242 8.2.1 Debugging Perl via DDD 244 8.2.2 Debugging Perl in Eclipse 246

8.3 Python 247 8.3.1 Debugging Python in DDD 249 8.3.2 Debugging Python in Eclipse 250

8.4 Debugging SWIG Code 251 8.5 Assembly Language 255

INDEX 259

X Contents in Detail


Recommended