2016-11-07 06:57:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-02-21 20:01:41 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Atomics.h"
|
2020-06-17 00:26:16 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2017-02-21 20:01:41 +03:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
MOZ_BEGIN_EXTERN_C
|
2016-11-07 06:57:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The crash reason is defined as a global variable here rather than in the
|
|
|
|
* crash reporter itself to make it available to all code, even libraries like
|
|
|
|
* JS that don't link with the crash reporter directly. This value will only
|
|
|
|
* be consumed if the crash reporter is used by the target application.
|
|
|
|
*/
|
2017-02-21 20:01:41 +03:00
|
|
|
MFBT_DATA const char* gMozCrashReason = nullptr;
|
|
|
|
|
|
|
|
static char sPrintfCrashReason[sPrintfCrashReasonSize] = {};
|
2018-07-21 17:31:21 +03:00
|
|
|
|
|
|
|
// Accesses to this atomic are not included in web replay recordings, so that
|
|
|
|
// if we crash in an area where recorded events are not allowed the true reason
|
|
|
|
// for the crash is not obscured by a record/replay error.
|
2020-02-27 20:39:15 +03:00
|
|
|
static mozilla::Atomic<bool, mozilla::SequentiallyConsistent> sCrashing(false);
|
2017-02-21 20:01:41 +03:00
|
|
|
|
2019-04-02 22:20:41 +03:00
|
|
|
MFBT_API MOZ_COLD MOZ_NEVER_INLINE MOZ_FORMAT_PRINTF(1, 2) const
|
|
|
|
char* MOZ_CrashPrintf(const char* aFormat, ...) {
|
2017-02-21 20:01:41 +03:00
|
|
|
if (!sCrashing.compareExchange(false, true)) {
|
|
|
|
// In the unlikely event of a race condition, skip
|
|
|
|
// setting the crash reason and just crash safely.
|
2019-04-02 22:20:41 +03:00
|
|
|
MOZ_RELEASE_ASSERT(false);
|
2017-02-21 20:01:41 +03:00
|
|
|
}
|
|
|
|
va_list aArgs;
|
|
|
|
va_start(aArgs, aFormat);
|
2020-06-17 00:26:16 +03:00
|
|
|
int ret = VsprintfLiteral(sPrintfCrashReason, aFormat, aArgs);
|
2017-02-21 20:01:41 +03:00
|
|
|
va_end(aArgs);
|
|
|
|
MOZ_RELEASE_ASSERT(
|
|
|
|
ret >= 0 && size_t(ret) < sPrintfCrashReasonSize,
|
|
|
|
"Could not write the explanation string to the supplied buffer!");
|
2019-04-02 22:20:41 +03:00
|
|
|
return sPrintfCrashReason;
|
2017-02-21 20:01:41 +03:00
|
|
|
}
|
2016-11-07 06:57:00 +03:00
|
|
|
|
|
|
|
MOZ_END_EXTERN_C
|