Skip to content

Commit 03604a7

Browse files
authored
[lldb] Make lldbassert fire only once per instance (#134343)
The `lldbassert` macro in LLDB behaves like a regular `assert` when assertions are enabled, and otherwise prints a pretty backtrace and prompts the user to file a bug. By default, this is emitted as a diagnostic event, but vendors can provide their own behavior, for example pre-populating a bug report. Recently, we ran into an issue where an `lldbassert` (in the Swift language plugin) would fire excessively, to the point that it was interfering with the usability of the debugger. Once an `lldbasser` has fired, there's no point in bothering the user over and over again for the same problem. This PR solves the problem by introducing a static `std::once_flag` in the macro. This way, every `lldbasser` will fire at most once per lldb instance. rdar://148520448
1 parent 9069ba1 commit 03604a7

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

lldb/include/lldb/Utility/LLDBAssert.h

Lines changed: 8 additions & 3 deletions
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,

lldb/source/Utility/LLDBAssert.cpp

Lines changed: 21 additions & 16 deletions
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 and include the backtrace, the "
58-
"version and 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)