Skip to content

Commit bfdd4ce

Browse files
authored
Merge pull request #10433 from swiftlang/cherrypick-stable/20240723-94b04b411903-03604a784011
[🍒 stable/20240723] [lldb] Make lldbassert fire only once per instance (llvm#134343)
2 parents 58a2900 + f112d6c commit bfdd4ce

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

Diff for: lldb/include/lldb/Utility/LLDBAssert.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_UTILITY_LLDBASSERT_H
1111

1212
#include "llvm/ADT/StringRef.h"
13+
#include <mutex>
1314

1415
#ifndef NDEBUG
1516
#define lldbassert(x) assert(x)
@@ -19,8 +20,11 @@
1920
// __FILE__ but only renders the last path component (the filename) instead of
2021
// an invocation dependent full path to that file.
2122
#define lldbassert(x) \
22-
lldb_private::_lldb_assert(static_cast<bool>(x), #x, __FUNCTION__, \
23-
__FILE_NAME__, __LINE__)
23+
do { \
24+
static std::once_flag _once_flag; \
25+
lldb_private::_lldb_assert(static_cast<bool>(x), #x, __FUNCTION__, \
26+
__FILE_NAME__, __LINE__, _once_flag); \
27+
} while (0)
2428
#else
2529
#define lldbassert(x) \
2630
lldb_private::_lldb_assert(static_cast<bool>(x), #x, __FUNCTION__, __FILE__, \
@@ -33,7 +37,8 @@ namespace lldb_private {
3337
/// Don't use _lldb_assert directly. Use the lldbassert macro instead so that
3438
/// LLDB asserts become regular asserts in NDEBUG builds.
3539
void _lldb_assert(bool expression, const char *expr_text, const char *func,
36-
const char *file, unsigned int line);
40+
const char *file, unsigned int line,
41+
std::once_flag &once_flag);
3742

3843
/// The default LLDB assert callback, which prints to stderr.
3944
typedef void (*LLDBAssertCallback)(llvm::StringRef message,

Diff for: lldb/source/Core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ add_lldb_library(lldbCore
7171
lldbTarget
7272
lldbUtility
7373
lldbValueObject
74+
lldbVersion
7475
lldbPluginCPlusPlusLanguage
7576
lldbPluginObjCLanguage
7677
${LLDB_CURSES_LIBS}

Diff for: lldb/source/Core/Debugger.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "lldb/Utility/State.h"
5353
#include "lldb/Utility/Stream.h"
5454
#include "lldb/Utility/StreamString.h"
55+
#include "lldb/Version/Version.h"
5556
#include "lldb/lldb-enumerations.h"
5657

5758
#if defined(_WIN32)
@@ -1546,8 +1547,9 @@ bool Debugger::FormatDisassemblerAddress(const FormatEntity::Entry *format,
15461547
void Debugger::AssertCallback(llvm::StringRef message,
15471548
llvm::StringRef backtrace,
15481549
llvm::StringRef prompt) {
1549-
Debugger::ReportError(
1550-
llvm::formatv("{0}\n{1}{2}", message, backtrace, prompt).str());
1550+
Debugger::ReportError(llvm::formatv("{0}\n{1}{2}\n{3}", message, backtrace,
1551+
GetVersion(), prompt)
1552+
.str());
15511553
}
15521554

15531555
void Debugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,

Diff for: lldb/source/Utility/LLDBAssert.cpp

+21-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/Support/FormatVariadic.h"
1212
#include "llvm/Support/Signals.h"
1313
#include "llvm/Support/raw_ostream.h"
14+
#include <mutex>
1415

1516
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
1617
#include <os/log.h>
@@ -33,29 +34,33 @@ static std::atomic<LLDBAssertCallback> g_lldb_assert_callback =
3334
&DefaultAssertCallback;
3435

3536
void _lldb_assert(bool expression, const char *expr_text, const char *func,
36-
const char *file, unsigned int line) {
37+
const char *file, unsigned int line,
38+
std::once_flag &once_flag) {
3739
if (LLVM_LIKELY(expression))
3840
return;
3941

42+
std::call_once(once_flag, [&]() {
4043
#if LLVM_SUPPORT_XCODE_SIGNPOSTS
41-
if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
42-
os_log_fault(OS_LOG_DEFAULT,
43-
"Assertion failed: (%s), function %s, file %s, line %u\n",
44-
expr_text, func, file, line);
45-
}
44+
if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
45+
os_log_fault(OS_LOG_DEFAULT,
46+
"Assertion failed: (%s), function %s, file %s, line %u\n",
47+
expr_text, func, file, line);
48+
}
4649
#endif
4750

48-
std::string buffer;
49-
llvm::raw_string_ostream backtrace(buffer);
50-
llvm::sys::PrintStackTrace(backtrace);
51+
std::string buffer;
52+
llvm::raw_string_ostream backtrace(buffer);
53+
llvm::sys::PrintStackTrace(backtrace);
5154

52-
(*g_lldb_assert_callback.load())(
53-
llvm::formatv("Assertion failed: ({0}), function {1}, file {2}, line {3}",
54-
expr_text, func, file, line)
55-
.str(),
56-
buffer,
57-
"Please file a bug report against lldb reporting this failure log, and "
58-
"as many details as possible");
55+
(*g_lldb_assert_callback.load())(
56+
llvm::formatv(
57+
"Assertion failed: ({0}), function {1}, file {2}, line {3}",
58+
expr_text, func, file, line)
59+
.str(),
60+
buffer,
61+
"Please file a bug report against lldb and include the backtrace, the "
62+
"version and as many details as possible.");
63+
});
5964
}
6065

6166
void SetLLDBAssertCallback(LLDBAssertCallback callback) {

0 commit comments

Comments
 (0)