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 Target &target = process->GetTarget(); 90 llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS(); 91 SymbolLocation location; 92 93 if (!GetAbortLocation(os, location)) 94 return; 95 96 target.GetFrameRecognizerManager().AddRecognizer( 97 StackFrameRecognizerSP(new AssertFrameRecognizer()), 98 location.module_spec.GetFilename(), location.symbols, 99 /*first_instruction_only*/ false); 100 } 101 102 } // namespace lldb_private 103 104 lldb::RecognizedStackFrameSP 105 AssertFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) { 106 ThreadSP thread_sp = frame_sp->GetThread(); 107 ProcessSP process_sp = thread_sp->GetProcess(); 108 Target &target = process_sp->GetTarget(); 109 llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS(); 110 SymbolLocation location; 111 112 if (!GetAssertLocation(os, location)) 113 return RecognizedStackFrameSP(); 114 115 const uint32_t frames_to_fetch = 5; 116 const uint32_t last_frame_index = frames_to_fetch - 1; 117 StackFrameSP prev_frame_sp = nullptr; 118 119 // Fetch most relevant frame 120 for (uint32_t frame_index = 0; frame_index < frames_to_fetch; frame_index++) { 121 prev_frame_sp = thread_sp->GetStackFrameAtIndex(frame_index); 122 123 if (!prev_frame_sp) { 124 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND)); 125 LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({1} frames)!", 126 frames_to_fetch); 127 break; 128 } 129 130 SymbolContext sym_ctx = 131 prev_frame_sp->GetSymbolContext(eSymbolContextEverything); 132 133 if (!sym_ctx.module_sp || 134 !sym_ctx.module_sp->GetFileSpec().FileEquals(location.module_spec)) 135 continue; 136 137 ConstString func_name = sym_ctx.GetFunctionName(); 138 139 if (llvm::is_contained(location.symbols, func_name)) { 140 // We go a frame beyond the assert location because the most relevant 141 // frame for the user is the one in which the assert function was called. 142 // If the assert location is the last frame fetched, then it is set as 143 // the most relevant frame. 144 145 StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex( 146 std::min(frame_index + 1, last_frame_index)); 147 148 // Pass assert location to AbortRecognizedStackFrame to set as most 149 // relevant frame. 150 return lldb::RecognizedStackFrameSP( 151 new AssertRecognizedStackFrame(most_relevant_frame_sp)); 152 } 153 } 154 155 return RecognizedStackFrameSP(); 156 } 157 158 AssertRecognizedStackFrame::AssertRecognizedStackFrame( 159 StackFrameSP most_relevant_frame_sp) 160 : m_most_relevant_frame(most_relevant_frame_sp) { 161 m_stop_desc = "hit program assert"; 162 } 163 164 lldb::StackFrameSP AssertRecognizedStackFrame::GetMostRelevantFrame() { 165 return m_most_relevant_frame; 166 } 167