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
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
)
53 MINIDUMP_EXCEPTION_INFORMATION mei
;
54 EXCEPTION_POINTERS ep
;
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");
64 hFile
= CreateFileA(file
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
65 FILE_ATTRIBUTE_NORMAL
, NULL
);
67 if (hFile
== INVALID_HANDLE_VALUE
) return;
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
);
83 #define Wine_ElfModuleListStream 0xFFF0
85 struct tgt_process_minidump_data
92 static inline struct tgt_process_minidump_data
* private_data(struct dbg_process
* pcs
)
97 static BOOL
tgt_process_minidump_read(HANDLE hProcess
, const void* addr
,
98 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
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
;
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
)
122 get_addr64(mmd
->StartOfMemoryRange
) + mmd
->Memory
.DataSize
- (DWORD_PTR
)addr
);
123 if (ilen
== len
) /* whole range is matched */
129 if (found
== -1 || ilen
> prev_len
) /* partial match, keep largest one */
138 mmd
= &mml
->MemoryRanges
[found
];
140 (char*)private_data(dbg_curr_process
)->mapping
+ mmd
->Memory
.Rva
+ (DWORD_PTR
)addr
- get_addr64(mmd
->StartOfMemoryRange
),
142 if (rlen
) *rlen
= prev_len
;
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
;
159 static BOOL
tgt_process_minidump_write(HANDLE hProcess
, void* addr
,
160 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
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
,
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
)
191 static enum dbg_start
minidump_do_reload(struct tgt_process_minidump_data
* data
)
194 DWORD pid
= 1; /* by default */
195 HANDLE hProc
= (HANDLE
)0x900DBAAD;
197 MINIDUMP_MODULE_LIST
* mml
;
199 MINIDUMP_STRING
* mds
;
200 MINIDUMP_DIRECTORY
* dir
;
201 WCHAR exec_name
[1024];
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
))
219 if (mml
->NumberOfModules
)
224 mds
= (MINIDUMP_STRING
*)((char*)data
->mapping
+ mm
->ModuleNameRva
);
225 len
= mds
->Length
/ 2;
226 memcpy(exec_name
, mds
->Buffer
, mds
->Length
);
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
));
239 if (MiniDumpReadDumpStream(data
->mapping
, SystemInfoStream
, &dir
, &stream
, NULL
))
241 MINIDUMP_SYSTEM_INFO
* msi
= stream
;
245 dbg_printf("WineDbg starting on minidump on pid %04lx\n", pid
);
246 switch (msi
->ProcessorArchitecture
)
248 case PROCESSOR_ARCHITECTURE_UNKNOWN
:
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;
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)));
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
));
279 case PROCESSOR_ARCHITECTURE_MIPS
:
282 case PROCESSOR_ARCHITECTURE_ALPHA
:
285 case PROCESSOR_ARCHITECTURE_PPC
:
288 case PROCESSOR_ARCHITECTURE_AMD64
:
291 case PROCESSOR_ARCHITECTURE_ARM
:
294 case PROCESSOR_ARCHITECTURE_ARM64
:
297 case PROCESSOR_ARCHITECTURE_MSIL
:
300 case PROCESSOR_ARCHITECTURE_NEUTRAL
:
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
)
313 switch (msi
->MinorVersion
)
315 case 51: str
= "NT 3.51"; break;
316 default: str
= "3-????"; break;
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;
329 switch (msi
->MinorVersion
)
331 case 0: str
= "2000"; break;
332 case 1: str
= "XP"; break;
334 if (msi
->ProductType
== 1) str
= "XP";
335 else if (msi
->ProductType
== 3) str
= "Server 2003";
338 default: str
= "5-????"; break;
342 switch (msi
->MinorVersion
)
345 if (msi
->ProductType
== 1) str
= "Vista";
346 else if (msi
->ProductType
== 3) str
= "Server 2008";
350 if (msi
->ProductType
== 1) str
= "Win7";
351 else if (msi
->ProductType
== 3) str
= "Server 2008";
355 if (msi
->ProductType
== 1) str
= "Win8";
356 else if (msi
->ProductType
== 3) str
= "Server 2012";
360 if (msi
->ProductType
== 1) str
= "Win8.1";
361 else if (msi
->ProductType
== 3) str
= "Server 2012 R2";
364 default: str
= "6-????"; break;
368 switch (msi
->MinorVersion
)
371 if (msi
->ProductType
== 1) str
= "Win10";
372 else str
= "10-????";
374 default: str
= "10-????"; 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
);
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
);
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
;
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
,
420 WCHAR buffer
[MAX_PATH
];
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
),
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
];
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
),
451 else if (is_pe_module_embedded(data
, mm
))
452 dbg_load_module(hProc
, NULL
, nameW
, get_addr64(mm
->BaseOfImage
),
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
)))
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);
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);
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
);
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
;
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
))
525 if (((MINIDUMP_HEADER
*)data
->mapping
)->Signature
== MINIDUMP_SIGNATURE
)
527 ret
= minidump_do_reload(data
);
532 dbg_printf("Unexpected fault while reading minidump %s\n", argv
[0]);
537 if (ret
!= start_ok
) cleanup(data
);
541 static BOOL
tgt_process_minidump_close_process(struct dbg_process
* pcs
, BOOL kill
)
543 struct tgt_process_minidump_data
* data
= private_data(pcs
);
546 pcs
->pio_data
= NULL
;
547 SymCleanup(pcs
->handle
);
548 dbg_del_process(pcs
);
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;
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
,