LLDB mainline
AssertFrameRecognizer.cpp
Go to the documentation of this file.
2#include "lldb/Core/Module.h"
10
11using namespace llvm;
12using namespace lldb;
13using namespace lldb_private;
14
15namespace lldb_private {
16
17/// Stores a function module spec, symbol name and possibly an alternate symbol
18/// name.
21 std::vector<ConstString> symbols;
22
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;
26};
27
28/// Fetches the abort frame location depending on the current platform.
29///
30/// \param[in] os
31/// The target's os type.
32/// \param[in,out] location
33/// The struct that will contain the abort module spec and symbol names.
34/// \return
35/// \b true, if the platform is supported
36/// \b false, otherwise.
37bool GetAbortLocation(llvm::Triple::OSType os, SymbolLocation &location) {
38 switch (os) {
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"));
43 break;
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;
51 break;
52 default:
54 LLDB_LOG(log, "AssertFrameRecognizer::GetAbortLocation Unsupported OS");
55 return false;
56 }
57
58 return true;
59}
60
61/// Fetches the assert frame location depending on the current platform.
62///
63/// \param[in] os
64/// The target's os type.
65/// \param[in,out] location
66/// The struct that will contain the assert module spec and symbol names.
67/// \return
68/// \b true, if the platform is supported
69/// \b false, otherwise.
70bool GetAssertLocation(llvm::Triple::OSType os, SymbolLocation &location) {
71 switch (os) {
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"));
76 break;
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"));
81 break;
82 default:
84 LLDB_LOG(log, "AssertFrameRecognizer::GetAssertLocation Unsupported OS");
85 return false;
86 }
87
88 return true;
89}
90
92 Target &target = process->GetTarget();
93 llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS();
94 SymbolLocation location;
95
96 if (!GetAbortLocation(os, location))
97 return;
98
99 if (!location.symbols_are_regex) {
101 std::make_shared<AssertFrameRecognizer>(),
102 location.module_spec.GetFilename(), location.symbols,
103 /*first_instruction_only*/ false);
104 return;
105 }
106 std::string module_re = "^";
107 for (char c : location.module_spec.GetFilename().GetStringRef()) {
108 if (c == '.')
109 module_re += '\\';
110 module_re += c;
111 }
112 module_re += '$';
113 std::string symbol_re = "^(";
114 for (auto it = location.symbols.cbegin(); it != location.symbols.cend();
115 ++it) {
116 if (it != location.symbols.cbegin())
117 symbol_re += '|';
118 symbol_re += it->GetStringRef();
119 }
120 // Strip the trailing @VER symbol version.
121 symbol_re += ")(@.*)?$";
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);
127}
128
129} // namespace lldb_private
130
131lldb::RecognizedStackFrameSP
132AssertFrameRecognizer::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;
138
139 if (!GetAssertLocation(os, location))
140 return RecognizedStackFrameSP();
141
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;
145
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);
149
150 if (!prev_frame_sp) {
151 Log *log = GetLog(LLDBLog::Unwind);
152 LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({1} frames)!",
153 frames_to_fetch);
154 break;
155 }
156
157 SymbolContext sym_ctx =
158 prev_frame_sp->GetSymbolContext(eSymbolContextEverything);
159
160 if (!sym_ctx.module_sp ||
161 !sym_ctx.module_sp->GetFileSpec().FileEquals(location.module_spec))
162 continue;
163
164 ConstString func_name = sym_ctx.GetFunctionName();
165
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.
171
172 StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(
173 std::min(frame_index + 1, last_frame_index));
174
175 // Pass assert location to AbortRecognizedStackFrame to set as most
176 // relevant frame.
177 return lldb::RecognizedStackFrameSP(
178 new AssertRecognizedStackFrame(most_relevant_frame_sp));
179 }
180 }
181
182 return RecognizedStackFrameSP();
183}
184
186 StackFrameSP most_relevant_frame_sp)
187 : m_most_relevant_frame(most_relevant_frame_sp) {
188 m_stop_desc = "hit program assert";
189}
190
193}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:463
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:39
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
A file utility class.
Definition: FileSpec.h:56
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
A plug-in interface definition class for debugging a process.
Definition: Process.h:342
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1219
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, bool first_instruction_only=true)
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
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:1427
const ArchSpec & GetArchitecture() const
Definition: Target.h:988
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:309
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.
Definition: SBAddress.h:15
Definition: Debugger.h:51
Stores a function module spec, symbol name and possibly an alternate symbol name.
std::vector< ConstString > symbols