66 Specific Ways to Debug Software & Systems
Total Page:16
File Type:pdf, Size:1020Kb
66 Specific Ways to Debug Software & Systems Every software developer and IT professional understands the crucial importance of effective debugging. Often, debugging consumes most of a developer’s workday, and mastering the required techniques and skills can take a lifetime. In Effective Debugging, Diomidis Spinellis helps experienced programmers accelerate their journey to mastery, by systematically categorizing, explaining, and illustrating the most useful debugging methods, strategies, techniques, and tools. Drawing on more than thirty-five years of experience, Spinellis expands your arsenal of debugging techniques, helping you choose the best approaches for each challenge. He presents vendor-neutral, example-rich advice on general principles, high-level strategies, concrete techniques, high-efficiency tools, creative tricks, and the behavioral traits associated 256 Pages | Paperback with effective debugging. Publication: June 2016 Spinellis’s 66 expert techniques address every facet of debugging and are illustrated with step-by-step instructions and actual code. He addresses the full ORDER & SAVE spectrum of problems that can arise in modern software systems, especially problems caused by SAVE 35% WHEN YOU ORDER complex interactions among components and from informit.com and enter the dis- services running on hosts scattered around the count code INFOQ during checkout planet. Whether you’re debugging isolated runtime FREE US SHIPPING on print books errors or catastrophic enterprise system failures, this guide will help you get the job done—more quickly, Major eBook formats and with less pain. Only InformIT offers PDF, EPUB, & MOBI together for one price Key features include High-level strategies and methods for addressing diverse software failures Specific techniques to apply when programming, OTHER AVAILABILITY compiling, and running code Better ways to make the most of your debugger General-purpose skills and tools worth investing in Through Safari Flow subscription service Advanced ideas and techniques for escaping dead-ends and the maze of complexity Booksellers and online retailers including Advice for making programs easier to debug Amazon/Kindle store and Specialized approaches for debugging multithreaded, Barnes and Noble/bn.com asynchronous, and embedded code Bug avoidance through improved software design, construction, and management i i “effective-debugging” — 2016/6/2 — 14:30 — page i — #1 i i Effective Debugging i i i i i i “effective-debugging” — 2016/6/2 — 14:30 — page ii — #2 i i The Effective Software Development Series Scott Meyers, Consulting Editor Visit informit.com/esds for a complete list of available publications. he Effective Software Development Series provides expert advice on all aspects of Tmodern software development. Titles in the series are well written, technically sound, and of lasting value. Each describes the critical things experts always do — or always avoid — to produce outstanding software. Scott Meyers, author of the best-selling books Effective C++ (now in its third edition), More Effective C++, and Effective STL (all available in both print and electronic versions), conceived of the series and acts as its consulting editor. Authors in the series work with Meyers to create essential reading in a format that is familiar and accessible for software developers of every stripe. Make sure to connect with us! informit.com/socialconnect i i i i i i “effective-debugging” — 2016/6/2 — 14:30 — page iii — #3 i i Effective Debugging 66 SPECIFIC WAYS TO DEBUG SOFTWARE AND SYSTEMS Diomidis Spinellis Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo i i i i i i “effective-debugging” — 2016/6/2 — 14:30 — page iv — #4 i i Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions about sales outside the U.S., please contact [email protected]. Visit us on the Web: informit.com/aw Library of Congress Control Number: 2016937082 Copyright © 2017 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms, and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/. ISBN-13: 978-0-13-439479-4 ISBN-10: 0-13-439479-8 Text printed in the United States at RR Donnelley in Crawfordsville, Indiana. First printing, July 2016 i i i i i i “effective-debugging” — 2016/6/2 — 14:30 — page v — #5 i i To my mentors, past and future i i i i i i “effective-debugging” — 2016/6/2 — 14:30 — page vii — #7 i i Contents Figures xi Listings xiii Preface xv Acknowledgments xxiii About the Author xxviii Chapter 1: High-Level Strategies 1 Item 1: Handle All Problems through an Issue-Tracking System 1 Item 2: Use Focused Queries to Search the Web for Insights into Your Problem 3 Item 3: Confirm That Preconditions and Postconditions Are Satisfied 5 Item 4: Drill Up from the Problem to the Bug or Down from the Program’s Start to the Bug 7 Item 5: Find the Difference between a Known Good System and a Failing One 9 Item 6: Use the Software’s Debugging Facilities 12 Item 7: Diversify Your Build and Execution Environment 17 Item 8: Focus Your Work on the Most Important Problems 20 Chapter 2: General-Purpose Methods and Practices 23 Item 9: Set Yourself Up for Debugging Success 23 Item 10: Enable the Efficient Reproduction of the Problem 25 i i i i i i “effective-debugging” — 2016/6/2 — 14:30 — page viii — #8 i i viii Contents Item 11: Minimize the Turnaround Time from Your Changes to Their Result 28 Item 12: Automate Complex Testing Scenarios 29 Item 13: Enable a Comprehensive Overview of Your Debugging Data 32 Item 14: Consider Updating Your Software 33 Item 15: Consult Third-Party Source Code for Insights on Its Use 34 Item 16: Use Specialized Monitoring and Test Equipment 36 Item 17: Increase the Prominence of a Failure’s Effects 40 Item 18: Enable the Debugging of Unwieldy Systems from Your Desk 42 Item 19: Automate Debugging Tasks 44 Item 20: Houseclean Before and After Debugging 45 Item 21: Fix All Instances of a Problem Class 46 Chapter 3: General-Purpose Tools and Techniques 49 Item 22: Analyze Debug Data with Unix Command-Line Tools 49 Item 23: Utilize Command-Line Tool Options and Idioms 55 Item 24: Explore Debug Data with Your Editor 57 Item 25: Optimize Your Work Environment 59 Item 26: Hunt the Causes and History of Bugs with the Revision Control System 64 Item 27: Use Monitoring Tools on Systems Composed of Independent Processes 67 Chapter 4: Debugger Techniques 71 Item 28: Use Code Compiled for Symbolic Debugging 71 Item 29: Step through the Code 76 Item 30: Use Code and Data Breakpoints 77 Item 31: Familiarize Yourself with Reverse Debugging 80 Item 32: Navigate along the Calls between Routines 82 Item 33: Look for Errors by Examining the Values of Variables and Expressions 84 Item 34: Know How to Attach a Debugger to a Running Process 87 Item 35: Know How to Work with Core Dumps 89 Item 36: Tune Your Debugging Tools 92 Item 37: Know How to View Assembly Code and Raw Memory 95 i i i i i i “effective-debugging” — 2016/6/2 — 14:30 — page ix — #9 i i Contents ix Chapter 5: Programming Techniques 101 Item 38: Review and Manually Execute Suspect Code 101 Item 39: Go Over Your Code and Reasoning with a Colleague 103 Item 40: Add Debugging Functionality 104 Item 41: Add Logging Statements 108 Item 42: Use Unit Tests 112 Item 43: Use Assertions 116 Item 44: Verify Your Reasoning by Perturbing the Debugged Program 119 Item 45: Minimize the Differences between a Working Example and the Failing Code 120 Item 46: Simplify the Suspect Code 121 Item 47: Consider Rewriting the Suspect Code in Another Language 124 Item 48: Improve the Suspect Code’s Readability and Structure 126 Item 49: Fix the Bug’s Cause, Rather Than Its Symptom 129 Chapter 6: Compile-Time Techniques 133 Item 50: Examine Generated Code 133 Item 51: Use Static Program Analysis 136 Item 52: Configure Deterministic Builds and Executions 141 Item 53: Configure the Use of Debugging Libraries and Checks 143 Chapter 7: Runtime Techniques 149 Item 54: Find the