`
netcome
  • 浏览: 475738 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

How to debug application crash/hang in production environment?

阅读更多

A common tough scenario is the application crash/hang in production environment. Production environment is different from development environment that it normally does not have any powerful tools to obtain information regarding application failure. In this blog entry, I will talk about 2 wonderful tools to help collect information in production environment: Windbg and Process Explorer.

 

Application Crash:

Crash is the most common scenario of application failure. It is actually caused by an unhanded exception generated in the application. There are 2 types of information that are common critical to resolve application crash:

1.       Unhandled exception type (it is called exception code in unmanaged world) and error message

2.       The full stack trace for the unhandled exception

 

These 2 types of information are trivial to obtain in development environment, because the application runs under the VS debugger, the debugger will tell us this information while unhandled exception is caught.

 

In production environment, it may not be practical to install Visual Studio IDE for debugging, because: 1. Visual Studio is not free. 2. VS has a large memory footprint. 3. The problem can even disappear after installing the Visual Studio.

 

Windbg and Process Explorer are 2 free download tools with much less impact on end user.

 

Using Windbg to obtain Application Crash information:

Windbg is included in the Microsoft “Debugging tools for Windows”. You may download the “Debugging tools for Windows” from the link below:

http://www.microsoft.com/whdc/devtools/debugging/default.mspx

 

After installing windbg, the first thing is setting up the symbol path for it. The symbol path is critical during debugging, because windbg will retrieve the Windows system dlls internal symbols from the symbol path to display the meaningful name for functions/variables. Without the correct symbol loaded, the debugger can only display the numeric memory address for the functions/variables. For example, in a windbg without symbol path, if we use “k” command to obtain the current call stack, we will see following output(note the red numeric):

 

0:000> k

*** ERROR: Module load completed but symbols could not be loaded for notepad.exe

ChildEBP RetAddr 

WARNING: Stack unwind information not available. Following frames may be wrong.

0007f6ec 01001fe4 USER32!MessageBoxW

0007f70c 010027e7 notepad+0x1fe4

0007f944 010035b9 notepad+0x27e7

0007f960 7739c3b7 notepad+0x35b9

0007f98c 7739c484 USER32!EnableMenuItem+0x4cd4

0007fa04 7739ca68 USER32!EnableMenuItem+0x4da1

0007fa60 7739ce7a USER32!TranslateMessageEx+0xd5

0007fa88 7c82ec9e USER32!MsgWaitForMultipleObjectsEx+0x126

0007fb08 773966f0 ntdll!KiUserCallbackDispatcher+0x2e

0007fb24 7739668a USER32!DefWindowProcW+0xc7

0007fb6c 010038e2 USER32!DefWindowProcW+0x61

0007fb94 7739c3b7 notepad+0x38e2

0007fbc0 7739c484 USER32!EnableMenuItem+0x4cd4

0007fc38 7739ca68 USER32!EnableMenuItem+0x4da1

0007fc94 7739ce7a USER32!TranslateMessageEx+0xd5

0007fcbc 7c82ec9e USER32!MsgWaitForMultipleObjectsEx+0x126

0007fd3c 773966f0 ntdll!KiUserCallbackDispatcher+0x2e

0007fd58 7739668a USER32!DefWindowProcW+0xc7

0007fda0 010037fc USER32!DefWindowProcW+0x61

0007fdc8 7739c3b7 notepad+0x37fc

 

Yes, the windbg can only recognize the export function names of the dll. Since we do not have the symbol for notepad.exe, the windbg will only display a *magic* number for the function memory address which is not meaningful to us.

 

To set the symbol path, you may set the following path in the windbg menu item File->”Symbol File Path…”:

“srv*DownstreamStore*http://msdl.microsoft.com/download/symbols”

 

 

Change “DownstreamStore” to any local folder to cache the downloaded symbol files on your machine. For example, I normally use “srv*c:\LocalSymbols* http://msdl.microsoft.com/download/symbols”.

 

Now, you may open the windbg and click File->”Open Executable…” and select your application for launching. Windbg will launch your application and break in the initial loader breakpoint. You can check the red rectangle in the figure to ensure the symbol path is correct:


 

You may press “F5” or input “g” command to tell windbg to ignore the initial loader breakpoint. Now, you may manipulate your application to reproduce the crash. When the application crashes with exception, windbg will automatically break-in your application. Then you may input “k” command to get the full stack trace of this exception/failure.

 

Note: the “k” command may require some time to load the symbols from Microsoft symbol server for the system dlls the first time. So the windbg will report “busy…” for sometime. You may wait to have a coffee for the symbol loading and output.

 

Use Process Explorer to obtain application hangs information:

Process Explorer can be free downloaded in the

http://www.microsoft.com/technet/sysinternals/utilities/ProcessExplorer.mspx

 

Process Explorer is especially suitable for troubleshooting hang problem in application, because it has a very good UI for monitor threads activity.

 

After downloading the Process Explorer, the first thing to do is still setting the symbol server path as we do in windbg:

 

 

Note: in the “Dbghelp.dll path” textbox above, I used to Dbghelp.dll in “Debugging Tools for Windows” package, because this Dbghelp.dll has a higher version than the one in “%windir%/System32/”. If you do not have “Debugging Tools for Windows” installed on end user machine, you can live with using the one in “%windir%/System32/” directory.

 

Let’s demonstrate the Process Explorer usage with a sample. In a .Net Winform application, we write the following code in an existing VC application to simulate the hang in application:

 

Sleep(30000);

 

While the application is hang, we can find the application in the Process Explorer and double click it to launch its property dialog. Then, we should switch to the “Threads” tabpage to examine the thread status. This is what I got in the sample:

 

 

As you can see there is only one thread in the test application. Then, we can select the thread and click “Stack” button in the dialog, we got:

 

 

Aha, by examining this stack trace, we can see that “VMQueryHelper” function calls Sleep() function, which causes the hang.

 

Use Windbg to obtain application hangs information:

Windbg can also be used to troubleshoot the application hang. I assume you have downloaded the windbg and set up the symbol path the same way as said in first section.

 

Now, another .Net application is hanging in customer’s machine. We can use windbg to attach the hang application with the menu item below:

 

 

In the dialog, we can choose the hang application and click Ok button:

 

 

In the windbg, the first thing we should do is checking how many threads are there in the hang application with “~*” command, please see the figure below.

 

As you can see in the figure, thread #0 is executing _CorExeMain method, which is .Net Winform application main GUI thread. The thread #5 has a “.” in the front which means the current examine thread. Because the application hang occurs in the main GUI thread, we should switch the current thread to the #0 thread by inputting “~0s”, see figure.

 

Now, the context is switched to the main GUI hang thread. We can input the “K” command to get the full stack trace of the hang thread to see what it is waiting for.

 

All these 3 commands and output are listed in the figure below.

 

 

As you can see it is the Button.Click() method in .Net Winform calls the System.Threading.Thread.Sleep(30000) that caused the hang.

http://blogs.msdn.com/b/msdnts/archive/2006/11/24/how-to-debug-application-crash-hang-in-production-environment.aspx

分享到:
评论

相关推荐

    dynamic-debug-howto动态调试1

    《动态调试技术详解——以Android系统中的dynamic-debug-howto为例》 在计算机软件开发领域,调试是必不可少的一个环节,它帮助我们找出代码中的错误并优化性能。动态调试(Dynamic Debug,简称ddebug)是一种强大...

    Docker: Up & Running: Shipping Reliable Containers in Production 2nd Edition

    you’ll learn how to use Docker to package your applications with all of their dependencies and then test, ship, scale, and support your containers in production. This edition includes significant ...

    A Stock Exchange simulator to show how timers and randon num

    A Stock Exchange simulator to show how timers and randon number generators work together. A cool simulation for anyone who might think about playing the stocks and spending money and get a general ...

    How To Use Adobe Photoshop CS2

    How To Use Adobe Photoshop CS2 <br>Photoshop CS2使用指南 <br>By Daniel Giordan, Doug Nelson <br>Publisher: Sams Publishing <br>Have you ever looked at your color pictures and wished ...

    藏经阁-SparkR under the hood How to debug your SparkR code.pdf

    藏经阁-SparkR under the hood How to debug your SparkR code.pdf

    Anatomy of testing in production

    So you want to test your complex application that involves large-scale distributed systems. But how do you feel about testing it effectively just using your test environment? Today, automated testing ...

    How to config the Python develop environment

    i write a document for how to config the Python 3.0 environment. it can help the Python newer

    Selenium WebDriver Recipes in C#(Apress,2ed,2015)

    How to debug test scripts and test data How to manage and deal with browser profiles and capabilities< How to manage tests for advanced user interactions and experiences (UX) How to work with and ...

    How to use SFTP

    You should ensure that the server's public keys are loaded by the client as described in How to use SFTP (with server validation - known hosts), or you may want to switch off server validation to get ...

    Linux Howto 中文文档

    Linux HOWTO(中文版) BootPrompt-HOWTO (28KB) 启动提示说明 CDROM-HOWTO (27KB) 如何安装, 设定及使用光驱,同时列出支援的硬体. Chinese-HOWTO (39KB) 如何在 Linux 的系统上使用中文?/TD> Config-...

    UE(官方下载)

    This tutorial will show you how to access the information you need in your browser by simply highlighting your text in the edit window and clicking your toolbar button How to install UE3 UE3 is the ...

    Preproduction.Blueprint.How.to.Plan.Game.Environments.and.Level.Designs.2nd.Ed

    This could be for a playable level or a game environment exploration to show off in a portfolio. Planning process is called pre-production and what you end up with is a "Preproduction Blueprint". It...

    Android代码-uCrop

    Made in [Yalantis] (https://yalantis.com/?utm_source=github) [How We Created uCrop] (https://yalantis.com/blog/how-we-created-ucrop-our-own-image-cropping-library-for-android/) Check this [project on...

    Peer to Peer Application Development: Cracking the Code

    Using flow charts and line-by-line analysis of a full-scale P2P file-sharing application, they show you how to solve for typical P2P programming challenges – and create your own sate-of-the-art P2P ...

    RK3399的DRM-HOWTO测试

    RK3399的DRM-HOWTO测试 最近在学习与调试RK3399的DRM显示架构,网上有很多例程,其中介绍比较多的是来于David Herrmann’s Github的drm-howto.开源代码 但这些源码下载后执行make编译出来的执行文件只能在PC机上测试...

    UNIX编程常见问题解答

    1.7 How do I get my program to act like a daemon? 1.8 How can I look at process in the system like ps does? 1.9 Given a pid, how can I tell if it's a running program? 1.10 What's the return value ...

    how to scrolling text 2

    you can know how to scrolling text with C#

    Simple application that shows how to use the Data Control to

    Simple application that shows how to use the Data Control to connect to the Biblio.mdb database and display all authors in the Authors table.

Global site tag (gtag.js) - Google Analytics