1 #include "lldb/Target/AssertFrameRecognizer.h"
2 #include "lldb/Core/Module.h"
3 #include "lldb/Symbol/Function.h"
4 #include "lldb/Symbol/SymbolContext.h"
5 #include "lldb/Target/Process.h"
6 #include "lldb/Target/StackFrameList.h"
7 #include "lldb/Target/Target.h"
8 #include "lldb/Target/Thread.h"
9 #include "lldb/Utility/LLDBLog.h"
13 using namespace lldb_private
;
15 namespace lldb_private
{
17 /// Stores a function module spec, symbol name and possibly an alternate symbol
19 struct SymbolLocation
{
21 std::vector
<ConstString
> symbols
;
23 // The symbols are regular expressions. In such case all symbols are matched
24 // with their trailing @VER symbol version stripped.
25 bool symbols_are_regex
= false;
28 /// Fetches the abort frame location depending on the current platform.
31 /// The target's os type.
32 /// \param[in,out] location
33 /// The struct that will contain the abort module spec and symbol names.
35 /// \b true, if the platform is supported
36 /// \b false, otherwise.
37 bool GetAbortLocation(llvm::Triple::OSType os
, SymbolLocation
&location
) {
39 case llvm::Triple::Darwin
:
40 case llvm::Triple::MacOSX
:
41 location
.module_spec
= FileSpec("libsystem_kernel.dylib");
42 location
.symbols
.push_back(ConstString("__pthread_kill"));
44 case llvm::Triple::Linux
:
45 location
.module_spec
= FileSpec("libc.so.6");
46 location
.symbols
.push_back(ConstString("raise"));
47 location
.symbols
.push_back(ConstString("__GI_raise"));
48 location
.symbols
.push_back(ConstString("gsignal"));
49 location
.symbols
.push_back(ConstString("pthread_kill"));
50 location
.symbols_are_regex
= true;
53 Log
*log
= GetLog(LLDBLog::Unwind
);
54 LLDB_LOG(log
, "AssertFrameRecognizer::GetAbortLocation Unsupported OS");
61 /// Fetches the assert frame location depending on the current platform.
64 /// The target's os type.
65 /// \param[in,out] location
66 /// The struct that will contain the assert module spec and symbol names.
68 /// \b true, if the platform is supported
69 /// \b false, otherwise.
70 bool GetAssertLocation(llvm::Triple::OSType os
, SymbolLocation
&location
) {
72 case llvm::Triple::Darwin
:
73 case llvm::Triple::MacOSX
:
74 location
.module_spec
= FileSpec("libsystem_c.dylib");
75 location
.symbols
.push_back(ConstString("__assert_rtn"));
77 case llvm::Triple::Linux
:
78 location
.module_spec
= FileSpec("libc.so.6");
79 location
.symbols
.push_back(ConstString("__assert_fail"));
80 location
.symbols
.push_back(ConstString("__GI___assert_fail"));
83 Log
*log
= GetLog(LLDBLog::Unwind
);
84 LLDB_LOG(log
, "AssertFrameRecognizer::GetAssertLocation Unsupported OS");
91 void RegisterAssertFrameRecognizer(Process
*process
) {
92 Target
&target
= process
->GetTarget();
93 llvm::Triple::OSType os
= target
.GetArchitecture().GetTriple().getOS();
94 SymbolLocation location
;
96 if (!GetAbortLocation(os
, location
))
99 if (!location
.symbols_are_regex
) {
100 target
.GetFrameRecognizerManager().AddRecognizer(
101 std::make_shared
<AssertFrameRecognizer
>(),
102 location
.module_spec
.GetFilename(), location
.symbols
,
103 /*first_instruction_only*/ false);
106 std::string module_re
= "^";
107 for (char c
: location
.module_spec
.GetFilename().GetStringRef()) {
113 std::string symbol_re
= "^(";
114 for (auto it
= location
.symbols
.cbegin(); it
!= location
.symbols
.cend();
116 if (it
!= location
.symbols
.cbegin())
118 symbol_re
+= it
->GetStringRef();
120 // Strip the trailing @VER symbol version.
121 symbol_re
+= ")(@.*)?$";
122 target
.GetFrameRecognizerManager().AddRecognizer(
123 std::make_shared
<AssertFrameRecognizer
>(),
124 std::make_shared
<RegularExpression
>(std::move(module_re
)),
125 std::make_shared
<RegularExpression
>(std::move(symbol_re
)),
126 /*first_instruction_only*/ false);
129 } // namespace lldb_private
131 lldb::RecognizedStackFrameSP
132 AssertFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp
) {
133 ThreadSP thread_sp
= frame_sp
->GetThread();
134 ProcessSP process_sp
= thread_sp
->GetProcess();
135 Target
&target
= process_sp
->GetTarget();
136 llvm::Triple::OSType os
= target
.GetArchitecture().GetTriple().getOS();
137 SymbolLocation location
;
139 if (!GetAssertLocation(os
, location
))
140 return RecognizedStackFrameSP();
142 const uint32_t frames_to_fetch
= 6;
143 const uint32_t last_frame_index
= frames_to_fetch
- 1;
144 StackFrameSP prev_frame_sp
= nullptr;
146 // Fetch most relevant frame
147 for (uint32_t frame_index
= 0; frame_index
< frames_to_fetch
; frame_index
++) {
148 prev_frame_sp
= thread_sp
->GetStackFrameAtIndex(frame_index
);
150 if (!prev_frame_sp
) {
151 Log
*log
= GetLog(LLDBLog::Unwind
);
152 LLDB_LOG(log
, "Abort Recognizer: Hit unwinding bound ({1} frames)!",
157 SymbolContext sym_ctx
=
158 prev_frame_sp
->GetSymbolContext(eSymbolContextEverything
);
160 if (!sym_ctx
.module_sp
||
161 !sym_ctx
.module_sp
->GetFileSpec().FileEquals(location
.module_spec
))
164 ConstString func_name
= sym_ctx
.GetFunctionName();
166 if (llvm::is_contained(location
.symbols
, func_name
)) {
167 // We go a frame beyond the assert location because the most relevant
168 // frame for the user is the one in which the assert function was called.
169 // If the assert location is the last frame fetched, then it is set as
170 // the most relevant frame.
172 StackFrameSP most_relevant_frame_sp
= thread_sp
->GetStackFrameAtIndex(
173 std::min(frame_index
+ 1, last_frame_index
));
175 // Pass assert location to AbortRecognizedStackFrame to set as most
177 return lldb::RecognizedStackFrameSP(
178 new AssertRecognizedStackFrame(most_relevant_frame_sp
));
182 return RecognizedStackFrameSP();
185 AssertRecognizedStackFrame::AssertRecognizedStackFrame(
186 StackFrameSP most_relevant_frame_sp
)
187 : m_most_relevant_frame(most_relevant_frame_sp
) {
188 m_stop_desc
= "hit program assert";
191 lldb::StackFrameSP
AssertRecognizedStackFrame::GetMostRelevantFrame() {
192 return m_most_relevant_frame
;