翼度科技»论坛 编程开发 .net 查看内容

记一次 .NET某上位机视觉程序 卡死分析

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
一:背景

1. 讲故事

前段时间有位朋友找到我,说他的窗体程序在客户这边出现了卡死,让我帮忙看下怎么回事?dump也生成了,既然有dump了那就上 windbg 分析吧。
二:WinDbg 分析

1. 为什么会卡死

窗体程序的卡死,入口门槛很低,后续往下分析就不一定了,不管怎么说先用 !clrstack 看下主线程,输出如下:
  1. 0:000> !clrstack
  2. OS Thread Id: 0x3118 (0)
  3.         Child SP               IP Call Site
  4. 000000c478afd1d8 00007ffc284e9a84 [HelperMethodFrame_1OBJ: 000000c478afd1d8] System.Threading.WaitHandle.WaitOneNative(System.Runtime.InteropServices.SafeHandle, UInt32, Boolean, Boolean)
  5. 000000c478afd300 00007ffbf2cc19ac System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle, Int64, Boolean, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 243]
  6. 000000c478afd330 00007ffbf2cc197f System.Threading.WaitHandle.WaitOne(Int32, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 194]
  7. 000000c478afd370 00007ffbf1421904 System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle)
  8. 000000c478afd3e0 00007ffbf0c8e2f4 System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
  9. 000000c478afd520 00007ffbf1425124 System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[])
  10. 000000c478afd590 00007ffb995d6fe8 DevComponents.DotNetBar.StyleManager.OnColorTintChanged(System.Drawing.Color, System.Drawing.Color)
  11. 000000c478afd5f0 00007ffb995d69ff DevComponents.DotNetBar.StyleManager.set_ColorTint(System.Drawing.Color)
  12. 000000c478afd680 00007ffb995d694c DevComponents.DotNetBar.StyleManager.set_ManagerColorTint(System.Drawing.Color)
  13. ...
  14. 000000c478afd6b0 00007ffb995d50f9 xxx.MarkInspectPadControl.InitializeComponent()
复制代码
有经验的朋友看到上面的卦象相信就知道咋事情了,即有工作线程创建了用户控件导致的,而且这个控件貌似和 DevComponents 有关,接下来的常规套路就是挖一下 WindowsFormsSynchronizationContext 对象看看到底是哪一个线程创建的,使用 !dso 即可。
  1. 0:000> !dso
  2. OS Thread Id: 0x3118 (0)
  3. RSP/REG          Object           Name
  4. 000000C478AFCF98 000002093b9143c0 System.Windows.Forms.WindowsFormsSynchronizationContext
  5. ...
  6. 0:000> !do poi(20939c91588)
  7. Name:        System.Threading.Thread
  8. MethodTable: 00007ffbf2769580
  9. EEClass:     00007ffbf288c658
  10. Size:        96(0x60) bytes
  11. 00007ffbf276aaf8  4001934       4c         System.Int32  1 instance                1 m_ManagedThreadId
复制代码
按照剧本的话 WindowsFormsSynchronizationContext 应该会有2个,但这里只有1个,这一个还是主线程的同步上下文,这就完犊子了。。。完全不按照剧本走,这也是真实dump分析的复杂性,那到底是谁创建的呢? 天要绝人之路吗?
2. 出路在哪里

所有东西的落地都在汇编里,而汇编又在方法里,所以突破口就是寻找线程栈中的方法,接下来到 System.Windows.Forms.Control.MarshaledInvoke 方法里看一看可有什么大货,简化后如下:
  1. private object MarshaledInvoke(Control caller, Delegate method, object[] args, bool synchronous)
  2. {
  3.     bool flag = false;
  4.     if (SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, Handle), out var _) == SafeNativeMethods.GetCurrentThreadId() && synchronous)
  5.     {
  6.         flag = true;
  7.     }
  8.     ThreadMethodEntry threadMethodEntry = new ThreadMethodEntry(caller, this, method, args, synchronous, executionContext);
  9.     lock (threadCallbackList)
  10.     {
  11.         if (threadCallbackMessage == 0)
  12.         {
  13.             threadCallbackMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_ThreadCallbackMessage");
  14.         }
  15.         threadCallbackList.Enqueue(threadMethodEntry);
  16.     }
  17.     if (flag)
  18.     {
  19.         InvokeMarshaledCallbacks();
  20.     }
  21.     else
  22.     {
  23.         UnsafeNativeMethods.PostMessage(new HandleRef(this, Handle), threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
  24.     }
  25.     if (synchronous)
  26.     {
  27.         if (!threadMethodEntry.IsCompleted)
  28.         {
  29.             WaitForWaitHandle(threadMethodEntry.AsyncWaitHandle);
  30.         }
  31.         return threadMethodEntry.retVal;
  32.     }
  33.     return threadMethodEntry;
  34. }
复制代码
从卦中的代码来看,这个 SafeNativeMethods.GetWindowThreadProcessId 方法是关键,它可以拿到这个窗口创建的processid和threadid,接下来观察下简化后的汇编代码。
  1. 0:000> !U /d 00007ffbf0c8e2f4
  2. preJIT generated code
  3. System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
  4. Begin 00007ffbf0c8dec0, size 4e9
  5. 00007ffb`f0c8dec0 55              push    rbp
  6. 00007ffb`f0c8dec1 4157            push    r15
  7. 00007ffb`f0c8dec3 4156            push    r14
  8. 00007ffb`f0c8dec5 4155            push    r13
  9. 00007ffb`f0c8dec7 4154            push    r12
  10. 00007ffb`f0c8dec9 57              push    rdi
  11. 00007ffb`f0c8deca 56              push    rsi
  12. 00007ffb`f0c8decb 53              push    rbx
  13. 00007ffb`f0c8decc 4881ecf8000000  sub     rsp,0F8h
  14. 00007ffb`f0c8ded3 488dac2430010000 lea     rbp,[rsp+130h]
  15. ...
  16. 00007ffb`f0c8dff0 488d55b0        lea     rdx,[rbp-50h]
  17. 00007ffb`f0c8dff4 ff151e1eddff    call    qword ptr [System_Windows_Forms_ni+0x8fe18 (00007ffb`f0a5fe18)] (System.Windows.Forms.SafeNativeMethods.GetWindowThreadProcessId(System.Runtime.InteropServices.HandleRef, Int32 ByRef), mdToken: 00000000060033c4)
  18. 00007ffb`f0c8dffa 448bf0          mov     r14d,eax
复制代码
根据卦中的汇编以及x64调用协定,lea     rdx,[rbp-50h] 就是我们的 processid,同时 mov     r14d,eax 中的 r14d 就是我们的 threadid,突破口已找到,接下来就是深挖了。
3. 如何挖出进程ID和线程ID

有一点要知道 000000c478afd520 和 MarshaledInvoke 方法的 rsp 隔了一个 0x8,同时方法中影响 rsp 的 push 和 sub 都要计算进去,这里就不赘述了,具体可以参考文章:https://www.cnblogs.com/huangxincheng/p/17250240.html 简单计算后如下:
  1. 0:000> ? 000000c478afd520-0x8-(0n8*0n8)-0xF8+0x130
  2. Evaluate expression: 843838379280 = 000000c4`78afd510
  3. 0:000> dp 000000c4`78afd510-0x50 L1
  4. 000000c4`78afd4c0  00000000`000029dc
  5. 0:000> r r14
  6. r14=000000c478afcf14
  7. 0:000> dp 000000c478afcf14 L1
  8. 000000c4`78afcf14  00000000`00000080
复制代码
从卦中可以看到 processid=29dc ,threadid=0x80,这东西是何方神圣呢,我们用 ~ 来找它的真身吧。
  1. 0:000> ~
  2. ...
  3.   18  Id: 29dc.80 Suspend: 0 Teb: 000000c4`7890d000 Unfrozen
  4. ...
  5. 0:018> k
  6. # Child-SP          RetAddr               Call Site
  7. 00 000000c4`7a2ffcc8 00007ffc`28028ba3     ntdll!NtWaitForSingleObject+0x14
  8. 01 000000c4`7a2ffcd0 00007ffb`fa651cf8     KERNELBASE!WaitForSingleObjectEx+0x93
  9. 02 000000c4`7a2ffd70 00007ffb`fa652a51     wpfgfx_v0400!CPartitionManager::GetWork+0x17b
  10. 03 000000c4`7a2ffdc0 00007ffb`fa67a2fb     wpfgfx_v0400!CPartitionThread::Run+0x21
  11. 04 000000c4`7a2ffdf0 00007ffc`2a037bd4     wpfgfx_v0400!CPartitionThread::ThreadMain+0x2b
  12. 05 000000c4`7a2ffe20 00007ffc`2a76ced1     kernel32!BaseThreadInitThunk+0x14
  13. 06 000000c4`7a2ffe50 00000000`00000000     ntdll!RtlUserThreadStart+0x21
复制代码
现在有点傻傻分不清了,怎么 winform 里还有 wpf 的渲染线程,有可能是 DevComponents 这种第三方控件在底层引入的吧。到这里路子又被堵死了,接下来该往哪里走呢?三步一回头,继续看主线程上的方法代码吧。
4. 在源码中寻找答案

虽然在两条路上的突围都失败了,但可以明显的看到离真相真的越来越近,也收获到了大量的作战信息,通过上面的 set_ManagerColorTint 方法的反编译,参考如下:
  1. private void InitializeComponent()
  2. {
  3.     this.styleManager1.ManagerColorTint = System.Drawing.Color.Black;
  4. }
  5. [Description("Indicates color current style is tinted with.")]
  6. [Category("Appearance")]
  7. public Color ManagerColorTint
  8. {
  9.     get
  10.     {
  11.         return ColorTint;
  12.     }
  13.     set
  14.     {
  15.         ColorTint = value;
  16.     }
  17. }
复制代码
看到源码之后太无语了,其实就是一个简单的 颜色赋值,根据前面的探索styleManager1是由渲染线程创建的,所以主线程对它的赋值自然是得不到渲染线程的反馈。
那这个问题该怎么办呢?大概是如下两种吧。

  • 重点关注 styleManager1 控件,用排除法观察程序运行状况。
  • 看文档是否用了错误的方式使用 styleManager1 控件。
三:总结

这次生产事故还是挺有意思的,为什么 WinForm 中可以存在 CPartitionThread 渲染线程,最后还祸在其身,给我几百例dump分析之旅中添加了一笔色彩!

来源:https://www.cnblogs.com/huangxincheng/p/18413420
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

x

举报 回复 使用道具