Monday, August 17, 2009

[Windbg] How to Brake at Child Process Creation

有時候,想要從進程(Process)一建立起來就開始除錯,一種方式是直接用 Windbg 直接用 .create 去把進程跑起來。可是在 Google Chrome 這種多進程的程式,想要從某個子進程開始除錯,一定要等主進程把子進程帶起來時才可以除錯。此時,就會需要用一些技巧讓 Windbg 去中斷子進程。
首先,先執行 Google Chrome,它是一個多進程架構的瀏覽器,適合作為這次的範例。
image
然後,在用命令列或GUI的方式 Attach 到主進程 Chrome.exe (1508)。
1) 用 .childdbg 1 命令 Windbg 中斷在子進程的建立。
0:013> .childdbg 1    
Processes created by the current process will be debugged
2) 用 g 命令 Windbg 繼續執行程式,並且去 Chrome 瀏覽個網頁讓它另外建立子進程。
image
3) 接下來會發現 Windbg 已經中斷在子進程當中,可以注意到提示字元已經從 0:013> 切換到 1:016> 。
0:013> g    
Symbol search path is: SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols;SRV*c:\GoogleSyms*http://build.chromium.org/buildbot/symsrv     
Executable search path is:     
ModLoad: 00400000 004d6000   chrome.exe     
eax=0044092a ebx=7ffde000 ecx=7c9363bb edx=7c99e178 esi=0012dc28 edi=00189318     
eip=7c810705 esp=0012fffc ebp=00000000 iopl=0         nv up ei pl nz na po nc     
cs=001b  ss=0023  ds=0023  es=0023  fs=0038  gs=0000             efl=00000200     
7c810705 ??              ???     
1:016> |     
   0    id: 5e4    attach    name: C:\Documents and Settings\Administrator\Local Settings\Application Data\Google\Chrome\Application\chrome.exe     
.  1    id: c40    child    name: chrome.exe
4) 此時只需要用 |0s 切換到原本的主進程,然後用 .detach 放掉主進程。
1:016> |0s    
eax=7c930250 ebx=00000000 ecx=000e1714 edx=00000c88 esi=7c99e420 edi=7c99e440     
eip=7c92e514 esp=049eff70 ebp=049effb4 iopl=0         nv up ei ng nz na pe nc     
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000286     
ntdll!KiFastSystemCallRet:     
7c92e514 c3              ret     
0:015> .detach     
eax=0044092a ebx=7ffde000 ecx=7c9363bb edx=7c99e178 esi=0012dc28 edi=00189318     
eip=7c810705 esp=0012fffc ebp=00000000 iopl=0         nv up ei pl nz na po nc     
cs=001b  ss=0023  ds=0023  es=0023  fs=0038  gs=0000             efl=00000200     
7c810705 ??              ???     
Detached     
1:016> |     
.  1    id: c40    child    name: chrome.exe
5) 此時就大功告成,接下來就可以開始對子進程除錯了。

No comments: