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。
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);
Render進程的主函數如下:
// 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); } }
轉載自:http://blog.csdn.net/leer168/article/details/8438149#