Share via


How to create memory dump for SqlException using DebugDiag

Sometimes we in support will ask you to create a dump for when your .Net client crash due to a SqlException.

We do this in order to be able to find out more about the state of the process at the time of the crash.

So today I will show you the simplest way to create that dump with DebugDiag.

First, let us create an application that will simply connect to a database and execute a query that will run for too long and thereby cause a time out exception.

(Normally you do not know what statement is causing the exception and this one of the reasons we ask for dumps, so that we can figure this out.)

So, create a new C# console application called “TimeOutApp” as follows:

        static void Main(string[] args)

        {

            Console.WriteLine("Hit any key to get exception.");

            Console.ReadKey();

            Console.WriteLine("Running...");

            string cs = @"Data Source=<your server>;Integrated security=SSPI;Initial Catalog=<your database>";

            using (SqlConnection con = new SqlConnection(cs))

            {

                con.Open();

                SqlCommand cmd = con.CreateCommand();

                cmd.CommandTimeout = 10; // Lowering from default of 30s. to execption quicker.

                cmd.CommandText = "waitfor delay '00:00:20';";

               cmd.ExecuteNonQuery();

                con.Close();

            }

        }

and run it. This should then produce the exception:

Unhandled Exception: System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)

   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)

   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)

   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)

   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

From this we can’t figure out what SQL command was executing. Of course we could trace this on server side, but that is another story. And you may not have access to the server.

So, time for some dump taking.

. Go to the download site for DebugDiag.

"Debug Diagnostic Tool v1.1"

https://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3&displaylang=en

. Download and install it on the machine that is running the crashing application.

. Start your application (“TimeOutApp” in this case).

. Start DebugDiag.

. You should now be shown a dialog called “Select Rule Type”, select “Crash” and “Next”.

. You should now be shown a dialog called “Select Target Type”, select “A specific process” and “Next”.

. You should now be shown a dialog called “Select Target”, this should list all running processes, so select your application (“TimeOutApp” in this case) and “Next”.

. You should now be shown a dialog called “Advanced Configuration”, click “Exceptions” in the “Advanced Settings” section.

. You should now be shown a dialog called “First Chance Exception Configuration”, click “Add Exception”.

. You should now be shown a dialog called “Configure Exception”. Set this up as follows.

Exception Code = E04334F4D (Just click it in the list to the left)

.Net Exception Type = System.Data.SqlClient.SqlException

Action Type = Full Userdump

Action Limit = 3 (this is so that we get more than one dump if needed).

  then click OK. This should now list the added rule. So click “Save & Close”

. You should now be back at the “Advanced Configuration”, click “Next”.

. You should now be shown a dialog called “Select Dump Location And Rule Name”, change “Userdump Location” to “C:\Dumps” and click “Next”.

. You should now be shown a dialog called “Rule Completed”, select “Activate the rule now” and click “Finish”.

That’s it.

Now reproduce your problem (just hitting any key in the “TimeOutApp” in this case).

When the application has crashed, then the “Userdump Count” should have increased from 0.

Have a look in the “C:\Dumps” directory; this should contain files with the name looking something like this:

TimeOutApp__PID__2068__Date__12_17_2009__Time_04_02_39PM__xxxxxx.dmp

Compress the whole directory and upload it to us for analysis.