minidump.c 17.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *	MiniDump dumping utility
 *
 * 	Copyright 2005 Eric Pouech
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 */

#include "config.h"
#include <stdarg.h>

#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "windef.h"
#include "winbase.h"
#include "winver.h"
#include "dbghelp.h"
#include "winedump.h"

static void dump_mdmp_data(const MINIDUMP_LOCATION_DESCRIPTOR* md, const char* pfx)
{
    if (md->DataSize)
        dump_data(PRD(md->Rva, md->DataSize), md->DataSize, pfx);
}

static void dump_mdmp_string(DWORD rva)
{
40
    const MINIDUMP_STRING*      ms = PRD(rva, sizeof(MINIDUMP_STRING));
41 42 43 44 45 46
    if (ms)
        dump_unicode_str( ms->Buffer, ms->Length / sizeof(WCHAR) );
    else
        printf("<<?>>");
}

47
static const MINIDUMP_DIRECTORY* get_mdmp_dir(const MINIDUMP_HEADER* hdr, unsigned int str_idx)
48
{
49 50
    const MINIDUMP_DIRECTORY*   dir;
    unsigned int                i;
51 52 53 54 55 56 57 58 59 60 61

    for (i = 0; i < hdr->NumberOfStreams; i++)
    {
        dir = PRD(hdr->StreamDirectoryRva + i * sizeof(MINIDUMP_DIRECTORY), 
                  sizeof(MINIDUMP_DIRECTORY));
        if (!dir) continue;
        if (dir->StreamType == str_idx) return dir;
    }
    return NULL;
}

62 63 64 65 66 67 68
enum FileSig get_kind_mdmp(void)
{
    const DWORD*        pdw;

    pdw = PRD(0, sizeof(DWORD));
    if (!pdw) {printf("Can't get main signature, aborting\n"); return SIG_UNKNOWN;}

69
    if (*pdw == 0x504D444D /* "MDMP" */) return SIG_MDMP;
70 71 72
    return SIG_UNKNOWN;
}

73 74
void mdmp_dump(void)
{
75
    const MINIDUMP_HEADER*      hdr = PRD(0, sizeof(MINIDUMP_HEADER));
76 77 78
    ULONG                       idx, ndir = 0;
    const MINIDUMP_DIRECTORY*   dir;
    const void*                 stream;
79 80 81 82 83 84 85

    if (!hdr)
    {
        printf("Cannot get Minidump header\n");
        return;
    }

86 87 88 89 90
    printf("Signature: %u (%.4s)\n", hdr->Signature, (const char*)&hdr->Signature);
    printf("Version: %x\n", hdr->Version);
    printf("NumberOfStreams: %u\n", hdr->NumberOfStreams);
    printf("StreamDirectoryRva: %u\n", hdr->StreamDirectoryRva);
    printf("CheckSum: %u\n", hdr->CheckSum);
91
    printf("TimeDateStamp: %s\n", get_time_str(hdr->u.TimeDateStamp));
92
    printf("Flags: %x%08x\n", (DWORD)(hdr->Flags >> 32), (DWORD)hdr->Flags);
93 94 95 96 97 98

    for (idx = 0; idx <= LastReservedStream; idx++)
    {
        if (!(dir = get_mdmp_dir(hdr, idx))) continue;

        stream = PRD(dir->Location.Rva, dir->Location.DataSize);
99
        printf("Directory [%u]: ", ndir++);
100 101 102 103
        switch (dir->StreamType)
        {
        case ThreadListStream:
        {
104
            const MINIDUMP_THREAD_LIST* mtl = (const MINIDUMP_THREAD_LIST*)stream;
105
            const MINIDUMP_THREAD*      mt = mtl->Threads;
106
            unsigned int                i;
107

108
            printf("Threads: %u\n", mtl->NumberOfThreads);
109 110 111
            for (i = 0; i < mtl->NumberOfThreads; i++, mt++)
            {
                printf("  Thread: #%d\n", i);
112 113 114 115 116 117
                printf("    ThreadId: %u\n", mt->ThreadId);
                printf("    SuspendCount: %u\n", mt->SuspendCount);
                printf("    PriorityClass: %u\n", mt->PriorityClass);
                printf("    Priority: %u\n", mt->Priority);
                printf("    Teb: 0x%x%08x\n", (DWORD)(mt->Teb >> 32), (DWORD)mt->Teb);
                printf("    Stack: 0x%x%08x-0x%x%08x\n",
118 119 120 121
                       (DWORD)(mt->Stack.StartOfMemoryRange >> 32),
                       (DWORD)mt->Stack.StartOfMemoryRange,
                       (DWORD)((mt->Stack.StartOfMemoryRange + mt->Stack.Memory.DataSize) >> 32),
                       (DWORD)(mt->Stack.StartOfMemoryRange + mt->Stack.Memory.DataSize));
122
                dump_mdmp_data(&mt->Stack.Memory, "    ");
123
                printf("    ThreadContext:\n");
124
                dump_mdmp_data(&mt->ThreadContext, "    ");
125 126 127 128 129 130
            }
        }
        break;
        case ModuleListStream:
        case 0xFFF0:
        {
131
            const MINIDUMP_MODULE_LIST* mml = (const MINIDUMP_MODULE_LIST*)stream;
132
            const MINIDUMP_MODULE*      mm = mml->Modules;
133 134 135
            unsigned int                i;
            const char*                 p1;
            const char*                 p2;
136

137
            printf("Modules (%s): %u\n",
138 139 140 141 142
                   dir->StreamType == ModuleListStream ? "PE" : "ELF",
                   mml->NumberOfModules);
            for (i = 0; i < mml->NumberOfModules; i++, mm++)
            {
                printf("  Module #%d:\n", i);
143
                printf("    BaseOfImage: 0x%x%08x\n",
144
		    (DWORD)(mm->BaseOfImage >> 32), (DWORD) mm->BaseOfImage);
145 146
                printf("    SizeOfImage: %u\n", mm->SizeOfImage);
                printf("    CheckSum: %u\n", mm->CheckSum);
147 148 149 150 151
                printf("    TimeDateStamp: %s\n", get_time_str(mm->TimeDateStamp));
                printf("    ModuleName: ");
                dump_mdmp_string(mm->ModuleNameRva);
                printf("\n");
                printf("    VersionInfo:\n");
152 153
                printf("      dwSignature: %x\n", mm->VersionInfo.dwSignature);
                printf("      dwStrucVersion: %x\n",
154 155 156 157 158 159 160 161 162 163 164
                       mm->VersionInfo.dwStrucVersion);
                printf("      dwFileVersion: %d,%d,%d,%d\n", 
                       HIWORD(mm->VersionInfo.dwFileVersionMS),
                       LOWORD(mm->VersionInfo.dwFileVersionMS),
                       HIWORD(mm->VersionInfo.dwFileVersionLS),
                       LOWORD(mm->VersionInfo.dwFileVersionLS));
                printf("      dwProductVersion %d,%d,%d,%d\n",
                       HIWORD(mm->VersionInfo.dwProductVersionMS),
                       LOWORD(mm->VersionInfo.dwProductVersionMS),
                       HIWORD(mm->VersionInfo.dwProductVersionLS),
                       LOWORD(mm->VersionInfo.dwProductVersionLS));
165
                printf("      dwFileFlagsMask: %u\n",
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                       mm->VersionInfo.dwFileFlagsMask);
                printf("      dwFileFlags: %s%s%s%s%s%s\n", 
                       mm->VersionInfo.dwFileFlags & VS_FF_DEBUG ? "Debug " : "",
                       mm->VersionInfo.dwFileFlags & VS_FF_INFOINFERRED ? "Inferred " : "",
                       mm->VersionInfo.dwFileFlags & VS_FF_PATCHED ? "Patched " : "",
                       mm->VersionInfo.dwFileFlags & VS_FF_PRERELEASE ? "PreRelease " : "",
                       mm->VersionInfo.dwFileFlags & VS_FF_PRIVATEBUILD ? "PrivateBuild " : "",
                       mm->VersionInfo.dwFileFlags & VS_FF_SPECIALBUILD ? "SpecialBuild " : "");
                if (mm->VersionInfo.dwFileOS)
                {
                    switch (mm->VersionInfo.dwFileOS & 0x000F)
                    {
                    case VOS__BASE:     p1 = "_base"; break;
                    case VOS__WINDOWS16:p1 = "16 bit Windows"; break;
                    case VOS__PM16:     p1 = "16 bit Presentation Manager"; break;
                    case VOS__PM32:     p1 = "32 bit Presentation Manager"; break;
                    case VOS__WINDOWS32:p1 = "32 bit Windows"; break;
                    default:            p1 = "---"; break;
                    }
                    switch (mm->VersionInfo.dwFileOS & 0xF0000)
                    {
                    case VOS_UNKNOWN:   p2 = "unknown"; break;
                    case VOS_DOS:       p2 = "DOS"; break;
                    case VOS_OS216:     p2 = "16 bit OS/2"; break;
                    case VOS_OS232:     p2 = "32 bit OS/2"; break;
                    case VOS_NT:        p2 = "Windows NT"; break;
                    default:            p2 = "---"; break;
                    }
                    printf("      dwFileOS: %s running on %s\n", p1, p2);
                }
                else printf("      dwFileOS: 0\n");
                switch (mm->VersionInfo.dwFileType)
                {
                case VFT_UNKNOWN:       p1 = "Unknown"; break;
                case VFT_APP:           p1 = "Application"; break;
                case VFT_DLL:           p1 = "DLL"; break;
                case VFT_DRV:           p1 = "Driver"; break;
                case VFT_FONT:          p1 = "Font"; break;
                case VFT_VXD:           p1 = "VxD"; break;
                case VFT_STATIC_LIB:    p1 = "Static Library"; break;
                default:                p1 = "---"; break;
                }
                printf("      dwFileType: %s\n", p1);
209
                printf("      dwFileSubtype: %u\n",
210
                       mm->VersionInfo.dwFileSubtype);
211
                printf("      dwFileDate: %x%08x\n",
212
                       mm->VersionInfo.dwFileDateMS, mm->VersionInfo.dwFileDateLS);
213
                printf("    CvRecord: <%u>\n", mm->CvRecord.DataSize);
214
                dump_mdmp_data(&mm->CvRecord, "    ");
215
                printf("    MiscRecord: <%u>\n", mm->MiscRecord.DataSize);
216
                dump_mdmp_data(&mm->MiscRecord, "    ");
217
                printf("    Reserved0: 0x%x%08x\n",
218
		    (DWORD)(mm->Reserved0 >> 32), (DWORD)mm->Reserved0);
219
                printf("    Reserved1: 0x%x%08x\n",
220
		    (DWORD)(mm->Reserved1 >> 32), (DWORD)mm->Reserved1);
221 222 223 224 225
            }
        }       
        break;
        case MemoryListStream:
        {
226
            const MINIDUMP_MEMORY_LIST*         mml = (const MINIDUMP_MEMORY_LIST*)stream;
227
            const MINIDUMP_MEMORY_DESCRIPTOR*   mmd = mml->MemoryRanges;
228
            unsigned int                        i;
229

230
            printf("Memory Ranges: %u\n", mml->NumberOfMemoryRanges);
231 232 233
            for (i = 0; i < mml->NumberOfMemoryRanges; i++, mmd++)
            {
                printf("  Memory Range #%d:\n", i);
234
                printf("    Range: 0x%x%08x-0x%x%08x\n",
235 236 237 238
                       (DWORD)(mmd->StartOfMemoryRange >> 32),
                       (DWORD)mmd->StartOfMemoryRange,
		       (DWORD)((mmd->StartOfMemoryRange + mmd->Memory.DataSize) >> 32),
		       (DWORD)(mmd->StartOfMemoryRange + mmd->Memory.DataSize));
239
                dump_mdmp_data(&mmd->Memory, "    ");
240 241 242 243 244
            }   
        }
        break;
        case SystemInfoStream:
        {
245
            const MINIDUMP_SYSTEM_INFO* msi = (const MINIDUMP_SYSTEM_INFO*)stream;
246
            const char*                 str;
247 248 249 250 251
            char                        tmp[128];

            printf("System Information:\n");
            switch (msi->ProcessorArchitecture)
            {
252
            case PROCESSOR_ARCHITECTURE_UNKNOWN:
253 254 255 256 257 258
                str = "Unknown";
                break;
            case PROCESSOR_ARCHITECTURE_INTEL:
                strcpy(tmp, "Intel ");
                switch (msi->ProcessorLevel)
                {
259 260 261 262 263
                case  3: str = "80386"; break;
                case  4: str = "80486"; break;
                case  5: str = "Pentium"; break;
                case  6: str = "Pentium Pro/II or AMD Athlon"; break;
                case 15: str = "Pentium 4 or AMD Athlon64"; break;
264 265 266
                default: str = "???"; break;
                }
                strcat(tmp, str);
267
                strcat(tmp, " (");
268 269
                if (msi->ProcessorLevel == 3 || msi->ProcessorLevel == 4)
                {
270 271
                    if (HIBYTE(msi->ProcessorRevision) == 0xFF)
                        sprintf(tmp + strlen(tmp), "%c%d", 'A' + ((msi->ProcessorRevision>>4)&0xf)-0x0a, msi->ProcessorRevision&0xf);
272
                    else
273
                        sprintf(tmp + strlen(tmp), "%c%d", 'A' + HIBYTE(msi->ProcessorRevision), LOBYTE(msi->ProcessorRevision));
274
                }
275
                else sprintf(tmp + strlen(tmp), "%d.%d", HIBYTE(msi->ProcessorRevision), LOBYTE(msi->ProcessorRevision));
276 277 278 279 280 281 282 283 284 285 286
                str = tmp;
                break;
            case PROCESSOR_ARCHITECTURE_MIPS:
                str = "Mips";
                break;
            case PROCESSOR_ARCHITECTURE_ALPHA:
                str = "Alpha";
                break;
            case PROCESSOR_ARCHITECTURE_PPC:
                str = "PowerPC";
                break;
287 288 289
            case PROCESSOR_ARCHITECTURE_ARM:
                str = "ARM";
                break;
290 291 292
            case PROCESSOR_ARCHITECTURE_AMD64:
                str = "X86_64";
                break;
293 294 295 296
            default:
                str = "???";
                break;
            }
297
            printf("  Processor: %s (#%d CPUs)\n", str, msi->u.s.NumberOfProcessors);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
            switch (msi->MajorVersion)
            {
            case 3:
                switch (msi->MinorVersion)
                {
                case 51: str = "NT 3.51"; break;
                default: str = "3-????"; break;
                }
                break;
            case 4:
                switch (msi->MinorVersion)
                {
                case 0: str = (msi->PlatformId == VER_PLATFORM_WIN32_NT) ? "NT 4.0" : "95"; break;
                case 10: str = "98"; break;
                case 90: str = "ME"; break;
                default: str = "5-????"; break;
                }
                break;
            case 5:
                switch (msi->MinorVersion)
                {
                case 0: str = "2000"; break;
                case 1: str = "XP"; break;
                case 2: str = "Server 2003"; break;
                default: str = "5-????"; break;
                }
                break;
            default: str = "???"; break;
            }
327 328
            printf("  Version: Windows %s (%u)\n", str, msi->BuildNumber);
            printf("  PlatformId: %u\n", msi->PlatformId);
329 330 331
            printf("  CSD: ");
            dump_mdmp_string(msi->CSDVersionRva);
            printf("\n");
332
            printf("  Reserved1: %u\n", msi->u1.Reserved1);
333 334
            if (msi->ProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
            {
335
                printf("  x86.VendorId: %.12s\n",
336
                       (const char*)msi->Cpu.X86CpuInfo.VendorId);
337
                printf("  x86.VersionInformation: %x\n",
338
                       msi->Cpu.X86CpuInfo.VersionInformation);
339
                printf("  x86.FeatureInformation: %x\n",
340
                       msi->Cpu.X86CpuInfo.FeatureInformation);
341
                printf("  x86.AMDExtendedCpuFeatures: %x\n",
342 343
                       msi->Cpu.X86CpuInfo.AMDExtendedCpuFeatures);
            }
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
            if (sizeof(MINIDUMP_SYSTEM_INFO) + 4 > dir->Location.DataSize &&
                msi->CSDVersionRva >= dir->Location.Rva + 4)
            {
                const char*  code = PRD(dir->Location.Rva + sizeof(MINIDUMP_SYSTEM_INFO), 4);
                const DWORD* wes;
                if (code && code[0] == 'W' && code[1] == 'I' && code[2] == 'N' && code[3] == 'E' &&
                    *(wes = (const DWORD*)(code += 4)) >= 3)
                {
                    /* assume we have wine extensions */
                    printf("  Wine details:\n");
                    printf("    build-id: %s\n", code + wes[1]);
                    printf("    system: %s\n", code + wes[2]);
                    printf("    release: %s\n", code + wes[3]);
                }
            }
359 360 361 362
        }
        break;
        case MiscInfoStream:
        {
363 364
            const MINIDUMP_MISC_INFO* mmi = (const MINIDUMP_MISC_INFO*)stream;

365
            printf("Misc Information\n");
366
            printf("  Size: %u\n", mmi->SizeOfInfo);
367 368 369 370
            printf("  Flags: %s%s\n", 
                   mmi->Flags1 & MINIDUMP_MISC1_PROCESS_ID ? "ProcessId " : "",
                   mmi->Flags1 & MINIDUMP_MISC1_PROCESS_TIMES ? "ProcessTimes " : "");
            if (mmi->Flags1 & MINIDUMP_MISC1_PROCESS_ID)
371
                printf("  ProcessId: %u\n", mmi->ProcessId);
372 373
            if (mmi->Flags1 & MINIDUMP_MISC1_PROCESS_TIMES)
            {
374 375 376
                printf("  ProcessCreateTime: %u\n", mmi->ProcessCreateTime);
                printf("  ProcessUserTime: %u\n", mmi->ProcessUserTime);
                printf("  ProcessKernelTime: %u\n", mmi->ProcessKernelTime);
377 378 379 380 381
            }
        }
        break;
        case ExceptionStream:
        {
382 383
            const MINIDUMP_EXCEPTION_STREAM*    mes = (const MINIDUMP_EXCEPTION_STREAM*)stream;
            unsigned int                        i;
384 385

            printf("Exception:\n");
386
            printf("  ThreadId: %08x\n", mes->ThreadId);
387
            printf("  ExceptionRecord:\n");
388 389 390
            printf("  ExceptionCode: %u\n", mes->ExceptionRecord.ExceptionCode);
            printf("  ExceptionFlags: %u\n", mes->ExceptionRecord.ExceptionFlags);
            printf("  ExceptionRecord: 0x%x%08x\n",
391 392
                   (DWORD)(mes->ExceptionRecord.ExceptionRecord  >> 32),
                   (DWORD)mes->ExceptionRecord.ExceptionRecord);
393
            printf("  ExceptionAddress: 0x%x%08x\n",
394 395
                   (DWORD)(mes->ExceptionRecord.ExceptionAddress >> 32),
                   (DWORD)(mes->ExceptionRecord.ExceptionAddress));
396
            printf("  ExceptionNumberParameters: %u\n",
397 398 399
                   mes->ExceptionRecord.NumberParameters);
            for (i = 0; i < mes->ExceptionRecord.NumberParameters; i++)
            {
400
                printf("    [%d]: 0x%x%08x\n", i,
401 402
                       (DWORD)(mes->ExceptionRecord.ExceptionInformation[i] >> 32),
                       (DWORD)mes->ExceptionRecord.ExceptionInformation[i]);
403 404 405 406 407 408 409
            }
            printf("  ThreadContext:\n");
            dump_mdmp_data(&mes->ThreadContext, "    ");
        }
        break;

        default:
410 411 412
            printf("NIY %d\n", dir->StreamType);
            printf("  RVA: %u\n", dir->Location.Rva);
            printf("  Size: %u\n", dir->Location.DataSize);
413
            dump_mdmp_data(&dir->Location, "    ");
414 415 416 417
            break;
        }
    }
}