LLDB mainline
AssertFrameRecognizer.cpp
Go to the documentation of this file.
2#include "lldb/Core/Module.h"
11
12using namespace llvm;
13using namespace lldb;
14using namespace lldb_private;
15
16namespace lldb_private {
17/// Fetches the abort frame location depending on the current platform.
18///
19/// \param[in] os
20/// The target's os type.
21/// \param[in,out] location
22/// The struct that will contain the abort module spec and symbol names.
23/// \return
24/// \b true, if the platform is supported
25/// \b false, otherwise.
26bool GetAbortLocation(llvm::Triple::OSType os, SymbolLocation &location) {
27 switch (os) {
28 case llvm::Triple::Darwin:
29 case llvm::Triple::MacOSX:
30 case llvm::Triple::IOS:
31 case llvm::Triple::TvOS:
32 case llvm::Triple::WatchOS:
33 case llvm::Triple::BridgeOS:
34 case llvm::Triple::DriverKit:
35 case llvm::Triple::XROS:
36 location.module_spec = FileSpec("libsystem_kernel.dylib");
37 location.symbols.push_back(ConstString("__pthread_kill"));
38 break;
39 case llvm::Triple::Linux:
40 location.module_spec = FileSpec("libc.so.6");
41 location.symbols.push_back(ConstString("raise"));
42 location.symbols.push_back(ConstString("__GI_raise"));
43 location.symbols.push_back(ConstString("gsignal"));
44 location.symbols.push_back(ConstString("pthread_kill"));
45 location.symbols_are_regex = true;
46 break;
47 default:
49 LLDB_LOG(log, "AssertFrameRecognizer::GetAbortLocation Unsupported OS");
50 return false;
51 }
52
53 return true;
54}
55
56/// Fetches the assert frame location depending on the current platform.
57///
58/// \param[in] os
59/// The target's os type.
60/// \param[in,out] location
61/// The struct that will contain the assert module spec and symbol names.
62/// \return
63/// \b true, if the platform is supported
64/// \b false, otherwise.
65bool GetAssertLocation(llvm::Triple::OSType os, SymbolLocation &location) {
66 switch (os) {
67 case llvm::Triple::Darwin:
68 case llvm::Triple::MacOSX:
69 case llvm::Triple::IOS:
70 case llvm::Triple::TvOS:
71 case llvm::Triple::WatchOS:
72 case llvm::Triple::BridgeOS:
73 case llvm::Triple::DriverKit:
74 case llvm::Triple::XROS:
75 location.module_spec = FileSpec("libsystem_c.dylib");
76 location.symbols.push_back(ConstString("__assert_rtn"));
77 break;
78 case llvm::Triple::Linux:
79 location.module_spec = FileSpec("libc.so.6");
80 location.symbols.push_back(ConstString("__assert_fail"));
81 location.symbols.push_back(ConstString("__GI___assert_fail"));
82 break;
83 default:
85 LLDB_LOG(log, "AssertFrameRecognizer::GetAssertLocation Unsupported OS");
86 return false;
87 }
88
89 return true;
90}
91
93 Target &target = process->GetTarget();
94 llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS();
95 SymbolLocation location;
96
97 if (!GetAbortLocation(os, location))
98 return;
99
100 if (!location.symbols_are_regex) {
102 std::make_shared<AssertFrameRecognizer>(),
103 location.module_spec.GetFilename(), location.symbols,
105 /*first_instruction_only*/ false);
106 return;
107 }
108 std::string module_re = "^";
109 for (char c : location.module_spec.GetFilename().GetStringRef()) {
110 if (c == '.')
111 module_re += '\\';
112 module_re += c;
113 }
114 module_re += '$';
115 std::string symbol_re = "^(";
116 for (auto it = location.symbols.cbegin(); it != location.symbols.cend();
117 ++it) {
118 if (it != location.symbols.cbegin())
119 symbol_re += '|';
120 symbol_re += it->GetStringRef();
121 }
122 // Strip the trailing @VER symbol version.
123 symbol_re += ")(@.*)?$";
125 std::make_shared<AssertFrameRecognizer>(),
126 std::make_shared<RegularExpression>(std::move(module_re)),
127 std::make_shared<RegularExpression>(std::move(symbol_re)),
129 /*first_instruction_only*/ false);
130}
131
132} // namespace lldb_private
133
136 ThreadSP thread_sp = frame_sp->GetThread();
137 ProcessSP process_sp = thread_sp->GetProcess();
138 Target &target = process_sp->GetTarget();
139 llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS();
140 SymbolLocation location;
141
142 if (!GetAssertLocation(os, location))
143 return RecognizedStackFrameSP();
144
145 const uint32_t frames_to_fetch = 6;
146 const uint32_t last_frame_index = frames_to_fetch - 1;
147 StackFrameSP prev_frame_sp = nullptr;
148
149 // Fetch most relevant frame
150 for (uint32_t frame_index = 0; frame_index < frames_to_fetch; frame_index++) {
151 prev_frame_sp = thread_sp->GetStackFrameAtIndex(frame_index);
152
153 if (!prev_frame_sp) {
154 Log *log = GetLog(LLDBLog::Unwind);
155 LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({} frames)!",
156 frames_to_fetch);
157 break;
158 }
159
160 SymbolContext sym_ctx =
161 prev_frame_sp->GetSymbolContext(eSymbolContextEverything);
162
163 if (!sym_ctx.module_sp ||
164 !sym_ctx.module_sp->GetFileSpec().FileEquals(location.module_spec))
165 continue;
166
167 ConstString func_name = sym_ctx.GetFunctionName();
168
169 if (llvm::is_contained(location.symbols, func_name)) {
170 // We go a frame beyond the assert location because the most relevant
171 // frame for the user is the one in which the assert function was called.
172 // If the assert location is the last frame fetched, then it is set as
173 // the most relevant frame.
174
175 StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(
176 std::min(frame_index + 1, last_frame_index));
177
178 // Pass assert location to AbortRecognizedStackFrame to set as most
179 // relevant frame.
181 new AssertRecognizedStackFrame(most_relevant_frame_sp));
182 }
183 }
184
185 return RecognizedStackFrameSP();
186}
187
189 StackFrameSP most_relevant_frame_sp)
190 : m_most_relevant_frame(most_relevant_frame_sp) {
191 m_stop_desc = "hit program assert";
192}
193
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:460
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame_sp) override
Holds the stack frame where the assert is called from.
AssertRecognizedStackFrame(lldb::StackFrameSP most_relevant_frame_sp)
lldb::StackFrameSP GetMostRelevantFrame() override
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
A file utility class.
Definition FileSpec.h:57
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:250
A plug-in interface definition class for debugging a process.
Definition Process.h:356
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1254
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, Mangled::NamePreference symbol_mangling, bool first_instruction_only=true)
Add a new recognizer that triggers on a given symbol name.
Defines a symbol context baton that can be handed other debug core functions.
ConstString GetFunctionName(Mangled::NamePreference preference=Mangled::ePreferDemangled) const
Find a name of the innermost function for the symbol context.
lldb::ModuleSP module_sp
The Module for a given query.
StackFrameRecognizerManager & GetFrameRecognizerManager()
Definition Target.h:1996
const ArchSpec & GetArchitecture() const
Definition Target.h:1282
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
void RegisterAssertFrameRecognizer(Process *process)
Registers the assert stack frame recognizer.
bool GetAbortLocation(llvm::Triple::OSType os, SymbolLocation &location)
Fetches the abort frame location depending on the current platform.
bool GetAssertLocation(llvm::Triple::OSType os, SymbolLocation &location)
Fetches the assert frame location depending on the current platform.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::Process > ProcessSP
Stores a function module spec, symbol name and possibly an alternate symbol name.
std::vector< ConstString > symbols