| 1 | // Licensed to the .NET Foundation under one or more agreements. | 
|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license. | 
|---|
| 3 | // See the LICENSE file in the project root for more information. | 
|---|
| 4 |  | 
|---|
| 5 | #ifdef BIT64 | 
|---|
| 6 | #define ELF_CLASS ELFCLASS64 | 
|---|
| 7 | #else | 
|---|
| 8 | #define ELF_CLASS ELFCLASS32 | 
|---|
| 9 | #endif | 
|---|
| 10 |  | 
|---|
| 11 | #define Ehdr   ElfW(Ehdr) | 
|---|
| 12 | #define Phdr   ElfW(Phdr) | 
|---|
| 13 | #define Shdr   ElfW(Shdr) | 
|---|
| 14 | #define Nhdr   ElfW(Nhdr) | 
|---|
| 15 | #define auxv_t ElfW(auxv_t) | 
|---|
| 16 |  | 
|---|
| 17 | #if defined(__x86_64__) | 
|---|
| 18 | #define ELF_ARCH  EM_X86_64 | 
|---|
| 19 | #elif defined(__i386__) | 
|---|
| 20 | #define ELF_ARCH  EM_386 | 
|---|
| 21 | #elif defined(__arm__) | 
|---|
| 22 | #define ELF_ARCH  EM_ARM | 
|---|
| 23 | #endif | 
|---|
| 24 |  | 
|---|
| 25 | #define PH_HDR_CANARY 0xFFFF | 
|---|
| 26 |  | 
|---|
| 27 | #ifndef NT_FILE | 
|---|
| 28 | #define NT_FILE		0x46494c45 | 
|---|
| 29 | #endif | 
|---|
| 30 |  | 
|---|
| 31 | class DumpWriter : IUnknown | 
|---|
| 32 | { | 
|---|
| 33 | private: | 
|---|
| 34 | LONG m_ref;                         // reference count | 
|---|
| 35 | int m_fd; | 
|---|
| 36 | CrashInfo& m_crashInfo; | 
|---|
| 37 | BYTE m_tempBuffer[0x4000]; | 
|---|
| 38 |  | 
|---|
| 39 | public: | 
|---|
| 40 | DumpWriter(CrashInfo& crashInfo); | 
|---|
| 41 | virtual ~DumpWriter(); | 
|---|
| 42 | bool OpenDump(const char* dumpFileName); | 
|---|
| 43 | bool WriteDump(); | 
|---|
| 44 |  | 
|---|
| 45 | // IUnknown | 
|---|
| 46 | STDMETHOD(QueryInterface)(___in REFIID InterfaceId, ___out PVOID* Interface); | 
|---|
| 47 | STDMETHOD_(ULONG, AddRef)(); | 
|---|
| 48 | STDMETHOD_(ULONG, Release)(); | 
|---|
| 49 |  | 
|---|
| 50 | private: | 
|---|
| 51 | bool WriteProcessInfo(); | 
|---|
| 52 | bool WriteAuxv(); | 
|---|
| 53 | size_t GetNTFileInfoSize(size_t* alignmentBytes = nullptr); | 
|---|
| 54 | bool WriteNTFileInfo(); | 
|---|
| 55 | bool WriteThread(const ThreadInfo& thread, int fatal_signal); | 
|---|
| 56 | bool WriteData(const void* buffer, size_t length); | 
|---|
| 57 |  | 
|---|
| 58 | size_t GetProcessInfoSize() const { return sizeof(Nhdr) + 8 + sizeof(prpsinfo_t); } | 
|---|
| 59 | size_t GetAuxvInfoSize() const { return sizeof(Nhdr) + 8 + m_crashInfo.GetAuxvSize(); } | 
|---|
| 60 | size_t GetThreadInfoSize() const | 
|---|
| 61 | { | 
|---|
| 62 | return m_crashInfo.Threads().size() * ((sizeof(Nhdr) + 8 + sizeof(prstatus_t)) | 
|---|
| 63 | #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) | 
|---|
| 64 | + sizeof(Nhdr) + 8 + sizeof(user_fpregs_struct) | 
|---|
| 65 | #endif | 
|---|
| 66 | #if defined(__i386__) | 
|---|
| 67 | + sizeof(Nhdr) + 8 + sizeof(user_fpxregs_struct) | 
|---|
| 68 | #endif | 
|---|
| 69 | #if defined(__arm__) && defined(__VFP_FP__) && !defined(__SOFTFP__) | 
|---|
| 70 | + sizeof(Nhdr) + 8 + sizeof(user_vfpregs_struct) | 
|---|
| 71 | #endif | 
|---|
| 72 | ); | 
|---|
| 73 | } | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|