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