xref: /llvm-project/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h (revision c2ffb42893eb543f64169e32851e00352feaca69)
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-exception
6 //
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's
10 //     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 address
13 //     space.
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSELFCORE_H
17 #define LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSELFCORE_H
18 
19 #include <list>
20 #include <vector>
21 
22 #include "lldb/Target/PostMortemProcess.h"
23 #include "lldb/Utility/Status.h"
24 
25 #include "Plugins/ObjectFile/ELF/ELFHeader.h"
26 #include "Plugins/Process/elf-core/RegisterUtilities.h"
27 
28 struct ThreadData;
29 
30 class ProcessElfCore : public lldb_private::PostMortemProcess {
31 public:
32   // Constructors and Destructors
33   static lldb::ProcessSP
34   CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
35                  const lldb_private::FileSpec *crash_file_path,
36                  bool can_connect);
37 
38   static void Initialize();
39 
40   static void Terminate();
41 
42   static llvm::StringRef GetPluginNameStatic() { return "elf-core"; }
43 
44   static llvm::StringRef GetPluginDescriptionStatic();
45 
46   // Constructors and Destructors
47   ProcessElfCore(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
48                  const lldb_private::FileSpec &core_file);
49 
50   ~ProcessElfCore() override;
51 
52   // Check if a given Process
53   bool CanDebug(lldb::TargetSP target_sp,
54                 bool plugin_specified_by_name) override;
55 
56   // Creating a new process, or attaching to an existing one
57   lldb_private::Status DoLoadCore() override;
58 
59   lldb_private::DynamicLoader *GetDynamicLoader() override;
60 
61   // PluginInterface protocol
62   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
63 
64   // Process Control
65   lldb_private::Status DoDestroy() override;
66 
67   void RefreshStateAfterStop() override;
68 
69   lldb_private::Status WillResume() override {
70     return lldb_private::Status::FromErrorStringWithFormatv(
71         "error: {0} does not support resuming processes", GetPluginName());
72   }
73 
74   // Process Queries
75   bool IsAlive() override;
76 
77   bool WarnBeforeDetach() const override { return false; }
78 
79   // Process Memory
80   size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
81                     lldb_private::Status &error) override;
82 
83   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
84                       lldb_private::Status &error) override;
85 
86   // We do not implement DoReadMemoryTags. Instead all the work is done in
87   // ReadMemoryTags which avoids having to unpack and repack tags.
88   llvm::Expected<std::vector<lldb::addr_t>> ReadMemoryTags(lldb::addr_t addr,
89                                                            size_t len) override;
90 
91   lldb::addr_t GetImageInfoAddress() override;
92 
93   lldb_private::ArchSpec GetArchitecture();
94 
95   // Returns AUXV structure found in the core file
96   lldb_private::DataExtractor GetAuxvData() override;
97 
98   bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override;
99 
100 protected:
101   void Clear();
102 
103   bool DoUpdateThreadList(lldb_private::ThreadList &old_thread_list,
104                           lldb_private::ThreadList &new_thread_list) override;
105 
106   lldb_private::Status
107   DoGetMemoryRegionInfo(lldb::addr_t load_addr,
108                         lldb_private::MemoryRegionInfo &region_info) override;
109 
110   bool SupportsMemoryTagging() override { return !m_core_tag_ranges.IsEmpty(); }
111 
112 private:
113   struct NT_FILE_Entry {
114     lldb::addr_t start;
115     lldb::addr_t end;
116     lldb::addr_t file_ofs;
117     std::string path;
118     // Add a UUID member for convenient access. The UUID value is not in the
119     // NT_FILE entries, we will find it in core memory and store it here for
120     // easy access.
121     lldb_private::UUID uuid;
122   };
123 
124   // For ProcessElfCore only
125   typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange;
126   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, FileRange>
127       VMRangeToFileOffset;
128   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>
129       VMRangeToPermissions;
130 
131   lldb::ModuleSP m_core_module_sp;
132   std::string m_dyld_plugin_name;
133 
134   // True if m_thread_contexts contains valid entries
135   bool m_thread_data_valid = false;
136 
137   // Contain thread data read from NOTE segments
138   std::vector<ThreadData> m_thread_data;
139 
140   // AUXV structure found from the NOTE segment
141   lldb_private::DataExtractor m_auxv;
142 
143   // Address ranges found in the core
144   VMRangeToFileOffset m_core_aranges;
145 
146   // Permissions for all ranges
147   VMRangeToPermissions m_core_range_infos;
148 
149   // Memory tag ranges found in the core
150   VMRangeToFileOffset m_core_tag_ranges;
151 
152   // NT_FILE entries found from the NOTE segment
153   std::vector<NT_FILE_Entry> m_nt_file_entries;
154 
155   // Parse thread(s) data structures(prstatus, prpsinfo) from given NOTE segment
156   llvm::Error ParseThreadContextsFromNoteSegment(
157       const elf::ELFProgramHeader &segment_header,
158       const lldb_private::DataExtractor &segment_data);
159 
160   // Returns number of thread contexts stored in the core file
161   uint32_t GetNumThreadContexts();
162 
163   // Populate gnu uuid for each NT_FILE entry
164   void UpdateBuildIdForNTFileEntries();
165 
166   lldb_private::UUID FindModuleUUID(const llvm::StringRef path) override;
167 
168   // Returns the value of certain type of note of a given start address
169   lldb_private::UUID FindBuidIdInCoreMemory(lldb::addr_t address);
170 
171   // Parse a contiguous address range of the process from LOAD segment
172   lldb::addr_t
173   AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader &header);
174 
175   // Parse a contiguous address range from a memory tag segment
176   lldb::addr_t
177   AddAddressRangeFromMemoryTagSegment(const elf::ELFProgramHeader &header);
178 
179   llvm::Expected<std::vector<lldb_private::CoreNote>>
180   parseSegment(const lldb_private::DataExtractor &segment);
181   llvm::Error parseFreeBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
182   llvm::Error parseNetBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
183   llvm::Error parseOpenBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
184   llvm::Error parseLinuxNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
185 };
186 
187 #endif // LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSELFCORE_H
188