title: How to find out what is throwing an exception using windbg
body: <p>I have a c# windows service that is throwing an exception.<br>
  I have launched this service using windbg and have let it run until the exception is thrown.</p>
  
  <p>WHen the debugger stops I do:</p>
  
  <ol>
  <li><strong>prefer_dml 1</strong></li>
  <li><strong>!threads.</strong>  This prints out the threads and I click on the Exception in question.</li>
  <li><p>I click on the <strong>_message</strong> and it displays:</p>
  
  <p>0:018> !DumpObj /d 0105e134
  Name:        System.String
  MethodTable: 79b9f9ac
  EEClass:     798d8bb0
  Size:        120(0x78) bytes
  File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
  String:      Object reference not set to an instance of an object.</p></li>
  <li><p>THen I do: <strong>!clrstack</strong> and get the following:</p>
  
  <p>06f8e090 04d48299 AD.Intellex.DriverService.ServerComponentManager.StopCrossFire()
  06f8e098 04d48214 AD.Intellex.DriverService.WindowsService.OnStop()
  76b5e8\System.ServiceProcess.ni.dll
  06f8e108 04d48177 AD.Intellex.DriverService.WindowsService.<strong>UnhandledExceptionEventHandler</strong>(System.Object, System.UnhandledExceptionEventArgs)
  06f8e4cc 791421db [GCFrame: 06f8e4cc] 
  06f8e568 791421db [GCFrame: 06f8e568] 
  06f8e63c 791421db [GCFrame: 06f8e63c] 
  06f8e740 791421db [GCFrame: 06f8e740] 
  06f8f234 791421db [HelperMethodFrame_PROTECTOBJ: 06f8f234]     System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
  06f8ec78 04d47f23 AD.HardwareInterface.IntellexHardwareInterface.ObjectProcessor.<strong>CameraAlarmActivate</strong>(Int32, SoftwareHouse.CrossFire.Common.DataServiceLayer.DataServiceObject, AD.Common.VideoObjectDefinitions.CameraAlertStatus, Boolean, System.DateTime, System.Collections.Generic.Dictionary`2 ByRef)
  06f8edb8 04d46db3 AD.HardwareInterface.IntellexHardwareInterface.IntellexProcessor.ProcessCameraAlarm(System.Object)
  06f8edf4 79b2d871 System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
  06f8edfc 79ab4db5 System.Threading.ExecutionContext.runTryCode(System.Object)
  06f8f234 791421db [HelperMethodFrame_PROTECTOBJ: 06f8f234] System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
  06f8f298 79ab4cba System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
  06f8f2b0 79ab7fc2 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
  06f8f2d4 79af2b66 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
  06f8f2e8 79af23f3 System.Threading.ThreadPoolWorkQueue.Dispatch()
  06f8f334 79af2299 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  06f8f6f4 791421db [DebuggerU2MCatchHandlerFrame: 06f8f6f4]</p></li>
  </ol>
  
  <p>If you read up the ways you will see that the function that throws the exception is <strong>ObjectProcessor.CameraAlarmActivate</strong></p>
  
  <p>The code for this function is as follows:</p>
  
  <pre><code> public override void CameraAlarmActivate(int cameraNumber, DataServiceObject dso, AD.Common.VideoObjectDefinitions.CameraAlertStatus status, bool isBegin, DateTime time, out Dictionary&lt;string, object&gt; msgFormatParameters)
      {
          msgFormatParameters = null;
          try
          {   
              _alarmMutex.WaitOne();
              Type type = TypeManager.Instance["SoftwareHouse.NextGen.Common.SecurityObjects.VideoCamera"];
              if (type == null) return;
  
              DataServiceObject camera = ClientServerConnection.Instance.FindObject(type, "Number = ? AND ServerID = ?", new object[] { cameraNumber, (int)dso[CameraKey.ObjectID] }, true) as DataServiceObject;
              if (camera != null)
              {
                  string alert = status.ToString();
                  List&lt;string&gt; alarmList = CheckIntellexCameraContainer(camera, dso, alert);
  
                  if(alarmList != null &amp;&amp; alarmList.Count &gt; 0)
                  {
                      bool alarmToJournal = false;
                      // If alarm is Motion...
                      if (status == AD.Common.VideoObjectDefinitions.CameraAlertStatus.Motion &amp;&amp; alarmList.Contains("motion"))
                      {
                          alarmToJournal = true;
                          if (isBegin)
                          {
                              // Set the Motion property to true.
                              camera["Motion"] = true;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Motion" }, new object[] { true });                                
                          }
                          else
                          {
                              // Set the Motion property to false.
                              camera["Motion"] = false;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Motion" }, new object[] { false });
                          }
                      }
  
                      // If alarm is VideoLoss...
                      if (status == AD.Common.VideoObjectDefinitions.CameraAlertStatus.VideoLoss &amp;&amp; alarmList.Contains(status.ToString().ToLower()))
                      {
                          alarmToJournal = true;
                          if (isBegin)
                          {
                              // Set the Videoloss property to true.
                              camera[CameraKey.Videoloss] = true;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { CameraKey.Videoloss }, new object[] { true });
                          }
                          else
                          {
                              // Set the Videoloss property to false;
                              camera[CameraKey.Videoloss] = false;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { CameraKey.Videoloss }, new object[] { false });
                          }
                      }
  
                      // If alarm is Light...
                      if (status == AD.Common.VideoObjectDefinitions.CameraAlertStatus.LightChange &amp;&amp; alarmList.Contains("light"))
                      {
                          alarmToJournal = true;
                          if (isBegin)
                          {
                              // Set the Light property to true.
                              camera["Light"] = true;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Light" }, new object[] { true });
                          }
                          else
                          {
                              // Set the Light property to false;
                              camera["Light"] = false;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Light" }, new object[] { false });
                          }
                      }
  
                      // If alarm is Perimeter...
                      if (status == AD.Common.VideoObjectDefinitions.CameraAlertStatus.PerimeterProtection &amp;&amp; alarmList.Contains("perimeter"))
                      {
                          alarmToJournal = true;
                          if (isBegin)
                          {
                              // Set the Perimeter property to true.
                              camera["Perimeter"] = true;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Perimeter" }, new object[] { true });
                          }
                          else
                          {
                              // Set the Light property to false;
                              camera["Perimeter"] = false;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Perimeter" }, new object[] { false });
                          }
                      }
  
                      // If alarm is AlarmIn(DryContact)...
                      if ((status == AD.Common.VideoObjectDefinitions.CameraAlertStatus.ExternalAlarm || status == AD.Common.VideoObjectDefinitions.CameraAlertStatus.DryContact) &amp;&amp; alarmList.Contains("alarmin"))
                      {
                          alarmToJournal = true;
                          if (isBegin)
                          {
                              // Set the AlarmIn property to true.
                              camera["Alarmin"] = true;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Alarmin" }, new object[] { true });
                          }
                          else
                          {
                              // Set the AlarmIn property to false;
                              camera["Alarmin"] = false;
                              ClientServerConnection.Instance.UpdateObject(camera.ObjectKey, new string[] { "Alarmin" }, new object[] { false });
                          }
                      }
  
                      if(alarmToJournal)
                          msgFormatParameters = CreateMessageParameters(status, camera, dso, time);
                  }
              }
          }  
          catch (Exception)
          {
          }
          finally
          {
              _alarmMutex.ReleaseMutex();
          }
      }
  </code></pre>
  
  <p>I want to look at this function in the debugger so I select the IP value for the function from the output of !clrstack. (04d47f23)</p>
  
  <p>I get the following:</p>
  
  <pre><code>0:018&gt; !U /d 04d47f23
  Normal JIT generated code
   AD.HardwareInterface.IntellexHardwareInterface.ObjectProcessor.CameraAlarmActivate(Int32, SoftwareHouse.CrossFire.Common.DataServiceLayer.DataServiceObject, AD.Common.VideoObjectDefinitions.CameraAlertStatus, Boolean, System.DateTime, System.Collections.Generic.Dictionary`2&lt;System.String,System.Object&gt; ByRef)
  Begin 04d474d0, size a79
  04d474d0 55              push    ebp
  04d474d1 8bec            mov     ebp,esp
  04d474d3 57              push    edi
  04d474d4 56              push    esi
  04d474d5 53              push    ebx
  04d474d6 81ec10010000    sub     esp,110h
  04d474dc 8bf1            mov     esi,ecx
  04d474de 8dbd78ffffff    lea     edi,[ebp-88h]
  04d474e4 b91e000000      mov     ecx,1Eh
  04d474e9 33c0            xor     eax,eax
  04d474eb f3ab            rep stos dword ptr es:[edi]
  04d474ed 8bce            mov     ecx,esi
  04d474ef 33c0            xor     eax,eax
  04d474f1 8945e8          mov     dword ptr [ebp-18h],eax
  04d474f4 898d74ffffff    mov     dword ptr [ebp-8Ch],ecx
  04d474fa 8bfa            mov     edi,edx
  04d474fc 8b5d18          mov     ebx,dword ptr [ebp+18h]
  04d474ff 8b4508          mov     eax,dword ptr [ebp+8]
  04d47502 33d2            xor     edx,edx
  04d47504 8910            mov     dword ptr [eax],edx
  04d47506 8b8574ffffff    mov     eax,dword ptr [ebp-8Ch]
  04d4750c 8b4834          mov     ecx,dword ptr [eax+34h]
  04d4750f 8b01            mov     eax,dword ptr [ecx]
  04d47511 8b402c          mov     eax,dword ptr [eax+2Ch]
  04d47514 ff500c          call    dword ptr [eax+0Ch]
  04d47517 e8d4ad4bfe      call    032022f0 (SoftwareHouse.CrossFire.Common.Core.TypeManager.get_Instance(), mdToken: 010ACF2C)
  04d4751c 8bc8            mov     ecx,eax
  04d4751e 8b158c28b501    mov     edx,dword ptr ds:[1B5288Ch] ("SoftwareHouse.NextGen.Common.SecurityObjects.VideoCamera")
  04d47524 3909            cmp     dword ptr [ecx],ecx
  04d47526 e8e5d04bfe      call    03204610 (SoftwareHouse.CrossFire.Common.Core.TypeManager.get_Item(System.String), mdToken:     010ACF2C)
  04d4752b 8945dc          mov     dword ptr [ebp-24h],eax
  04d4752e 8bc8            mov     ecx,eax
  04d47530 33d2            xor     edx,edx
  04d47532 e853134274      call    clr!RuntimeTypeHandle::TypeEQ (7916888a)
  04d47537 85c0            test    eax,eax
  04d47539 7418            je      &lt;Unloaded_avcodec-53.dll&gt;+0x8e7553 (04d47553)
  04d4753b c745e400000000  mov     dword ptr [ebp-1Ch],0
  04d47542 c745e8fc000000  mov     dword ptr [ebp-18h],0FCh
  04d47549 68377fd404      push    offset &lt;Unloaded_avcodec-53.dll&gt;+0x8e7f37 (04d47f37)
  04d4754e e9c7090000      jmp     &lt;Unloaded_avcodec-53.dll&gt;+0x8e7f1a (04d47f1a)
  04d47553 e878ca4bfe      call    03203fd0 (SoftwareHouse.CrossFire.Common.ClientInterfaceLayer.ClientServerConnection.get_Instance(), mdToken: 010ACF2C)
  04d47558 898534ffffff    mov     dword ptr [ebp-0CCh],eax
  04d4755e ba02000000      mov     edx,2
  04d47563 b9e2428879      mov     ecx,offset mscorlib_ni+0x42e2 (798842e2)
  04d47568 e853acc3fb      call    009821c0 (JitHelp: CORINFO_HELP_NEWARR_1_OBJ)
  04d4756d 89856cffffff    mov     dword ptr [ebp-94h],eax
  04d47573 b97829ba79      mov     ecx,offset mscorlib_ni+0x322978 (79ba2978) (MT:   System.Int32)
  04d47578 e8a3aac3fb      call    00982020 (JitHelp: CORINFO_HELP_NEWSFAST)
  04d4757d 8bf0            mov     esi,eax
  04d4757f 8b856cffffff    mov     eax,dword ptr [ebp-94h]
  04d47585 8945b0          mov     dword ptr [ebp-50h],eax
  04d47588 897e04          mov     dword ptr [esi+4],edi
  04d4758b 56              push    esi
  04d4758c 8b8d6cffffff    mov     ecx,dword ptr [ebp-94h]
  04d47592 33d2            xor     edx,edx
  04d47594 e8fb584574      call    clr!JIT_Stelem_Ref (7919ce94)
  04d47599 8b15002ab501    mov     edx,dword ptr ds:[1B52A00h] ("ObjectID")
  04d4759f 8b4d1c          mov     ecx,dword ptr [ebp+1Ch]
  04d475a2 8b01            mov     eax,dword ptr [ecx]
  04d475a4 8b4034          mov     eax,dword ptr [eax+34h]
  04d475a7 ff5014          call    dword ptr [eax+14h]
  04d475aa 8bf8            mov     edi,eax
  04d475ac 813f7829ba79    cmp     dword ptr [edi],offset mscorlib_ni+0x322978 (79ba2978)
  04d475b2 740c            je      &lt;Unloaded_avcodec-53.dll&gt;+0x8e75c0 (04d475c0)
  04d475b4 8bd7            mov     edx,edi
  04d475b6 b97829ba79      mov     ecx,offset mscorlib_ni+0x322978 (79ba2978) (MT: System.Int32)
  04d475bb e85e614274      call    clr!JIT_Unbox (7916d71e)
  04d475c0 b97829ba79      mov     ecx,offset mscorlib_ni+0x322978 (79ba2978) (MT: System.Int32)
  04d475c5 e856aac3fb      call    00982020 (JitHelp: CORINFO_HELP_NEWSFAST)
  04d475ca 8bf0            mov     esi,eax
  04d475cc 8b856cffffff    mov     eax,dword ptr [ebp-94h]
  04d475d2 8945ac          mov     dword ptr [ebp-54h],eax
  04d475d5 8b4704          mov     eax,dword ptr [edi+4]
  04d475d8 894604          mov     dword ptr [esi+4],eax
  04d475db 56              push    esi
  04d475dc 8b8d6cffffff    mov     ecx,dword ptr [ebp-94h]
  04d475e2 ba01000000      mov     edx,1
  04d475e7 e8a8584574      call    clr!JIT_Stelem_Ref (7919ce94)
  04d475ec 8b8534ffffff    mov     eax,dword ptr [ebp-0CCh]
  04d475f2 3900            cmp     dword ptr [eax],eax
  04d475f4 ff358833b101    push    dword ptr ds:[1B13388h] ("Number = ? AND ServerID = ?")
  04d475fa ffb56cffffff    push    dword ptr [ebp-94h]
  04d47600 6a01            push    1
  04d47602 6a00            push    0
  04d47604 8b55dc          mov     edx,dword ptr [ebp-24h]
  04d47607 8bc8            mov     ecx,eax
  04d47609 ff15c05a9900    call    dword ptr ds:[995AC0h] (SoftwareHouse.CrossFire.Common.ClientInterfaceLayer.ClientServerConnection.FindObject(System.Type, System.String, System.Object[], Boolean, SoftwareHouse.CrossFire.Common.Core.RemoteProxyOption), mdToken: 010ACF2C)
  04d4760f 8bd0            mov     edx,eax
  04d47611 b9a0576e03      mov     ecx,36E57A0h (MT: SoftwareHouse.CrossFire.Common.DataServiceLayer.DataServiceObject)
  04d47616 e8b7584574      call    clr!JIT_IsInstanceOfClass (7919ced2)
  04d4761b 8bf8            mov     edi,eax
  04d4761d 85ff            test    edi,edi
  04d4761f 0f84e0080000    je      &lt;Unloaded_avcodec-53.dll&gt;+0x8e7f05 (04d47f05)
  04d47625 b918889503      mov     ecx,3958818h (MT: AD.Common.VideoObjectDefinitions.CameraAlertStatus)
  04d4762a e8f1a9c3fb      call    00982020 (JitHelp: CORINFO_HELP_NEWSFAST)
  04d4762f 8bf0            mov     esi,eax
  </code></pre>
  
  <p>Is there any way from this that I can tell what could be throwing the exception???</p>