THE ART OF DEBUGGING WITH GDB, DDD, AND ECLIPSE
Norman Matloff
Peter Jay Salzman
Copyright 2008
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
555 De Haro Street, Suite 250, San Francisco, CA 94107
phone: 415.863.9900; fax: 415.863.9950;
Library of Congress Cataloging-in-Publication Data
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 2008005.1'4-dc22 2003017566
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an "As Is" basis, without warranty. While every precaution has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
No Starch Press
PREFACE
Hey, this thing really works!" So said one of our students, Andrew, after he made serious use of a debugging tool for the first time. He had learned about debugging tools three years earlier in his freshman programming 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 using 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.
There are many "Andrews" out there, among students and among working programmers, and we hope this book will trigger an "Andrew-like" epiphany for them. But even more so, we hope to reach the many people who do use debugging tools but are not sure what can be done in specialized situations and who would like to learn more about debugging tools and the philosophy behind them.
As this book's copyeditor pointed out, much knowledge of debugging exists in some communities as kind of folklore, but it is not written in books. Well, this book will change that. We'll address questions like the following:
How do you debug threads code?
Why do breakpoints sometimes end up at slightly different locations than where you set them?
Why does the GDB until
command sometimes jump to a "surprising" place?
What cool tricks can be done with DDD and Eclipse?
In today's GUI age, do text-based applications such as GDB have any value?
Why didn't a segmentation fault occur when your erroneous code exceeded the bounds of an array?
Why is one of our example data structures named nsp?
(Sorry, that's an inside joke with our publisher.)
This book is neither a glorified user's manual nor an abstract treatise on the cognitive theory of the debugging process. Instead, it is something intermediate to these two genres. On one hand, we do indeed give information on the "how-to" for specific commands in GDB, DDD, and Eclipse; but on the other hand, we do set forth and make frequent use of some general principles for the debugging process.
We chose GDB, DDD, and Eclipse as our illustrative tools because of their popularity in the Linux/open source communities. Our examples slightly favor GDB, not only because its text-based nature makes it more compact to present on a page but also because, as alluded to above, we find that text-based commands still play a valuable role in the debugging process.
Eclipse has become quite widely used for much more than simply the debugging role we treat here, and it does provide an attractive, versatile tool for debugging. On the other hand, DDD has a much smaller footprint and includes some powerful features not found in Eclipse.
, "Some Preliminaries for Beginners and Pros," is an overview. Many experienced programmers may be tempted to skip it, but we urge them to read through it, as we do set forth a number of simple but powerful general guidelines that we recommend for the debugging process.
Then , "Stopping to Take a Look Around," covers the workhorse of debugging, the breakpoint, discussing all the ins and outssetting, deleting, and disabling breakpoints; moving from one breakpoint to the next; viewing detailed information on breakpoints; and so on.
Once you arrive at a breakpoint, then what? , "Inspecting and Setting Variables," addresses this question. Our running example here concerns code that traverses a tree. The key point is convenient display of the contents of a node in the tree when we reach a breakpoint. Here GDB really shines, providing some very flexible features that enable you to effectively display the information of interest each time the program pauses. And we present an especially nice DDD feature for graphical display of trees and other linked data structures.
, "When a Program Crashes," covers the dreaded runtime errors arising from segmentation faults. We first present material on what is happening at the lower levels, including memory allocation for a program and the cooperative roles of the hardware and the operating system. Readers with a good systems knowledge may skim through this material, but we believe that many others will profit by acquiring this foundation. We then turn to core fileshow they are created, how to use them to perform "post mortems," and so on. We finish the chapter with an extended example of a debugging session in which several bugs produce seg faults.
We chose "Debugging in A Multiple-Activities Context" for the title of concerns parallel programming with the popular MPI and OpenMP packages. We end with an extended example in the context of OpenMP.
, "Special Topics," covers some important miscellaneous topics. A debugging tool can't help you if your code doesn't even compile, so we discuss some approaches for dealing with this. Then we treat the problem of failure to link, due to missing libraries; once again we felt it was useful here to give some "theory"types of libraries and how they are linked to your main code, for example. And what about debugging GUI programs? For simplicity, we stick to a "semi-GUI" setting here, that of curses programming, and show how to get GDB, DDD, and Eclipse to interact with the events in your curses window.
As noted earlier, the debugging process can be greatly enhanced through the use of supplementary tools, several of which we present in , "Other Tools." We have additional coverage of errno
and strace
, some material on lint
, and tips on the effective use of a text editor.
Though the book focuses on C/C++, we have coverage of other languages in , "Using GDB/DDD/Eclipse for Other Languages," treating Java, Python, Perl, and assembly language.
We apologize if we have somehow missed the reader's favorite debugging topic, but we have covered the material that we have found useful in our own programming.
We owe a great debt of gratitude to the many staffers at No Starch Press who assisted us on this project over its long duration. We especially thank the firm's founder and editor, Bill Pollock. He had faith in this offbeat project from the beginning and was remarkably tolerant of our many delays.