blob: c9e8a57d2d000cd213e4312b666e2aa32e074b08 [file] [log] [blame]
Adrian McCarthyc96516f2015-08-03 23:01:51 +00001//===-- ProcessWinMiniDump.cpp ----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ProcessWinMiniDump.h"
11
12#include "lldb/Host/windows/windows.h"
13#include <DbgHelp.h>
14
15#include <assert.h>
16#include <stdlib.h>
17
18#include <mutex>
19
20#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Module.h"
22#include "lldb/Core/ModuleSpec.h"
23#include "lldb/Core/Section.h"
24#include "lldb/Core/State.h"
25#include "lldb/Core/DataBufferHeap.h"
26#include "lldb/Core/Log.h"
27#include "lldb/Target/Target.h"
28#include "lldb/Target/DynamicLoader.h"
29#include "lldb/Target/UnixSignals.h"
30#include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h"
31
32#include "ThreadWinMiniDump.h"
33
34using namespace lldb_private;
35
36// Encapsulates the private data for ProcessWinMiniDump.
37// TODO(amccarth): Determine if we need a mutex for access.
38class ProcessWinMiniDump::Data
39{
40public:
41 Data();
42 ~Data();
43
44 FileSpec m_core_file;
45 HANDLE m_dump_file; // handle to the open minidump file
46 HANDLE m_mapping; // handle to the file mapping for the minidump file
47 void * m_base_addr; // base memory address of the minidump
48};
49
50ConstString
51ProcessWinMiniDump::GetPluginNameStatic()
52{
53 static ConstString g_name("win-minidump");
54 return g_name;
55}
56
57const char *
58ProcessWinMiniDump::GetPluginDescriptionStatic()
59{
60 return "Windows minidump plug-in.";
61}
62
63void
64ProcessWinMiniDump::Terminate()
65{
66 PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance);
67}
68
69
70lldb::ProcessSP
71ProcessWinMiniDump::CreateInstance(Target &target, Listener &listener, const FileSpec *crash_file)
72{
73 lldb::ProcessSP process_sp;
74 if (crash_file)
75 {
76 process_sp.reset(new ProcessWinMiniDump(target, listener, *crash_file));
77 }
78 return process_sp;
79}
80
81bool
82ProcessWinMiniDump::CanDebug(Target &target, bool plugin_specified_by_name)
83{
84 // TODO(amccarth): Eventually, this needs some actual logic.
85 return true;
86}
87
88ProcessWinMiniDump::ProcessWinMiniDump(Target& target, Listener &listener,
89 const FileSpec &core_file) :
90 Process(target, listener),
91 m_data_up(new Data)
92{
93 m_data_up->m_core_file = core_file;
94}
95
96ProcessWinMiniDump::~ProcessWinMiniDump()
97{
98 Clear();
99 // We need to call finalize on the process before destroying ourselves
100 // to make sure all of the broadcaster cleanup goes as planned. If we
101 // destruct this class, then Process::~Process() might have problems
102 // trying to fully destroy the broadcaster.
103 Finalize();
104}
105
106ConstString
107ProcessWinMiniDump::GetPluginName()
108{
109 return GetPluginNameStatic();
110}
111
112uint32_t
113ProcessWinMiniDump::GetPluginVersion()
114{
115 return 1;
116}
117
118
119Error
120ProcessWinMiniDump::DoLoadCore()
121{
122 Error error;
123
124 error = MapMiniDumpIntoMemory(m_data_up->m_core_file.GetCString());
125 if (error.Fail())
126 {
127 return error;
128 }
129
130 m_target.SetArchitecture(DetermineArchitecture());
131 // TODO(amccarth): Build the module list.
132 // TODO(amccarth): Read the exeception record.
133
134 return error;
135
136}
137
138DynamicLoader *
139ProcessWinMiniDump::GetDynamicLoader()
140{
141 if (m_dyld_ap.get() == NULL)
142 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString()));
143 return m_dyld_ap.get();
144}
145
146bool
147ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
148{
149 assert(m_data_up != nullptr);
150 assert(m_data_up->m_base_addr != 0);
151
152 MINIDUMP_DIRECTORY *dir = nullptr;
153 void *ptr = nullptr;
154 ULONG size = 0;
155 if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, ThreadListStream, &dir, &ptr, &size))
156 {
157 assert(dir->StreamType == ThreadListStream);
158 assert(size == dir->Location.DataSize);
159 assert(ptr == static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva));
160 auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(ptr);
161 const ULONG32 thread_count = thread_list_ptr->NumberOfThreads;
162 assert(thread_count < std::numeric_limits<int>::max());
163 for (int i = 0; i < thread_count; ++i) {
164 std::shared_ptr<ThreadWinMiniDump> thread_sp(new ThreadWinMiniDump(*this, thread_list_ptr->Threads[i].ThreadId));
165 new_thread_list.AddThread(thread_sp);
166 }
167 }
168
169 return new_thread_list.GetSize(false) > 0;
170}
171
172void
173ProcessWinMiniDump::RefreshStateAfterStop()
174{
175}
176
177Error
178ProcessWinMiniDump::DoDestroy()
179{
180 return Error();
181}
182
183bool
184ProcessWinMiniDump::IsAlive()
185{
186 return true;
187}
188
189size_t
190ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
191{
192 // Don't allow the caching that lldb_private::Process::ReadMemory does
193 // since in core files we have it all cached our our core file anyway.
194 return DoReadMemory(addr, buf, size, error);
195}
196
197size_t
198ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error)
199{
200 // TODO
201 return 0;
202}
203
204void
205ProcessWinMiniDump::Clear()
206{
207 m_thread_list.Clear();
208}
209
210void
211ProcessWinMiniDump::Initialize()
212{
213 static std::once_flag g_once_flag;
214
215 std::call_once(g_once_flag, []()
216 {
217 PluginManager::RegisterPlugin(GetPluginNameStatic(),
218 GetPluginDescriptionStatic(),
219 CreateInstance);
220 });
221}
222
223lldb::addr_t
224ProcessWinMiniDump::GetImageInfoAddress()
225{
226 Target *target = &GetTarget();
227 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
228 Address addr = obj_file->GetImageInfoAddress(target);
229
230 if (addr.IsValid())
231 return addr.GetLoadAddress(target);
232 return LLDB_INVALID_ADDRESS;
233}
234
235ArchSpec
236ProcessWinMiniDump::GetArchitecture()
237{
238 // TODO
239 return ArchSpec();
240}
241
242
243ProcessWinMiniDump::Data::Data() :
244 m_dump_file(INVALID_HANDLE_VALUE),
245 m_mapping(NULL),
246 m_base_addr(nullptr)
247{
248}
249
250ProcessWinMiniDump::Data::~Data()
251{
252 if (m_base_addr)
253 {
254 ::UnmapViewOfFile(m_base_addr);
255 m_base_addr = nullptr;
256 }
257 if (m_mapping)
258 {
259 ::CloseHandle(m_mapping);
260 m_mapping = NULL;
261 }
262 if (m_dump_file != INVALID_HANDLE_VALUE)
263 {
264 ::CloseHandle(m_dump_file);
265 m_dump_file = INVALID_HANDLE_VALUE;
266 }
267}
268
269Error
270ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file)
271{
272 Error error;
273
274 m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ,
275 NULL, OPEN_EXISTING,
276 FILE_ATTRIBUTE_NORMAL, NULL);
277 if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE)
278 {
279 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
280 return error;
281 }
282
283 m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL,
284 PAGE_READONLY, 0, 0, NULL);
285 if (m_data_up->m_mapping == NULL)
286 {
287 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
288 return error;
289 }
290
291 m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0);
292 if (m_data_up->m_base_addr == NULL)
293 {
294 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
295 return error;
296 }
297
298 return error;
299}
300
301
302ArchSpec
303ProcessWinMiniDump::DetermineArchitecture()
304{
305 assert(m_data_up != nullptr);
306 assert(m_data_up->m_base_addr != 0);
307
308 MINIDUMP_DIRECTORY *dir = nullptr;
309 void *ptr = nullptr;
310 ULONG size = 0;
311 if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, SystemInfoStream, &dir, &ptr, &size))
312 {
313 assert(dir->StreamType == SystemInfoStream);
314 assert(size == dir->Location.DataSize);
315 assert(ptr == static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva));
316 auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(ptr);
317 switch (system_info_ptr->ProcessorArchitecture)
318 {
319 case PROCESSOR_ARCHITECTURE_INTEL:
320 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE);
321 case PROCESSOR_ARCHITECTURE_AMD64:
322 return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE);
323 default:
324 break;
325 }
326 }
327
328 return ArchSpec(); // invalid or unknown
329}