Skip to content

Commit

Permalink
fixup! adjust to new __builtin_verbose_trap message format
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael137 committed Jul 12, 2024
1 parent 5bb5195 commit 4dd2c33
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
54 changes: 39 additions & 15 deletions lldb/source/Target/VerboseTrapFrameRecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StackFrameRecognizer.h"
#include "lldb/Target/Target.h"

#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"

#include "clang/CodeGen/ModuleBuilder.h"

using namespace llvm;
using namespace lldb;
using namespace lldb_private;
Expand Down Expand Up @@ -55,26 +58,47 @@ VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
if (!inline_info)
return {};

auto error_message = inline_info->GetName().GetString();
if (error_message.empty())
auto func_name = inline_info->GetName().GetStringRef();
if (func_name.empty())
return {};

// Replaces "__llvm_verbose_trap: " with "Runtime Error: "
auto space_position = error_message.find(" ");
if (space_position == std::string::npos) {
Log *log = GetLog(LLDBLog::Unwind);
LLDB_LOGF(log,
"Unexpected function name format. Expected '<trap prefix>: "
"<trap message>' but got: '%s'.",
error_message.c_str());
static auto trap_regex =
llvm::Regex(llvm::formatv("^{0}\\$(.*)\\$(.*)$", ClangTrapPrefix).str());
SmallVector<llvm::StringRef, 2> matches;
std::string regex_err_msg;
if (!trap_regex.match(func_name, &matches, &regex_err_msg)) {
LLDB_LOGF(GetLog(LLDBLog::Unwind),
"Failed to parse match trap regex for '%s': %s", func_name.data(),
regex_err_msg.c_str());

return {};
}

// For `__clang_trap_msg$category$message$` we expect 3 matches:
// 1. entire string
// 2. category
// 3. message
if (matches.size() != 3) {
LLDB_LOGF(GetLog(LLDBLog::Unwind),
"Unexpected function name format. Expected '<trap prefix>$<trap "
"category>$<trap message>'$ but got: '%s'. %zu",
func_name.data(), matches.size());

return {};
}

error_message.replace(0, space_position, "Runtime Error:");
auto category = matches[1];
auto message = matches[2];

std::string stop_reason =
category.empty() ? "<empty category>" : category.str();
if (!message.empty()) {
stop_reason += ": ";
stop_reason += message.str();
}

return lldb::RecognizedStackFrameSP(new VerboseTrapRecognizedStackFrame(
most_relevant_frame_sp, std::move(error_message)));
return std::make_shared<VerboseTrapRecognizedStackFrame>(
most_relevant_frame_sp, std::move(stop_reason));
}

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

void RegisterVerboseTrapFrameRecognizer(Process &process) {
RegularExpressionSP module_regex_sp = nullptr;
RegularExpressionSP symbol_regex_sp(
new RegularExpression("^__llvm_verbose_trap: "));
auto symbol_regex_sp = std::make_shared<RegularExpression>(
llvm::formatv("^{0}", ClangTrapPrefix).str());

StackFrameRecognizerSP srf_recognizer_sp =
std::make_shared<VerboseTrapFrameRecognizer>();
Expand Down
6 changes: 5 additions & 1 deletion lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#if !defined(VERBOSE_TRAP_TEST_CATEGORY) || !defined(VERBOSE_TRAP_TEST_MESSAGE)
#error Please define required macros
#endif

struct Dummy {
void func() { __builtin_verbose_trap("Function is not implemented"); }
void func() { __builtin_verbose_trap(VERBOSE_TRAP_TEST_CATEGORY, VERBOSE_TRAP_TEST_MESSAGE); }
};

int main() {
Expand Down
19 changes: 16 additions & 3 deletions lldb/test/Shell/Recognizer/verbose_trap.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out
# RUN: %lldb -b -s %s %t.out | FileCheck %s
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"Foo\" -DVERBOSE_TRAP_TEST_MESSAGE=\"Bar\"
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-BOTH
#
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"\" -DVERBOSE_TRAP_TEST_MESSAGE=\"Bar\"
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-MESSAGE_ONLY
#
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"Foo\" -DVERBOSE_TRAP_TEST_MESSAGE=\"\"
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-CATEGORY_ONLY
#
# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out -DVERBOSE_TRAP_TEST_CATEGORY=\"\" -DVERBOSE_TRAP_TEST_MESSAGE=\"\"
# RUN: %lldb -b -s %s %t.out | FileCheck %s --check-prefixes=CHECK,CHECK-NONE

run
# CHECK: thread #{{.*}}stop reason = Runtime Error: Function is not implemented
# CHECK-BOTH: thread #{{.*}}stop reason = Foo: Bar
# CHECK-MESSAGE_ONLY: thread #{{.*}}stop reason = <empty category>: Bar
# CHECK-CATEGORY_ONLY: thread #{{.*}}stop reason = Foo
# CHECK-NONE: thread #{{.*}}stop reason = <empty category>
frame info
# CHECK: frame #{{.*}}`Dummy::func(this={{.*}}) at verbose_trap.cpp
frame recognizer info 0
Expand Down

0 comments on commit 4dd2c33

Please sign in to comment.