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
25 #include "wine/port.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
)
59 MINIDUMP_EXCEPTION_INFORMATION mei
;
60 EXCEPTION_POINTERS ep
;
62 hFile
= CreateFileA(file
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
63 FILE_ATTRIBUTE_NORMAL
, NULL
);
65 if (hFile
== INVALID_HANDLE_VALUE
) return;
69 mei
.ThreadId
= dbg_curr_thread
->tid
;
70 mei
.ExceptionPointers
= &ep
;
71 ep
.ExceptionRecord
= (EXCEPTION_RECORD
*)rec
;
72 ep
.ContextRecord
= &dbg_context
;
73 mei
.ClientPointers
= FALSE
;
75 MiniDumpWriteDump(dbg_curr_process
->handle
, dbg_curr_process
->pid
,
76 hFile
, MiniDumpNormal
/*|MiniDumpWithDataSegs*/,
77 rec
? &mei
: NULL
, NULL
, NULL
);
81 #define Wine_ElfModuleListStream 0xFFF0
83 struct tgt_process_minidump_data
90 static inline struct tgt_process_minidump_data
* private_data(struct dbg_process
* pcs
)
95 static BOOL
tgt_process_minidump_read(HANDLE hProcess
, const void* addr
,
96 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
100 if (!private_data(dbg_curr_process
)->mapping
) return FALSE
;
101 if (MiniDumpReadDumpStream(private_data(dbg_curr_process
)->mapping
,
102 MemoryListStream
, NULL
, &stream
, NULL
))
104 MINIDUMP_MEMORY_LIST
* mml
= stream
;
105 MINIDUMP_MEMORY_DESCRIPTOR
* mmd
= &mml
->MemoryRanges
[0];
108 for (i
= 0; i
< mml
->NumberOfMemoryRanges
; i
++, mmd
++)
110 if (get_addr64(mmd
->StartOfMemoryRange
) <= (DWORD_PTR
)addr
&&
111 (DWORD_PTR
)addr
< get_addr64(mmd
->StartOfMemoryRange
) + mmd
->Memory
.DataSize
)
114 get_addr64(mmd
->StartOfMemoryRange
) + mmd
->Memory
.DataSize
- (DWORD_PTR
)addr
);
116 (char*)private_data(dbg_curr_process
)->mapping
+ mmd
->Memory
.Rva
+ (DWORD_PTR
)addr
- get_addr64(mmd
->StartOfMemoryRange
),
118 if (rlen
) *rlen
= len
;
123 /* FIXME: this is a dirty hack to let the last frame in a bt to work
124 * However, we need to check who's to blame, this code or the current
125 * dbghelp!StackWalk implementation
127 if ((DWORD_PTR
)addr
< 32)
129 memset(buffer
, 0, len
);
130 if (rlen
) *rlen
= len
;
136 static BOOL
tgt_process_minidump_write(HANDLE hProcess
, void* addr
,
137 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
142 static BOOL CALLBACK
validate_file(PCWSTR name
, void* user
)
144 return FALSE
; /* get the first file we find !! */
147 static BOOL
is_pe_module_embedded(struct tgt_process_minidump_data
* data
,
148 MINIDUMP_MODULE
* pe_mm
)
150 MINIDUMP_MODULE_LIST
* mml
;
152 if (MiniDumpReadDumpStream(data
->mapping
, Wine_ElfModuleListStream
, NULL
,
158 for (i
= 0, mm
= &mml
->Modules
[0]; i
< mml
->NumberOfModules
; i
++, mm
++)
160 if (get_addr64(mm
->BaseOfImage
) <= get_addr64(pe_mm
->BaseOfImage
) &&
161 get_addr64(mm
->BaseOfImage
) + mm
->SizeOfImage
>= get_addr64(pe_mm
->BaseOfImage
) + pe_mm
->SizeOfImage
)
168 static enum dbg_start
minidump_do_reload(struct tgt_process_minidump_data
* data
)
171 DWORD pid
= 1; /* by default */
172 HANDLE hProc
= (HANDLE
)0x900DBAAD;
174 MINIDUMP_MODULE_LIST
* mml
;
176 MINIDUMP_STRING
* mds
;
177 WCHAR exec_name
[1024];
180 static WCHAR default_exec_name
[] = {'<','m','i','n','i','d','u','m','p','-','e','x','e','c','>',0};
183 if (MiniDumpReadDumpStream(data
->mapping
, MiscInfoStream
, NULL
, &stream
, NULL
))
185 MINIDUMP_MISC_INFO
* mmi
= stream
;
186 if (mmi
->Flags1
& MINIDUMP_MISC1_PROCESS_ID
)
187 pid
= mmi
->ProcessId
;
190 /* fetch executable name (it's normally the first one in module list) */
191 lstrcpyW(exec_name
, default_exec_name
);
192 if (MiniDumpReadDumpStream(data
->mapping
, ModuleListStream
, NULL
, &stream
, NULL
))
195 if (mml
->NumberOfModules
)
199 mm
= &mml
->Modules
[0];
200 mds
= (MINIDUMP_STRING
*)((char*)data
->mapping
+ mm
->ModuleNameRva
);
201 len
= mds
->Length
/ 2;
202 memcpy(exec_name
, mds
->Buffer
, mds
->Length
);
204 for (ptr
= exec_name
+ len
- 1; ptr
>= exec_name
; ptr
--)
206 if (*ptr
== '/' || *ptr
== '\\')
208 memmove(exec_name
, ptr
+ 1, (lstrlenW(ptr
+ 1) + 1) * sizeof(WCHAR
));
215 if (MiniDumpReadDumpStream(data
->mapping
, SystemInfoStream
, NULL
, &stream
, NULL
))
217 MINIDUMP_SYSTEM_INFO
* msi
= stream
;
221 dbg_printf("WineDbg starting on minidump on pid %04x\n", pid
);
222 switch (msi
->ProcessorArchitecture
)
224 case PROCESSOR_ARCHITECTURE_UNKNOWN
:
227 case PROCESSOR_ARCHITECTURE_INTEL
:
228 strcpy(tmp
, "Intel ");
229 switch (msi
->ProcessorLevel
)
231 case 3: str
= "80386"; break;
232 case 4: str
= "80486"; break;
233 case 5: str
= "Pentium"; break;
234 case 6: str
= "Pentium Pro/II or AMD Athlon"; break;
235 case 15: str
= "Pentium 4 or AMD Athlon64"; break;
236 default: str
= "???"; break;
239 if (msi
->ProcessorLevel
== 3 || msi
->ProcessorLevel
== 4)
241 if (HIWORD(msi
->ProcessorRevision
) == 0xFF)
242 sprintf(tmp
+ strlen(tmp
), " (%c%d)",
243 'A' + HIBYTE(LOWORD(msi
->ProcessorRevision
)),
244 LOBYTE(LOWORD(msi
->ProcessorRevision
)));
246 sprintf(tmp
+ strlen(tmp
), " (%c%d)",
247 'A' + HIWORD(msi
->ProcessorRevision
),
248 LOWORD(msi
->ProcessorRevision
));
250 else sprintf(tmp
+ strlen(tmp
), " (%d.%d)",
251 HIWORD(msi
->ProcessorRevision
),
252 LOWORD(msi
->ProcessorRevision
));
255 case PROCESSOR_ARCHITECTURE_MIPS
:
258 case PROCESSOR_ARCHITECTURE_ALPHA
:
261 case PROCESSOR_ARCHITECTURE_PPC
:
264 case PROCESSOR_ARCHITECTURE_AMD64
:
271 dbg_printf(" %s was running on #%d %s CPU%s",
272 dbg_W2A(exec_name
, -1), msi
->u
.s
.NumberOfProcessors
, str
,
273 msi
->u
.s
.NumberOfProcessors
< 2 ? "" : "s");
274 switch (msi
->MajorVersion
)
277 switch (msi
->MinorVersion
)
279 case 51: str
= "NT 3.51"; break;
280 default: str
= "3-????"; break;
284 switch (msi
->MinorVersion
)
286 case 0: str
= (msi
->PlatformId
== VER_PLATFORM_WIN32_NT
) ? "NT 4.0" : "95"; break;
287 case 10: str
= "98"; break;
288 case 90: str
= "ME"; break;
289 default: str
= "5-????"; break;
293 switch (msi
->MinorVersion
)
295 case 0: str
= "2000"; break;
296 case 1: str
= "XP"; break;
297 case 2: str
= "Server 2003"; break;
298 default: str
= "5-????"; break;
301 default: str
= "???"; break;
303 dbg_printf(" on Windows %s (%u)\n", str
, msi
->BuildNumber
);
304 /* FIXME CSD: msi->CSDVersionRva */
307 dbg_curr_process
= dbg_add_process(&be_process_minidump_io
, pid
, hProc
);
309 dbg_curr_process
->pio_data
= data
;
310 dbg_set_process_name(dbg_curr_process
, exec_name
);
312 dbg_init(hProc
, NULL
, FALSE
);
314 if (MiniDumpReadDumpStream(data
->mapping
, ThreadListStream
, NULL
, &stream
, NULL
))
316 MINIDUMP_THREAD_LIST
* mtl
= stream
;
319 for (i
= 0; i
< mtl
->NumberOfThreads
; i
++)
321 dbg_add_thread(dbg_curr_process
, mtl
->Threads
[i
].ThreadId
, NULL
,
322 (void*)(DWORD_PTR
)get_addr64(mtl
->Threads
[i
].Teb
));
325 /* first load ELF modules, then do the PE ones */
326 if (MiniDumpReadDumpStream(data
->mapping
, Wine_ElfModuleListStream
, NULL
,
329 WCHAR buffer
[MAX_PATH
];
332 for (i
= 0, mm
= &mml
->Modules
[0]; i
< mml
->NumberOfModules
; i
++, mm
++)
334 mds
= (MINIDUMP_STRING
*)((char*)data
->mapping
+ mm
->ModuleNameRva
);
335 memcpy(nameW
, mds
->Buffer
, mds
->Length
);
336 nameW
[mds
->Length
/ sizeof(WCHAR
)] = 0;
337 if (SymFindFileInPathW(hProc
, NULL
, nameW
, (void*)(DWORD_PTR
)mm
->CheckSum
,
338 0, 0, SSRVOPT_DWORD
, buffer
, validate_file
, NULL
))
339 dbg_load_module(hProc
, NULL
, buffer
, get_addr64(mm
->BaseOfImage
),
342 SymLoadModuleExW(hProc
, NULL
, nameW
, NULL
, get_addr64(mm
->BaseOfImage
),
343 mm
->SizeOfImage
, NULL
, SLMFLAG_VIRTUAL
);
346 if (MiniDumpReadDumpStream(data
->mapping
, ModuleListStream
, NULL
, &stream
, NULL
))
348 WCHAR buffer
[MAX_PATH
];
351 for (i
= 0, mm
= &mml
->Modules
[0]; i
< mml
->NumberOfModules
; i
++, mm
++)
353 mds
= (MINIDUMP_STRING
*)((char*)data
->mapping
+ mm
->ModuleNameRva
);
354 memcpy(nameW
, mds
->Buffer
, mds
->Length
);
355 nameW
[mds
->Length
/ sizeof(WCHAR
)] = 0;
356 if (SymFindFileInPathW(hProc
, NULL
, nameW
, (void*)(DWORD_PTR
)mm
->TimeDateStamp
,
357 mm
->SizeOfImage
, 0, SSRVOPT_DWORD
, buffer
, validate_file
, NULL
))
358 dbg_load_module(hProc
, NULL
, buffer
, get_addr64(mm
->BaseOfImage
),
360 else if (is_pe_module_embedded(data
, mm
))
361 dbg_load_module(hProc
, NULL
, nameW
, get_addr64(mm
->BaseOfImage
),
364 SymLoadModuleExW(hProc
, NULL
, nameW
, NULL
, get_addr64(mm
->BaseOfImage
),
365 mm
->SizeOfImage
, NULL
, SLMFLAG_VIRTUAL
);
368 if (MiniDumpReadDumpStream(data
->mapping
, ExceptionStream
, NULL
, &stream
, NULL
))
370 MINIDUMP_EXCEPTION_STREAM
* mes
= stream
;
372 if ((dbg_curr_thread
= dbg_get_thread(dbg_curr_process
, mes
->ThreadId
)))
376 dbg_curr_tid
= mes
->ThreadId
;
377 dbg_curr_thread
->in_exception
= TRUE
;
378 dbg_curr_thread
->excpt_record
.ExceptionCode
= mes
->ExceptionRecord
.ExceptionCode
;
379 dbg_curr_thread
->excpt_record
.ExceptionFlags
= mes
->ExceptionRecord
.ExceptionFlags
;
380 dbg_curr_thread
->excpt_record
.ExceptionRecord
= (void*)(DWORD_PTR
)get_addr64(mes
->ExceptionRecord
.ExceptionRecord
);
381 dbg_curr_thread
->excpt_record
.ExceptionAddress
= (void*)(DWORD_PTR
)get_addr64(mes
->ExceptionRecord
.ExceptionAddress
);
382 dbg_curr_thread
->excpt_record
.NumberParameters
= mes
->ExceptionRecord
.NumberParameters
;
383 for (i
= 0; i
< dbg_curr_thread
->excpt_record
.NumberParameters
; i
++)
385 dbg_curr_thread
->excpt_record
.ExceptionInformation
[i
] = mes
->ExceptionRecord
.ExceptionInformation
[i
];
387 memcpy(&dbg_context
, (char*)data
->mapping
+ mes
->ThreadContext
.Rva
,
388 min(sizeof(dbg_context
), mes
->ThreadContext
.DataSize
));
389 memory_get_current_pc(&addr
);
390 stack_fetch_frames(&dbg_context
);
391 be_cpu
->print_context(dbg_curr_thread
->handle
, &dbg_context
, 0);
393 be_cpu
->print_segment_info(dbg_curr_thread
->handle
, &dbg_context
);
394 stack_backtrace(mes
->ThreadId
);
395 source_list_from_addr(&addr
, 0);
401 static void cleanup(struct tgt_process_minidump_data
* data
)
403 if (data
->mapping
) UnmapViewOfFile(data
->mapping
);
404 if (data
->hMap
) CloseHandle(data
->hMap
);
405 if (data
->hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(data
->hFile
);
406 HeapFree(GetProcessHeap(), 0, data
);
409 static struct be_process_io be_process_minidump_io
;
411 enum dbg_start
minidump_reload(int argc
, char* argv
[])
413 struct tgt_process_minidump_data
* data
;
414 enum dbg_start ret
= start_error_parse
;
416 /* try the form <myself> minidump-file */
417 if (argc
!= 1) return start_error_parse
;
419 WINE_TRACE("Processing Minidump file %s\n", argv
[0]);
421 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct tgt_process_minidump_data
));
422 if (!data
) return start_error_init
;
423 data
->mapping
= NULL
;
425 data
->hFile
= INVALID_HANDLE_VALUE
;
427 if ((data
->hFile
= CreateFileA(argv
[0], GENERIC_READ
, FILE_SHARE_READ
, NULL
,
428 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
&&
429 ((data
->hMap
= CreateFileMappingA(data
->hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0) &&
430 ((data
->mapping
= MapViewOfFile(data
->hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
))
434 if (((MINIDUMP_HEADER
*)data
->mapping
)->Signature
== MINIDUMP_SIGNATURE
)
436 ret
= minidump_do_reload(data
);
441 dbg_printf("Unexpected fault while reading minidump %s\n", argv
[0]);
446 if (ret
!= start_ok
) cleanup(data
);
450 static BOOL
tgt_process_minidump_close_process(struct dbg_process
* pcs
, BOOL kill
)
452 struct tgt_process_minidump_data
* data
= private_data(pcs
);
455 pcs
->pio_data
= NULL
;
456 SymCleanup(pcs
->handle
);
457 dbg_del_process(pcs
);
461 static BOOL
tgt_process_minidump_get_selector(HANDLE hThread
, DWORD sel
, LDT_ENTRY
* le
)
463 /* so far, pretend all selectors are valid, and mapped to a 32bit flat address space */
464 memset(le
, 0, sizeof(*le
));
465 le
->HighWord
.Bits
.Default_Big
= 1;
469 static struct be_process_io be_process_minidump_io
=
471 tgt_process_minidump_close_process
,
472 tgt_process_minidump_read
,
473 tgt_process_minidump_write
,
474 tgt_process_minidump_get_selector
,