Skip to content

Commit 4dd2c33

Browse files
committed
fixup! adjust to new __builtin_verbose_trap message format
1 parent 5bb5195 commit 4dd2c33

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

lldb/source/Target/VerboseTrapFrameRecognizer.cpp

+39-15
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
#include "lldb/Symbol/Function.h"
55
#include "lldb/Symbol/SymbolContext.h"
66
#include "lldb/Target/Process.h"
7+
#include "lldb/Target/StackFrameRecognizer.h"
78
#include "lldb/Target/Target.h"
89

910
#include "lldb/Utility/LLDBLog.h"
1011
#include "lldb/Utility/Log.h"
1112

13+
#include "clang/CodeGen/ModuleBuilder.h"
14+
1215
using namespace llvm;
1316
using namespace lldb;
1417
using namespace lldb_private;
@@ -55,26 +58,47 @@ VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
5558
if (!inline_info)
5659
return {};
5760

58-
auto error_message = inline_info->GetName().GetString();
59-
if (error_message.empty())
61+
auto func_name = inline_info->GetName().GetStringRef();
62+
if (func_name.empty())
6063
return {};
6164

62-
// Replaces "__llvm_verbose_trap: " with "Runtime Error: "
63-
auto space_position = error_message.find(" ");
64-
if (space_position == std::string::npos) {
65-
Log *log = GetLog(LLDBLog::Unwind);
66-
LLDB_LOGF(log,
67-
"Unexpected function name format. Expected '<trap prefix>: "
68-
"<trap message>' but got: '%s'.",
69-
error_message.c_str());
65+
static auto trap_regex =
66+
llvm::Regex(llvm::formatv("^{0}\\$(.*)\\$(.*)$", ClangTrapPrefix).str());
67+
SmallVector<llvm::StringRef, 2> matches;
68+
std::string regex_err_msg;
69+
if (!trap_regex.match(func_name, &matches, &regex_err_msg)) {
70+
LLDB_LOGF(GetLog(LLDBLog::Unwind),
71+
"Failed to parse match trap regex for '%s': %s", func_name.data(),
72+
regex_err_msg.c_str());
73+
74+
return {};
75+
}
76+
77+
// For `__clang_trap_msg$category$message$` we expect 3 matches:
78+
// 1. entire string
79+
// 2. category
80+
// 3. message
81+
if (matches.size() != 3) {
82+
LLDB_LOGF(GetLog(LLDBLog::Unwind),
83+
"Unexpected function name format. Expected '<trap prefix>$<trap "
84+
"category>$<trap message>'$ but got: '%s'. %zu",
85+
func_name.data(), matches.size());
7086

7187
return {};
7288
}
7389

74-
error_message.replace(0, space_position, "Runtime Error:");
90+
auto category = matches[1];
91+
auto message = matches[2];
92+
93+
std::string stop_reason =
94+
category.empty() ? "<empty category>" : category.str();
95+
if (!message.empty()) {
96+
stop_reason += ": ";
97+
stop_reason += message.str();
98+
}
7599

76-
return lldb::RecognizedStackFrameSP(new VerboseTrapRecognizedStackFrame(
77-
most_relevant_frame_sp, std::move(error_message)));
100+
return std::make_shared<VerboseTrapRecognizedStackFrame>(
101+
most_relevant_frame_sp, std::move(stop_reason));
78102
}
79103

80104
lldb::StackFrameSP VerboseTrapRecognizedStackFrame::GetMostRelevantFrame() {
@@ -85,8 +109,8 @@ namespace lldb_private {
85109

86110
void RegisterVerboseTrapFrameRecognizer(Process &process) {
87111
RegularExpressionSP module_regex_sp = nullptr;
88-
RegularExpressionSP symbol_regex_sp(
89-
new RegularExpression("^__llvm_verbose_trap: "));
112+
auto symbol_regex_sp = std::make_shared<RegularExpression>(
113+
llvm::formatv("^{0}", ClangTrapPrefix).str());
90114

91115
StackFrameRecognizerSP srf_recognizer_sp =
92116
std::make_shared<VerboseTrapFrameRecognizer>();

lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#if !defined(VERBOSE_TRAP_TEST_CATEGORY) || !defined(VERBOSE_TRAP_TEST_MESSAGE)
2+
#error Please define required macros
3+
#endif
4+
15
struct Dummy {
2-
void func() { __builtin_verbose_trap("Function is not implemented"); }
6+
void func() { __builtin_verbose_trap(VERBOSE_TRAP_TEST_CATEGORY, VERBOSE_TRAP_TEST_MESSAGE); }
37
};
48

59
int main() {

lldb/test/Shell/Recognizer/verbose_trap.test

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out
2-
# RUN: %lldb -b -s %s %t.out | FileCheck %s
1+
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"Foo\" -DVERBOSE_TRAP_TEST_MESSAGE=\"Bar\"
2+
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-BOTH
3+
#
4+
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"\" -DVERBOSE_TRAP_TEST_MESSAGE=\"Bar\"
5+
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-MESSAGE_ONLY
6+
#
7+
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"Foo\" -DVERBOSE_TRAP_TEST_MESSAGE=\"\"
8+
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-CATEGORY_ONLY
9+
#
10+
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"\" -DVERBOSE_TRAP_TEST_MESSAGE=\"\"
11+
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-NONE
12+
313
run
4-
# CHECK: thread #{{.*}}stop reason = Runtime Error: Function is not implemented
14+
# CHECK-BOTH: thread #{{.*}}stop reason = Foo: Bar
15+
# CHECK-MESSAGE_ONLY: thread #{{.*}}stop reason = <empty category>: Bar
16+
# CHECK-CATEGORY_ONLY: thread #{{.*}}stop reason = Foo
17+
# CHECK-NONE: thread #{{.*}}stop reason = <empty category>
518
frame info
619
# CHECK: frame #{{.*}}`Dummy::func(this={{.*}}) at verbose_trap.cpp
720
frame recognizer info 0

0 commit comments

Comments
 (0)