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 
33 struct ThreadData;
34 
35 class ProcessElfCore : public lldb_private::Process {
36 public:
37   //------------------------------------------------------------------
38   // Constructors and Destructors
39   //------------------------------------------------------------------
40   static lldb::ProcessSP
41   CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
42                  const lldb_private::FileSpec *crash_file_path);
43 
44   static void Initialize();
45 
46   static void Terminate();
47 
48   static lldb_private::ConstString GetPluginNameStatic();
49 
50   static const char *GetPluginDescriptionStatic();
51 
52   //------------------------------------------------------------------
53   // Constructors and Destructors
54   //------------------------------------------------------------------
55   ProcessElfCore(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
56                  const lldb_private::FileSpec &core_file);
57 
58   ~ProcessElfCore() override;
59 
60   //------------------------------------------------------------------
61   // Check if a given Process
62   //------------------------------------------------------------------
63   bool CanDebug(lldb::TargetSP target_sp,
64                 bool plugin_specified_by_name) override;
65 
66   //------------------------------------------------------------------
67   // Creating a new process, or attaching to an existing one
68   //------------------------------------------------------------------
69   lldb_private::Status DoLoadCore() override;
70 
71   lldb_private::DynamicLoader *GetDynamicLoader() override;
72 
73   //------------------------------------------------------------------
74   // PluginInterface protocol
75   //------------------------------------------------------------------
76   lldb_private::ConstString GetPluginName() override;
77 
78   uint32_t GetPluginVersion() override;
79 
80   //------------------------------------------------------------------
81   // Process Control
82   //------------------------------------------------------------------
83   lldb_private::Status DoDestroy() override;
84 
85   void RefreshStateAfterStop() override;
86 
87   lldb_private::Status WillResume() override {
88     lldb_private::Status error;
89     error.SetErrorStringWithFormat(
90         "error: %s does not support resuming processes",
91         GetPluginName().GetCString());
92     return error;
93   }
94 
95   //------------------------------------------------------------------
96   // Process Queries
97   //------------------------------------------------------------------
98   bool IsAlive() override;
99 
100   bool WarnBeforeDetach() const override { return false; }
101 
102   //------------------------------------------------------------------
103   // Process Memory
104   //------------------------------------------------------------------
105   size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
106                     lldb_private::Status &error) override;
107 
108   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
109                       lldb_private::Status &error) override;
110 
111   lldb_private::Status
112   GetMemoryRegionInfo(lldb::addr_t load_addr,
113                       lldb_private::MemoryRegionInfo &region_info) override;
114 
115   lldb::addr_t GetImageInfoAddress() override;
116 
117   lldb_private::ArchSpec GetArchitecture();
118 
119   // Returns AUXV structure found in the core file
120   const lldb::DataBufferSP GetAuxvData() override;
121 
122   bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override;
123 
124 protected:
125   void Clear();
126 
127   bool UpdateThreadList(lldb_private::ThreadList &old_thread_list,
128                         lldb_private::ThreadList &new_thread_list) override;
129 
130 private:
131   struct NT_FILE_Entry {
132     lldb::addr_t start;
133     lldb::addr_t end;
134     lldb::addr_t file_ofs;
135     lldb_private::ConstString path;
136   };
137 
138   //------------------------------------------------------------------
139   // For ProcessElfCore only
140   //------------------------------------------------------------------
141   typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange;
142   typedef lldb_private::RangeDataArray<lldb::addr_t, lldb::addr_t, FileRange, 1>
143       VMRangeToFileOffset;
144   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>
145       VMRangeToPermissions;
146 
147   lldb::ModuleSP m_core_module_sp;
148   lldb_private::FileSpec m_core_file;
149   std::string m_dyld_plugin_name;
150   DISALLOW_COPY_AND_ASSIGN(ProcessElfCore);
151 
152   llvm::Triple::OSType m_os;
153 
154   // True if m_thread_contexts contains valid entries
155   bool m_thread_data_valid;
156 
157   // Contain thread data read from NOTE segments
158   std::vector<ThreadData> m_thread_data;
159 
160   // AUXV structure found from the NOTE segment
161   lldb_private::DataExtractor m_auxv;
162 
163   // Address ranges found in the core
164   VMRangeToFileOffset m_core_aranges;
165 
166   // Permissions for all ranges
167   VMRangeToPermissions m_core_range_infos;
168 
169   // NT_FILE entries found from the NOTE segment
170   std::vector<NT_FILE_Entry> m_nt_file_entries;
171 
172   // Parse thread(s) data structures(prstatus, prpsinfo) from given NOTE segment
173   lldb_private::Status ParseThreadContextsFromNoteSegment(
174       const elf::ELFProgramHeader *segment_header,
175       lldb_private::DataExtractor segment_data);
176 
177   // Returns number of thread contexts stored in the core file
178   uint32_t GetNumThreadContexts();
179 
180   // Parse a contiguous address range of the process from LOAD segment
181   lldb::addr_t
182   AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader *header);
183 };
184 
185 #endif // liblldb_ProcessElfCore_h_
186