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 std::vector<ConstString> symbols; 25 }; 26 27 /// Fetches the abort frame location depending on the current platform. 28 /// 29 /// \param[in] os 30 /// The target's os type. 31 /// \param[in,out] location 32 /// The struct that will contain the abort module spec and symbol names. 33 /// \return 34 /// \b true, if the platform is supported 35 /// \b false, otherwise. 36 bool GetAbortLocation(llvm::Triple::OSType os, SymbolLocation &location) { 37 switch (os) { 38 case llvm::Triple::Darwin: 39 case llvm::Triple::MacOSX: 40 location.module_spec = FileSpec("libsystem_kernel.dylib"); 41 location.symbols.push_back(ConstString("__pthread_kill")); 42 break; 43 case llvm::Triple::Linux: 44 location.module_spec = FileSpec("libc.so.6"); 45 location.symbols.push_back(ConstString("raise")); 46 location.symbols.push_back(ConstString("__GI_raise")); 47 location.symbols.push_back(ConstString("gsignal")); 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.symbols.push_back(ConstString("__assert_rtn")); 73 break; 74 case llvm::Triple::Linux: 75 location.module_spec = FileSpec("libc.so.6"); 76 location.symbols.push_back(ConstString("__assert_fail")); 77 location.symbols.push_back(ConstString("__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(), location.symbols, 101 /*first_instruction_only*/ false); 102 }); 103 } 104 105 } // namespace lldb_private 106 107 lldb::RecognizedStackFrameSP 108 AssertFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) { 109 ThreadSP thread_sp = frame_sp->GetThread(); 110 ProcessSP process_sp = thread_sp->GetProcess(); 111 Target &target = process_sp->GetTarget(); 112 llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS(); 113 SymbolLocation location; 114 115 if (!GetAssertLocation(os, location)) 116 return RecognizedStackFrameSP(); 117 118 const uint32_t frames_to_fetch = 5; 119 const uint32_t last_frame_index = frames_to_fetch - 1; 120 StackFrameSP prev_frame_sp = nullptr; 121 122 // Fetch most relevant frame 123 for (uint32_t frame_index = 0; frame_index < frames_to_fetch; frame_index++) { 124 prev_frame_sp = thread_sp->GetStackFrameAtIndex(frame_index); 125 126 if (!prev_frame_sp) { 127 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND)); 128 LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({1} frames)!", 129 frames_to_fetch); 130 break; 131 } 132 133 SymbolContext sym_ctx = 134 prev_frame_sp->GetSymbolContext(eSymbolContextEverything); 135 136 if (!sym_ctx.module_sp->GetFileSpec().FileEquals(location.module_spec)) 137 continue; 138 139 ConstString func_name = sym_ctx.GetFunctionName(); 140 141 if (llvm::is_contained(location.symbols, func_name)) { 142 // We go a frame beyond the assert location because the most relevant 143 // frame for the user is the one in which the assert function was called. 144 // If the assert location is the last frame fetched, then it is set as 145 // the most relevant frame. 146 147 StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex( 148 std::min(frame_index + 1, last_frame_index)); 149 150 // Pass assert location to AbortRecognizedStackFrame to set as most 151 // relevant frame. 152 return lldb::RecognizedStackFrameSP( 153 new AssertRecognizedStackFrame(most_relevant_frame_sp)); 154 } 155 } 156 157 return RecognizedStackFrameSP(); 158 } 159 160 AssertRecognizedStackFrame::AssertRecognizedStackFrame( 161 StackFrameSP most_relevant_frame_sp) 162 : m_most_relevant_frame(most_relevant_frame_sp) { 163 m_stop_desc = "hit program assert"; 164 } 165 166 lldb::StackFrameSP AssertRecognizedStackFrame::GetMostRelevantFrame() { 167 return m_most_relevant_frame; 168 } 169