1 //===-- ProcessMinidump.cpp -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Project includes 11 #include "ProcessMinidump.h" 12 #include "ThreadMinidump.h" 13 14 // Other libraries and framework includes 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/ModuleSpec.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/Section.h" 19 #include "lldb/Core/State.h" 20 #include "lldb/Target/DynamicLoader.h" 21 #include "lldb/Target/MemoryRegionInfo.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Target/UnixSignals.h" 24 #include "lldb/Utility/DataBufferLLVM.h" 25 #include "lldb/Utility/LLDBAssert.h" 26 #include "lldb/Utility/Log.h" 27 28 #include "llvm/Support/MemoryBuffer.h" 29 #include "llvm/Support/Threading.h" 30 31 // C includes 32 // C++ includes 33 34 using namespace lldb_private; 35 using namespace minidump; 36 37 ConstString ProcessMinidump::GetPluginNameStatic() { 38 static ConstString g_name("minidump"); 39 return g_name; 40 } 41 42 const char *ProcessMinidump::GetPluginDescriptionStatic() { 43 return "Minidump plug-in."; 44 } 45 46 lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp, 47 lldb::ListenerSP listener_sp, 48 const FileSpec *crash_file) { 49 if (!crash_file) 50 return nullptr; 51 52 lldb::ProcessSP process_sp; 53 // Read enough data for the Minidump header 54 constexpr size_t header_size = sizeof(MinidumpHeader); 55 auto DataPtr = 56 DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0); 57 if (!DataPtr) 58 return nullptr; 59 60 assert(DataPtr->GetByteSize() == header_size); 61 62 // first, only try to parse the header, beacuse we need to be fast 63 llvm::ArrayRef<uint8_t> HeaderBytes = DataPtr->GetData(); 64 const MinidumpHeader *header = MinidumpHeader::Parse(HeaderBytes); 65 if (header == nullptr) 66 return nullptr; 67 68 auto AllData = DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), -1, 0); 69 if (!AllData) 70 return nullptr; 71 72 auto minidump_parser = MinidumpParser::Create(AllData); 73 // check if the parser object is valid 74 if (!minidump_parser) 75 return nullptr; 76 77 return std::make_shared<ProcessMinidump>(target_sp, listener_sp, *crash_file, 78 minidump_parser.getValue()); 79 } 80 81 bool ProcessMinidump::CanDebug(lldb::TargetSP target_sp, 82 bool plugin_specified_by_name) { 83 return true; 84 } 85 86 ProcessMinidump::ProcessMinidump(lldb::TargetSP target_sp, 87 lldb::ListenerSP listener_sp, 88 const FileSpec &core_file, 89 MinidumpParser minidump_parser) 90 : Process(target_sp, listener_sp), m_minidump_parser(minidump_parser), 91 m_core_file(core_file), m_is_wow64(false) {} 92 93 ProcessMinidump::~ProcessMinidump() { 94 Clear(); 95 // We need to call finalize on the process before destroying ourselves 96 // to make sure all of the broadcaster cleanup goes as planned. If we 97 // destruct this class, then Process::~Process() might have problems 98 // trying to fully destroy the broadcaster. 99 Finalize(); 100 } 101 102 void ProcessMinidump::Initialize() { 103 static llvm::once_flag g_once_flag; 104 105 llvm::call_once(g_once_flag, []() { 106 PluginManager::RegisterPlugin(GetPluginNameStatic(), 107 GetPluginDescriptionStatic(), 108 ProcessMinidump::CreateInstance); 109 }); 110 } 111 112 void ProcessMinidump::Terminate() { 113 PluginManager::UnregisterPlugin(ProcessMinidump::CreateInstance); 114 } 115 116 Status ProcessMinidump::DoLoadCore() { 117 Status error; 118 119 m_thread_list = m_minidump_parser.GetThreads(); 120 m_active_exception = m_minidump_parser.GetExceptionStream(); 121 ReadModuleList(); 122 GetTarget().SetArchitecture(GetArchitecture()); 123 124 llvm::Optional<lldb::pid_t> pid = m_minidump_parser.GetPid(); 125 if (!pid) { 126 error.SetErrorString("failed to parse PID"); 127 return error; 128 } 129 SetID(pid.getValue()); 130 131 return error; 132 } 133 134 DynamicLoader *ProcessMinidump::GetDynamicLoader() { 135 if (m_dyld_ap.get() == nullptr) 136 m_dyld_ap.reset(DynamicLoader::FindPlugin(this, nullptr)); 137 return m_dyld_ap.get(); 138 } 139 140 ConstString ProcessMinidump::GetPluginName() { return GetPluginNameStatic(); } 141 142 uint32_t ProcessMinidump::GetPluginVersion() { return 1; } 143 144 Status ProcessMinidump::DoDestroy() { return Status(); } 145 146 void ProcessMinidump::RefreshStateAfterStop() { 147 if (!m_active_exception) 148 return; 149 150 if (m_active_exception->exception_record.exception_code == 151 MinidumpException::DumpRequested) { 152 return; 153 } 154 155 lldb::StopInfoSP stop_info; 156 lldb::ThreadSP stop_thread; 157 158 Process::m_thread_list.SetSelectedThreadByID(m_active_exception->thread_id); 159 stop_thread = Process::m_thread_list.GetSelectedThread(); 160 ArchSpec arch = GetArchitecture(); 161 162 if (arch.GetTriple().getOS() == llvm::Triple::Linux) { 163 stop_info = StopInfo::CreateStopReasonWithSignal( 164 *stop_thread, m_active_exception->exception_record.exception_code); 165 } else { 166 std::string desc; 167 llvm::raw_string_ostream desc_stream(desc); 168 desc_stream << "Exception " 169 << llvm::format_hex( 170 m_active_exception->exception_record.exception_code, 8) 171 << " encountered at address " 172 << llvm::format_hex( 173 m_active_exception->exception_record.exception_address, 174 8); 175 stop_info = StopInfo::CreateStopReasonWithException( 176 *stop_thread, desc_stream.str().c_str()); 177 } 178 179 stop_thread->SetStopInfo(stop_info); 180 } 181 182 bool ProcessMinidump::IsAlive() { return true; } 183 184 bool ProcessMinidump::WarnBeforeDetach() const { return false; } 185 186 size_t ProcessMinidump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 187 Status &error) { 188 // Don't allow the caching that lldb_private::Process::ReadMemory does 189 // since we have it all cached in our dump file anyway. 190 return DoReadMemory(addr, buf, size, error); 191 } 192 193 size_t ProcessMinidump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, 194 Status &error) { 195 196 llvm::ArrayRef<uint8_t> mem = m_minidump_parser.GetMemory(addr, size); 197 if (mem.empty()) { 198 error.SetErrorString("could not parse memory info"); 199 return 0; 200 } 201 202 std::memcpy(buf, mem.data(), mem.size()); 203 return mem.size(); 204 } 205 206 ArchSpec ProcessMinidump::GetArchitecture() { 207 if (!m_is_wow64) { 208 return m_minidump_parser.GetArchitecture(); 209 } 210 211 llvm::Triple triple; 212 triple.setVendor(llvm::Triple::VendorType::UnknownVendor); 213 triple.setArch(llvm::Triple::ArchType::x86); 214 triple.setOS(llvm::Triple::OSType::Win32); 215 return ArchSpec(triple); 216 } 217 218 Status ProcessMinidump::GetMemoryRegionInfo(lldb::addr_t load_addr, 219 MemoryRegionInfo &range_info) { 220 Status error; 221 auto info = m_minidump_parser.GetMemoryRegionInfo(load_addr); 222 if (!info) { 223 error.SetErrorString("No valid MemoryRegionInfo found!"); 224 return error; 225 } 226 range_info = info.getValue(); 227 return error; 228 } 229 230 void ProcessMinidump::Clear() { Process::m_thread_list.Clear(); } 231 232 bool ProcessMinidump::UpdateThreadList(ThreadList &old_thread_list, 233 ThreadList &new_thread_list) { 234 uint32_t num_threads = 0; 235 if (m_thread_list.size() > 0) 236 num_threads = m_thread_list.size(); 237 238 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) { 239 llvm::ArrayRef<uint8_t> context; 240 if (!m_is_wow64) 241 context = m_minidump_parser.GetThreadContext(m_thread_list[tid]); 242 else 243 context = m_minidump_parser.GetThreadContextWow64(m_thread_list[tid]); 244 245 lldb::ThreadSP thread_sp( 246 new ThreadMinidump(*this, m_thread_list[tid], context)); 247 new_thread_list.AddThread(thread_sp); 248 } 249 return new_thread_list.GetSize(false) > 0; 250 } 251 252 void ProcessMinidump::ReadModuleList() { 253 std::vector<const MinidumpModule *> filtered_modules = 254 m_minidump_parser.GetFilteredModuleList(); 255 256 for (auto module : filtered_modules) { 257 llvm::Optional<std::string> name = 258 m_minidump_parser.GetMinidumpString(module->module_name_rva); 259 260 if (!name) 261 continue; 262 263 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); 264 if (log) { 265 log->Printf("ProcessMinidump::%s found module: name: %s %#010" PRIx64 266 "-%#010" PRIx64 " size: %" PRIu32, 267 __FUNCTION__, name.getValue().c_str(), 268 uint64_t(module->base_of_image), 269 module->base_of_image + module->size_of_image, 270 uint32_t(module->size_of_image)); 271 } 272 273 // check if the process is wow64 - a 32 bit windows process running on a 274 // 64 bit windows 275 if (llvm::StringRef(name.getValue()).endswith_lower("wow64.dll")) { 276 m_is_wow64 = true; 277 } 278 279 const auto file_spec = FileSpec(name.getValue(), true); 280 ModuleSpec module_spec = file_spec; 281 Status error; 282 lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec, &error); 283 if (!module_sp || error.Fail()) { 284 continue; 285 } 286 287 if (log) { 288 log->Printf("ProcessMinidump::%s load module: name: %s", __FUNCTION__, 289 name.getValue().c_str()); 290 } 291 292 bool load_addr_changed = false; 293 module_sp->SetLoadAddress(GetTarget(), module->base_of_image, false, 294 load_addr_changed); 295 } 296 } 297 298 bool ProcessMinidump::GetProcessInfo(ProcessInstanceInfo &info) { 299 info.Clear(); 300 info.SetProcessID(GetID()); 301 info.SetArchitecture(GetArchitecture()); 302 lldb::ModuleSP module_sp = GetTarget().GetExecutableModule(); 303 if (module_sp) { 304 const bool add_exe_file_as_first_arg = false; 305 info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(), 306 add_exe_file_as_first_arg); 307 } 308 return true; 309 } 310