Production Debugging for .NET Framework Applications
Total Page:16
File Type:pdf, Size:1020Kb
Production Debugging for .NET Framework Applications Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft, MS-DOS, Windows, Visual C#, Visual Basic, Visual C++, Visual Studio, and Win32 are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. © 2002 Microsoft Corporation. All rights reserved. Version 1.0 The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Contents Chapter 1 Introduction to Production Debugging for .NET Framework Applications 1 Production Debugging vs. Development Debugging . 2 Environment . 2 Tools . 3 Actions . 3 Debugging Tools . 4 Discovery Phase . 4 Debugging Phase . 5 ASP.NET Process Model and Health Monitoring . 6 What Is the ASP.NET Process Model? . 7 IIS 5.x Process Model . 7 Health Monitoring in IIS 5.x . 9 IIS 6.0 Process Model in Windows .NET Server Release Candidate 1 . 9 IIS 6.0 Application Pools . 10 Orphaning Failed ASP.NET Worker Processes . 12 System Requirements . 12 Additional Software . 13 Documentation Conventions . 14 Summary . 15 Chapter 2 Debugging Memory Problems 17 .NET Memory Management and Garbage Collection . 17 Generations . 19 Roots . 19 Large Object Heap . 21 Scenario: Memory Consumption . 22 Breaking Down the Thought Processes . 22 Memory Consumption Walkthrough . 25 Debugging User-Mode Dump Files with WinDbg . 36 Analyzing the Dump: Summary . 41 Diagnosing Memory Leaks with the Allocation Profiler . 51 Conclusion . 55 iv Contents Chapter 3 Debugging Contention Problems 57 ASP.NET Thread Pools . 57 Managed Threads and AppDomains . 58 The .NET ThreadPool . 58 ASP.NET Thread Pool . 59 Scenario: Contention or Deadlock Symptoms . 60 Breaking Down the Thought Processes . 61 Contention Walkthrough . 64 Debugging a Dump File with WinDbg . 68 Debugging with Visual Studio .NET . 98 Conclusion . 102 Chapter 4 Debugging Unexpected Process Termination 103 Handling COM Threading in ASP.NET . 103 ASP.NET and COM Interop . 104 COM Components and ASP.NET . 104 Scenario: Unexpected Process Termination . 107 Breaking Down the Thought Processes . 107 Unexpected Process Termination Walkthrough . 109 Debugging a Dump File with WinDbg . 113 Examining the Dump File . 113 Conclusion . 146 Appendix Thread State Values . 148 Application Code . 149 Contention.aspx.cs . 149 Memory.aspx.cs . 151 Unexpected.aspx.cs . 155 Pmhealth.cs . 156 Debugging with CorDbg . 161 Using CorDbg to Display Call Stack Information . 162 Using a Script File . 164 The managed_threads.cmd Script . 170 Exploring Further . 171 1 Introduction to Production Debugging for .NET Framework Applications Every developer or support engineer debugs applications at some stage in his or her career, yet debugging is often viewed as an arcane and difficult topic. While it is often difficult to determine why an application is hanging, leaking memory, or crashing, there are techniques you can apply to make the debugging process more productive and efficient. The Production Debugging for .NET Framework Applications guide aims to equip you with the mindset, tools, and techniques that will help you successfully identify and resolve common application debugging. The techniques presented here are not simplified classroom exercises. They are based on the experience of both Microsoft customers and Microsoft internal devel- opment teams. These techniques represent proven, best-practice approaches to debugging a variety of issues. This guide presents walkthroughs of three scenarios that ASP.NET applications may encounter when running in production environments: memory consumption, contention (also known as “deadlock”), and unexpected server crashes. These scenarios concentrate on debugging Microsoft® .NET framework applications in a production environment, focusing on low-level details such as thread states and memory allocations. However, the thought processes and techniques discussed will help you understand debugging on a much broader scale. This document helps you learn how to think about debugging in a general sense, and equips you with the mindset to successfully approach unknown and unforeseen debugging situations in the future. 2 Production Debugging for .NET Framework Applications Although the example scenarios target ASP.NET and the Visual C#™ development tool, the thought processes and techniques discussed are common across all .NET technologies and languages, and problems encountered are common themes. The scenarios also introduce the tools that Microsoft provides for the Microsoft Win- dows® operating system and .NET debugging, including the use of WinDbg, Core Debugger (CDB), and debugger extension DLLs for displaying managed call stacks and object data. This chapter provides an introduction to the Production Debugging for .NET Framework Applications guide, defines the scope of the chapters, and provides background material. It also introduces the system requirements for the walkthroughs, and gives instructions for installing the required software. Production Debugging vs. Development Debugging The primary goal of debugging a system is to isolate and determine the root cause of a performance, configuration, or abnormal error condition that impedes normal system operation. Generally speaking, this goal holds true whether you are debug- ging in a development environment or in a production environment. However, there are crucial differences between the two environments. When debugging in a production environment, determining the root cause may be less important than simply getting the system into an operational state. Time constraints and business cases also differ between development and produc- tion environments. Debugging within a production environment is usually done within much tighter time constraints, given that the end user or customer may be losing revenue as a consequence of a non-operational system. The walkthroughs presented in this guide demonstrate many non-invasive tech- niques that can be used in a production debugging environment. Environment In a production environment, the system experiences live operational loads and timing situations, which may be difficult or impossible to reproduce in a develop- ment environment. In addition, scenarios can arise that have not been anticipated during system design. Timing-sensitive or load-sensitive scenarios that lead to abnormal behavior can be difficult to reproduce or debug successfully, even during live operation in a production environment. Because physical access to a production system is often restricted, offline or remote-access techniques, such as Terminal Services sessions and remote debugging sessions, are required. Chapter 1: Introduction to Production Debugging for .NET Framework Applications 3 Tools In a development environment, you usually have access to an integrated develop- ment environment (IDE), source code, and debug versions of libraries and symbols. In a production environment, the IDE and source.