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"; break;
226 default: str = "???"; break;
229 if (msi->ProcessorLevel == 3 || msi->ProcessorLevel == 4)
231 if (HIWORD(msi->ProcessorRevision) == 0xFF)
232 sprintf(tmp + strlen(tmp), "-%c%d",
233 'A' + HIBYTE(LOWORD(msi->ProcessorRevision)),
234 LOBYTE(LOWORD(msi->ProcessorRevision)));
236 sprintf(tmp + strlen(tmp), "-%c%d",
237 'A' + HIWORD(msi->ProcessorRevision),
238 LOWORD(msi->ProcessorRevision));
240 else sprintf(tmp + strlen(tmp), "-%d.%d",
241 HIWORD(msi->ProcessorRevision),
242 LOWORD(msi->ProcessorRevision));
245 case PROCESSOR_ARCHITECTURE_MIPS:
248 case PROCESSOR_ARCHITECTURE_ALPHA:
251 case PROCESSOR_ARCHITECTURE_PPC:
258 dbg_printf(" %s was running on #%d %s CPU%s",
259 exec_name, msi->u.s.NumberOfProcessors, str,
260 msi->u.s.NumberOfProcessors < 2 ? "" : "s");
261 switch (msi->MajorVersion)
264 switch (msi->MinorVersion)
266 case 51: str = "NT 3.51"; break;
267 default: str = "3-????"; break;
271 switch (msi->MinorVersion)
273 case 0: str = (msi->PlatformId == VER_PLATFORM_WIN32_NT) ? "NT 4.0" : "95"; break;
274 case 10: str = "98"; break;
275 case 90: str = "ME"; break;
276 default: str = "5-????"; break;
280 switch (msi->MinorVersion)
282 case 0: str = "2000"; break;
283 case 1: str = "XP"; break;
284 case 2: str = "Server 2003"; break;
285 default: str = "5-????"; break;
288 default: str = "???"; break;
290 dbg_printf(" on Windows %s (%u)\n", str, msi->BuildNumber);
291 /* FIXME CSD: msi->CSDVersionRva */
294 dbg_curr_process = dbg_add_process(&be_process_minidump_io, pid, hProc);
296 dbg_curr_process->pio_data = data;
297 dbg_set_process_name(dbg_curr_process, exec_name);
299 SymInitialize(hProc, NULL, FALSE);
301 if (MiniDumpReadDumpStream(data->mapping, ThreadListStream, &dir, &stream, &size))
303 MINIDUMP_THREAD_LIST* mtl = (MINIDUMP_THREAD_LIST*)stream;
306 for (i = 0; i < mtl->NumberOfThreads; i++)
308 dbg_add_thread(dbg_curr_process, mtl->Threads[i].ThreadId, NULL,
309 (void*)(DWORD_PTR)mtl->Threads[i].Teb);
312 /* first load ELF modules, then do the PE ones */
313 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, &dir,
316 WCHAR buffer[MAX_PATH];
318 mml = (MINIDUMP_MODULE_LIST*)stream;
319 for (i = 0, mm = &mml->Modules[0]; i < mml->NumberOfModules; i++, mm++)
321 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
322 memcpy(nameW, mds->Buffer, mds->Length);
323 nameW[mds->Length / sizeof(WCHAR)] = 0;
324 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->CheckSum,
325 0, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
326 SymLoadModuleExW(hProc, NULL, buffer, NULL, mm->BaseOfImage, mm->SizeOfImage,
329 SymLoadModuleExW(hProc, NULL, nameW, NULL, mm->BaseOfImage, mm->SizeOfImage,
330 NULL, SLMFLAG_VIRTUAL);
333 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, &dir, &stream, &size))
335 WCHAR buffer[MAX_PATH];
337 mml = (MINIDUMP_MODULE_LIST*)stream;
338 for (i = 0, mm = &mml->Modules[0]; i < mml->NumberOfModules; i++, mm++)
340 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
341 memcpy(nameW, mds->Buffer, mds->Length);
342 nameW[mds->Length / sizeof(WCHAR)] = 0;
343 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->TimeDateStamp,
344 mm->SizeOfImage, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
345 SymLoadModuleExW(hProc, NULL, buffer, NULL, mm->BaseOfImage, mm->SizeOfImage,
347 else if (is_pe_module_embedded(data, mm))
348 SymLoadModuleExW(hProc, NULL, nameW, NULL, mm->BaseOfImage, mm->SizeOfImage,
351 SymLoadModuleExW(hProc, NULL, nameW, NULL, mm->BaseOfImage, mm->SizeOfImage,
352 NULL, SLMFLAG_VIRTUAL);
355 if (MiniDumpReadDumpStream(data->mapping, ExceptionStream, &dir, &stream, &size))
357 MINIDUMP_EXCEPTION_STREAM* mes = (MINIDUMP_EXCEPTION_STREAM*)stream;
359 if ((dbg_curr_thread = dbg_get_thread(dbg_curr_process, mes->ThreadId)))
363 dbg_curr_tid = mes->ThreadId;
364 dbg_curr_thread->in_exception = TRUE;
365 dbg_curr_thread->excpt_record.ExceptionCode = mes->ExceptionRecord.ExceptionCode;
366 dbg_curr_thread->excpt_record.ExceptionFlags = mes->ExceptionRecord.ExceptionFlags;
367 dbg_curr_thread->excpt_record.ExceptionRecord = (void*)(DWORD_PTR)mes->ExceptionRecord.ExceptionRecord;
368 dbg_curr_thread->excpt_record.ExceptionAddress = (void*)(DWORD_PTR)mes->ExceptionRecord.ExceptionAddress;
369 dbg_curr_thread->excpt_record.NumberParameters = mes->ExceptionRecord.NumberParameters;
370 for (i = 0; i < dbg_curr_thread->excpt_record.NumberParameters; i++)
372 dbg_curr_thread->excpt_record.ExceptionInformation[i] = mes->ExceptionRecord.ExceptionInformation[i];
374 memcpy(&dbg_context, (char*)data->mapping + mes->ThreadContext.Rva,
375 min(sizeof(dbg_context), mes->ThreadContext.DataSize));
376 memory_get_current_pc(&addr);
377 stack_fetch_frames();
378 be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
380 be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
381 stack_backtrace(mes->ThreadId);
382 source_list_from_addr(&addr, 0);
388 static void cleanup(struct tgt_process_minidump_data* data)
390 if (data->mapping) UnmapViewOfFile(data->mapping);
391 if (data->hMap) CloseHandle(data->hMap);
392 if (data->hFile != INVALID_HANDLE_VALUE) CloseHandle(data->hFile);
393 HeapFree(GetProcessHeap(), 0, data);
396 static struct be_process_io be_process_minidump_io;
398 enum dbg_start minidump_reload(int argc, char* argv[])
400 struct tgt_process_minidump_data* data;
401 enum dbg_start ret = start_error_parse;
403 /* try the form <myself> minidump-file */
404 if (argc != 1) return start_error_parse;
406 WINE_TRACE("Processing Minidump file %s\n", argv[0]);
408 data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct tgt_process_minidump_data));
409 if (!data) return start_error_init;
410 data->mapping = NULL;
412 data->hFile = INVALID_HANDLE_VALUE;
414 if ((data->hFile = CreateFileA(argv[0], GENERIC_READ, FILE_SHARE_READ, NULL,
415 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
416 ((data->hMap = CreateFileMappingA(data->hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
417 ((data->mapping = MapViewOfFile(data->hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
421 if (((MINIDUMP_HEADER*)data->mapping)->Signature == MINIDUMP_SIGNATURE)
423 ret = minidump_do_reload(data);
428 dbg_printf("Unexpected fault while reading minidump %s\n", argv[0]);
433 if (ret != start_ok) cleanup(data);
437 static BOOL tgt_process_minidump_close_process(struct dbg_process* pcs, BOOL kill)
439 struct tgt_process_minidump_data* data = PRIVATE(pcs);
442 pcs->pio_data = NULL;
443 SymCleanup(pcs->handle);
444 dbg_del_process(pcs);
448 static struct be_process_io be_process_minidump_io =
450 tgt_process_minidump_close_process,
451 tgt_process_minidump_read,
452 tgt_process_minidump_write,