1 //===-- DynamicLoaderMacOS.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 // This is the DynamicLoader plugin for Darwin (macOS / iPhoneOS / tvOS /
11 // watchOS / BridgeOS)
12 // platforms late 2016 and newer, where lldb will call dyld SPI functions to get
13 // information about shared libraries, information about the shared cache, and
14 // the _dyld_debugger_notification function we put a breakpoint on give us an
15 // array of load addresses for solibs loaded and unloaded.  The SPI will tell us
16 // about both dyld and the executable, in addition to all of the usual solibs.
17 
18 #ifndef liblldb_DynamicLoaderMacOS_h_
19 #define liblldb_DynamicLoaderMacOS_h_
20 
21 #include <mutex>
22 #include <vector>
23 
24 #include "lldb/Target/DynamicLoader.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Utility/FileSpec.h"
27 #include "lldb/Utility/StructuredData.h"
28 #include "lldb/Utility/UUID.h"
29 
30 #include "DynamicLoaderDarwin.h"
31 
32 class DynamicLoaderMacOS : public lldb_private::DynamicLoaderDarwin {
33 public:
34   DynamicLoaderMacOS(lldb_private::Process *process);
35 
36   virtual ~DynamicLoaderMacOS() override;
37 
38   //------------------------------------------------------------------
39   // Static Functions
40   //------------------------------------------------------------------
41   static void Initialize();
42 
43   static void Terminate();
44 
45   static lldb_private::ConstString GetPluginNameStatic();
46 
47   static const char *GetPluginDescriptionStatic();
48 
49   static lldb_private::DynamicLoader *
50   CreateInstance(lldb_private::Process *process, bool force);
51 
52   //------------------------------------------------------------------
53   /// Called after attaching a process.
54   ///
55   /// Allow DynamicLoader plug-ins to execute some code after
56   /// attaching to a process.
57   //------------------------------------------------------------------
58   bool ProcessDidExec() override;
59 
60   lldb_private::Status CanLoadImage() override;
61 
62   bool GetSharedCacheInformation(
63       lldb::addr_t &base_address, lldb_private::UUID &uuid,
64       lldb_private::LazyBool &using_shared_cache,
65       lldb_private::LazyBool &private_shared_cache) override;
66 
67   //------------------------------------------------------------------
68   // PluginInterface protocol
69   //------------------------------------------------------------------
70   lldb_private::ConstString GetPluginName() override;
71 
72   uint32_t GetPluginVersion() override;
73 
74 protected:
75   void PutToLog(lldb_private::Log *log) const;
76 
77   void DoInitialImageFetch() override;
78 
79   bool NeedToDoInitialImageFetch() override;
80 
81   bool DidSetNotificationBreakpoint() override;
82 
83   void AddBinaries(const std::vector<lldb::addr_t> &load_addresses);
84 
85   void DoClear() override;
86 
87   static bool
88   NotifyBreakpointHit(void *baton,
89                       lldb_private::StoppointCallbackContext *context,
90                       lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
91 
92   bool SetNotificationBreakpoint() override;
93 
94   void ClearNotificationBreakpoint() override;
95 
96   void UpdateImageInfosHeaderAndLoadCommands(ImageInfo::collection &image_infos,
97                                              uint32_t infos_count,
98                                              bool update_executable);
99 
100   lldb::addr_t
101   GetDyldLockVariableAddressFromModule(lldb_private::Module *module);
102 
103   uint32_t m_image_infos_stop_id; // The Stop ID the last time we
104                                   // loaded/unloaded images
105   lldb::user_id_t m_break_id;
106   mutable std::recursive_mutex m_mutex;
107   lldb::addr_t m_maybe_image_infos_address; // If dyld is still maintaining the
108                                             // all_image_infos address, store it
109                                             // here so we can use it to detect
110                                             // exec's when talking to
111                                             // debugservers that don't support
112                                             // the "reason:exec" annotation.
113 
114 private:
115   DISALLOW_COPY_AND_ASSIGN(DynamicLoaderMacOS);
116 };
117 
118 #endif // liblldb_DynamicLoaderMacOS_h_
119