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