Bug 1882518 - Print the PID when an assertion fails or MOZ_CRASH is reached in debug. r=gsvelto,glandium

Differential Revision: https://phabricator.services.mozilla.com/D202937
This commit is contained in:
Paul Adenot 2024-03-22 12:19:50 +00:00
parent bbcf4a3807
commit 30c2d20f64

View file

@ -17,6 +17,18 @@
# define MOZ_BUFFER_STDERR # define MOZ_BUFFER_STDERR
#endif #endif
// It appears that this is sometimes compiled without XP_WIN
#if defined(WIN32)
# include <process.h>
# define MOZ_GET_PID() _getpid()
#elif !defined(__wasi__)
# include <unistd.h>
# define MOZ_GET_PID() getpid()
#else
// Prevent compiler warning
# define MOZ_GET_PID() -1
#endif
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/Compiler.h" #include "mozilla/Compiler.h"
#include "mozilla/Fuzzing.h" #include "mozilla/Fuzzing.h"
@ -95,14 +107,15 @@ MOZ_MAYBE_UNUSED static void MOZ_ReportAssertionFailurePrintFrame(
* method is primarily for internal use in this header, and only secondarily * method is primarily for internal use in this header, and only secondarily
* for use in implementing release-build assertions. * for use in implementing release-build assertions.
*/ */
MOZ_MAYBE_UNUSED static MOZ_COLD MOZ_NEVER_INLINE void MOZ_MAYBE_UNUSED static MOZ_COLD MOZ_NEVER_INLINE void
MOZ_ReportAssertionFailure(const char* aStr, const char* aFilename, MOZ_ReportAssertionFailure(const char* aStr, const char* aFilename,
int aLine) MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS { int aLine) MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS {
MOZ_FUZZING_HANDLE_CRASH_EVENT4("MOZ_ASSERT", aFilename, aLine, aStr); MOZ_FUZZING_HANDLE_CRASH_EVENT4("MOZ_ASSERT", aFilename, aLine, aStr);
#ifdef ANDROID #ifdef ANDROID
__android_log_print(ANDROID_LOG_FATAL, "MOZ_Assert", __android_log_print(ANDROID_LOG_FATAL, "MOZ_Assert",
"Assertion failure: %s, at %s:%d\n", aStr, aFilename, "[%d] Assertion failure: %s, at %s:%d\n", MOZ_GET_PID(),
aLine); aStr, aFilename, aLine);
# if defined(MOZ_DUMP_ASSERTION_STACK) # if defined(MOZ_DUMP_ASSERTION_STACK)
MozWalkTheStackWithWriter(MOZ_ReportAssertionFailurePrintFrame, CallerPC(), MozWalkTheStackWithWriter(MOZ_ReportAssertionFailurePrintFrame, CallerPC(),
/* aMaxFrames */ 0); /* aMaxFrames */ 0);
@ -110,11 +123,12 @@ MOZ_ReportAssertionFailure(const char* aStr, const char* aFilename,
#else #else
# if defined(MOZ_BUFFER_STDERR) # if defined(MOZ_BUFFER_STDERR)
char msg[1024] = ""; char msg[1024] = "";
snprintf(msg, sizeof(msg) - 1, "Assertion failure: %s, at %s:%d\n", aStr, snprintf(msg, sizeof(msg) - 1, "[%d] Assertion failure: %s, at %s:%d\n",
aFilename, aLine); MOZ_GET_PID(), aStr, aFilename, aLine);
fputs(msg, stderr); fputs(msg, stderr);
# else # else
fprintf(stderr, "Assertion failure: %s, at %s:%d\n", aStr, aFilename, aLine); fprintf(stderr, "[%d] Assertion failure: %s, at %s:%d\n", MOZ_GET_PID(), aStr,
aFilename, aLine);
# endif # endif
# if defined(MOZ_DUMP_ASSERTION_STACK) # if defined(MOZ_DUMP_ASSERTION_STACK)
MozWalkTheStack(stderr, CallerPC(), /* aMaxFrames */ 0); MozWalkTheStack(stderr, CallerPC(), /* aMaxFrames */ 0);
@ -128,15 +142,17 @@ MOZ_MAYBE_UNUSED static MOZ_COLD MOZ_NEVER_INLINE void MOZ_ReportCrash(
int aLine) MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS { int aLine) MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS {
#ifdef ANDROID #ifdef ANDROID
__android_log_print(ANDROID_LOG_FATAL, "MOZ_CRASH", __android_log_print(ANDROID_LOG_FATAL, "MOZ_CRASH",
"Hit MOZ_CRASH(%s) at %s:%d\n", aStr, aFilename, aLine); "[%d] Hit MOZ_CRASH(%s) at %s:%d\n", MOZ_GET_PID(), aStr,
aFilename, aLine);
#else #else
# if defined(MOZ_BUFFER_STDERR) # if defined(MOZ_BUFFER_STDERR)
char msg[1024] = ""; char msg[1024] = "";
snprintf(msg, sizeof(msg) - 1, "Hit MOZ_CRASH(%s) at %s:%d\n", aStr, snprintf(msg, sizeof(msg) - 1, "[%d] Hit MOZ_CRASH(%s) at %s:%d\n",
aFilename, aLine); MOZ_GET_PID(), aStr, aFilename, aLine);
fputs(msg, stderr); fputs(msg, stderr);
# else # else
fprintf(stderr, "Hit MOZ_CRASH(%s) at %s:%d\n", aStr, aFilename, aLine); fprintf(stderr, "[%d] Hit MOZ_CRASH(%s) at %s:%d\n", MOZ_GET_PID(), aStr,
aFilename, aLine);
# endif # endif
# if defined(MOZ_DUMP_ASSERTION_STACK) # if defined(MOZ_DUMP_ASSERTION_STACK)
MozWalkTheStack(stderr, CallerPC(), /* aMaxFrames */ 0); MozWalkTheStack(stderr, CallerPC(), /* aMaxFrames */ 0);