FatalHelper.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/init/Init.h>
  17. #include <folly/logging/xlog.h>
  18. #include <folly/portability/Stdlib.h>
  19. #include <iostream>
  20. DEFINE_string(
  21. category,
  22. "",
  23. "Crash with a message to this category instead of the default");
  24. DEFINE_bool(crash, true, "Crash with a fatal log message.");
  25. DEFINE_bool(
  26. check_debug,
  27. false,
  28. "Print whether this binary was built in debug mode "
  29. "and then exit successfully");
  30. DEFINE_bool(fail_fatal_xlog_if, false, "Fail an XLOG_IF(FATAL) check.");
  31. DEFINE_bool(fail_dfatal_xlog_if, false, "Fail an XLOG_IF(DFATAL) check.");
  32. DEFINE_bool(fail_xcheck, false, "Fail an XCHECK() test.");
  33. DEFINE_bool(
  34. fail_xcheck_nomsg,
  35. false,
  36. "Fail an XCHECK() test with no additional message.");
  37. DEFINE_bool(fail_xdcheck, false, "Fail an XDCHECK() test.");
  38. using folly::LogLevel;
  39. namespace {
  40. /**
  41. * Helper class to optionally log a fatal message during static initialization
  42. * or destruction.
  43. *
  44. * Since command line arguments have not been processed during static
  45. * initialization, we check an environment variable.
  46. */
  47. class InitChecker {
  48. public:
  49. InitChecker() : value_{getenv("CRASH_DURING_INIT")} {
  50. if (value_ && strcmp(value_, "shutdown") != 0) {
  51. XLOG(FATAL) << "crashing during static initialization";
  52. }
  53. }
  54. ~InitChecker() {
  55. if (value_) {
  56. XLOG(FATAL) << "crashing during static destruction";
  57. }
  58. }
  59. const char* value_{nullptr};
  60. };
  61. static InitChecker initChecker;
  62. } // namespace
  63. namespace {
  64. int runHelper() {
  65. if (!FLAGS_category.empty()) {
  66. folly::Logger logger{FLAGS_category};
  67. FB_LOG(logger, FATAL, "crashing to category ", FLAGS_category);
  68. }
  69. if (!FLAGS_crash) {
  70. return 0;
  71. }
  72. XLOG(FATAL) << "test program crashing!";
  73. // Even though this function is defined to return an integer, the compiler
  74. // should be able to detect that XLOG(FATAL) never returns. It shouldn't
  75. // complain that we don't return an integer here.
  76. }
  77. } // namespace
  78. std::string fbLogFatalCheck() {
  79. folly::Logger logger("some.category");
  80. FB_LOG(logger, FATAL) << "we always crash";
  81. // This function mostly exists to make sure the compiler does not warn
  82. // about a missing return statement here.
  83. }
  84. /*
  85. * This is a simple helper program to exercise the LOG(FATAL) functionality.
  86. */
  87. int main(int argc, char* argv[]) {
  88. auto init = folly::Init(&argc, &argv);
  89. if (FLAGS_check_debug) {
  90. std::cout << "DEBUG=" << static_cast<int>(folly::kIsDebug) << "\n";
  91. return 0;
  92. }
  93. XLOG_IF(FATAL, FLAGS_fail_fatal_xlog_if) << "--fail_fatal_xlog_if specified!";
  94. XLOG_IF(DFATAL, FLAGS_fail_dfatal_xlog_if)
  95. << "--fail_dfatal_xlog_if specified!";
  96. XCHECK(!FLAGS_fail_xcheck) << ": --fail_xcheck specified!";
  97. XCHECK(!FLAGS_fail_xcheck_nomsg);
  98. XDCHECK(!FLAGS_fail_xdcheck) << ": --fail_xdcheck specified!";
  99. // Do most of the work in a separate helper function.
  100. //
  101. // The main reason for putting this in a helper function is to ensure that
  102. // the compiler does not warn about missing return statements on XLOG(FATAL)
  103. // code paths. Unfortunately it appears like some compilers always suppress
  104. // this warning for main().
  105. return runHelper();
  106. }