LLDB mainline
MinidumpFileBuilder.h
Go to the documentation of this file.
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 <cstdint>
21#include <map>
22#include <unordered_map>
23#include <utility>
24#include <variant>
25
26#include "lldb/Core/Progress.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/Target.h"
31#include "lldb/Utility/Status.h"
32#include "lldb/lldb-forward.h"
33#include "lldb/lldb-types.h"
34
35#include "llvm/BinaryFormat/Minidump.h"
36#include "llvm/Object/Minidump.h"
37
38// Write std::string to minidump in the UTF16 format(with null terminator)
39// with the size(without null terminator) preceding the UTF16 string.
40// Empty strings are also printed with zero length and just a null terminator.
41lldb_private::Status WriteString(const std::string &to_write,
43
44/// \class MinidumpFileBuilder
45/// Minidump writer for Linux
46///
47/// This class provides a Minidump writer that is able to
48/// snapshot the current process state.
49///
50/// Minidumps are a Microsoft format for dumping process state.
51/// This class constructs the minidump on disk starting with
52/// Headers and Directories are written at the top of the file,
53/// with the amount of bytes being precalculates before any writing takes place
54/// Then the smaller data sections are written
55/// SystemInfo, ModuleList, Misc Info.
56/// Then Threads are emitted, threads are the first section that needs to be
57/// 'fixed up' this happens when later we emit the memory stream, we identify if
58/// that stream is the expected stack, and if so we update the stack with the
59/// current RVA. Lastly the Memory lists are added. For Memory List, this will
60/// contain everything that can fit within 4.2gb. MemoryList has it's
61/// descriptors written at the end so it cannot be allowed to overflow.
62///
63/// Memory64List is a special case where it has to be begin before 4.2gb but can
64/// expand forever The difference in Memory64List is there are no RVA's and all
65/// the addresses are figured out by starting at the base RVA, and adding the
66/// antecedent memory sections.
67///
68/// Because Memory64List can be arbitrarily large, this class has to write
69/// chunks to disk this means we have to precalculate the descriptors and write
70/// them first, and if we encounter any error, or are unable to read the same
71/// number of bytes we have to go back and update them on disk.
72///
73/// And as the last step, after all the directories have been added, we go back
74/// to the top of the file to fill in the header and the redirectory sections
75/// that we preallocated.
77public:
79 const lldb::ProcessSP &process_sp,
80 lldb_private::SaveCoreOptions &save_core_options)
81 : m_process_sp(process_sp), m_core_file(std::move(core_file)),
82 m_save_core_options(save_core_options) {}
83
86
89
91
92 // This method only calculates the amount of bytes the header and directories
93 // will take up. It does not write the directories or headers. This function
94 // must be called with a followup to fill in the data.
96 // Add SystemInfo stream, used for storing the most basic information
97 // about the system, platform etc...
99 // Add ModuleList stream, containing information about all loaded modules
100 // at the time of saving minidump.
102 // Add ThreadList stream, containing information about all threads running
103 // at the moment of core saving. Contains information about thread
104 // contexts.
106 // Add Exception streams for any threads that stopped with exceptions.
108 // Add MemoryList stream, containing dumps of important memory segments
110 // Add MiscInfo stream, mainly providing ProcessId
112 // Add informative files about a Linux process
114
115 // Run cleanup and write all remaining bytes to file
117
118 // Delete the file if it exists
119 void DeleteFile() noexcept;
120
121private:
123 // Add data to the end of the buffer, if the buffer exceeds the flush level,
124 // trigger a flush.
125 lldb_private::Status AddData(const void *data, uint64_t size);
126 // Add MemoryList stream, containing dumps of important memory segments
128 AddMemoryList_64(std::vector<lldb_private::CoreFileMemoryRange> &ranges,
129 lldb_private::Progress &progress);
131 AddMemoryList_32(std::vector<lldb_private::CoreFileMemoryRange> &ranges,
132 lldb_private::Progress &progress);
133 // Update the thread list on disk with the newly emitted stack RVAs.
136
139 // Add directory of StreamType pointing to the current end of the prepared
140 // file with the specified size.
141 lldb_private::Status AddDirectory(llvm::minidump::StreamType type,
142 uint64_t stream_size);
143 lldb::offset_t GetCurrentDataEndOffset() const;
144
145 // Read a memory region from the process and write it to the file
146 // in fixed size chunks.
148 ReadWriteMemoryInChunks(lldb_private::DataBufferHeap &data_buffer,
149 const lldb_private::CoreFileMemoryRange &range,
150 uint64_t &bytes_read);
151
152 // Stores directories to fill in later
153 std::vector<llvm::minidump::Directory> m_directories;
154 // When we write off the threads for the first time, we need to clean them up
155 // and give them the correct RVA once we write the stack memory list.
156 // We save by the end because we only take from the stack pointer up
157 // So the saved off range base can differ from the memory region the stack
158 // pointer is in.
159 std::unordered_map<lldb::addr_t, llvm::minidump::Thread>
161 // Main data buffer consisting of data without the minidump header and
162 // directories
163 lldb_private::DataBufferHeap m_data;
164 lldb::ProcessSP m_process_sp;
165
167 uint64_t m_saved_data_size = 0;
169 // We set the max write amount to 128 mb, this is arbitrary
170 // but we want to try to keep the size of m_data small
171 // and we will only exceed a 128 mb buffer if we get a memory region
172 // that is larger than 128 mb.
173 static constexpr uint64_t MAX_WRITE_CHUNK_SIZE = (1024 * 1024 * 128);
174
175 static constexpr size_t HEADER_SIZE = sizeof(llvm::minidump::Header);
176 static constexpr size_t DIRECTORY_SIZE = sizeof(llvm::minidump::Directory);
177
178 // More that one place can mention the register thread context locations,
179 // so when we emit the thread contents, remember where it is so we don't have
180 // to duplicate it in the exception data.
181 std::unordered_map<lldb::tid_t, llvm::minidump::LocationDescriptor>
185};
186#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H
lldb_private::Status WriteString(const std::string &to_write, lldb_private::DataBufferHeap *buffer)
lldb_private::Status AddExceptions()
std::unordered_map< lldb::tid_t, llvm::minidump::LocationDescriptor > m_tid_to_reg_ctx
lldb_private::Status AddLLDBGeneratedStream()
~MinidumpFileBuilder()=default
lldb_private::Status AddThreadList()
lldb_private::Status AddHeaderAndCalculateDirectories()
static constexpr uint64_t MAX_WRITE_CHUNK_SIZE
static constexpr size_t DIRECTORY_SIZE
lldb_private::DataBufferHeap m_data
lldb_private::Status AddSystemInfo()
lldb_private::Status ReadWriteMemoryInChunks(lldb_private::DataBufferHeap &data_buffer, const lldb_private::CoreFileMemoryRange &range, uint64_t &bytes_read)
lldb_private::Status AddMemoryList()
std::unordered_map< lldb::addr_t, llvm::minidump::Thread > m_thread_by_range_end
lldb_private::Status AddData(const void *data, uint64_t size)
lldb_private::Status DumpFile()
lldb_private::SaveCoreOptions m_save_core_options
lldb_private::Status FlushBufferToDisk()
MinidumpFileBuilder(MinidumpFileBuilder &&other)=default
lldb::offset_t m_thread_list_start
lldb_private::Status AddDirectory(llvm::minidump::StreamType type, uint64_t stream_size)
MinidumpFileBuilder & operator=(MinidumpFileBuilder &&other)=default
lldb_private::Status FixThreadStacks()
lldb::ProcessSP m_process_sp
static constexpr size_t HEADER_SIZE
lldb::offset_t GetCurrentDataEndOffset() const
lldb_private::Status AddMemoryList_32(std::vector< lldb_private::CoreFileMemoryRange > &ranges, lldb_private::Progress &progress)
std::vector< llvm::minidump::Directory > m_directories
lldb_private::Status DumpDirectories() const
lldb_private::Status AddLinuxFileStreams()
lldb_private::Status AddModuleList()
MinidumpFileBuilder(lldb::FileUP &&core_file, const lldb::ProcessSP &process_sp, lldb_private::SaveCoreOptions &save_core_options)
lldb_private::Status AddMiscInfo()
lldb_private::Status DumpHeader() const
lldb_private::Status AddMemoryList_64(std::vector< lldb_private::CoreFileMemoryRange > &ranges, lldb_private::Progress &progress)
MinidumpFileBuilder & operator=(const MinidumpFileBuilder &)=delete
MinidumpFileBuilder(const MinidumpFileBuilder &)=delete
A subclass of DataBuffer that stores a data buffer on the heap.
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
std::unique_ptr< lldb_private::File > FileUP
std::shared_ptr< lldb_private::Process > ProcessSP