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] triple
20/// The target's triple.
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(const llvm::Triple &triple, SymbolLocation &location) {
27 switch (triple.getOS()) {
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 case llvm::Triple::Win32:
48 if (!triple.isWindowsMSVCEnvironment()) {
50 LLDB_LOG(log, "AssertFrameRecognizer::GetAbortLocation Unsupported "
51 "Windows environment");
52 return false;
53 }
54 // Windows MSVC CRT can be statically or dynamically linked. With dynamic
55 // linking, abort lives in ucrtbase.dll. With static linking it's embedded
56 // in the executable.
57 // Match on the symbol only to handle both.
58 location.symbols.push_back(ConstString("abort"));
59 break;
60 default:
62 LLDB_LOG(log, "AssertFrameRecognizer::GetAbortLocation Unsupported OS");
63 return false;
64 }
65
66 return true;
67}
68
69/// Fetches the assert frame location depending on the current platform.
70///
71/// \param[in] triple
72/// The target's triple.
73/// \param[in,out] location
74/// The struct that will contain the assert module spec and symbol names.
75/// \return
76/// \b true, if the platform is supported
77/// \b false, otherwise.
78bool GetAssertLocation(const llvm::Triple &triple, SymbolLocation &location) {
79 switch (triple.getOS()) {
80 case llvm::Triple::Darwin:
81 case llvm::Triple::MacOSX:
82 case llvm::Triple::IOS:
83 case llvm::Triple::TvOS:
84 case llvm::Triple::WatchOS:
85 case llvm::Triple::BridgeOS:
86 case llvm::Triple::DriverKit:
87 case llvm::Triple::XROS:
88 location.module_spec = FileSpec("libsystem_c.dylib");
89 location.symbols.push_back(ConstString("__assert_rtn"));
90 break;
91 case llvm::Triple::Linux:
92 location.module_spec = FileSpec("libc.so.6");
93 location.symbols.push_back(ConstString("__assert_fail"));
94 location.symbols.push_back(ConstString("__GI___assert_fail"));
95 break;
96 case llvm::Triple::Win32:
97 if (!triple.isWindowsMSVCEnvironment()) {
99 LLDB_LOG(log, "AssertFrameRecognizer::GetAssertLocation Unsupported "
100 "Windows environment");
101 return false;
102 }
103 // See comment in GetAbortLocation for why we skip the module check.
104 location.symbols.push_back(ConstString("_wassert"));
105 break;
106 default:
107 Log *log = GetLog(LLDBLog::Unwind);
108 LLDB_LOG(log, "AssertFrameRecognizer::GetAssertLocation Unsupported OS");
109 return false;
110 }
111
112 return true;
113}
114
116 Target &target = process->GetTarget();
117 const llvm::Triple &triple = target.GetArchitecture().GetTriple();
118 SymbolLocation location;
119
120 if (!GetAbortLocation(triple, location))
121 return;
122
123 if (!location.symbols_are_regex) {
125 std::make_shared<AssertFrameRecognizer>(),
126 location.module_spec.GetFilename().str(), location.symbols,
128 /*first_instruction_only*/ false);
129 return;
130 }
131 std::string module_re = "^";
132 for (char c : location.module_spec.GetFilename()) {
133 if (c == '.')
134 module_re += '\\';
135 module_re += c;
136 }
137 module_re += '$';
138 std::string symbol_re = "^(";
139 for (auto it = location.symbols.cbegin(); it != location.symbols.cend();
140 ++it) {
141 if (it != location.symbols.cbegin())
142 symbol_re += '|';
143 symbol_re += it->GetStringRef();
144 }
145 // Strip the trailing @VER symbol version.
146 symbol_re += ")(@.*)?$";
148 std::make_shared<AssertFrameRecognizer>(),
149 std::make_shared<RegularExpression>(std::move(module_re)),
150 std::make_shared<RegularExpression>(std::move(symbol_re)),
152 /*first_instruction_only*/ false);
153}
154
155} // namespace lldb_private
156
159 ThreadSP thread_sp = frame_sp->GetThread();
160 ProcessSP process_sp = thread_sp->GetProcess();
161 Target &target = process_sp->GetTarget();
162 const llvm::Triple &triple = target.GetArchitecture().GetTriple();
163 SymbolLocation location;
164
165 if (!GetAssertLocation(triple, location))
166 return RecognizedStackFrameSP();
167
168 const uint32_t frames_to_fetch = 6;
169 const uint32_t last_frame_index = frames_to_fetch - 1;
170 StackFrameSP prev_frame_sp = nullptr;
171
172 // Fetch most relevant frame
173 for (uint32_t frame_index = 0; frame_index < frames_to_fetch; frame_index++) {
174 prev_frame_sp = thread_sp->GetStackFrameAtIndex(frame_index);
175
176 if (!prev_frame_sp) {
177 Log *log = GetLog(LLDBLog::Unwind);
178 LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({} frames)!",
179 frames_to_fetch);
180 break;
181 }
182
183 SymbolContext sym_ctx =
184 prev_frame_sp->GetSymbolContext(eSymbolContextEverything);
185
186 if (!sym_ctx.module_sp)
187 continue;
188 // On Windows the abort/assert symbols may live in the executable (static
189 // CRT) or a CRT DLL, so skip the module check there.
190 if (!triple.isOSWindows() &&
191 !sym_ctx.module_sp->GetFileSpec().FileEquals(location.module_spec))
192 continue;
193
194 ConstString func_name = sym_ctx.GetFunctionName();
195
196 if (llvm::is_contained(location.symbols, func_name)) {
197 // We go a frame beyond the assert location because the most relevant
198 // frame for the user is the one in which the assert function was called.
199 // If the assert location is the last frame fetched, then it is set as
200 // the most relevant frame.
201
202 StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(
203 std::min(frame_index + 1, last_frame_index));
204
205 // Pass assert location to AbortRecognizedStackFrame to set as most
206 // relevant frame.
208 new AssertRecognizedStackFrame(most_relevant_frame_sp));
209 }
210 }
211
212 return RecognizedStackFrameSP();
213}
214
216 StackFrameSP most_relevant_frame_sp)
217 : m_most_relevant_frame(most_relevant_frame_sp) {
218 m_stop_desc = "hit program assert";
219}
220
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:545
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
A file utility class.
Definition FileSpec.h:56
llvm::StringRef GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:248
A plug-in interface definition class for debugging a process.
Definition Process.h:368
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1267
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, std::string module, std::vector< 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:2022
const ArchSpec & GetArchitecture() const
Definition Target.h:1315
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:338
bool GetAbortLocation(const llvm::Triple &triple, SymbolLocation &location)
Fetches the abort frame location depending on the current platform.
bool GetAssertLocation(const llvm::Triple &triple, SymbolLocation &location)
Fetches the assert frame location depending on the current platform.
void RegisterAssertFrameRecognizer(Process *process)
Registers the assert stack frame recognizer.
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