mf/session: Complete session_start command even if already started.
[wine.git] / programs / winedbg / tgt_minidump.c
blob95af7a601e5dc351b23e24987b65ec062842a17b
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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdarg.h>
32 #include "debugger.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "tlhelp32.h"
36 #include "wine/debug.h"
37 #include "wine/exception.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
41 static struct be_process_io be_process_minidump_io;
43 /* we need this function on 32bit hosts to ensure we zero out the higher DWORD
44 * stored in the minidump file (sometimes it's not cleared, or the conversion from
45 * 32bit to 64bit wide integers is done as signed, which is wrong)
46 * So we clamp on 32bit CPUs (as stored in minidump information) all addresses to
47 * keep only the lower 32 bits.
48 * FIXME: as of today, since we don't support a backend CPU which is different from
49 * CPU this process is running on, casting to (DWORD_PTR) will do just fine.
51 static inline DWORD64 get_addr64(DWORD64 addr)
53 return (DWORD_PTR)addr;
56 void minidump_write(const char* file, const EXCEPTION_RECORD* rec)
58 HANDLE hFile;
59 MINIDUMP_EXCEPTION_INFORMATION mei;
60 EXCEPTION_POINTERS ep;
62 #ifdef __x86_64__
63 if (dbg_curr_process->be_cpu->machine != IMAGE_FILE_MACHINE_AMD64)
65 FIXME("Cannot write minidump for 32-bit process using 64-bit winedbg\n");
66 return;
68 #endif
70 hFile = CreateFileA(file, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
71 FILE_ATTRIBUTE_NORMAL, NULL);
73 if (hFile == INVALID_HANDLE_VALUE) return;
75 if (rec)
77 mei.ThreadId = dbg_curr_thread->tid;
78 mei.ExceptionPointers = &ep;
79 ep.ExceptionRecord = (EXCEPTION_RECORD*)rec;
80 ep.ContextRecord = &dbg_context.ctx;
81 mei.ClientPointers = FALSE;
83 MiniDumpWriteDump(dbg_curr_process->handle, dbg_curr_process->pid,
84 hFile, MiniDumpNormal/*|MiniDumpWithDataSegs*/,
85 rec ? &mei : NULL, NULL, NULL);
86 CloseHandle(hFile);
89 #define Wine_ElfModuleListStream 0xFFF0
91 struct tgt_process_minidump_data
93 void* mapping;
94 HANDLE hFile;
95 HANDLE hMap;
98 static inline struct tgt_process_minidump_data* private_data(struct dbg_process* pcs)
100 return pcs->pio_data;
103 static BOOL tgt_process_minidump_read(HANDLE hProcess, const void* addr,
104 void* buffer, SIZE_T len, SIZE_T* rlen)
106 void* stream;
108 if (!private_data(dbg_curr_process)->mapping) return FALSE;
109 if (MiniDumpReadDumpStream(private_data(dbg_curr_process)->mapping,
110 MemoryListStream, NULL, &stream, NULL))
112 MINIDUMP_MEMORY_LIST* mml = stream;
113 MINIDUMP_MEMORY_DESCRIPTOR* mmd = mml->MemoryRanges;
114 int i, found = -1;
115 SIZE_T ilen, prev_len = 0;
117 /* There's no reason that memory ranges inside a minidump do not overlap.
118 * So be smart when looking for a given memory range (either grab a
119 * range that covers the whole requested area, or if none, the range that
120 * has the largest overlap with requested area)
122 for (i = 0; i < mml->NumberOfMemoryRanges; i++, mmd++)
124 if (get_addr64(mmd->StartOfMemoryRange) <= (DWORD_PTR)addr &&
125 (DWORD_PTR)addr < get_addr64(mmd->StartOfMemoryRange) + mmd->Memory.DataSize)
127 ilen = min(len,
128 get_addr64(mmd->StartOfMemoryRange) + mmd->Memory.DataSize - (DWORD_PTR)addr);
129 if (ilen == len) /* whole range is matched */
131 found = i;
132 prev_len = ilen;
133 break;
135 if (found == -1 || ilen > prev_len) /* partial match, keep largest one */
137 found = i;
138 prev_len = ilen;
142 if (found != -1)
144 mmd = &mml->MemoryRanges[found];
145 memcpy(buffer,
146 (char*)private_data(dbg_curr_process)->mapping + mmd->Memory.Rva + (DWORD_PTR)addr - get_addr64(mmd->StartOfMemoryRange),
147 prev_len);
148 if (rlen) *rlen = prev_len;
149 return TRUE;
152 /* FIXME: this is a dirty hack to let the last frame in a bt to work
153 * However, we need to check who's to blame, this code or the current
154 * dbghelp!StackWalk implementation
156 if ((DWORD_PTR)addr < 32)
158 memset(buffer, 0, len);
159 if (rlen) *rlen = len;
160 return TRUE;
162 return FALSE;
165 static BOOL tgt_process_minidump_write(HANDLE hProcess, void* addr,
166 const void* buffer, SIZE_T len, SIZE_T* wlen)
168 return FALSE;
171 static BOOL CALLBACK validate_file(PCWSTR name, void* user)
173 return FALSE; /* get the first file we find !! */
176 static BOOL is_pe_module_embedded(struct tgt_process_minidump_data* data,
177 MINIDUMP_MODULE* pe_mm)
179 MINIDUMP_MODULE_LIST* mml;
181 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, NULL,
182 (void**)&mml, NULL))
184 MINIDUMP_MODULE* mm;
185 unsigned i;
187 for (i = 0, mm = mml->Modules; i < mml->NumberOfModules; i++, mm++)
189 if (get_addr64(mm->BaseOfImage) <= get_addr64(pe_mm->BaseOfImage) &&
190 get_addr64(mm->BaseOfImage) + mm->SizeOfImage >= get_addr64(pe_mm->BaseOfImage) + pe_mm->SizeOfImage)
191 return TRUE;
194 return FALSE;
197 static enum dbg_start minidump_do_reload(struct tgt_process_minidump_data* data)
199 void* stream;
200 DWORD pid = 1; /* by default */
201 HANDLE hProc = (HANDLE)0x900DBAAD;
202 int i;
203 MINIDUMP_MODULE_LIST* mml;
204 MINIDUMP_MODULE* mm;
205 MINIDUMP_STRING* mds;
206 MINIDUMP_DIRECTORY* dir;
207 WCHAR exec_name[1024];
208 WCHAR nameW[1024];
209 unsigned len;
210 static const WCHAR default_exec_name[] = {'<','m','i','n','i','d','u','m','p','-','e','x','e','c','>',0};
212 /* fetch PID */
213 if (MiniDumpReadDumpStream(data->mapping, MiscInfoStream, NULL, &stream, NULL))
215 MINIDUMP_MISC_INFO* mmi = stream;
216 if (mmi->Flags1 & MINIDUMP_MISC1_PROCESS_ID)
217 pid = mmi->ProcessId;
220 /* fetch executable name (it's normally the first one in module list) */
221 lstrcpyW(exec_name, default_exec_name);
222 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, NULL, &stream, NULL))
224 mml = stream;
225 if (mml->NumberOfModules)
227 WCHAR* ptr;
229 mm = mml->Modules;
230 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
231 len = mds->Length / 2;
232 memcpy(exec_name, mds->Buffer, mds->Length);
233 exec_name[len] = 0;
234 for (ptr = exec_name + len - 1; ptr >= exec_name; ptr--)
236 if (*ptr == '/' || *ptr == '\\')
238 memmove(exec_name, ptr + 1, (lstrlenW(ptr + 1) + 1) * sizeof(WCHAR));
239 break;
245 if (MiniDumpReadDumpStream(data->mapping, SystemInfoStream, &dir, &stream, NULL))
247 MINIDUMP_SYSTEM_INFO* msi = stream;
248 const char *str;
249 char tmp[128];
251 dbg_printf("WineDbg starting on minidump on pid %04x\n", pid);
252 switch (msi->ProcessorArchitecture)
254 case PROCESSOR_ARCHITECTURE_UNKNOWN:
255 str = "Unknown";
256 break;
257 case PROCESSOR_ARCHITECTURE_INTEL:
258 strcpy(tmp, "Intel ");
259 switch (msi->ProcessorLevel)
261 case 3: str = "80386"; break;
262 case 4: str = "80486"; break;
263 case 5: str = "Pentium"; break;
264 case 6: str = "Pentium Pro/II or AMD Athlon"; break;
265 case 15: str = "Pentium 4 or AMD Athlon64"; break;
266 default: str = "???"; break;
268 strcat(tmp, str);
269 if (msi->ProcessorLevel == 3 || msi->ProcessorLevel == 4)
271 if (HIBYTE(msi->ProcessorRevision) == 0xFF)
272 sprintf(tmp + strlen(tmp), " (%c%d)",
273 'A' + ((msi->ProcessorRevision>>4)&0xf)-0x0a,
274 ((msi->ProcessorRevision&0xf)));
275 else
276 sprintf(tmp + strlen(tmp), " (%c%d)",
277 'A' + HIBYTE(msi->ProcessorRevision),
278 LOBYTE(msi->ProcessorRevision));
280 else sprintf(tmp + strlen(tmp), " (%d.%d)",
281 HIBYTE(msi->ProcessorRevision),
282 LOBYTE(msi->ProcessorRevision));
283 str = tmp;
284 break;
285 case PROCESSOR_ARCHITECTURE_MIPS:
286 str = "Mips";
287 break;
288 case PROCESSOR_ARCHITECTURE_ALPHA:
289 str = "Alpha";
290 break;
291 case PROCESSOR_ARCHITECTURE_PPC:
292 str = "PowerPC";
293 break;
294 case PROCESSOR_ARCHITECTURE_AMD64:
295 str = "X86_64";
296 break;
297 case PROCESSOR_ARCHITECTURE_ARM:
298 str = "ARM";
299 break;
300 case PROCESSOR_ARCHITECTURE_ARM64:
301 str = "ARM64";
302 break;
303 case PROCESSOR_ARCHITECTURE_MSIL:
304 str = "MSIL";
305 break;
306 case PROCESSOR_ARCHITECTURE_NEUTRAL:
307 str = "Neutral";
308 break;
309 default:
310 str = "???";
311 break;
313 dbg_printf(" %s was running on #%d %s CPU%s",
314 dbg_W2A(exec_name, -1), msi->u.s.NumberOfProcessors, str,
315 msi->u.s.NumberOfProcessors < 2 ? "" : "s");
316 switch (msi->MajorVersion)
318 case 3:
319 switch (msi->MinorVersion)
321 case 51: str = "NT 3.51"; break;
322 default: str = "3-????"; break;
324 break;
325 case 4:
326 switch (msi->MinorVersion)
328 case 0: str = (msi->PlatformId == VER_PLATFORM_WIN32_NT) ? "NT 4.0" : "95"; break;
329 case 10: str = "98"; break;
330 case 90: str = "ME"; break;
331 default: str = "4-????"; break;
333 break;
334 case 5:
335 switch (msi->MinorVersion)
337 case 0: str = "2000"; break;
338 case 1: str = "XP"; break;
339 case 2:
340 if (msi->u.s.ProductType == 1) str = "XP";
341 else if (msi->u.s.ProductType == 3) str = "Server 2003";
342 else str = "5-????";
343 break;
344 default: str = "5-????"; break;
346 break;
347 case 6:
348 switch (msi->MinorVersion)
350 case 0:
351 if (msi->u.s.ProductType == 1) str = "Vista";
352 else if (msi->u.s.ProductType == 3) str = "Server 2008";
353 else str = "6-????";
354 break;
355 case 1:
356 if (msi->u.s.ProductType == 1) str = "Win7";
357 else if (msi->u.s.ProductType == 3) str = "Server 2008";
358 else str = "6-????";
359 break;
360 case 2:
361 if (msi->u.s.ProductType == 1) str = "Win8";
362 else if (msi->u.s.ProductType == 3) str = "Server 2012";
363 else str = "6-????";
364 break;
365 case 3:
366 if (msi->u.s.ProductType == 1) str = "Win8.1";
367 else if (msi->u.s.ProductType == 3) str = "Server 2012 R2";
368 else str = "6-????";
369 break;
370 default: str = "6-????"; break;
372 break;
373 case 10:
374 switch (msi->MinorVersion)
376 case 0:
377 if (msi->u.s.ProductType == 1) str = "Win10";
378 else str = "10-????";
379 break;
380 default: str = "10-????"; break;
382 break;
383 default: str = "???"; break;
385 dbg_printf(" on Windows %s (%u)\n", str, msi->BuildNumber);
386 /* FIXME CSD: msi->CSDVersionRva */
388 if (sizeof(MINIDUMP_SYSTEM_INFO) + 4 > dir->Location.DataSize &&
389 msi->CSDVersionRva >= dir->Location.Rva + sizeof(MINIDUMP_SYSTEM_INFO) + 4)
391 const char* code = (const char*)stream + sizeof(MINIDUMP_SYSTEM_INFO);
392 const DWORD* wes;
394 if (code[0] == 'W' && code[1] == 'I' && code[2] == 'N' && code[3] == 'E' &&
395 *(wes = (const DWORD*)(code += 4)) >= 3)
397 /* assume we have wine extensions */
398 dbg_printf(" [on %s, on top of %s (%s)]\n",
399 code + wes[1], code + wes[2], code + wes[3]);
404 dbg_curr_process = dbg_add_process(&be_process_minidump_io, pid, hProc);
405 dbg_curr_pid = pid;
406 dbg_curr_process->pio_data = data;
407 dbg_set_process_name(dbg_curr_process, exec_name);
409 dbg_init(hProc, NULL, FALSE);
411 if (MiniDumpReadDumpStream(data->mapping, ThreadListStream, NULL, &stream, NULL))
413 MINIDUMP_THREAD_LIST* mtl = stream;
414 ULONG i;
416 for (i = 0; i < mtl->NumberOfThreads; i++)
418 dbg_add_thread(dbg_curr_process, mtl->Threads[i].ThreadId, NULL,
419 (void*)(DWORD_PTR)get_addr64(mtl->Threads[i].Teb));
422 /* first load ELF modules, then do the PE ones */
423 if (MiniDumpReadDumpStream(data->mapping, Wine_ElfModuleListStream, NULL,
424 &stream, NULL))
426 WCHAR buffer[MAX_PATH];
428 mml = stream;
429 for (i = 0, mm = mml->Modules; i < mml->NumberOfModules; i++, mm++)
431 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
432 memcpy(nameW, mds->Buffer, mds->Length);
433 nameW[mds->Length / sizeof(WCHAR)] = 0;
434 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->CheckSum,
435 0, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
436 dbg_load_module(hProc, NULL, buffer, get_addr64(mm->BaseOfImage),
437 mm->SizeOfImage);
438 else
439 SymLoadModuleExW(hProc, NULL, nameW, NULL, get_addr64(mm->BaseOfImage),
440 mm->SizeOfImage, NULL, SLMFLAG_VIRTUAL);
443 if (MiniDumpReadDumpStream(data->mapping, ModuleListStream, NULL, &stream, NULL))
445 WCHAR buffer[MAX_PATH];
447 mml = stream;
448 for (i = 0, mm = mml->Modules; i < mml->NumberOfModules; i++, mm++)
450 mds = (MINIDUMP_STRING*)((char*)data->mapping + mm->ModuleNameRva);
451 memcpy(nameW, mds->Buffer, mds->Length);
452 nameW[mds->Length / sizeof(WCHAR)] = 0;
453 if (SymFindFileInPathW(hProc, NULL, nameW, (void*)(DWORD_PTR)mm->TimeDateStamp,
454 mm->SizeOfImage, 0, SSRVOPT_DWORD, buffer, validate_file, NULL))
455 dbg_load_module(hProc, NULL, buffer, get_addr64(mm->BaseOfImage),
456 mm->SizeOfImage);
457 else if (is_pe_module_embedded(data, mm))
458 dbg_load_module(hProc, NULL, nameW, get_addr64(mm->BaseOfImage),
459 mm->SizeOfImage);
460 else
461 SymLoadModuleExW(hProc, NULL, nameW, NULL, get_addr64(mm->BaseOfImage),
462 mm->SizeOfImage, NULL, SLMFLAG_VIRTUAL);
465 if (MiniDumpReadDumpStream(data->mapping, ExceptionStream, NULL, &stream, NULL))
467 MINIDUMP_EXCEPTION_STREAM* mes = stream;
469 if ((dbg_curr_thread = dbg_get_thread(dbg_curr_process, mes->ThreadId)))
471 ADDRESS64 addr;
473 dbg_curr_tid = mes->ThreadId;
474 dbg_curr_thread->in_exception = TRUE;
475 dbg_curr_thread->excpt_record.ExceptionCode = mes->ExceptionRecord.ExceptionCode;
476 dbg_curr_thread->excpt_record.ExceptionFlags = mes->ExceptionRecord.ExceptionFlags;
477 dbg_curr_thread->excpt_record.ExceptionRecord = (void*)(DWORD_PTR)get_addr64(mes->ExceptionRecord.ExceptionRecord);
478 dbg_curr_thread->excpt_record.ExceptionAddress = (void*)(DWORD_PTR)get_addr64(mes->ExceptionRecord.ExceptionAddress);
479 dbg_curr_thread->excpt_record.NumberParameters = mes->ExceptionRecord.NumberParameters;
480 for (i = 0; i < dbg_curr_thread->excpt_record.NumberParameters; i++)
482 dbg_curr_thread->excpt_record.ExceptionInformation[i] = mes->ExceptionRecord.ExceptionInformation[i];
484 memcpy(&dbg_context, (char*)data->mapping + mes->ThreadContext.Rva,
485 min(sizeof(dbg_context), mes->ThreadContext.DataSize));
486 memory_get_current_pc(&addr);
487 stack_fetch_frames(&dbg_context);
488 dbg_curr_process->be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
489 stack_info(-1);
490 dbg_curr_process->be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
491 stack_backtrace(mes->ThreadId);
492 source_list_from_addr(&addr, 0);
495 return start_ok;
498 static void cleanup(struct tgt_process_minidump_data* data)
500 if (data->mapping) UnmapViewOfFile(data->mapping);
501 if (data->hMap) CloseHandle(data->hMap);
502 if (data->hFile != INVALID_HANDLE_VALUE) CloseHandle(data->hFile);
503 HeapFree(GetProcessHeap(), 0, data);
506 static struct be_process_io be_process_minidump_io;
508 enum dbg_start minidump_reload(int argc, char* argv[])
510 struct tgt_process_minidump_data* data;
511 enum dbg_start ret = start_error_parse;
513 /* try the form <myself> minidump-file */
514 if (argc != 1) return start_error_parse;
516 WINE_TRACE("Processing Minidump file %s\n", argv[0]);
518 data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct tgt_process_minidump_data));
519 if (!data) return start_error_init;
520 data->mapping = NULL;
521 data->hMap = NULL;
522 data->hFile = INVALID_HANDLE_VALUE;
524 if ((data->hFile = CreateFileA(argv[0], GENERIC_READ, FILE_SHARE_READ, NULL,
525 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
526 ((data->hMap = CreateFileMappingA(data->hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
527 ((data->mapping = MapViewOfFile(data->hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
529 __TRY
531 if (((MINIDUMP_HEADER*)data->mapping)->Signature == MINIDUMP_SIGNATURE)
533 ret = minidump_do_reload(data);
536 __EXCEPT_PAGE_FAULT
538 dbg_printf("Unexpected fault while reading minidump %s\n", argv[0]);
539 dbg_curr_pid = 0;
541 __ENDTRY;
543 if (ret != start_ok) cleanup(data);
544 return ret;
547 static BOOL tgt_process_minidump_close_process(struct dbg_process* pcs, BOOL kill)
549 struct tgt_process_minidump_data* data = private_data(pcs);
551 cleanup(data);
552 pcs->pio_data = NULL;
553 SymCleanup(pcs->handle);
554 dbg_del_process(pcs);
555 return TRUE;
558 static BOOL tgt_process_minidump_get_selector(HANDLE hThread, DWORD sel, LDT_ENTRY* le)
560 /* so far, pretend all selectors are valid, and mapped to a 32bit flat address space */
561 memset(le, 0, sizeof(*le));
562 le->HighWord.Bits.Default_Big = 1;
563 return TRUE;
566 static struct be_process_io be_process_minidump_io =
568 tgt_process_minidump_close_process,
569 tgt_process_minidump_read,
570 tgt_process_minidump_write,
571 tgt_process_minidump_get_selector,