首页 > 代码库 > WinDbg调试CPU占用高的问题 试验+实战 《第七篇》

WinDbg调试CPU占用高的问题 试验+实战 《第七篇》

一、High CPU试验

  1、示例代码

        static void Main(string[] args)
        {
            Console.Clear();
            Console.WriteLine("到命令行下,切换到windbg目录,执行adplus -hang -pn highcpu.exe -o c:\\dumps");
            Console.WriteLine("如果要停止,按Ctrl+C结束程序");
            Console.WriteLine("====================================================");
            
            while (true)
            {
                Console.SetCursorPosition(0, 3);
                Console.Write(DateTime.Now.Ticks.ToString());
            }

            Console.ReadKey();
        }

  2、运行控制台程序后,抓取3次Dump,最好每次间隔几分钟。

  3、分别对每个Dump执行如下指令。

第一个Dump:
0:000> .load clr20\sos.dll
0:000> !runaway
 User Mode Time
  Thread       Time
   0:334       0 days 0:00:00.468
   3:dc8       0 days 0:00:00.000
   2:14d0      0 days 0:00:00.000
   1:750       0 days 0:00:00.000

第二个Dump:
0:000> .load clr20\sos.dll
0:000> !runaway
 User Mode Time
  Thread       Time
   0:334       0 days 0:00:08.221
   3:dc8       0 days 0:00:00.000
   2:14d0      0 days 0:00:00.000
   1:750       0 days 0:00:00.000

第三个Dump:
0:000> .load clr20\sos.dll
0:000> !runaway
 User Mode Time
  Thread       Time
   0:334       0 days 0:00:11.559
   3:dc8       0 days 0:00:00.000
   2:14d0      0 days 0:00:00.000
   1:750       0 days 0:00:00.000

  从上面的输出可以看到,线程0的CPU时间不断增加,CPU高占用很可能是由于线程0正在执行的代码有问题造成的。

  可以用!clrstack命令查看一下当前的调用堆栈以及其上的局部变量和值。

  当执行!clrstack时,报出了如下提示:

0:000> !clrstack
Failed to find runtime DLL (mscorwks.dll), 0x80004005
Extension commands need mscorwks.dll in order to have something to do.

  这不是我想要的,加载mscorwks失败。后来经过调试发现,原来是第一步就.load时就错了,因为我的那段代码用的是vs2010。所以应该加载.net frameword 4.0。

  所以,最初应该执行的.load C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/sos.dll

  然后在执行!clrstack

  第三个Dump

0:000> !clrstack
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for clr.dll - 
PDB symbol for clr.dll not loaded
OS Thread Id: 0x334 (0)
Child SP       IP Call Site
001cf2b8 779e70f4 [InlinedCallFrame: 001cf2b8] 
001cf2b4 6a14672e *** WARNING: Unable to verify checksum for mscorlib.ni.dll
DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, COORD)
001cf2b8 6a1b6cfd [InlinedCallFrame: 001cf2b8] Microsoft.Win32.Win32Native.SetConsoleCursorPosition(IntPtr, COORD)
001cf2ec 6a1b6cfd System.Console.SetCursorPosition(Int32, Int32)
001cf324 005500bb *** WARNING: Unable to verify checksum for ConsoleApplication3.exe
ConsoleApplication3.Program.Main(System.String[]) [C:\Users\ChenZhuo\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs @ 22]
001cf4dc 70a33e22 [GCFrame: 001cf4dc] 

  第二个Dump

0:000> !clrstack
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for clr.dll - 
PDB symbol for clr.dll not loaded
OS Thread Id: 0x334 (0)
Child SP       IP Call Site
001cf2b8 779e70f4 [InlinedCallFrame: 001cf2b8] 
001cf2b4 6a14672e *** WARNING: Unable to verify checksum for mscorlib.ni.dll
DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, COORD)
001cf2b8 6a1b6cfd [InlinedCallFrame: 001cf2b8] Microsoft.Win32.Win32Native.SetConsoleCursorPosition(IntPtr, COORD)
001cf2ec 6a1b6cfd System.Console.SetCursorPosition(Int32, Int32)
001cf324 005500bb *** WARNING: Unable to verify checksum for ConsoleApplication3.exe
ConsoleApplication3.Program.Main(System.String[]) [C:\Users\ChenZhuo\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs @ 22]
001cf4dc 70a33e22 [GCFrame: 001cf4dc] 

  第一个Dump:

0:000> !clrstack
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for clr.dll - 
PDB symbol for clr.dll not loaded
OS Thread Id: 0x334 (0)
Child SP       IP Call Site
001cf2b8 779e70f4 [InlinedCallFrame: 001cf2b8] 
001cf2b4 6a14672e *** WARNING: Unable to verify checksum for mscorlib.ni.dll
DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, COORD)
001cf2b8 6a1b6cfd [InlinedCallFrame: 001cf2b8] Microsoft.Win32.Win32Native.SetConsoleCursorPosition(IntPtr, COORD)
001cf2ec 6a1b6cfd System.Console.SetCursorPosition(Int32, Int32)
001cf324 005500bb *** WARNING: Unable to verify checksum for ConsoleApplication3.exe
ConsoleApplication3.Program.Main(System.String[]) [C:\Users\ChenZhuo\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs @ 22]
001cf4dc 70a33e22 [GCFrame: 001cf4dc] 

  不知道为什么,我的第三个Dump并没有出现HighCPU的提示,也许是我的电脑配置有点高吧。不过,出问题的代码已可以看得到,就在Main方法里。只需要细细查看Main方法找到问题所在就OK了。

  学习自:http://www.cnblogs.com/juqiang/archive/2008/01/11/1035689.html