michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include michael@0: michael@0: #include "base/debug_on_start.h" michael@0: michael@0: #include "base/base_switches.h" michael@0: #include "base/basictypes.h" michael@0: #include "base/debug_util.h" michael@0: michael@0: // Minimalist implementation to try to find a command line argument. We can use michael@0: // kernel32 exported functions but not the CRT functions because we're too early michael@0: // in the process startup. michael@0: // The code is not that bright and will find things like ---argument or michael@0: // /-/argument. michael@0: // Note: command_line is non-destructively modified. michael@0: bool DebugOnStart::FindArgument(wchar_t* command_line, const wchar_t* argument) michael@0: { michael@0: int argument_len = lstrlen(argument); michael@0: int command_line_len = lstrlen(command_line); michael@0: while (command_line_len > argument_len) { michael@0: wchar_t first_char = command_line[0]; michael@0: wchar_t last_char = command_line[argument_len+1]; michael@0: // Try to find an argument. michael@0: if ((first_char == L'-' || first_char == L'/') && michael@0: (last_char == L' ' || last_char == 0 || last_char == L'=')) { michael@0: command_line[argument_len+1] = 0; michael@0: // Skip the - or / michael@0: if (lstrcmpi(command_line+1, argument) == 0) { michael@0: // Found it. michael@0: command_line[argument_len+1] = last_char; michael@0: return true; michael@0: } michael@0: // Fix back. michael@0: command_line[argument_len+1] = last_char; michael@0: } michael@0: // Continue searching. michael@0: ++command_line; michael@0: --command_line_len; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // static michael@0: int __cdecl DebugOnStart::Init() { michael@0: // Try to find the argument. michael@0: if (FindArgument(GetCommandLine(), switches::kDebugOnStart)) { michael@0: // We can do 2 things here: michael@0: // - Ask for a debugger to attach to us. This involve reading the registry michael@0: // key and creating the process. michael@0: // - Do a int3. michael@0: michael@0: // It will fails if we run in a sandbox. That is expected. michael@0: DebugUtil::SpawnDebuggerOnProcess(GetCurrentProcessId()); michael@0: michael@0: // Wait for a debugger to come take us. michael@0: DebugUtil::WaitForDebugger(60, false); michael@0: } else if (FindArgument(GetCommandLine(), switches::kWaitForDebugger)) { michael@0: // Wait for a debugger to come take us. michael@0: DebugUtil::WaitForDebugger(60, true); michael@0: } michael@0: return 0; michael@0: }