Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1 | //===-- DynamicLoaderDarwin.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 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef liblldb_DynamicLoaderDarwin_h_ |
| 11 | #define liblldb_DynamicLoaderDarwin_h_ |
| 12 | |
| 13 | // C Includes |
| 14 | // C++ Includes |
| 15 | #include <map> |
| 16 | #include <mutex> |
| 17 | #include <vector> |
| 18 | |
| 19 | // Other libraries and framework includes |
| 20 | // Project includes |
| 21 | #include "lldb/Target/DynamicLoader.h" |
| 22 | #include "lldb/Host/FileSpec.h" |
| 23 | #include "lldb/Core/StructuredData.h" |
| 24 | #include "lldb/Core/UUID.h" |
| 25 | #include "lldb/Host/Mutex.h" |
| 26 | #include "lldb/Target/Process.h" |
| 27 | #include "lldb/Utility/SafeMachO.h" |
| 28 | |
| 29 | namespace lldb_private { |
| 30 | |
| 31 | class DynamicLoaderDarwin : public lldb_private::DynamicLoader |
| 32 | { |
| 33 | public: |
| 34 | DynamicLoaderDarwin(lldb_private::Process *process); |
| 35 | |
| 36 | virtual ~DynamicLoaderDarwin() override; |
| 37 | |
| 38 | //------------------------------------------------------------------ |
| 39 | /// Called after attaching a process. |
| 40 | /// |
| 41 | /// Allow DynamicLoader plug-ins to execute some code after |
| 42 | /// attaching to a process. |
| 43 | //------------------------------------------------------------------ |
| 44 | void |
| 45 | DidAttach() override; |
| 46 | |
| 47 | void |
| 48 | DidLaunch() override; |
| 49 | |
| 50 | lldb::ThreadPlanSP |
| 51 | GetStepThroughTrampolinePlan(lldb_private::Thread &thread, |
| 52 | bool stop_others) override; |
| 53 | |
| 54 | size_t |
| 55 | FindEquivalentSymbols(lldb_private::Symbol *original_symbol, |
| 56 | lldb_private::ModuleList &module_list, |
| 57 | lldb_private::SymbolContextList &equivalent_symbols) override; |
| 58 | |
| 59 | lldb::addr_t |
| 60 | GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr) override; |
| 61 | |
| 62 | bool |
| 63 | AlwaysRelyOnEHUnwindInfo(lldb_private::SymbolContext &sym_ctx) override; |
| 64 | |
| 65 | virtual void |
| 66 | DoInitialImageFetch () = 0; |
| 67 | |
| 68 | virtual bool |
| 69 | NeedToDoInitialImageFetch () = 0; |
| 70 | |
| 71 | protected: |
| 72 | void |
| 73 | PrivateInitialize (lldb_private::Process *process); |
| 74 | |
| 75 | void |
| 76 | PrivateProcessStateChanged (lldb_private::Process *process, |
| 77 | lldb::StateType state); |
| 78 | |
| 79 | void |
| 80 | Clear (bool clear_process); |
| 81 | |
| 82 | // Clear method for classes derived from this one |
| 83 | virtual void |
| 84 | DoClear () = 0; |
| 85 | |
| 86 | void |
| 87 | SetDYLDModule (lldb::ModuleSP &dyld_module_sp); |
| 88 | |
| 89 | lldb::ModuleSP |
| 90 | GetDYLDModule (); |
| 91 | |
| 92 | class Segment |
| 93 | { |
| 94 | public: |
| 95 | Segment() : |
| 96 | name(), |
| 97 | vmaddr(LLDB_INVALID_ADDRESS), |
| 98 | vmsize(0), |
| 99 | fileoff(0), |
| 100 | filesize(0), |
| 101 | maxprot(0), |
| 102 | initprot(0), |
| 103 | nsects(0), |
| 104 | flags(0) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | lldb_private::ConstString name; |
| 109 | lldb::addr_t vmaddr; |
| 110 | lldb::addr_t vmsize; |
| 111 | lldb::addr_t fileoff; |
| 112 | lldb::addr_t filesize; |
| 113 | uint32_t maxprot; |
| 114 | uint32_t initprot; |
| 115 | uint32_t nsects; |
| 116 | uint32_t flags; |
| 117 | |
| 118 | bool |
| 119 | operator==(const Segment& rhs) const |
| 120 | { |
| 121 | return name == rhs.name && vmaddr == rhs.vmaddr && vmsize == rhs.vmsize; |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | PutToLog (lldb_private::Log *log, |
| 126 | lldb::addr_t slide) const; |
| 127 | |
| 128 | }; |
| 129 | |
| 130 | struct ImageInfo |
| 131 | { |
| 132 | lldb::addr_t address; // Address of mach header for this dylib |
| 133 | lldb::addr_t slide; // The amount to slide all segments by if there is a global slide. |
| 134 | lldb::addr_t mod_date; // Modification date for this dylib |
| 135 | lldb_private::FileSpec file_spec; // Resolved path for this dylib |
| 136 | lldb_private::UUID uuid; // UUID for this dylib if it has one, else all zeros |
| 137 | llvm::MachO::mach_header header; // The mach header for this image |
| 138 | std::vector<Segment> segments; // All segment vmaddr and vmsize pairs for this executable (from memory of inferior) |
| 139 | uint32_t load_stop_id; // The process stop ID that the sections for this image were loaded |
| 140 | |
| 141 | ImageInfo() : |
| 142 | address(LLDB_INVALID_ADDRESS), |
| 143 | slide(0), |
| 144 | mod_date(0), |
| 145 | file_spec(), |
| 146 | uuid(), |
| 147 | header(), |
| 148 | segments(), |
| 149 | load_stop_id(0) |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | Clear(bool load_cmd_data_only) |
| 155 | { |
| 156 | if (!load_cmd_data_only) |
| 157 | { |
| 158 | address = LLDB_INVALID_ADDRESS; |
| 159 | slide = 0; |
| 160 | mod_date = 0; |
| 161 | file_spec.Clear(); |
| 162 | ::memset (&header, 0, sizeof(header)); |
| 163 | } |
| 164 | uuid.Clear(); |
| 165 | segments.clear(); |
| 166 | load_stop_id = 0; |
| 167 | } |
| 168 | |
| 169 | bool |
| 170 | operator == (const ImageInfo& rhs) const |
| 171 | { |
| 172 | return address == rhs.address |
| 173 | && slide == rhs.slide |
| 174 | && mod_date == rhs.mod_date |
| 175 | && file_spec == rhs.file_spec |
| 176 | && uuid == rhs.uuid |
| 177 | && memcmp(&header, &rhs.header, sizeof(header)) == 0 |
| 178 | && segments == rhs.segments; |
| 179 | } |
| 180 | |
| 181 | bool |
| 182 | UUIDValid() const |
| 183 | { |
| 184 | return uuid.IsValid(); |
| 185 | } |
| 186 | |
| 187 | uint32_t |
| 188 | GetAddressByteSize () |
| 189 | { |
| 190 | if (header.cputype) |
| 191 | { |
| 192 | if (header.cputype & llvm::MachO::CPU_ARCH_ABI64) |
| 193 | return 8; |
| 194 | else |
| 195 | return 4; |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | lldb_private::ArchSpec |
| 201 | GetArchitecture () const |
| 202 | { |
| 203 | return lldb_private::ArchSpec (lldb_private::eArchTypeMachO, header.cputype, header.cpusubtype); |
| 204 | } |
| 205 | |
| 206 | const Segment * |
| 207 | FindSegment (const lldb_private::ConstString &name) const; |
| 208 | |
| 209 | void |
| 210 | PutToLog (lldb_private::Log *log) const; |
| 211 | |
| 212 | typedef std::vector<ImageInfo> collection; |
| 213 | typedef collection::iterator iterator; |
| 214 | typedef collection::const_iterator const_iterator; |
| 215 | }; |
| 216 | |
| 217 | bool |
| 218 | UpdateImageLoadAddress(lldb_private::Module *module, ImageInfo& info); |
| 219 | |
| 220 | bool |
| 221 | UnloadModuleSections (lldb_private::Module *module, ImageInfo& info); |
| 222 | |
| 223 | ImageInfo * |
| 224 | FindImageInfoForAddress (lldb::addr_t load_address); |
| 225 | |
| 226 | lldb::ModuleSP |
| 227 | FindTargetModuleForImageInfo (ImageInfo &image_info, |
| 228 | bool can_create, |
| 229 | bool *did_create_ptr); |
| 230 | |
| 231 | void |
| 232 | UnloadImages (const std::vector<lldb::addr_t> &solib_addresses); |
| 233 | |
| 234 | virtual bool |
| 235 | SetNotificationBreakpoint () = 0; |
| 236 | |
| 237 | virtual void |
| 238 | ClearNotificationBreakpoint () = 0; |
| 239 | |
| 240 | virtual bool |
| 241 | DidSetNotificationBreakpoint () = 0; |
| 242 | |
| 243 | typedef std::map<uint64_t, lldb::addr_t> PthreadKeyToTLSMap; |
| 244 | typedef std::map<lldb::user_id_t, PthreadKeyToTLSMap> ThreadIDToTLSMap; |
| 245 | |
| 246 | std::recursive_mutex & |
| 247 | GetMutex () const |
| 248 | { |
| 249 | return m_mutex; |
| 250 | } |
| 251 | |
| 252 | lldb::ModuleSP |
| 253 | GetPThreadLibraryModule(); |
| 254 | |
| 255 | lldb_private::Address |
| 256 | GetPthreadSetSpecificAddress(); |
| 257 | |
| 258 | bool |
| 259 | JSONImageInformationIntoImageInfo (lldb_private::StructuredData::ObjectSP image_details, ImageInfo::collection &image_infos); |
| 260 | |
| 261 | // If image_infos contains / may contain dyld image, call this method |
| 262 | // to keep our internal record keeping of the special dyld binary up-to-date. |
| 263 | void |
| 264 | UpdateDYLDImageInfoFromNewImageInfos (ImageInfo::collection &image_infos); |
| 265 | |
| 266 | // if image_info is a dyld binary, call this method |
| 267 | void |
| 268 | UpdateDYLDImageInfoFromNewImageInfo (ImageInfo &image_info); |
| 269 | |
| 270 | // If image_infos contains / may contain executable image, call this method |
| 271 | // to keep our internal record keeping of the special dyld binary up-to-date. |
| 272 | void |
| 273 | AddExecutableModuleIfInImageInfos (ImageInfo::collection &image_infos); |
| 274 | |
| 275 | bool |
| 276 | AddModulesUsingImageInfos (ImageInfo::collection &image_infos); |
| 277 | |
| 278 | lldb::ModuleWP m_dyld_module_wp; |
| 279 | lldb::ModuleWP m_libpthread_module_wp; |
| 280 | lldb_private::Address m_pthread_getspecific_addr; |
| 281 | ThreadIDToTLSMap m_tid_to_tls_map; |
| 282 | ImageInfo::collection m_dyld_image_infos; // Current shared libraries information |
| 283 | uint32_t m_dyld_image_infos_stop_id; // The process stop ID that "m_dyld_image_infos" is valid for |
| 284 | ImageInfo m_dyld; |
| 285 | mutable std::recursive_mutex m_mutex; |
| 286 | |
| 287 | private: |
| 288 | DISALLOW_COPY_AND_ASSIGN (DynamicLoaderDarwin); |
| 289 | }; |
| 290 | |
| 291 | } // namespace lldb_private |
| 292 | |
| 293 | #endif // liblldb_DynamicLoaderDarwin_h_ |