hvsimanagementapi: Add IIsolatedWindowsEnvironmentHostStatics stub interface.
[wine.git] / programs / winedbg / tgt_minidump.c
blob8ca2bd7e5b6b8e5177ce53a5e1f2dc5d4707e5aa
1 /*
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 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
26 #include "debugger.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "tlhelp32.h"
30 #include "wine/debug.h"
31 #include "wine/exception.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 static struct be_process_io be_process_minidump_io;
37 /* we need this function on 32bit hosts to ensure we zero out the higher DWORD
38 * stored in the minidump file (sometimes it's not cleared, or the conversion from
39 * 32bit to 64bit wide integers is done as signed, which is wrong)
40 * So we clamp on 32bit CPUs (as stored in minidump information) all addresses to
41 * keep only the lower 32 bits.
42 * FIXME: as of today, since we don't support a backend CPU which is different from
43 * CPU this process is running on, casting to (DWORD_PTR) will do just fine.
45 static inline DWORD64 get_addr64(DWORD64 addr)
47 return (DWORD_PTR)addr;
50 void minidump_write(const char* file, const EXCEPTION_RECORD* rec)
52 HANDLE hFile;
53 MINIDUMP_EXCEPTION_INFORMATION mei;
54 EXCEPTION_POINTERS ep;
56 #ifdef __x86_64__
57 if (dbg_curr_process->be_cpu->machine != IMAGE_FILE_MACHINE_AMD64)
59 FIXME("Cannot write minidump for 32-bit process using 64-bit winedbg\n");
60 return;
62 #endif
64 hFile = CreateFileA(file, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
65 FILE_ATTRIBUTE_NORMAL, NULL);
67 if (hFile == INVALID_HANDLE_VALUE) return;
69 if (rec)
71 mei.ThreadId = dbg_curr_thread->tid;
72 mei.ExceptionPointers = &ep;
73 ep.ExceptionRecord = (EXCEPTION_RECORD*)rec;
74 ep.ContextRecord = &dbg_context.ctx;
75 mei.ClientPointers = FALSE;
77 MiniDumpWriteDump(dbg_curr_process->handle, dbg_curr_process->pid,
78 hFile, MiniDumpNormal/*|MiniDumpWithDataSegs*/,
79 rec ? &mei : NULL, NULL, NULL);
80 CloseHandle(hFile);
83 #define Wine_ElfModuleListStream 0xFFF0
85 struct tgt_process_minidump_data
87 void* mapping;
88 HANDLE hFile;
89 HANDLE hMap;
92 static inline struct tgt_process_minidump_data* private_data(struct dbg_process* pcs)
94 return pcs->pio_data;
97 static BOOL tgt_process_minidump_read(HANDLE hProcess, const void* addr,
98 void* buffer, SIZE_T len, SIZE_T* rlen)
100 void* stream;
102 if (!private_data(dbg_curr_process)->mapping) return FALSE;
103 if (MiniDumpReadDumpStream(private_data(dbg_curr_process)->mapping,
104 MemoryListStream, NULL, &stream, NULL))
106 MINIDUMP_MEMORY_LIST* mml = stream;
107 MINIDUMP_MEMORY_DESCRIPTOR* mmd = mml->MemoryRanges;
108 int i, found = -1;
109 SIZE_T ilen, prev_len = 0;
111 /* There's no reason that memory ranges inside a minidump do not overlap.
112 * So be smart when looking for a given memory range (either grab a
113 * range that covers the whole requested area, or if none, the range that
114 * has the largest overlap with requested area)
116 for (i = 0; i < mml->NumberOfMemoryRanges; i++, mmd++)
118 if (get_addr64(mmd->StartOfMemoryRange) <= (DWORD_PTR)addr &&
119 (DWORD_PTR)addr < get_addr64(mmd->StartOfMemoryRange) + mmd->Memory.DataSize)
121 ilen = min(len,
122 get_addr64(mmd->StartOfMemoryRange) + mmd->Memory.DataSize - (DWORD_PTR)addr);
123 if (ilen == len) /* whole range is matched */
125 found = i;
126 prev_len = ilen;
127 break;
129 if (found == -1 || ilen > prev_len) /* partial match, keep largest one */
131 found = i;
132 prev_len = ilen;
136 if (found != -1)
138 mmd = &mml->MemoryRanges[found];
139 memcpy(buffer,
140 (char*)private_data(dbg_curr_process)->mapping + mmd->Memory.Rva + (DWORD_PTR)addr - get_addr64(mmd->StartOfMemoryRange),
141 prev_len);
142 if (rlen) *rlen = prev_len;
143 return TRUE;
146 /* FIXME: this is a dirty hack to let the last frame in a bt to work
147 * However, we need to check who's to blame, this code or the current
148 * dbghelp!StackWalk implementation
150 if ((DWORD_PTR)addr < 32)
152 memset(buffer, 0, len);
153 if (rlen) *rlen = len;
154 return TRUE;
156 return FALSE;
159 static BOOL tgt_process_minidump_write(HANDLE hProcess, void* addr,
160 const void* buffer, SIZE_T len, SIZE_T* wlen)
162 return FALSE;
165 static BOOL CALLBACK validate_file(PCWSTR name, void* user)
167 return FALSE; /* get the first file we find !! */
170 static BOOL is_pe_module_embedded(struct tgt_process_minidump_data* data,
171 MINIDUMP_MODULE* pe_mm)
173 MINIDUMP_MODULE_LIST* mml;
175 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, NULL,
176 (void**)&mml, NULL))
178 MINIDUMP_MODULE* mm;
179 unsigned i;
181 for (i = 0, mm = mml->Modules; i < mml->NumberOfModules; i++, mm++)
183 if (get_addr64(mm->BaseOfImage) <= get_addr64(pe_mm->BaseOfImage) &&
184 get_addr64(mm->BaseOfImage) + mm->SizeOfImage >= get_addr64(pe_mm->BaseOfImage) + pe_mm->SizeOfImage)
185 return TRUE;
188 return FALSE;
191 static enum dbg_start minidump_do_reload(struct tgt_process_minidump_data* data)
193 void* stream;
194 DWORD pid = 1; /* by default */
195 HANDLE hProc = (HANDLE)0x900DBAAD;
196 int i;
197 MINIDUMP_MODULE_LIST* mml;
198 MINIDUMP_MODULE* mm;
199 MINIDUMP_STRING* mds;
200 MINIDUMP_DIRECTORY* dir;
201 WCHAR exec_name[1024];
202 WCHAR nameW[1024];
203 unsigned len;
205 /* fetch PID */
206 if (MiniDumpReadDumpStream(data->mapping, MiscInfoStream, NULL, &stream, NULL))
208 MINIDUMP_MISC_INFO* mmi = stream;
209 if (mmi->Flags1 & MINIDUMP_MISC1_PROCESS_ID)
210 pid = mmi->ProcessId;
213 /* fetch executable name (it's normally the first one in module list) */
214 lstrcpyW(exec_name, L"<minidump-exec>");
216 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, NULL, &stream, NULL))
218 mml = stream;
219 if (mml->NumberOfModules)
221 WCHAR* ptr;
223 mm = mml->Modules;
224 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
225 len = mds->Length / 2;
226 memcpy(exec_name, mds->Buffer, mds->Length);
227 exec_name[len] = 0;
228 for (ptr = exec_name + len - 1; ptr >= exec_name; ptr--)
230 if (*ptr == '/' || *ptr == '\\')
232 memmove(exec_name, ptr + 1, (lstrlenW(ptr + 1) + 1) * sizeof(WCHAR));
233 break;
239 if (MiniDumpReadDumpStream(data->mapping, SystemInfoStream, &dir, &stream, NULL))
241 MINIDUMP_SYSTEM_INFO* msi = stream;
242 const char *str;
243 char tmp[128];
245 dbg_printf("WineDbg starting on minidump on pid %04lx\n", pid);
246 switch (msi->ProcessorArchitecture)
248 case PROCESSOR_ARCHITECTURE_UNKNOWN:
249 str = "Unknown";
250 break;
251 case PROCESSOR_ARCHITECTURE_INTEL:
252 strcpy(tmp, "Intel ");
253 switch (msi->ProcessorLevel)
255 case 3: str = "80386"; break;
256 case 4: str = "80486"; break;
257 case 5: str = "Pentium"; break;
258 case 6: str = "Pentium Pro/II or AMD Athlon"; break;
259 case 15: str = "Pentium 4 or AMD Athlon64"; break;
260 default: str = "???"; break;
262 strcat(tmp, str);
263 if (msi->ProcessorLevel == 3 || msi->ProcessorLevel == 4)
265 if (HIBYTE(msi->ProcessorRevision) == 0xFF)
266 sprintf(tmp + strlen(tmp), " (%c%d)",
267 'A' + ((msi->ProcessorRevision>>4)&0xf)-0x0a,
268 ((msi->ProcessorRevision&0xf)));
269 else
270 sprintf(tmp + strlen(tmp), " (%c%d)",
271 'A' + HIBYTE(msi->ProcessorRevision),
272 LOBYTE(msi->ProcessorRevision));
274 else sprintf(tmp + strlen(tmp), " (%d.%d)",
275 HIBYTE(msi->ProcessorRevision),
276 LOBYTE(msi->ProcessorRevision));
277 str = tmp;
278 break;
279 case PROCESSOR_ARCHITECTURE_MIPS:
280 str = "Mips";
281 break;
282 case PROCESSOR_ARCHITECTURE_ALPHA:
283 str = "Alpha";
284 break;
285 case PROCESSOR_ARCHITECTURE_PPC:
286 str = "PowerPC";
287 break;
288 case PROCESSOR_ARCHITECTURE_AMD64:
289 str = "X86_64";
290 break;
291 case PROCESSOR_ARCHITECTURE_ARM:
292 str = "ARM";
293 break;
294 case PROCESSOR_ARCHITECTURE_ARM64:
295 str = "ARM64";
296 break;
297 case PROCESSOR_ARCHITECTURE_MSIL:
298 str = "MSIL";
299 break;
300 case PROCESSOR_ARCHITECTURE_NEUTRAL:
301 str = "Neutral";
302 break;
303 default:
304 str = "???";
305 break;
307 dbg_printf(" %ls was running on #%d %s CPU%s",
308 exec_name, msi->NumberOfProcessors, str,
309 msi->NumberOfProcessors < 2 ? "" : "s");
310 switch (msi->MajorVersion)
312 case 3:
313 switch (msi->MinorVersion)
315 case 51: str = "NT 3.51"; break;
316 default: str = "3-????"; break;
318 break;
319 case 4:
320 switch (msi->MinorVersion)
322 case 0: str = (msi->PlatformId == VER_PLATFORM_WIN32_NT) ? "NT 4.0" : "95"; break;
323 case 10: str = "98"; break;
324 case 90: str = "ME"; break;
325 default: str = "4-????"; break;
327 break;
328 case 5:
329 switch (msi->MinorVersion)
331 case 0: str = "2000"; break;
332 case 1: str = "XP"; break;
333 case 2:
334 if (msi->ProductType == 1) str = "XP";
335 else if (msi->ProductType == 3) str = "Server 2003";
336 else str = "5-????";
337 break;
338 default: str = "5-????"; break;
340 break;
341 case 6:
342 switch (msi->MinorVersion)
344 case 0:
345 if (msi->ProductType == 1) str = "Vista";
346 else if (msi->ProductType == 3) str = "Server 2008";
347 else str = "6-????";
348 break;
349 case 1:
350 if (msi->ProductType == 1) str = "Win7";
351 else if (msi->ProductType == 3) str = "Server 2008";
352 else str = "6-????";
353 break;
354 case 2:
355 if (msi->ProductType == 1) str = "Win8";
356 else if (msi->ProductType == 3) str = "Server 2012";
357 else str = "6-????";
358 break;
359 case 3:
360 if (msi->ProductType == 1) str = "Win8.1";
361 else if (msi->ProductType == 3) str = "Server 2012 R2";
362 else str = "6-????";
363 break;
364 default: str = "6-????"; break;
366 break;
367 case 10:
368 switch (msi->MinorVersion)
370 case 0:
371 if (msi->ProductType == 1) str = "Win10";
372 else str = "10-????";
373 break;
374 default: str = "10-????"; break;
376 break;
377 default: str = "???"; break;
379 dbg_printf(" on Windows %s (%u)\n", str, msi->BuildNumber);
380 /* FIXME CSD: msi->CSDVersionRva */
382 if (sizeof(MINIDUMP_SYSTEM_INFO) + 4 > dir->Location.DataSize &&
383 msi->CSDVersionRva >= dir->Location.Rva + sizeof(MINIDUMP_SYSTEM_INFO) + 4)
385 const char* code = (const char*)stream + sizeof(MINIDUMP_SYSTEM_INFO);
386 const DWORD* wes;
388 if (code[0] == 'W' && code[1] == 'I' && code[2] == 'N' && code[3] == 'E' &&
389 *(wes = (const DWORD*)(code += 4)) >= 3)
391 /* assume we have wine extensions */
392 dbg_printf(" [on %s, on top of %s (%s)]\n",
393 code + wes[1], code + wes[2], code + wes[3]);
398 dbg_curr_process = dbg_add_process(&be_process_minidump_io, pid, hProc);
399 dbg_curr_pid = pid;
400 dbg_curr_process->pio_data = data;
401 dbg_set_process_name(dbg_curr_process, exec_name);
403 dbg_init(hProc, NULL, FALSE);
405 if (MiniDumpReadDumpStream(data->mapping, ThreadListStream, NULL, &stream, NULL))
407 MINIDUMP_THREAD_LIST* mtl = stream;
408 ULONG i;
410 for (i = 0; i < mtl->NumberOfThreads; i++)
412 dbg_add_thread(dbg_curr_process, mtl->Threads[i].ThreadId, NULL,
413 (void*)(DWORD_PTR)get_addr64(mtl->Threads[i].Teb));
416 /* first load ELF modules, then do the PE ones */
417 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, NULL,
418 &stream, NULL))
420 WCHAR buffer[MAX_PATH];
422 mml = stream;
423 for (i = 0, mm = mml->Modules; i < mml->NumberOfModules; i++, mm++)
425 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
426 memcpy(nameW, mds->Buffer, mds->Length);
427 nameW[mds->Length / sizeof(WCHAR)] = 0;
428 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->CheckSum,
429 0, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
430 dbg_load_module(hProc, NULL, buffer, get_addr64(mm->BaseOfImage),
431 mm->SizeOfImage);
432 else
433 SymLoadModuleExW(hProc, NULL, nameW, NULL, get_addr64(mm->BaseOfImage),
434 mm->SizeOfImage, NULL, SLMFLAG_VIRTUAL);
437 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, NULL, &stream, NULL))
439 WCHAR buffer[MAX_PATH];
441 mml = stream;
442 for (i = 0, mm = mml->Modules; i < mml->NumberOfModules; i++, mm++)
444 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
445 memcpy(nameW, mds->Buffer, mds->Length);
446 nameW[mds->Length / sizeof(WCHAR)] = 0;
447 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->TimeDateStamp,
448 mm->SizeOfImage, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
449 dbg_load_module(hProc, NULL, buffer, get_addr64(mm->BaseOfImage),
450 mm->SizeOfImage);
451 else if (is_pe_module_embedded(data, mm))
452 dbg_load_module(hProc, NULL, nameW, get_addr64(mm->BaseOfImage),
453 mm->SizeOfImage);
454 else
455 SymLoadModuleExW(hProc, NULL, nameW, NULL, get_addr64(mm->BaseOfImage),
456 mm->SizeOfImage, NULL, SLMFLAG_VIRTUAL);
459 if (MiniDumpReadDumpStream(data->mapping, ExceptionStream, NULL, &stream, NULL))
461 MINIDUMP_EXCEPTION_STREAM* mes = stream;
463 if ((dbg_curr_thread = dbg_get_thread(dbg_curr_process, mes->ThreadId)))
465 ADDRESS64 addr;
467 dbg_curr_tid = mes->ThreadId;
468 dbg_curr_thread->in_exception = TRUE;
469 dbg_curr_thread->excpt_record.ExceptionCode = mes->ExceptionRecord.ExceptionCode;
470 dbg_curr_thread->excpt_record.ExceptionFlags = mes->ExceptionRecord.ExceptionFlags;
471 dbg_curr_thread->excpt_record.ExceptionRecord = (void*)(DWORD_PTR)get_addr64(mes->ExceptionRecord.ExceptionRecord);
472 dbg_curr_thread->excpt_record.ExceptionAddress = (void*)(DWORD_PTR)get_addr64(mes->ExceptionRecord.ExceptionAddress);
473 dbg_curr_thread->excpt_record.NumberParameters = mes->ExceptionRecord.NumberParameters;
474 for (i = 0; i < dbg_curr_thread->excpt_record.NumberParameters; i++)
476 dbg_curr_thread->excpt_record.ExceptionInformation[i] = mes->ExceptionRecord.ExceptionInformation[i];
478 memcpy(&dbg_context, (char*)data->mapping + mes->ThreadContext.Rva,
479 min(sizeof(dbg_context), mes->ThreadContext.DataSize));
480 memory_get_current_pc(&addr);
481 stack_fetch_frames(&dbg_context);
482 dbg_curr_process->be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
483 stack_info(-1);
484 dbg_curr_process->be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
485 stack_backtrace(mes->ThreadId);
486 source_list_from_addr(&addr, 0);
489 return start_ok;
492 static void cleanup(struct tgt_process_minidump_data* data)
494 if (data->mapping) UnmapViewOfFile(data->mapping);
495 if (data->hMap) CloseHandle(data->hMap);
496 if (data->hFile != INVALID_HANDLE_VALUE) CloseHandle(data->hFile);
497 free(data);
500 static struct be_process_io be_process_minidump_io;
502 enum dbg_start minidump_reload(int argc, char* argv[])
504 struct tgt_process_minidump_data* data;
505 enum dbg_start ret = start_error_parse;
507 /* try the form <myself> minidump-file */
508 if (argc != 1) return start_error_parse;
510 WINE_TRACE("Processing Minidump file %s\n", argv[0]);
512 data = malloc(sizeof(struct tgt_process_minidump_data));
513 if (!data) return start_error_init;
514 data->mapping = NULL;
515 data->hMap = NULL;
516 data->hFile = INVALID_HANDLE_VALUE;
518 if ((data->hFile = CreateFileA(argv[0], GENERIC_READ, FILE_SHARE_READ, NULL,
519 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
520 ((data->hMap = CreateFileMappingA(data->hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
521 ((data->mapping = MapViewOfFile(data->hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
523 __TRY
525 if (((MINIDUMP_HEADER*)data->mapping)->Signature == MINIDUMP_SIGNATURE)
527 ret = minidump_do_reload(data);
530 __EXCEPT_PAGE_FAULT
532 dbg_printf("Unexpected fault while reading minidump %s\n", argv[0]);
533 dbg_curr_pid = 0;
535 __ENDTRY;
537 if (ret != start_ok) cleanup(data);
538 return ret;
541 static BOOL tgt_process_minidump_close_process(struct dbg_process* pcs, BOOL kill)
543 struct tgt_process_minidump_data* data = private_data(pcs);
545 cleanup(data);
546 pcs->pio_data = NULL;
547 SymCleanup(pcs->handle);
548 dbg_del_process(pcs);
549 return TRUE;
552 static BOOL tgt_process_minidump_get_selector(HANDLE hThread, DWORD sel, LDT_ENTRY* le)
554 /* so far, pretend all selectors are valid, and mapped to a 32bit flat address space */
555 memset(le, 0, sizeof(*le));
556 le->HighWord.Bits.Default_Big = 1;
557 return TRUE;
560 static struct be_process_io be_process_minidump_io =
562 tgt_process_minidump_close_process,
563 tgt_process_minidump_read,
564 tgt_process_minidump_write,
565 tgt_process_minidump_get_selector,