michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // Define the necessary code and global data to look for kDebugOnStart command michael@0: // line argument. When the command line argument is detected, it invokes the michael@0: // debugger, if no system-wide debugger is registered, a debug break is done. michael@0: michael@0: #ifndef BASE_DEBUG_ON_START_H_ michael@0: #define BASE_DEBUG_ON_START_H_ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: // This only works on MSVC. michael@0: #if defined(COMPILER_MSVC) michael@0: michael@0: #ifndef DECLSPEC_SELECTANY michael@0: #define DECLSPEC_SELECTANY __declspec(selectany) michael@0: #endif michael@0: michael@0: // Debug on start functions and data. michael@0: class DebugOnStart { michael@0: public: michael@0: // Expected function type in the .CRT$XI* section. michael@0: // Note: See VC\crt\src\internal.h for reference. michael@0: typedef int (__cdecl *PIFV)(void); michael@0: michael@0: // Looks at the command line for kDebugOnStart argument. If found, it invokes michael@0: // the debugger, if this fails, it crashes. michael@0: static int __cdecl Init(); michael@0: michael@0: // Returns true if the 'argument' is present in the 'command_line'. It does michael@0: // not use the CRT, only Kernel32 functions. michael@0: static bool FindArgument(wchar_t* command_line, const wchar_t* argument); michael@0: }; michael@0: michael@0: // Set the function pointer to our function to look for a crash on start. The michael@0: // XIB section is started pretty early in the program initialization so in michael@0: // theory it should be called before any user created global variable michael@0: // initialization code and CRT initialization code. michael@0: // Note: See VC\crt\src\defsects.inc and VC\crt\src\crt0.c for reference. michael@0: #ifdef _WIN64 michael@0: michael@0: // "Fix" the segment. On x64, the .CRT segment is merged into the .rdata segment michael@0: // so it contains const data only. michael@0: #pragma const_seg(push, ".CRT$XIB") michael@0: // Declare the pointer so the CRT will find it. michael@0: extern const DebugOnStart::PIFV debug_on_start; michael@0: DECLSPEC_SELECTANY const DebugOnStart::PIFV debug_on_start = michael@0: &DebugOnStart::Init; michael@0: // Fix back the segment. michael@0: #pragma const_seg(pop) michael@0: michael@0: #else // _WIN64 michael@0: michael@0: // "Fix" the segment. On x86, the .CRT segment is merged into the .data segment michael@0: // so it contains non-const data only. michael@0: #pragma data_seg(push, ".CRT$XIB") michael@0: // Declare the pointer so the CRT will find it. michael@0: DECLSPEC_SELECTANY DebugOnStart::PIFV debug_on_start = &DebugOnStart::Init; michael@0: // Fix back the segment. michael@0: #pragma data_seg(pop) michael@0: michael@0: #endif // _WIN64 michael@0: #endif // defined(OS_WIN) michael@0: michael@0: #endif // BASE_DEBUG_ON_START_H_