summaryrefslogtreecommitdiff
path: root/src/debug/createdump/dumpwriter.h
blob: 7da0d63ccfd3c4b8ded063d49959be3adac3c808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#ifdef BIT64
#define ELF_CLASS ELFCLASS64
#else
#define ELF_CLASS ELFCLASS32
#endif

#define Ehdr   ElfW(Ehdr)
#define Phdr   ElfW(Phdr)
#define Shdr   ElfW(Shdr)
#define Nhdr   ElfW(Nhdr)
#define auxv_t ElfW(auxv_t)

#if defined(__x86_64__)
#define ELF_ARCH  EM_X86_64
#elif defined(__i386__)
#define ELF_ARCH  EM_386
#elif defined(__arm__)
#define ELF_ARCH  EM_ARM
#endif

#define PH_HDR_CANARY 0xFFFF

#ifndef NT_FILE
#define NT_FILE		0x46494c45
#endif

class DumpWriter : IUnknown
{
private:
    LONG m_ref;                         // reference count
    int m_fd;
    CrashInfo& m_crashInfo;
    BYTE m_tempBuffer[0x4000];

public:
    DumpWriter(CrashInfo& crashInfo);
    virtual ~DumpWriter();
    bool OpenDump(const char* dumpFileName);
    bool WriteDump();

    // IUnknown
    STDMETHOD(QueryInterface)(___in REFIID InterfaceId, ___out PVOID* Interface);
    STDMETHOD_(ULONG, AddRef)();
    STDMETHOD_(ULONG, Release)();

private:
    bool WriteProcessInfo();
    bool WriteAuxv();
    size_t GetNTFileInfoSize(size_t* alignmentBytes = nullptr);
    bool WriteNTFileInfo();
    bool WriteThread(const ThreadInfo& thread, int fatal_signal);
    bool WriteData(const void* buffer, size_t length);

    const size_t GetProcessInfoSize() const { return sizeof(Nhdr) + 8 + sizeof(prpsinfo_t); }
    const size_t GetAuxvInfoSize() const { return sizeof(Nhdr) + 8 + m_crashInfo.GetAuxvSize(); }
    const size_t GetThreadInfoSize() const 
    {
        return m_crashInfo.Threads().size() * ((sizeof(Nhdr) + 8 + sizeof(prstatus_t))
#if defined(__i386__) || defined(__x86_64__)
            + sizeof(Nhdr) + 8 + sizeof(user_fpregs_struct)
#endif
#if defined(__i386__)
            + sizeof(Nhdr) + 8 + sizeof(user_fpxregs_struct)
#endif
        );
    }
};