Home | History | Annotate | Download | only in debug
      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.
      4 
      5 // Define the necessary code and global data to look for kDebugOnStart command
      6 // line argument. When the command line argument is detected, it invokes the
      7 // debugger, if no system-wide debugger is registered, a debug break is done.
      8 
      9 #ifndef BASE_DEBUG_DEBUG_ON_START_WIN_H_
     10 #define BASE_DEBUG_DEBUG_ON_START_WIN_H_
     11 
     12 #include "base/basictypes.h"
     13 #include "build/build_config.h"
     14 
     15 // This only works on Windows. It's legal to include on other platforms, but
     16 // will be a NOP.
     17 #if defined(OS_WIN)
     18 
     19 #ifndef DECLSPEC_SELECTANY
     20 #define DECLSPEC_SELECTANY  __declspec(selectany)
     21 #endif
     22 
     23 namespace base {
     24 namespace debug {
     25 
     26 // There is no way for this code, as currently implemented, to work across DLLs.
     27 // TODO(rvargas): It looks like we really don't use this code, at least not for
     28 // Chrome. Figure out if it's really worth implementing something simpler.
     29 #if !defined(COMPONENT_BUILD)
     30 
     31 // Debug on start functions and data.
     32 class DebugOnStart {
     33  public:
     34   // Expected function type in the .CRT$XI* section.
     35   // Note: See VC\crt\src\internal.h for reference.
     36   typedef int  (__cdecl *PIFV)(void);
     37 
     38   // Looks at the command line for kDebugOnStart argument. If found, it invokes
     39   // the debugger, if this fails, it crashes.
     40   static int __cdecl Init();
     41 
     42   // Returns true if the 'argument' is present in the 'command_line'. It does
     43   // not use the CRT, only Kernel32 functions.
     44   static bool FindArgument(wchar_t* command_line, const char* argument);
     45 };
     46 
     47 // Set the function pointer to our function to look for a crash on start. The
     48 // XIB section is started pretty early in the program initialization so in
     49 // theory it should be called before any user created global variable
     50 // initialization code and CRT initialization code.
     51 // Note: See VC\crt\src\defsects.inc and VC\crt\src\crt0.c for reference.
     52 #ifdef _WIN64
     53 
     54 // "Fix" the segment. On x64, the .CRT segment is merged into the .rdata segment
     55 // so it contains const data only.
     56 #pragma const_seg(push, ".CRT$XIB")
     57 // Declare the pointer so the CRT will find it.
     58 extern const DebugOnStart::PIFV debug_on_start;
     59 DECLSPEC_SELECTANY const DebugOnStart::PIFV debug_on_start =
     60     &DebugOnStart::Init;
     61 // Fix back the segment.
     62 #pragma const_seg(pop)
     63 
     64 #else  // _WIN64
     65 
     66 // "Fix" the segment. On x86, the .CRT segment is merged into the .data segment
     67 // so it contains non-const data only.
     68 #pragma data_seg(push, ".CRT$XIB")
     69 // Declare the pointer so the CRT will find it.
     70 DECLSPEC_SELECTANY DebugOnStart::PIFV debug_on_start = &DebugOnStart::Init;
     71 // Fix back the segment.
     72 #pragma data_seg(pop)
     73 
     74 #endif  // _WIN64
     75 
     76 #endif  // defined(COMPONENT_BUILD)
     77 
     78 }  // namespace debug
     79 }  // namespace base
     80 
     81 #endif  // defined(OS_WIN)
     82 
     83 #endif  // BASE_DEBUG_DEBUG_ON_START_WIN_H_
     84