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 static void fetch_thread_stack(struct dump_context* dc, void* teb_addr,
116 const CONTEXT* ctx, MINIDUMP_MEMORY_DESCRIPTOR* mmd)
120 if (ReadProcessMemory(dc->hProcess, teb_addr, &tib, sizeof(tib), NULL))
123 /* limiting the stack dumping to the size actually used */
125 mmd->StartOfMemoryRange = (ctx->Esp - 4);
127 mmd->StartOfMemoryRange = (ULONG_PTR)tib.StackLimit;
128 #elif defined(__powerpc__)
130 mmd->StartOfMemoryRange = ctx->Iar - 4;
132 mmd->StartOfMemoryRange = (ULONG_PTR)tib.StackLimit;
133 #elif defined(__x86_64__)
135 mmd->StartOfMemoryRange = (ctx->Rsp - 8);
137 mmd->StartOfMemoryRange = (ULONG_PTR)tib.StackLimit;
139 #error unsupported CPU
141 mmd->Memory.DataSize = (ULONG_PTR)tib.StackBase - mmd->StartOfMemoryRange;
145 /******************************************************************
148 * fetches some information about thread of id 'tid'
150 static BOOL fetch_thread_info(struct dump_context* dc, int thd_idx,
151 const MINIDUMP_EXCEPTION_INFORMATION* except,
152 MINIDUMP_THREAD* mdThd, CONTEXT* ctx)
154 DWORD tid = dc->spi->ti[thd_idx].dwThreadID;
156 THREAD_BASIC_INFORMATION tbi;
158 memset(ctx, 0, sizeof(*ctx));
160 mdThd->ThreadId = dc->spi->ti[thd_idx].dwThreadID;
161 mdThd->SuspendCount = 0;
163 mdThd->Stack.StartOfMemoryRange = 0;
164 mdThd->Stack.Memory.DataSize = 0;
165 mdThd->Stack.Memory.Rva = 0;
166 mdThd->ThreadContext.DataSize = 0;
167 mdThd->ThreadContext.Rva = 0;
168 mdThd->PriorityClass = dc->spi->ti[thd_idx].dwBasePriority; /* FIXME */
169 mdThd->Priority = dc->spi->ti[thd_idx].dwCurrentPriority;
171 if ((hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, tid)) == NULL)
173 FIXME("Couldn't open thread %u (%u)\n",
174 dc->spi->ti[thd_idx].dwThreadID, GetLastError());
178 if (NtQueryInformationThread(hThread, ThreadBasicInformation,
179 &tbi, sizeof(tbi), NULL) == STATUS_SUCCESS)
181 mdThd->Teb = (ULONG_PTR)tbi.TebBaseAddress;
182 if (tbi.ExitStatus == STILL_ACTIVE)
184 if (tid != GetCurrentThreadId() &&
185 (mdThd->SuspendCount = SuspendThread(hThread)) != (DWORD)-1)
187 mdThd->SuspendCount--;
188 ctx->ContextFlags = CONTEXT_FULL;
189 if (!GetThreadContext(hThread, ctx))
190 memset(ctx, 0, sizeof(*ctx));
192 fetch_thread_stack(dc, tbi.TebBaseAddress, ctx, &mdThd->Stack);
193 ResumeThread(hThread);
195 else if (tid == GetCurrentThreadId() && except)
198 if (except->ClientPointers)
200 EXCEPTION_POINTERS ep;
202 ReadProcessMemory(dc->hProcess, except->ExceptionPointers,
203 &ep, sizeof(ep), NULL);
204 ReadProcessMemory(dc->hProcess, ep.ContextRecord,
205 &ctx, sizeof(ctx), NULL);
208 else pctx = except->ExceptionPointers->ContextRecord;
209 fetch_thread_stack(dc, tbi.TebBaseAddress, pctx, &mdThd->Stack);
213 CloseHandle(hThread);
217 /******************************************************************
220 * Add a module to a dump context
222 static BOOL add_module(struct dump_context* dc, const char* name,
223 DWORD base, DWORD size, DWORD timestamp, DWORD checksum,
227 dc->module = HeapAlloc(GetProcessHeap(), 0,
228 ++dc->num_module * sizeof(*dc->module));
230 dc->module = HeapReAlloc(GetProcessHeap(), 0, dc->module,
231 ++dc->num_module * sizeof(*dc->module));
232 if (!dc->module) return FALSE;
234 !GetModuleFileNameExA(dc->hProcess, (HMODULE)base,
235 dc->module[dc->num_module - 1].name,
236 sizeof(dc->module[dc->num_module - 1].name)))
237 lstrcpynA(dc->module[dc->num_module - 1].name, name,
238 sizeof(dc->module[dc->num_module - 1].name));
239 dc->module[dc->num_module - 1].base = base;
240 dc->module[dc->num_module - 1].size = size;
241 dc->module[dc->num_module - 1].timestamp = timestamp;
242 dc->module[dc->num_module - 1].checksum = checksum;
243 dc->module[dc->num_module - 1].is_elf = is_elf;
248 /******************************************************************
249 * fetch_pe_module_info_cb
251 * Callback for accumulating in dump_context a PE modules set
253 static BOOL WINAPI fetch_pe_module_info_cb(char* name, DWORD base, DWORD size,
256 struct dump_context* dc = (struct dump_context*)user;
257 IMAGE_NT_HEADERS nth;
259 if (pe_load_nt_header(dc->hProcess, base, &nth))
260 add_module((struct dump_context*)user, name, base, size,
261 nth.FileHeader.TimeDateStamp, nth.OptionalHeader.CheckSum,
266 /******************************************************************
267 * fetch_elf_module_info_cb
269 * Callback for accumulating in dump_context an ELF modules set
271 static BOOL fetch_elf_module_info_cb(const char* name, unsigned long base,
274 struct dump_context* dc = (struct dump_context*)user;
275 DWORD rbase, size, checksum;
277 /* FIXME: there's no relevant timestamp on ELF modules */
278 /* NB: if we have a non-null base from the live-target use it (whenever
279 * the ELF module is relocatable or not). If we have a null base (ELF
280 * module isn't relocatable) then grab its base address from ELF file
282 if (!elf_fetch_file_info(name, &rbase, &size, &checksum))
284 add_module(dc, name, base ? base : rbase, size, 0 /* FIXME */, checksum, TRUE);
288 static void fetch_module_info(struct dump_context* dc)
290 EnumerateLoadedModules(dc->hProcess, fetch_pe_module_info_cb, dc);
291 /* Since we include ELF modules in a separate stream from the regular PE ones,
292 * we can always include those ELF modules (they don't eat lots of space)
293 * And it's always a good idea to have a trace of the loaded ELF modules for
294 * a given application in a post mortem debugging condition.
296 elf_enum_modules(dc->hProcess, fetch_elf_module_info_cb, dc);
299 /******************************************************************
302 * Add a memory block to be dumped in a minidump
303 * If rva is non 0, it's the rva in the minidump where has to be stored
304 * also the rva of the memory block when written (this allows to reference
305 * a memory block from outside the list of memory blocks).
307 static void add_memory_block(struct dump_context* dc, ULONG64 base, ULONG size, ULONG rva)
310 dc->mem = HeapReAlloc(GetProcessHeap(), 0, dc->mem,
311 ++dc->num_mem * sizeof(*dc->mem));
313 dc->mem = HeapAlloc(GetProcessHeap(), 0, ++dc->num_mem * sizeof(*dc->mem));
316 dc->mem[dc->num_mem - 1].base = base;
317 dc->mem[dc->num_mem - 1].size = size;
318 dc->mem[dc->num_mem - 1].rva = rva;
320 else dc->num_mem = 0;
323 /******************************************************************
326 * Writes a chunk of data at a given position in the minidump
328 static void writeat(struct dump_context* dc, RVA rva, void* data, unsigned size)
332 SetFilePointer(dc->hFile, rva, NULL, FILE_BEGIN);
333 WriteFile(dc->hFile, data, size, &written, NULL);
336 /******************************************************************
339 * writes a new chunk of data to the minidump, increasing the current
342 static void append(struct dump_context* dc, void* data, unsigned size)
344 writeat(dc, dc->rva, data, size);
348 /******************************************************************
349 * dump_exception_info
351 * Write in File the exception information from pcs
353 static void dump_exception_info(struct dump_context* dc,
354 const MINIDUMP_EXCEPTION_INFORMATION* except)
356 MINIDUMP_EXCEPTION_STREAM mdExcpt;
357 EXCEPTION_RECORD rec, *prec;
361 mdExcpt.ThreadId = except->ThreadId;
362 mdExcpt.__alignment = 0;
363 if (except->ClientPointers)
365 EXCEPTION_POINTERS ep;
367 ReadProcessMemory(dc->hProcess,
368 except->ExceptionPointers, &ep, sizeof(ep), NULL);
369 ReadProcessMemory(dc->hProcess,
370 ep.ExceptionRecord, &rec, sizeof(rec), NULL);
371 ReadProcessMemory(dc->hProcess,
372 ep.ContextRecord, &ctx, sizeof(ctx), NULL);
378 prec = except->ExceptionPointers->ExceptionRecord;
379 pctx = except->ExceptionPointers->ContextRecord;
381 mdExcpt.ExceptionRecord.ExceptionCode = prec->ExceptionCode;
382 mdExcpt.ExceptionRecord.ExceptionFlags = prec->ExceptionFlags;
383 mdExcpt.ExceptionRecord.ExceptionRecord = (DWORD_PTR)prec->ExceptionRecord;
384 mdExcpt.ExceptionRecord.ExceptionAddress = (DWORD_PTR)prec->ExceptionAddress;
385 mdExcpt.ExceptionRecord.NumberParameters = prec->NumberParameters;
386 mdExcpt.ExceptionRecord.__unusedAlignment = 0;
387 for (i = 0; i < mdExcpt.ExceptionRecord.NumberParameters; i++)
388 mdExcpt.ExceptionRecord.ExceptionInformation[i] = (DWORD_PTR)prec->ExceptionInformation[i];
389 mdExcpt.ThreadContext.DataSize = sizeof(*pctx);
390 mdExcpt.ThreadContext.Rva = dc->rva + sizeof(mdExcpt);
392 append(dc, &mdExcpt, sizeof(mdExcpt));
393 append(dc, pctx, sizeof(*pctx));
396 /******************************************************************
399 * Write in File the modules from pcs
401 static void dump_modules(struct dump_context* dc, BOOL dump_elf)
403 MINIDUMP_MODULE mdModule;
404 MINIDUMP_MODULE_LIST mdModuleList;
406 MINIDUMP_STRING* ms = (MINIDUMP_STRING*)tmp;
411 for (i = nmod = 0; i < dc->num_module; i++)
413 if ((dc->module[i].is_elf && dump_elf) ||
414 (!dc->module[i].is_elf && !dump_elf))
418 mdModuleList.NumberOfModules = 0;
419 /* reserve space for mdModuleList
420 * FIXME: since we don't support 0 length arrays, we cannot use the
421 * size of mdModuleList
422 * FIXME: if we don't ask for all modules in cb, we'll get a hole in the file
425 dc->rva += sizeof(mdModuleList.NumberOfModules) + sizeof(mdModule) * nmod;
426 for (i = 0; i < dc->num_module; i++)
428 if ((dc->module[i].is_elf && !dump_elf) ||
429 (!dc->module[i].is_elf && dump_elf))
432 flags_out = ModuleWriteModule | ModuleWriteMiscRecord | ModuleWriteCvRecord;
433 if (dc->type & MiniDumpWithDataSegs)
434 flags_out |= ModuleWriteDataSeg;
435 if (dc->type & MiniDumpWithProcessThreadData)
436 flags_out |= ModuleWriteTlsData;
437 if (dc->type & MiniDumpWithCodeSegs)
438 flags_out |= ModuleWriteCodeSegs;
439 ms->Length = MultiByteToWideChar(CP_ACP, 0,
440 dc->module[i].name, -1,
441 NULL, 0) * sizeof(WCHAR);
442 if (sizeof(ULONG) + ms->Length > sizeof(tmp))
443 FIXME("Buffer overflow!!!\n");
444 MultiByteToWideChar(CP_ACP, 0, dc->module[i].name, -1,
445 ms->Buffer, ms->Length/sizeof(WCHAR));
449 MINIDUMP_CALLBACK_INPUT cbin;
450 MINIDUMP_CALLBACK_OUTPUT cbout;
452 cbin.ProcessId = dc->pid;
453 cbin.ProcessHandle = dc->hProcess;
454 cbin.CallbackType = ModuleCallback;
456 cbin.u.Module.FullPath = ms->Buffer;
457 cbin.u.Module.BaseOfImage = dc->module[i].base;
458 cbin.u.Module.SizeOfImage = dc->module[i].size;
459 cbin.u.Module.CheckSum = dc->module[i].checksum;
460 cbin.u.Module.TimeDateStamp = dc->module[i].timestamp;
461 memset(&cbin.u.Module.VersionInfo, 0, sizeof(cbin.u.Module.VersionInfo));
462 cbin.u.Module.CvRecord = NULL;
463 cbin.u.Module.SizeOfCvRecord = 0;
464 cbin.u.Module.MiscRecord = NULL;
465 cbin.u.Module.SizeOfMiscRecord = 0;
467 cbout.u.ModuleWriteFlags = flags_out;
468 if (!dc->cb->CallbackRoutine(dc->cb->CallbackParam, &cbin, &cbout))
470 flags_out &= cbout.u.ModuleWriteFlags;
472 if (flags_out & ModuleWriteModule)
474 mdModule.BaseOfImage = dc->module[i].base;
475 mdModule.SizeOfImage = dc->module[i].size;
476 mdModule.CheckSum = dc->module[i].checksum;
477 mdModule.TimeDateStamp = dc->module[i].timestamp;
478 mdModule.ModuleNameRva = dc->rva;
479 ms->Length -= sizeof(WCHAR);
480 append(dc, ms, sizeof(ULONG) + ms->Length);
481 memset(&mdModule.VersionInfo, 0, sizeof(mdModule.VersionInfo)); /* FIXME */
482 mdModule.CvRecord.DataSize = 0; /* FIXME */
483 mdModule.CvRecord.Rva = 0; /* FIXME */
484 mdModule.MiscRecord.DataSize = 0; /* FIXME */
485 mdModule.MiscRecord.Rva = 0; /* FIXME */
486 mdModule.Reserved0 = 0; /* FIXME */
487 mdModule.Reserved1 = 0; /* FIXME */
489 rva_base + sizeof(mdModuleList.NumberOfModules) +
490 mdModuleList.NumberOfModules++ * sizeof(mdModule),
491 &mdModule, sizeof(mdModule));
494 writeat(dc, rva_base, &mdModuleList.NumberOfModules,
495 sizeof(mdModuleList.NumberOfModules));
498 /******************************************************************
501 * Dumps into File the information about the system
503 static void dump_system_info(struct dump_context* dc)
505 MINIDUMP_SYSTEM_INFO mdSysInfo;
507 OSVERSIONINFOW osInfo;
511 GetSystemInfo(&sysInfo);
512 osInfo.dwOSVersionInfoSize = sizeof(osInfo);
513 GetVersionExW(&osInfo);
515 mdSysInfo.ProcessorArchitecture = sysInfo.u.s.wProcessorArchitecture;
516 mdSysInfo.ProcessorLevel = sysInfo.wProcessorLevel;
517 mdSysInfo.ProcessorRevision = sysInfo.wProcessorRevision;
518 mdSysInfo.u.s.NumberOfProcessors = sysInfo.dwNumberOfProcessors;
519 mdSysInfo.u.s.ProductType = VER_NT_WORKSTATION; /* FIXME */
520 mdSysInfo.MajorVersion = osInfo.dwMajorVersion;
521 mdSysInfo.MinorVersion = osInfo.dwMinorVersion;
522 mdSysInfo.BuildNumber = osInfo.dwBuildNumber;
523 mdSysInfo.PlatformId = osInfo.dwPlatformId;
525 mdSysInfo.CSDVersionRva = dc->rva + sizeof(mdSysInfo);
526 mdSysInfo.u1.Reserved1 = 0;
528 memset(&mdSysInfo.Cpu, 0, sizeof(mdSysInfo.Cpu));
530 append(dc, &mdSysInfo, sizeof(mdSysInfo));
532 slen = lstrlenW(osInfo.szCSDVersion) * sizeof(WCHAR);
533 WriteFile(dc->hFile, &slen, sizeof(slen), &written, NULL);
534 WriteFile(dc->hFile, osInfo.szCSDVersion, slen, &written, NULL);
535 dc->rva += sizeof(ULONG) + slen;
538 /******************************************************************
541 * Dumps into File the information about running threads
543 static void dump_threads(struct dump_context* dc,
544 const MINIDUMP_EXCEPTION_INFORMATION* except)
546 MINIDUMP_THREAD mdThd;
547 MINIDUMP_THREAD_LIST mdThdList;
553 mdThdList.NumberOfThreads = 0;
556 dc->rva += sizeof(mdThdList.NumberOfThreads) +
557 dc->spi->dwThreadCount * sizeof(mdThd);
559 for (i = 0; i < dc->spi->dwThreadCount; i++)
561 fetch_thread_info(dc, i, except, &mdThd, &ctx);
563 flags_out = ThreadWriteThread | ThreadWriteStack | ThreadWriteContext |
564 ThreadWriteInstructionWindow;
565 if (dc->type & MiniDumpWithProcessThreadData)
566 flags_out |= ThreadWriteThreadData;
567 if (dc->type & MiniDumpWithThreadInfo)
568 flags_out |= ThreadWriteThreadInfo;
572 MINIDUMP_CALLBACK_INPUT cbin;
573 MINIDUMP_CALLBACK_OUTPUT cbout;
575 cbin.ProcessId = dc->pid;
576 cbin.ProcessHandle = dc->hProcess;
577 cbin.CallbackType = ThreadCallback;
578 cbin.u.Thread.ThreadId = dc->spi->ti[i].dwThreadID;
579 cbin.u.Thread.ThreadHandle = 0; /* FIXME */
580 memcpy(&cbin.u.Thread.Context, &ctx, sizeof(CONTEXT));
581 cbin.u.Thread.SizeOfContext = sizeof(CONTEXT);
582 cbin.u.Thread.StackBase = mdThd.Stack.StartOfMemoryRange;
583 cbin.u.Thread.StackEnd = mdThd.Stack.StartOfMemoryRange +
584 mdThd.Stack.Memory.DataSize;
586 cbout.u.ThreadWriteFlags = flags_out;
587 if (!dc->cb->CallbackRoutine(dc->cb->CallbackParam, &cbin, &cbout))
589 flags_out &= cbout.u.ThreadWriteFlags;
591 if (flags_out & ThreadWriteThread)
593 if (ctx.ContextFlags && (flags_out & ThreadWriteContext))
595 mdThd.ThreadContext.Rva = dc->rva;
596 mdThd.ThreadContext.DataSize = sizeof(CONTEXT);
597 append(dc, &ctx, sizeof(CONTEXT));
599 if (mdThd.Stack.Memory.DataSize && (flags_out & ThreadWriteStack))
601 add_memory_block(dc, mdThd.Stack.StartOfMemoryRange,
602 mdThd.Stack.Memory.DataSize,
603 rva_base + sizeof(mdThdList.NumberOfThreads) +
604 mdThdList.NumberOfThreads * sizeof(mdThd) +
605 FIELD_OFFSET(MINIDUMP_THREAD, Stack.Memory.Rva));
608 rva_base + sizeof(mdThdList.NumberOfThreads) +
609 mdThdList.NumberOfThreads * sizeof(mdThd),
610 &mdThd, sizeof(mdThd));
611 mdThdList.NumberOfThreads++;
613 if (ctx.ContextFlags && (flags_out & ThreadWriteInstructionWindow))
615 /* FIXME: - Native dbghelp also dumps 0x80 bytes around EIP
616 * - also crop values across module boundaries,
617 * - and don't make it i386 dependent
619 /* add_memory_block(dc, ctx.Eip - 0x80, ctx.Eip + 0x80, 0); */
622 writeat(dc, rva_base,
623 &mdThdList.NumberOfThreads, sizeof(mdThdList.NumberOfThreads));
626 /******************************************************************
629 * dumps information about the memory of the process (stack of the threads)
631 static void dump_memory_info(struct dump_context* dc)
633 MINIDUMP_MEMORY_LIST mdMemList;
634 MINIDUMP_MEMORY_DESCRIPTOR mdMem;
636 unsigned i, pos, len;
640 mdMemList.NumberOfMemoryRanges = dc->num_mem;
641 append(dc, &mdMemList.NumberOfMemoryRanges,
642 sizeof(mdMemList.NumberOfMemoryRanges));
644 dc->rva += mdMemList.NumberOfMemoryRanges * sizeof(mdMem);
646 for (i = 0; i < dc->num_mem; i++)
648 mdMem.StartOfMemoryRange = dc->mem[i].base;
649 mdMem.Memory.Rva = dc->rva;
650 mdMem.Memory.DataSize = dc->mem[i].size;
651 SetFilePointer(dc->hFile, dc->rva, NULL, FILE_BEGIN);
652 for (pos = 0; pos < dc->mem[i].size; pos += sizeof(tmp))
654 len = min(dc->mem[i].size - pos, sizeof(tmp));
655 if (ReadProcessMemory(dc->hProcess,
656 (void*)(ULONG)(dc->mem[i].base + pos),
658 WriteFile(dc->hFile, tmp, len, &written, NULL);
660 dc->rva += mdMem.Memory.DataSize;
661 writeat(dc, rva_base + i * sizeof(mdMem), &mdMem, sizeof(mdMem));
664 writeat(dc, dc->mem[i].rva, &mdMem.Memory.Rva, sizeof(mdMem.Memory.Rva));
669 static void dump_misc_info(struct dump_context* dc)
671 MINIDUMP_MISC_INFO mmi;
673 mmi.SizeOfInfo = sizeof(mmi);
674 mmi.Flags1 = MINIDUMP_MISC1_PROCESS_ID;
675 mmi.ProcessId = dc->pid;
676 /* FIXME: create/user/kernel time */
677 append(dc, &mmi, sizeof(mmi));
680 /******************************************************************
681 * MiniDumpWriteDump (DEBUGHLP.@)
684 BOOL WINAPI MiniDumpWriteDump(HANDLE hProcess, DWORD pid, HANDLE hFile,
685 MINIDUMP_TYPE DumpType,
686 PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
687 PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
688 PMINIDUMP_CALLBACK_INFORMATION CallbackParam)
690 MINIDUMP_HEADER mdHead;
691 MINIDUMP_DIRECTORY mdDir;
692 DWORD i, nStreams, idx_stream;
693 struct dump_context dc;
695 dc.hProcess = hProcess;
700 dc.cb = CallbackParam;
706 if (!fetch_process_info(&dc)) return FALSE;
707 fetch_module_info(&dc);
710 nStreams = 6 + (ExceptionParam ? 1 : 0) +
711 (UserStreamParam ? UserStreamParam->UserStreamCount : 0);
713 if (DumpType & MiniDumpWithDataSegs)
714 FIXME("NIY MiniDumpWithDataSegs\n");
715 if (DumpType & MiniDumpWithFullMemory)
716 FIXME("NIY MiniDumpWithFullMemory\n");
717 if (DumpType & MiniDumpWithHandleData)
718 FIXME("NIY MiniDumpWithHandleData\n");
719 if (DumpType & MiniDumpFilterMemory)
720 FIXME("NIY MiniDumpFilterMemory\n");
721 if (DumpType & MiniDumpScanMemory)
722 FIXME("NIY MiniDumpScanMemory\n");
724 /* 2) write header */
725 mdHead.Signature = MINIDUMP_SIGNATURE;
726 mdHead.Version = MINIDUMP_VERSION;
727 mdHead.NumberOfStreams = nStreams;
728 mdHead.StreamDirectoryRva = sizeof(mdHead);
729 mdHead.u.TimeDateStamp = time(NULL);
730 mdHead.Flags = DumpType;
731 append(&dc, &mdHead, sizeof(mdHead));
733 /* 3) write stream directories */
734 dc.rva += nStreams * sizeof(mdDir);
737 /* 3.1) write data stream directories */
739 mdDir.StreamType = ThreadListStream;
740 mdDir.Location.Rva = dc.rva;
741 dump_threads(&dc, ExceptionParam);
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 = ModuleListStream;
747 mdDir.Location.Rva = dc.rva;
748 dump_modules(&dc, FALSE);
749 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
750 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
751 &mdDir, sizeof(mdDir));
753 mdDir.StreamType = 0xfff0; /* FIXME: this is part of MS reserved streams */
754 mdDir.Location.Rva = dc.rva;
755 dump_modules(&dc, TRUE);
756 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
757 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
758 &mdDir, sizeof(mdDir));
760 mdDir.StreamType = MemoryListStream;
761 mdDir.Location.Rva = dc.rva;
762 dump_memory_info(&dc);
763 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
764 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
765 &mdDir, sizeof(mdDir));
767 mdDir.StreamType = SystemInfoStream;
768 mdDir.Location.Rva = dc.rva;
769 dump_system_info(&dc);
770 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
771 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
772 &mdDir, sizeof(mdDir));
774 mdDir.StreamType = MiscInfoStream;
775 mdDir.Location.Rva = dc.rva;
777 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
778 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
779 &mdDir, sizeof(mdDir));
781 /* 3.2) write exception information (if any) */
784 mdDir.StreamType = ExceptionStream;
785 mdDir.Location.Rva = dc.rva;
786 dump_exception_info(&dc, ExceptionParam);
787 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
788 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
789 &mdDir, sizeof(mdDir));
792 /* 3.3) write user defined streams (if any) */
795 for (i = 0; i < UserStreamParam->UserStreamCount; i++)
797 mdDir.StreamType = UserStreamParam->UserStreamArray[i].Type;
798 mdDir.Location.DataSize = UserStreamParam->UserStreamArray[i].BufferSize;
799 mdDir.Location.Rva = dc.rva;
800 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
801 &mdDir, sizeof(mdDir));
802 append(&dc, UserStreamParam->UserStreamArray[i].Buffer,
803 UserStreamParam->UserStreamArray[i].BufferSize);
807 HeapFree(GetProcessHeap(), 0, dc.pcs_buffer);
808 HeapFree(GetProcessHeap(), 0, dc.mem);
809 HeapFree(GetProcessHeap(), 0, dc.module);
814 /******************************************************************
815 * MiniDumpReadDumpStream (DEBUGHLP.@)
819 BOOL WINAPI MiniDumpReadDumpStream(void* base, ULONG str_idx,
820 PMINIDUMP_DIRECTORY* pdir,
821 void** stream, ULONG* size)
823 MINIDUMP_HEADER* mdHead = (MINIDUMP_HEADER*)base;
825 if (mdHead->Signature == MINIDUMP_SIGNATURE)
827 MINIDUMP_DIRECTORY* dir;
830 dir = (MINIDUMP_DIRECTORY*)((char*)base + mdHead->StreamDirectoryRva);
831 for (i = 0; i < mdHead->NumberOfStreams; i++, dir++)
833 if (dir->StreamType == str_idx)
836 *stream = (char*)base + dir->Location.Rva;
837 *size = dir->Location.DataSize;
842 SetLastError(ERROR_INVALID_PARAMETER);