Software ExorcismA Handbook for Debugging and Optimizing Legacy Code
Bill Blunden
Apress
Copyright 2003 Bill Blunden
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
1-59059-234-4
10987654321
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
Technical Reviewer: Doug Holland
Editorial Board: Dan Appleman, Craig Berry, Gary Cornell, Tony Davis, Steven Rycroft, Julian Skinner, Martin Streicher, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski
Assistant Publisher: Grace Wong
Copy Editor: Ami Knox
Production Manager: Kari Brooks
Compositor and Artist: Kinetic Publishing Services, LLC
Proofreader: Thistle Hill Publishing Services
Indexer: Carol Burbo
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, email .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email .
The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress 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 this work.
The source code for this book is available to readers at http://www.apress.com in the Downloads section.
The medieval illustrations that appear on the cover and throughout this book were taken from Devils, Demons, and Witchcraft: 244 Illustrations for Artists by Ernst and Johanna Lehner (Dover Publications, ISBN: 0486227510) and were reprinted with permission.
This book is dedicated to bad kung fu movies.
About the Author
Reverend Blunden has spent much of his life wandering through the subterranean catacombs of system software. At an early age, he discovered the DOS debug program, and he has been trying to peek behind the curtain ever since. Along the way, he received a degree in physics from Cornell University and a masters in operations research from Case Western Reserve University. Having narrowly escaped a lifetime of servitude to the Society of Actuaries, Reverend Bill fled westward. After ten years of searching in the desert, Reverend Bill was visited by an epiphany: a great smiling head named J. R. "Bob" Dobbs. Through Dobbs, Reverend Bill has learned the true nature of what makes software evil. He has come back to civilization to spread the word.
Acknowledgments
Writing a Book is Almost like being pregnant. For months you feel tired and sleep deprived. You have strange mood swings and food cravings. When you are finally done, you're exhausted and overjoyed that the tribulation is over. Yet people still continue to write, even after the first ordeal. I believe that the need to write must be an inveterate quality in some authors.
I would like to thank all the people at Apress who encouraged me and put up with all of my shenanigans. Specifically, I would like to thank Gary Cornell for giving me the opportunity to write for Apress. I would also like to thank Jim Sumser and Hollie Fischer for entertaining my lengthy diatribes and offering feedback on my dubious cogitations. Finally, I would like to thank Ami Knox, Kari Brooks, Beth Christmas, and Jessica Dolcourt for their assistance during the production process.
Praise Bob,
Rev. Bill Blunden
Church of the SubGenius
Introduction
.
Figure 1: Post-release life cycle of the average software application
The post-release cycle can be initiated by the addition of a new feature or the submission of a bug report. In either case, unit and system tests are used to determine if the patched code is doing what it is supposed to. Once the paths of execution have been sufficiently stabilized, the application can be profiled to locate performance bottlenecks. A variety of optimization techniques can then be implemented to increase execution speed and decrease memory footprint. As customers demand new features and report new bugs, the cycle will repeat itself and the application will evolve.
Most books on software engineering focus intently on the phases of development leading up to the release of an application (e.g., requirements, analysis, use cases, implementation, and so on). Everyone knows that it's much easier, and more gratifying, to write a program from scratch. Software maintenance, on the other hand, is the ugly stepchild of computer science. Most professors would prefer not to mention it in public. This is because discussing the rigors of maintenance work might raise questions dangerous questions. Why spend four years completing a degree in computer science when the fruits of your labor land you in an insanely frustrating maintenance role? If professors told their students the truth about software engineering as a profession, there would be a mass exodus out of computer science courses. A smaller department would translate into budget cuts and loss of prestige, so you'd better believe that the professors are more than willing to sidestep a few unpleasant truths to bolster the size of their classes.
This book is different. Rather than shield your eyes from the sordid realities of the software industry, I am going to dust off my old 8mm films and let you take a good look at the uncensored truth for yourself. You may want to keep a paper bag handy in case you get sick. This book examines the tools that engineers can use to facilitate the debugging and optimization of legacy software. In other words, this is a book on maintaining software that has been hacked to death by other people. Grab a bowl of popcorn and have a seat, but don't complain that I didn't warn you.
A Marine Corps Recon officer once told me that martial artists should not talk about techniques that are "combat effective" unless they have used those techniques under the threat of being killed. In this spirit, I feel that a book on software maintenance should not offer advice unless it has been used in the field, by the author, under stressful circumstances. This book intentionally avoids ivory tower abstraction in favor of field-ready tactics. I stick to simple tools that have worked for me in the past. The average maintenance engineer does not have time to waste on cloud diagrams or fashionable development methodologies. Maintenance engineers have a job to do and an immediate need to get it done. This book presents tactics that may be used, in the trenches, with little or no preparation.
Historical Motivation
Back in the Iron Age (i.e., the late 1960s and 1970s), most software engineers did not have much computational real estate to play with. Room-sized computers like the CDC 6600 had only 65,000 60-bit words of memory (less than a megabyte). In this type of environment, every single bit counted, and engineers went to extraordinary lengths to save space. At the same time, processor speed was also a serious issue. Most people dropped off their programs with the system operator in the afternoon and came back the next day to pick up the results. Hence, engineers in the days of yore had to meticulously balance memory usage with the need to minimize the number of CPU cycles consumed.