dbghelp: Zero-terminate module name in minidump.
[wine/wine64.git] / dlls / dbghelp / minidump.c
blob3bd0f740797482f13ae87b9fbd4998a8de680b1c
1 /*
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
21 #include <time.h>
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "dbghelp_private.h"
29 #include "winnls.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "psapi.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
37 struct dump_memory
39 ULONG base;
40 ULONG size;
41 ULONG rva;
44 struct dump_module
46 unsigned is_elf;
47 ULONG base;
48 ULONG size;
49 DWORD timestamp;
50 DWORD checksum;
51 WCHAR name[MAX_PATH];
54 struct dump_context
56 /* process & thread information */
57 HANDLE hProcess;
58 DWORD pid;
59 void* pcs_buffer;
60 SYSTEM_PROCESS_INFORMATION* spi;
61 /* module information */
62 struct dump_module* module;
63 unsigned num_module;
64 /* exception information */
65 /* output information */
66 MINIDUMP_TYPE type;
67 HANDLE hFile;
68 RVA rva;
69 struct dump_memory* mem;
70 unsigned num_mem;
71 /* callback information */
72 MINIDUMP_CALLBACK_INFORMATION* cb;
75 /******************************************************************
76 * fetch_process_info
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;
84 NTSTATUS nts;
86 dc->pcs_buffer = NULL;
87 if (!(dc->pcs_buffer = HeapAlloc(GetProcessHeap(), 0, buf_size))) return FALSE;
88 for (;;)
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,
94 buf_size *= 2);
95 if (!dc->pcs_buffer) return FALSE;
98 if (nts == STATUS_SUCCESS)
100 dc->spi = dc->pcs_buffer;
101 for (;;)
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;
111 dc->spi = NULL;
112 return FALSE;
115 static void fetch_thread_stack(struct dump_context* dc, const void* teb_addr,
116 const CONTEXT* ctx, MINIDUMP_MEMORY_DESCRIPTOR* mmd)
118 NT_TIB tib;
120 if (ReadProcessMemory(dc->hProcess, teb_addr, &tib, sizeof(tib), NULL))
122 #ifdef __i386__
123 /* limiting the stack dumping to the size actually used */
124 if (ctx->Esp)
125 mmd->StartOfMemoryRange = (ctx->Esp - 4);
126 else
127 mmd->StartOfMemoryRange = (ULONG_PTR)tib.StackLimit;
128 #elif defined(__powerpc__)
129 if (ctx->Iar)
130 mmd->StartOfMemoryRange = ctx->Iar - 4;
131 else
132 mmd->StartOfMemoryRange = (ULONG_PTR)tib.StackLimit;
133 #elif defined(__x86_64__)
134 if (ctx->Rsp)
135 mmd->StartOfMemoryRange = (ctx->Rsp - 8);
136 else
137 mmd->StartOfMemoryRange = (ULONG_PTR)tib.StackLimit;
138 #else
139 #error unsupported CPU
140 #endif
141 mmd->Memory.DataSize = (ULONG_PTR)tib.StackBase - mmd->StartOfMemoryRange;
145 /******************************************************************
146 * fetch_thread_info
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;
155 HANDLE hThread;
156 THREAD_BASIC_INFORMATION tbi;
158 memset(ctx, 0, sizeof(*ctx));
160 mdThd->ThreadId = dc->spi->ti[thd_idx].dwThreadID;
161 mdThd->SuspendCount = 0;
162 mdThd->Teb = 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());
175 return FALSE;
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)
197 CONTEXT lctx, *pctx;
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);
206 pctx = &lctx;
208 else pctx = except->ExceptionPointers->ContextRecord;
209 fetch_thread_stack(dc, tbi.TebBaseAddress, pctx, &mdThd->Stack);
213 CloseHandle(hThread);
214 return TRUE;
217 /******************************************************************
218 * add_module
220 * Add a module to a dump context
222 static BOOL add_module(struct dump_context* dc, const WCHAR* name,
223 DWORD base, DWORD size, DWORD timestamp, DWORD checksum,
224 BOOL is_elf)
226 if (!dc->module)
227 dc->module = HeapAlloc(GetProcessHeap(), 0,
228 ++dc->num_module * sizeof(*dc->module));
229 else
230 dc->module = HeapReAlloc(GetProcessHeap(), 0, dc->module,
231 ++dc->num_module * sizeof(*dc->module));
232 if (!dc->module) return FALSE;
233 if (is_elf ||
234 !GetModuleFileNameExW(dc->hProcess, (HMODULE)base,
235 dc->module[dc->num_module - 1].name,
236 sizeof(dc->module[dc->num_module - 1].name) / sizeof(WCHAR)))
237 lstrcpynW(dc->module[dc->num_module - 1].name, name,
238 sizeof(dc->module[dc->num_module - 1].name) / sizeof(WCHAR));
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;
245 return TRUE;
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(WCHAR* name, DWORD64 base, DWORD size,
254 void* user)
256 struct dump_context* dc = (struct dump_context*)user;
257 IMAGE_NT_HEADERS nth;
259 if (!validate_addr64(base)) return FALSE;
261 if (pe_load_nt_header(dc->hProcess, base, &nth))
262 add_module((struct dump_context*)user, name, base, size,
263 nth.FileHeader.TimeDateStamp, nth.OptionalHeader.CheckSum,
264 FALSE);
265 return TRUE;
268 /******************************************************************
269 * fetch_elf_module_info_cb
271 * Callback for accumulating in dump_context an ELF modules set
273 static BOOL fetch_elf_module_info_cb(const WCHAR* name, unsigned long base,
274 void* user)
276 struct dump_context* dc = (struct dump_context*)user;
277 DWORD rbase, size, checksum;
279 /* FIXME: there's no relevant timestamp on ELF modules */
280 /* NB: if we have a non-null base from the live-target use it (whenever
281 * the ELF module is relocatable or not). If we have a null base (ELF
282 * module isn't relocatable) then grab its base address from ELF file
284 if (!elf_fetch_file_info(name, &rbase, &size, &checksum))
285 size = checksum = 0;
286 add_module(dc, name, base ? base : rbase, size, 0 /* FIXME */, checksum, TRUE);
287 return TRUE;
290 static void fetch_module_info(struct dump_context* dc)
292 EnumerateLoadedModulesW64(dc->hProcess, fetch_pe_module_info_cb, dc);
293 /* Since we include ELF modules in a separate stream from the regular PE ones,
294 * we can always include those ELF modules (they don't eat lots of space)
295 * And it's always a good idea to have a trace of the loaded ELF modules for
296 * a given application in a post mortem debugging condition.
298 elf_enum_modules(dc->hProcess, fetch_elf_module_info_cb, dc);
301 /******************************************************************
302 * add_memory_block
304 * Add a memory block to be dumped in a minidump
305 * If rva is non 0, it's the rva in the minidump where has to be stored
306 * also the rva of the memory block when written (this allows to reference
307 * a memory block from outside the list of memory blocks).
309 static void add_memory_block(struct dump_context* dc, ULONG64 base, ULONG size, ULONG rva)
311 if (dc->mem)
312 dc->mem = HeapReAlloc(GetProcessHeap(), 0, dc->mem,
313 ++dc->num_mem * sizeof(*dc->mem));
314 else
315 dc->mem = HeapAlloc(GetProcessHeap(), 0, ++dc->num_mem * sizeof(*dc->mem));
316 if (dc->mem)
318 dc->mem[dc->num_mem - 1].base = base;
319 dc->mem[dc->num_mem - 1].size = size;
320 dc->mem[dc->num_mem - 1].rva = rva;
322 else dc->num_mem = 0;
325 /******************************************************************
326 * writeat
328 * Writes a chunk of data at a given position in the minidump
330 static void writeat(struct dump_context* dc, RVA rva, const void* data, unsigned size)
332 DWORD written;
334 SetFilePointer(dc->hFile, rva, NULL, FILE_BEGIN);
335 WriteFile(dc->hFile, data, size, &written, NULL);
338 /******************************************************************
339 * append
341 * writes a new chunk of data to the minidump, increasing the current
342 * rva in dc
344 static void append(struct dump_context* dc, void* data, unsigned size)
346 writeat(dc, dc->rva, data, size);
347 dc->rva += size;
350 /******************************************************************
351 * dump_exception_info
353 * Write in File the exception information from pcs
355 static void dump_exception_info(struct dump_context* dc,
356 const MINIDUMP_EXCEPTION_INFORMATION* except)
358 MINIDUMP_EXCEPTION_STREAM mdExcpt;
359 EXCEPTION_RECORD rec, *prec;
360 CONTEXT ctx, *pctx;
361 int i;
363 mdExcpt.ThreadId = except->ThreadId;
364 mdExcpt.__alignment = 0;
365 if (except->ClientPointers)
367 EXCEPTION_POINTERS ep;
369 ReadProcessMemory(dc->hProcess,
370 except->ExceptionPointers, &ep, sizeof(ep), NULL);
371 ReadProcessMemory(dc->hProcess,
372 ep.ExceptionRecord, &rec, sizeof(rec), NULL);
373 ReadProcessMemory(dc->hProcess,
374 ep.ContextRecord, &ctx, sizeof(ctx), NULL);
375 prec = &rec;
376 pctx = &ctx;
378 else
380 prec = except->ExceptionPointers->ExceptionRecord;
381 pctx = except->ExceptionPointers->ContextRecord;
383 mdExcpt.ExceptionRecord.ExceptionCode = prec->ExceptionCode;
384 mdExcpt.ExceptionRecord.ExceptionFlags = prec->ExceptionFlags;
385 mdExcpt.ExceptionRecord.ExceptionRecord = (DWORD_PTR)prec->ExceptionRecord;
386 mdExcpt.ExceptionRecord.ExceptionAddress = (DWORD_PTR)prec->ExceptionAddress;
387 mdExcpt.ExceptionRecord.NumberParameters = prec->NumberParameters;
388 mdExcpt.ExceptionRecord.__unusedAlignment = 0;
389 for (i = 0; i < mdExcpt.ExceptionRecord.NumberParameters; i++)
390 mdExcpt.ExceptionRecord.ExceptionInformation[i] = (DWORD_PTR)prec->ExceptionInformation[i];
391 mdExcpt.ThreadContext.DataSize = sizeof(*pctx);
392 mdExcpt.ThreadContext.Rva = dc->rva + sizeof(mdExcpt);
394 append(dc, &mdExcpt, sizeof(mdExcpt));
395 append(dc, pctx, sizeof(*pctx));
398 /******************************************************************
399 * dump_modules
401 * Write in File the modules from pcs
403 static void dump_modules(struct dump_context* dc, BOOL dump_elf)
405 MINIDUMP_MODULE mdModule;
406 MINIDUMP_MODULE_LIST mdModuleList;
407 char tmp[1024];
408 MINIDUMP_STRING* ms = (MINIDUMP_STRING*)tmp;
409 ULONG i, nmod;
410 RVA rva_base;
411 DWORD flags_out;
413 for (i = nmod = 0; i < dc->num_module; i++)
415 if ((dc->module[i].is_elf && dump_elf) ||
416 (!dc->module[i].is_elf && !dump_elf))
417 nmod++;
420 mdModuleList.NumberOfModules = 0;
421 /* reserve space for mdModuleList
422 * FIXME: since we don't support 0 length arrays, we cannot use the
423 * size of mdModuleList
424 * FIXME: if we don't ask for all modules in cb, we'll get a hole in the file
426 rva_base = dc->rva;
427 dc->rva += sizeof(mdModuleList.NumberOfModules) + sizeof(mdModule) * nmod;
428 for (i = 0; i < dc->num_module; i++)
430 if ((dc->module[i].is_elf && !dump_elf) ||
431 (!dc->module[i].is_elf && dump_elf))
432 continue;
434 flags_out = ModuleWriteModule | ModuleWriteMiscRecord | ModuleWriteCvRecord;
435 if (dc->type & MiniDumpWithDataSegs)
436 flags_out |= ModuleWriteDataSeg;
437 if (dc->type & MiniDumpWithProcessThreadData)
438 flags_out |= ModuleWriteTlsData;
439 if (dc->type & MiniDumpWithCodeSegs)
440 flags_out |= ModuleWriteCodeSegs;
441 ms->Length = (lstrlenW(dc->module[i].name) + 1) * sizeof(WCHAR);
442 if (sizeof(ULONG) + ms->Length > sizeof(tmp))
443 FIXME("Buffer overflow!!!\n");
444 lstrcpyW(ms->Buffer, dc->module[i].name);
446 if (dc->cb)
448 MINIDUMP_CALLBACK_INPUT cbin;
449 MINIDUMP_CALLBACK_OUTPUT cbout;
451 cbin.ProcessId = dc->pid;
452 cbin.ProcessHandle = dc->hProcess;
453 cbin.CallbackType = ModuleCallback;
455 cbin.u.Module.FullPath = ms->Buffer;
456 cbin.u.Module.BaseOfImage = dc->module[i].base;
457 cbin.u.Module.SizeOfImage = dc->module[i].size;
458 cbin.u.Module.CheckSum = dc->module[i].checksum;
459 cbin.u.Module.TimeDateStamp = dc->module[i].timestamp;
460 memset(&cbin.u.Module.VersionInfo, 0, sizeof(cbin.u.Module.VersionInfo));
461 cbin.u.Module.CvRecord = NULL;
462 cbin.u.Module.SizeOfCvRecord = 0;
463 cbin.u.Module.MiscRecord = NULL;
464 cbin.u.Module.SizeOfMiscRecord = 0;
466 cbout.u.ModuleWriteFlags = flags_out;
467 if (!dc->cb->CallbackRoutine(dc->cb->CallbackParam, &cbin, &cbout))
468 continue;
469 flags_out &= cbout.u.ModuleWriteFlags;
471 if (flags_out & ModuleWriteModule)
473 mdModule.BaseOfImage = dc->module[i].base;
474 mdModule.SizeOfImage = dc->module[i].size;
475 mdModule.CheckSum = dc->module[i].checksum;
476 mdModule.TimeDateStamp = dc->module[i].timestamp;
477 mdModule.ModuleNameRva = dc->rva;
478 ms->Length -= sizeof(WCHAR);
479 append(dc, ms, sizeof(ULONG) + ms->Length + sizeof(WCHAR));
480 memset(&mdModule.VersionInfo, 0, sizeof(mdModule.VersionInfo)); /* FIXME */
481 mdModule.CvRecord.DataSize = 0; /* FIXME */
482 mdModule.CvRecord.Rva = 0; /* FIXME */
483 mdModule.MiscRecord.DataSize = 0; /* FIXME */
484 mdModule.MiscRecord.Rva = 0; /* FIXME */
485 mdModule.Reserved0 = 0; /* FIXME */
486 mdModule.Reserved1 = 0; /* FIXME */
487 writeat(dc,
488 rva_base + sizeof(mdModuleList.NumberOfModules) +
489 mdModuleList.NumberOfModules++ * sizeof(mdModule),
490 &mdModule, sizeof(mdModule));
493 writeat(dc, rva_base, &mdModuleList.NumberOfModules,
494 sizeof(mdModuleList.NumberOfModules));
497 /******************************************************************
498 * dump_system_info
500 * Dumps into File the information about the system
502 static void dump_system_info(struct dump_context* dc)
504 MINIDUMP_SYSTEM_INFO mdSysInfo;
505 SYSTEM_INFO sysInfo;
506 OSVERSIONINFOW osInfo;
507 DWORD written;
508 ULONG slen;
510 GetSystemInfo(&sysInfo);
511 osInfo.dwOSVersionInfoSize = sizeof(osInfo);
512 GetVersionExW(&osInfo);
514 mdSysInfo.ProcessorArchitecture = sysInfo.u.s.wProcessorArchitecture;
515 mdSysInfo.ProcessorLevel = sysInfo.wProcessorLevel;
516 mdSysInfo.ProcessorRevision = sysInfo.wProcessorRevision;
517 mdSysInfo.u.s.NumberOfProcessors = sysInfo.dwNumberOfProcessors;
518 mdSysInfo.u.s.ProductType = VER_NT_WORKSTATION; /* FIXME */
519 mdSysInfo.MajorVersion = osInfo.dwMajorVersion;
520 mdSysInfo.MinorVersion = osInfo.dwMinorVersion;
521 mdSysInfo.BuildNumber = osInfo.dwBuildNumber;
522 mdSysInfo.PlatformId = osInfo.dwPlatformId;
524 mdSysInfo.CSDVersionRva = dc->rva + sizeof(mdSysInfo);
525 mdSysInfo.u1.Reserved1 = 0;
527 memset(&mdSysInfo.Cpu, 0, sizeof(mdSysInfo.Cpu));
529 append(dc, &mdSysInfo, sizeof(mdSysInfo));
531 slen = lstrlenW(osInfo.szCSDVersion) * sizeof(WCHAR);
532 WriteFile(dc->hFile, &slen, sizeof(slen), &written, NULL);
533 WriteFile(dc->hFile, osInfo.szCSDVersion, slen, &written, NULL);
534 dc->rva += sizeof(ULONG) + slen;
537 /******************************************************************
538 * dump_threads
540 * Dumps into File the information about running threads
542 static void dump_threads(struct dump_context* dc,
543 const MINIDUMP_EXCEPTION_INFORMATION* except)
545 MINIDUMP_THREAD mdThd;
546 MINIDUMP_THREAD_LIST mdThdList;
547 unsigned i;
548 RVA rva_base;
549 DWORD flags_out;
550 CONTEXT ctx;
552 mdThdList.NumberOfThreads = 0;
554 rva_base = dc->rva;
555 dc->rva += sizeof(mdThdList.NumberOfThreads) +
556 dc->spi->dwThreadCount * sizeof(mdThd);
558 for (i = 0; i < dc->spi->dwThreadCount; i++)
560 fetch_thread_info(dc, i, except, &mdThd, &ctx);
562 flags_out = ThreadWriteThread | ThreadWriteStack | ThreadWriteContext |
563 ThreadWriteInstructionWindow;
564 if (dc->type & MiniDumpWithProcessThreadData)
565 flags_out |= ThreadWriteThreadData;
566 if (dc->type & MiniDumpWithThreadInfo)
567 flags_out |= ThreadWriteThreadInfo;
569 if (dc->cb)
571 MINIDUMP_CALLBACK_INPUT cbin;
572 MINIDUMP_CALLBACK_OUTPUT cbout;
574 cbin.ProcessId = dc->pid;
575 cbin.ProcessHandle = dc->hProcess;
576 cbin.CallbackType = ThreadCallback;
577 cbin.u.Thread.ThreadId = dc->spi->ti[i].dwThreadID;
578 cbin.u.Thread.ThreadHandle = 0; /* FIXME */
579 memcpy(&cbin.u.Thread.Context, &ctx, sizeof(CONTEXT));
580 cbin.u.Thread.SizeOfContext = sizeof(CONTEXT);
581 cbin.u.Thread.StackBase = mdThd.Stack.StartOfMemoryRange;
582 cbin.u.Thread.StackEnd = mdThd.Stack.StartOfMemoryRange +
583 mdThd.Stack.Memory.DataSize;
585 cbout.u.ThreadWriteFlags = flags_out;
586 if (!dc->cb->CallbackRoutine(dc->cb->CallbackParam, &cbin, &cbout))
587 continue;
588 flags_out &= cbout.u.ThreadWriteFlags;
590 if (flags_out & ThreadWriteThread)
592 if (ctx.ContextFlags && (flags_out & ThreadWriteContext))
594 mdThd.ThreadContext.Rva = dc->rva;
595 mdThd.ThreadContext.DataSize = sizeof(CONTEXT);
596 append(dc, &ctx, sizeof(CONTEXT));
598 if (mdThd.Stack.Memory.DataSize && (flags_out & ThreadWriteStack))
600 add_memory_block(dc, mdThd.Stack.StartOfMemoryRange,
601 mdThd.Stack.Memory.DataSize,
602 rva_base + sizeof(mdThdList.NumberOfThreads) +
603 mdThdList.NumberOfThreads * sizeof(mdThd) +
604 FIELD_OFFSET(MINIDUMP_THREAD, Stack.Memory.Rva));
606 writeat(dc,
607 rva_base + sizeof(mdThdList.NumberOfThreads) +
608 mdThdList.NumberOfThreads * sizeof(mdThd),
609 &mdThd, sizeof(mdThd));
610 mdThdList.NumberOfThreads++;
612 if (ctx.ContextFlags && (flags_out & ThreadWriteInstructionWindow))
614 /* FIXME: - Native dbghelp also dumps 0x80 bytes around EIP
615 * - also crop values across module boundaries,
616 * - and don't make it i386 dependent
618 /* add_memory_block(dc, ctx.Eip - 0x80, ctx.Eip + 0x80, 0); */
621 writeat(dc, rva_base,
622 &mdThdList.NumberOfThreads, sizeof(mdThdList.NumberOfThreads));
625 /******************************************************************
626 * dump_memory_info
628 * dumps information about the memory of the process (stack of the threads)
630 static void dump_memory_info(struct dump_context* dc)
632 MINIDUMP_MEMORY_LIST mdMemList;
633 MINIDUMP_MEMORY_DESCRIPTOR mdMem;
634 DWORD written;
635 unsigned i, pos, len;
636 RVA rva_base;
637 char tmp[1024];
639 mdMemList.NumberOfMemoryRanges = dc->num_mem;
640 append(dc, &mdMemList.NumberOfMemoryRanges,
641 sizeof(mdMemList.NumberOfMemoryRanges));
642 rva_base = dc->rva;
643 dc->rva += mdMemList.NumberOfMemoryRanges * sizeof(mdMem);
645 for (i = 0; i < dc->num_mem; i++)
647 mdMem.StartOfMemoryRange = dc->mem[i].base;
648 mdMem.Memory.Rva = dc->rva;
649 mdMem.Memory.DataSize = dc->mem[i].size;
650 SetFilePointer(dc->hFile, dc->rva, NULL, FILE_BEGIN);
651 for (pos = 0; pos < dc->mem[i].size; pos += sizeof(tmp))
653 len = min(dc->mem[i].size - pos, sizeof(tmp));
654 if (ReadProcessMemory(dc->hProcess,
655 (void*)(ULONG)(dc->mem[i].base + pos),
656 tmp, len, NULL))
657 WriteFile(dc->hFile, tmp, len, &written, NULL);
659 dc->rva += mdMem.Memory.DataSize;
660 writeat(dc, rva_base + i * sizeof(mdMem), &mdMem, sizeof(mdMem));
661 if (dc->mem[i].rva)
663 writeat(dc, dc->mem[i].rva, &mdMem.Memory.Rva, sizeof(mdMem.Memory.Rva));
668 static void dump_misc_info(struct dump_context* dc)
670 MINIDUMP_MISC_INFO mmi;
672 mmi.SizeOfInfo = sizeof(mmi);
673 mmi.Flags1 = MINIDUMP_MISC1_PROCESS_ID;
674 mmi.ProcessId = dc->pid;
675 /* FIXME: create/user/kernel time */
676 append(dc, &mmi, sizeof(mmi));
679 /******************************************************************
680 * MiniDumpWriteDump (DEBUGHLP.@)
683 BOOL WINAPI MiniDumpWriteDump(HANDLE hProcess, DWORD pid, HANDLE hFile,
684 MINIDUMP_TYPE DumpType,
685 PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
686 PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
687 PMINIDUMP_CALLBACK_INFORMATION CallbackParam)
689 MINIDUMP_HEADER mdHead;
690 MINIDUMP_DIRECTORY mdDir;
691 DWORD i, nStreams, idx_stream;
692 struct dump_context dc;
694 dc.hProcess = hProcess;
695 dc.hFile = hFile;
696 dc.pid = pid;
697 dc.module = NULL;
698 dc.num_module = 0;
699 dc.cb = CallbackParam;
700 dc.type = DumpType;
701 dc.mem = NULL;
702 dc.num_mem = 0;
703 dc.rva = 0;
705 if (!fetch_process_info(&dc)) return FALSE;
706 fetch_module_info(&dc);
708 /* 1) init */
709 nStreams = 6 + (ExceptionParam ? 1 : 0) +
710 (UserStreamParam ? UserStreamParam->UserStreamCount : 0);
712 if (DumpType & MiniDumpWithDataSegs)
713 FIXME("NIY MiniDumpWithDataSegs\n");
714 if (DumpType & MiniDumpWithFullMemory)
715 FIXME("NIY MiniDumpWithFullMemory\n");
716 if (DumpType & MiniDumpWithHandleData)
717 FIXME("NIY MiniDumpWithHandleData\n");
718 if (DumpType & MiniDumpFilterMemory)
719 FIXME("NIY MiniDumpFilterMemory\n");
720 if (DumpType & MiniDumpScanMemory)
721 FIXME("NIY MiniDumpScanMemory\n");
723 /* 2) write header */
724 mdHead.Signature = MINIDUMP_SIGNATURE;
725 mdHead.Version = MINIDUMP_VERSION;
726 mdHead.NumberOfStreams = nStreams;
727 mdHead.StreamDirectoryRva = sizeof(mdHead);
728 mdHead.u.TimeDateStamp = time(NULL);
729 mdHead.Flags = DumpType;
730 append(&dc, &mdHead, sizeof(mdHead));
732 /* 3) write stream directories */
733 dc.rva += nStreams * sizeof(mdDir);
734 idx_stream = 0;
736 /* 3.1) write data stream directories */
738 mdDir.StreamType = ThreadListStream;
739 mdDir.Location.Rva = dc.rva;
740 dump_threads(&dc, ExceptionParam);
741 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
742 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
743 &mdDir, sizeof(mdDir));
745 mdDir.StreamType = ModuleListStream;
746 mdDir.Location.Rva = dc.rva;
747 dump_modules(&dc, FALSE);
748 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
749 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
750 &mdDir, sizeof(mdDir));
752 mdDir.StreamType = 0xfff0; /* FIXME: this is part of MS reserved streams */
753 mdDir.Location.Rva = dc.rva;
754 dump_modules(&dc, TRUE);
755 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
756 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
757 &mdDir, sizeof(mdDir));
759 mdDir.StreamType = MemoryListStream;
760 mdDir.Location.Rva = dc.rva;
761 dump_memory_info(&dc);
762 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
763 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
764 &mdDir, sizeof(mdDir));
766 mdDir.StreamType = SystemInfoStream;
767 mdDir.Location.Rva = dc.rva;
768 dump_system_info(&dc);
769 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
770 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
771 &mdDir, sizeof(mdDir));
773 mdDir.StreamType = MiscInfoStream;
774 mdDir.Location.Rva = dc.rva;
775 dump_misc_info(&dc);
776 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
777 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
778 &mdDir, sizeof(mdDir));
780 /* 3.2) write exception information (if any) */
781 if (ExceptionParam)
783 mdDir.StreamType = ExceptionStream;
784 mdDir.Location.Rva = dc.rva;
785 dump_exception_info(&dc, ExceptionParam);
786 mdDir.Location.DataSize = dc.rva - mdDir.Location.Rva;
787 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
788 &mdDir, sizeof(mdDir));
791 /* 3.3) write user defined streams (if any) */
792 if (UserStreamParam)
794 for (i = 0; i < UserStreamParam->UserStreamCount; i++)
796 mdDir.StreamType = UserStreamParam->UserStreamArray[i].Type;
797 mdDir.Location.DataSize = UserStreamParam->UserStreamArray[i].BufferSize;
798 mdDir.Location.Rva = dc.rva;
799 writeat(&dc, mdHead.StreamDirectoryRva + idx_stream++ * sizeof(mdDir),
800 &mdDir, sizeof(mdDir));
801 append(&dc, UserStreamParam->UserStreamArray[i].Buffer,
802 UserStreamParam->UserStreamArray[i].BufferSize);
806 HeapFree(GetProcessHeap(), 0, dc.pcs_buffer);
807 HeapFree(GetProcessHeap(), 0, dc.mem);
808 HeapFree(GetProcessHeap(), 0, dc.module);
810 return TRUE;
813 /******************************************************************
814 * MiniDumpReadDumpStream (DEBUGHLP.@)
818 BOOL WINAPI MiniDumpReadDumpStream(void* base, ULONG str_idx,
819 PMINIDUMP_DIRECTORY* pdir,
820 void** stream, ULONG* size)
822 MINIDUMP_HEADER* mdHead = (MINIDUMP_HEADER*)base;
824 if (mdHead->Signature == MINIDUMP_SIGNATURE)
826 MINIDUMP_DIRECTORY* dir;
827 int i;
829 dir = (MINIDUMP_DIRECTORY*)((char*)base + mdHead->StreamDirectoryRva);
830 for (i = 0; i < mdHead->NumberOfStreams; i++, dir++)
832 if (dir->StreamType == str_idx)
834 *pdir = dir;
835 *stream = (char*)base + dir->Location.Rva;
836 *size = dir->Location.DataSize;
837 return TRUE;
841 SetLastError(ERROR_INVALID_PARAMETER);
842 return FALSE;