[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-03-12 Thread Michael Buch via lldb-commits


@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H

Michael137 wrote:

done

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-03-12 Thread Michael Buch via lldb-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/80368

>From 4ffcb261af05b2a68781cf353d8e45bf921f8cb7 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Thu, 25 Jan 2024 11:05:02 +
Subject: [PATCH 1/6] [lldb] Add frame recognizer for __builtin_verbose_trap

This patch adds a frame recognizer for Clang's
`__builtin_verbose_trap`, which behaves like a
`__builtin_trap`, but emits a failure-reason string
into debug-info in order for debuggers to display
it to a user.

The frame recognizer triggers when we encounter
a frame with a function name that begins with
`__llvm_verbose_trap`, which is the magic prefix
Clang emits into debug-info for verbose traps.
Once such frame is encountered we display the
frame function name as the `Stop Reason` and display
that frame to the user.

Example output:
```
(lldb) run
warning: a.out was compiled with optimization - stepping may behave oddly; 
variables may not be available.
Process 35942 launched: 'a.out' (arm64)
Process 35942 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
   1struct Dummy {
   2  void func() {
-> 3__builtin_verbose_trap("Function is not implemented");
   4  }
   5};
   6
   7int main() {
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #0: 0x00013fa4 a.out`main [inlined] __llvm_verbose_trap: 
Function is not implemented at verbose_trap.cpp:0 [opt]
  * frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
frame #2: 0x00013fa4 a.out`main at verbose_trap.cpp:8:13 [opt]
frame #3: 0x000189d518b4 dyld`start + 1988
```
---
 .../lldb/Target/VerboseTrapFrameRecognizer.h  | 39 +
 lldb/source/Target/CMakeLists.txt |  1 +
 lldb/source/Target/Process.cpp|  2 +
 .../Target/VerboseTrapFrameRecognizer.cpp | 85 +++
 .../Shell/Recognizer/Inputs/verbose_trap.cpp  |  8 ++
 lldb/test/Shell/Recognizer/verbose_trap.test  |  9 ++
 6 files changed, 144 insertions(+)
 create mode 100644 lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
 create mode 100644 lldb/source/Target/VerboseTrapFrameRecognizer.cpp
 create mode 100644 lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp
 create mode 100644 lldb/test/Shell/Recognizer/verbose_trap.test

diff --git a/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h 
b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
new file mode 100644
index 00..7e045760a28be6
--- /dev/null
+++ b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+#define LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+
+#include "lldb/Target/StackFrameRecognizer.h"
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process );
+
+/// Holds the stack frame that caused the Verbose trap and the inlined stop
+/// reason message.
+class VerboseTrapRecognizedStackFrame : public RecognizedStackFrame {
+public:
+  VerboseTrapRecognizedStackFrame(lldb::StackFrameSP most_relevant_frame_sp,
+  std::string stop_desc);
+
+  lldb::StackFrameSP GetMostRelevantFrame() override;
+
+private:
+  lldb::StackFrameSP m_most_relevant_frame;
+};
+
+/// When a thread stops, it checks the current frame contains a
+/// Verbose Trap diagnostic. If so, it returns a \a
+/// VerboseTrapRecognizedStackFrame holding the diagnostic a stop reason
+/// description with and the parent frame as the most relavant frame.
+class VerboseTrapFrameRecognizer : public StackFrameRecognizer {
+public:
+  std::string GetName() override {
+return "Verbose Trap StackFrame Recognizer";
+  }
+
+  lldb::RecognizedStackFrameSP
+  RecognizeFrame(lldb::StackFrameSP frame) override;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
diff --git a/lldb/source/Target/CMakeLists.txt 
b/lldb/source/Target/CMakeLists.txt
index cf4818eae3eb8b..8186ccbea27d42 100644
--- a/lldb/source/Target/CMakeLists.txt
+++ b/lldb/source/Target/CMakeLists.txt
@@ -78,6 +78,7 @@ add_lldb_library(lldbTarget
   UnixSignals.cpp
   UnwindAssembly.cpp
   UnwindLLDB.cpp
+  VerboseTrapFrameRecognizer.cpp
 
   LINK_LIBS
 lldbBreakpoint
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 6d58873b54a3ad..cd9b48248e4bd9 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -63,6 +63,7 @@
 #include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Target/ThreadPlanStack.h"
 #include "lldb/Target/UnixSignals.h"
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
 #include "lldb/Utility/Event.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
@@ -497,6 +498,7 

[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-03-12 Thread Michael Buch via lldb-commits


@@ -0,0 +1,9 @@
+# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out
+# RUN: %lldb -b -s %s %t.out | FileCheck %s
+run
+# CHECK: thread #{{.*}}stop reason = Runtime Error: Function is not implemented

Michael137 wrote:

How about `Runtime trap: ...`?

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-03-12 Thread Michael Buch via lldb-commits


@@ -0,0 +1,98 @@
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
+
+#include "lldb/Core/Module.h"
+#include "lldb/Symbol/Function.h"
+#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
+
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+
+using namespace llvm;
+using namespace lldb;
+using namespace lldb_private;
+
+VerboseTrapRecognizedStackFrame::VerboseTrapRecognizedStackFrame(
+StackFrameSP most_relevant_frame_sp, std::string stop_desc)
+: m_most_relevant_frame(most_relevant_frame_sp) {
+  m_stop_desc = std::move(stop_desc);
+}
+
+lldb::RecognizedStackFrameSP
+VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
+  if (frame_sp->GetFrameIndex())
+return {};
+
+  ThreadSP thread_sp = frame_sp->GetThread();
+  ProcessSP process_sp = thread_sp->GetProcess();
+
+  StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(1);
+
+  if (!most_relevant_frame_sp) {
+Log *log = GetLog(LLDBLog::Unwind);
+LLDB_LOG(
+log,
+"Failed to find most relevant frame: Hit unwinding bound (1 frame)!");
+return {};
+  }
+
+  SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
+
+  if (!sc.block)
+return {};
+
+  // The runtime error is set as the function name in the inlined function info
+  // of frame #0 by the compiler
+  const InlineFunctionInfo *inline_info = nullptr;
+  Block *inline_block = sc.block->GetContainingInlinedBlock();
+
+  if (!inline_block)
+return {};
+
+  inline_info = sc.block->GetInlinedFunctionInfo();
+
+  if (!inline_info)
+return {};
+
+  auto error_message = inline_info->GetName().GetString();
+  if (error_message.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 ': "
+  "' but got: '%s'.",
+  error_message.c_str());
+
+return {};
+  }
+
+  error_message.replace(0, space_position, "Runtime Error:");
+
+  return lldb::RecognizedStackFrameSP(new VerboseTrapRecognizedStackFrame(
+  most_relevant_frame_sp, std::move(error_message)));
+}
+
+lldb::StackFrameSP VerboseTrapRecognizedStackFrame::GetMostRelevantFrame() {
+  return m_most_relevant_frame;
+}
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process ) {
+  RegularExpressionSP module_regex_sp = nullptr;
+  RegularExpressionSP symbol_regex_sp(
+  new RegularExpression("^__llvm_verbose_trap: "));

Michael137 wrote:

Yup that would be nice

I'll leave it to @ahatanak to do it in 
https://github.com/llvm/llvm-project/pull/79230 and then use it from LLDB

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-07 Thread Dan Liew via lldb-commits


@@ -0,0 +1,9 @@
+# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out
+# RUN: %lldb -b -s %s %t.out | FileCheck %s
+run
+# CHECK: thread #{{.*}}stop reason = Runtime Error: Function is not implemented

delcypher wrote:

`Runtime Error:` is pretty generic. Is there any reason we aren't mentioning 
that a trap was hit?

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-07 Thread Dan Liew via lldb-commits


@@ -0,0 +1,98 @@
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
+
+#include "lldb/Core/Module.h"
+#include "lldb/Symbol/Function.h"
+#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
+
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+
+using namespace llvm;
+using namespace lldb;
+using namespace lldb_private;
+
+VerboseTrapRecognizedStackFrame::VerboseTrapRecognizedStackFrame(
+StackFrameSP most_relevant_frame_sp, std::string stop_desc)
+: m_most_relevant_frame(most_relevant_frame_sp) {
+  m_stop_desc = std::move(stop_desc);
+}
+
+lldb::RecognizedStackFrameSP
+VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
+  if (frame_sp->GetFrameIndex())
+return {};
+
+  ThreadSP thread_sp = frame_sp->GetThread();
+  ProcessSP process_sp = thread_sp->GetProcess();
+
+  StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(1);
+
+  if (!most_relevant_frame_sp) {
+Log *log = GetLog(LLDBLog::Unwind);
+LLDB_LOG(
+log,
+"Failed to find most relevant frame: Hit unwinding bound (1 frame)!");
+return {};
+  }
+
+  SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
+
+  if (!sc.block)
+return {};
+
+  // The runtime error is set as the function name in the inlined function info
+  // of frame #0 by the compiler
+  const InlineFunctionInfo *inline_info = nullptr;
+  Block *inline_block = sc.block->GetContainingInlinedBlock();
+
+  if (!inline_block)
+return {};
+
+  inline_info = sc.block->GetInlinedFunctionInfo();
+
+  if (!inline_info)
+return {};
+
+  auto error_message = inline_info->GetName().GetString();
+  if (error_message.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 ': "
+  "' but got: '%s'.",
+  error_message.c_str());
+
+return {};
+  }
+
+  error_message.replace(0, space_position, "Runtime Error:");
+
+  return lldb::RecognizedStackFrameSP(new VerboseTrapRecognizedStackFrame(
+  most_relevant_frame_sp, std::move(error_message)));
+}
+
+lldb::StackFrameSP VerboseTrapRecognizedStackFrame::GetMostRelevantFrame() {
+  return m_most_relevant_frame;
+}
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process ) {
+  RegularExpressionSP module_regex_sp = nullptr;
+  RegularExpressionSP symbol_regex_sp(
+  new RegularExpression("^__llvm_verbose_trap: "));

delcypher wrote:

Rather than hard coding the name should we be getting it from a header file 
vended by Clang?

I mentioned this in the corresponding Clang review 
[here](https://github.com/llvm/llvm-project/pull/79230#discussion_r1475241985).

This wouldn't need to be anything sophisticated. Just a header file under 
`include/clang/CodeGen` that is something like

```
#define CLANG_VERBOSE_TRAP_PREFIX "__llvm_verbose_trap"
```

and then from LLDB that would be something like:

```
new RegularExpression("^" CLANG_VERBOSE_TRAP_PREFIX  ": ")
```

That way there is a single source of truth for the name used.

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-07 Thread Jonas Devlieghere via lldb-commits


@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H

JDevlieghere wrote:

Missing license header.

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-07 Thread Jonas Devlieghere via lldb-commits

https://github.com/JDevlieghere approved this pull request.

LGTM with comments addressed. 

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-07 Thread Jonas Devlieghere via lldb-commits


@@ -0,0 +1,98 @@
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
+
+#include "lldb/Core/Module.h"
+#include "lldb/Symbol/Function.h"
+#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
+
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+
+using namespace llvm;
+using namespace lldb;
+using namespace lldb_private;
+
+VerboseTrapRecognizedStackFrame::VerboseTrapRecognizedStackFrame(
+StackFrameSP most_relevant_frame_sp, std::string stop_desc)
+: m_most_relevant_frame(most_relevant_frame_sp) {
+  m_stop_desc = std::move(stop_desc);
+}
+
+lldb::RecognizedStackFrameSP
+VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
+  if (frame_sp->GetFrameIndex())
+return {};
+
+  ThreadSP thread_sp = frame_sp->GetThread();
+  ProcessSP process_sp = thread_sp->GetProcess();
+
+  StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(1);
+
+  if (!most_relevant_frame_sp) {
+Log *log = GetLog(LLDBLog::Unwind);
+LLDB_LOG(
+log,
+"Failed to find most relevant frame: Hit unwinding bound (1 frame)!");
+return {};
+  }
+
+  SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
+
+  if (!sc.block)
+return {};
+
+  // The runtime error is set as the function name in the inlined function info
+  // of frame #0 by the compiler
+  const InlineFunctionInfo *inline_info = nullptr;
+  Block *inline_block = sc.block->GetContainingInlinedBlock();
+
+  if (!inline_block)
+return {};
+
+  inline_info = sc.block->GetInlinedFunctionInfo();
+
+  if (!inline_info)
+return {};
+
+  auto error_message = inline_info->GetName().GetString();
+  if (error_message.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,

JDevlieghere wrote:

`LLDB_LOG` for consistency?

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-07 Thread Jonas Devlieghere via lldb-commits


@@ -0,0 +1,98 @@
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
+
+#include "lldb/Core/Module.h"
+#include "lldb/Symbol/Function.h"
+#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
+
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+
+using namespace llvm;
+using namespace lldb;
+using namespace lldb_private;
+
+VerboseTrapRecognizedStackFrame::VerboseTrapRecognizedStackFrame(
+StackFrameSP most_relevant_frame_sp, std::string stop_desc)
+: m_most_relevant_frame(most_relevant_frame_sp) {
+  m_stop_desc = std::move(stop_desc);
+}
+
+lldb::RecognizedStackFrameSP
+VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
+  if (frame_sp->GetFrameIndex())
+return {};
+
+  ThreadSP thread_sp = frame_sp->GetThread();
+  ProcessSP process_sp = thread_sp->GetProcess();
+
+  StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(1);
+
+  if (!most_relevant_frame_sp) {
+Log *log = GetLog(LLDBLog::Unwind);
+LLDB_LOG(
+log,
+"Failed to find most relevant frame: Hit unwinding bound (1 frame)!");
+return {};
+  }
+
+  SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
+
+  if (!sc.block)
+return {};
+
+  // The runtime error is set as the function name in the inlined function info
+  // of frame #0 by the compiler
+  const InlineFunctionInfo *inline_info = nullptr;
+  Block *inline_block = sc.block->GetContainingInlinedBlock();
+
+  if (!inline_block)
+return {};
+
+  inline_info = sc.block->GetInlinedFunctionInfo();
+
+  if (!inline_info)
+return {};
+
+  auto error_message = inline_info->GetName().GetString();
+  if (error_message.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);

JDevlieghere wrote:

Maybe worth hoisting `Log *log = GetLog(LLDBLog::Unwind);`, it should be pretty 
cheap?

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-07 Thread Jonas Devlieghere via lldb-commits

https://github.com/JDevlieghere edited 
https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-05 Thread Michael Buch via lldb-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/80368

>From 3f3d7aeffe1d156efd4f54cdce6f6c6ca933da75 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Thu, 25 Jan 2024 11:05:02 +
Subject: [PATCH 1/4] [lldb] Add frame recognizer for __builtin_verbose_trap

This patch adds a frame recognizer for Clang's
`__builtin_verbose_trap`, which behaves like a
`__builtin_trap`, but emits a failure-reason string
into debug-info in order for debuggers to display
it to a user.

The frame recognizer triggers when we encounter
a frame with a function name that begins with
`__llvm_verbose_trap`, which is the magic prefix
Clang emits into debug-info for verbose traps.
Once such frame is encountered we display the
frame function name as the `Stop Reason` and display
that frame to the user.

Example output:
```
(lldb) run
warning: a.out was compiled with optimization - stepping may behave oddly; 
variables may not be available.
Process 35942 launched: 'a.out' (arm64)
Process 35942 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
   1struct Dummy {
   2  void func() {
-> 3__builtin_verbose_trap("Function is not implemented");
   4  }
   5};
   6
   7int main() {
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #0: 0x00013fa4 a.out`main [inlined] __llvm_verbose_trap: 
Function is not implemented at verbose_trap.cpp:0 [opt]
  * frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
frame #2: 0x00013fa4 a.out`main at verbose_trap.cpp:8:13 [opt]
frame #3: 0x000189d518b4 dyld`start + 1988
```
---
 .../lldb/Target/VerboseTrapFrameRecognizer.h  | 39 +
 lldb/source/Target/CMakeLists.txt |  1 +
 lldb/source/Target/Process.cpp|  2 +
 .../Target/VerboseTrapFrameRecognizer.cpp | 85 +++
 .../Shell/Recognizer/Inputs/verbose_trap.cpp  |  8 ++
 lldb/test/Shell/Recognizer/verbose_trap.test  |  9 ++
 6 files changed, 144 insertions(+)
 create mode 100644 lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
 create mode 100644 lldb/source/Target/VerboseTrapFrameRecognizer.cpp
 create mode 100644 lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp
 create mode 100644 lldb/test/Shell/Recognizer/verbose_trap.test

diff --git a/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h 
b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
new file mode 100644
index 0..7e045760a28be
--- /dev/null
+++ b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+#define LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+
+#include "lldb/Target/StackFrameRecognizer.h"
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process );
+
+/// Holds the stack frame that caused the Verbose trap and the inlined stop
+/// reason message.
+class VerboseTrapRecognizedStackFrame : public RecognizedStackFrame {
+public:
+  VerboseTrapRecognizedStackFrame(lldb::StackFrameSP most_relevant_frame_sp,
+  std::string stop_desc);
+
+  lldb::StackFrameSP GetMostRelevantFrame() override;
+
+private:
+  lldb::StackFrameSP m_most_relevant_frame;
+};
+
+/// When a thread stops, it checks the current frame contains a
+/// Verbose Trap diagnostic. If so, it returns a \a
+/// VerboseTrapRecognizedStackFrame holding the diagnostic a stop reason
+/// description with and the parent frame as the most relavant frame.
+class VerboseTrapFrameRecognizer : public StackFrameRecognizer {
+public:
+  std::string GetName() override {
+return "Verbose Trap StackFrame Recognizer";
+  }
+
+  lldb::RecognizedStackFrameSP
+  RecognizeFrame(lldb::StackFrameSP frame) override;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
diff --git a/lldb/source/Target/CMakeLists.txt 
b/lldb/source/Target/CMakeLists.txt
index cf4818eae3eb8..8186ccbea27d4 100644
--- a/lldb/source/Target/CMakeLists.txt
+++ b/lldb/source/Target/CMakeLists.txt
@@ -78,6 +78,7 @@ add_lldb_library(lldbTarget
   UnixSignals.cpp
   UnwindAssembly.cpp
   UnwindLLDB.cpp
+  VerboseTrapFrameRecognizer.cpp
 
   LINK_LIBS
 lldbBreakpoint
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 23a8a66645c02..04b00aaa1fac9 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -63,6 +63,7 @@
 #include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Target/ThreadPlanStack.h"
 #include "lldb/Target/UnixSignals.h"
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
 #include "lldb/Utility/Event.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
@@ -497,6 +498,7 @@ 

[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-05 Thread Michael Buch via lldb-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/80368

>From 3f3d7aeffe1d156efd4f54cdce6f6c6ca933da75 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Thu, 25 Jan 2024 11:05:02 +
Subject: [PATCH 1/3] [lldb] Add frame recognizer for __builtin_verbose_trap

This patch adds a frame recognizer for Clang's
`__builtin_verbose_trap`, which behaves like a
`__builtin_trap`, but emits a failure-reason string
into debug-info in order for debuggers to display
it to a user.

The frame recognizer triggers when we encounter
a frame with a function name that begins with
`__llvm_verbose_trap`, which is the magic prefix
Clang emits into debug-info for verbose traps.
Once such frame is encountered we display the
frame function name as the `Stop Reason` and display
that frame to the user.

Example output:
```
(lldb) run
warning: a.out was compiled with optimization - stepping may behave oddly; 
variables may not be available.
Process 35942 launched: 'a.out' (arm64)
Process 35942 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
   1struct Dummy {
   2  void func() {
-> 3__builtin_verbose_trap("Function is not implemented");
   4  }
   5};
   6
   7int main() {
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #0: 0x00013fa4 a.out`main [inlined] __llvm_verbose_trap: 
Function is not implemented at verbose_trap.cpp:0 [opt]
  * frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
frame #2: 0x00013fa4 a.out`main at verbose_trap.cpp:8:13 [opt]
frame #3: 0x000189d518b4 dyld`start + 1988
```
---
 .../lldb/Target/VerboseTrapFrameRecognizer.h  | 39 +
 lldb/source/Target/CMakeLists.txt |  1 +
 lldb/source/Target/Process.cpp|  2 +
 .../Target/VerboseTrapFrameRecognizer.cpp | 85 +++
 .../Shell/Recognizer/Inputs/verbose_trap.cpp  |  8 ++
 lldb/test/Shell/Recognizer/verbose_trap.test  |  9 ++
 6 files changed, 144 insertions(+)
 create mode 100644 lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
 create mode 100644 lldb/source/Target/VerboseTrapFrameRecognizer.cpp
 create mode 100644 lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp
 create mode 100644 lldb/test/Shell/Recognizer/verbose_trap.test

diff --git a/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h 
b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
new file mode 100644
index 0..7e045760a28be
--- /dev/null
+++ b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+#define LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+
+#include "lldb/Target/StackFrameRecognizer.h"
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process );
+
+/// Holds the stack frame that caused the Verbose trap and the inlined stop
+/// reason message.
+class VerboseTrapRecognizedStackFrame : public RecognizedStackFrame {
+public:
+  VerboseTrapRecognizedStackFrame(lldb::StackFrameSP most_relevant_frame_sp,
+  std::string stop_desc);
+
+  lldb::StackFrameSP GetMostRelevantFrame() override;
+
+private:
+  lldb::StackFrameSP m_most_relevant_frame;
+};
+
+/// When a thread stops, it checks the current frame contains a
+/// Verbose Trap diagnostic. If so, it returns a \a
+/// VerboseTrapRecognizedStackFrame holding the diagnostic a stop reason
+/// description with and the parent frame as the most relavant frame.
+class VerboseTrapFrameRecognizer : public StackFrameRecognizer {
+public:
+  std::string GetName() override {
+return "Verbose Trap StackFrame Recognizer";
+  }
+
+  lldb::RecognizedStackFrameSP
+  RecognizeFrame(lldb::StackFrameSP frame) override;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
diff --git a/lldb/source/Target/CMakeLists.txt 
b/lldb/source/Target/CMakeLists.txt
index cf4818eae3eb8..8186ccbea27d4 100644
--- a/lldb/source/Target/CMakeLists.txt
+++ b/lldb/source/Target/CMakeLists.txt
@@ -78,6 +78,7 @@ add_lldb_library(lldbTarget
   UnixSignals.cpp
   UnwindAssembly.cpp
   UnwindLLDB.cpp
+  VerboseTrapFrameRecognizer.cpp
 
   LINK_LIBS
 lldbBreakpoint
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 23a8a66645c02..04b00aaa1fac9 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -63,6 +63,7 @@
 #include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Target/ThreadPlanStack.h"
 #include "lldb/Target/UnixSignals.h"
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
 #include "lldb/Utility/Event.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
@@ -497,6 +498,7 @@ 

[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-05 Thread Michael Buch via lldb-commits

https://github.com/Michael137 updated 
https://github.com/llvm/llvm-project/pull/80368

>From 3f3d7aeffe1d156efd4f54cdce6f6c6ca933da75 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Thu, 25 Jan 2024 11:05:02 +
Subject: [PATCH 1/2] [lldb] Add frame recognizer for __builtin_verbose_trap

This patch adds a frame recognizer for Clang's
`__builtin_verbose_trap`, which behaves like a
`__builtin_trap`, but emits a failure-reason string
into debug-info in order for debuggers to display
it to a user.

The frame recognizer triggers when we encounter
a frame with a function name that begins with
`__llvm_verbose_trap`, which is the magic prefix
Clang emits into debug-info for verbose traps.
Once such frame is encountered we display the
frame function name as the `Stop Reason` and display
that frame to the user.

Example output:
```
(lldb) run
warning: a.out was compiled with optimization - stepping may behave oddly; 
variables may not be available.
Process 35942 launched: 'a.out' (arm64)
Process 35942 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
   1struct Dummy {
   2  void func() {
-> 3__builtin_verbose_trap("Function is not implemented");
   4  }
   5};
   6
   7int main() {
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #0: 0x00013fa4 a.out`main [inlined] __llvm_verbose_trap: 
Function is not implemented at verbose_trap.cpp:0 [opt]
  * frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
frame #2: 0x00013fa4 a.out`main at verbose_trap.cpp:8:13 [opt]
frame #3: 0x000189d518b4 dyld`start + 1988
```
---
 .../lldb/Target/VerboseTrapFrameRecognizer.h  | 39 +
 lldb/source/Target/CMakeLists.txt |  1 +
 lldb/source/Target/Process.cpp|  2 +
 .../Target/VerboseTrapFrameRecognizer.cpp | 85 +++
 .../Shell/Recognizer/Inputs/verbose_trap.cpp  |  8 ++
 lldb/test/Shell/Recognizer/verbose_trap.test  |  9 ++
 6 files changed, 144 insertions(+)
 create mode 100644 lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
 create mode 100644 lldb/source/Target/VerboseTrapFrameRecognizer.cpp
 create mode 100644 lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp
 create mode 100644 lldb/test/Shell/Recognizer/verbose_trap.test

diff --git a/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h 
b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
new file mode 100644
index 00..7e045760a28be6
--- /dev/null
+++ b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+#define LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+
+#include "lldb/Target/StackFrameRecognizer.h"
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process );
+
+/// Holds the stack frame that caused the Verbose trap and the inlined stop
+/// reason message.
+class VerboseTrapRecognizedStackFrame : public RecognizedStackFrame {
+public:
+  VerboseTrapRecognizedStackFrame(lldb::StackFrameSP most_relevant_frame_sp,
+  std::string stop_desc);
+
+  lldb::StackFrameSP GetMostRelevantFrame() override;
+
+private:
+  lldb::StackFrameSP m_most_relevant_frame;
+};
+
+/// When a thread stops, it checks the current frame contains a
+/// Verbose Trap diagnostic. If so, it returns a \a
+/// VerboseTrapRecognizedStackFrame holding the diagnostic a stop reason
+/// description with and the parent frame as the most relavant frame.
+class VerboseTrapFrameRecognizer : public StackFrameRecognizer {
+public:
+  std::string GetName() override {
+return "Verbose Trap StackFrame Recognizer";
+  }
+
+  lldb::RecognizedStackFrameSP
+  RecognizeFrame(lldb::StackFrameSP frame) override;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
diff --git a/lldb/source/Target/CMakeLists.txt 
b/lldb/source/Target/CMakeLists.txt
index cf4818eae3eb8b..8186ccbea27d42 100644
--- a/lldb/source/Target/CMakeLists.txt
+++ b/lldb/source/Target/CMakeLists.txt
@@ -78,6 +78,7 @@ add_lldb_library(lldbTarget
   UnixSignals.cpp
   UnwindAssembly.cpp
   UnwindLLDB.cpp
+  VerboseTrapFrameRecognizer.cpp
 
   LINK_LIBS
 lldbBreakpoint
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 23a8a66645c02d..04b00aaa1fac9e 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -63,6 +63,7 @@
 #include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Target/ThreadPlanStack.h"
 #include "lldb/Target/UnixSignals.h"
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
 #include "lldb/Utility/Event.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
@@ -497,6 +498,7 

[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-02 Thread via lldb-commits

jimingham wrote:

I don't think the C++ or ObjC runtimes would vend their own C language 
runtimes.  I don't think that would reflect a situation any system has 
(different C runtimes for C++ and ObjC).  Rather these would just be three 
independent plugin instances.

Jim

> On Feb 2, 2024, at 3:57 PM, Adrian Prantl ***@***.***> wrote:
> 
> 
> What will happen in an Objective-C++ frame then? Will there be two C runtimes 
> because we have both a C++ and an ObjC runtime?
> 
> —
> Reply to this email directly, view it on GitHub 
> , or 
> unsubscribe 
> .
> You are receiving this because you are on a team that was mentioned.
> 



https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-02 Thread Adrian Prantl via lldb-commits


@@ -0,0 +1,85 @@
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
+
+#include "lldb/Core/Module.h"
+#include "lldb/Symbol/Function.h"
+#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
+
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+
+using namespace llvm;
+using namespace lldb;
+using namespace lldb_private;
+
+VerboseTrapRecognizedStackFrame::VerboseTrapRecognizedStackFrame(
+StackFrameSP most_relevant_frame_sp, std::string stop_desc)
+: m_most_relevant_frame(most_relevant_frame_sp) {
+  m_stop_desc = std::move(stop_desc);
+}
+
+lldb::RecognizedStackFrameSP
+VerboseTrapFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
+  if (frame_sp->GetFrameIndex())
+return {};
+
+  ThreadSP thread_sp = frame_sp->GetThread();
+  ProcessSP process_sp = thread_sp->GetProcess();
+
+  StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(1);
+
+  if (!most_relevant_frame_sp) {
+Log *log = GetLog(LLDBLog::Unwind);
+LLDB_LOG(
+log,
+"Failed to find most relevant frame: Hit unwinding bound (1 frame)!");
+return {};
+  }
+
+  SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
+
+  if (!sc.block)
+return {};
+
+  // The runtime error is set as the function name in the inlined function info
+  // of frame #0 by the compiler
+  const InlineFunctionInfo *inline_info = nullptr;
+  Block *inline_block = sc.block->GetContainingInlinedBlock();
+
+  if (!inline_block)
+return {};
+
+  inline_info = sc.block->GetInlinedFunctionInfo();
+
+  if (!inline_info)
+return {};
+
+  auto runtime_error = inline_info->GetName().GetString();
+
+  if (runtime_error.empty())
+return {};
+
+  return lldb::RecognizedStackFrameSP(new VerboseTrapRecognizedStackFrame(
+  most_relevant_frame_sp, std::move(runtime_error)));
+}
+
+lldb::StackFrameSP VerboseTrapRecognizedStackFrame::GetMostRelevantFrame() {
+  return m_most_relevant_frame;
+}
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process ) {
+  RegularExpressionSP module_regex_sp = nullptr;
+  RegularExpressionSP symbol_regex_sp(
+  new RegularExpression("(__llvm_verbose_trap)"));

adrian-prantl wrote:

Should this be `(^__llvm_verbose_trap)` so `my__llvm_verbose_trap` doesn't 
match?

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-02 Thread Adrian Prantl via lldb-commits


@@ -0,0 +1,9 @@
+# RUN: %clang_host -g -O0 %S/Inputs/verbose_trap.cpp -o %t.out
+# RUN: %lldb -b -s %s %t.out | FileCheck %s
+run
+# CHECK: thread #{{.*}}stop reason = __llvm_verbose_trap: Function is not 
implemented

adrian-prantl wrote:

Should we hide the `__llvm_verbose_trap` implementation detail and display 
something like "Runtime Error:" instead?

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-02 Thread Adrian Prantl via lldb-commits

adrian-prantl wrote:

What will happen in an Objective-C++ frame then? Will there be two C runtimes 
because we have both a C++ and an ObjC runtime?

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-02 Thread via lldb-commits

jimingham wrote:

I think that makes the most sense.

Jim

> On Feb 2, 2024, at 1:45 AM, Michael Buch ***@***.***> wrote:
> 
> 
> The patch looks fine to me, but I'm a bit bugged by the fact that it is in 
> Target. In my mind, this is part of the C LanguageRuntime, except we don't 
> yet have a C Language Runtime... But in any case, somewhere more specific 
> than Target.
> 
> Seems reasonable to me. Should I create a CLanguageRuntime plugin and move 
> the AssertRecognizedStackFrame there then? I'll open a separate PR for this 
> then
> 
> —
> Reply to this email directly, view it on GitHub 
> , or 
> unsubscribe 
> .
> You are receiving this because you are on a team that was mentioned.
> 



https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-02 Thread Michael Buch via lldb-commits

Michael137 wrote:

> The patch looks fine to me, but I'm a bit bugged by the fact that it is in 
> Target. In my mind, this is part of the C LanguageRuntime, except we don't 
> yet have a C Language Runtime... But in any case, somewhere more specific 
> than Target.

Seems reasonable to me. Should I create a `CLanguageRuntime` plugin and move 
the `AssertRecognizedStackFrame` there then? I'll open a separate PR for this 
then

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-01 Thread via lldb-commits

jimingham wrote:

That one's odd, it almost belongs in Platform, after all, there's no guarantee 
that "assert" is implemented by pthread functions at all.  It just happens to 
be on Unix systems.

Jim


> On Feb 1, 2024, at 7:32 PM, Jim Ingham ***@***.***> wrote:
> 
> There IS an AssertFrameRecogizer that does just what you suggest, but it is 
> in source/Target.  These really should go somewhere else, however, Target is 
> too general for anything but the base recognizer class.
> 
> Jim
> 
> 
>> On Feb 1, 2024, at 6:43 PM, Greg Clayton ***@***.***> wrote:
>> 
>> 
>> This almost seems like a compiler runtime plug-in, but C language would be 
>> fine. I would rather see this in plug-in somewhere if possible. It might be 
>> nice to have these plug-ins register one or more frame regular expression 
>> values that point to the plug-in so we can add more things. It would be nice 
>> to also add support for C assert() calls that would unwind the stack to the 
>> offinding assert function above the 3 or 4 pthread functions that occur when 
>> an assertion is hit
>> 
>> —
>> Reply to this email directly, view it on GitHub 
>> , 
>> or unsubscribe 
>> .
>> You are receiving this because you are on a team that was mentioned.
>> 
> 



https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-01 Thread via lldb-commits

jimingham wrote:

There IS an AssertFrameRecogizer that does just what you suggest, but it is in 
source/Target.  These really should go somewhere else, however, Target is too 
general for anything but the base recognizer class.

Jim


> On Feb 1, 2024, at 6:43 PM, Greg Clayton ***@***.***> wrote:
> 
> 
> This almost seems like a compiler runtime plug-in, but C language would be 
> fine. I would rather see this in plug-in somewhere if possible. It might be 
> nice to have these plug-ins register one or more frame regular expression 
> values that point to the plug-in so we can add more things. It would be nice 
> to also add support for C assert() calls that would unwind the stack to the 
> offinding assert function above the 3 or 4 pthread functions that occur when 
> an assertion is hit
> 
> —
> Reply to this email directly, view it on GitHub 
> , or 
> unsubscribe 
> .
> You are receiving this because you are on a team that was mentioned.
> 



https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-01 Thread Greg Clayton via lldb-commits

clayborg wrote:

This almost seems like a compiler runtime plug-in, but C language would be 
fine. I would rather see this in plug-in somewhere if possible. It might be 
nice to have these plug-ins register one or more frame regular expression 
values that point to the plug-in so we can add more things. It would be nice to 
also add support for C assert() calls that would unwind the stack to the 
offinding assert function above the 3 or 4 pthread functions that occur when an 
assertion is hit


https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-01 Thread via lldb-commits

jimingham wrote:

The patch looks fine to me, but I'm a bit bugged by the fact that it is in 
Target.  In my mind, this is part of the C LanguageRuntime, except we don't yet 
have a C Language Runtime...

https://github.com/llvm/llvm-project/pull/80368
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-01 Thread via lldb-commits

llvmbot wrote:




@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)


Changes

This patch adds a frame recognizer for Clang's
`__builtin_verbose_trap`, which behaves like a
`__builtin_trap`, but emits a failure-reason string into debug-info in order 
for debuggers to display
it to a user.

The frame recognizer triggers when we encounter
a frame with a function name that begins with
`__llvm_verbose_trap`, which is the magic prefix
Clang emits into debug-info for verbose traps.
Once such frame is encountered we display the
frame function name as the `Stop Reason` and display that frame to the user.

Example output:
```
(lldb) run
warning: a.out was compiled with optimization - stepping may behave oddly; 
variables may not be available.
Process 35942 launched: 'a.out' (arm64)
Process 35942 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=unavailable) at verbose_trap.cpp:3:5 [opt]
   1struct Dummy {
   2  void func() {
- 3__builtin_verbose_trap("Function is not implemented");
   4  }
   5};
   6
   7int main() {
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #0: 0x00013fa4 a.out`main [inlined] 
__llvm_verbose_trap: Function is not implemented at verbose_trap.cpp:0 [opt]
  * frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=unavailable) at verbose_trap.cpp:3:5 [opt]
frame #2: 0x00013fa4 a.out`main at verbose_trap.cpp:8:13 
[opt]
frame #3: 0x000189d518b4 dyld`start + 1988
```

---
Full diff: https://github.com/llvm/llvm-project/pull/80368.diff


6 Files Affected:

- (added) lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h (+39) 
- (modified) lldb/source/Target/CMakeLists.txt (+1) 
- (modified) lldb/source/Target/Process.cpp (+2) 
- (added) lldb/source/Target/VerboseTrapFrameRecognizer.cpp (+85) 
- (added) lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp (+8) 
- (added) lldb/test/Shell/Recognizer/verbose_trap.test (+9) 


``diff
diff --git a/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h 
b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
new file mode 100644
index 0..7e045760a28be
--- /dev/null
+++ b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+#define LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+
+#include "lldb/Target/StackFrameRecognizer.h"
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process );
+
+/// Holds the stack frame that caused the Verbose trap and the inlined stop
+/// reason message.
+class VerboseTrapRecognizedStackFrame : public RecognizedStackFrame {
+public:
+  VerboseTrapRecognizedStackFrame(lldb::StackFrameSP most_relevant_frame_sp,
+  std::string stop_desc);
+
+  lldb::StackFrameSP GetMostRelevantFrame() override;
+
+private:
+  lldb::StackFrameSP m_most_relevant_frame;
+};
+
+/// When a thread stops, it checks the current frame contains a
+/// Verbose Trap diagnostic. If so, it returns a \a
+/// VerboseTrapRecognizedStackFrame holding the diagnostic a stop reason
+/// description with and the parent frame as the most relavant frame.
+class VerboseTrapFrameRecognizer : public StackFrameRecognizer {
+public:
+  std::string GetName() override {
+return "Verbose Trap StackFrame Recognizer";
+  }
+
+  lldb::RecognizedStackFrameSP
+  RecognizeFrame(lldb::StackFrameSP frame) override;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
diff --git a/lldb/source/Target/CMakeLists.txt 
b/lldb/source/Target/CMakeLists.txt
index cf4818eae3eb8..8186ccbea27d4 100644
--- a/lldb/source/Target/CMakeLists.txt
+++ b/lldb/source/Target/CMakeLists.txt
@@ -78,6 +78,7 @@ add_lldb_library(lldbTarget
   UnixSignals.cpp
   UnwindAssembly.cpp
   UnwindLLDB.cpp
+  VerboseTrapFrameRecognizer.cpp
 
   LINK_LIBS
 lldbBreakpoint
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 23a8a66645c02..04b00aaa1fac9 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -63,6 +63,7 @@
 #include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Target/ThreadPlanStack.h"
 #include "lldb/Target/UnixSignals.h"
+#include "lldb/Target/VerboseTrapFrameRecognizer.h"
 #include "lldb/Utility/Event.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
@@ -497,6 +498,7 @@ Process::Process(lldb::TargetSP target_sp, ListenerSP 
listener_sp,
 value_sp->SetValueAs(platform_cache_line_size);
 
   RegisterAssertFrameRecognizer(this);
+  RegisterVerboseTrapFrameRecognizer(*this);
 }
 
 Process::~Process() {
diff --git a/lldb/source/Target/VerboseTrapFrameRecognizer.cpp 
b/lldb/source/Target/VerboseTrapFrameRecognizer.cpp
new file mode 100644
index 

[Lldb-commits] [lldb] [lldb] Add frame recognizer for __builtin_verbose_trap (PR #80368)

2024-02-01 Thread Michael Buch via lldb-commits

https://github.com/Michael137 created 
https://github.com/llvm/llvm-project/pull/80368

This patch adds a frame recognizer for Clang's
`__builtin_verbose_trap`, which behaves like a
`__builtin_trap`, but emits a failure-reason string into debug-info in order 
for debuggers to display
it to a user.

The frame recognizer triggers when we encounter
a frame with a function name that begins with
`__llvm_verbose_trap`, which is the magic prefix
Clang emits into debug-info for verbose traps.
Once such frame is encountered we display the
frame function name as the `Stop Reason` and display that frame to the user.

Example output:
```
(lldb) run
warning: a.out was compiled with optimization - stepping may behave oddly; 
variables may not be available.
Process 35942 launched: 'a.out' (arm64)
Process 35942 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
   1struct Dummy {
   2  void func() {
-> 3__builtin_verbose_trap("Function is not implemented");
   4  }
   5};
   6
   7int main() {
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #0: 0x00013fa4 a.out`main [inlined] __llvm_verbose_trap: 
Function is not implemented at verbose_trap.cpp:0 [opt]
  * frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
frame #2: 0x00013fa4 a.out`main at verbose_trap.cpp:8:13 [opt]
frame #3: 0x000189d518b4 dyld`start + 1988
```

>From 3f3d7aeffe1d156efd4f54cdce6f6c6ca933da75 Mon Sep 17 00:00:00 2001
From: Michael Buch 
Date: Thu, 25 Jan 2024 11:05:02 +
Subject: [PATCH] [lldb] Add frame recognizer for __builtin_verbose_trap

This patch adds a frame recognizer for Clang's
`__builtin_verbose_trap`, which behaves like a
`__builtin_trap`, but emits a failure-reason string
into debug-info in order for debuggers to display
it to a user.

The frame recognizer triggers when we encounter
a frame with a function name that begins with
`__llvm_verbose_trap`, which is the magic prefix
Clang emits into debug-info for verbose traps.
Once such frame is encountered we display the
frame function name as the `Stop Reason` and display
that frame to the user.

Example output:
```
(lldb) run
warning: a.out was compiled with optimization - stepping may behave oddly; 
variables may not be available.
Process 35942 launched: 'a.out' (arm64)
Process 35942 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
   1struct Dummy {
   2  void func() {
-> 3__builtin_verbose_trap("Function is not implemented");
   4  }
   5};
   6
   7int main() {
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = 
__llvm_verbose_trap: Function is not implemented
frame #0: 0x00013fa4 a.out`main [inlined] __llvm_verbose_trap: 
Function is not implemented at verbose_trap.cpp:0 [opt]
  * frame #1: 0x00013fa4 a.out`main [inlined] 
Dummy::func(this=) at verbose_trap.cpp:3:5 [opt]
frame #2: 0x00013fa4 a.out`main at verbose_trap.cpp:8:13 [opt]
frame #3: 0x000189d518b4 dyld`start + 1988
```
---
 .../lldb/Target/VerboseTrapFrameRecognizer.h  | 39 +
 lldb/source/Target/CMakeLists.txt |  1 +
 lldb/source/Target/Process.cpp|  2 +
 .../Target/VerboseTrapFrameRecognizer.cpp | 85 +++
 .../Shell/Recognizer/Inputs/verbose_trap.cpp  |  8 ++
 lldb/test/Shell/Recognizer/verbose_trap.test  |  9 ++
 6 files changed, 144 insertions(+)
 create mode 100644 lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
 create mode 100644 lldb/source/Target/VerboseTrapFrameRecognizer.cpp
 create mode 100644 lldb/test/Shell/Recognizer/Inputs/verbose_trap.cpp
 create mode 100644 lldb/test/Shell/Recognizer/verbose_trap.test

diff --git a/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h 
b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
new file mode 100644
index 0..7e045760a28be
--- /dev/null
+++ b/lldb/include/lldb/Target/VerboseTrapFrameRecognizer.h
@@ -0,0 +1,39 @@
+#ifndef LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+#define LLDB_TARGET_VERBOSETRAPFRAMERECOGNIZER_H
+
+#include "lldb/Target/StackFrameRecognizer.h"
+
+namespace lldb_private {
+
+void RegisterVerboseTrapFrameRecognizer(Process );
+
+/// Holds the stack frame that caused the Verbose trap and the inlined stop
+/// reason message.
+class VerboseTrapRecognizedStackFrame : public RecognizedStackFrame {
+public:
+  VerboseTrapRecognizedStackFrame(lldb::StackFrameSP most_relevant_frame_sp,
+  std::string stop_desc);
+
+  lldb::StackFrameSP