2 * Wine debugger - minidump handling
4 * Copyright 2005 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
35 #include "wine/debug.h"
36 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
40 static struct be_process_io be_process_minidump_io;
42 void minidump_write(const char* file, const EXCEPTION_RECORD* rec)
45 MINIDUMP_EXCEPTION_INFORMATION mei;
46 EXCEPTION_POINTERS ep;
48 hFile = CreateFile(file, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
49 FILE_ATTRIBUTE_NORMAL, NULL);
51 if (hFile == INVALID_HANDLE_VALUE) return;
55 mei.ThreadId = dbg_curr_thread->tid;
56 mei.ExceptionPointers = &ep;
57 ep.ExceptionRecord = (EXCEPTION_RECORD*)rec;
58 ep.ContextRecord = &dbg_context;
59 mei.ClientPointers = FALSE;
61 MiniDumpWriteDump(dbg_curr_process->handle, dbg_curr_process->pid,
62 hFile, MiniDumpNormal/*|MiniDumpWithDataSegs*/,
63 rec ? &mei : NULL, NULL, NULL);
67 #define Wine_ElfModuleListStream 0xFFF0
69 struct tgt_process_minidump_data
76 static inline struct tgt_process_minidump_data* PRIVATE(struct dbg_process* pcs)
78 return (struct tgt_process_minidump_data*)pcs->pio_data;
81 static BOOL WINAPI tgt_process_minidump_read(HANDLE hProcess, const void* addr,
82 void* buffer, SIZE_T len, SIZE_T* rlen)
85 MINIDUMP_DIRECTORY* dir;
88 if (!PRIVATE(dbg_curr_process)->mapping) return FALSE;
89 if (MiniDumpReadDumpStream(PRIVATE(dbg_curr_process)->mapping,
90 MemoryListStream, &dir, &stream, &size))
92 MINIDUMP_MEMORY_LIST* mml = (MINIDUMP_MEMORY_LIST*)stream;
93 MINIDUMP_MEMORY_DESCRIPTOR* mmd = &mml->MemoryRanges[0];
96 for (i = 0; i < mml->NumberOfMemoryRanges; i++, mmd++)
98 if (mmd->StartOfMemoryRange <= (DWORD_PTR)addr &&
99 (DWORD_PTR)addr < mmd->StartOfMemoryRange + mmd->Memory.DataSize)
101 len = min(len, mmd->StartOfMemoryRange + mmd->Memory.DataSize - (DWORD_PTR)addr);
103 (char*)PRIVATE(dbg_curr_process)->mapping + mmd->Memory.Rva + (DWORD_PTR)addr - mmd->StartOfMemoryRange,
105 if (rlen) *rlen = len;
110 /* FIXME: this is a dirty hack to let the last frame in a bt to work
111 * However, we need to check who's to blame, this code or the current
112 * dbghelp!StackWalk implementation
114 if ((DWORD_PTR)addr < 32)
116 memset(buffer, 0, len);
117 if (rlen) *rlen = len;
123 static BOOL WINAPI tgt_process_minidump_write(HANDLE hProcess, void* addr,
124 const void* buffer, SIZE_T len, SIZE_T* wlen)
129 BOOL CALLBACK validate_file(PCWSTR name, void* user)
131 return FALSE; /* get the first file we find !! */
134 static BOOL is_pe_module_embedded(struct tgt_process_minidump_data* data,
135 MINIDUMP_MODULE* pe_mm)
138 MINIDUMP_DIRECTORY* dir;
139 MINIDUMP_MODULE_LIST* mml;
141 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, &dir,
142 (void**)&mml, &size))
147 for (i = 0, mm = &mml->Modules[0]; i < mml->NumberOfModules; i++, mm++)
149 if (mm->BaseOfImage <= pe_mm->BaseOfImage &&
150 mm->BaseOfImage + mm->SizeOfImage >= pe_mm->BaseOfImage + pe_mm->SizeOfImage)
157 static enum dbg_start minidump_do_reload(struct tgt_process_minidump_data* data)
160 MINIDUMP_DIRECTORY* dir;
162 DWORD pid = 1; /* by default */
163 HANDLE hProc = (HANDLE)0x900DBAAD;
165 MINIDUMP_MODULE_LIST* mml;
167 MINIDUMP_STRING* mds;
168 char exec_name[1024];
173 if (MiniDumpReadDumpStream(data->mapping, MiscInfoStream, &dir, &stream, &size))
175 MINIDUMP_MISC_INFO* mmi = (MINIDUMP_MISC_INFO*)stream;
176 if (mmi->Flags1 & MINIDUMP_MISC1_PROCESS_ID)
177 pid = mmi->ProcessId;
180 /* fetch executable name (it's normally the first one in module list) */
181 strcpy(exec_name, "<minidump-exec>"); /* default */
182 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, &dir, &stream, &size))
184 mml = (MINIDUMP_MODULE_LIST*)stream;
185 if (mml->NumberOfModules)
189 mm = &mml->Modules[0];
190 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
191 len = WideCharToMultiByte(CP_ACP, 0, mds->Buffer,
192 mds->Length / sizeof(WCHAR),
193 exec_name, sizeof(exec_name) - 1, NULL, NULL);
195 for (ptr = exec_name + len - 1; ptr >= exec_name; ptr--)
197 if (*ptr == '/' || *ptr == '\\')
199 memmove(exec_name, ptr + 1, strlen(ptr + 1) + 1);
206 if (MiniDumpReadDumpStream(data->mapping, SystemInfoStream, &dir, &stream, &size))
208 MINIDUMP_SYSTEM_INFO* msi = (MINIDUMP_SYSTEM_INFO*)stream;
212 dbg_printf("WineDbg starting on minidump on pid %04x\n", pid);
213 switch (msi->ProcessorArchitecture)
215 case PROCESSOR_ARCHITECTURE_UNKNOWN:
218 case PROCESSOR_ARCHITECTURE_INTEL:
219 strcpy(tmp, "Intel ");
220 switch (msi->ProcessorLevel)
222 case 3: str = "80386"; break;
223 case 4: str = "80486"; break;
224 case 5: str = "Pentium"; break;
225 case 6: str = "Pentium Pro/II or AMD Athlon"; break;
226 case 15: str = "Pentium 4 or AMD Athlon64"; break;
227 default: str = "???"; break;
230 if (msi->ProcessorLevel == 3 || msi->ProcessorLevel == 4)
232 if (HIWORD(msi->ProcessorRevision) == 0xFF)
233 sprintf(tmp + strlen(tmp), " (%c%d)",
234 'A' + HIBYTE(LOWORD(msi->ProcessorRevision)),
235 LOBYTE(LOWORD(msi->ProcessorRevision)));
237 sprintf(tmp + strlen(tmp), " (%c%d)",
238 'A' + HIWORD(msi->ProcessorRevision),
239 LOWORD(msi->ProcessorRevision));
241 else sprintf(tmp + strlen(tmp), " (%d.%d)",
242 HIWORD(msi->ProcessorRevision),
243 LOWORD(msi->ProcessorRevision));
246 case PROCESSOR_ARCHITECTURE_MIPS:
249 case PROCESSOR_ARCHITECTURE_ALPHA:
252 case PROCESSOR_ARCHITECTURE_PPC:
259 dbg_printf(" %s was running on #%d %s CPU%s",
260 exec_name, msi->u.s.NumberOfProcessors, str,
261 msi->u.s.NumberOfProcessors < 2 ? "" : "s");
262 switch (msi->MajorVersion)
265 switch (msi->MinorVersion)
267 case 51: str = "NT 3.51"; break;
268 default: str = "3-????"; break;
272 switch (msi->MinorVersion)
274 case 0: str = (msi->PlatformId == VER_PLATFORM_WIN32_NT) ? "NT 4.0" : "95"; break;
275 case 10: str = "98"; break;
276 case 90: str = "ME"; break;
277 default: str = "5-????"; break;
281 switch (msi->MinorVersion)
283 case 0: str = "2000"; break;
284 case 1: str = "XP"; break;
285 case 2: str = "Server 2003"; break;
286 default: str = "5-????"; break;
289 default: str = "???"; break;
291 dbg_printf(" on Windows %s (%u)\n", str, msi->BuildNumber);
292 /* FIXME CSD: msi->CSDVersionRva */
295 dbg_curr_process = dbg_add_process(&be_process_minidump_io, pid, hProc);
297 dbg_curr_process->pio_data = data;
298 dbg_set_process_name(dbg_curr_process, exec_name);
300 SymInitialize(hProc, NULL, FALSE);
302 if (MiniDumpReadDumpStream(data->mapping, ThreadListStream, &dir, &stream, &size))
304 MINIDUMP_THREAD_LIST* mtl = (MINIDUMP_THREAD_LIST*)stream;
307 for (i = 0; i < mtl->NumberOfThreads; i++)
309 dbg_add_thread(dbg_curr_process, mtl->Threads[i].ThreadId, NULL,
310 (void*)(DWORD_PTR)mtl->Threads[i].Teb);
313 /* first load ELF modules, then do the PE ones */
314 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, &dir,
317 WCHAR buffer[MAX_PATH];
319 mml = (MINIDUMP_MODULE_LIST*)stream;
320 for (i = 0, mm = &mml->Modules[0]; i < mml->NumberOfModules; i++, mm++)
322 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
323 memcpy(nameW, mds->Buffer, mds->Length);
324 nameW[mds->Length / sizeof(WCHAR)] = 0;
325 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->CheckSum,
326 0, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
327 SymLoadModuleExW(hProc, NULL, buffer, NULL, mm->BaseOfImage, mm->SizeOfImage,
330 SymLoadModuleExW(hProc, NULL, nameW, NULL, mm->BaseOfImage, mm->SizeOfImage,
331 NULL, SLMFLAG_VIRTUAL);
334 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, &dir, &stream, &size))
336 WCHAR buffer[MAX_PATH];
338 mml = (MINIDUMP_MODULE_LIST*)stream;
339 for (i = 0, mm = &mml->Modules[0]; i < mml->NumberOfModules; i++, mm++)
341 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
342 memcpy(nameW, mds->Buffer, mds->Length);
343 nameW[mds->Length / sizeof(WCHAR)] = 0;
344 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->TimeDateStamp,
345 mm->SizeOfImage, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
346 SymLoadModuleExW(hProc, NULL, buffer, NULL, mm->BaseOfImage, mm->SizeOfImage,
348 else if (is_pe_module_embedded(data, mm))
349 SymLoadModuleExW(hProc, NULL, nameW, NULL, mm->BaseOfImage, mm->SizeOfImage,
352 SymLoadModuleExW(hProc, NULL, nameW, NULL, mm->BaseOfImage, mm->SizeOfImage,
353 NULL, SLMFLAG_VIRTUAL);
356 if (MiniDumpReadDumpStream(data->mapping, ExceptionStream, &dir, &stream, &size))
358 MINIDUMP_EXCEPTION_STREAM* mes = (MINIDUMP_EXCEPTION_STREAM*)stream;
360 if ((dbg_curr_thread = dbg_get_thread(dbg_curr_process, mes->ThreadId)))
364 dbg_curr_tid = mes->ThreadId;
365 dbg_curr_thread->in_exception = TRUE;
366 dbg_curr_thread->excpt_record.ExceptionCode = mes->ExceptionRecord.ExceptionCode;
367 dbg_curr_thread->excpt_record.ExceptionFlags = mes->ExceptionRecord.ExceptionFlags;
368 dbg_curr_thread->excpt_record.ExceptionRecord = (void*)(DWORD_PTR)mes->ExceptionRecord.ExceptionRecord;
369 dbg_curr_thread->excpt_record.ExceptionAddress = (void*)(DWORD_PTR)mes->ExceptionRecord.ExceptionAddress;
370 dbg_curr_thread->excpt_record.NumberParameters = mes->ExceptionRecord.NumberParameters;
371 for (i = 0; i < dbg_curr_thread->excpt_record.NumberParameters; i++)
373 dbg_curr_thread->excpt_record.ExceptionInformation[i] = mes->ExceptionRecord.ExceptionInformation[i];
375 memcpy(&dbg_context, (char*)data->mapping + mes->ThreadContext.Rva,
376 min(sizeof(dbg_context), mes->ThreadContext.DataSize));
377 memory_get_current_pc(&addr);
378 stack_fetch_frames();
379 be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
381 be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
382 stack_backtrace(mes->ThreadId);
383 source_list_from_addr(&addr, 0);
389 static void cleanup(struct tgt_process_minidump_data* data)
391 if (data->mapping) UnmapViewOfFile(data->mapping);
392 if (data->hMap) CloseHandle(data->hMap);
393 if (data->hFile != INVALID_HANDLE_VALUE) CloseHandle(data->hFile);
394 HeapFree(GetProcessHeap(), 0, data);
397 static struct be_process_io be_process_minidump_io;
399 enum dbg_start minidump_reload(int argc, char* argv[])
401 struct tgt_process_minidump_data* data;
402 enum dbg_start ret = start_error_parse;
404 /* try the form <myself> minidump-file */
405 if (argc != 1) return start_error_parse;
407 WINE_TRACE("Processing Minidump file %s\n", argv[0]);
409 data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct tgt_process_minidump_data));
410 if (!data) return start_error_init;
411 data->mapping = NULL;
413 data->hFile = INVALID_HANDLE_VALUE;
415 if ((data->hFile = CreateFileA(argv[0], GENERIC_READ, FILE_SHARE_READ, NULL,
416 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
417 ((data->hMap = CreateFileMappingA(data->hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
418 ((data->mapping = MapViewOfFile(data->hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
422 if (((MINIDUMP_HEADER*)data->mapping)->Signature == MINIDUMP_SIGNATURE)
424 ret = minidump_do_reload(data);
429 dbg_printf("Unexpected fault while reading minidump %s\n", argv[0]);
434 if (ret != start_ok) cleanup(data);
438 static BOOL tgt_process_minidump_close_process(struct dbg_process* pcs, BOOL kill)
440 struct tgt_process_minidump_data* data = PRIVATE(pcs);
443 pcs->pio_data = NULL;
444 SymCleanup(pcs->handle);
445 dbg_del_process(pcs);
449 static struct be_process_io be_process_minidump_io =
451 tgt_process_minidump_close_process,
452 tgt_process_minidump_read,
453 tgt_process_minidump_write,