blob: 6ca88dde20c9086fceee9503c5e673cc4f2ebbc5 [file] [log] [blame]
[email protected]10f33b1b2011-01-01 19:55:221// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]568d3f52008-08-08 22:07:484
[email protected]10f33b1b2011-01-01 19:55:225#include "base/debug/debug_on_start_win.h"
initial.commitd7cae122008-07-26 21:49:386
[email protected]10f33b1b2011-01-01 19:55:227#include <windows.h>
initial.commitd7cae122008-07-26 21:49:388
9#include "base/base_switches.h"
10#include "base/basictypes.h"
[email protected]58580352010-10-26 04:07:5011#include "base/debug/debugger.h"
initial.commitd7cae122008-07-26 21:49:3812
[email protected]10f33b1b2011-01-01 19:55:2213namespace base {
14namespace debug {
15
initial.commitd7cae122008-07-26 21:49:3816// 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
20// /-/argument.
21// Note: command_line is non-destructively modified.
[email protected]ee5e3792009-10-13 23:23:4722bool DebugOnStart::FindArgument(wchar_t* command_line, const char* argument_c) {
[email protected]514f9322011-02-22 20:46:0723 wchar_t argument[50] = {};
[email protected]b7e0a2a2009-10-13 02:07:2524 for (int i = 0; argument_c[i]; ++i)
25 argument[i] = argument_c[i];
26
initial.commitd7cae122008-07-26 21:49:3827 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;
36 // Skip the - or /
37 if (lstrcmpi(command_line+1, argument) == 0) {
38 // Found it.
39 command_line[argument_len+1] = last_char;
40 return true;
41 }
42 // Fix back.
43 command_line[argument_len+1] = last_char;
44 }
45 // Continue searching.
46 ++command_line;
47 --command_line_len;
48 }
49 return false;
50}
51
52// static
53int __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.
59 // - Do a int3.
60
61 // It will fails if we run in a sandbox. That is expected.
[email protected]58580352010-10-26 04:07:5062 base::debug::SpawnDebuggerOnProcess(GetCurrentProcessId());
initial.commitd7cae122008-07-26 21:49:3863
64 // Wait for a debugger to come take us.
[email protected]58580352010-10-26 04:07:5065 base::debug::WaitForDebugger(60, false);
initial.commitd7cae122008-07-26 21:49:3866 } else if (FindArgument(GetCommandLine(), switches::kWaitForDebugger)) {
67 // Wait for a debugger to come take us.
[email protected]58580352010-10-26 04:07:5068 base::debug::WaitForDebugger(60, true);
initial.commitd7cae122008-07-26 21:49:3869 }
70 return 0;
71}
[email protected]10f33b1b2011-01-01 19:55:2272
73} // namespace debug
74} // namespace base