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