Chrome的多進程模型給DEBUG帶來了很大的挑戰。
一、如果你設定代碼的斷點,預設情況下,VS只會跟蹤那些在主進程Browser代碼中的那些斷點。VS提供了"Attach To Process"的方法。比如當Render Process啟動之後,可以用菜單"Debug"=>"Attach To Process"選項,選擇那個新產生的進程,然後在你需要跟蹤的代碼處設定斷點,就可以。但是這種方法,只能在子進程啟動之後,才比較有效,如果我們想在子進程啟動時,跟蹤某些代碼的執行,就沒有辦法了。
二、針對這個Chrome從原始碼層級提供了支援。共有兩種方法:
1. 用啟動選項“--single-process“,以單進程的方法來啟動Chrome。發現這個方法不好用了已經
2.用啟動選項"--renderer-startup-dialog"和"--no-sandbox"。兩個選項將會讓子進程在啟動之後,彈出一個模態對話方塊。之後在關閉這個對話方塊之後才可以繼續運行代碼。在這期間,我們可以用上述"Attach To Process"的方法來跟蹤子進程代碼的執行。
之所以要加上"--no-sandbox",是因為預設情況下Chrome的子進程的建立和啟動是在sanbox中(也就說訪問系統資源是嚴格限制的),無法顯示模態對話方塊UI。
Render進程的主函數如下:
[cpp] view plain copy print ? int RendererMain(const MainFunctionParams& parameters) { const CommandLine& parsed_command_line = parameters.command_line_; base::ScopedNSAutoreleasePool* pool = parameters.autorelease_pool_; // This function allows pausing execution using the --renderer-startup-dialog // flag allowing us to attach a debugger. // Do not move this function down since that would mean we can't easily debug // whatever occurs before it. HandleRendererErrorTestParameters(parsed_command_line);
HandleRenderErrorTestParameters函數會顯示這個模態對話方塊。
// This function provides some ways to test crash and assertion handling// behavior of the renderer.static void HandleRendererErrorTestParameters(const CommandLine& command_line) { if (command_line.HasSwitch(switches::kWaitForDebugger)) base::debug::WaitForDebugger(60, true); if (command_line.HasSwitch(switches::kRendererStartupDialog)) ChildProcess::WaitForDebugger("Renderer"); // This parameter causes an assertion. if (command_line.HasSwitch(switches::kRendererAssertTest)) { DCHECK(false); }}