Skip to content

Commit 6bbf376

Browse files
authored
Merge pull request #10480 from swiftlang/lldbassert-6.2
Cherrypick lldbassert fixes to 6.2
2 parents 73daaea + 4c2fe97 commit 6bbf376

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
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/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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}

lldb/source/Core/Debugger.cpp

Lines changed: 4 additions & 2 deletions
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)
@@ -1553,8 +1554,9 @@ bool Debugger::FormatDisassemblerAddress(const FormatEntity::Entry *format,
15531554
void Debugger::AssertCallback(llvm::StringRef message,
15541555
llvm::StringRef backtrace,
15551556
llvm::StringRef prompt) {
1556-
Debugger::ReportError(
1557-
llvm::formatv("{0}\n{1}{2}", message, backtrace, prompt).str());
1557+
Debugger::ReportError(llvm::formatv("{0}\n{1}{2}\n{3}", message, backtrace,
1558+
GetVersion(), prompt)
1559+
.str());
15581560
}
15591561

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

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 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)