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;
49 hFile = CreateFile(file, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
50 FILE_ATTRIBUTE_NORMAL, NULL);
52 if (hFile == INVALID_HANDLE_VALUE) return;
56 mei.ThreadId = dbg_curr_thread->tid;
57 mei.ExceptionPointers = &ep;
58 ep.ExceptionRecord = (EXCEPTION_RECORD*)rec;
59 ep.ContextRecord = &dbg_context;
60 mei.ClientPointers = FALSE;
62 /* this is a wine specific options to return also ELF modules in the
65 SymSetOptions((wine_opt = SymGetOptions()) | 0x40000000);
66 MiniDumpWriteDump(dbg_curr_process->handle, dbg_curr_process->pid,
67 hFile, MiniDumpNormal/*|MiniDumpWithDataSegs*/,
68 rec ? &mei : NULL, NULL, NULL);
69 SymSetOptions(wine_opt);
73 #define Wine_ElfModuleListStream 0xFFF0
75 struct tgt_process_minidump_data
82 static inline struct tgt_process_minidump_data* PRIVATE(struct dbg_process* pcs)
84 return (struct tgt_process_minidump_data*)pcs->pio_data;
87 static BOOL WINAPI tgt_process_minidump_read(HANDLE hProcess, const void* addr,
88 void* buffer, DWORD len, DWORD* rlen)
91 MINIDUMP_DIRECTORY* dir;
94 if (!PRIVATE(dbg_curr_process)->mapping) return FALSE;
95 if (MiniDumpReadDumpStream(PRIVATE(dbg_curr_process)->mapping,
96 MemoryListStream, &dir, &stream, &size))
98 MINIDUMP_MEMORY_LIST* mml = (MINIDUMP_MEMORY_LIST*)stream;
99 MINIDUMP_MEMORY_DESCRIPTOR* mmd = &mml->MemoryRanges[0];
102 for (i = 0; i < mml->NumberOfMemoryRanges; i++, mmd++)
104 if (mmd->StartOfMemoryRange <= (DWORD_PTR)addr &&
105 (DWORD_PTR)addr < mmd->StartOfMemoryRange + mmd->Memory.DataSize)
107 len = min(len, mmd->StartOfMemoryRange + mmd->Memory.DataSize - (DWORD_PTR)addr);
109 (char*)PRIVATE(dbg_curr_process)->mapping + mmd->Memory.Rva + (DWORD_PTR)addr - mmd->StartOfMemoryRange,
111 if (rlen) *rlen = len;
116 /* FIXME: this is a dirty hack to let the last frame in a bt to work
117 * However, we need to check who's to blame, this code or the current
118 * dbghelp!StackWalk implementation
120 if ((DWORD_PTR)addr < 32)
122 memset(buffer, 0, len);
123 if (rlen) *rlen = len;
129 static BOOL WINAPI tgt_process_minidump_write(HANDLE hProcess, void* addr,
130 const void* buffer, DWORD len, DWORD* wlen)
135 BOOL CALLBACK validate_file(PSTR name, void* user)
137 return FALSE; /* get the first file we find !! */
140 static enum dbg_start minidump_do_reload(struct tgt_process_minidump_data* data)
143 MINIDUMP_DIRECTORY* dir;
145 DWORD pid = 1; /* by default */
146 HANDLE hProc = (HANDLE)0x900DBAAD;
148 MINIDUMP_MODULE_LIST* mml;
150 MINIDUMP_STRING* mds;
151 char exec_name[1024];
156 if (MiniDumpReadDumpStream(data->mapping, MiscInfoStream, &dir, &stream, &size))
158 MINIDUMP_MISC_INFO* mmi = (MINIDUMP_MISC_INFO*)stream;
159 if (mmi->Flags1 & MINIDUMP_MISC1_PROCESS_ID)
160 pid = mmi->ProcessId;
163 /* fetch executable name (it's normally the first one in module list) */
164 strcpy(exec_name, "<minidump-exec>"); /* default */
165 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, &dir, &stream, &size))
167 mml = (MINIDUMP_MODULE_LIST*)stream;
168 if (mml->NumberOfModules)
172 mm = &mml->Modules[0];
173 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
174 len = WideCharToMultiByte(CP_ACP, 0, mds->Buffer,
175 mds->Length / sizeof(WCHAR),
176 name, sizeof(name) - 1, NULL, NULL);
178 for (ptr = name + len - 1; ptr >= name; ptr--)
180 if (*ptr == '/' || *ptr == '\\')
182 strcpy(exec_name, ptr + 1);
186 if (ptr < name) strcpy(exec_name, name);
190 if (MiniDumpReadDumpStream(data->mapping, SystemInfoStream, &dir, &stream, &size))
192 MINIDUMP_SYSTEM_INFO* msi = (MINIDUMP_SYSTEM_INFO*)stream;
196 dbg_printf("WineDbg starting on minidump on pid %lu\n", pid);
197 switch (msi->ProcessorArchitecture)
199 case PROCESSOR_ARCHITECTURE_UNKNOWN:
202 case PROCESSOR_ARCHITECTURE_INTEL:
203 strcpy(tmp, "Intel ");
204 switch (msi->ProcessorLevel)
206 case 3: str = "80386"; break;
207 case 4: str = "80486"; break;
208 case 5: str = "Pentium"; break;
209 case 6: str = "Pentium Pro/II"; break;
210 default: str = "???"; break;
213 if (msi->ProcessorLevel == 3 || msi->ProcessorLevel == 4)
215 if (HIWORD(msi->ProcessorRevision) == 0xFF)
216 sprintf(tmp + strlen(tmp), "-%c%d",
217 'A' + HIBYTE(LOWORD(msi->ProcessorRevision)),
218 LOBYTE(LOWORD(msi->ProcessorRevision)));
220 sprintf(tmp + strlen(tmp), "-%c%d",
221 'A' + HIWORD(msi->ProcessorRevision),
222 LOWORD(msi->ProcessorRevision));
224 else sprintf(tmp + strlen(tmp), "-%d.%d",
225 HIWORD(msi->ProcessorRevision),
226 LOWORD(msi->ProcessorRevision));
229 case PROCESSOR_ARCHITECTURE_MIPS:
232 case PROCESSOR_ARCHITECTURE_ALPHA:
235 case PROCESSOR_ARCHITECTURE_PPC:
242 dbg_printf(" %s was running on #%d %s CPU%s",
243 exec_name, msi->u.s.NumberOfProcessors, str,
244 msi->u.s.NumberOfProcessors < 2 ? "" : "s");
245 switch (msi->MajorVersion)
248 switch (msi->MinorVersion)
250 case 51: str = "NT 3.51"; break;
251 default: str = "3-????"; break;
255 switch (msi->MinorVersion)
257 case 0: str = (msi->PlatformId == VER_PLATFORM_WIN32_NT) ? "NT 4.0" : "95"; break;
258 case 10: str = "98"; break;
259 case 90: str = "ME"; break;
260 default: str = "5-????"; break;
264 switch (msi->MinorVersion)
266 case 0: str = "2000"; break;
267 case 1: str = "XP"; break;
268 case 2: str = "Server 2003"; break;
269 default: str = "5-????"; break;
272 default: str = "???"; break;
274 dbg_printf(" on Windows %s (%lu)\n", str, msi->BuildNumber);
275 /* FIXME CSD: msi->CSDVersionRva */
278 dbg_curr_process = dbg_add_process(&be_process_minidump_io, pid, hProc);
280 dbg_curr_process->pio_data = data;
281 dbg_set_process_name(dbg_curr_process, exec_name);
283 SymInitialize(hProc, NULL, FALSE);
285 if (MiniDumpReadDumpStream(data->mapping, ThreadListStream, &dir, &stream, &size))
287 MINIDUMP_THREAD_LIST* mtl = (MINIDUMP_THREAD_LIST*)stream;
288 MINIDUMP_THREAD* mt = &mtl->Threads[0];
290 dbg_add_thread(dbg_curr_process, mt->ThreadId, NULL,
291 (void*)(DWORD_PTR)mt->Teb);
293 /* first load ELF modules, then do the PE ones */
294 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, &dir,
297 char buffer[MAX_PATH];
299 mml = (MINIDUMP_MODULE_LIST*)stream;
300 for (i = 0, mm = &mml->Modules[0]; i < mml->NumberOfModules; i++, mm++)
302 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
303 len = WideCharToMultiByte(CP_ACP, 0, mds->Buffer,
304 mds->Length / sizeof(WCHAR),
305 name, sizeof(name) - 1, NULL, NULL);
307 if (SymFindFileInPath(hProc, NULL, name, (void*)(DWORD_PTR)mm->CheckSum,
308 0, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
309 SymLoadModule(hProc, NULL, buffer, NULL, mm->BaseOfImage, mm->SizeOfImage);
311 SymLoadModuleEx(hProc, NULL, name, NULL, mm->BaseOfImage, mm->SizeOfImage,
312 NULL, SLMFLAG_VIRTUAL);
315 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, &dir, &stream, &size))
317 mml = (MINIDUMP_MODULE_LIST*)stream;
318 for (i = 0, mm = &mml->Modules[0]; i < mml->NumberOfModules; i++, mm++)
320 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
321 len = WideCharToMultiByte(CP_ACP, 0, mds->Buffer,
322 mds->Length / sizeof(WCHAR),
323 name, sizeof(name) - 1, NULL, NULL);
325 SymLoadModule(hProc, NULL, name, NULL,
326 mm->BaseOfImage, mm->SizeOfImage);
329 if (MiniDumpReadDumpStream(data->mapping, ExceptionStream, &dir, &stream, &size))
331 MINIDUMP_EXCEPTION_STREAM* mes = (MINIDUMP_EXCEPTION_STREAM*)stream;
333 if ((dbg_curr_thread = dbg_get_thread(dbg_curr_process, mes->ThreadId)))
337 dbg_curr_tid = mes->ThreadId;
338 dbg_curr_thread->in_exception = TRUE;
339 dbg_curr_thread->excpt_record.ExceptionCode = mes->ExceptionRecord.ExceptionCode;
340 dbg_curr_thread->excpt_record.ExceptionFlags = mes->ExceptionRecord.ExceptionFlags;
341 dbg_curr_thread->excpt_record.ExceptionRecord = (void*)(DWORD_PTR)mes->ExceptionRecord.ExceptionRecord;
342 dbg_curr_thread->excpt_record.ExceptionAddress = (void*)(DWORD_PTR)mes->ExceptionRecord.ExceptionAddress;
343 dbg_curr_thread->excpt_record.NumberParameters = mes->ExceptionRecord.NumberParameters;
344 for (i = 0; i < dbg_curr_thread->excpt_record.NumberParameters; i++)
346 dbg_curr_thread->excpt_record.ExceptionInformation[i] = mes->ExceptionRecord.ExceptionInformation[i];
348 memcpy(&dbg_context, (char*)data->mapping + mes->ThreadContext.Rva,
349 min(sizeof(dbg_context), mes->ThreadContext.DataSize));
350 memory_get_current_pc(&addr);
351 stack_fetch_frames();
352 be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
354 be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
355 stack_backtrace(mes->ThreadId);
356 source_list_from_addr(&addr, 0);
362 static void cleanup(struct tgt_process_minidump_data* data)
364 if (data->mapping) UnmapViewOfFile(data->mapping);
365 if (data->hMap) CloseHandle(data->hMap);
366 if (data->hFile != INVALID_HANDLE_VALUE) CloseHandle(data->hFile);
367 HeapFree(GetProcessHeap(), 0, data);
370 static struct be_process_io be_process_minidump_io;
372 enum dbg_start minidump_reload(int argc, char* argv[])
374 struct tgt_process_minidump_data* data;
375 enum dbg_start ret = start_error_parse;
377 /* try the form <myself> minidump-file */
378 if (argc != 1) return start_error_parse;
380 WINE_TRACE("Processing Minidump file %s\n", argv[0]);
382 data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct tgt_process_minidump_data));
383 if (!data) return start_error_init;
384 data->mapping = NULL;
386 data->hFile = INVALID_HANDLE_VALUE;
388 if ((data->hFile = CreateFileA(argv[0], GENERIC_READ, FILE_SHARE_READ, NULL,
389 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
390 ((data->hMap = CreateFileMappingA(data->hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
391 ((data->mapping = MapViewOfFile(data->hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
395 if (((MINIDUMP_HEADER*)data->mapping)->Signature == MINIDUMP_SIGNATURE)
397 ret = minidump_do_reload(data);
402 dbg_printf("Unexpected fault while reading minidump %s\n", argv[0]);
407 if (ret != start_ok) cleanup(data);
411 static BOOL tgt_process_minidump_close_process(struct dbg_process* pcs, BOOL kill)
413 struct tgt_process_minidump_data* data = PRIVATE(pcs);
416 pcs->pio_data = NULL;
417 SymCleanup(pcs->handle);
418 dbg_del_process(pcs);
422 static struct be_process_io be_process_minidump_io =
424 tgt_process_minidump_close_process,
425 tgt_process_minidump_read,
426 tgt_process_minidump_write,