process_startup_helper.cc 1.8 KB

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