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/Process.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/Utility/Status.h"
25 
26 #include "Plugins/ObjectFile/ELF/ELFHeader.h"
27 #include "Plugins/Process/elf-core/RegisterUtilities.h"
28 
29 struct ThreadData;
30 
31 class ProcessElfCore : public lldb_private::Process {
32 public:
33   // Constructors and Destructors
34   static lldb::ProcessSP
35   CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
36                  const lldb_private::FileSpec *crash_file_path);
37 
38   static void Initialize();
39 
40   static void Terminate();
41 
42   static lldb_private::ConstString GetPluginNameStatic();
43 
44   static const char *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   lldb_private::ConstString GetPluginName() override;
63 
64   uint32_t GetPluginVersion() override;
65 
66   // Process Control
67   lldb_private::Status DoDestroy() override;
68 
69   void RefreshStateAfterStop() override;
70 
71   lldb_private::Status WillResume() override {
72     lldb_private::Status error;
73     error.SetErrorStringWithFormat(
74         "error: %s does not support resuming processes",
75         GetPluginName().GetCString());
76     return error;
77   }
78 
79   // Process Queries
80   bool IsAlive() override;
81 
82   bool WarnBeforeDetach() const override { return false; }
83 
84   // Process Memory
85   size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
86                     lldb_private::Status &error) override;
87 
88   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
89                       lldb_private::Status &error) override;
90 
91   lldb_private::Status
92   GetMemoryRegionInfo(lldb::addr_t load_addr,
93                       lldb_private::MemoryRegionInfo &region_info) override;
94 
95   lldb::addr_t GetImageInfoAddress() override;
96 
97   lldb_private::ArchSpec GetArchitecture();
98 
99   // Returns AUXV structure found in the core file
100   lldb_private::DataExtractor GetAuxvData() override;
101 
102   bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override;
103 
104 protected:
105   void Clear();
106 
107   bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
108                         lldb_private::ThreadList &new_thread_list) override;
109 
110 private:
111   struct NT_FILE_Entry {
112     lldb::addr_t start;
113     lldb::addr_t end;
114     lldb::addr_t file_ofs;
115     lldb_private::ConstString path;
116   };
117 
118   // For ProcessElfCore only
119   typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange;
120   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, FileRange>
121       VMRangeToFileOffset;
122   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>
123       VMRangeToPermissions;
124 
125   lldb::ModuleSP m_core_module_sp;
126   lldb_private::FileSpec m_core_file;
127   std::string m_dyld_plugin_name;
128   ProcessElfCore(const ProcessElfCore &) = delete;
129   const ProcessElfCore &operator=(const ProcessElfCore &) = delete;
130 
131   // True if m_thread_contexts contains valid entries
132   bool m_thread_data_valid = false;
133 
134   // Contain thread data read from NOTE segments
135   std::vector<ThreadData> m_thread_data;
136 
137   // AUXV structure found from the NOTE segment
138   lldb_private::DataExtractor m_auxv;
139 
140   // Address ranges found in the core
141   VMRangeToFileOffset m_core_aranges;
142 
143   // Permissions for all ranges
144   VMRangeToPermissions m_core_range_infos;
145 
146   // NT_FILE entries found from the NOTE segment
147   std::vector<NT_FILE_Entry> m_nt_file_entries;
148 
149   // Parse thread(s) data structures(prstatus, prpsinfo) from given NOTE segment
150   llvm::Error ParseThreadContextsFromNoteSegment(
151       const elf::ELFProgramHeader &segment_header,
152       lldb_private::DataExtractor segment_data);
153 
154   // Returns number of thread contexts stored in the core file
155   uint32_t GetNumThreadContexts();
156 
157   // Parse a contiguous address range of the process from LOAD segment
158   lldb::addr_t
159   AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader &header);
160 
161   llvm::Expected<std::vector<lldb_private::CoreNote>>
162   parseSegment(const lldb_private::DataExtractor &segment);
163   llvm::Error parseFreeBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
164   llvm::Error parseNetBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
165   llvm::Error parseOpenBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
166   llvm::Error parseLinuxNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
167 };
168 
169 #endif // LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSELFCORE_H
170