How to catch unhandled exception from .Net application with procdump (or similar)?
Asked Answered
S

2

3

The long (boring) story

Currently i have an application that leads to an exception on only one pc. After some digging around i could encapsulate the problem with a small sample application, but the true reason is still hiding.

Due to the fact that on this pc is no Visual Studio installed nor we are able to do so i searched for another solution to find the true reason.

In a first approach i stripped down my small application more and more by just carefully reading the exception message, compare to the code and try & error to come to the concrete line of problem. But that wouldn't help to get all the informations about what are the current values of all the used variables etc.

So i searched for another better way to get the informations i need. I already read about Dumps and Minidumps about a long time but never need them, cause till now i could always reproduce the user described scenario on my developer or test machine.

But this time there seems to be only one pc having the problem and unfortunately changing the complete machine or installing everything new there isn't an option.

To get familiar on how to work with dumps i just wrote a simple c# test application, containing a single button which does nothing more than throw new ArgumentException("Test");

Now i need to create such a magical dump file after pushing the malicious button, read it into Visual Studio 2008 Professional and can look at it like a normal running application within VS except that Step next, etc. won't work.

As far as i know the best tool to create a dump at a specific point in time is procdump, due to the possibility you can define when one or multiple dumps are taken.

So i simply downloaded it and started it by calling procdump -o -e MyApp.exe d:\MyApp.dmp. It claims that MyApp.exe doesn't exists. Okay, my fault. Just start MyApp first and afterwards procdump.

Procdump is running now showing me all its options it uses and seems to await an unhandled exception. Nothing easier than this, i simply push my malicious button ... and nothing happens in procdump.

Instead a dialog window from my application pops up that explains that an unhandled exception occured (surprise, surprise) and what i'd like to do (Details, Continue, Quit). But no matter what i select, procdump isn't able to create a dump file automatically.

If i go and just call procdump -o MyApp.exe d:\MyApp.dmp when the dialog is showing the dump file seems to be useless, cause after opening it in VS the call stack is just hanging around somewhere in ntdll.dll but nowhere within my code (i would guess that this is the MessageQueue of the dialog waiting for some mouse clicks).

If i take a closer look into the details you'll find some informations about how to delegate the unhandled exception to a JIT-debugger. But i don't want a JIT-debugger, i'd like to crash the application to get the dump file.

After these tries i found ClrDump, but that didn't produce any better dumps (if i load it into VS and take a look at the call stack).

With these informations taken into account you are now (hopefully) able to provide me with a (working) solution for my .Net application:

The (short) question

How i'm able to create a dump file when an unhandled exception occurs within an .net application, that i'm able to load MyApp .pdb files and see under which circumstances the exception was thrown?

The answer

With the help of Naveen and Lex Li i could get a little more insight on how debugging with crash dumps works. And now i just like to recap all the stuff, that is needed to get it to work:

Whenever you like to get a dump of a process, you can select between several tools to get the job done:

  • Procdump
    • easy command line tool that can create dumps in complicated scenarios, but it does not work to catch .net unhandled exceptions.
  • DebugDiag
    • easy graphical tool that can create dumps at crashes (even .net exceptions), but it can't create dumps at advanced scenarios like Procdump.

As you can see you'll get two tools that are capable to create dumps under different circumstances, so both are more partners then rivals in creating a dump at the needed point in time.

After creating a dump with one of the above tool it's time to analyze the dump to find out the reason for the problem. For analyzing you can grab WinDbg. It is part of the Debugging Tools for Windows and can be get from Microsoft. Unfortunately is the entry-barrier of WinDbg quite high but it is really powerful. Maybe take a look into this blog to get a better understanding on how to use this tool.

If you have a .Net 4 application and using Visual Studio 2010 you can also use this for analyzing. It's much easier to use, due to the better graphical user interface, but it doesn't have the power of WinDbg. To get a better comparison, you should take a look into this article.

Last but not least you can also use the sos.dll in Visual Studio 2008. Here is the article describing what you can do with it.

Sloane answered 24/1, 2011 at 9:55 Comment(1)
I can't believe ProcDump should not work for .NET processes. For SOS to work, you'll need the -ma switch (full memory). I tried it for the answer at #24874527Seedbed
P
3

DebugDiag is one of the easiest way to get memory dump based on an exception.

Look for "Configure Exception Dialog" help section within debugdiag to generate a dump based on exception.

Below is an example to generate full memory dump based on ArgumentException

enter image description here

Perspicacious answered 24/1, 2011 at 19:3 Comment(7)
@Naveen: I tested DebugDiag and it is a really cool tool. Unfortunately it also just brings me down to the unmanaged code. Isn't there any possibility to get a dump that shows the managed stack and not the unmanaged?Sloane
@Oliver: The dump should have managed as well as native stacks. Are you using an extension like sos/sosex to get the managed stacks?Perspicacious
@Naveen: Currently i just have the .dmp file, Visual Studio and DebugDiag. What is additionally needed to read the managed part of the dump file?Sloane
@Oliver: What you need is Windbg. Here are some good debugging tutorials from Tess blogs.msdn.com/b/tess/archive/2008/02/04/… which should help you in diagnosing the issue with Windbg. Let me know if you need any help.Perspicacious
@Naveen: Thanks for the link to Tess blog. It is a very good source for starting with WinDbg. So i'm able to see the Stack and some other informations in WinDbg, but the debugging is such an uncomfortable process, than live debugging in VS. So i'm wondering if there isn't anything in VS (maybe 2010) that you can explore a crash-dump like a live debug session.Sloane
@Oliver: FYI VS2010 can do dump debugging. But it is available only for .NET 4.0 memory dumps. Here is post on that blogs.msdn.com/b/tess/archive/2009/06/16/…Perspicacious
@Naveen: That's a really interesting link. Seems that i need to switch on .Net 4 and 2010 to get the comfort i like and for now i have to check how far i come with DebugDiag and WinDbg.Sloane
P
1

DebugDiag or WinDbg is OK.

http://support.microsoft.com/kb/919789

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

Pip answered 24/1, 2011 at 11:49 Comment(1)
The problem is not the view of a dump file. The problem is to create the dump at the correct point in time. So how can i tell my .Net application to really crash (to get procdump to work) instead of starting some jit-debugger or the unhandled exception dialog? Or must i start some kind of jit-debugger and create the dump from there? If yes, how to do this?Sloane

© 2022 - 2024 — McMap. All rights reserved.