LLDB  mainline
DynamicLoaderMacOSXDYLD.h
Go to the documentation of this file.
1 //===-- DynamicLoaderMacOSXDYLD.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 // This is the DynamicLoader plugin for Darwin (macOS / iPhoneOS / tvOS /
10 // watchOS / BridgeOS)
11 // platforms earlier than 2016, where lldb would read the "dyld_all_image_infos"
12 // dyld internal structure to understand where things were loaded and the
13 // solib loaded/unloaded notification function we put a breakpoint on gives us
14 // an array of (load address, mod time, file path) tuples.
15 //
16 // As of late 2016, the new DynamicLoaderMacOS plugin should be used, which uses
17 // dyld SPI functions to get the same information without reading internal dyld
18 // data structures.
19 
20 #ifndef LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERMACOSXDYLD_H
21 #define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERMACOSXDYLD_H
22 
23 #include <mutex>
24 #include <vector>
25 
26 #include "lldb/Host/SafeMachO.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Utility/FileSpec.h"
31 #include "lldb/Utility/UUID.h"
32 
33 #include "DynamicLoaderDarwin.h"
34 
36 public:
38 
39  ~DynamicLoaderMacOSXDYLD() override;
40 
41  // Static Functions
42  static void Initialize();
43 
44  static void Terminate();
45 
47 
48  static const char *GetPluginDescriptionStatic();
49 
51  CreateInstance(lldb_private::Process *process, bool force);
52 
53  /// Called after attaching a process.
54  ///
55  /// Allow DynamicLoader plug-ins to execute some code after
56  /// attaching to a process.
57  bool ProcessDidExec() override;
58 
60 
62  lldb::addr_t &base_address, lldb_private::UUID &uuid,
63  lldb_private::LazyBool &using_shared_cache,
64  lldb_private::LazyBool &private_shared_cache) override;
65 
66  // PluginInterface protocol
68 
69  uint32_t GetPluginVersion() override;
70 
71  bool IsFullyInitialized() override;
72 
73 protected:
74  void PutToLog(lldb_private::Log *log) const;
75 
76  void DoInitialImageFetch() override;
77 
78  bool NeedToDoInitialImageFetch() override;
79 
80  bool DidSetNotificationBreakpoint() override;
81 
82  void DoClear() override;
83 
85 
86  static bool
87  NotifyBreakpointHit(void *baton,
89  lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
90 
92 
93  bool ReadMachHeader(lldb::addr_t addr, llvm::MachO::mach_header *header,
94  lldb_private::DataExtractor *load_command_data);
95 
97  ImageInfo &dylib_info,
98  lldb_private::FileSpec *lc_id_dylinker);
99 
102  uint32_t dylib_info_count; // Version >= 1
103  lldb::addr_t dylib_info_addr; // Version >= 1
104  lldb::addr_t notification; // Version >= 1
105  bool processDetachedFromSharedRegion; // Version >= 1
106  bool libSystemInitialized; // Version >= 2
108 
110  : version(0), dylib_info_count(0),
115 
116  void Clear() {
117  version = 0;
118  dylib_info_count = 0;
122  libSystemInitialized = false;
124  }
125 
126  bool IsValid() const { return version >= 1 && version <= 6; }
127  };
128 
130 
131  bool SetNotificationBreakpoint() override;
132 
133  void ClearNotificationBreakpoint() override;
134 
135  // There is a little tricky bit where you might initially attach while dyld is
136  // updating
137  // the all_image_infos, and you can't read the infos, so you have to continue
138  // and pick it
139  // up when you hit the update breakpoint. At that point, you need to run this
140  // initialize
141  // function, but when you do it that way you DON'T need to do the extra work
142  // you would at
143  // the breakpoint.
144  // So this function will only do actual work if the image infos haven't been
145  // read yet.
146  // If it does do any work, then it will return true, and false otherwise.
147  // That way you can
148  // call it in the breakpoint action, and if it returns true you're done.
150 
152 
153  bool AddModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr,
154  uint32_t image_infos_count);
155 
157  uint32_t image_infos_count);
158 
160  uint32_t infos_count,
161  bool update_executable);
162 
163  bool ReadImageInfos(lldb::addr_t image_infos_addr, uint32_t image_infos_count,
164  ImageInfo::collection &image_infos);
165 
170  mutable std::recursive_mutex m_mutex;
172 
173 private:
176  operator=(const DynamicLoaderMacOSXDYLD &) = delete;
177 };
178 
179 #endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_MACOSX_DYLD_DYNAMICLOADERMACOSXDYLD_H
lldb_private::UUID
Definition: UUID.h:23
DynamicLoaderMacOSXDYLD::m_break_id
lldb::user_id_t m_break_id
Definition: DynamicLoaderMacOSXDYLD.h:169
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::IsValid
bool IsValid() const
Definition: DynamicLoaderMacOSXDYLD.h:126
DynamicLoaderMacOSXDYLD::ReadImageInfos
bool ReadImageInfos(lldb::addr_t image_infos_addr, uint32_t image_infos_count, ImageInfo::collection &image_infos)
Definition: DynamicLoaderMacOSXDYLD.cpp:654
lldb_private::StoppointCallbackContext
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
Definition: StoppointCallbackContext.h:26
DynamicLoaderMacOSXDYLD::ProcessDidExec
bool ProcessDidExec() override
Called after attaching a process.
Definition: DynamicLoaderMacOSXDYLD.cpp:110
StructuredData.h
UUID.h
lldb_private::DynamicLoaderDarwin::ImageInfo::collection
std::vector< ImageInfo > collection
Definition: DynamicLoaderDarwin.h:170
DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfosAddress
bool AddModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr, uint32_t image_infos_count)
Definition: DynamicLoaderMacOSXDYLD.cpp:529
lldb_private::Process
Definition: Process.h:342
DynamicLoaderDarwin.h
DynamicLoaderMacOSXDYLD::UpdateImageInfosHeaderAndLoadCommands
void UpdateImageInfosHeaderAndLoadCommands(ImageInfo::collection &image_infos, uint32_t infos_count, bool update_executable)
Definition: DynamicLoaderMacOSXDYLD.cpp:912
DynamicLoaderMacOSXDYLD::DynamicLoaderMacOSXDYLD
DynamicLoaderMacOSXDYLD(lldb_private::Process *process)
Definition: DynamicLoaderMacOSXDYLD.cpp:97
DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic
static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic)
Definition: DynamicLoaderMacOSXDYLD.cpp:1169
DynamicLoaderMacOSXDYLD::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: DynamicLoaderMacOSXDYLD.cpp:1134
DynamicLoaderMacOSXDYLD::PutToLog
void PutToLog(lldb_private::Log *log) const
Definition: DynamicLoaderMacOSXDYLD.cpp:970
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::LazyBool
LazyBool
Definition: lldb-private-enumerations.h:115
lldb_private::DynamicLoaderDarwin::ImageInfo
Definition: DynamicLoaderDarwin.h:97
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::notification
lldb::addr_t notification
Definition: DynamicLoaderMacOSXDYLD.h:104
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::dylib_info_addr
lldb::addr_t dylib_info_addr
Definition: DynamicLoaderMacOSXDYLD.h:103
Process.h
DynamicLoaderMacOSXDYLD::NotifyBreakpointHit
static bool NotifyBreakpointHit(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
Definition: DynamicLoaderMacOSXDYLD.cpp:308
SafeMachO.h
DynamicLoaderMacOSXDYLD::operator=
const DynamicLoaderMacOSXDYLD & operator=(const DynamicLoaderMacOSXDYLD &)=delete
DynamicLoaderMacOSXDYLD::m_mutex
std::recursive_mutex m_mutex
Definition: DynamicLoaderMacOSXDYLD.h:170
lldb_private::FileSpec
Definition: FileSpec.h:56
DynamicLoaderMacOSXDYLD::InitializeFromAllImageInfos
bool InitializeFromAllImageInfos()
Definition: DynamicLoaderMacOSXDYLD.cpp:696
DynamicLoaderMacOSXDYLD::m_dyld_all_image_infos_addr
lldb::addr_t m_dyld_all_image_infos_addr
Definition: DynamicLoaderMacOSXDYLD.h:166
DynamicLoaderMacOSXDYLD::DoInitialImageFetch
void DoInitialImageFetch() override
Definition: DynamicLoaderMacOSXDYLD.cpp:184
DynamicLoaderMacOSXDYLD::ReadAllImageInfosStructure
bool ReadAllImageInfosStructure()
Definition: DynamicLoaderMacOSXDYLD.cpp:408
lldb_private::DataExtractor
Definition: DataExtractor.h:48
DynamicLoaderMacOSXDYLD::ParseLoadCommands
uint32_t ParseLoadCommands(const lldb_private::DataExtractor &data, ImageInfo &dylib_info, lldb_private::FileSpec *lc_id_dylinker)
Definition: DynamicLoaderMacOSXDYLD.cpp:823
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::Clear
void Clear()
Definition: DynamicLoaderMacOSXDYLD.h:116
DynamicLoaderMacOSXDYLD::GetSharedCacheInformation
bool GetSharedCacheInformation(lldb::addr_t &base_address, lldb_private::UUID &uuid, lldb_private::LazyBool &using_shared_cache, lldb_private::LazyBool &private_shared_cache) override
Get information about the shared cache for a process, if possible.
Definition: DynamicLoaderMacOSXDYLD.cpp:1045
lldb_private::ConstString
Definition: ConstString.h:40
DynamicLoaderMacOSXDYLD::~DynamicLoaderMacOSXDYLD
~DynamicLoaderMacOSXDYLD() override
Definition: DynamicLoaderMacOSXDYLD.cpp:105
lldb_private::DynamicLoader
Definition: DynamicLoader.h:52
DynamicLoaderMacOSXDYLD::m_dyld_all_image_infos_stop_id
uint32_t m_dyld_all_image_infos_stop_id
Definition: DynamicLoaderMacOSXDYLD.h:168
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos
Definition: DynamicLoaderMacOSXDYLD.h:100
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::processDetachedFromSharedRegion
bool processDetachedFromSharedRegion
Definition: DynamicLoaderMacOSXDYLD.h:105
lldb_private::DynamicLoaderDarwin
Definition: DynamicLoaderDarwin.h:27
DynamicLoaderMacOSXDYLD::m_process_image_addr_is_all_images_infos
bool m_process_image_addr_is_all_images_infos
Definition: DynamicLoaderMacOSXDYLD.h:171
DynamicLoaderMacOSXDYLD::m_dyld_all_image_infos
DYLDAllImageInfos m_dyld_all_image_infos
Definition: DynamicLoaderMacOSXDYLD.h:167
DynamicLoaderMacOSXDYLD::NeedToDoInitialImageFetch
bool NeedToDoInitialImageFetch() override
Definition: DynamicLoaderMacOSXDYLD.cpp:300
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::dylib_info_count
uint32_t dylib_info_count
Definition: DynamicLoaderMacOSXDYLD.h:102
lldb_private::Status
Definition: Status.h:44
uint32_t
DynamicLoaderMacOSXDYLD::GetPluginVersion
uint32_t GetPluginVersion() override
Definition: DynamicLoaderMacOSXDYLD.cpp:1149
DynamicLoaderMacOSXDYLD::AddrByteSize
uint32_t AddrByteSize()
Definition: DynamicLoaderMacOSXDYLD.cpp:1151
DynamicLoaderMacOSXDYLD::CreateInstance
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
Definition: DynamicLoaderMacOSXDYLD.cpp:55
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::libSystemInitialized
bool libSystemInitialized
Definition: DynamicLoaderMacOSXDYLD.h:106
DynamicLoaderMacOSXDYLD::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: DynamicLoaderMacOSXDYLD.cpp:1139
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
DynamicLoaderMacOSXDYLD::ClearNotificationBreakpoint
void ClearNotificationBreakpoint() override
Definition: DynamicLoaderMacOSXDYLD.cpp:174
DynamicLoaderMacOSXDYLD::ReadDYLDInfoFromMemoryAndSetNotificationCallback
bool ReadDYLDInfoFromMemoryAndSetNotificationCallback(lldb::addr_t addr)
Definition: DynamicLoaderMacOSXDYLD.cpp:249
lldb::user_id_t
uint64_t user_id_t
Definition: lldb-types.h:84
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::dyldImageLoadAddress
lldb::addr_t dyldImageLoadAddress
Definition: DynamicLoaderMacOSXDYLD.h:107
DynamicLoaderMacOSXDYLD::ReadMachHeader
bool ReadMachHeader(lldb::addr_t addr, llvm::MachO::mach_header *header, lldb_private::DataExtractor *load_command_data)
Definition: DynamicLoaderMacOSXDYLD.cpp:756
FileSpec.h
lldb_private::Log
Definition: Log.h:49
DynamicLoaderMacOSXDYLD::IsFullyInitialized
bool IsFullyInitialized() override
Return whether the dynamic loader is fully initialized and it's safe to call its APIs.
Definition: DynamicLoaderMacOSXDYLD.cpp:1117
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::version
uint32_t version
Definition: DynamicLoaderMacOSXDYLD.h:101
DynamicLoaderMacOSXDYLD::DidSetNotificationBreakpoint
bool DidSetNotificationBreakpoint() override
Definition: DynamicLoaderMacOSXDYLD.cpp:170
DynamicLoader.h
DynamicLoaderMacOSXDYLD::Terminate
static void Terminate()
Definition: DynamicLoaderMacOSXDYLD.cpp:1129
DynamicLoaderMacOSXDYLD::GetPluginName
lldb_private::ConstString GetPluginName() override
Definition: DynamicLoaderMacOSXDYLD.cpp:1145
DynamicLoaderMacOSXDYLD::Initialize
static void Initialize()
Definition: DynamicLoaderMacOSXDYLD.cpp:1123
DynamicLoaderMacOSXDYLD::RemoveModulesUsingImageInfosAddress
bool RemoveModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr, uint32_t image_infos_count)
Definition: DynamicLoaderMacOSXDYLD.cpp:570
DynamicLoaderMacOSXDYLD::DoClear
void DoClear() override
Definition: DynamicLoaderMacOSXDYLD.cpp:158
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138
DynamicLoaderMacOSXDYLD::SetNotificationBreakpoint
bool SetNotificationBreakpoint() override
Definition: DynamicLoaderMacOSXDYLD.cpp:992
DynamicLoaderMacOSXDYLD
Definition: DynamicLoaderMacOSXDYLD.h:35
DynamicLoaderMacOSXDYLD::CanLoadImage
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
Definition: DynamicLoaderMacOSXDYLD.cpp:1027
DynamicLoaderMacOSXDYLD::DYLDAllImageInfos::DYLDAllImageInfos
DYLDAllImageInfos()
Definition: DynamicLoaderMacOSXDYLD.h:109