xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ObjectFile/Minidump/MinidumpFileBuilder.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1349cc55cSDimitry Andric //===-- MinidumpFileBuilder.h ---------------------------------------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric //
9349cc55cSDimitry Andric /// \file
10349cc55cSDimitry Andric /// Structure holding data neccessary for minidump file creation.
11349cc55cSDimitry Andric ///
12349cc55cSDimitry Andric /// The class MinidumpFileWriter is used to hold the data that will eventually
13349cc55cSDimitry Andric /// be dumped to the file.
14349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
15349cc55cSDimitry Andric 
16349cc55cSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
17349cc55cSDimitry Andric #define LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
18349cc55cSDimitry Andric 
19349cc55cSDimitry Andric #include <cstddef>
20*5f757f3fSDimitry Andric #include <map>
21349cc55cSDimitry Andric 
22349cc55cSDimitry Andric #include "lldb/Target/Target.h"
23349cc55cSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
24349cc55cSDimitry Andric #include "lldb/Utility/Status.h"
25349cc55cSDimitry Andric 
26349cc55cSDimitry Andric #include "llvm/Object/Minidump.h"
27349cc55cSDimitry Andric 
28349cc55cSDimitry Andric // Write std::string to minidump in the UTF16 format(with null termination char)
29349cc55cSDimitry Andric // with the size(without null termination char) preceding the UTF16 string.
30349cc55cSDimitry Andric // Empty strings are also printed with zero length and just null termination
31349cc55cSDimitry Andric // char.
32349cc55cSDimitry Andric lldb_private::Status WriteString(const std::string &to_write,
33349cc55cSDimitry Andric                                  lldb_private::DataBufferHeap *buffer);
34349cc55cSDimitry Andric 
35349cc55cSDimitry Andric /// \class MinidumpFileBuilder
36349cc55cSDimitry Andric /// Minidump writer for Linux
37349cc55cSDimitry Andric ///
38349cc55cSDimitry Andric /// This class provides a Minidump writer that is able to
39349cc55cSDimitry Andric /// snapshot the current process state. For the whole time, it stores all
40349cc55cSDimitry Andric /// the data on heap.
41349cc55cSDimitry Andric class MinidumpFileBuilder {
42349cc55cSDimitry Andric public:
43349cc55cSDimitry Andric   MinidumpFileBuilder() = default;
44349cc55cSDimitry Andric 
45349cc55cSDimitry Andric   MinidumpFileBuilder(const MinidumpFileBuilder &) = delete;
46349cc55cSDimitry Andric   MinidumpFileBuilder &operator=(const MinidumpFileBuilder &) = delete;
47349cc55cSDimitry Andric 
48349cc55cSDimitry Andric   MinidumpFileBuilder(MinidumpFileBuilder &&other) = default;
49349cc55cSDimitry Andric   MinidumpFileBuilder &operator=(MinidumpFileBuilder &&other) = default;
50349cc55cSDimitry Andric 
51349cc55cSDimitry Andric   ~MinidumpFileBuilder() = default;
52349cc55cSDimitry Andric 
53349cc55cSDimitry Andric   // Add SystemInfo stream, used for storing the most basic information
54349cc55cSDimitry Andric   // about the system, platform etc...
55349cc55cSDimitry Andric   lldb_private::Status AddSystemInfo(const llvm::Triple &target_triple);
56349cc55cSDimitry Andric   // Add ModuleList stream, containing information about all loaded modules
57349cc55cSDimitry Andric   // at the time of saving minidump.
58349cc55cSDimitry Andric   lldb_private::Status AddModuleList(lldb_private::Target &target);
59349cc55cSDimitry Andric   // Add ThreadList stream, containing information about all threads running
60349cc55cSDimitry Andric   // at the moment of core saving. Contains information about thread
61349cc55cSDimitry Andric   // contexts.
62349cc55cSDimitry Andric   lldb_private::Status AddThreadList(const lldb::ProcessSP &process_sp);
63*5f757f3fSDimitry Andric   // Add Exception streams for any threads that stopped with exceptions.
64*5f757f3fSDimitry Andric   void AddExceptions(const lldb::ProcessSP &process_sp);
65349cc55cSDimitry Andric   // Add MemoryList stream, containing dumps of important memory segments
66*5f757f3fSDimitry Andric   lldb_private::Status AddMemoryList(const lldb::ProcessSP &process_sp,
67*5f757f3fSDimitry Andric                                      lldb::SaveCoreStyle core_style);
68349cc55cSDimitry Andric   // Add MiscInfo stream, mainly providing ProcessId
69349cc55cSDimitry Andric   void AddMiscInfo(const lldb::ProcessSP &process_sp);
70349cc55cSDimitry Andric   // Add informative files about a Linux process
71349cc55cSDimitry Andric   void AddLinuxFileStreams(const lldb::ProcessSP &process_sp);
72349cc55cSDimitry Andric   // Dump the prepared data into file. In case of the failure data are
73349cc55cSDimitry Andric   // intact.
74349cc55cSDimitry Andric   lldb_private::Status Dump(lldb::FileUP &core_file) const;
75349cc55cSDimitry Andric   // Returns the current number of directories(streams) that have been so far
76349cc55cSDimitry Andric   // created. This number of directories will be dumped when calling Dump()
77349cc55cSDimitry Andric   size_t GetDirectoriesNum() const;
78349cc55cSDimitry Andric 
79349cc55cSDimitry Andric private:
80349cc55cSDimitry Andric   // Add directory of StreamType pointing to the current end of the prepared
81349cc55cSDimitry Andric   // file with the specified size.
82349cc55cSDimitry Andric   void AddDirectory(llvm::minidump::StreamType type, size_t stream_size);
83349cc55cSDimitry Andric   size_t GetCurrentDataEndOffset() const;
84349cc55cSDimitry Andric 
85349cc55cSDimitry Andric   // Stores directories to later put them at the end of minidump file
86349cc55cSDimitry Andric   std::vector<llvm::minidump::Directory> m_directories;
87349cc55cSDimitry Andric   // Main data buffer consisting of data without the minidump header and
88349cc55cSDimitry Andric   // directories
89349cc55cSDimitry Andric   lldb_private::DataBufferHeap m_data;
90*5f757f3fSDimitry Andric 
91*5f757f3fSDimitry Andric   // More that one place can mention the register thread context locations,
92*5f757f3fSDimitry Andric   // so when we emit the thread contents, remember where it is so we don't have
93*5f757f3fSDimitry Andric   // to duplicate it in the exception data.
94*5f757f3fSDimitry Andric   std::map<lldb::tid_t, llvm::minidump::LocationDescriptor> m_tid_to_reg_ctx;
95349cc55cSDimitry Andric };
96349cc55cSDimitry Andric 
97349cc55cSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
98