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