1 // Copyright (c) 2011 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.
5 #include "base/debug/debug_on_start_win.h"
9 #include "base/base_switches.h"
10 #include "base/basictypes.h"
11 #include "base/debug/debugger.h"
16 // Minimalist implementation to try to find a command line argument. We can use
17 // kernel32 exported functions but not the CRT functions because we're too early
18 // in the process startup.
19 // The code is not that bright and will find things like ---argument or
21 // Note: command_line is non-destructively modified.
22 bool DebugOnStart::FindArgument(wchar_t* command_line
, const char* argument_c
) {
23 wchar_t argument
[50] = {};
24 for (int i
= 0; argument_c
[i
]; ++i
)
25 argument
[i
] = argument_c
[i
];
27 int argument_len
= lstrlen(argument
);
28 int command_line_len
= lstrlen(command_line
);
29 while (command_line_len
> argument_len
) {
30 wchar_t first_char
= command_line
[0];
31 wchar_t last_char
= command_line
[argument_len
+1];
32 // Try to find an argument.
33 if ((first_char
== L
'-' || first_char
== L
'/') &&
34 (last_char
== L
' ' || last_char
== 0 || last_char
== L
'=')) {
35 command_line
[argument_len
+1] = 0;
37 if (lstrcmpi(command_line
+1, argument
) == 0) {
39 command_line
[argument_len
+1] = last_char
;
43 command_line
[argument_len
+1] = last_char
;
45 // Continue searching.
53 int __cdecl
DebugOnStart::Init() {
54 // Try to find the argument.
55 if (FindArgument(GetCommandLine(), switches::kDebugOnStart
)) {
56 // We can do 2 things here:
57 // - Ask for a debugger to attach to us. This involve reading the registry
58 // key and creating the process.
61 // It will fails if we run in a sandbox. That is expected.
62 base::debug::SpawnDebuggerOnProcess(GetCurrentProcessId());
64 // Wait for a debugger to come take us.
65 base::debug::WaitForDebugger(60, false);
66 } else if (FindArgument(GetCommandLine(), switches::kWaitForDebugger
)) {
67 // Wait for a debugger to come take us.
68 base::debug::WaitForDebugger(60, true);