1 //===-- ProcessElfCore.h ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Notes about Linux Process core dumps:
9 //  1) Linux core dump is stored as ELF file.
10 //  2) The ELF file's PT_NOTE and PT_LOAD segments describes the program's
11 //     address space and thread contexts.
12 //  3) PT_NOTE segment contains note entries which describes a thread context.
13 //  4) PT_LOAD segment describes a valid contiguous range of process address
14 //     space.
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef liblldb_ProcessElfCore_h_
18 #define liblldb_ProcessElfCore_h_
19 
20 // C Includes
21 // C++ Includes
22 #include <list>
23 #include <vector>
24 
25 // Other libraries and framework includes
26 // Project includes
27 #include "lldb/Target/Process.h"
28 #include "lldb/Utility/ConstString.h"
29 #include "lldb/Utility/Status.h"
30 
31 #include "Plugins/ObjectFile/ELF/ELFHeader.h"
32 #include "Plugins/Process/elf-core/RegisterUtilities.h"
33 
34 struct ThreadData;
35 
36 class ProcessElfCore : public lldb_private::Process {
37 public:
38   //------------------------------------------------------------------
39   // Constructors and Destructors
40   //------------------------------------------------------------------
41   static lldb::ProcessSP
42   CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
43                  const lldb_private::FileSpec *crash_file_path);
44 
45   static void Initialize();
46 
47   static void Terminate();
48 
49   static lldb_private::ConstString GetPluginNameStatic();
50 
51   static const char *GetPluginDescriptionStatic();
52 
53   //------------------------------------------------------------------
54   // Constructors and Destructors
55   //------------------------------------------------------------------
56   ProcessElfCore(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
57                  const lldb_private::FileSpec &core_file);
58 
59   ~ProcessElfCore() override;
60 
61   //------------------------------------------------------------------
62   // Check if a given Process
63   //------------------------------------------------------------------
64   bool CanDebug(lldb::TargetSP target_sp,
65                 bool plugin_specified_by_name) override;
66 
67   //------------------------------------------------------------------
68   // Creating a new process, or attaching to an existing one
69   //------------------------------------------------------------------
70   lldb_private::Status DoLoadCore() override;
71 
72   lldb_private::DynamicLoader *GetDynamicLoader() override;
73 
74   //------------------------------------------------------------------
75   // PluginInterface protocol
76   //------------------------------------------------------------------
77   lldb_private::ConstString GetPluginName() override;
78 
79   uint32_t GetPluginVersion() override;
80 
81   //------------------------------------------------------------------
82   // Process Control
83   //------------------------------------------------------------------
84   lldb_private::Status DoDestroy() override;
85 
86   void RefreshStateAfterStop() override;
87 
88   lldb_private::Status WillResume() override {
89     lldb_private::Status error;
90     error.SetErrorStringWithFormat(
91         "error: %s does not support resuming processes",
92         GetPluginName().GetCString());
93     return error;
94   }
95 
96   //------------------------------------------------------------------
97   // Process Queries
98   //------------------------------------------------------------------
99   bool IsAlive() override;
100 
101   bool WarnBeforeDetach() const override { return false; }
102 
103   //------------------------------------------------------------------
104   // Process Memory
105   //------------------------------------------------------------------
106   size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
107                     lldb_private::Status &error) override;
108 
109   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
110                       lldb_private::Status &error) override;
111 
112   lldb_private::Status
113   GetMemoryRegionInfo(lldb::addr_t load_addr,
114                       lldb_private::MemoryRegionInfo &region_info) override;
115 
116   lldb::addr_t GetImageInfoAddress() override;
117 
118   lldb_private::ArchSpec GetArchitecture();
119 
120   // Returns AUXV structure found in the core file
121   const lldb::DataBufferSP GetAuxvData() override;
122 
123   bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override;
124 
125 protected:
126   void Clear();
127 
128   bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
129                         lldb_private::ThreadList &new_thread_list) override;
130 
131 private:
132   struct NT_FILE_Entry {
133     lldb::addr_t start;
134     lldb::addr_t end;
135     lldb::addr_t file_ofs;
136     lldb_private::ConstString path;
137   };
138 
139   //------------------------------------------------------------------
140   // For ProcessElfCore only
141   //------------------------------------------------------------------
142   typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange;
143   typedef lldb_private::RangeDataArray<lldb::addr_t, lldb::addr_t, FileRange, 1>
144       VMRangeToFileOffset;
145   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>
146       VMRangeToPermissions;
147 
148   lldb::ModuleSP m_core_module_sp;
149   lldb_private::FileSpec m_core_file;
150   std::string m_dyld_plugin_name;
151   DISALLOW_COPY_AND_ASSIGN(ProcessElfCore);
152 
153   // True if m_thread_contexts contains valid entries
154   bool m_thread_data_valid = false;
155 
156   // Contain thread data read from NOTE segments
157   std::vector<ThreadData> m_thread_data;
158 
159   // AUXV structure found from the NOTE segment
160   lldb_private::DataExtractor m_auxv;
161 
162   // Address ranges found in the core
163   VMRangeToFileOffset m_core_aranges;
164 
165   // Permissions for all ranges
166   VMRangeToPermissions m_core_range_infos;
167 
168   // NT_FILE entries found from the NOTE segment
169   std::vector<NT_FILE_Entry> m_nt_file_entries;
170 
171   // Parse thread(s) data structures(prstatus, prpsinfo) from given NOTE segment
172   llvm::Error ParseThreadContextsFromNoteSegment(
173       const elf::ELFProgramHeader *segment_header,
174       lldb_private::DataExtractor segment_data);
175 
176   // Returns number of thread contexts stored in the core file
177   uint32_t GetNumThreadContexts();
178 
179   // Parse a contiguous address range of the process from LOAD segment
180   lldb::addr_t
181   AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader *header);
182 
183   llvm::Expected<std::vector<lldb_private::CoreNote>>
184   parseSegment(const lldb_private::DataExtractor &segment);
185   llvm::Error parseFreeBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
186   llvm::Error parseNetBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
187   llvm::Error parseOpenBSDNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
188   llvm::Error parseLinuxNotes(llvm::ArrayRef<lldb_private::CoreNote> notes);
189 };
190 
191 #endif // liblldb_ProcessElfCore_h_
192