check.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2020 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/check.h"
  5. #include "base/check_op.h"
  6. #include "base/debug/alias.h"
  7. #include "base/debug/debugging_buildflags.h"
  8. #include "base/debug/dump_without_crashing.h"
  9. #include "base/logging.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "base/thread_annotations.h"
  12. #include "build/build_config.h"
  13. #if !BUILDFLAG(IS_NACL)
  14. #include "base/debug/crash_logging.h"
  15. #endif // !BUILDFLAG(IS_NACL)
  16. #include <atomic>
  17. namespace logging {
  18. namespace {
  19. // DCHECK_IS_CONFIGURABLE and ENABLE_LOG_ERROR_NOT_REACHED are both interested
  20. // in non-FATAL DCHECK()/NOTREACHED() reports.
  21. #if BUILDFLAG(DCHECK_IS_CONFIGURABLE) || BUILDFLAG(ENABLE_LOG_ERROR_NOT_REACHED)
  22. void DCheckDumpOnceWithoutCrashing(LogMessage* log_message) {
  23. // Best-effort gate to prevent multiple DCHECKs from being dumped. This will
  24. // race if multiple threads DCHECK at the same time, but we'll eventually stop
  25. // reporting and at most report once per thread.
  26. static std::atomic<bool> has_dumped = false;
  27. if (!has_dumped.load(std::memory_order_relaxed)) {
  28. const std::string str = log_message->BuildCrashString();
  29. // Copy the LogMessage message to stack memory to make sure it can be
  30. // recovered in crash dumps.
  31. // TODO(pbos): Surface DCHECK_MESSAGE well in crash reporting to make this
  32. // redundant, then remove it.
  33. DEBUG_ALIAS_FOR_CSTR(log_message_str, str.c_str(), 1024);
  34. #if !BUILDFLAG(IS_NACL)
  35. // Report the log message as DCHECK_MESSAGE in the dump we're about to do.
  36. SCOPED_CRASH_KEY_STRING1024("Logging", "DCHECK_MESSAGE", str);
  37. #endif // !BUILDFLAG(IS_NACL)
  38. // Note that dumping may fail if the crash handler hasn't been set yet. In
  39. // that case we want to try again on the next failing DCHECK.
  40. if (base::debug::DumpWithoutCrashingUnthrottled())
  41. has_dumped.store(true, std::memory_order_relaxed);
  42. }
  43. }
  44. class NotReachedLogMessage : public LogMessage {
  45. public:
  46. using LogMessage::LogMessage;
  47. ~NotReachedLogMessage() override {
  48. if (severity() != logging::LOGGING_FATAL)
  49. DCheckDumpOnceWithoutCrashing(this);
  50. }
  51. };
  52. #else
  53. using NotReachedLogMessage = LogMessage;
  54. #endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE) ||
  55. // BUILDFLAG(ENABLE_LOG_ERROR_NOT_REACHED)
  56. #if BUILDFLAG(DCHECK_IS_CONFIGURABLE)
  57. class DCheckLogMessage : public LogMessage {
  58. public:
  59. using LogMessage::LogMessage;
  60. ~DCheckLogMessage() override {
  61. if (severity() != logging::LOGGING_FATAL)
  62. DCheckDumpOnceWithoutCrashing(this);
  63. }
  64. };
  65. #if BUILDFLAG(IS_WIN)
  66. class DCheckWin32ErrorLogMessage : public Win32ErrorLogMessage {
  67. public:
  68. using Win32ErrorLogMessage::Win32ErrorLogMessage;
  69. ~DCheckWin32ErrorLogMessage() override {
  70. if (severity() != logging::LOGGING_FATAL)
  71. DCheckDumpOnceWithoutCrashing(this);
  72. }
  73. };
  74. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  75. class DCheckErrnoLogMessage : public ErrnoLogMessage {
  76. public:
  77. using ErrnoLogMessage::ErrnoLogMessage;
  78. ~DCheckErrnoLogMessage() override {
  79. if (severity() != logging::LOGGING_FATAL)
  80. DCheckDumpOnceWithoutCrashing(this);
  81. }
  82. };
  83. #endif // BUILDFLAG(IS_WIN)
  84. #else
  85. static_assert(logging::LOGGING_DCHECK == logging::LOGGING_FATAL);
  86. using DCheckLogMessage = LogMessage;
  87. #if BUILDFLAG(IS_WIN)
  88. using DCheckWin32ErrorLogMessage = Win32ErrorLogMessage;
  89. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  90. using DCheckErrnoLogMessage = ErrnoLogMessage;
  91. #endif // BUILDFLAG(IS_WIN)
  92. #endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE)
  93. } // namespace
  94. CheckError CheckError::Check(const char* file,
  95. int line,
  96. const char* condition) {
  97. auto* const log_message = new LogMessage(file, line, LOGGING_FATAL);
  98. log_message->stream() << "Check failed: " << condition << ". ";
  99. return CheckError(log_message);
  100. }
  101. CheckError CheckError::CheckOp(const char* file,
  102. int line,
  103. CheckOpResult* check_op_result) {
  104. auto* const log_message = new LogMessage(file, line, LOGGING_FATAL);
  105. log_message->stream() << "Check failed: " << check_op_result->message_;
  106. free(check_op_result->message_);
  107. check_op_result->message_ = nullptr;
  108. return CheckError(log_message);
  109. }
  110. CheckError CheckError::DCheck(const char* file,
  111. int line,
  112. const char* condition) {
  113. auto* const log_message = new DCheckLogMessage(file, line, LOGGING_DCHECK);
  114. log_message->stream() << "Check failed: " << condition << ". ";
  115. return CheckError(log_message);
  116. }
  117. CheckError CheckError::DCheckOp(const char* file,
  118. int line,
  119. CheckOpResult* check_op_result) {
  120. auto* const log_message = new DCheckLogMessage(file, line, LOGGING_DCHECK);
  121. log_message->stream() << "Check failed: " << check_op_result->message_;
  122. free(check_op_result->message_);
  123. check_op_result->message_ = nullptr;
  124. return CheckError(log_message);
  125. }
  126. CheckError CheckError::PCheck(const char* file,
  127. int line,
  128. const char* condition) {
  129. SystemErrorCode err_code = logging::GetLastSystemErrorCode();
  130. #if BUILDFLAG(IS_WIN)
  131. auto* const log_message =
  132. new Win32ErrorLogMessage(file, line, LOGGING_FATAL, err_code);
  133. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  134. auto* const log_message =
  135. new ErrnoLogMessage(file, line, LOGGING_FATAL, err_code);
  136. #endif
  137. log_message->stream() << "Check failed: " << condition << ". ";
  138. return CheckError(log_message);
  139. }
  140. CheckError CheckError::PCheck(const char* file, int line) {
  141. return PCheck(file, line, "");
  142. }
  143. CheckError CheckError::DPCheck(const char* file,
  144. int line,
  145. const char* condition) {
  146. SystemErrorCode err_code = logging::GetLastSystemErrorCode();
  147. #if BUILDFLAG(IS_WIN)
  148. auto* const log_message =
  149. new DCheckWin32ErrorLogMessage(file, line, LOGGING_DCHECK, err_code);
  150. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  151. auto* const log_message =
  152. new DCheckErrnoLogMessage(file, line, LOGGING_DCHECK, err_code);
  153. #endif
  154. log_message->stream() << "Check failed: " << condition << ". ";
  155. return CheckError(log_message);
  156. }
  157. CheckError CheckError::NotImplemented(const char* file,
  158. int line,
  159. const char* function) {
  160. auto* const log_message = new LogMessage(file, line, LOGGING_ERROR);
  161. log_message->stream() << "Not implemented reached in " << function;
  162. return CheckError(log_message);
  163. }
  164. CheckError CheckError::NotReached(const char* file, int line) {
  165. // Outside DCHECK builds NOTREACHED() should not be FATAL. For now.
  166. const LogSeverity severity = DCHECK_IS_ON() ? LOGGING_DCHECK : LOGGING_ERROR;
  167. auto* const log_message = new NotReachedLogMessage(file, line, severity);
  168. // TODO(pbos): Consider a better message for NotReached(), this is here to
  169. // match existing behavior + test expectations.
  170. log_message->stream() << "Check failed: false. ";
  171. return CheckError(log_message);
  172. }
  173. std::ostream& CheckError::stream() {
  174. return log_message_->stream();
  175. }
  176. CheckError::~CheckError() {
  177. // Note: This function ends up in crash stack traces. If its full name
  178. // changes, the crash server's magic signature logic needs to be updated.
  179. // See cl/306632920.
  180. delete log_message_;
  181. }
  182. CheckError::CheckError(LogMessage* log_message) : log_message_(log_message) {}
  183. void RawCheck(const char* message) {
  184. RawLog(LOGGING_FATAL, message);
  185. }
  186. void RawError(const char* message) {
  187. RawLog(LOGGING_ERROR, message);
  188. }
  189. } // namespace logging