1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/Assertions.h"
7 #include "mozilla/Atomics.h"
14 * The crash reason is defined as a global variable here rather than in the
15 * crash reporter itself to make it available to all code, even libraries like
16 * JS that don't link with the crash reporter directly. This value will only
17 * be consumed if the crash reporter is used by the target application.
19 MFBT_DATA
const char* gMozCrashReason
= nullptr;
21 static char sPrintfCrashReason
[sPrintfCrashReasonSize
] = {};
23 // Accesses to this atomic are not included in web replay recordings, so that
24 // if we crash in an area where recorded events are not allowed the true reason
25 // for the crash is not obscured by a record/replay error.
26 static mozilla::Atomic
<bool, mozilla::SequentiallyConsistent
> sCrashing(false);
28 MFBT_API MOZ_COLD MOZ_NEVER_INLINE
MOZ_FORMAT_PRINTF(1, 2) const
29 char* MOZ_CrashPrintf(const char* aFormat
, ...) {
30 if (!sCrashing
.compareExchange(false, true)) {
31 // In the unlikely event of a race condition, skip
32 // setting the crash reason and just crash safely.
33 MOZ_RELEASE_ASSERT(false);
36 va_start(aArgs
, aFormat
);
38 vsnprintf(sPrintfCrashReason
, sPrintfCrashReasonSize
, aFormat
, aArgs
);
41 ret
>= 0 && size_t(ret
) < sPrintfCrashReasonSize
,
42 "Could not write the explanation string to the supplied buffer!");
43 return sPrintfCrashReason
;