1 #include "lldb/Core/Module.h"
2 #include "lldb/Symbol/Function.h"
3 #include "lldb/Symbol/SymbolContext.h"
4 #include "lldb/Target/Process.h"
5 #include "lldb/Target/StackFrameList.h"
6 #include "lldb/Target/Target.h"
7 #include "lldb/Target/Thread.h"
8 
9 #include "lldb/Utility/Log.h"
10 #include "lldb/Utility/Logging.h"
11 
12 #include "lldb/Target/AssertFrameRecognizer.h"
13 
14 using namespace llvm;
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 namespace lldb_private {
19 /// Fetches the abort frame location depending on the current platform.
20 ///
21 /// \param[in] process_sp
22 ///    The process that is currently aborting. This will give us information on
23 ///    the target and the platform.
24 /// \return
25 ///    If the platform is supported, returns an optional tuple containing
26 ///    the abort module as a \a FileSpec and the symbol name as a \a StringRef.
27 ///    Otherwise, returns \a llvm::None.
28 llvm::Optional<std::tuple<FileSpec, StringRef>>
29 GetAbortLocation(Process *process) {
30   Target &target = process->GetTarget();
31 
32   StringRef module_name;
33   StringRef symbol_name;
34 
35   switch (target.GetArchitecture().GetTriple().getOS()) {
36   case llvm::Triple::Darwin:
37   case llvm::Triple::MacOSX:
38     module_name = "libsystem_kernel.dylib";
39     symbol_name = "__pthread_kill";
40     break;
41   case llvm::Triple::Linux:
42     module_name = "libc.so.6";
43     symbol_name = "__GI_raise";
44     break;
45   default:
46     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
47     LLDB_LOG(log, "AssertFrameRecognizer::GetAbortLocation Unsupported OS");
48     return llvm::None;
49   }
50 
51   return std::make_tuple(FileSpec(module_name), symbol_name);
52 }
53 
54 /// Fetches the assert frame location depending on the current platform.
55 ///
56 /// \param[in] process_sp
57 ///    The process that is currently asserting. This will give us information on
58 ///    the target and the platform.
59 /// \return
60 ///    If the platform is supported, returns an optional tuple containing
61 ///    the asserting frame module as  a \a FileSpec and the symbol name as a \a
62 ///    StringRef.
63 ///    Otherwise, returns \a llvm::None.
64 llvm::Optional<std::tuple<FileSpec, StringRef>>
65 GetAssertLocation(Process *process) {
66   Target &target = process->GetTarget();
67 
68   StringRef module_name;
69   StringRef symbol_name;
70 
71   switch (target.GetArchitecture().GetTriple().getOS()) {
72   case llvm::Triple::Darwin:
73   case llvm::Triple::MacOSX:
74     module_name = "libsystem_c.dylib";
75     symbol_name = "__assert_rtn";
76     break;
77   case llvm::Triple::Linux:
78     module_name = "libc.so.6";
79     symbol_name = "__GI___assert_fail";
80     break;
81   default:
82     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
83     LLDB_LOG(log, "AssertFrameRecognizer::GetAbortLocation Unsupported OS");
84     return llvm::None;
85   }
86 
87   return std::make_tuple(FileSpec(module_name), symbol_name);
88 }
89 
90 void RegisterAssertFrameRecognizer(Process *process) {
91   static llvm::once_flag g_once_flag;
92   llvm::call_once(g_once_flag, [process]() {
93     auto abort_location = GetAbortLocation(process);
94 
95     if (!abort_location.hasValue())
96       return;
97 
98     FileSpec module_spec;
99     StringRef function_name;
100     std::tie(module_spec, function_name) = *abort_location;
101 
102     StackFrameRecognizerManager::AddRecognizer(
103         StackFrameRecognizerSP(new AssertFrameRecognizer()),
104         module_spec.GetFilename(), ConstString(function_name), false);
105   });
106 }
107 
108 } // namespace lldb_private
109 
110 AssertRecognizedStackFrame::AssertRecognizedStackFrame(
111     StackFrameSP most_relevant_frame_sp)
112     : m_most_relevant_frame(most_relevant_frame_sp) {}
113 
114 lldb::RecognizedStackFrameSP
115 AssertFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
116   ThreadSP thread_sp = frame_sp->GetThread();
117   ProcessSP process_sp = thread_sp->GetProcess();
118 
119   auto assert_location = GetAssertLocation(process_sp.get());
120 
121   if (!assert_location.hasValue())
122     return RecognizedStackFrameSP();
123 
124   FileSpec module_spec;
125   StringRef function_name;
126   std::tie(module_spec, function_name) = *assert_location;
127 
128   const uint32_t frames_to_fetch = 5;
129   const uint32_t last_frame_index = frames_to_fetch - 1;
130   StackFrameSP prev_frame_sp = nullptr;
131 
132   // Fetch most relevant frame
133   for (uint32_t frame_index = 0; frame_index < frames_to_fetch; frame_index++) {
134     prev_frame_sp = thread_sp->GetStackFrameAtIndex(frame_index);
135 
136     if (!prev_frame_sp) {
137       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
138       LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({1} frames)!",
139                frames_to_fetch);
140       break;
141     }
142 
143     SymbolContext sym_ctx =
144         prev_frame_sp->GetSymbolContext(eSymbolContextEverything);
145 
146     if (sym_ctx.module_sp->GetFileSpec().FileEquals(module_spec) &&
147         sym_ctx.GetFunctionName() == ConstString(function_name)) {
148 
149       // We go a frame beyond the assert location because the most relevant
150       // frame for the user is the one in which the assert function was called.
151       // If the assert location is the last frame fetched, then it is set as
152       // the most relevant frame.
153 
154       StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(
155           std::min(frame_index + 1, last_frame_index));
156 
157       // Pass assert location to AbortRecognizedStackFrame to set as most
158       // relevant frame.
159       return lldb::RecognizedStackFrameSP(
160           new AssertRecognizedStackFrame(most_relevant_frame_sp));
161     }
162   }
163 
164   return RecognizedStackFrameSP();
165 };
166 
167 lldb::StackFrameSP AssertRecognizedStackFrame::GetMostRelevantFrame() {
168   return m_most_relevant_frame;
169 }
170 
171 llvm::StringRef AssertRecognizedStackFrame::GetStopDescription() {
172   return "hit program assert";
173 }
174