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#include <map>
21
22#include "lldb/Target/Target.h"
23#include "lldb/Utility/DataBufferHeap.h"
24#include "lldb/Utility/Status.h"
25
26#include "llvm/Object/Minidump.h"
27
28// Write std::string to minidump in the UTF16 format(with null termination char)
29// with the size(without null termination char) preceding the UTF16 string.
30// Empty strings are also printed with zero length and just null termination
31// char.
32lldb_private::Status WriteString(const std::string &to_write,
33                                 lldb_private::DataBufferHeap *buffer);
34
35/// \class MinidumpFileBuilder
36/// Minidump writer for Linux
37///
38/// This class provides a Minidump writer that is able to
39/// snapshot the current process state. For the whole time, it stores all
40/// the data on heap.
41class MinidumpFileBuilder {
42public:
43  MinidumpFileBuilder() = default;
44
45  MinidumpFileBuilder(const MinidumpFileBuilder &) = delete;
46  MinidumpFileBuilder &operator=(const MinidumpFileBuilder &) = delete;
47
48  MinidumpFileBuilder(MinidumpFileBuilder &&other) = default;
49  MinidumpFileBuilder &operator=(MinidumpFileBuilder &&other) = default;
50
51  ~MinidumpFileBuilder() = default;
52
53  // Add SystemInfo stream, used for storing the most basic information
54  // about the system, platform etc...
55  lldb_private::Status AddSystemInfo(const llvm::Triple &target_triple);
56  // Add ModuleList stream, containing information about all loaded modules
57  // at the time of saving minidump.
58  lldb_private::Status AddModuleList(lldb_private::Target &target);
59  // Add ThreadList stream, containing information about all threads running
60  // at the moment of core saving. Contains information about thread
61  // contexts.
62  lldb_private::Status AddThreadList(const lldb::ProcessSP &process_sp);
63  // Add Exception streams for any threads that stopped with exceptions.
64  void AddExceptions(const lldb::ProcessSP &process_sp);
65  // Add MemoryList stream, containing dumps of important memory segments
66  lldb_private::Status AddMemoryList(const lldb::ProcessSP &process_sp,
67                                     lldb::SaveCoreStyle core_style);
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  // More that one place can mention the register thread context locations,
92  // so when we emit the thread contents, remember where it is so we don't have
93  // to duplicate it in the exception data.
94  std::map<lldb::tid_t, llvm::minidump::LocationDescriptor> m_tid_to_reg_ctx;
95};
96
97#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
98