Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug a .NET EXE file that crashes immediately

I'm working with a managed EXE file that crashes immediately when run. Usually I'd expect a dialog which allows an option to launch the debugger, but no such luck in this case. Also, the program crashes too quickly for me to use attach to process in Visual Studio.

What is the solution?

like image 976
James Cadd Avatar asked Feb 19 '10 20:02

James Cadd


People also ask

How do you debug a crashed application?

You can use the stack trace available in the report details if you are using Play Console or the output of the logcat tool. If you don't have a stack trace available, you should locally reproduce the crash, either by manually testing the app or by reaching out to affected users, and reproduce it while using logcat.

How do I debug a Windows executable?

Just use File/Open Project/Solution, select EXE file and Open it. Then select Debug/Start debugging. The other option is to run the EXE first and then Select Debug/Attach to process.


1 Answers

If you have WinDbg installed, use menu FileOpen Executable to open the application directly under the debugger and automatically break immediately.

You can then use the commands under Debug (i.e., Go) to execute it normally and debug it. Also load the SOS Extensions. Not as nice as Visual Studio, but useful if you only have the EXE (and hopefully the PDB, although that's optional) and no source.

Example: This is my source code, which we assume is unavailable:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        int x = 10 - 10;
        int i = 2000/x;

        Application.Run(new Form1());
    }

This crashes immediately, with no chance for you to attach a debugger in time. This is the WinDbg output after hitting "Run":

Removed dead ImageShack link - Freehand circles by me

After loading SOS.dll, you can use !DumpStack to see where the Exception was thrown:

Removed dead ImageShack link - No Freehand Circles, sorry!

Note that JIT or compiler optimizations may cause methods to be inlined which may make the StackTrace not 100% reliable, but for a quick overview it works.

WinDbg is a bit arcane, but once you got some basics done it's awesome and at least helps finding the root of the issue.

like image 166
Michael Stum Avatar answered Sep 27 '22 18:09

Michael Stum