Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 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 { |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 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 | FileSpec module_spec; |
| 33 | StringRef symbol_name; |
| 34 | |
| 35 | switch (target.GetArchitecture().GetTriple().getOS()) { |
| 36 | case llvm::Triple::Darwin: |
| 37 | case llvm::Triple::MacOSX: |
| 38 | module_spec = FileSpec("libsystem_kernel.dylib"); |
| 39 | symbol_name = "__pthread_kill"; |
| 40 | break; |
| 41 | case llvm::Triple::Linux: |
| 42 | module_spec = FileSpec("libc.so.6"); |
Jan Kratochvil | cf1046c | 2020-02-07 22:23:09 +0100 | [diff] [blame] | 43 | symbol_name = "raise"; |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 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(module_spec, 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 | FileSpec module_spec; |
| 69 | StringRef symbol_name; |
| 70 | |
| 71 | switch (target.GetArchitecture().GetTriple().getOS()) { |
| 72 | case llvm::Triple::Darwin: |
| 73 | case llvm::Triple::MacOSX: |
| 74 | module_spec = FileSpec("libsystem_c.dylib"); |
| 75 | symbol_name = "__assert_rtn"; |
| 76 | break; |
| 77 | case llvm::Triple::Linux: |
| 78 | module_spec = FileSpec("libc.so.6"); |
Jan Kratochvil | cf1046c | 2020-02-07 22:23:09 +0100 | [diff] [blame] | 79 | symbol_name = "__assert_fail"; |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 80 | break; |
| 81 | default: |
| 82 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND)); |
| 83 | LLDB_LOG(log, "AssertFrameRecognizer::GetAssertLocation Unsupported OS"); |
| 84 | return llvm::None; |
| 85 | } |
| 86 | |
| 87 | return std::make_tuple(module_spec, 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 | lldb::RecognizedStackFrameSP |
| 111 | AssertFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) { |
| 112 | ThreadSP thread_sp = frame_sp->GetThread(); |
| 113 | ProcessSP process_sp = thread_sp->GetProcess(); |
| 114 | |
| 115 | auto assert_location = GetAssertLocation(process_sp.get()); |
| 116 | |
| 117 | if (!assert_location.hasValue()) |
| 118 | return RecognizedStackFrameSP(); |
| 119 | |
| 120 | FileSpec module_spec; |
| 121 | StringRef function_name; |
| 122 | std::tie(module_spec, function_name) = *assert_location; |
| 123 | |
| 124 | const uint32_t frames_to_fetch = 5; |
| 125 | const uint32_t last_frame_index = frames_to_fetch - 1; |
| 126 | StackFrameSP prev_frame_sp = nullptr; |
| 127 | |
| 128 | // Fetch most relevant frame |
| 129 | for (uint32_t frame_index = 0; frame_index < frames_to_fetch; frame_index++) { |
| 130 | prev_frame_sp = thread_sp->GetStackFrameAtIndex(frame_index); |
| 131 | |
| 132 | if (!prev_frame_sp) { |
| 133 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND)); |
| 134 | LLDB_LOG(log, "Abort Recognizer: Hit unwinding bound ({1} frames)!", |
| 135 | frames_to_fetch); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | SymbolContext sym_ctx = |
| 140 | prev_frame_sp->GetSymbolContext(eSymbolContextEverything); |
| 141 | |
| 142 | if (sym_ctx.module_sp->GetFileSpec().FileEquals(module_spec) && |
| 143 | sym_ctx.GetFunctionName() == ConstString(function_name)) { |
| 144 | |
| 145 | // We go a frame beyond the assert location because the most relevant |
| 146 | // frame for the user is the one in which the assert function was called. |
| 147 | // If the assert location is the last frame fetched, then it is set as |
| 148 | // the most relevant frame. |
| 149 | |
| 150 | StackFrameSP most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex( |
| 151 | std::min(frame_index + 1, last_frame_index)); |
| 152 | |
| 153 | // Pass assert location to AbortRecognizedStackFrame to set as most |
| 154 | // relevant frame. |
| 155 | return lldb::RecognizedStackFrameSP( |
| 156 | new AssertRecognizedStackFrame(most_relevant_frame_sp)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return RecognizedStackFrameSP(); |
Med Ismail Bennani | 17d0091 | 2020-02-06 19:46:19 +0100 | [diff] [blame] | 161 | } |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 162 | |
| 163 | AssertRecognizedStackFrame::AssertRecognizedStackFrame( |
| 164 | StackFrameSP most_relevant_frame_sp) |
| 165 | : m_most_relevant_frame(most_relevant_frame_sp) { |
| 166 | m_stop_desc = "hit program assert"; |
| 167 | } |
| 168 | |
| 169 | lldb::StackFrameSP AssertRecognizedStackFrame::GetMostRelevantFrame() { |
| 170 | return m_most_relevant_frame; |
| 171 | } |