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 static DWORD64
get_addr64(DWORD64 addr
)
45 return addr
& 0xFFFFFFFF;
48 void minidump_write(const char* file
, const EXCEPTION_RECORD
* rec
)
51 MINIDUMP_EXCEPTION_INFORMATION mei
;
52 EXCEPTION_POINTERS ep
;
54 hFile
= CreateFile(file
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
55 FILE_ATTRIBUTE_NORMAL
, NULL
);
57 if (hFile
== INVALID_HANDLE_VALUE
) return;
61 mei
.ThreadId
= dbg_curr_thread
->tid
;
62 mei
.ExceptionPointers
= &ep
;
63 ep
.ExceptionRecord
= (EXCEPTION_RECORD
*)rec
;
64 ep
.ContextRecord
= &dbg_context
;
65 mei
.ClientPointers
= FALSE
;
67 MiniDumpWriteDump(dbg_curr_process
->handle
, dbg_curr_process
->pid
,
68 hFile
, MiniDumpNormal
/*|MiniDumpWithDataSegs*/,
69 rec
? &mei
: NULL
, NULL
, NULL
);
73 #define Wine_ElfModuleListStream 0xFFF0
75 struct tgt_process_minidump_data
82 static inline struct tgt_process_minidump_data
* private_data(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
, SIZE_T len
, SIZE_T
* rlen
)
91 MINIDUMP_DIRECTORY
* dir
;
94 if (!private_data(dbg_curr_process
)->mapping
) return FALSE
;
95 if (MiniDumpReadDumpStream(private_data(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 (get_addr64(mmd
->StartOfMemoryRange
) <= (DWORD_PTR
)addr
&&
105 (DWORD_PTR
)addr
< get_addr64(mmd
->StartOfMemoryRange
) + mmd
->Memory
.DataSize
)
108 get_addr64(mmd
->StartOfMemoryRange
) + mmd
->Memory
.DataSize
- (DWORD_PTR
)addr
);
110 (char*)private_data(dbg_curr_process
)->mapping
+ mmd
->Memory
.Rva
+ (DWORD_PTR
)addr
- get_addr64(mmd
->StartOfMemoryRange
),
112 if (rlen
) *rlen
= len
;
117 /* FIXME: this is a dirty hack to let the last frame in a bt to work
118 * However, we need to check who's to blame, this code or the current
119 * dbghelp!StackWalk implementation
121 if ((DWORD_PTR
)addr
< 32)
123 memset(buffer
, 0, len
);
124 if (rlen
) *rlen
= len
;
130 static BOOL WINAPI
tgt_process_minidump_write(HANDLE hProcess
, void* addr
,
131 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
136 static BOOL CALLBACK
validate_file(PCWSTR name
, void* user
)
138 return FALSE
; /* get the first file we find !! */
141 static BOOL
is_pe_module_embedded(struct tgt_process_minidump_data
* data
,
142 MINIDUMP_MODULE
* pe_mm
)
145 MINIDUMP_DIRECTORY
* dir
;
146 MINIDUMP_MODULE_LIST
* mml
;
148 if (MiniDumpReadDumpStream(data
->mapping
, Wine_ElfModuleListStream
, &dir
,
149 (void**)&mml
, &size
))
154 for (i
= 0, mm
= &mml
->Modules
[0]; i
< mml
->NumberOfModules
; i
++, mm
++)
156 if (get_addr64(mm
->BaseOfImage
) <= get_addr64(pe_mm
->BaseOfImage
) &&
157 get_addr64(mm
->BaseOfImage
) + mm
->SizeOfImage
>= get_addr64(pe_mm
->BaseOfImage
) + pe_mm
->SizeOfImage
)
164 static enum dbg_start
minidump_do_reload(struct tgt_process_minidump_data
* data
)
167 MINIDUMP_DIRECTORY
* dir
;
169 DWORD pid
= 1; /* by default */
170 HANDLE hProc
= (HANDLE
)0x900DBAAD;
172 MINIDUMP_MODULE_LIST
* mml
;
174 MINIDUMP_STRING
* mds
;
175 WCHAR exec_name
[1024];
178 static WCHAR default_exec_name
[] = {'<','m','i','n','i','d','u','m','p','-','e','x','e','c','>',0};
181 if (MiniDumpReadDumpStream(data
->mapping
, MiscInfoStream
, &dir
, &stream
, &size
))
183 MINIDUMP_MISC_INFO
* mmi
= (MINIDUMP_MISC_INFO
*)stream
;
184 if (mmi
->Flags1
& MINIDUMP_MISC1_PROCESS_ID
)
185 pid
= mmi
->ProcessId
;
188 /* fetch executable name (it's normally the first one in module list) */
189 lstrcpyW(exec_name
, default_exec_name
);
190 if (MiniDumpReadDumpStream(data
->mapping
, ModuleListStream
, &dir
, &stream
, &size
))
192 mml
= (MINIDUMP_MODULE_LIST
*)stream
;
193 if (mml
->NumberOfModules
)
197 mm
= &mml
->Modules
[0];
198 mds
= (MINIDUMP_STRING
*)((char*)data
->mapping
+ mm
->ModuleNameRva
);
199 len
= mds
->Length
/ 2;
200 memcpy(exec_name
, mds
->Buffer
, mds
->Length
);
202 for (ptr
= exec_name
+ len
- 1; ptr
>= exec_name
; ptr
--)
204 if (*ptr
== '/' || *ptr
== '\\')
206 memmove(exec_name
, ptr
+ 1, (lstrlenW(ptr
+ 1) + 1) * sizeof(WCHAR
));
213 if (MiniDumpReadDumpStream(data
->mapping
, SystemInfoStream
, &dir
, &stream
, &size
))
215 MINIDUMP_SYSTEM_INFO
* msi
= (MINIDUMP_SYSTEM_INFO
*)stream
;
219 dbg_printf("WineDbg starting on minidump on pid %04x\n", pid
);
220 switch (msi
->ProcessorArchitecture
)
222 case PROCESSOR_ARCHITECTURE_UNKNOWN
:
225 case PROCESSOR_ARCHITECTURE_INTEL
:
226 strcpy(tmp
, "Intel ");
227 switch (msi
->ProcessorLevel
)
229 case 3: str
= "80386"; break;
230 case 4: str
= "80486"; break;
231 case 5: str
= "Pentium"; break;
232 case 6: str
= "Pentium Pro/II or AMD Athlon"; break;
233 case 15: str
= "Pentium 4 or AMD Athlon64"; break;
234 default: str
= "???"; break;
237 if (msi
->ProcessorLevel
== 3 || msi
->ProcessorLevel
== 4)
239 if (HIWORD(msi
->ProcessorRevision
) == 0xFF)
240 sprintf(tmp
+ strlen(tmp
), " (%c%d)",
241 'A' + HIBYTE(LOWORD(msi
->ProcessorRevision
)),
242 LOBYTE(LOWORD(msi
->ProcessorRevision
)));
244 sprintf(tmp
+ strlen(tmp
), " (%c%d)",
245 'A' + HIWORD(msi
->ProcessorRevision
),
246 LOWORD(msi
->ProcessorRevision
));
248 else sprintf(tmp
+ strlen(tmp
), " (%d.%d)",
249 HIWORD(msi
->ProcessorRevision
),
250 LOWORD(msi
->ProcessorRevision
));
253 case PROCESSOR_ARCHITECTURE_MIPS
:
256 case PROCESSOR_ARCHITECTURE_ALPHA
:
259 case PROCESSOR_ARCHITECTURE_PPC
:
266 dbg_printf(" %s was running on #%d %s CPU%s",
267 dbg_W2A(exec_name
, -1), msi
->u
.s
.NumberOfProcessors
, str
,
268 msi
->u
.s
.NumberOfProcessors
< 2 ? "" : "s");
269 switch (msi
->MajorVersion
)
272 switch (msi
->MinorVersion
)
274 case 51: str
= "NT 3.51"; break;
275 default: str
= "3-????"; break;
279 switch (msi
->MinorVersion
)
281 case 0: str
= (msi
->PlatformId
== VER_PLATFORM_WIN32_NT
) ? "NT 4.0" : "95"; break;
282 case 10: str
= "98"; break;
283 case 90: str
= "ME"; break;
284 default: str
= "5-????"; break;
288 switch (msi
->MinorVersion
)
290 case 0: str
= "2000"; break;
291 case 1: str
= "XP"; break;
292 case 2: str
= "Server 2003"; break;
293 default: str
= "5-????"; break;
296 default: str
= "???"; break;
298 dbg_printf(" on Windows %s (%u)\n", str
, msi
->BuildNumber
);
299 /* FIXME CSD: msi->CSDVersionRva */
302 dbg_curr_process
= dbg_add_process(&be_process_minidump_io
, pid
, hProc
);
304 dbg_curr_process
->pio_data
= data
;
305 dbg_set_process_name(dbg_curr_process
, exec_name
);
307 dbg_init(hProc
, NULL
, FALSE
);
309 if (MiniDumpReadDumpStream(data
->mapping
, ThreadListStream
, &dir
, &stream
, &size
))
311 MINIDUMP_THREAD_LIST
* mtl
= (MINIDUMP_THREAD_LIST
*)stream
;
314 for (i
= 0; i
< mtl
->NumberOfThreads
; i
++)
316 dbg_add_thread(dbg_curr_process
, mtl
->Threads
[i
].ThreadId
, NULL
,
317 (void*)(DWORD_PTR
)get_addr64(mtl
->Threads
[i
].Teb
));
320 /* first load ELF modules, then do the PE ones */
321 if (MiniDumpReadDumpStream(data
->mapping
, Wine_ElfModuleListStream
, &dir
,
324 WCHAR buffer
[MAX_PATH
];
326 mml
= (MINIDUMP_MODULE_LIST
*)stream
;
327 for (i
= 0, mm
= &mml
->Modules
[0]; i
< mml
->NumberOfModules
; i
++, mm
++)
329 mds
= (MINIDUMP_STRING
*)((char*)data
->mapping
+ mm
->ModuleNameRva
);
330 memcpy(nameW
, mds
->Buffer
, mds
->Length
);
331 nameW
[mds
->Length
/ sizeof(WCHAR
)] = 0;
332 if (SymFindFileInPathW(hProc
, NULL
, nameW
, (void*)(DWORD_PTR
)mm
->CheckSum
,
333 0, 0, SSRVOPT_DWORD
, buffer
, validate_file
, NULL
))
334 dbg_load_module(hProc
, NULL
, buffer
, get_addr64(mm
->BaseOfImage
),
337 SymLoadModuleExW(hProc
, NULL
, nameW
, NULL
, get_addr64(mm
->BaseOfImage
),
338 mm
->SizeOfImage
, NULL
, SLMFLAG_VIRTUAL
);
341 if (MiniDumpReadDumpStream(data
->mapping
, ModuleListStream
, &dir
, &stream
, &size
))
343 WCHAR buffer
[MAX_PATH
];
345 mml
= (MINIDUMP_MODULE_LIST
*)stream
;
346 for (i
= 0, mm
= &mml
->Modules
[0]; i
< mml
->NumberOfModules
; i
++, mm
++)
348 mds
= (MINIDUMP_STRING
*)((char*)data
->mapping
+ mm
->ModuleNameRva
);
349 memcpy(nameW
, mds
->Buffer
, mds
->Length
);
350 nameW
[mds
->Length
/ sizeof(WCHAR
)] = 0;
351 if (SymFindFileInPathW(hProc
, NULL
, nameW
, (void*)(DWORD_PTR
)mm
->TimeDateStamp
,
352 mm
->SizeOfImage
, 0, SSRVOPT_DWORD
, buffer
, validate_file
, NULL
))
353 dbg_load_module(hProc
, NULL
, buffer
, get_addr64(mm
->BaseOfImage
),
355 else if (is_pe_module_embedded(data
, mm
))
356 dbg_load_module(hProc
, NULL
, nameW
, get_addr64(mm
->BaseOfImage
),
359 SymLoadModuleExW(hProc
, NULL
, nameW
, NULL
, get_addr64(mm
->BaseOfImage
),
360 mm
->SizeOfImage
, NULL
, SLMFLAG_VIRTUAL
);
363 if (MiniDumpReadDumpStream(data
->mapping
, ExceptionStream
, &dir
, &stream
, &size
))
365 MINIDUMP_EXCEPTION_STREAM
* mes
= (MINIDUMP_EXCEPTION_STREAM
*)stream
;
367 if ((dbg_curr_thread
= dbg_get_thread(dbg_curr_process
, mes
->ThreadId
)))
371 dbg_curr_tid
= mes
->ThreadId
;
372 dbg_curr_thread
->in_exception
= TRUE
;
373 dbg_curr_thread
->excpt_record
.ExceptionCode
= mes
->ExceptionRecord
.ExceptionCode
;
374 dbg_curr_thread
->excpt_record
.ExceptionFlags
= mes
->ExceptionRecord
.ExceptionFlags
;
375 dbg_curr_thread
->excpt_record
.ExceptionRecord
= (void*)(DWORD_PTR
)get_addr64(mes
->ExceptionRecord
.ExceptionRecord
);
376 dbg_curr_thread
->excpt_record
.ExceptionAddress
= (void*)(DWORD_PTR
)get_addr64(mes
->ExceptionRecord
.ExceptionAddress
);
377 dbg_curr_thread
->excpt_record
.NumberParameters
= mes
->ExceptionRecord
.NumberParameters
;
378 for (i
= 0; i
< dbg_curr_thread
->excpt_record
.NumberParameters
; i
++)
380 dbg_curr_thread
->excpt_record
.ExceptionInformation
[i
] = mes
->ExceptionRecord
.ExceptionInformation
[i
];
382 memcpy(&dbg_context
, (char*)data
->mapping
+ mes
->ThreadContext
.Rva
,
383 min(sizeof(dbg_context
), mes
->ThreadContext
.DataSize
));
384 memory_get_current_pc(&addr
);
385 stack_fetch_frames();
386 be_cpu
->print_context(dbg_curr_thread
->handle
, &dbg_context
, 0);
388 be_cpu
->print_segment_info(dbg_curr_thread
->handle
, &dbg_context
);
389 stack_backtrace(mes
->ThreadId
);
390 source_list_from_addr(&addr
, 0);
396 static void cleanup(struct tgt_process_minidump_data
* data
)
398 if (data
->mapping
) UnmapViewOfFile(data
->mapping
);
399 if (data
->hMap
) CloseHandle(data
->hMap
);
400 if (data
->hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(data
->hFile
);
401 HeapFree(GetProcessHeap(), 0, data
);
404 static struct be_process_io be_process_minidump_io
;
406 enum dbg_start
minidump_reload(int argc
, char* argv
[])
408 struct tgt_process_minidump_data
* data
;
409 enum dbg_start ret
= start_error_parse
;
411 /* try the form <myself> minidump-file */
412 if (argc
!= 1) return start_error_parse
;
414 WINE_TRACE("Processing Minidump file %s\n", argv
[0]);
416 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct tgt_process_minidump_data
));
417 if (!data
) return start_error_init
;
418 data
->mapping
= NULL
;
420 data
->hFile
= INVALID_HANDLE_VALUE
;
422 if ((data
->hFile
= CreateFileA(argv
[0], GENERIC_READ
, FILE_SHARE_READ
, NULL
,
423 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
&&
424 ((data
->hMap
= CreateFileMappingA(data
->hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0) &&
425 ((data
->mapping
= MapViewOfFile(data
->hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
))
429 if (((MINIDUMP_HEADER
*)data
->mapping
)->Signature
== MINIDUMP_SIGNATURE
)
431 ret
= minidump_do_reload(data
);
436 dbg_printf("Unexpected fault while reading minidump %s\n", argv
[0]);
441 if (ret
!= start_ok
) cleanup(data
);
445 static BOOL
tgt_process_minidump_close_process(struct dbg_process
* pcs
, BOOL kill
)
447 struct tgt_process_minidump_data
* data
= private_data(pcs
);
450 pcs
->pio_data
= NULL
;
451 SymCleanup(pcs
->handle
);
452 dbg_del_process(pcs
);
456 static BOOL WINAPI
tgt_process_minidump_get_selector(HANDLE hThread
, DWORD sel
, LDT_ENTRY
* le
)
458 /* so far, pretend all selectors are valid, and mapped to a 32bit flat address space */
459 memset(le
, 0, sizeof(*le
));
460 le
->HighWord
.Bits
.Default_Big
= 1;
464 static struct be_process_io be_process_minidump_io
=
466 tgt_process_minidump_close_process
,
467 tgt_process_minidump_read
,
468 tgt_process_minidump_write
,
469 tgt_process_minidump_get_selector
,