1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/win/process_startup_helper.h"
6
7 #include <crtdbg.h>
8 #include <new.h>
9
10 #include "base/base_switches.h"
11 #include "base/command_line.h"
12
13 namespace {
14
15 // Handlers for invalid parameter and pure call. They generate a breakpoint to
16 // tell breakpad that it needs to dump the process.
17 // These functions should be written to be unique in order to avoid confusing
18 // call stacks from /OPT:ICF function folding. Printing a unique message or
19 // returning a unique value will do this. Note that for best results they need
20 // to be unique from *all* functions in Chrome.
InvalidParameter(const wchar_t * expression,const wchar_t * function,const wchar_t * file,unsigned int line,uintptr_t reserved)21 void InvalidParameter(const wchar_t* expression,
22 const wchar_t* function,
23 const wchar_t* file,
24 unsigned int line,
25 uintptr_t reserved) {
26 __debugbreak();
27 // Use a different exit code from PureCall to avoid COMDAT folding.
28 _exit(1);
29 }
30
PureCall()31 void PureCall() {
32 __debugbreak();
33 // Use a different exit code from InvalidParameter to avoid COMDAT folding.
34 _exit(2);
35 }
36
37 } // namespace
38
39 namespace base {
40 namespace win {
41
42 // Register the invalid param handler and pure call handler to be able to
43 // notify breakpad when it happens.
RegisterInvalidParamHandler()44 void RegisterInvalidParamHandler() {
45 _set_invalid_parameter_handler(InvalidParameter);
46 _set_purecall_handler(PureCall);
47 }
48
SetupCRT(const CommandLine & command_line)49 void SetupCRT(const CommandLine& command_line) {
50 #if defined(_CRTDBG_MAP_ALLOC)
51 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
52 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
53 #else
54 if (!command_line.HasSwitch(switches::kDisableBreakpad)) {
55 _CrtSetReportMode(_CRT_ASSERT, 0);
56 }
57 #endif
58 }
59
60 } // namespace win
61 } // namespace base
62