Creating a core dump on clr exception thrown - c#

I'm trying to create a dump using windbg every time a specific clr exception is THROWN.
I know about !soe, but I don't want the execution to stop, I just want it to create a dump.
Is this possible?
If it's not possible using windbg, I'm also open to other recommendations on how to achieve this...

Try procdump. Maybe use the option "-e 1" to capture a dump on first-chance exceptions.
Edit:
For specific exceptions, you may need ADPlus. See this post for example.
More edits:
Also possible without ADPlus: In Windbg -> Debug -> Event Filters, set CLR exception to ignored - not handled. Click the Commands button and then enter the following (with your particular exception type of course):
!soe System.ArgumentNullException 1;.if(#$t1==0) {g} .else {.dump /ma /u d:\ANE.dmp}

In WinDbg, 'Debug > Event Filters', find the specifc exception you want to execute a command for or add a numbered exception.
Then click 'Commands' and enter:
.dump /ma c:\mydump.dmp
in the command box, you can even enter a second command for second chance exception handling and this will write a full mini-dump when the exception event is triggered.

Related

How to debug COM exceptions, caused by Excel Interop, in C# program?

I'm working on a C# application, trying to use Excel features. In order to do that, I'm working with Interop as a library. I have an exception in following line of source code:
Range rng_Destination =
xlApp.get_Range(xlWrkSht.Cells[2,1],
xlWrkSht.Cells[xlWrkSht.Cells[1,1].CurrentArray.Rows.Count+1,
dt_main.Columns.Count]);
In order to investigate this, I've tried to add xlWrkSht.Cells[2,1] to Visual Studio's Watch window, but this just shows {System.__ComObject}, which is not very useful.
So, I tried another approach, using this kind of source code:
System.Diagnostics.Debug.WriteLine("xlWrkSht.Cells[2 ,1]: [%s]",
xlWrkSht.Cells[2, 1]);
This, however, causes following Exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500
Message=Cannot dynamically invoke method 'WriteLine' because it has
a Conditional attribute
Source=<Cannot evaluate the exception source>
StackTrace:<Cannot evaluate the exception stack trace>
So my question is very simple: how can I debug Interop related objects, like the mentioned xlWrkSht.Cells[2, 1]?
For your information: I already tried unchecking "Just my code" but this did not help until now (I found this somewhere on the internet, I have no idea what it does):
I've been looking for other resources about Interop debugging, but the information was far too general to be useful for my specific case.
I've also seen some posts on the internet, informing about the event viewer but there, I just have some "Information" and at most some "Warning" messages, no "Error" ones (I've been looking in "Windows Logs, Application/Security and System".
Edit after some more investigation
Meanwhile I've seen that I can find the value of that cell, using:
xlWrkSht.Cells[2, 1]
=> Dynamic View
FormulaLocal
That last attribute contains the value.
However, the following issue is that I'll need to get the Excel array this cell belongs to:
xlWrkSht.Cells[2, 1].CurrentArray.
This one seems not to be accessible, even not using "Dynamic view". Any ideas?

LoadLibrary() error code 998

I’ve got a c++ dll written in visual studio 2012. I’d like to load it in a C# program(VS2012 too) with using of LoadLibrary function.
I set the SetLastError = true to get the probabilistic returned error code by LoadLibrary. When I run my C# program I always receive 998 error code but I can’t understand what’s wrong with it!
Should you help me to resolve this problem,please?
Any help would be appreciated.
(It should be mentioned that I saw some posts about error code 998 but none of them couldn't give me a solution.)
In response to this comment "please tell me how can I do this instruction":
To troubleshoot the LoadLibrary() failure, run the application under a
debugger and enable first chance exception handling for the C0000005
Access Violation exception. If an access violation occurs when the
LoadLibrary() function is called, the application will break into the
debugger. The debugger's call stack can then be used to trace where
the exception occurred. The stack trace should help you narrow down
the actual problem related to the exception being encountered.
Open your project in Visual Studio
In the menu, click Debug > Exceptions
In the Exceptions window, click Find... and enter C0000005 click Ok
Check the box next to the highlighted exception under the column Thrown.
Now, when you debug your program and the exception is thrown, it will break and you should be able to at least inspect the exception details of the thrown exception.
It's likely you'll be thrown into disassembly window so you may not see any readable code. If the exception detail isn't enough, you could try decompiling with Dot Peek.
Here's a tutorial on enabling Dot Peek as a symbol server. Doing this will hopefully decompile the library on-the-fly so you can start inspecting the line of code that has caused the exception.

When should we capture the DUMP file for debugging

Let's say I have a .NET application that crashes when I close it and I want to use DebugDiag to see what is going on. So we should create a dump file. My question is When do we create this dump file? Should I create it when I start the application? well if I do that it says a dump file has been created at this temp location...ok..then I continue working the application and make it to crash, but my confusion is that well it created the dump file very early, but crash happened at the end, so how even that dump file can be helpful? does it like get updated automatically once we are working with that application?
For a crash (which is usually an unhandled exception) you should create the dump file when the exception becomes a second chance exception. I am not very familiar with DebugDiag, but tools such as adplus (Debugging Tools for Windows) and ProcDump will both let you create dumps for this.
In some cases you may want to create dumps for first chance exceptions as well (i.e. before any code gets to handle the exception). E.g. if the exception which causes the crash wraps some other exception you may not have enough information at that point. In those cases creating dump files on first chance exceptions will give you detailed information on the state of the original exception.
You can configure windows to create dump files when the process crashes. This feature is not enabled by default: Collecting User-Mode Dumps. The process is different for Windows XP: Capturing Application Crash Dumps. Once you have the dump file you can open it and investigae with WinDbg. I highly recommend blog by Tess Fernandez and this book.

Traceback StackOverflow Exception

I've got an Umbraco site using a fair ammount of usercontrols in which, somewhere - somehow, a stackoverflow exception gets thrown every now and then.
Since the SOE doesn't occur when testing is has something to do with a user posting or getting some information (fair enough: something I've missed).
How can I trace back where my stackoverflow exception took place inside my code? Are there tools available to check my sources to see if I've missed some recursive method? Or how can I debug the running process?
I have found that debugging the application (sometimes having to attach VS to the correct w3wp.exe process) and setting VS to break on exception is the most helpful. You could still see the stack trace which is full of the same series of method calls.
If the application crashes, enable this registry setting to create a crashdump for all crashing apps:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps]
"DumpFolder"="C:\\TEMP"
"DumpCount"=dword:00000010
"DumpType"=dword:00000002
It will create a crash dump in your C:\Temp folder. Either open this dump file in a newer version of Visual Studio which will show you the exact code it crashed (just like if you where debugging and you got an unhandled exception).
Or an extremely useful MS tool for all your dump analysis needs: Debug Diagnostics Tools (https://www.microsoft.com/en-us/download/details.aspx?id=49924). Video walkthrough here: https://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-123-DebugDiag-Part-3
This will analyse your crashed application and show you all the threads and their managed and unmanaged stacktraces.
It will likely point out exactly the issue of your crash and where it occurred so you can check that stacktrace.
If however the program did not crash, you can open task manager and rightclick the process and create a dump file manually. Sending this dump file through Debug Diagnostics on a crash analysis it will show you the last X exceptions that did occur in your application with stacktraces. I use this method to find the actual error of user applications when the user gets a nice simplified error message. As an alternative to creating a dump from taskmanager you can also use procdump.exe or Debug Diagnostics itself to create rules on when to create dump files.
I really believe more people should be aware of how incredible easy a DUMP file together with Debug Diagnostics is to find any error that occurred in a production environment, where we don't have our handy development tools available.

How to troubleshoot .NET 2.0 Error Reporting messages in the event log?

I work on an open source product called EVEMon written in C# targeting the .NET 2.0 platform, I have one user who is suffering from a strange .NET crash that we have been unable to resolve.
Event Type: Error
Event Source: .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID: 5000
Date: 4/29/2009
Time: 10:58:10 PM
User: N/A
Computer: removed this
Description:
EventType clr20r3, P1 evemon.exe, P2 1.2.7.1301, P3 49ea37c8, P4
system.windows.forms, P5 2.0.0.0, P6 4889dee7, P7 6cd3, P8 18, P9
system.argumentexception, P10 NIL.
Data:
//hex representation of the above Description
The application itself crashes with out displaying an error (despite having a error handling UI), the above messages was copied out of the Windows Event log. The end user has re-installed .NET and updated to the latest versions. The .PDB files are distributed with every release version of the program to aid in debugging and testing, the user with the problem in question has the full complement of PDB files for the correct version of EVEMon.
Is there a specific, tried and tested technique to analyse and diagnose this type of crash? and if so what tools and technologies are available to aid in debugging?
Special Thanks
I would like to give special thanks to Steffen Opel and highlight that his answer whilst not directly answering the question I was asking, addressed the bigger issue with my code base that the global error handling was missing an important component.
This is how I would tackle the problem for a end user with a crash.
Download and install Debugging Tools for Windows at http://www.microsoft.com/whdc/devtools/debugging/default.mspx
Once the tools are installed (they end up going to C:\Program Files\ by default) start a command line window.
Change to the directory which contains adplus (e.g "C:\Program Files\Debugging Tools for Windows (x86)").
Run the follwing command. This will start the application and attach adplus.
adplus -crash -o C:\debug\ -FullOnFirst -sc C:\path\to\your\app.exe
After the crash dump is created
Once the application crashes start WinDbg and load the .dmp file that is created in C:\debug. (File --> Open Crash Dump)
Execute these commands to see the stack trace and hopefully find the problem.
To load SOS for debugging
Pre .NET 4.0
.loadby sos mscorwks
.NET 4.0
.loadby sos clr
To see the stack trace
!clrstack
To see a more useful stack trace
!clrstack –p
To poke inside an object..perhaps see what caused the exception
!do <address>
e.g This is the result from a application that faulted randomly with an IO exception. WinDbg pointed out the path that was being referenced which was incorrect.
0:009> !do 017f2b7c
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 124(0x7c) bytes
(C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: \\server\path\not_here.txt
Fields:
MT Field Offset Type VT Attr Value Name
79102290 4000096 4 System.Int32 1 instance 54 m_arrayLength
79102290 4000097 8 System.Int32 1 instance 53 m_stringLength
790ff328 4000098 c System.Char 1 instance 5c m_firstChar
790fd8c4 4000099 10 System.String 0 shared static Empty
>> Domain:Value 00161df8:790d884c <<
7912dd40 400009a 14 System.Char[] 0 shared static WhitespaceChars
>> Domain:Value 00161df8:014113e8 <<
Peeking into your source code (trunk) indicates that your unhandled exception handling seems to be incomplete in regard to Windows Forms applications:
You need to handle both non-UI thread exceptions and UI thread exceptions:
For the former you need to implement a CLR unhandled exception handler via AppDomain.CurrentDomain.UnhandledException, which is in place already.
For the latter you need to implement a Windows Forms unhandled exception handler via Application.ThreadException, which seems to be missing; this could indeed yield exactly those problems you are witnessing. For an implementation example see MSDN documentation of Application.ThreadException Event.
Please note that right now you explicitly suppress catching unhandled Windows Forms exceptions via Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException), you'll need to change this to UnhandledExceptionMode.CatchException to enable routing to your handler for Application.ThreadException, as correctly suggested by Jehof already.
What OS (Windows XP, Windows Vista, etc.) does the user use?
If Windows Vista try to disable "Problem Reports and Solutions feature" (Control Panel-->Problem Reports and Solutions-->Change Settings-->Advanced Settings-->Turn off for my programs, problem reporting)
Or try to set
Application.SetUnhandledExceptionMode( UnhandledExceptionMode.CatchException );
This will always route exceptions to the ThreadException handler.
In a nutshell: there is an unhandled exception in the application.
If you have access to the machine (through remote access, etc.), try installing Visual Studio Express and starting the application. You should see a dialog offering the chance to debug the application with a new instance of Visual Studio.
It may also be that there is something preventing Windows Forms from properly initializing. I've seen forum posts that suggest that font issues can cause this -- ensure that the users have the fonts installed that your application needs plus the usual defaults such as MS SansSerif, Arial, Tahoma, Times and suchlike.
And failing that... try sacrificing a chicken over the PC. Works a charm every time!
We've had issues with Exceptions in Thread-Code. If you spawn a new Thread and forget to handle an exception in the thread method, the application just "stops" - no error message, no nothing, but only an entry in the Event Log. Not even then UnhandledExceptionHandler is triggered.
Maybe something like this is the cause?
...if you are able to contact that suffering user, here is an
Idea: log pre-execution stages
Instead of making a shortcut to your program.exe, make a shortcut to program.bat, which will
echo "Pre-start" > stage.txt
start program.exe
The first line of Program.cs will therefore be
File.WriteAllLines("stage.txt", "Program execution started.");
In the handler of AppDomain.UnhandledException first line will be
File.WriteAllLines("stage.txt", "Unhandled exception has been caught.");
Also, make sure that the handler does not allocate memory or resources — pre-allocate them on programs start. Handler only trigger the writing to the log.
Comments
It is very likely that the stage.txt (sent by the user) will contain "Pre-start". This happens when an exception is thrown in 3rd party .dll — even before your program has started.
In that case you will need a simple checker program, which will not reference the assemblies that you program.exe does, but will Assembly.Load(...) them.
P.S.
stage.txt should be placed somewhere under %APPDATA%, not in Program Files.
I found an interesting case on Server 2003 and another nice discussion.
You should get a more detailed stack trace by sending the .pdb file for that particular release to the user (to be put next to the .exe) and having them reproduce the crash.
You should handle AppDomain.UnhandledException in code.
There was a similar question asked. See related ones too.

Categories