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"
8 #include "mozilla/Sprintf.h"
15 * The crash reason is defined as a global variable here rather than in the
16 * crash reporter itself to make it available to all code, even libraries like
17 * JS that don't link with the crash reporter directly. This value will only
18 * be consumed if the crash reporter is used by the target application.
20 MFBT_DATA
const char* gMozCrashReason
= nullptr;
22 static char sPrintfCrashReason
[sPrintfCrashReasonSize
] = {};
24 // Accesses to this atomic are not included in web replay recordings, so that
25 // if we crash in an area where recorded events are not allowed the true reason
26 // for the crash is not obscured by a record/replay error.
27 static mozilla::Atomic
<bool, mozilla::SequentiallyConsistent
> sCrashing(false);
29 MFBT_API MOZ_COLD MOZ_NEVER_INLINE
MOZ_FORMAT_PRINTF(1, 2) const
30 char* MOZ_CrashPrintf(const char* aFormat
, ...) {
31 if (!sCrashing
.compareExchange(false, true)) {
32 // In the unlikely event of a race condition, skip
33 // setting the crash reason and just crash safely.
34 MOZ_RELEASE_ASSERT(false);
37 va_start(aArgs
, aFormat
);
38 int ret
= VsprintfLiteral(sPrintfCrashReason
, aFormat
, aArgs
);
41 ret
>= 0 && size_t(ret
) < sPrintfCrashReasonSize
,
42 "Could not write the explanation string to the supplied buffer!");
43 return sPrintfCrashReason
;