xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ObjectFile/Minidump/MinidumpFileBuilder.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1*349cc55cSDimitry Andric //===-- MinidumpFileBuilder.h ---------------------------------------------===//
2*349cc55cSDimitry Andric //
3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*349cc55cSDimitry Andric //
7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8*349cc55cSDimitry Andric //
9*349cc55cSDimitry Andric /// \file
10*349cc55cSDimitry Andric /// Structure holding data neccessary for minidump file creation.
11*349cc55cSDimitry Andric ///
12*349cc55cSDimitry Andric /// The class MinidumpFileWriter is used to hold the data that will eventually
13*349cc55cSDimitry Andric /// be dumped to the file.
14*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
15*349cc55cSDimitry Andric 
16*349cc55cSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
17*349cc55cSDimitry Andric #define LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
18*349cc55cSDimitry Andric 
19*349cc55cSDimitry Andric #include <cstddef>
20*349cc55cSDimitry Andric 
21*349cc55cSDimitry Andric #include "lldb/Target/Target.h"
22*349cc55cSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
23*349cc55cSDimitry Andric #include "lldb/Utility/Status.h"
24*349cc55cSDimitry Andric 
25*349cc55cSDimitry Andric #include "llvm/Object/Minidump.h"
26*349cc55cSDimitry Andric 
27*349cc55cSDimitry Andric // Write std::string to minidump in the UTF16 format(with null termination char)
28*349cc55cSDimitry Andric // with the size(without null termination char) preceding the UTF16 string.
29*349cc55cSDimitry Andric // Empty strings are also printed with zero length and just null termination
30*349cc55cSDimitry Andric // char.
31*349cc55cSDimitry Andric lldb_private::Status WriteString(const std::string &to_write,
32*349cc55cSDimitry Andric                                  lldb_private::DataBufferHeap *buffer);
33*349cc55cSDimitry Andric 
34*349cc55cSDimitry Andric /// \class MinidumpFileBuilder
35*349cc55cSDimitry Andric /// Minidump writer for Linux
36*349cc55cSDimitry Andric ///
37*349cc55cSDimitry Andric /// This class provides a Minidump writer that is able to
38*349cc55cSDimitry Andric /// snapshot the current process state. For the whole time, it stores all
39*349cc55cSDimitry Andric /// the data on heap.
40*349cc55cSDimitry Andric class MinidumpFileBuilder {
41*349cc55cSDimitry Andric public:
42*349cc55cSDimitry Andric   MinidumpFileBuilder() = default;
43*349cc55cSDimitry Andric 
44*349cc55cSDimitry Andric   MinidumpFileBuilder(const MinidumpFileBuilder &) = delete;
45*349cc55cSDimitry Andric   MinidumpFileBuilder &operator=(const MinidumpFileBuilder &) = delete;
46*349cc55cSDimitry Andric 
47*349cc55cSDimitry Andric   MinidumpFileBuilder(MinidumpFileBuilder &&other) = default;
48*349cc55cSDimitry Andric   MinidumpFileBuilder &operator=(MinidumpFileBuilder &&other) = default;
49*349cc55cSDimitry Andric 
50*349cc55cSDimitry Andric   ~MinidumpFileBuilder() = default;
51*349cc55cSDimitry Andric 
52*349cc55cSDimitry Andric   // Add SystemInfo stream, used for storing the most basic information
53*349cc55cSDimitry Andric   // about the system, platform etc...
54*349cc55cSDimitry Andric   lldb_private::Status AddSystemInfo(const llvm::Triple &target_triple);
55*349cc55cSDimitry Andric   // Add ModuleList stream, containing information about all loaded modules
56*349cc55cSDimitry Andric   // at the time of saving minidump.
57*349cc55cSDimitry Andric   lldb_private::Status AddModuleList(lldb_private::Target &target);
58*349cc55cSDimitry Andric   // Add ThreadList stream, containing information about all threads running
59*349cc55cSDimitry Andric   // at the moment of core saving. Contains information about thread
60*349cc55cSDimitry Andric   // contexts.
61*349cc55cSDimitry Andric   lldb_private::Status AddThreadList(const lldb::ProcessSP &process_sp);
62*349cc55cSDimitry Andric   // Add Exception stream, this contains information about the exception
63*349cc55cSDimitry Andric   // that stopped the process. In case no thread made exception it return
64*349cc55cSDimitry Andric   // failed status.
65*349cc55cSDimitry Andric   lldb_private::Status AddException(const lldb::ProcessSP &process_sp);
66*349cc55cSDimitry Andric   // Add MemoryList stream, containing dumps of important memory segments
67*349cc55cSDimitry Andric   lldb_private::Status AddMemoryList(const lldb::ProcessSP &process_sp);
68*349cc55cSDimitry Andric   // Add MiscInfo stream, mainly providing ProcessId
69*349cc55cSDimitry Andric   void AddMiscInfo(const lldb::ProcessSP &process_sp);
70*349cc55cSDimitry Andric   // Add informative files about a Linux process
71*349cc55cSDimitry Andric   void AddLinuxFileStreams(const lldb::ProcessSP &process_sp);
72*349cc55cSDimitry Andric   // Dump the prepared data into file. In case of the failure data are
73*349cc55cSDimitry Andric   // intact.
74*349cc55cSDimitry Andric   lldb_private::Status Dump(lldb::FileUP &core_file) const;
75*349cc55cSDimitry Andric   // Returns the current number of directories(streams) that have been so far
76*349cc55cSDimitry Andric   // created. This number of directories will be dumped when calling Dump()
77*349cc55cSDimitry Andric   size_t GetDirectoriesNum() const;
78*349cc55cSDimitry Andric 
79*349cc55cSDimitry Andric private:
80*349cc55cSDimitry Andric   // Add directory of StreamType pointing to the current end of the prepared
81*349cc55cSDimitry Andric   // file with the specified size.
82*349cc55cSDimitry Andric   void AddDirectory(llvm::minidump::StreamType type, size_t stream_size);
83*349cc55cSDimitry Andric   size_t GetCurrentDataEndOffset() const;
84*349cc55cSDimitry Andric 
85*349cc55cSDimitry Andric   // Stores directories to later put them at the end of minidump file
86*349cc55cSDimitry Andric   std::vector<llvm::minidump::Directory> m_directories;
87*349cc55cSDimitry Andric   // Main data buffer consisting of data without the minidump header and
88*349cc55cSDimitry Andric   // directories
89*349cc55cSDimitry Andric   lldb_private::DataBufferHeap m_data;
90*349cc55cSDimitry Andric };
91*349cc55cSDimitry Andric 
92*349cc55cSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
93