贫民 发表于 6 天前

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

一:背景

1. 讲故事

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

1. 为什么会卡死

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

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

有一点要知道 000000c478afd520 和 MarshaledInvoke 方法的 rsp 隔了一个 0x8,同时方法中影响 rsp 的 push 和 sub 都要计算进去,这里就不赘述了,具体可以参考文章:https://www.cnblogs.com/huangxincheng/p/17250240.html 简单计算后如下:
0:000> ? 000000c478afd520-0x8-(0n8*0n8)-0xF8+0x130
Evaluate expression: 843838379280 = 000000c4`78afd510
0:000> dp 000000c4`78afd510-0x50 L1
000000c4`78afd4c000000000`000029dc

0:000> r r14
r14=000000c478afcf14
0:000> dp 000000c478afcf14 L1
000000c4`78afcf1400000000`00000080从卦中可以看到 processid=29dc ,threadid=0x80,这东西是何方神圣呢,我们用 ~ 来找它的真身吧。
0:000> ~
...
18Id: 29dc.80 Suspend: 0 Teb: 000000c4`7890d000 Unfrozen
...

0:018> k
# Child-SP          RetAddr               Call Site
00 000000c4`7a2ffcc8 00007ffc`28028ba3   ntdll!NtWaitForSingleObject+0x14
01 000000c4`7a2ffcd0 00007ffb`fa651cf8   KERNELBASE!WaitForSingleObjectEx+0x93
02 000000c4`7a2ffd70 00007ffb`fa652a51   wpfgfx_v0400!CPartitionManager::GetWork+0x17b
03 000000c4`7a2ffdc0 00007ffb`fa67a2fb   wpfgfx_v0400!CPartitionThread::Run+0x21
04 000000c4`7a2ffdf0 00007ffc`2a037bd4   wpfgfx_v0400!CPartitionThread::ThreadMain+0x2b
05 000000c4`7a2ffe20 00007ffc`2a76ced1   kernel32!BaseThreadInitThunk+0x14
06 000000c4`7a2ffe50 00000000`00000000   ntdll!RtlUserThreadStart+0x21现在有点傻傻分不清了,怎么 winform 里还有 wpf 的渲染线程,有可能是 DevComponents 这种第三方控件在底层引入的吧。到这里路子又被堵死了,接下来该往哪里走呢?三步一回头,继续看主线程上的方法代码吧。
4. 在源码中寻找答案

虽然在两条路上的突围都失败了,但可以明显的看到离真相真的越来越近,也收获到了大量的作战信息,通过上面的 set_ManagerColorTint 方法的反编译,参考如下:
private void InitializeComponent()
{
    this.styleManager1.ManagerColorTint = System.Drawing.Color.Black;
}



public Color ManagerColorTint
{
    get
    {
      return ColorTint;
    }
    set
    {
      ColorTint = value;
    }
}看到源码之后太无语了,其实就是一个简单的 颜色赋值,根据前面的探索styleManager1是由渲染线程创建的,所以主线程对它的赋值自然是得不到渲染线程的反馈。
那这个问题该怎么办呢?大概是如下两种吧。

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

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

来源:https://www.cnblogs.com/huangxincheng/p/18413420
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 记一次 .NET某上位机视觉程序 卡死分析