Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 1 | //===-- ProcessWinMiniDump.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 | #include "ProcessWinMiniDump.h" |
| 11 | |
| 12 | #include "lldb/Host/windows/windows.h" |
| 13 | #include <DbgHelp.h> |
| 14 | |
| 15 | #include <assert.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <mutex> |
| 19 | |
| 20 | #include "lldb/Core/PluginManager.h" |
| 21 | #include "lldb/Core/Module.h" |
| 22 | #include "lldb/Core/ModuleSpec.h" |
| 23 | #include "lldb/Core/Section.h" |
| 24 | #include "lldb/Core/State.h" |
| 25 | #include "lldb/Core/DataBufferHeap.h" |
| 26 | #include "lldb/Core/Log.h" |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 27 | #include "lldb/Target/StopInfo.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Target.h" |
| 29 | #include "lldb/Target/DynamicLoader.h" |
| 30 | #include "lldb/Target/UnixSignals.h" |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Format.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame^] | 33 | #include "llvm/Support/ConvertUTF.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 34 | #include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h" |
| 35 | |
Adrian McCarthy | 27785dd | 2015-08-24 16:00:51 +0000 | [diff] [blame] | 36 | #include "ExceptionRecord.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 37 | #include "ThreadWinMiniDump.h" |
| 38 | |
| 39 | using namespace lldb_private; |
| 40 | |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame^] | 41 | namespace |
| 42 | { |
| 43 | |
| 44 | // Getting a string out of a mini dump is a chore. You're usually given a |
| 45 | // relative virtual address (RVA), which points to a counted string that's in |
| 46 | // Windows Unicode (UTF-16). This wrapper handles all the redirection and |
| 47 | // returns a UTF-8 copy of the string. |
| 48 | std::string |
| 49 | GetMiniDumpString(const void *base_addr, const RVA rva) |
| 50 | { |
| 51 | std::string result; |
| 52 | if (!base_addr) |
| 53 | { |
| 54 | return result; |
| 55 | } |
| 56 | auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(base_addr) + rva); |
| 57 | auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer); |
| 58 | const auto source_length = ::wcslen(md_string->Buffer); |
| 59 | const auto source_end = source_start + source_length; |
| 60 | result.resize(4*source_length); // worst case length |
| 61 | auto result_start = reinterpret_cast<UTF8 *>(&result[0]); |
| 62 | const auto result_end = result_start + result.size(); |
| 63 | ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion); |
| 64 | const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start); |
| 65 | result.resize(result_size); // shrink to actual length |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | } // anonymous namespace |
| 70 | |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 71 | // Encapsulates the private data for ProcessWinMiniDump. |
| 72 | // TODO(amccarth): Determine if we need a mutex for access. |
| 73 | class ProcessWinMiniDump::Data |
| 74 | { |
| 75 | public: |
| 76 | Data(); |
| 77 | ~Data(); |
| 78 | |
| 79 | FileSpec m_core_file; |
| 80 | HANDLE m_dump_file; // handle to the open minidump file |
| 81 | HANDLE m_mapping; // handle to the file mapping for the minidump file |
| 82 | void * m_base_addr; // base memory address of the minidump |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 83 | std::shared_ptr<ExceptionRecord> m_exception_sp; |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | ConstString |
| 87 | ProcessWinMiniDump::GetPluginNameStatic() |
| 88 | { |
| 89 | static ConstString g_name("win-minidump"); |
| 90 | return g_name; |
| 91 | } |
| 92 | |
| 93 | const char * |
| 94 | ProcessWinMiniDump::GetPluginDescriptionStatic() |
| 95 | { |
| 96 | return "Windows minidump plug-in."; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | ProcessWinMiniDump::Terminate() |
| 101 | { |
| 102 | PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | lldb::ProcessSP |
| 107 | ProcessWinMiniDump::CreateInstance(Target &target, Listener &listener, const FileSpec *crash_file) |
| 108 | { |
| 109 | lldb::ProcessSP process_sp; |
| 110 | if (crash_file) |
| 111 | { |
| 112 | process_sp.reset(new ProcessWinMiniDump(target, listener, *crash_file)); |
| 113 | } |
| 114 | return process_sp; |
| 115 | } |
| 116 | |
| 117 | bool |
| 118 | ProcessWinMiniDump::CanDebug(Target &target, bool plugin_specified_by_name) |
| 119 | { |
| 120 | // TODO(amccarth): Eventually, this needs some actual logic. |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | ProcessWinMiniDump::ProcessWinMiniDump(Target& target, Listener &listener, |
| 125 | const FileSpec &core_file) : |
| 126 | Process(target, listener), |
| 127 | m_data_up(new Data) |
| 128 | { |
| 129 | m_data_up->m_core_file = core_file; |
| 130 | } |
| 131 | |
| 132 | ProcessWinMiniDump::~ProcessWinMiniDump() |
| 133 | { |
| 134 | Clear(); |
| 135 | // We need to call finalize on the process before destroying ourselves |
| 136 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 137 | // destruct this class, then Process::~Process() might have problems |
| 138 | // trying to fully destroy the broadcaster. |
| 139 | Finalize(); |
| 140 | } |
| 141 | |
| 142 | ConstString |
| 143 | ProcessWinMiniDump::GetPluginName() |
| 144 | { |
| 145 | return GetPluginNameStatic(); |
| 146 | } |
| 147 | |
| 148 | uint32_t |
| 149 | ProcessWinMiniDump::GetPluginVersion() |
| 150 | { |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | Error |
| 156 | ProcessWinMiniDump::DoLoadCore() |
| 157 | { |
| 158 | Error error; |
| 159 | |
| 160 | error = MapMiniDumpIntoMemory(m_data_up->m_core_file.GetCString()); |
| 161 | if (error.Fail()) |
| 162 | { |
| 163 | return error; |
| 164 | } |
| 165 | |
| 166 | m_target.SetArchitecture(DetermineArchitecture()); |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame^] | 167 | ReadModuleList(); |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 168 | ReadExceptionRecord(); |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 169 | |
| 170 | return error; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | DynamicLoader * |
| 175 | ProcessWinMiniDump::GetDynamicLoader() |
| 176 | { |
| 177 | if (m_dyld_ap.get() == NULL) |
| 178 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString())); |
| 179 | return m_dyld_ap.get(); |
| 180 | } |
| 181 | |
| 182 | bool |
| 183 | ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 184 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 185 | size_t size = 0; |
| 186 | auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size)); |
| 187 | if (thread_list_ptr) |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 188 | { |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 189 | const ULONG32 thread_count = thread_list_ptr->NumberOfThreads; |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 190 | for (ULONG32 i = 0; i < thread_count; ++i) { |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 191 | std::shared_ptr<ThreadWinMiniDump> thread_sp(new ThreadWinMiniDump(*this, thread_list_ptr->Threads[i].ThreadId)); |
| 192 | new_thread_list.AddThread(thread_sp); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return new_thread_list.GetSize(false) > 0; |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | ProcessWinMiniDump::RefreshStateAfterStop() |
| 201 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 202 | if (!m_data_up) return; |
| 203 | if (!m_data_up->m_exception_sp) return; |
| 204 | |
| 205 | auto active_exception = m_data_up->m_exception_sp; |
| 206 | std::string desc; |
| 207 | llvm::raw_string_ostream desc_stream(desc); |
| 208 | desc_stream << "Exception " |
| 209 | << llvm::format_hex(active_exception->GetExceptionCode(), 8) |
| 210 | << " encountered at address " |
| 211 | << llvm::format_hex(active_exception->GetExceptionAddress(), 8); |
| 212 | m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); |
| 213 | auto stop_thread = m_thread_list.GetSelectedThread(); |
| 214 | auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str()); |
| 215 | stop_thread->SetStopInfo(stop_info); |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | Error |
| 219 | ProcessWinMiniDump::DoDestroy() |
| 220 | { |
| 221 | return Error(); |
| 222 | } |
| 223 | |
| 224 | bool |
| 225 | ProcessWinMiniDump::IsAlive() |
| 226 | { |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | size_t |
| 231 | ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) |
| 232 | { |
| 233 | // Don't allow the caching that lldb_private::Process::ReadMemory does |
| 234 | // since in core files we have it all cached our our core file anyway. |
| 235 | return DoReadMemory(addr, buf, size, error); |
| 236 | } |
| 237 | |
| 238 | size_t |
| 239 | ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) |
| 240 | { |
| 241 | // TODO |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | ProcessWinMiniDump::Clear() |
| 247 | { |
| 248 | m_thread_list.Clear(); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | ProcessWinMiniDump::Initialize() |
| 253 | { |
| 254 | static std::once_flag g_once_flag; |
| 255 | |
| 256 | std::call_once(g_once_flag, []() |
| 257 | { |
| 258 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 259 | GetPluginDescriptionStatic(), |
| 260 | CreateInstance); |
| 261 | }); |
| 262 | } |
| 263 | |
| 264 | lldb::addr_t |
| 265 | ProcessWinMiniDump::GetImageInfoAddress() |
| 266 | { |
| 267 | Target *target = &GetTarget(); |
| 268 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 269 | Address addr = obj_file->GetImageInfoAddress(target); |
| 270 | |
| 271 | if (addr.IsValid()) |
| 272 | return addr.GetLoadAddress(target); |
| 273 | return LLDB_INVALID_ADDRESS; |
| 274 | } |
| 275 | |
| 276 | ArchSpec |
| 277 | ProcessWinMiniDump::GetArchitecture() |
| 278 | { |
| 279 | // TODO |
| 280 | return ArchSpec(); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | ProcessWinMiniDump::Data::Data() : |
| 285 | m_dump_file(INVALID_HANDLE_VALUE), |
| 286 | m_mapping(NULL), |
| 287 | m_base_addr(nullptr) |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | ProcessWinMiniDump::Data::~Data() |
| 292 | { |
| 293 | if (m_base_addr) |
| 294 | { |
| 295 | ::UnmapViewOfFile(m_base_addr); |
| 296 | m_base_addr = nullptr; |
| 297 | } |
| 298 | if (m_mapping) |
| 299 | { |
| 300 | ::CloseHandle(m_mapping); |
| 301 | m_mapping = NULL; |
| 302 | } |
| 303 | if (m_dump_file != INVALID_HANDLE_VALUE) |
| 304 | { |
| 305 | ::CloseHandle(m_dump_file); |
| 306 | m_dump_file = INVALID_HANDLE_VALUE; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | Error |
| 311 | ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file) |
| 312 | { |
| 313 | Error error; |
| 314 | |
| 315 | m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ, |
| 316 | NULL, OPEN_EXISTING, |
| 317 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 318 | if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE) |
| 319 | { |
| 320 | error.SetError(::GetLastError(), lldb::eErrorTypeWin32); |
| 321 | return error; |
| 322 | } |
| 323 | |
| 324 | m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL, |
| 325 | PAGE_READONLY, 0, 0, NULL); |
| 326 | if (m_data_up->m_mapping == NULL) |
| 327 | { |
| 328 | error.SetError(::GetLastError(), lldb::eErrorTypeWin32); |
| 329 | return error; |
| 330 | } |
| 331 | |
| 332 | m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0); |
| 333 | if (m_data_up->m_base_addr == NULL) |
| 334 | { |
| 335 | error.SetError(::GetLastError(), lldb::eErrorTypeWin32); |
| 336 | return error; |
| 337 | } |
| 338 | |
| 339 | return error; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | ArchSpec |
| 344 | ProcessWinMiniDump::DetermineArchitecture() |
| 345 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 346 | size_t size = 0; |
| 347 | auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size)); |
| 348 | if (system_info_ptr) |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 349 | { |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 350 | switch (system_info_ptr->ProcessorArchitecture) |
| 351 | { |
| 352 | case PROCESSOR_ARCHITECTURE_INTEL: |
| 353 | return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE); |
| 354 | case PROCESSOR_ARCHITECTURE_AMD64: |
| 355 | return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE); |
| 356 | default: |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return ArchSpec(); // invalid or unknown |
| 362 | } |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 363 | |
| 364 | void |
| 365 | ProcessWinMiniDump::ReadExceptionRecord() { |
| 366 | size_t size = 0; |
| 367 | auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size)); |
| 368 | if (exception_stream_ptr) |
| 369 | { |
| 370 | m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId)); |
| 371 | } |
| 372 | } |
| 373 | |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame^] | 374 | void |
| 375 | ProcessWinMiniDump::ReadModuleList() { |
| 376 | size_t size = 0; |
| 377 | auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size)); |
| 378 | if (!module_list_ptr || module_list_ptr->NumberOfModules == 0) |
| 379 | { |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i) |
| 384 | { |
| 385 | const auto &module = module_list_ptr->Modules[i]; |
| 386 | const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva); |
| 387 | ModuleSpec module_spec = FileSpec(file_name, true); |
| 388 | |
| 389 | lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec); |
| 390 | if (!module_sp) |
| 391 | { |
| 392 | continue; |
| 393 | } |
| 394 | bool load_addr_changed = false; |
| 395 | module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed); |
| 396 | } |
| 397 | } |
| 398 | |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 399 | void * |
| 400 | ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) { |
| 401 | void *stream = nullptr; |
| 402 | *size_out = 0; |
| 403 | |
| 404 | assert(m_data_up != nullptr); |
| 405 | assert(m_data_up->m_base_addr != 0); |
| 406 | |
| 407 | MINIDUMP_DIRECTORY *dir = nullptr; |
| 408 | if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) && |
| 409 | dir != nullptr && dir->Location.DataSize > 0) |
| 410 | { |
| 411 | assert(dir->StreamType == stream_number); |
| 412 | *size_out = dir->Location.DataSize; |
| 413 | stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva); |
| 414 | } |
| 415 | |
| 416 | return stream; |
| 417 | } |