194 lines · c
1//===-- ProcessElfCore.h ----------------------------------------*- C++ -*-===//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-exception6//7// Notes about Linux Process core dumps:8// 1) Linux core dump is stored as ELF file.9// 2) The ELF file's PT_NOTE and PT_LOAD segments describes the program's10// address space and thread contexts.11// 3) PT_NOTE segment contains note entries which describes a thread context.12// 4) PT_LOAD segment describes a valid contiguous range of process address13// space.14//===----------------------------------------------------------------------===//15 16#ifndef LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSELFCORE_H17#define LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSELFCORE_H18 19#include <list>20#include <unordered_map>21#include <vector>22 23#include "lldb/Target/PostMortemProcess.h"24#include "lldb/Utility/Status.h"25 26#include "Plugins/ObjectFile/ELF/ELFHeader.h"27#include "Plugins/Process/elf-core/RegisterUtilities.h"28 29struct ThreadData;30 31class ProcessElfCore : public lldb_private::PostMortemProcess {32public:33 // Constructors and Destructors34 static lldb::ProcessSP35 CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,36 const lldb_private::FileSpec *crash_file_path,37 bool can_connect);38 39 static void Initialize();40 41 static void Terminate();42 43 static llvm::StringRef GetPluginNameStatic() { return "elf-core"; }44 45 static llvm::StringRef GetPluginDescriptionStatic();46 47 // Constructors and Destructors48 ProcessElfCore(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,49 const lldb_private::FileSpec &core_file);50 51 ~ProcessElfCore() override;52 53 // Check if a given Process54 bool CanDebug(lldb::TargetSP target_sp,55 bool plugin_specified_by_name) override;56 57 // Creating a new process, or attaching to an existing one58 lldb_private::Status DoLoadCore() override;59 60 lldb_private::DynamicLoader *GetDynamicLoader() override;61 62 // PluginInterface protocol63 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }64 65 // Process Control66 lldb_private::Status DoDestroy() override;67 68 void RefreshStateAfterStop() override;69 70 lldb_private::Status WillResume() override {71 return lldb_private::Status::FromErrorStringWithFormatv(72 "error: {0} does not support resuming processes", GetPluginName());73 }74 75 // Process Queries76 bool IsAlive() override;77 78 bool WarnBeforeDetach() const override { return false; }79 80 // Process Memory81 size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,82 lldb_private::Status &error) override;83 84 size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,85 lldb_private::Status &error) override;86 87 // We do not implement DoReadMemoryTags. Instead all the work is done in88 // ReadMemoryTags which avoids having to unpack and repack tags.89 llvm::Expected<std::vector<lldb::addr_t>> ReadMemoryTags(lldb::addr_t addr,90 size_t len) override;91 92 lldb::addr_t GetImageInfoAddress() override;93 94 lldb_private::ArchSpec GetArchitecture();95 96 // Returns AUXV structure found in the core file97 lldb_private::DataExtractor GetAuxvData() override;98 99 bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override;100 101protected:102 void Clear();103 104 bool DoUpdateThreadList(lldb_private::ThreadList &old_thread_list,105 lldb_private::ThreadList &new_thread_list) override;106 107 lldb_private::Status108 DoGetMemoryRegionInfo(lldb::addr_t load_addr,109 lldb_private::MemoryRegionInfo ®ion_info) override;110 111 bool SupportsMemoryTagging() override { return !m_core_tag_ranges.IsEmpty(); }112 113private:114 struct NT_FILE_Entry {115 lldb::addr_t start;116 lldb::addr_t end;117 lldb::addr_t file_ofs;118 std::string path;119 };120 121 // For ProcessElfCore only122 typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange;123 typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, FileRange>124 VMRangeToFileOffset;125 typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>126 VMRangeToPermissions;127 128 lldb::ModuleSP m_core_module_sp;129 std::string m_dyld_plugin_name;130 131 // True if m_thread_contexts contains valid entries132 bool m_thread_data_valid = false;133 134 // Contain thread data read from NOTE segments135 std::vector<ThreadData> m_thread_data;136 137 // AUXV structure found from the NOTE segment138 lldb_private::DataExtractor m_auxv;139 140 // Address ranges found in the core141 VMRangeToFileOffset m_core_aranges;142 143 // Permissions for all ranges144 VMRangeToPermissions m_core_range_infos;145 146 // Memory tag ranges found in the core147 VMRangeToFileOffset m_core_tag_ranges;148 149 // NT_FILE entries found from the NOTE segment150 std::vector<NT_FILE_Entry> m_nt_file_entries;151 152 // Map from file path to UUID for quick lookup153 std::unordered_map<std::string, lldb_private::UUID> m_uuids;154 155 // Executable name found from the ELF PRPSINFO156 std::string m_executable_name;157 158 // Parse thread(s) data structures(prstatus, prpsinfo) from given NOTE segment159 llvm::Error ParseThreadContextsFromNoteSegment(160 const elf::ELFProgramHeader &segment_header,161 const lldb_private::DataExtractor &segment_data);162 163 // Returns number of thread contexts stored in the core file164 uint32_t GetNumThreadContexts();165 166 // Populate gnu uuid for each NT_FILE entry167 void UpdateBuildIdForNTFileEntries();168 169 lldb_private::UUID FindModuleUUID(const llvm::StringRef path) override;170 171 // Returns the main executable path172 llvm::StringRef GetMainExecutablePath();173 174 // Returns the value of certain type of note of a given start address175 lldb_private::UUID FindBuidIdInCoreMemory(lldb::addr_t address);176 177 // Parse a contiguous address range of the process from LOAD segment178 lldb::addr_t179 AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader &header);180 181 // Parse a contiguous address range from a memory tag segment182 lldb::addr_t183 AddAddressRangeFromMemoryTagSegment(const elf::ELFProgramHeader &header);184 185 llvm::Expected<std::vector<lldb_private::CoreNote>>186 parseSegment(const lldb_private::DataExtractor &segment);187 llvm::Error parseFreeBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);188 llvm::Error parseNetBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);189 llvm::Error parseOpenBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);190 llvm::Error parseLinuxNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);191};192 193#endif // LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSELFCORE_H194