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> |
Adrian McCarthy | d9fa2b5 | 2015-11-12 21:16:15 +0000 | [diff] [blame] | 17 | #include <memory> |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 18 | #include <mutex> |
| 19 | |
Adrian McCarthy | 0c35cde | 2015-12-04 22:22:15 +0000 | [diff] [blame] | 20 | #include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 21 | #include "lldb/Core/DataBufferHeap.h" |
| 22 | #include "lldb/Core/Log.h" |
Adrian McCarthy | 0c35cde | 2015-12-04 22:22:15 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Module.h" |
| 24 | #include "lldb/Core/ModuleSpec.h" |
| 25 | #include "lldb/Core/PluginManager.h" |
| 26 | #include "lldb/Core/Section.h" |
| 27 | #include "lldb/Core/State.h" |
| 28 | #include "lldb/Target/DynamicLoader.h" |
| 29 | #include "lldb/Target/MemoryRegionInfo.h" |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 30 | #include "lldb/Target/StopInfo.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 31 | #include "lldb/Target/Target.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 32 | #include "lldb/Target/UnixSignals.h" |
Adrian McCarthy | 278a6c9 | 2015-12-09 00:29:38 +0000 | [diff] [blame^] | 33 | #include "lldb/Utility/LLDBAssert.h" |
Adrian McCarthy | 0c35cde | 2015-12-04 22:22:15 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ConvertUTF.h" |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Format.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 37 | |
Adrian McCarthy | 27785dd | 2015-08-24 16:00:51 +0000 | [diff] [blame] | 38 | #include "ExceptionRecord.h" |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 39 | #include "ThreadWinMiniDump.h" |
| 40 | |
| 41 | using namespace lldb_private; |
| 42 | |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame] | 43 | namespace |
| 44 | { |
| 45 | |
| 46 | // Getting a string out of a mini dump is a chore. You're usually given a |
| 47 | // relative virtual address (RVA), which points to a counted string that's in |
| 48 | // Windows Unicode (UTF-16). This wrapper handles all the redirection and |
| 49 | // returns a UTF-8 copy of the string. |
| 50 | std::string |
| 51 | GetMiniDumpString(const void *base_addr, const RVA rva) |
| 52 | { |
| 53 | std::string result; |
| 54 | if (!base_addr) |
| 55 | { |
| 56 | return result; |
| 57 | } |
| 58 | auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(base_addr) + rva); |
| 59 | auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer); |
| 60 | const auto source_length = ::wcslen(md_string->Buffer); |
| 61 | const auto source_end = source_start + source_length; |
| 62 | result.resize(4*source_length); // worst case length |
| 63 | auto result_start = reinterpret_cast<UTF8 *>(&result[0]); |
| 64 | const auto result_end = result_start + result.size(); |
| 65 | ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion); |
| 66 | const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start); |
| 67 | result.resize(result_size); // shrink to actual length |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | } // anonymous namespace |
| 72 | |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 73 | // Encapsulates the private data for ProcessWinMiniDump. |
| 74 | // TODO(amccarth): Determine if we need a mutex for access. |
| 75 | class ProcessWinMiniDump::Data |
| 76 | { |
| 77 | public: |
| 78 | Data(); |
| 79 | ~Data(); |
| 80 | |
| 81 | FileSpec m_core_file; |
| 82 | HANDLE m_dump_file; // handle to the open minidump file |
| 83 | HANDLE m_mapping; // handle to the file mapping for the minidump file |
| 84 | void * m_base_addr; // base memory address of the minidump |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 85 | std::shared_ptr<ExceptionRecord> m_exception_sp; |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | ConstString |
| 89 | ProcessWinMiniDump::GetPluginNameStatic() |
| 90 | { |
| 91 | static ConstString g_name("win-minidump"); |
| 92 | return g_name; |
| 93 | } |
| 94 | |
| 95 | const char * |
| 96 | ProcessWinMiniDump::GetPluginDescriptionStatic() |
| 97 | { |
| 98 | return "Windows minidump plug-in."; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | ProcessWinMiniDump::Terminate() |
| 103 | { |
| 104 | PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | lldb::ProcessSP |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 109 | ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file) |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 110 | { |
| 111 | lldb::ProcessSP process_sp; |
| 112 | if (crash_file) |
| 113 | { |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 114 | process_sp.reset(new ProcessWinMiniDump(target_sp, listener, *crash_file)); |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 115 | } |
| 116 | return process_sp; |
| 117 | } |
| 118 | |
| 119 | bool |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 120 | ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 121 | { |
| 122 | // TODO(amccarth): Eventually, this needs some actual logic. |
| 123 | return true; |
| 124 | } |
| 125 | |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 126 | ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp, Listener &listener, |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 127 | const FileSpec &core_file) : |
Adrian McCarthy | 18a9135d | 2015-10-28 18:21:45 +0000 | [diff] [blame] | 128 | ProcessWindows(target_sp, listener), |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 129 | m_data_up(new Data) |
| 130 | { |
| 131 | m_data_up->m_core_file = core_file; |
| 132 | } |
| 133 | |
| 134 | ProcessWinMiniDump::~ProcessWinMiniDump() |
| 135 | { |
| 136 | Clear(); |
| 137 | // We need to call finalize on the process before destroying ourselves |
| 138 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 139 | // destruct this class, then Process::~Process() might have problems |
| 140 | // trying to fully destroy the broadcaster. |
| 141 | Finalize(); |
| 142 | } |
| 143 | |
| 144 | ConstString |
| 145 | ProcessWinMiniDump::GetPluginName() |
| 146 | { |
| 147 | return GetPluginNameStatic(); |
| 148 | } |
| 149 | |
| 150 | uint32_t |
| 151 | ProcessWinMiniDump::GetPluginVersion() |
| 152 | { |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | Error |
| 158 | ProcessWinMiniDump::DoLoadCore() |
| 159 | { |
| 160 | Error error; |
| 161 | |
| 162 | error = MapMiniDumpIntoMemory(m_data_up->m_core_file.GetCString()); |
| 163 | if (error.Fail()) |
| 164 | { |
| 165 | return error; |
| 166 | } |
| 167 | |
Zachary Turner | 7529df9 | 2015-09-01 20:02:29 +0000 | [diff] [blame] | 168 | GetTarget().SetArchitecture(DetermineArchitecture()); |
Adrian McCarthy | ab59a0f | 2015-09-17 20:52:29 +0000 | [diff] [blame] | 169 | ReadMiscInfo(); // notably for process ID |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame] | 170 | ReadModuleList(); |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 171 | ReadExceptionRecord(); |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 172 | |
| 173 | return error; |
| 174 | |
| 175 | } |
| 176 | |
| 177 | DynamicLoader * |
| 178 | ProcessWinMiniDump::GetDynamicLoader() |
| 179 | { |
| 180 | if (m_dyld_ap.get() == NULL) |
| 181 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString())); |
| 182 | return m_dyld_ap.get(); |
| 183 | } |
| 184 | |
| 185 | bool |
| 186 | ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 187 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 188 | size_t size = 0; |
| 189 | auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size)); |
| 190 | if (thread_list_ptr) |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 191 | { |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 192 | const ULONG32 thread_count = thread_list_ptr->NumberOfThreads; |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 193 | for (ULONG32 i = 0; i < thread_count; ++i) { |
Adrian McCarthy | d9fa2b5 | 2015-11-12 21:16:15 +0000 | [diff] [blame] | 194 | const auto &mini_dump_thread = thread_list_ptr->Threads[i]; |
| 195 | auto thread_sp = std::make_shared<ThreadWinMiniDump>(*this, mini_dump_thread.ThreadId); |
| 196 | if (mini_dump_thread.ThreadContext.DataSize >= sizeof(CONTEXT)) |
| 197 | { |
| 198 | const CONTEXT *context = reinterpret_cast<const CONTEXT *>(static_cast<const char *>(m_data_up->m_base_addr) + mini_dump_thread.ThreadContext.Rva); |
| 199 | thread_sp->SetContext(context); |
| 200 | } |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 201 | new_thread_list.AddThread(thread_sp); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return new_thread_list.GetSize(false) > 0; |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | ProcessWinMiniDump::RefreshStateAfterStop() |
| 210 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 211 | if (!m_data_up) return; |
| 212 | if (!m_data_up->m_exception_sp) return; |
| 213 | |
| 214 | auto active_exception = m_data_up->m_exception_sp; |
| 215 | std::string desc; |
| 216 | llvm::raw_string_ostream desc_stream(desc); |
| 217 | desc_stream << "Exception " |
| 218 | << llvm::format_hex(active_exception->GetExceptionCode(), 8) |
| 219 | << " encountered at address " |
| 220 | << llvm::format_hex(active_exception->GetExceptionAddress(), 8); |
| 221 | m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); |
| 222 | auto stop_thread = m_thread_list.GetSelectedThread(); |
| 223 | auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str()); |
| 224 | stop_thread->SetStopInfo(stop_info); |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | Error |
| 228 | ProcessWinMiniDump::DoDestroy() |
| 229 | { |
| 230 | return Error(); |
| 231 | } |
| 232 | |
| 233 | bool |
| 234 | ProcessWinMiniDump::IsAlive() |
| 235 | { |
| 236 | return true; |
| 237 | } |
| 238 | |
Adrian McCarthy | 6c3d03c | 2015-09-01 16:59:31 +0000 | [diff] [blame] | 239 | bool |
| 240 | ProcessWinMiniDump::WarnBeforeDetach () const |
| 241 | { |
| 242 | // Since this is post-mortem debugging, there's no need to warn the user |
| 243 | // that quitting the debugger will terminate the process. |
| 244 | return false; |
| 245 | } |
| 246 | |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 247 | size_t |
| 248 | ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) |
| 249 | { |
| 250 | // Don't allow the caching that lldb_private::Process::ReadMemory does |
Adrian McCarthy | 6c3d03c | 2015-09-01 16:59:31 +0000 | [diff] [blame] | 251 | // since we have it all cached our our dump file anyway. |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 252 | return DoReadMemory(addr, buf, size, error); |
| 253 | } |
| 254 | |
| 255 | size_t |
| 256 | ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) |
| 257 | { |
Adrian McCarthy | 6c3d03c | 2015-09-01 16:59:31 +0000 | [diff] [blame] | 258 | // I don't have a sense of how frequently this is called or how many memory |
| 259 | // ranges a mini dump typically has, so I'm not sure if searching for the |
| 260 | // appropriate range linearly each time is stupid. Perhaps we should build |
| 261 | // an index for faster lookups. |
| 262 | Range range = {0}; |
| 263 | if (!FindMemoryRange(addr, &range)) |
| 264 | { |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | // There's at least some overlap between the beginning of the desired range |
| 269 | // (addr) and the current range. Figure out where the overlap begins and |
| 270 | // how much overlap there is, then copy it to the destination buffer. |
Adrian McCarthy | 278a6c9 | 2015-12-09 00:29:38 +0000 | [diff] [blame^] | 271 | lldbassert(range.start <= addr); |
| 272 | const size_t offset = addr - range.start; |
| 273 | lldbassert(offset < range.size); |
Adrian McCarthy | 6c3d03c | 2015-09-01 16:59:31 +0000 | [diff] [blame] | 274 | const size_t overlap = std::min(size, range.size - offset); |
| 275 | std::memcpy(buf, range.ptr + offset, overlap); |
| 276 | return overlap; |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Adrian McCarthy | 0c35cde | 2015-12-04 22:22:15 +0000 | [diff] [blame] | 279 | Error |
| 280 | ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) |
| 281 | { |
| 282 | Error error; |
| 283 | size_t size; |
| 284 | const auto list = reinterpret_cast<const MINIDUMP_MEMORY_INFO_LIST *>(FindDumpStream(MemoryInfoListStream, &size)); |
| 285 | if (list == nullptr || size < sizeof(MINIDUMP_MEMORY_INFO_LIST)) |
| 286 | { |
| 287 | error.SetErrorString("the mini dump contains no memory range information"); |
| 288 | return error; |
| 289 | } |
| 290 | |
| 291 | if (list->SizeOfEntry < sizeof(MINIDUMP_MEMORY_INFO)) |
| 292 | { |
| 293 | error.SetErrorString("the entries in the mini dump memory info list are smaller than expected"); |
| 294 | return error; |
| 295 | } |
| 296 | |
| 297 | if (size < list->SizeOfHeader + list->SizeOfEntry * list->NumberOfEntries) |
| 298 | { |
| 299 | error.SetErrorString("the mini dump memory info list is incomplete"); |
| 300 | return error; |
| 301 | } |
| 302 | |
| 303 | for (int i = 0; i < list->NumberOfEntries; ++i) |
| 304 | { |
| 305 | const auto entry = reinterpret_cast<const MINIDUMP_MEMORY_INFO *>(reinterpret_cast<const char *>(list) + |
| 306 | list->SizeOfHeader + i * list->SizeOfEntry); |
| 307 | const auto head = entry->BaseAddress; |
| 308 | const auto tail = head + entry->RegionSize; |
| 309 | if (head <= load_addr && load_addr < tail) |
| 310 | { |
| 311 | info.SetReadable(IsPageReadable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); |
| 312 | info.SetWritable(IsPageWritable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); |
| 313 | info.SetExecutable(IsPageExecutable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); |
| 314 | return error; |
| 315 | } |
| 316 | } |
| 317 | // Note that the memory info list doesn't seem to contain ranges in kernel space, |
| 318 | // so if you're walking a stack that has kernel frames, the stack may appear |
| 319 | // truncated. |
| 320 | error.SetErrorString("address is not in a known range"); |
| 321 | return error; |
| 322 | } |
| 323 | |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 324 | void |
| 325 | ProcessWinMiniDump::Clear() |
| 326 | { |
| 327 | m_thread_list.Clear(); |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | ProcessWinMiniDump::Initialize() |
| 332 | { |
| 333 | static std::once_flag g_once_flag; |
| 334 | |
| 335 | std::call_once(g_once_flag, []() |
| 336 | { |
| 337 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 338 | GetPluginDescriptionStatic(), |
| 339 | CreateInstance); |
| 340 | }); |
| 341 | } |
| 342 | |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 343 | ArchSpec |
| 344 | ProcessWinMiniDump::GetArchitecture() |
| 345 | { |
| 346 | // TODO |
| 347 | return ArchSpec(); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | ProcessWinMiniDump::Data::Data() : |
| 352 | m_dump_file(INVALID_HANDLE_VALUE), |
| 353 | m_mapping(NULL), |
| 354 | m_base_addr(nullptr) |
| 355 | { |
| 356 | } |
| 357 | |
| 358 | ProcessWinMiniDump::Data::~Data() |
| 359 | { |
| 360 | if (m_base_addr) |
| 361 | { |
| 362 | ::UnmapViewOfFile(m_base_addr); |
| 363 | m_base_addr = nullptr; |
| 364 | } |
| 365 | if (m_mapping) |
| 366 | { |
| 367 | ::CloseHandle(m_mapping); |
| 368 | m_mapping = NULL; |
| 369 | } |
| 370 | if (m_dump_file != INVALID_HANDLE_VALUE) |
| 371 | { |
| 372 | ::CloseHandle(m_dump_file); |
| 373 | m_dump_file = INVALID_HANDLE_VALUE; |
| 374 | } |
| 375 | } |
| 376 | |
Adrian McCarthy | 6c3d03c | 2015-09-01 16:59:31 +0000 | [diff] [blame] | 377 | bool |
| 378 | ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const |
| 379 | { |
| 380 | size_t stream_size = 0; |
| 381 | auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size)); |
| 382 | if (mem_list_stream) |
| 383 | { |
| 384 | for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) { |
| 385 | const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i]; |
| 386 | const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory; |
| 387 | const lldb::addr_t range_start = mem_desc.StartOfMemoryRange; |
| 388 | const size_t range_size = loc_desc.DataSize; |
| 389 | if (range_start <= addr && addr < range_start + range_size) |
| 390 | { |
| 391 | range_out->start = range_start; |
| 392 | range_out->size = range_size; |
| 393 | range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva; |
| 394 | return true; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Some mini dumps have a Memory64ListStream that captures all the heap |
| 400 | // memory. We can't exactly use the same loop as above, because the mini |
| 401 | // dump uses slightly different data structures to describe those. |
| 402 | auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size)); |
| 403 | if (mem_list64_stream) |
| 404 | { |
| 405 | size_t base_rva = mem_list64_stream->BaseRva; |
| 406 | for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) { |
| 407 | const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i]; |
| 408 | const lldb::addr_t range_start = mem_desc.StartOfMemoryRange; |
| 409 | const size_t range_size = mem_desc.DataSize; |
| 410 | if (range_start <= addr && addr < range_start + range_size) |
| 411 | { |
| 412 | range_out->start = range_start; |
| 413 | range_out->size = range_size; |
| 414 | range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva; |
| 415 | return true; |
| 416 | } |
| 417 | base_rva += range_size; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 425 | Error |
| 426 | ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file) |
| 427 | { |
| 428 | Error error; |
| 429 | |
| 430 | m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ, |
| 431 | NULL, OPEN_EXISTING, |
| 432 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 433 | if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE) |
| 434 | { |
| 435 | error.SetError(::GetLastError(), lldb::eErrorTypeWin32); |
| 436 | return error; |
| 437 | } |
| 438 | |
| 439 | m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL, |
| 440 | PAGE_READONLY, 0, 0, NULL); |
| 441 | if (m_data_up->m_mapping == NULL) |
| 442 | { |
| 443 | error.SetError(::GetLastError(), lldb::eErrorTypeWin32); |
| 444 | return error; |
| 445 | } |
| 446 | |
| 447 | m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0); |
| 448 | if (m_data_up->m_base_addr == NULL) |
| 449 | { |
| 450 | error.SetError(::GetLastError(), lldb::eErrorTypeWin32); |
| 451 | return error; |
| 452 | } |
| 453 | |
| 454 | return error; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | ArchSpec |
| 459 | ProcessWinMiniDump::DetermineArchitecture() |
| 460 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 461 | size_t size = 0; |
| 462 | auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size)); |
| 463 | if (system_info_ptr) |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 464 | { |
Adrian McCarthy | c96516f | 2015-08-03 23:01:51 +0000 | [diff] [blame] | 465 | switch (system_info_ptr->ProcessorArchitecture) |
| 466 | { |
| 467 | case PROCESSOR_ARCHITECTURE_INTEL: |
| 468 | return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE); |
| 469 | case PROCESSOR_ARCHITECTURE_AMD64: |
| 470 | return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE); |
| 471 | default: |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return ArchSpec(); // invalid or unknown |
| 477 | } |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 478 | |
| 479 | void |
Adrian McCarthy | ab59a0f | 2015-09-17 20:52:29 +0000 | [diff] [blame] | 480 | ProcessWinMiniDump::ReadExceptionRecord() |
| 481 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 482 | size_t size = 0; |
| 483 | auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size)); |
| 484 | if (exception_stream_ptr) |
| 485 | { |
| 486 | m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId)); |
| 487 | } |
| 488 | } |
| 489 | |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame] | 490 | void |
Adrian McCarthy | ab59a0f | 2015-09-17 20:52:29 +0000 | [diff] [blame] | 491 | ProcessWinMiniDump::ReadMiscInfo() |
| 492 | { |
| 493 | size_t size = 0; |
| 494 | const auto misc_info_ptr = static_cast<MINIDUMP_MISC_INFO*>(FindDumpStream(MiscInfoStream, &size)); |
| 495 | if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) { |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) { |
| 500 | // This misc info record has the process ID. |
| 501 | SetID(misc_info_ptr->ProcessId); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | ProcessWinMiniDump::ReadModuleList() |
| 507 | { |
Adrian McCarthy | 23d14b6 | 2015-08-28 14:42:03 +0000 | [diff] [blame] | 508 | size_t size = 0; |
| 509 | auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size)); |
| 510 | if (!module_list_ptr || module_list_ptr->NumberOfModules == 0) |
| 511 | { |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i) |
| 516 | { |
| 517 | const auto &module = module_list_ptr->Modules[i]; |
| 518 | const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva); |
| 519 | ModuleSpec module_spec = FileSpec(file_name, true); |
| 520 | |
| 521 | lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec); |
| 522 | if (!module_sp) |
| 523 | { |
| 524 | continue; |
| 525 | } |
| 526 | bool load_addr_changed = false; |
| 527 | module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed); |
| 528 | } |
| 529 | } |
| 530 | |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 531 | void * |
Adrian McCarthy | 6c3d03c | 2015-09-01 16:59:31 +0000 | [diff] [blame] | 532 | ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const |
| 533 | { |
Adrian McCarthy | 61ede15 | 2015-08-19 20:43:22 +0000 | [diff] [blame] | 534 | void *stream = nullptr; |
| 535 | *size_out = 0; |
| 536 | |
| 537 | assert(m_data_up != nullptr); |
| 538 | assert(m_data_up->m_base_addr != 0); |
| 539 | |
| 540 | MINIDUMP_DIRECTORY *dir = nullptr; |
| 541 | if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) && |
| 542 | dir != nullptr && dir->Location.DataSize > 0) |
| 543 | { |
| 544 | assert(dir->StreamType == stream_number); |
| 545 | *size_out = dir->Location.DataSize; |
| 546 | stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva); |
| 547 | } |
| 548 | |
| 549 | return stream; |
| 550 | } |