Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ObjectFile/Minidump/MinidumpFileBuilder.h
39642 views
//===-- MinidumpFileBuilder.h ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8/// \file9/// Structure holding data neccessary for minidump file creation.10///11/// The class MinidumpFileWriter is used to hold the data that will eventually12/// be dumped to the file.13//===----------------------------------------------------------------------===//1415#ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H16#define LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H1718#include <cstddef>19#include <cstdint>20#include <map>21#include <unordered_map>22#include <utility>23#include <variant>2425#include "lldb/Target/Process.h"26#include "lldb/Target/Target.h"27#include "lldb/Utility/DataBufferHeap.h"28#include "lldb/Utility/Status.h"29#include "lldb/lldb-forward.h"30#include "lldb/lldb-types.h"3132#include "llvm/BinaryFormat/Minidump.h"33#include "llvm/Object/Minidump.h"3435// Write std::string to minidump in the UTF16 format(with null termination char)36// with the size(without null termination char) preceding the UTF16 string.37// Empty strings are also printed with zero length and just null termination38// char.39lldb_private::Status WriteString(const std::string &to_write,40lldb_private::DataBufferHeap *buffer);4142/// \class MinidumpFileBuilder43/// Minidump writer for Linux44///45/// This class provides a Minidump writer that is able to46/// snapshot the current process state.47///48/// Minidumps are a Microsoft format for dumping process state.49/// This class constructs the minidump on disk starting with50/// Headers and Directories are written at the top of the file,51/// with the amount of bytes being precalculates before any writing takes place52/// Then the smaller data sections are written53/// SystemInfo, ModuleList, Misc Info.54/// Then Threads are emitted, threads are the first section that needs to be55/// 'fixed up' this happens when later we emit the memory stream, we identify if56/// that stream is the expected stack, and if so we update the stack with the57/// current RVA. Lastly the Memory lists are added. For Memory List, this will58/// contain everything that can fit within 4.2gb. MemoryList has it's59/// descriptors written at the end so it cannot be allowed to overflow.60///61/// Memory64List is a special case where it has to be begin before 4.2gb but can62/// expand forever The difference in Memory64List is there are no RVA's and all63/// the addresses are figured out by starting at the base RVA, and adding the64/// antecedent memory sections.65///66/// Because Memory64List can be arbitrarily large, this class has to write67/// chunks to disk this means we have to precalculate the descriptors and write68/// them first, and if we encounter any error, or are unable to read the same69/// number of bytes we have to go back and update them on disk.70///71/// And as the last step, after all the directories have been added, we go back72/// to the top of the file to fill in the header and the redirectory sections73/// that we preallocated.74class MinidumpFileBuilder {75public:76MinidumpFileBuilder(lldb::FileUP &&core_file,77const lldb::ProcessSP &process_sp)78: m_process_sp(process_sp), m_core_file(std::move(core_file)){};7980MinidumpFileBuilder(const MinidumpFileBuilder &) = delete;81MinidumpFileBuilder &operator=(const MinidumpFileBuilder &) = delete;8283MinidumpFileBuilder(MinidumpFileBuilder &&other) = default;84MinidumpFileBuilder &operator=(MinidumpFileBuilder &&other) = default;8586~MinidumpFileBuilder() = default;8788// This method only calculates the amount of bytes the header and directories89// will take up. It does not write the directories or headers. This function90// must be called with a followup to fill in the data.91lldb_private::Status AddHeaderAndCalculateDirectories();92// Add SystemInfo stream, used for storing the most basic information93// about the system, platform etc...94lldb_private::Status AddSystemInfo();95// Add ModuleList stream, containing information about all loaded modules96// at the time of saving minidump.97lldb_private::Status AddModuleList();98// Add ThreadList stream, containing information about all threads running99// at the moment of core saving. Contains information about thread100// contexts.101lldb_private::Status AddThreadList();102// Add Exception streams for any threads that stopped with exceptions.103lldb_private::Status AddExceptions();104// Add MemoryList stream, containing dumps of important memory segments105lldb_private::Status AddMemoryList(lldb::SaveCoreStyle core_style);106// Add MiscInfo stream, mainly providing ProcessId107lldb_private::Status AddMiscInfo();108// Add informative files about a Linux process109lldb_private::Status AddLinuxFileStreams();110111// Run cleanup and write all remaining bytes to file112lldb_private::Status DumpFile();113114private:115// Add data to the end of the buffer, if the buffer exceeds the flush level,116// trigger a flush.117lldb_private::Status AddData(const void *data, uint64_t size);118// Add MemoryList stream, containing dumps of important memory segments119lldb_private::Status120AddMemoryList_64(lldb_private::Process::CoreFileMemoryRanges &ranges);121lldb_private::Status122AddMemoryList_32(lldb_private::Process::CoreFileMemoryRanges &ranges);123// Update the thread list on disk with the newly emitted stack RVAs.124lldb_private::Status FixThreadStacks();125lldb_private::Status FlushBufferToDisk();126127lldb_private::Status DumpHeader() const;128lldb_private::Status DumpDirectories() const;129// Add directory of StreamType pointing to the current end of the prepared130// file with the specified size.131lldb_private::Status AddDirectory(llvm::minidump::StreamType type,132uint64_t stream_size);133lldb::offset_t GetCurrentDataEndOffset() const;134// Stores directories to fill in later135std::vector<llvm::minidump::Directory> m_directories;136// When we write off the threads for the first time, we need to clean them up137// and give them the correct RVA once we write the stack memory list.138// We save by the end because we only take from the stack pointer up139// So the saved off range base can differ from the memory region the stack140// pointer is in.141std::unordered_map<lldb::addr_t, llvm::minidump::Thread>142m_thread_by_range_end;143// Main data buffer consisting of data without the minidump header and144// directories145lldb_private::DataBufferHeap m_data;146lldb::ProcessSP m_process_sp;147148size_t m_expected_directories = 0;149uint64_t m_saved_data_size = 0;150lldb::offset_t m_thread_list_start = 0;151// We set the max write amount to 128 mb, this is arbitrary152// but we want to try to keep the size of m_data small153// and we will only exceed a 128 mb buffer if we get a memory region154// that is larger than 128 mb.155static constexpr size_t MAX_WRITE_CHUNK_SIZE = (1024 * 1024 * 128);156157static constexpr size_t HEADER_SIZE = sizeof(llvm::minidump::Header);158static constexpr size_t DIRECTORY_SIZE = sizeof(llvm::minidump::Directory);159160// More that one place can mention the register thread context locations,161// so when we emit the thread contents, remember where it is so we don't have162// to duplicate it in the exception data.163std::unordered_map<lldb::tid_t, llvm::minidump::LocationDescriptor>164m_tid_to_reg_ctx;165lldb::FileUP m_core_file;166};167168#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MINIDUMP_MINIDUMPFILEBUILDER_H169170171