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"
21#include "lldb/Utility/UUID.h"
22
23#include "llvm/TargetParser/Triple.h"
24
25namespace lldb_private {
26
28public:
30
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
42 bool stop_others) override;
43
45 const lldb_private::Symbol *original_symbol,
46 lldb_private::ModuleList &module_list,
47 lldb_private::SymbolContextList &equivalent_symbols) override;
48
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 std::optional<lldb_private::Address> GetStartAddress() override;
60
61protected:
63
65 lldb::StateType state);
66
67 void Clear(bool clear_process);
68
69 // Clear method for classes derived from this one
70 virtual void DoClear() = 0;
71
72 void SetDYLDModule(lldb::ModuleSP &dyld_module_sp);
73
75
76 void ClearDYLDModule();
77
78 class Segment {
79 public:
80 Segment() : name() {}
81
87 uint32_t maxprot = 0;
88 uint32_t initprot = 0;
89 uint32_t nsects = 0;
90 uint32_t flags = 0;
91
92 bool operator==(const Segment &rhs) const {
93 return name == rhs.name && vmaddr == rhs.vmaddr && vmsize == rhs.vmsize;
94 }
95
96 void PutToLog(lldb_private::Log *log, lldb::addr_t slide) const;
97 };
98
99 struct ImageInfo {
100 /// Address of mach header for this dylib.
102 /// The amount to slide all segments by if there is a global
103 /// slide.
105 /// Resolved path for this dylib.
107 /// UUID for this dylib if it has one, else all zeros.
109 /// The mach header for this image.
110 llvm::MachO::mach_header header;
111 /// All segment vmaddr and vmsize pairs for this executable (from
112 /// memory of inferior).
113 std::vector<Segment> segments;
114 /// The process stop ID that the sections for this image were
115 /// loaded.
116 uint32_t load_stop_id = 0;
117 /// LC_VERSION_MIN_... load command os type.
118 llvm::Triple::OSType os_type = llvm::Triple::OSType::UnknownOS;
119 /// LC_VERSION_MIN_... load command os environment.
120 llvm::Triple::EnvironmentType os_env =
121 llvm::Triple::EnvironmentType::UnknownEnvironment;
122 /// LC_VERSION_MIN_... SDK.
124 /// When we need to read a binary's mach header and load commands
125 /// out of memory, this specifies how much to read to get
126 /// everything in one read packet, if known. Increase the
127 /// default 512 bytes to 8192 which is enough to include most
128 /// mach header + load commands.
129 uint32_t mh_and_load_cmd_size = 8192;
130
131 ImageInfo() = default;
132
133 void Clear(bool load_cmd_data_only) {
134 if (!load_cmd_data_only) {
136 slide = 0;
137 file_spec.Clear();
138 ::memset(&header, 0, sizeof(header));
139 }
140 uuid.Clear();
141 segments.clear();
142 load_stop_id = 0;
143 os_type = llvm::Triple::OSType::UnknownOS;
144 os_env = llvm::Triple::EnvironmentType::UnknownEnvironment;
145 min_version_os_sdk.clear();
147 }
148
149 bool operator==(const ImageInfo &rhs) const {
150 return address == rhs.address && slide == rhs.slide &&
151 file_spec == rhs.file_spec && uuid == rhs.uuid &&
152 memcmp(&header, &rhs.header, sizeof(header)) == 0 &&
153 segments == rhs.segments && os_type == rhs.os_type &&
154 os_env == rhs.os_env &&
156 }
157
158 bool UUIDValid() const { return uuid.IsValid(); }
159
161 if (header.cputype) {
162 if (header.cputype & llvm::MachO::CPU_ARCH_ABI64)
163 return 8;
164 else
165 return 4;
166 }
167 return 0;
168 }
169
171
173
174 void PutToLog(lldb_private::Log *log) const;
175
176 typedef std::vector<ImageInfo> collection;
177 typedef collection::iterator iterator;
178 typedef collection::const_iterator const_iterator;
179 };
180
182
184
186 bool can_create,
187 bool *did_create_ptr);
188
189 void UnloadImages(const std::vector<lldb::addr_t> &solib_addresses);
190
191 void UnloadAllImages();
192
193 virtual bool SetNotificationBreakpoint() = 0;
194
195 virtual void ClearNotificationBreakpoint() = 0;
196
198
199 typedef std::map<uint64_t, lldb::addr_t> PthreadKeyToTLSMap;
200 typedef std::map<lldb::user_id_t, PthreadKeyToTLSMap> ThreadIDToTLSMap;
201
202 std::recursive_mutex &GetMutex() const { return m_mutex; }
203
205
207
210 ImageInfo::collection &image_infos);
211
212 // Finds/loads modules for a given `image_infos` and returns pairs
213 // (ImageInfo, ModuleSP).
214 // Prefer using this method rather than calling `FindTargetModuleForImageInfo`
215 // directly as this method may load the modules in parallel.
216 std::vector<std::pair<ImageInfo, lldb::ModuleSP>>
218
219 // If `images` contains / may contain dyld or executable image, call this
220 // method to keep our internal record keeping of the special binaries
221 // up-to-date.
223 std::vector<std::pair<ImageInfo, lldb::ModuleSP>> &images);
224
225 // if image_info is a dyld binary, call this method
226 bool UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info);
227
228 // If image_infos contains / may contain executable image, call this method
229 // to keep our internal record keeping of the special dyld binary up-to-date.
231
234 std::vector<std::pair<ImageInfo, lldb::ModuleSP>> &images);
235
236 // Whether we should use the new dyld SPI to get shared library information,
237 // or read
238 // it directly out of the dyld_all_image_infos. Whether we use the (newer)
239 // DynamicLoaderMacOS
240 // plugin or the (older) DynamicLoaderMacOSX plugin.
241 static bool UseDYLDSPI(lldb_private::Process *process);
242
243 lldb::ModuleWP m_dyld_module_wp; // the dyld whose file type (mac, ios, etc)
244 // matches the process
249 m_dyld_image_infos; // Current shared libraries information
250 uint32_t m_dyld_image_infos_stop_id; // The process stop ID that
251 // "m_dyld_image_infos" is valid for
253 mutable std::recursive_mutex m_mutex;
254
255private:
258};
259
260} // namespace lldb_private
261
262#endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERDARWIN_H
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:32
A uniqued constant string class.
Definition ConstString.h:40
bool operator==(const Segment &rhs) const
void PutToLog(lldb_private::Log *log, lldb::addr_t slide) const
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.
bool UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info)
const DynamicLoaderDarwin & operator=(const DynamicLoaderDarwin &)=delete
std::map< lldb::user_id_t, PthreadKeyToTLSMap > ThreadIDToTLSMap
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...
virtual bool NeedToDoInitialImageFetch()=0
DynamicLoaderDarwin(const DynamicLoaderDarwin &)=delete
virtual void DoInitialImageFetch()=0
void AddExecutableModuleIfInImageInfos(ImageInfo::collection &image_infos)
std::map< uint64_t, lldb::addr_t > PthreadKeyToTLSMap
DynamicLoaderDarwin(lldb_private::Process *process)
std::recursive_mutex & GetMutex() const
void PrivateProcessStateChanged(lldb_private::Process *process, lldb::StateType state)
void DidLaunch() override
Called after attaching a process.
lldb::ModuleSP FindTargetModuleForImageInfo(const ImageInfo &image_info, bool can_create, bool *did_create_ptr)
virtual bool SetNotificationBreakpoint()=0
bool AddModulesUsingImageInfos(ImageInfo::collection &image_infos)
void FindEquivalentSymbols(const 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...
void DidAttach() override
Called after attaching a process.
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.
std::vector< std::pair< ImageInfo, lldb::ModuleSP > > PreloadModulesFromImageInfos(const ImageInfo::collection &image_infos)
bool JSONImageInformationIntoImageInfo(lldb_private::StructuredData::ObjectSP image_details, ImageInfo::collection &image_infos)
bool UpdateImageLoadAddress(lldb_private::Module *module, ImageInfo &info)
std::optional< lldb_private::Address > GetStartAddress() override
Return the start address in the dynamic loader module.
void UpdateSpecialBinariesFromPreloadedModules(std::vector< std::pair< ImageInfo, lldb::ModuleSP > > &images)
void PrivateInitialize(lldb_private::Process *process)
lldb_private::Address GetPthreadSetSpecificAddress()
virtual void ClearNotificationBreakpoint()=0
bool AddModulesUsingPreloadedModules(std::vector< std::pair< ImageInfo, lldb::ModuleSP > > &images)
void SetDYLDModule(lldb::ModuleSP &dyld_module_sp)
static bool UseDYLDSPI(lldb_private::Process *process)
lldb_private::Address m_pthread_getspecific_addr
bool UnloadModuleSections(lldb_private::Module *module, ImageInfo &info)
void UnloadImages(const std::vector< lldb::addr_t > &solib_addresses)
virtual bool DidSetNotificationBreakpoint()=0
A plug-in interface definition class for dynamic loaders.
A file utility class.
Definition FileSpec.h:57
A collection class for Module objects.
Definition ModuleList.h:125
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:91
A plug-in interface definition class for debugging a process.
Definition Process.h:357
std::shared_ptr< Object > ObjectSP
Defines a list of symbol context objects.
Defines a symbol context baton that can be handed other debug core functions.
Represents UUID's of various sizes.
Definition UUID.h:27
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::weak_ptr< lldb_private::Module > ModuleWP
std::shared_ptr< lldb_private::Thread > ThreadSP
StateType
Process and Thread States.
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
lldb_private::UUID uuid
UUID for this dylib if it has one, else all zeros.
lldb::addr_t address
Address of mach header for this dylib.
std::string min_version_os_sdk
LC_VERSION_MIN_... SDK.
lldb::addr_t slide
The amount to slide all segments by if there is a global slide.
bool operator==(const ImageInfo &rhs) const
llvm::MachO::mach_header header
The mach header for this image.
llvm::Triple::OSType os_type
LC_VERSION_MIN_... load command os type.
std::vector< Segment > segments
All segment vmaddr and vmsize pairs for this executable (from memory of inferior).
const Segment * FindSegment(lldb_private::ConstString name) const
lldb_private::FileSpec file_spec
Resolved path for this dylib.
uint32_t mh_and_load_cmd_size
When we need to read a binary's mach header and load commands out of memory, this specifies how much ...
llvm::Triple::EnvironmentType os_env
LC_VERSION_MIN_... load command os environment.
uint32_t load_stop_id
The process stop ID that the sections for this image were loaded.
void PutToLog(lldb_private::Log *log) const