diff --git a/llvm/include/llvm/BinaryFormat/Minidump.h b/llvm/include/llvm/BinaryFormat/Minidump.h --- a/llvm/include/llvm/BinaryFormat/Minidump.h +++ b/llvm/include/llvm/BinaryFormat/Minidump.h @@ -217,6 +217,34 @@ }; static_assert(sizeof(Thread) == 48, ""); +// Exception stuff +struct Exception { + static constexpr size_t MaxParameters = 15; + + support::ulittle32_t ExceptionCode; + support::ulittle32_t ExceptionFlags; + support::ulittle64_t ExceptionRecord; + support::ulittle64_t ExceptionAddress; + support::ulittle32_t NumberParameters; + support::ulittle32_t UnusedAlignment; + support::ulittle64_t ExceptionInformation[MaxParameters]; +}; +static_assert(sizeof(Exception) == 152, ""); + +// Breakpad defines a smattering of ExceptionCode values +enum class ExceptionCode : uint32_t { +#define HANDLE_MDMP_EXN_CODE(CODE, NAME) NAME = CODE, +#include "llvm/BinaryFormat/MinidumpConstants.def" +}; + +struct ExceptionStream { + support::ulittle32_t ThreadId; + support::ulittle32_t UnusedAlignment; + Exception ExceptionRecord; + LocationDescriptor ThreadContext; +}; +static_assert(sizeof(ExceptionStream) == 168, ""); + } // namespace minidump template <> struct DenseMapInfo { diff --git a/llvm/include/llvm/BinaryFormat/MinidumpConstants.def b/llvm/include/llvm/BinaryFormat/MinidumpConstants.def --- a/llvm/include/llvm/BinaryFormat/MinidumpConstants.def +++ b/llvm/include/llvm/BinaryFormat/MinidumpConstants.def @@ -8,7 +8,8 @@ #if !(defined(HANDLE_MDMP_STREAM_TYPE) || defined(HANDLE_MDMP_ARCH) || \ defined(HANDLE_MDMP_PLATFORM) || defined(HANDLE_MDMP_PROTECT) || \ - defined(HANDLE_MDMP_MEMSTATE) || defined(HANDLE_MDMP_MEMTYPE)) + defined(HANDLE_MDMP_MEMSTATE) || defined(HANDLE_MDMP_MEMTYPE) || \ + defined(HANDLE_MDMP_EXN_CODE)) #error "Missing HANDLE_MDMP definition" #endif @@ -36,6 +37,10 @@ #define HANDLE_MDMP_MEMTYPE(CODE, NAME, NATIVENAME) #endif +#ifndef HANDLE_MDMP_EXN_CODE +#define HANDLE_MDMP_EXN_CODE(CODE, NAME) +#endif + HANDLE_MDMP_STREAM_TYPE(0x0003, ThreadList) HANDLE_MDMP_STREAM_TYPE(0x0004, ModuleList) HANDLE_MDMP_STREAM_TYPE(0x0005, MemoryList) @@ -136,9 +141,94 @@ HANDLE_MDMP_MEMTYPE(0x0040000, Mapped, MEM_MAPPED) HANDLE_MDMP_MEMTYPE(0x1000000, Image, MEM_IMAGE) +// Exception codes are defined by breakpad in groups +// according to OS type +// Linux: +// Signal numbers are used as exception codes +HANDLE_MDMP_EXN_CODE(1, Linux_SIGHUP) +HANDLE_MDMP_EXN_CODE(2, Linux_SIGINT) +HANDLE_MDMP_EXN_CODE(3, Linux_SIGQUIT) +HANDLE_MDMP_EXN_CODE(4, Linux_SIGILL) +HANDLE_MDMP_EXN_CODE(5, Linux_SIGTRAP) +HANDLE_MDMP_EXN_CODE(6, Linux_SIGABRT) +HANDLE_MDMP_EXN_CODE(7, Linux_SIGBUS) +HANDLE_MDMP_EXN_CODE(8, Linux_SIGFPE) +HANDLE_MDMP_EXN_CODE(9, Linux_SIGKILL) +HANDLE_MDMP_EXN_CODE(10, Linux_SIGUSR1) +HANDLE_MDMP_EXN_CODE(11, Linux_SIGSEGV) +HANDLE_MDMP_EXN_CODE(12, Linux_SIGUSR2) +HANDLE_MDMP_EXN_CODE(13, Linux_SIGPIPE) +HANDLE_MDMP_EXN_CODE(14, Linux_SIGALRM) +HANDLE_MDMP_EXN_CODE(15, Linux_SIGTERM) +HANDLE_MDMP_EXN_CODE(16, Linux_SIGSTKFLT) +HANDLE_MDMP_EXN_CODE(17, Linux_SIGCHLD) +HANDLE_MDMP_EXN_CODE(18, Linux_SIGCONT) +HANDLE_MDMP_EXN_CODE(19, Linux_SIGSTOP) +HANDLE_MDMP_EXN_CODE(20, Linux_SIGTSTP) +HANDLE_MDMP_EXN_CODE(21, Linux_SIGTTIN) +HANDLE_MDMP_EXN_CODE(22, Linux_SIGTTOU) +HANDLE_MDMP_EXN_CODE(23, Linux_SIGURG) +HANDLE_MDMP_EXN_CODE(24, Linux_SIGXCPU) +HANDLE_MDMP_EXN_CODE(25, Linux_SIGXFSZ) +HANDLE_MDMP_EXN_CODE(26, Linux_SIGVTALRM) +HANDLE_MDMP_EXN_CODE(27, Linux_SIGPROF) +HANDLE_MDMP_EXN_CODE(28, Linux_SIGWINCH) +HANDLE_MDMP_EXN_CODE(29, Linux_SIGIO) +HANDLE_MDMP_EXN_CODE(30, Linux_SIGPWR) +HANDLE_MDMP_EXN_CODE(31, Linux_SIGSYS) +// Special code sometimes used to write dump for process that hasn't crashed. +HANDLE_MDMP_EXN_CODE(0xFFFFFFFF, Linux_DumpRequested) +// Mac: +// The high 16 bits of exception code indicate the Mac OS "exception": +HANDLE_MDMP_EXN_CODE(1, Mac_BadAccess) +HANDLE_MDMP_EXN_CODE(2, Mac_BadInstruction) +HANDLE_MDMP_EXN_CODE(3, Mac_Arithmetic) +HANDLE_MDMP_EXN_CODE(4, Mac_Emulation) +HANDLE_MDMP_EXN_CODE(5, Mac_Software) +HANDLE_MDMP_EXN_CODE(6, Mac_Breakpoint) +HANDLE_MDMP_EXN_CODE(7, Mac_Syscall) +HANDLE_MDMP_EXN_CODE(8, Mac_MachSyscall) +HANDLE_MDMP_EXN_CODE(9, Mac_RpcAlert) +// Special code sometimes used to write dump for process that hasn't crashed. +HANDLE_MDMP_EXN_CODE(0x43507378, Mac_Simulated) +// Windows: +// Exception codes from windows headers: +HANDLE_MDMP_EXN_CODE(0x40010005, Windows_ControlC) +HANDLE_MDMP_EXN_CODE(0x80000001, Windows_GuardPageViolation) +HANDLE_MDMP_EXN_CODE(0x80000002, Windows_DatatypeMisalignment) +HANDLE_MDMP_EXN_CODE(0x80000003, Windows_Breakpoint) +HANDLE_MDMP_EXN_CODE(0x80000004, Windows_SingleStep) +HANDLE_MDMP_EXN_CODE(0xc0000005, Windows_AccessViolation) +HANDLE_MDMP_EXN_CODE(0xc0000006, Windows_InPageError) +HANDLE_MDMP_EXN_CODE(0xc0000008, Windows_InvalidHandle) +HANDLE_MDMP_EXN_CODE(0xc000001d, Windows_IllegalInstruction) +HANDLE_MDMP_EXN_CODE(0xc0000025, Windows_NoncontinuableException) +HANDLE_MDMP_EXN_CODE(0xc0000026, Windows_InvalidDisposition) +HANDLE_MDMP_EXN_CODE(0xc000008c, Windows_ArrayBoundsExceeded) +HANDLE_MDMP_EXN_CODE(0xc000008d, Windows_FloatDenormalOperand) +HANDLE_MDMP_EXN_CODE(0xc000008e, Windows_FloatDivideByZero) +HANDLE_MDMP_EXN_CODE(0xc000008f, Windows_FloatInexactResult) +HANDLE_MDMP_EXN_CODE(0xc0000090, Windows_FloatInvalidOperation) +HANDLE_MDMP_EXN_CODE(0xc0000091, Windows_FloatOverflow) +HANDLE_MDMP_EXN_CODE(0xc0000092, Windows_FloatStackCheck) +HANDLE_MDMP_EXN_CODE(0xc0000093, Windows_FloatUnderflow) +HANDLE_MDMP_EXN_CODE(0xc0000094, Windows_IntegerDivideByZero) +HANDLE_MDMP_EXN_CODE(0xc0000095, Windows_IntegerOverflow) +HANDLE_MDMP_EXN_CODE(0xc0000096, Windows_PrivilegedInstruction) +HANDLE_MDMP_EXN_CODE(0xc00000fd, Windows_StackOverflow) +HANDLE_MDMP_EXN_CODE(0xc00000ff, Windows_BadFunctionTable) +HANDLE_MDMP_EXN_CODE(0xc0000194, Windows_PossibleDeadlock) +HANDLE_MDMP_EXN_CODE(0xc0000409, Windows_StackBufferOverrun) +HANDLE_MDMP_EXN_CODE(0xc0000374, Windows_HeapCorruption) +HANDLE_MDMP_EXN_CODE(0xe0000008, Windows_OutOfMemory) +HANDLE_MDMP_EXN_CODE(0xe06d7363, Windows_UnhandledCppException) +// Special code sometimes used to write dump for process that hasn't crashed. +HANDLE_MDMP_EXN_CODE(0x0517a7ed, Windows_Simulated) + #undef HANDLE_MDMP_STREAM_TYPE #undef HANDLE_MDMP_ARCH #undef HANDLE_MDMP_PLATFORM #undef HANDLE_MDMP_PROTECT #undef HANDLE_MDMP_MEMSTATE #undef HANDLE_MDMP_MEMTYPE +#undef HANDLE_MDMP_EXN_CODE diff --git a/llvm/include/llvm/Object/Minidump.h b/llvm/include/llvm/Object/Minidump.h --- a/llvm/include/llvm/Object/Minidump.h +++ b/llvm/include/llvm/Object/Minidump.h @@ -81,6 +81,15 @@ return getListStream(minidump::StreamType::ThreadList); } + /// Returns the contents of the Exception stream. An error is returned if the + /// file does not contain this stream, or the stream is smaller than the size + /// of the ExceptionStream structure. The internal consistency of the stream + /// is not checked in any way. + Expected getExceptionStream() const { + return getStream( + minidump::StreamType::Exception); + } + /// Returns the list of descriptors embedded in the MemoryList stream. The /// descriptors provide the content of interesting regions of memory at the /// time the minidump was taken. An error is returned if the file does not diff --git a/llvm/unittests/Object/MinidumpTest.cpp b/llvm/unittests/Object/MinidumpTest.cpp --- a/llvm/unittests/Object/MinidumpTest.cpp +++ b/llvm/unittests/Object/MinidumpTest.cpp @@ -710,3 +710,64 @@ testing::ElementsAre(0x0000000003020100u, 0x0000070605040000u, 0x0001000908000000u)); } + +TEST(MinidumpFile, getExceptionStream) { + std::vector Data{ + // Header + 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version + 1, 0, 0, 0, // NumberOfStreams, + 0x20, 0, 0, 0, // StreamDirectoryRVA + 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp + 8, 9, 0, 1, 2, 3, 4, 5, // Flags + // Stream Directory + 6, 0, 0, 0, 168, 0, 0, 0, // Type, DataSize, + 0x2c, 0, 0, 0, // RVA + // Exception Stream + 1, 2, 3, 4, // Thread ID + 0, 0, 0, 0, // Padding + // Exception Record + 2, 3, 4, 2, 7, 8, 8, 9, // Code, Flags + 3, 4, 5, 6, 7, 8, 9, 10, // Inner exception record address + 8, 7, 6, 5, 4, 3, 2, 1, // Exception address + 4, 0, 0, 0, 0, 0, 0, 0, // Parameter count, padding + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // Parameter 0 + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, // Parameter 1 + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // Parameter 2 + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, // Parameter 3 + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, // Parameter 4 + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, // Parameter 5 + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, // Parameter 6 + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // Parameter 7 + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, // Parameter 8 + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, // Parameter 9 + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, // Parameter 10 + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // Parameter 11 + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, // Parameter 12 + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, // Parameter 13 + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, // Parameter 14 + // Thread Context + 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, // DataSize, RVA + }; + auto ExpectedFile = create(Data); + ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded()); + const MinidumpFile &File = **ExpectedFile; + Expected ExpectedStream = + File.getExceptionStream(); + if (!ExpectedStream) { + errs() << ExpectedStream.takeError(); + } + ASSERT_THAT_EXPECTED(ExpectedStream, Succeeded()); + EXPECT_EQ(0x04030201u, ExpectedStream->ThreadId); + const minidump::Exception &Exception = ExpectedStream->ExceptionRecord; + EXPECT_EQ(0x02040302u, Exception.ExceptionCode); + EXPECT_EQ(0x09080807u, Exception.ExceptionFlags); + EXPECT_EQ(0x0a09080706050403u, Exception.ExceptionRecord); + EXPECT_EQ(0x0102030405060708u, Exception.ExceptionAddress); + EXPECT_EQ(4u, Exception.NumberParameters); + for (uint64_t index = 0; index < Exception.MaxParameters; ++index) { + EXPECT_EQ(0x1716151413121110u + index * 0x1010101010101010u, + Exception.ExceptionInformation[index]); + } + EXPECT_EQ(0x84838281, ExpectedStream->ThreadContext.DataSize); + EXPECT_EQ(0x88878685, ExpectedStream->ThreadContext.RVA); +}