如何使用windbg找出抛出异常的内容

时间:2013-07-23 22:19:21

标签: debugging windbg remote-debugging

我有一个c#windows服务正在抛出异常 我使用windbg启动了这项服务,让它一直运行直到抛出异常。

当调试器停止时我会这样做:

  1. prefer_dml 1
  2. !threads。这会打印出线程,然后点击相关的例外。
  3. 点击 _message ,会显示:

    0:018> !DumpObj / d 0105e134 名称:System.String MethodTable:79b9f9ac EEClass:798d8bb0 大小:120(0x78)字节 文件:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_32 \ mscorlib \ v4.0_4.0.0.0__b77a5c561934e089 \ mscorlib.dll 字符串:未将对象引用设置为对象的实例。

  4. 我这样做:!clrstack 并获取以下内容:

    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。 UnhandledExceptionEventHandler (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。 CameraAlarmActivate (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]

  5. 如果您阅读了以下方法,您将看到抛出异常的函数是 ObjectProcessor.CameraAlarmActivate

    此功能的代码如下:

     public override void CameraAlarmActivate(int cameraNumber, DataServiceObject dso, AD.Common.VideoObjectDefinitions.CameraAlertStatus status, bool isBegin, DateTime time, out Dictionary<string, object> 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<string> alarmList = CheckIntellexCameraContainer(camera, dso, alert);
    
                    if(alarmList != null && alarmList.Count > 0)
                    {
                        bool alarmToJournal = false;
                        // If alarm is Motion...
                        if (status == AD.Common.VideoObjectDefinitions.CameraAlertStatus.Motion && 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 && 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 && 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 && 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) && 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();
            }
        }
    

    我想在调试器中查看这个函数,所以我从!clrstack的输出中选择函数的IP值。 (04d47f23)

    我得到以下内容:

    0:018> !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<System.String,System.Object> 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      <Unloaded_avcodec-53.dll>+0x8e7553 (04d47553)
    04d4753b c745e400000000  mov     dword ptr [ebp-1Ch],0
    04d47542 c745e8fc000000  mov     dword ptr [ebp-18h],0FCh
    04d47549 68377fd404      push    offset <Unloaded_avcodec-53.dll>+0x8e7f37 (04d47f37)
    04d4754e e9c7090000      jmp     <Unloaded_avcodec-53.dll>+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      <Unloaded_avcodec-53.dll>+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      <Unloaded_avcodec-53.dll>+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
    

    有什么方法可以告诉我什么可以抛出异常???

1 个答案:

答案 0 :(得分:2)

由于调试器正在捕获异常,并且已确定名为“CameraAlarmActivate”的方法正在抛出异常,因此您可以在方法的顶部设置断点并逐步执行,直到异常命中。然后,您可以检查变量并进行相应的故障排除。

从Exception对象中,您还可以获得更多信息。

// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();

从这里开始:C# - get line number which threw exception

一般来说,拥有空的异常处理程序并不是一个好习惯,因为错误可能会被忽视。我建议至少集成一个日志库,以便您的应用程序有自己的日志文件,然后从所有异常中写入错误日志条目。您还可以编写其他详细类型的日志,这些日志可以帮助您在发生错误后通过日志文件跟踪应用程序状态。这对于Windows服务或任何基于后台的作业尤其有用,在任何地方都可能发生错误。 Log4net是一个易于使用的好的日志库:http://logging.apache.org/log4net/

除此之外,快速查看代码的猜测是,客户端连接可能会在某个时刻丢失,而ClientServerConnection或ClientServerConnection.Instance在某些时候可能会变为NULL。这将是您在异常块中可以执行的操作的一个很好的示例。您可以捕获该特定异常,然后重新连接并重新尝试。

最后,您可能缺少某些符号来更详细地调试该函数,或者您可能必须启用项目与使用非托管代码进行调试相关的一些选项。