13f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Define the necessary code and global data to look for kDebugOnStart command
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// line argument. When the command line argument is detected, it invokes the
7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// debugger, if no system-wide debugger is registered, a debug break is done.
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
93f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#ifndef BASE_DEBUG_DEBUG_ON_START_WIN_H_
103f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#define BASE_DEBUG_DEBUG_ON_START_WIN_H_
113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h"
143f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#include "build/build_config.h"
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
163f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// This only works on Windows. It's legal to include on other platforms, but
173f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// will be a NOP.
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef DECLSPEC_SELECTANY
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define DECLSPEC_SELECTANY  __declspec(selectany)
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
243f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsennamespace base {
253f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsennamespace debug {
263f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
27ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// There is no way for this code, as currently implemented, to work across DLLs.
28ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// TODO(rvargas): It looks like we really don't use this code, at least not for
29ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Chrome. Figure out if it's really worth implementing something simpler.
30ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#if !defined(BASE_DLL)
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Debug on start functions and data.
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass DebugOnStart {
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Expected function type in the .CRT$XI* section.
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Note: See VC\crt\src\internal.h for reference.
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  typedef int  (__cdecl *PIFV)(void);
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Looks at the command line for kDebugOnStart argument. If found, it invokes
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // the debugger, if this fails, it crashes.
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static int __cdecl Init();
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns true if the 'argument' is present in the 'command_line'. It does
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // not use the CRT, only Kernel32 functions.
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static bool FindArgument(wchar_t* command_line, const char* argument);
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Set the function pointer to our function to look for a crash on start. The
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// XIB section is started pretty early in the program initialization so in
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// theory it should be called before any user created global variable
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// initialization code and CRT initialization code.
52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Note: See VC\crt\src\defsects.inc and VC\crt\src\crt0.c for reference.
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifdef _WIN64
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// "Fix" the segment. On x64, the .CRT segment is merged into the .rdata segment
56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// so it contains const data only.
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#pragma const_seg(push, ".CRT$XIB")
58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Declare the pointer so the CRT will find it.
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottextern const DebugOnStart::PIFV debug_on_start;
60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottDECLSPEC_SELECTANY const DebugOnStart::PIFV debug_on_start =
61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    &DebugOnStart::Init;
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Fix back the segment.
63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#pragma const_seg(pop)
64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#else  // _WIN64
66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// "Fix" the segment. On x86, the .CRT segment is merged into the .data segment
68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// so it contains non-const data only.
69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#pragma data_seg(push, ".CRT$XIB")
70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Declare the pointer so the CRT will find it.
71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottDECLSPEC_SELECTANY DebugOnStart::PIFV debug_on_start = &DebugOnStart::Init;
72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Fix back the segment.
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#pragma data_seg(pop)
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // _WIN64
763f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
77ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#endif  // defined(BASE_DLL)
78ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
793f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen}  // namespace debug
803f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen}  // namespace base
813f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // defined(OS_WIN)
83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
843f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#endif  // BASE_DEBUG_DEBUG_ON_START_WIN_H_
85