LLDB  mainline
DynamicLoaderDarwin.h
Go to the documentation of this file.
1 //===-- DynamicLoaderDarwin.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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERDARWIN_H
10 #define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERDARWIN_H
11 
12 #include <map>
13 #include <mutex>
14 #include <vector>
15 
16 #include "lldb/Host/SafeMachO.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Utility/FileSpec.h"
21 #include "lldb/Utility/UUID.h"
22 
23 #include "llvm/ADT/Triple.h"
24 
25 namespace lldb_private {
26 
28 public:
30 
31  ~DynamicLoaderDarwin() override;
32 
33  /// Called after attaching a process.
34  ///
35  /// Allow DynamicLoader plug-ins to execute some code after
36  /// attaching to a process.
37  void DidAttach() override;
38 
39  void DidLaunch() override;
40 
41  lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread,
42  bool stop_others) override;
43 
45  lldb_private::Symbol *original_symbol,
46  lldb_private::ModuleList &module_list,
47  lldb_private::SymbolContextList &equivalent_symbols) override;
48 
49  lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module,
50  const lldb::ThreadSP thread,
51  lldb::addr_t tls_file_addr) override;
52 
54 
55  virtual void DoInitialImageFetch() = 0;
56 
57  virtual bool NeedToDoInitialImageFetch() = 0;
58 
59 protected:
61 
63  lldb::StateType state);
64 
65  void Clear(bool clear_process);
66 
67  // Clear method for classes derived from this one
68  virtual void DoClear() = 0;
69 
70  void SetDYLDModule(lldb::ModuleSP &dyld_module_sp);
71 
72  lldb::ModuleSP GetDYLDModule();
73 
74  class Segment {
75  public:
76  Segment() : name() {}
77 
87 
88  bool operator==(const Segment &rhs) const {
89  return name == rhs.name && vmaddr == rhs.vmaddr && vmsize == rhs.vmsize;
90  }
91 
92  void PutToLog(lldb_private::Log *log, lldb::addr_t slide) const;
93  };
94 
95  struct ImageInfo {
96  /// Address of mach header for this dylib.
98  /// The amount to slide all segments by if there is a global
99  /// slide.
101  /// Modification date for this dylib.
103  /// Resolved path for this dylib.
105  /// UUID for this dylib if it has one, else all zeros.
107  /// The mach header for this image.
108  llvm::MachO::mach_header header;
109  /// All segment vmaddr and vmsize pairs for this executable (from
110  /// memory of inferior).
111  std::vector<Segment> segments;
112  /// The process stop ID that the sections for this image were
113  /// loaded.
115  /// LC_VERSION_MIN_... load command os type.
116  llvm::Triple::OSType os_type = llvm::Triple::OSType::UnknownOS;
117  /// LC_VERSION_MIN_... load command os environment.
118  llvm::Triple::EnvironmentType os_env =
119  llvm::Triple::EnvironmentType::UnknownEnvironment;
120  /// LC_VERSION_MIN_... SDK.
122 
123  ImageInfo() = default;
124 
125  void Clear(bool load_cmd_data_only) {
126  if (!load_cmd_data_only) {
128  slide = 0;
129  mod_date = 0;
130  file_spec.Clear();
131  ::memset(&header, 0, sizeof(header));
132  }
133  uuid.Clear();
134  segments.clear();
135  load_stop_id = 0;
136  os_type = llvm::Triple::OSType::UnknownOS;
137  os_env = llvm::Triple::EnvironmentType::UnknownEnvironment;
138  min_version_os_sdk.clear();
139  }
140 
141  bool operator==(const ImageInfo &rhs) const {
142  return address == rhs.address && slide == rhs.slide &&
143  mod_date == rhs.mod_date && file_spec == rhs.file_spec &&
144  uuid == rhs.uuid &&
145  memcmp(&header, &rhs.header, sizeof(header)) == 0 &&
146  segments == rhs.segments && os_type == rhs.os_type &&
147  os_env == rhs.os_env;
148  }
149 
150  bool UUIDValid() const { return uuid.IsValid(); }
151 
153  if (header.cputype) {
154  if (header.cputype & llvm::MachO::CPU_ARCH_ABI64)
155  return 8;
156  else
157  return 4;
158  }
159  return 0;
160  }
161 
163 
164  const Segment *FindSegment(lldb_private::ConstString name) const;
165 
166  void PutToLog(lldb_private::Log *log) const;
167 
168  typedef std::vector<ImageInfo> collection;
169  typedef collection::iterator iterator;
170  typedef collection::const_iterator const_iterator;
171  };
172 
174 
176 
177  lldb::ModuleSP FindTargetModuleForImageInfo(ImageInfo &image_info,
178  bool can_create,
179  bool *did_create_ptr);
180 
181  void UnloadImages(const std::vector<lldb::addr_t> &solib_addresses);
182 
183  void UnloadAllImages();
184 
185  virtual bool SetNotificationBreakpoint() = 0;
186 
187  virtual void ClearNotificationBreakpoint() = 0;
188 
189  virtual bool DidSetNotificationBreakpoint() = 0;
190 
191  typedef std::map<uint64_t, lldb::addr_t> PthreadKeyToTLSMap;
192  typedef std::map<lldb::user_id_t, PthreadKeyToTLSMap> ThreadIDToTLSMap;
193 
194  std::recursive_mutex &GetMutex() const { return m_mutex; }
195 
196  lldb::ModuleSP GetPThreadLibraryModule();
197 
199 
202  ImageInfo::collection &image_infos);
203 
204  // If image_infos contains / may contain dyld or executable image, call this
205  // method
206  // to keep our internal record keeping of the special binaries up-to-date.
207  void
209 
210  // if image_info is a dyld binary, call this method
211  void UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info);
212 
213  // If image_infos contains / may contain executable image, call this method
214  // to keep our internal record keeping of the special dyld binary up-to-date.
216 
218 
219  // Whether we should use the new dyld SPI to get shared library information,
220  // or read
221  // it directly out of the dyld_all_image_infos. Whether we use the (newer)
222  // DynamicLoaderMacOS
223  // plugin or the (older) DynamicLoaderMacOSX plugin.
224  static bool UseDYLDSPI(lldb_private::Process *process);
225 
226  lldb::ModuleWP m_dyld_module_wp; // the dyld whose file type (mac, ios, etc)
227  // matches the process
228  lldb::ModuleWP m_libpthread_module_wp;
232  m_dyld_image_infos; // Current shared libraries information
233  uint32_t m_dyld_image_infos_stop_id; // The process stop ID that
234  // "m_dyld_image_infos" is valid for
236  mutable std::recursive_mutex m_mutex;
237 
238 private:
239  DynamicLoaderDarwin(const DynamicLoaderDarwin &) = delete;
240  const DynamicLoaderDarwin &operator=(const DynamicLoaderDarwin &) = delete;
241 };
242 
243 } // namespace lldb_private
244 
245 #endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERDARWIN_H
lldb_private::DynamicLoaderDarwin::FindEquivalentSymbols
void FindEquivalentSymbols(lldb_private::Symbol *original_symbol, lldb_private::ModuleList &module_list, lldb_private::SymbolContextList &equivalent_symbols) override
Some dynamic loaders provide features where there are a group of symbols "equivalent to" a given symb...
Definition: DynamicLoaderDarwin.cpp:1016
lldb_private::UUID
Definition: UUID.h:23
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::DynamicLoaderDarwin::GetPThreadLibraryModule
lldb::ModuleSP GetPThreadLibraryModule()
Definition: DynamicLoaderDarwin.cpp:1035
lldb_private::DynamicLoaderDarwin::Segment::nsects
uint32_t nsects
Definition: DynamicLoaderDarwin.h:85
lldb_private::DynamicLoaderDarwin::ImageInfo::segments
std::vector< Segment > segments
All segment vmaddr and vmsize pairs for this executable (from memory of inferior).
Definition: DynamicLoaderDarwin.h:111
lldb_private::DynamicLoaderDarwin::GetDYLDModule
lldb::ModuleSP GetDYLDModule()
Definition: DynamicLoaderDarwin.cpp:625
lldb_private::DynamicLoaderDarwin::ImageInfo::GetAddressByteSize
uint32_t GetAddressByteSize()
Definition: DynamicLoaderDarwin.h:152
lldb_private::DynamicLoaderDarwin::m_dyld
ImageInfo m_dyld
Definition: DynamicLoaderDarwin.h:235
lldb_private::Symbol
Definition: Symbol.h:20
StructuredData.h
UUID.h
lldb_private::DynamicLoaderDarwin::UpdateImageLoadAddress
bool UpdateImageLoadAddress(lldb_private::Module *module, ImageInfo &info)
Definition: DynamicLoaderDarwin.cpp:243
lldb_private::DynamicLoaderDarwin::JSONImageInformationIntoImageInfo
bool JSONImageInformationIntoImageInfo(lldb_private::StructuredData::ObjectSP image_details, ImageInfo::collection &image_infos)
Definition: DynamicLoaderDarwin.cpp:355
lldb_private::DynamicLoaderDarwin::m_tid_to_tls_map
ThreadIDToTLSMap m_tid_to_tls_map
Definition: DynamicLoaderDarwin.h:230
lldb_private::DynamicLoaderDarwin::m_pthread_getspecific_addr
lldb_private::Address m_pthread_getspecific_addr
Definition: DynamicLoaderDarwin.h:229
lldb_private::DynamicLoaderDarwin::ImageInfo::collection
std::vector< ImageInfo > collection
Definition: DynamicLoaderDarwin.h:168
lldb_private::UUID::IsValid
bool IsValid() const
Definition: UUID.h:79
lldb_private::DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos
void UpdateSpecialBinariesFromNewImageInfos(ImageInfo::collection &image_infos)
Definition: DynamicLoaderDarwin.cpp:531
lldb_private::Process
Definition: Process.h:341
lldb_private::DynamicLoaderDarwin::GetMutex
std::recursive_mutex & GetMutex() const
Definition: DynamicLoaderDarwin.h:194
lldb_private::SymbolContextList
Definition: SymbolContext.h:379
lldb_private::Module
Definition: Module.h:85
lldb_private::DynamicLoaderDarwin::DoClear
virtual void DoClear()=0
lldb_private::DynamicLoaderDarwin::ImageInfo::UUIDValid
bool UUIDValid() const
Definition: DynamicLoaderDarwin.h:150
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::DynamicLoaderDarwin::UnloadModuleSections
bool UnloadModuleSections(lldb_private::Module *module, ImageInfo &info)
Definition: DynamicLoaderDarwin.cpp:320
lldb_private::DynamicLoaderDarwin::ImageInfo
Definition: DynamicLoaderDarwin.h:95
lldb_private::DynamicLoaderDarwin::ImageInfo::FindSegment
const Segment * FindSegment(lldb_private::ConstString name) const
Definition: DynamicLoaderDarwin.cpp:795
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::DynamicLoaderDarwin::ImageInfo::file_spec
lldb_private::FileSpec file_spec
Resolved path for this dylib.
Definition: DynamicLoaderDarwin.h:104
lldb_private::DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo
bool AlwaysRelyOnEHUnwindInfo(lldb_private::SymbolContext &sym_ctx) override
Ask if the eh_frame information for the given SymbolContext should be relied on even when it's the fi...
Definition: DynamicLoaderDarwin.cpp:737
Process.h
lldb_private::DynamicLoaderDarwin::SetDYLDModule
void SetDYLDModule(lldb::ModuleSP &dyld_module_sp)
Definition: DynamicLoaderDarwin.cpp:621
SafeMachO.h
lldb_private::DynamicLoaderDarwin::AddExecutableModuleIfInImageInfos
void AddExecutableModuleIfInImageInfos(ImageInfo::collection &image_infos)
lldb_private::DynamicLoaderDarwin::PrivateProcessStateChanged
void PrivateProcessStateChanged(lldb_private::Process *process, lldb::StateType state)
Definition: DynamicLoaderDarwin.cpp:828
lldb_private::DynamicLoaderDarwin::ImageInfo::iterator
collection::iterator iterator
Definition: DynamicLoaderDarwin.h:169
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo
void UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info)
Definition: DynamicLoaderDarwin.cpp:606
lldb_private::DynamicLoaderDarwin::DidLaunch
void DidLaunch() override
Called after attaching a process.
Definition: DynamicLoaderDarwin.cpp:79
lldb_private::DynamicLoaderDarwin::AddModulesUsingImageInfos
bool AddModulesUsingImageInfos(ImageInfo::collection &image_infos)
Definition: DynamicLoaderDarwin.cpp:630
lldb_private::DynamicLoaderDarwin::ImageInfo::Clear
void Clear(bool load_cmd_data_only)
Definition: DynamicLoaderDarwin.h:125
lldb_private::DynamicLoaderDarwin::ImageInfo::address
lldb::addr_t address
Address of mach header for this dylib.
Definition: DynamicLoaderDarwin.h:97
lldb_private::DynamicLoaderDarwin::m_dyld_image_infos_stop_id
uint32_t m_dyld_image_infos_stop_id
Definition: DynamicLoaderDarwin.h:233
lldb_private::DynamicLoaderDarwin::UseDYLDSPI
static bool UseDYLDSPI(lldb_private::Process *process)
Definition: DynamicLoaderDarwin.cpp:1153
lldb_private::DynamicLoaderDarwin::ImageInfo::mod_date
lldb::addr_t mod_date
Modification date for this dylib.
Definition: DynamicLoaderDarwin.h:102
lldb_private::DynamicLoaderDarwin::ImageInfo::load_stop_id
uint32_t load_stop_id
The process stop ID that the sections for this image were loaded.
Definition: DynamicLoaderDarwin.h:114
lldb_private::ModuleList
Definition: ModuleList.h:72
lldb_private::DynamicLoaderDarwin::m_mutex
std::recursive_mutex m_mutex
Definition: DynamicLoaderDarwin.h:236
lldb_private::DynamicLoaderDarwin::Segment::vmaddr
lldb::addr_t vmaddr
Definition: DynamicLoaderDarwin.h:79
lldb_private::Thread
Definition: Thread.h:60
lldb_private::DynamicLoaderDarwin::Segment::flags
uint32_t flags
Definition: DynamicLoaderDarwin.h:86
lldb_private::DynamicLoaderDarwin::GetStepThroughTrampolinePlan
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, bool stop_others) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
Definition: DynamicLoaderDarwin.cpp:863
lldb_private::DynamicLoaderDarwin::ImageInfo::uuid
lldb_private::UUID uuid
UUID for this dylib if it has one, else all zeros.
Definition: DynamicLoaderDarwin.h:106
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::DynamicLoaderDarwin::operator=
const DynamicLoaderDarwin & operator=(const DynamicLoaderDarwin &)=delete
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::DynamicLoaderDarwin::ImageInfo::header
llvm::MachO::mach_header header
The mach header for this image.
Definition: DynamicLoaderDarwin.h:108
lldb_private::DynamicLoaderDarwin::Segment::vmsize
lldb::addr_t vmsize
Definition: DynamicLoaderDarwin.h:80
lldb_private::DynamicLoaderDarwin::GetPthreadSetSpecificAddress
lldb_private::Address GetPthreadSetSpecificAddress()
Definition: DynamicLoaderDarwin.cpp:1055
lldb_private::DynamicLoaderDarwin::m_dyld_module_wp
lldb::ModuleWP m_dyld_module_wp
Definition: DynamicLoaderDarwin.h:226
lldb_private::DynamicLoaderDarwin::ImageInfo::PutToLog
void PutToLog(lldb_private::Log *log) const
Definition: DynamicLoaderDarwin.cpp:805
lldb_private::DynamicLoaderDarwin::NeedToDoInitialImageFetch
virtual bool NeedToDoInitialImageFetch()=0
lldb_private::DynamicLoaderDarwin::Segment::filesize
lldb::addr_t filesize
Definition: DynamicLoaderDarwin.h:82
lldb_private::DynamicLoader
Definition: DynamicLoader.h:52
lldb_private::DynamicLoaderDarwin::DynamicLoaderDarwin
DynamicLoaderDarwin(lldb_private::Process *process)
Definition: DynamicLoaderDarwin.cpp:57
lldb_private::DynamicLoaderDarwin::PthreadKeyToTLSMap
std::map< uint64_t, lldb::addr_t > PthreadKeyToTLSMap
Definition: DynamicLoaderDarwin.h:191
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::DynamicLoaderDarwin::Segment::name
lldb_private::ConstString name
Definition: DynamicLoaderDarwin.h:78
lldb_private::DynamicLoaderDarwin::Segment
Definition: DynamicLoaderDarwin.h:74
lldb_private::DynamicLoaderDarwin::ClearNotificationBreakpoint
virtual void ClearNotificationBreakpoint()=0
lldb_private::DynamicLoaderDarwin
Definition: DynamicLoaderDarwin.h:27
lldb_private::DynamicLoaderDarwin::PrivateInitialize
void PrivateInitialize(lldb_private::Process *process)
Definition: DynamicLoaderDarwin.cpp:819
lldb_private::DynamicLoaderDarwin::ImageInfo::const_iterator
collection::const_iterator const_iterator
Definition: DynamicLoaderDarwin.h:170
lldb_private::DynamicLoaderDarwin::Clear
void Clear(bool clear_process)
Definition: DynamicLoaderDarwin.cpp:86
lldb_private::DynamicLoaderDarwin::Segment::initprot
uint32_t initprot
Definition: DynamicLoaderDarwin.h:84
lldb_private::FileSpec::Clear
void Clear()
Clears the object state.
Definition: FileSpec.cpp:261
lldb_private::UUID::Clear
void Clear()
Definition: UUID.h:72
lldb_private::DynamicLoaderDarwin::ImageInfo::operator==
bool operator==(const ImageInfo &rhs) const
Definition: DynamicLoaderDarwin.h:141
lldb_private::DynamicLoaderDarwin::GetThreadLocalData
lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr) override
Retrieves the per-module TLS block for a given thread.
Definition: DynamicLoaderDarwin.cpp:1073
lldb_private::DynamicLoaderDarwin::ImageInfo::os_env
llvm::Triple::EnvironmentType os_env
LC_VERSION_MIN_... load command os environment.
Definition: DynamicLoaderDarwin.h:118
lldb_private::DynamicLoaderDarwin::Segment::fileoff
lldb::addr_t fileoff
Definition: DynamicLoaderDarwin.h:81
lldb_private::DynamicLoaderDarwin::FindTargetModuleForImageInfo
lldb::ModuleSP FindTargetModuleForImageInfo(ImageInfo &image_info, bool can_create, bool *did_create_ptr)
Definition: DynamicLoaderDarwin.cpp:95
uint32_t
lldb_private::Address
Definition: Address.h:59
lldb_private::DynamicLoaderDarwin::UnloadAllImages
void UnloadAllImages()
Definition: DynamicLoaderDarwin.cpp:211
lldb_private::DynamicLoaderDarwin::Segment::operator==
bool operator==(const Segment &rhs) const
Definition: DynamicLoaderDarwin.h:88
lldb_private::DynamicLoaderDarwin::UnloadImages
void UnloadImages(const std::vector< lldb::addr_t > &solib_addresses)
Definition: DynamicLoaderDarwin.cpp:163
lldb_private::DynamicLoaderDarwin::ImageInfo::ImageInfo
ImageInfo()=default
lldb_private::DynamicLoaderDarwin::m_dyld_image_infos
ImageInfo::collection m_dyld_image_infos
Definition: DynamicLoaderDarwin.h:232
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::DynamicLoaderDarwin::SetNotificationBreakpoint
virtual bool SetNotificationBreakpoint()=0
lldb_private::DynamicLoaderDarwin::ImageInfo::slide
lldb::addr_t slide
The amount to slide all segments by if there is a global slide.
Definition: DynamicLoaderDarwin.h:100
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::DynamicLoaderDarwin::DidSetNotificationBreakpoint
virtual bool DidSetNotificationBreakpoint()=0
lldb_private::DynamicLoaderDarwin::DidAttach
void DidAttach() override
Called after attaching a process.
Definition: DynamicLoaderDarwin.cpp:69
FileSpec.h
lldb_private::DynamicLoaderDarwin::~DynamicLoaderDarwin
~DynamicLoaderDarwin() override
lldb_private::DynamicLoaderDarwin::ImageInfo::GetArchitecture
lldb_private::ArchSpec GetArchitecture() const
Definition: DynamicLoaderDarwin.cpp:770
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::Log
Definition: Log.h:49
lldb_private::DynamicLoaderDarwin::ThreadIDToTLSMap
std::map< lldb::user_id_t, PthreadKeyToTLSMap > ThreadIDToTLSMap
Definition: DynamicLoaderDarwin.h:192
lldb_private::DynamicLoaderDarwin::m_libpthread_module_wp
lldb::ModuleWP m_libpthread_module_wp
Definition: DynamicLoaderDarwin.h:228
lldb_private::DynamicLoaderDarwin::ImageInfo::min_version_os_sdk
std::string min_version_os_sdk
LC_VERSION_MIN_... SDK.
Definition: DynamicLoaderDarwin.h:121
DynamicLoader.h
lldb_private::DynamicLoaderDarwin::Segment::Segment
Segment()
Definition: DynamicLoaderDarwin.h:76
lldb_private::DynamicLoaderDarwin::DoInitialImageFetch
virtual void DoInitialImageFetch()=0
lldb_private::DynamicLoaderDarwin::Segment::PutToLog
void PutToLog(lldb_private::Log *log, lldb::addr_t slide) const
Definition: DynamicLoaderDarwin.cpp:755
lldb_private::DynamicLoaderDarwin::ImageInfo::os_type
llvm::Triple::OSType os_type
LC_VERSION_MIN_... load command os type.
Definition: DynamicLoaderDarwin.h:116
lldb_private::DynamicLoaderDarwin::Segment::maxprot
uint32_t maxprot
Definition: DynamicLoaderDarwin.h:83