记一次 .NET 某光放测试体系 瓦解分析

[复制链接]
发表于 2025-10-23 14:08:17 | 显示全部楼层 |阅读模式
一:配景

1. 讲故事

微信挚友里有位朋侪找到我,说他摆设在windows上的步调,用debug模式正常,但用 release 模式跑步调就瓦解,假如把步调切到 .NET6 的话又都正常,以是很渺茫,让我看看怎么回事,哈哈,这种题目直接抓dump分析就好了。
二:瓦解分析

1. 为什么会瓦解

分析过瓦解步调的朋侪应该知道,不管是托管还好坏托管瓦解,先用 !analyze -v 下令开路,简化输出如下:
  1. 0:000> !analyze -v
  2. *******************************************************************************
  3. *                                                                             *
  4. *                        Exception Analysis                                   *
  5. *                                                                             *
  6. *******************************************************************************
  7. CONTEXT:  (.ecxr)
  8. rax=0000000000000004 rbx=000001e34b283ec0 rcx=0000000000000228
  9. rdx=0000000000000000 rsi=000001e34ac2f4e0 rdi=000001e34ab58e70
  10. rip=00007ff95ac53659 rsp=0000007735d7e1c0 rbp=0000007735d7e1e0
  11. r8=0000000000000000  r9=000001e3464ba1c0 r10=0000000000000228
  12. r11=0000000000000228 r12=0000000000000000 r13=000001e34880eae8
  13. r14=000001e34ab58e70 r15=0000000000000008
  14. iopl=0         nv up di pl nz na pe nc
  15. cs=0000  ss=0000  ds=0000  es=0000  fs=0000  gs=0000             efl=00000000
  16. System_Private_CoreLib!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw+0x39:
  17. 00007ff9`5ac53659 cc              int     3
  18. Resetting default scope
  19. EXCEPTION_RECORD:  (.exr -1)
  20. ExceptionAddress: 00007ff95ac53659 (System_Private_CoreLib!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw+0x0000000000000039)
  21.    ExceptionCode: e0434f4d (CLR exception)
  22.   ExceptionFlags: 00000000
  23. NumberParameters: 0
  24. ...
复制代码
从卦中的 ExceptionCode: e0434f4d (CLR exception) 来看,这是一个经典的托管非常,既然是托管非常,这个题目就比力简单了,利用 !t 找下到底是哪一个托管线程抛的,输出如下:
  1. 0:000> !t
  2. ThreadCount:      15
  3. UnstartedThread:  0
  4. BackgroundThread: 11
  5. PendingThread:    0
  6. DeadThread:       3
  7. Hosted Runtime:   no
  8.                                                                                                             Lock  
  9. DBG   ID     OSID ThreadOBJ           State GC Mode     GC Alloc Context                  Domain           Count Apt Exception
  10.    0    1     81d8 000001E3464BA1C0    a6028 Preemptive  000001E34ABA2340:000001E34ABA3D30 000001e347fc40b0 -00001 STA Prism.Ioc.ContainerResolutionException 000001e34a986608
  11.    6    2     448c 000001E34803A440    2b228 Preemptive  000001E34A876980:000001E34A8784B0 000001e347fc40b0 -00001 MTA (Finalizer)
  12.    ...
复制代码
从卦中的 Prism.Ioc.ContainerResolutionException 来看,貌似是和 Prism 有关,接下来可以用 !pe 下令观察调用栈详情。
  1. 0:000> !pe
  2. Exception object: 000001e34a986608
  3. Exception type:   Prism.Ioc.ContainerResolutionException
  4. Message:          An unexpected error occurred while resolving 'xxx.Views.LoginWindow'
  5. InnerException:   Unity.ResolutionFailedException, Use !PrintException 000001E34A986228 to see more.
  6. StackTrace (generated):
  7.     SP               IP               Function
  8.     0000007735D668E0 00007FF95A64DEC8 Prism_Unity_Wpf!Prism.Unity.UnityContainerExtension.Resolve(System.Type, System.ValueTuple`2<System.Type,System.Object>[])+0x2a8
  9.     0000007735D7DC60 00007FF95A64DBFD Prism_Unity_Wpf!Prism.Unity.UnityContainerExtension.Resolve(System.Type)+0x3d
  10.     0000007735D7DCA0 00007FF95A64DB88 Prism!Prism.Ioc.IContainerProviderExtensions.Resolve[[System.__Canon, System.Private.CoreLib]](Prism.Ioc.IContainerProvider)+0x48
  11.     0000007735D7DCF0 00007FF95A956742 xxx!xxx.App.InitializeShell(System.Windows.Window)+0x42
  12.     0000007735D7DD40 00007FF959B21148 Prism_Wpf!Prism.PrismApplicationBase.Initialize()+0x208
  13.     0000007735D7DDA0 00007FF959B20F17 xxx!xxx.App.<>n__0()+0x17
  14.     ....
复制代码
从卦象来看,这不是最原始的非常,言外之意就是下面尚有子非常,也只有找到最里层的非常才气发现劫难的祸根,颠末一层层的下钻,末了找到了最原始的非常,参考如下:
  1. 0:000> !PrintException /d 000001E34A97E940
  2. Exception object: 000001e34a97e940
  3. Exception type:   System.PlatformNotSupportedException
  4. Message:          System.IO.Ports is currently only supported on Windows.
  5. InnerException:   <none>
  6. StackTrace (generated):
  7.     SP               IP               Function
  8.     0000007735D7B580 00007FF95A9588E7 System_IO_Ports!System.IO.Ports.SerialPort.GetPortNames()+0x47
  9.     0000007735D7B5C0 00007FF95A958859 xxx!xxx.ViewModels.LoginWindowViewModel.RefreshComs()+0x19
  10.     0000007735D7B600 00007FF95A957FBC xxx!xxx.ViewModels.LoginWindowViewModel..ctor()+0x14c
  11.     0000007735D7B9D0 0000000000000000 System_Private_CoreLib!System.RuntimeMethodHandle.InvokeMethod(System.Object, Void**, System.Signature, Boolean)+0x46a770b0
  12.     0000007735D7B9D0 00007FF9B8C03106 System_Private_CoreLib!System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(System.Object, System.Reflection.BindingFlags)+0x36
  13. StackTraceString: <none>
  14. HResult: 80131539
复制代码
从卦中来看是 GetPortNames() 方法抛出来的平台不支持非常,这就很迷惑了。
2. 为什么会平台不支持

相识 PlatformNotSupportedException 非常,只能探求相干的源代码了,通过dnspy截图如下:

从卦中来看这是一个空方法,接下来拿这个非常在网上找下资料,看样子是这位朋侪必要升级大概降级 system.io.ports 的版本,截图如下:

完备链接:https://learn.microsoft.com/en-us/answers/questions/1621393/system-io-ports-only-availble-on-windows-but-im-us
原来是很高兴的,以为是类似多线程操控非 volatile 变量导致的debug和release活动差别等呢,效果是这玩意,害!
三:总结

本次故障相对比力简单,对我们这些老手来说简直是 1+1,但我们何尝不是重新手练过来的,以是本篇是初学者很好的一个练手素材。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表