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