2 * File minidump.c - management of dumps (read & write)
4 * Copyright (C) 2004-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
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
27 #define WIN32_NO_STATUS
28 #include "dbghelp_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
56 /* process & thread information */
60 SYSTEM_PROCESS_INFORMATION
* spi
;
61 /* module information */
62 struct dump_module
* module
;
64 /* exception information */
65 /* output information */
69 struct dump_memory
* mem
;
71 /* callback information */
72 MINIDUMP_CALLBACK_INFORMATION
* cb
;
75 /******************************************************************
78 * reads system wide process information, and make spi point to the record
79 * for process of id 'pid'
81 static BOOL
fetch_process_info(struct dump_context
* dc
)
83 ULONG buf_size
= 0x1000;
86 dc
->pcs_buffer
= NULL
;
87 if (!(dc
->pcs_buffer
= HeapAlloc(GetProcessHeap(), 0, buf_size
))) return FALSE
;
90 nts
= NtQuerySystemInformation(SystemProcessInformation
,
91 dc
->pcs_buffer
, buf_size
, NULL
);
92 if (nts
!= STATUS_INFO_LENGTH_MISMATCH
) break;
93 dc
->pcs_buffer
= HeapReAlloc(GetProcessHeap(), 0, dc
->pcs_buffer
,
95 if (!dc
->pcs_buffer
) return FALSE
;
98 if (nts
== STATUS_SUCCESS
)
100 dc
->spi
= dc
->pcs_buffer
;
103 if (dc
->spi
->dwProcessID
== dc
->pid
) return TRUE
;
104 if (!dc
->spi
->dwOffset
) break;
105 dc
->spi
= (SYSTEM_PROCESS_INFORMATION
*)
106 ((char*)dc
->spi
+ dc
->spi
->dwOffset
);
109 HeapFree(GetProcessHeap(), 0, dc
->pcs_buffer
);
110 dc
->pcs_buffer
= NULL
;
115 /******************************************************************
118 * fetches some information about thread of id 'tid'
120 static BOOL
fetch_thread_info(struct dump_context
* dc
, int thd_idx
,
121 MINIDUMP_THREAD
* mdThd
, CONTEXT
* ctx
)
124 DWORD tid
= dc
->spi
->ti
[thd_idx
].dwThreadID
;
126 THREAD_BASIC_INFORMATION tbi
;
128 memset(ctx
, 0, sizeof(*ctx
));
130 mdThd
->ThreadId
= dc
->spi
->ti
[thd_idx
].dwThreadID
;
131 mdThd
->SuspendCount
= 0;
133 mdThd
->Stack
.StartOfMemoryRange
= 0;
134 mdThd
->Stack
.Memory
.DataSize
= 0;
135 mdThd
->Stack
.Memory
.Rva
= 0;
136 mdThd
->ThreadContext
.DataSize
= 0;
137 mdThd
->ThreadContext
.Rva
= 0;
138 mdThd
->PriorityClass
= dc
->spi
->ti
[thd_idx
].dwBasePriority
; /* FIXME */
139 mdThd
->Priority
= dc
->spi
->ti
[thd_idx
].dwCurrentPriority
;
141 if ((hThread
= OpenThread(THREAD_ALL_ACCESS
, FALSE
, tid
)) == NULL
)
143 FIXME("Couldn't open thread %lu (%lu)\n",
144 dc
->spi
->ti
[thd_idx
].dwThreadID
, GetLastError());
148 if (NtQueryInformationThread(hThread
, ThreadBasicInformation
,
149 &tbi
, sizeof(tbi
), NULL
) == STATUS_SUCCESS
)
151 mdThd
->Teb
= (ULONG_PTR
)tbi
.TebBaseAddress
;
152 if (tbi
.ExitStatus
== STILL_ACTIVE
&& tid
!= GetCurrentThreadId() &&
153 (mdThd
->SuspendCount
= SuspendThread(hThread
)) != (DWORD
)-1)
155 mdThd
->SuspendCount
--;
156 ctx
->ContextFlags
= CONTEXT_FULL
;
157 if (!GetThreadContext(hThread
, ctx
))
158 memset(ctx
, 0, sizeof(*ctx
));
160 if (ReadProcessMemory(dc
->hProcess
, tbi
.TebBaseAddress
,
161 &tib
, sizeof(tib
), NULL
))
164 /* limiting the stack dumping to the size actually used */
166 mdThd
->Stack
.StartOfMemoryRange
= (ctx
->Esp
- 4);
168 mdThd
->Stack
.StartOfMemoryRange
= (ULONG_PTR
)tib
.StackLimit
;
169 mdThd
->Stack
.Memory
.DataSize
= (ULONG_PTR
)tib
.StackBase
-
170 mdThd
->Stack
.StartOfMemoryRange
;
171 #elif defined(__powerpc__)
173 mdThd
->Stack
.StartOfMemoryRange
= ctx
->Iar
- 4;
175 mdThd
->Stack
.StartOfMemoryRange
= (ULONG_PTR
)tib
.StackLimit
;
176 mdThd
->Stack
.Memory
.DataSize
= (ULONG_PTR
)tib
.StackBase
-
177 mdThd
->Stack
.StartOfMemoryRange
;
179 #error unsupported CPU
182 ResumeThread(hThread
);
185 CloseHandle(hThread
);
189 /******************************************************************
192 * Add a module to a dump context
194 static BOOL
add_module(struct dump_context
* dc
, const char* name
,
195 DWORD base
, DWORD size
, DWORD timestamp
, DWORD checksum
,
199 dc
->module
= HeapAlloc(GetProcessHeap(), 0,
200 ++dc
->num_module
* sizeof(*dc
->module
));
202 dc
->module
= HeapReAlloc(GetProcessHeap(), 0, dc
->module
,
203 ++dc
->num_module
* sizeof(*dc
->module
));
204 if (!dc
->module
) return FALSE
;
206 !GetModuleFileNameExA(dc
->hProcess
, (HMODULE
)base
,
207 dc
->module
[dc
->num_module
- 1].name
,
208 sizeof(dc
->module
[dc
->num_module
- 1].name
)))
209 lstrcpynA(dc
->module
[dc
->num_module
- 1].name
, name
,
210 sizeof(dc
->module
[dc
->num_module
- 1].name
));
211 dc
->module
[dc
->num_module
- 1].base
= base
;
212 dc
->module
[dc
->num_module
- 1].size
= size
;
213 dc
->module
[dc
->num_module
- 1].timestamp
= timestamp
;
214 dc
->module
[dc
->num_module
- 1].checksum
= checksum
;
215 dc
->module
[dc
->num_module
- 1].is_elf
= is_elf
;
220 /******************************************************************
221 * fetch_pe_module_info_cb
223 * Callback for accumulating in dump_context a PE modules set
225 static BOOL WINAPI
fetch_pe_module_info_cb(char* name
, DWORD base
, DWORD size
,
228 struct dump_context
* dc
= (struct dump_context
*)user
;
229 IMAGE_NT_HEADERS nth
;
231 if (pe_load_nt_header(dc
->hProcess
, base
, &nth
))
232 add_module((struct dump_context
*)user
, name
, base
, size
,
233 nth
.FileHeader
.TimeDateStamp
, nth
.OptionalHeader
.CheckSum
,
238 /******************************************************************
239 * fetch_elf_module_info_cb
241 * Callback for accumulating in dump_context an ELF modules set
243 static BOOL
fetch_elf_module_info_cb(const char* name
, unsigned long base
,
246 struct dump_context
* dc
= (struct dump_context
*)user
;
247 DWORD size
, checksum
;
249 /* FIXME: there's no relevant timestamp on ELF modules */
250 /* NB: if we have a non-null base from the live-target use it (whenever
251 * the ELF module is relocatable or not). If we have a null base (ELF
252 * module isn't relocatable) then grab its base address from ELF file
254 if (!elf_fetch_file_info(name
, base
? NULL
: &base
, &size
, &checksum
))
256 add_module(dc
, name
, base
, size
, 0 /* FIXME */, checksum
, TRUE
);
260 static void fetch_module_info(struct dump_context
* dc
)
262 EnumerateLoadedModules(dc
->hProcess
, fetch_pe_module_info_cb
, dc
);
263 /* Since we include ELF modules in a separate stream from the regular PE ones,
264 * we can always include those ELF modules (they don't eat lots of space)
265 * And it's always a good idea to have a trace of the loaded ELF modules for
266 * a given application in a post mortem debugging condition.
268 elf_enum_modules(dc
->hProcess
, fetch_elf_module_info_cb
, dc
);
271 /******************************************************************
274 * Add a memory block to be dumped in a minidump
275 * If rva is non 0, it's the rva in the minidump where has to be stored
276 * also the rva of the memory block when written (this allows to reference
277 * a memory block from outside the list of memory blocks).
279 static void add_memory_block(struct dump_context
* dc
, ULONG64 base
, ULONG size
, ULONG rva
)
282 dc
->mem
= HeapReAlloc(GetProcessHeap(), 0, dc
->mem
,
283 ++dc
->num_mem
* sizeof(*dc
->mem
));
285 dc
->mem
= HeapAlloc(GetProcessHeap(), 0, ++dc
->num_mem
* sizeof(*dc
->mem
));
288 dc
->mem
[dc
->num_mem
- 1].base
= base
;
289 dc
->mem
[dc
->num_mem
- 1].size
= size
;
290 dc
->mem
[dc
->num_mem
- 1].rva
= rva
;
292 else dc
->num_mem
= 0;
295 /******************************************************************
298 * Writes a chunk of data at a given position in the minidump
300 static void writeat(struct dump_context
* dc
, RVA rva
, void* data
, unsigned size
)
304 SetFilePointer(dc
->hFile
, rva
, NULL
, FILE_BEGIN
);
305 WriteFile(dc
->hFile
, data
, size
, &written
, NULL
);
308 /******************************************************************
311 * writes a new chunk of data to the minidump, increasing the current
314 static void append(struct dump_context
* dc
, void* data
, unsigned size
)
316 writeat(dc
, dc
->rva
, data
, size
);
320 /******************************************************************
321 * dump_exception_info
323 * Write in File the exception information from pcs
325 static void dump_exception_info(struct dump_context
* dc
,
326 const MINIDUMP_EXCEPTION_INFORMATION
* except
)
328 MINIDUMP_EXCEPTION_STREAM mdExcpt
;
329 EXCEPTION_RECORD rec
, *prec
;
333 mdExcpt
.ThreadId
= except
->ThreadId
;
334 mdExcpt
.__alignment
= 0;
335 if (except
->ClientPointers
)
337 EXCEPTION_POINTERS ep
;
339 ReadProcessMemory(dc
->hProcess
,
340 except
->ExceptionPointers
, &ep
, sizeof(ep
), NULL
);
341 ReadProcessMemory(dc
->hProcess
,
342 ep
.ExceptionRecord
, &rec
, sizeof(rec
), NULL
);
343 ReadProcessMemory(dc
->hProcess
,
344 ep
.ContextRecord
, &ctx
, sizeof(ctx
), NULL
);
351 prec
= except
->ExceptionPointers
->ExceptionRecord
;
352 pctx
= except
->ExceptionPointers
->ContextRecord
;
354 mdExcpt
.ExceptionRecord
.ExceptionCode
= prec
->ExceptionCode
;
355 mdExcpt
.ExceptionRecord
.ExceptionFlags
= prec
->ExceptionFlags
;
356 mdExcpt
.ExceptionRecord
.ExceptionRecord
= (DWORD_PTR
)prec
->ExceptionRecord
;
357 mdExcpt
.ExceptionRecord
.ExceptionAddress
= (DWORD_PTR
)prec
->ExceptionAddress
;
358 mdExcpt
.ExceptionRecord
.NumberParameters
= prec
->NumberParameters
;
359 mdExcpt
.ExceptionRecord
.__unusedAlignment
= 0;
360 for (i
= 0; i
< mdExcpt
.ExceptionRecord
.NumberParameters
; i
++)
361 mdExcpt
.ExceptionRecord
.ExceptionInformation
[i
] = (DWORD_PTR
)prec
->ExceptionInformation
[i
];
362 mdExcpt
.ThreadContext
.DataSize
= sizeof(*pctx
);
363 mdExcpt
.ThreadContext
.Rva
= dc
->rva
+ sizeof(mdExcpt
);
365 append(dc
, &mdExcpt
, sizeof(mdExcpt
));
366 append(dc
, pctx
, sizeof(*pctx
));
369 /******************************************************************
372 * Write in File the modules from pcs
374 static void dump_modules(struct dump_context
* dc
, BOOL dump_elf
)
376 MINIDUMP_MODULE mdModule
;
377 MINIDUMP_MODULE_LIST mdModuleList
;
379 MINIDUMP_STRING
* ms
= (MINIDUMP_STRING
*)tmp
;
384 for (i
= nmod
= 0; i
< dc
->num_module
; i
++)
386 if ((dc
->module
[i
].is_elf
&& dump_elf
) ||
387 (!dc
->module
[i
].is_elf
&& !dump_elf
))
391 mdModuleList
.NumberOfModules
= 0;
392 /* reserve space for mdModuleList
393 * FIXME: since we don't support 0 length arrays, we cannot use the
394 * size of mdModuleList
395 * FIXME: if we don't ask for all modules in cb, we'll get a hole in the file
398 dc
->rva
+= sizeof(mdModuleList
.NumberOfModules
) + sizeof(mdModule
) * nmod
;
399 for (i
= 0; i
< dc
->num_module
; i
++)
401 if ((dc
->module
[i
].is_elf
&& !dump_elf
) ||
402 (!dc
->module
[i
].is_elf
&& dump_elf
))
405 flags_out
= ModuleWriteModule
| ModuleWriteMiscRecord
| ModuleWriteCvRecord
;
406 if (dc
->type
& MiniDumpWithDataSegs
)
407 flags_out
|= ModuleWriteDataSeg
;
408 if (dc
->type
& MiniDumpWithProcessThreadData
)
409 flags_out
|= ModuleWriteTlsData
;
410 if (dc
->type
& MiniDumpWithCodeSegs
)
411 flags_out
|= ModuleWriteCodeSegs
;
412 ms
->Length
= MultiByteToWideChar(CP_ACP
, 0,
413 dc
->module
[i
].name
, -1,
414 NULL
, 0) * sizeof(WCHAR
);
415 if (sizeof(ULONG
) + ms
->Length
> sizeof(tmp
))
416 FIXME("Buffer overflow!!!\n");
417 MultiByteToWideChar(CP_ACP
, 0, dc
->module
[i
].name
, -1,
418 ms
->Buffer
, ms
->Length
/sizeof(WCHAR
));
422 MINIDUMP_CALLBACK_INPUT cbin
;
423 MINIDUMP_CALLBACK_OUTPUT cbout
;
425 cbin
.ProcessId
= dc
->pid
;
426 cbin
.ProcessHandle
= dc
->hProcess
;
427 cbin
.CallbackType
= ModuleCallback
;
429 cbin
.u
.Module
.FullPath
= ms
->Buffer
;
430 cbin
.u
.Module
.BaseOfImage
= dc
->module
[i
].base
;
431 cbin
.u
.Module
.SizeOfImage
= dc
->module
[i
].size
;
432 cbin
.u
.Module
.CheckSum
= dc
->module
[i
].checksum
;
433 cbin
.u
.Module
.TimeDateStamp
= dc
->module
[i
].timestamp
;
434 memset(&cbin
.u
.Module
.VersionInfo
, 0, sizeof(cbin
.u
.Module
.VersionInfo
));
435 cbin
.u
.Module
.CvRecord
= NULL
;
436 cbin
.u
.Module
.SizeOfCvRecord
= 0;
437 cbin
.u
.Module
.MiscRecord
= NULL
;
438 cbin
.u
.Module
.SizeOfMiscRecord
= 0;
440 cbout
.u
.ModuleWriteFlags
= flags_out
;
441 if (!dc
->cb
->CallbackRoutine(dc
->cb
->CallbackParam
, &cbin
, &cbout
))
443 flags_out
&= cbout
.u
.ModuleWriteFlags
;
445 if (flags_out
& ModuleWriteModule
)
447 mdModule
.BaseOfImage
= dc
->module
[i
].base
;
448 mdModule
.SizeOfImage
= dc
->module
[i
].size
;
449 mdModule
.CheckSum
= dc
->module
[i
].checksum
;
450 mdModule
.TimeDateStamp
= dc
->module
[i
].timestamp
;
451 mdModule
.ModuleNameRva
= dc
->rva
;
452 ms
->Length
-= sizeof(WCHAR
);
453 append(dc
, ms
, sizeof(ULONG
) + ms
->Length
);
454 memset(&mdModule
.VersionInfo
, 0, sizeof(mdModule
.VersionInfo
)); /* FIXME */
455 mdModule
.CvRecord
.DataSize
= 0; /* FIXME */
456 mdModule
.CvRecord
.Rva
= 0; /* FIXME */
457 mdModule
.MiscRecord
.DataSize
= 0; /* FIXME */
458 mdModule
.MiscRecord
.Rva
= 0; /* FIXME */
459 mdModule
.Reserved0
= 0; /* FIXME */
460 mdModule
.Reserved1
= 0; /* FIXME */
462 rva_base
+ sizeof(mdModuleList
.NumberOfModules
) +
463 mdModuleList
.NumberOfModules
++ * sizeof(mdModule
),
464 &mdModule
, sizeof(mdModule
));
467 writeat(dc
, rva_base
, &mdModuleList
.NumberOfModules
,
468 sizeof(mdModuleList
.NumberOfModules
));
471 /******************************************************************
474 * Dumps into File the information about the system
476 static void dump_system_info(struct dump_context
* dc
)
478 MINIDUMP_SYSTEM_INFO mdSysInfo
;
480 OSVERSIONINFOW osInfo
;
484 GetSystemInfo(&sysInfo
);
485 osInfo
.dwOSVersionInfoSize
= sizeof(osInfo
);
486 GetVersionExW(&osInfo
);
488 mdSysInfo
.ProcessorArchitecture
= sysInfo
.u
.s
.wProcessorArchitecture
;
489 mdSysInfo
.ProcessorLevel
= sysInfo
.wProcessorLevel
;
490 mdSysInfo
.ProcessorRevision
= sysInfo
.wProcessorRevision
;
491 mdSysInfo
.u
.s
.NumberOfProcessors
= sysInfo
.dwNumberOfProcessors
;
492 mdSysInfo
.u
.s
.ProductType
= VER_NT_WORKSTATION
; /* FIXME */
493 mdSysInfo
.MajorVersion
= osInfo
.dwMajorVersion
;
494 mdSysInfo
.MinorVersion
= osInfo
.dwMinorVersion
;
495 mdSysInfo
.BuildNumber
= osInfo
.dwBuildNumber
;
496 mdSysInfo
.PlatformId
= osInfo
.dwPlatformId
;
498 mdSysInfo
.CSDVersionRva
= dc
->rva
+ sizeof(mdSysInfo
);
499 mdSysInfo
.u1
.Reserved1
= 0;
501 memset(&mdSysInfo
.Cpu
, 0, sizeof(mdSysInfo
.Cpu
));
503 append(dc
, &mdSysInfo
, sizeof(mdSysInfo
));
505 slen
= lstrlenW(osInfo
.szCSDVersion
) * sizeof(WCHAR
);
506 WriteFile(dc
->hFile
, &slen
, sizeof(slen
), &written
, NULL
);
507 WriteFile(dc
->hFile
, osInfo
.szCSDVersion
, slen
, &written
, NULL
);
508 dc
->rva
+= sizeof(ULONG
) + slen
;
511 /******************************************************************
514 * Dumps into File the information about running threads
516 static void dump_threads(struct dump_context
* dc
)
518 MINIDUMP_THREAD mdThd
;
519 MINIDUMP_THREAD_LIST mdThdList
;
525 mdThdList
.NumberOfThreads
= 0;
528 dc
->rva
+= sizeof(mdThdList
.NumberOfThreads
) +
529 dc
->spi
->dwThreadCount
* sizeof(mdThd
);
531 for (i
= 0; i
< dc
->spi
->dwThreadCount
; i
++)
533 fetch_thread_info(dc
, i
, &mdThd
, &ctx
);
535 flags_out
= ThreadWriteThread
| ThreadWriteStack
| ThreadWriteContext
|
536 ThreadWriteInstructionWindow
;
537 if (dc
->type
& MiniDumpWithProcessThreadData
)
538 flags_out
|= ThreadWriteThreadData
;
539 if (dc
->type
& MiniDumpWithThreadInfo
)
540 flags_out
|= ThreadWriteThreadInfo
;
544 MINIDUMP_CALLBACK_INPUT cbin
;
545 MINIDUMP_CALLBACK_OUTPUT cbout
;
547 cbin
.ProcessId
= dc
->pid
;
548 cbin
.ProcessHandle
= dc
->hProcess
;
549 cbin
.CallbackType
= ThreadCallback
;
550 cbin
.u
.Thread
.ThreadId
= dc
->spi
->ti
[i
].dwThreadID
;
551 cbin
.u
.Thread
.ThreadHandle
= 0; /* FIXME */
552 memcpy(&cbin
.u
.Thread
.Context
, &ctx
, sizeof(CONTEXT
));
553 cbin
.u
.Thread
.SizeOfContext
= sizeof(CONTEXT
);
554 cbin
.u
.Thread
.StackBase
= mdThd
.Stack
.StartOfMemoryRange
;
555 cbin
.u
.Thread
.StackEnd
= mdThd
.Stack
.StartOfMemoryRange
+
556 mdThd
.Stack
.Memory
.DataSize
;
558 cbout
.u
.ThreadWriteFlags
= flags_out
;
559 if (!dc
->cb
->CallbackRoutine(dc
->cb
->CallbackParam
, &cbin
, &cbout
))
561 flags_out
&= cbout
.u
.ThreadWriteFlags
;
563 if (flags_out
& ThreadWriteThread
)
565 if (ctx
.ContextFlags
&& (flags_out
& ThreadWriteContext
))
567 mdThd
.ThreadContext
.Rva
= dc
->rva
;
568 mdThd
.ThreadContext
.DataSize
= sizeof(CONTEXT
);
569 append(dc
, &ctx
, sizeof(CONTEXT
));
571 if (mdThd
.Stack
.Memory
.DataSize
&& (flags_out
& ThreadWriteStack
))
573 add_memory_block(dc
, mdThd
.Stack
.StartOfMemoryRange
,
574 mdThd
.Stack
.Memory
.DataSize
,
575 rva_base
+ sizeof(mdThdList
.NumberOfThreads
) +
576 mdThdList
.NumberOfThreads
* sizeof(mdThd
) +
577 FIELD_OFFSET(MINIDUMP_THREAD
, Stack
.Memory
.Rva
));
580 rva_base
+ sizeof(mdThdList
.NumberOfThreads
) +
581 mdThdList
.NumberOfThreads
* sizeof(mdThd
),
582 &mdThd
, sizeof(mdThd
));
583 mdThdList
.NumberOfThreads
++;
585 if (ctx
.ContextFlags
&& (flags_out
& ThreadWriteInstructionWindow
))
587 /* FIXME: - Native dbghelp also dumps 0x80 bytes around EIP
588 * - also crop values across module boundaries,
589 * - and don't make it i386 dependent
591 /* add_memory_block(dc, ctx.Eip - 0x80, ctx.Eip + 0x80, 0); */
594 writeat(dc
, rva_base
,
595 &mdThdList
.NumberOfThreads
, sizeof(mdThdList
.NumberOfThreads
));
598 /******************************************************************
601 * dumps information about the memory of the process (stack of the threads)
603 static void dump_memory_info(struct dump_context
* dc
)
605 MINIDUMP_MEMORY_LIST mdMemList
;
606 MINIDUMP_MEMORY_DESCRIPTOR mdMem
;
608 unsigned i
, pos
, len
;
612 mdMemList
.NumberOfMemoryRanges
= dc
->num_mem
;
613 append(dc
, &mdMemList
.NumberOfMemoryRanges
,
614 sizeof(mdMemList
.NumberOfMemoryRanges
));
616 dc
->rva
+= mdMemList
.NumberOfMemoryRanges
* sizeof(mdMem
);
618 for (i
= 0; i
< dc
->num_mem
; i
++)
620 mdMem
.StartOfMemoryRange
= dc
->mem
[i
].base
;
621 mdMem
.Memory
.Rva
= dc
->rva
;
622 mdMem
.Memory
.DataSize
= dc
->mem
[i
].size
;
623 SetFilePointer(dc
->hFile
, dc
->rva
, NULL
, FILE_BEGIN
);
624 for (pos
= 0; pos
< dc
->mem
[i
].size
; pos
+= sizeof(tmp
))
626 len
= min(dc
->mem
[i
].size
- pos
, sizeof(tmp
));
627 if (ReadProcessMemory(dc
->hProcess
,
628 (void*)(ULONG
)(dc
->mem
[i
].base
+ pos
),
630 WriteFile(dc
->hFile
, tmp
, len
, &written
, NULL
);
632 dc
->rva
+= mdMem
.Memory
.DataSize
;
633 writeat(dc
, rva_base
+ i
* sizeof(mdMem
), &mdMem
, sizeof(mdMem
));
636 writeat(dc
, dc
->mem
[i
].rva
, &mdMem
.Memory
.Rva
, sizeof(mdMem
.Memory
.Rva
));
641 static void dump_misc_info(struct dump_context
* dc
)
643 MINIDUMP_MISC_INFO mmi
;
645 mmi
.SizeOfInfo
= sizeof(mmi
);
646 mmi
.Flags1
= MINIDUMP_MISC1_PROCESS_ID
;
647 mmi
.ProcessId
= dc
->pid
;
648 /* FIXME: create/user/kernel time */
649 append(dc
, &mmi
, sizeof(mmi
));
652 /******************************************************************
653 * MiniDumpWriteDump (DEBUGHLP.@)
656 BOOL WINAPI
MiniDumpWriteDump(HANDLE hProcess
, DWORD pid
, HANDLE hFile
,
657 MINIDUMP_TYPE DumpType
,
658 PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam
,
659 PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam
,
660 PMINIDUMP_CALLBACK_INFORMATION CallbackParam
)
662 MINIDUMP_HEADER mdHead
;
663 MINIDUMP_DIRECTORY mdDir
;
664 DWORD i
, nStreams
, idx_stream
;
665 struct dump_context dc
;
667 dc
.hProcess
= hProcess
;
672 dc
.cb
= CallbackParam
;
678 if (!fetch_process_info(&dc
)) return FALSE
;
679 fetch_module_info(&dc
);
682 nStreams
= 6 + (ExceptionParam
? 1 : 0) +
683 (UserStreamParam
? UserStreamParam
->UserStreamCount
: 0);
685 if (DumpType
& MiniDumpWithDataSegs
)
686 FIXME("NIY MiniDumpWithDataSegs\n");
687 if (DumpType
& MiniDumpWithFullMemory
)
688 FIXME("NIY MiniDumpWithFullMemory\n");
689 if (DumpType
& MiniDumpWithHandleData
)
690 FIXME("NIY MiniDumpWithHandleData\n");
691 if (DumpType
& MiniDumpFilterMemory
)
692 FIXME("NIY MiniDumpFilterMemory\n");
693 if (DumpType
& MiniDumpScanMemory
)
694 FIXME("NIY MiniDumpScanMemory\n");
696 /* 2) write header */
697 mdHead
.Signature
= MINIDUMP_SIGNATURE
;
698 mdHead
.Version
= MINIDUMP_VERSION
;
699 mdHead
.NumberOfStreams
= nStreams
;
700 mdHead
.StreamDirectoryRva
= sizeof(mdHead
);
701 mdHead
.u
.TimeDateStamp
= time(NULL
);
702 mdHead
.Flags
= DumpType
;
703 append(&dc
, &mdHead
, sizeof(mdHead
));
705 /* 3) write stream directories */
706 dc
.rva
+= nStreams
* sizeof(mdDir
);
709 /* 3.1) write data stream directories */
711 mdDir
.StreamType
= ThreadListStream
;
712 mdDir
.Location
.Rva
= dc
.rva
;
714 mdDir
.Location
.DataSize
= dc
.rva
- mdDir
.Location
.Rva
;
715 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
716 &mdDir
, sizeof(mdDir
));
718 mdDir
.StreamType
= ModuleListStream
;
719 mdDir
.Location
.Rva
= dc
.rva
;
720 dump_modules(&dc
, FALSE
);
721 mdDir
.Location
.DataSize
= dc
.rva
- mdDir
.Location
.Rva
;
722 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
723 &mdDir
, sizeof(mdDir
));
725 mdDir
.StreamType
= 0xfff0; /* FIXME: this is part of MS reserved streams */
726 mdDir
.Location
.Rva
= dc
.rva
;
727 dump_modules(&dc
, TRUE
);
728 mdDir
.Location
.DataSize
= dc
.rva
- mdDir
.Location
.Rva
;
729 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
730 &mdDir
, sizeof(mdDir
));
732 mdDir
.StreamType
= MemoryListStream
;
733 mdDir
.Location
.Rva
= dc
.rva
;
734 dump_memory_info(&dc
);
735 mdDir
.Location
.DataSize
= dc
.rva
- mdDir
.Location
.Rva
;
736 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
737 &mdDir
, sizeof(mdDir
));
739 mdDir
.StreamType
= SystemInfoStream
;
740 mdDir
.Location
.Rva
= dc
.rva
;
741 dump_system_info(&dc
);
742 mdDir
.Location
.DataSize
= dc
.rva
- mdDir
.Location
.Rva
;
743 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
744 &mdDir
, sizeof(mdDir
));
746 mdDir
.StreamType
= MiscInfoStream
;
747 mdDir
.Location
.Rva
= dc
.rva
;
749 mdDir
.Location
.DataSize
= dc
.rva
- mdDir
.Location
.Rva
;
750 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
751 &mdDir
, sizeof(mdDir
));
753 /* 3.2) write exception information (if any) */
756 mdDir
.StreamType
= ExceptionStream
;
757 mdDir
.Location
.Rva
= dc
.rva
;
758 dump_exception_info(&dc
, ExceptionParam
);
759 mdDir
.Location
.DataSize
= dc
.rva
- mdDir
.Location
.Rva
;
760 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
761 &mdDir
, sizeof(mdDir
));
764 /* 3.3) write user defined streams (if any) */
767 for (i
= 0; i
< UserStreamParam
->UserStreamCount
; i
++)
769 mdDir
.StreamType
= UserStreamParam
->UserStreamArray
[i
].Type
;
770 mdDir
.Location
.DataSize
= UserStreamParam
->UserStreamArray
[i
].BufferSize
;
771 mdDir
.Location
.Rva
= dc
.rva
;
772 writeat(&dc
, mdHead
.StreamDirectoryRva
+ idx_stream
++ * sizeof(mdDir
),
773 &mdDir
, sizeof(mdDir
));
774 append(&dc
, UserStreamParam
->UserStreamArray
[i
].Buffer
,
775 UserStreamParam
->UserStreamArray
[i
].BufferSize
);
779 HeapFree(GetProcessHeap(), 0, dc
.pcs_buffer
);
780 HeapFree(GetProcessHeap(), 0, dc
.mem
);
781 HeapFree(GetProcessHeap(), 0, dc
.module
);
786 /******************************************************************
787 * MiniDumpReadDumpStream (DEBUGHLP.@)
791 BOOL WINAPI
MiniDumpReadDumpStream(void* base
, ULONG str_idx
,
792 PMINIDUMP_DIRECTORY
* pdir
,
793 void** stream
, ULONG
* size
)
795 MINIDUMP_HEADER
* mdHead
= (MINIDUMP_HEADER
*)base
;
797 if (mdHead
->Signature
== MINIDUMP_SIGNATURE
)
799 MINIDUMP_DIRECTORY
* dir
;
802 dir
= (MINIDUMP_DIRECTORY
*)((char*)base
+ mdHead
->StreamDirectoryRva
);
803 for (i
= 0; i
< mdHead
->NumberOfStreams
; i
++, dir
++)
805 if (dir
->StreamType
== str_idx
)
808 *stream
= (char*)base
+ dir
->Location
.Rva
;
809 *size
= dir
->Location
.DataSize
;
814 SetLastError(ERROR_INVALID_PARAMETER
);