blob: 9011ca200eccd6611e31802d757dbcb508993cc2 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
brettw@google.comcc1eb1b2008-08-09 07:07:48 +09004
initial.commit3f4a7322008-07-27 06:49:38 +09005#include <windows.h>
6
7#include "base/debug_on_start.h"
8
9#include "base/base_switches.h"
10#include "base/basictypes.h"
11#include "base/debug_util.h"
12
13// Minimalist implementation to try to find a command line argument. We can use
14// kernel32 exported functions but not the CRT functions because we're too early
15// in the process startup.
16// The code is not that bright and will find things like ---argument or
17// /-/argument.
18// Note: command_line is non-destructively modified.
evan@chromium.orge3fca692009-10-13 11:07:25 +090019bool DebugOnStart::FindArgument(wchar_t* command_line, const char* argument_c)
initial.commit3f4a7322008-07-27 06:49:38 +090020{
evan@chromium.orge3fca692009-10-13 11:07:25 +090021 wchar_t argument[50];
22 for (int i = 0; argument_c[i]; ++i)
23 argument[i] = argument_c[i];
24
initial.commit3f4a7322008-07-27 06:49:38 +090025 int argument_len = lstrlen(argument);
26 int command_line_len = lstrlen(command_line);
27 while (command_line_len > argument_len) {
28 wchar_t first_char = command_line[0];
29 wchar_t last_char = command_line[argument_len+1];
30 // Try to find an argument.
31 if ((first_char == L'-' || first_char == L'/') &&
32 (last_char == L' ' || last_char == 0 || last_char == L'=')) {
33 command_line[argument_len+1] = 0;
34 // Skip the - or /
35 if (lstrcmpi(command_line+1, argument) == 0) {
36 // Found it.
37 command_line[argument_len+1] = last_char;
38 return true;
39 }
40 // Fix back.
41 command_line[argument_len+1] = last_char;
42 }
43 // Continue searching.
44 ++command_line;
45 --command_line_len;
46 }
47 return false;
48}
49
50// static
51int __cdecl DebugOnStart::Init() {
52 // Try to find the argument.
53 if (FindArgument(GetCommandLine(), switches::kDebugOnStart)) {
54 // We can do 2 things here:
55 // - Ask for a debugger to attach to us. This involve reading the registry
56 // key and creating the process.
57 // - Do a int3.
58
59 // It will fails if we run in a sandbox. That is expected.
60 DebugUtil::SpawnDebuggerOnProcess(GetCurrentProcessId());
61
62 // Wait for a debugger to come take us.
63 DebugUtil::WaitForDebugger(60, false);
64 } else if (FindArgument(GetCommandLine(), switches::kWaitForDebugger)) {
65 // Wait for a debugger to come take us.
66 DebugUtil::WaitForDebugger(60, true);
67 }
68 return 0;
69}