ipc/chromium/src/base/debug_on_start.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/debug_on_start.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,65 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include <windows.h>
     1.9 +
    1.10 +#include "base/debug_on_start.h"
    1.11 +
    1.12 +#include "base/base_switches.h"
    1.13 +#include "base/basictypes.h"
    1.14 +#include "base/debug_util.h"
    1.15 +
    1.16 +// Minimalist implementation to try to find a command line argument. We can use
    1.17 +// kernel32 exported functions but not the CRT functions because we're too early
    1.18 +// in the process startup.
    1.19 +// The code is not that bright and will find things like ---argument or
    1.20 +// /-/argument.
    1.21 +// Note: command_line is non-destructively modified.
    1.22 +bool DebugOnStart::FindArgument(wchar_t* command_line, const wchar_t* argument)
    1.23 +{
    1.24 +  int argument_len = lstrlen(argument);
    1.25 +  int command_line_len = lstrlen(command_line);
    1.26 +  while (command_line_len > argument_len) {
    1.27 +    wchar_t first_char = command_line[0];
    1.28 +    wchar_t last_char = command_line[argument_len+1];
    1.29 +    // Try to find an argument.
    1.30 +    if ((first_char == L'-' || first_char == L'/') &&
    1.31 +        (last_char == L' ' || last_char == 0 || last_char == L'=')) {
    1.32 +      command_line[argument_len+1] = 0;
    1.33 +      // Skip the - or /
    1.34 +      if (lstrcmpi(command_line+1, argument) == 0) {
    1.35 +        // Found it.
    1.36 +        command_line[argument_len+1] = last_char;
    1.37 +        return true;
    1.38 +      }
    1.39 +      // Fix back.
    1.40 +      command_line[argument_len+1] = last_char;
    1.41 +    }
    1.42 +    // Continue searching.
    1.43 +    ++command_line;
    1.44 +    --command_line_len;
    1.45 +  }
    1.46 +  return false;
    1.47 +}
    1.48 +
    1.49 +// static
    1.50 +int __cdecl DebugOnStart::Init() {
    1.51 +  // Try to find the argument.
    1.52 +  if (FindArgument(GetCommandLine(), switches::kDebugOnStart)) {
    1.53 +    // We can do 2 things here:
    1.54 +    // - Ask for a debugger to attach to us. This involve reading the registry
    1.55 +    //   key and creating the process.
    1.56 +    // - Do a int3.
    1.57 +
    1.58 +    // It will fails if we run in a sandbox. That is expected.
    1.59 +    DebugUtil::SpawnDebuggerOnProcess(GetCurrentProcessId());
    1.60 +
    1.61 +    // Wait for a debugger to come take us.
    1.62 +    DebugUtil::WaitForDebugger(60, false);
    1.63 +  } else if (FindArgument(GetCommandLine(), switches::kWaitForDebugger)) {
    1.64 +    // Wait for a debugger to come take us.
    1.65 +    DebugUtil::WaitForDebugger(60, true);
    1.66 +  }
    1.67 +  return 0;
    1.68 +}

mercurial