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 
20 /// Stores a function module spec, symbol name and possibly an alternate symbol
21 /// name.
22 struct SymbolLocation {
23   FileSpec module_spec;
24   ConstString symbol_name;
25   ConstString alternate_symbol_name;
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.
37 bool 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.symbol_name.SetString("__pthread_kill");
43     break;
44   case llvm::Triple::Linux:
45     location.module_spec = FileSpec("libc.so.6");
46     location.symbol_name.SetString("raise");
47     location.alternate_symbol_name.SetString("__GI_raise");
48     break;
49   default:
50     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
51     LLDB_LOG(log, "AssertFrameRecognizer::GetAbortLocation Unsupported OS");
52     return false;
53   }
54 
55   return true;
56 }
57 
58 /// Fetches the assert frame location depending on the current platform.
59 ///
60 /// \param[in] os
61 ///    The target's os type.
62 /// \param[in,out] location
63 ///    The struct that will contain the assert module spec and symbol names.
64 /// \return
65 ///    \b true, if the platform is supported
66 ///    \b false, otherwise.
67 bool GetAssertLocation(llvm::Triple::OSType os, SymbolLocation &location) {
68   switch (os) {
69   case llvm::Triple::Darwin:
70   case llvm::Triple::MacOSX:
71     location.module_spec = FileSpec("libsystem_c.dylib");
72     location.symbol_name.SetString("__assert_rtn");
73     break;
74   case llvm::Triple::Linux:
75     location.module_spec = FileSpec("libc.so.6");
76     location.symbol_name.SetString("__assert_fail");
77     location.alternate_symbol_name.SetString("__GI___assert_fail");
78     break;
79   default:
80     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
81     LLDB_LOG(log, "AssertFrameRecognizer::GetAssertLocation Unsupported OS");
82     return false;
83   }
84 
85   return true;
86 }
87 
88 void RegisterAssertFrameRecognizer(Process *process) {
89   static llvm::once_flag g_once_flag;
90   llvm::call_once(g_once_flag, [process]() {
91     Target &target = process->GetTarget();
92     llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS();
93     SymbolLocation location;
94 
95     if (!GetAbortLocation(os, location))
96       return;
97 
98     StackFrameRecognizerManager::AddRecognizer(
99         StackFrameRecognizerSP(new AssertFrameRecognizer()),
100         location.module_spec.GetFilename(), ConstString(location.symbol_name),
101         ConstString(location.alternate_symbol_name),
102         /*first_instruction_only*/ false);
103   });
104 }
105 
106 } // namespace lldb_private
107 
108 lldb::RecognizedStackFrameSP
109 AssertFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
110   ThreadSP thread_sp = frame_sp->GetThread();
111   ProcessSP process_sp = thread_sp->GetProcess();
112   Target &target = process_sp->GetTarget();
113   llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS();
114   SymbolLocation location;
115 
116   if (!GetAssertLocation(os, location))
117     return RecognizedStackFrameSP();
118 
119   const uint32_t frames_to_fetch = 5;
120   const uint32_t last_frame_index = frames_to_fetch - 1;
121   StackFrameSP prev_frame_sp = nullptr;
122 
123   // Fetch most relevant frame
124   for (uint32_t frame_index = 0; frame_index < frames_to_fetch; frame_index++) {
125     prev_frame_sp = thread_sp->GetStackFrameAtIndex(frame_index);
126 
127     if (!prev_frame_sp) {
128       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
129       LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({1} frames)!",
130                frames_to_fetch);
131       break;
132     }
133 
134     SymbolContext sym_ctx =
135         prev_frame_sp->GetSymbolContext(eSymbolContextEverything);
136 
137     if (!sym_ctx.module_sp->GetFileSpec().FileEquals(location.module_spec))
138       continue;
139 
140     ConstString func_name = sym_ctx.GetFunctionName();
141 
142     if (func_name == location.symbol_name ||
143         (!location.alternate_symbol_name.IsEmpty() &&
144          func_name == location.alternate_symbol_name)) {
145 
146       // We go a frame beyond the assert location because the most relevant
147       // frame for the user is the one in which the assert function was called.
148       // If the assert location is the last frame fetched, then it is set as
149       // the most relevant frame.
150 
151       StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(
152           std::min(frame_index + 1, last_frame_index));
153 
154       // Pass assert location to AbortRecognizedStackFrame to set as most
155       // relevant frame.
156       return lldb::RecognizedStackFrameSP(
157           new AssertRecognizedStackFrame(most_relevant_frame_sp));
158     }
159   }
160 
161   return RecognizedStackFrameSP();
162 }
163 
164 AssertRecognizedStackFrame::AssertRecognizedStackFrame(
165     StackFrameSP most_relevant_frame_sp)
166     : m_most_relevant_frame(most_relevant_frame_sp) {
167   m_stop_desc = "hit program assert";
168 }
169 
170 lldb::StackFrameSP AssertRecognizedStackFrame::GetMostRelevantFrame() {
171   return m_most_relevant_frame;
172 }
173