DynamicLoader.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //===-- DynamicLoader.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. #ifndef LLDB_TARGET_DYNAMICLOADER_H
  9. #define LLDB_TARGET_DYNAMICLOADER_H
  10. #include "lldb/Core/PluginInterface.h"
  11. #include "lldb/Utility/FileSpec.h"
  12. #include "lldb/Utility/Status.h"
  13. #include "lldb/Utility/UUID.h"
  14. #include "lldb/lldb-defines.h"
  15. #include "lldb/lldb-forward.h"
  16. #include "lldb/lldb-private-enumerations.h"
  17. #include "lldb/lldb-types.h"
  18. #include <cstddef>
  19. #include <cstdint>
  20. namespace lldb_private {
  21. class ModuleList;
  22. class Process;
  23. class SectionList;
  24. class Symbol;
  25. class SymbolContext;
  26. class SymbolContextList;
  27. class Thread;
  28. }
  29. namespace lldb_private {
  30. /// \class DynamicLoader DynamicLoader.h "lldb/Target/DynamicLoader.h"
  31. /// A plug-in interface definition class for dynamic loaders.
  32. ///
  33. /// Dynamic loader plug-ins track image (shared library) loading and
  34. /// unloading. The class is initialized given a live process that is halted at
  35. /// its entry point or just after attaching.
  36. ///
  37. /// Dynamic loader plug-ins can track the process by registering callbacks
  38. /// using the: Process::RegisterNotificationCallbacks (const Notifications&)
  39. /// function.
  40. ///
  41. /// Breakpoints can also be set in the process which can register functions
  42. /// that get called using: Process::BreakpointSetCallback (lldb::user_id_t,
  43. /// BreakpointHitCallback, void *). These breakpoint callbacks return a
  44. /// boolean value that indicates if the process should continue or halt and
  45. /// should return the global setting for this using:
  46. /// DynamicLoader::StopWhenImagesChange() const.
  47. class DynamicLoader : public PluginInterface {
  48. public:
  49. /// Find a dynamic loader plugin for a given process.
  50. ///
  51. /// Scans the installed DynamicLoader plug-ins and tries to find an instance
  52. /// that can be used to track image changes in \a process.
  53. ///
  54. /// \param[in] process
  55. /// The process for which to try and locate a dynamic loader
  56. /// plug-in instance.
  57. ///
  58. /// \param[in] plugin_name
  59. /// An optional name of a specific dynamic loader plug-in that
  60. /// should be used. If NULL, pick the best plug-in.
  61. static DynamicLoader *FindPlugin(Process *process, const char *plugin_name);
  62. /// Construct with a process.
  63. DynamicLoader(Process *process);
  64. /// Called after attaching a process.
  65. ///
  66. /// Allow DynamicLoader plug-ins to execute some code after attaching to a
  67. /// process.
  68. virtual void DidAttach() = 0;
  69. /// Called after launching a process.
  70. ///
  71. /// Allow DynamicLoader plug-ins to execute some code after the process has
  72. /// stopped for the first time on launch.
  73. virtual void DidLaunch() = 0;
  74. /// Helper function that can be used to detect when a process has called
  75. /// exec and is now a new and different process. This can be called when
  76. /// necessary to try and detect the exec. The process might be able to
  77. /// answer this question, but sometimes it might not be able and the dynamic
  78. /// loader often knows what the program entry point is. So the process and
  79. /// the dynamic loader can work together to detect this.
  80. virtual bool ProcessDidExec() { return false; }
  81. /// Get whether the process should stop when images change.
  82. ///
  83. /// When images (executables and shared libraries) get loaded or unloaded,
  84. /// often debug sessions will want to try and resolve or unresolve
  85. /// breakpoints that are set in these images. Any breakpoints set by
  86. /// DynamicLoader plug-in instances should return this value to ensure
  87. /// consistent debug session behaviour.
  88. ///
  89. /// \return
  90. /// Returns \b true if the process should stop when images
  91. /// change, \b false if the process should resume.
  92. bool GetStopWhenImagesChange() const;
  93. /// Set whether the process should stop when images change.
  94. ///
  95. /// When images (executables and shared libraries) get loaded or unloaded,
  96. /// often debug sessions will want to try and resolve or unresolve
  97. /// breakpoints that are set in these images. The default is set so that the
  98. /// process stops when images change, but this can be overridden using this
  99. /// function callback.
  100. ///
  101. /// \param[in] stop
  102. /// Boolean value that indicates whether the process should stop
  103. /// when images change.
  104. void SetStopWhenImagesChange(bool stop);
  105. /// Provides a plan to step through the dynamic loader trampoline for the
  106. /// current state of \a thread.
  107. ///
  108. ///
  109. /// \param[in] stop_others
  110. /// Whether the plan should be set to stop other threads.
  111. ///
  112. /// \return
  113. /// A pointer to the plan (caller owned) or NULL if we are not at such
  114. /// a trampoline.
  115. virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread,
  116. bool stop_others) = 0;
  117. /// Some dynamic loaders provide features where there are a group of symbols
  118. /// "equivalent to" a given symbol one of which will be chosen when the
  119. /// symbol is bound. If you want to set a breakpoint on one of these
  120. /// symbols, you really need to set it on all the equivalent symbols.
  121. ///
  122. ///
  123. /// \param[in] original_symbol
  124. /// The symbol for which we are finding equivalences.
  125. ///
  126. /// \param[in] module_list
  127. /// The set of modules in which to search.
  128. ///
  129. /// \param[out] equivalent_symbols
  130. /// The equivalent symbol list - any equivalent symbols found are appended
  131. /// to this list.
  132. ///
  133. virtual void FindEquivalentSymbols(Symbol *original_symbol,
  134. ModuleList &module_list,
  135. SymbolContextList &equivalent_symbols) {}
  136. /// Ask if it is ok to try and load or unload an shared library (image).
  137. ///
  138. /// The dynamic loader often knows when it would be ok to try and load or
  139. /// unload a shared library. This function call allows the dynamic loader
  140. /// plug-ins to check any current dyld state to make sure it is an ok time
  141. /// to load a shared library.
  142. ///
  143. /// \return
  144. /// \b true if it is currently ok to try and load a shared
  145. /// library into the process, \b false otherwise.
  146. virtual Status CanLoadImage() = 0;
  147. /// Ask if the eh_frame information for the given SymbolContext should be
  148. /// relied on even when it's the first frame in a stack unwind.
  149. ///
  150. /// The CFI instructions from the eh_frame section are normally only valid
  151. /// at call sites -- places where a program could throw an exception and
  152. /// need to unwind out. But some Modules may be known to the system as
  153. /// having reliable eh_frame information at all call sites. This would be
  154. /// the case if the Module's contents are largely hand-written assembly with
  155. /// hand-written eh_frame information. Normally when unwinding from a
  156. /// function at the beginning of a stack unwind lldb will examine the
  157. /// assembly instructions to understand how the stack frame is set up and
  158. /// where saved registers are stored. But with hand-written assembly this is
  159. /// not reliable enough -- we need to consult those function's hand-written
  160. /// eh_frame information.
  161. ///
  162. /// \return
  163. /// \b True if the symbol context should use eh_frame instructions
  164. /// unconditionally when unwinding from this frame. Else \b false,
  165. /// the normal lldb unwind behavior of only using eh_frame when the
  166. /// function appears in the middle of the stack.
  167. virtual bool AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) {
  168. return false;
  169. }
  170. /// Retrieves the per-module TLS block for a given thread.
  171. ///
  172. /// \param[in] module
  173. /// The module to query TLS data for.
  174. ///
  175. /// \param[in] thread
  176. /// The specific thread to query TLS data for.
  177. ///
  178. /// \return
  179. /// If the given thread has TLS data allocated for the
  180. /// module, the address of the TLS block. Otherwise
  181. /// LLDB_INVALID_ADDRESS is returned.
  182. virtual lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module,
  183. const lldb::ThreadSP thread,
  184. lldb::addr_t tls_file_addr) {
  185. return LLDB_INVALID_ADDRESS;
  186. }
  187. /// Locates or creates a module given by \p file and updates/loads the
  188. /// resulting module at the virtual base address \p base_addr.
  189. virtual lldb::ModuleSP LoadModuleAtAddress(const lldb_private::FileSpec &file,
  190. lldb::addr_t link_map_addr,
  191. lldb::addr_t base_addr,
  192. bool base_addr_is_offset);
  193. /// Get information about the shared cache for a process, if possible.
  194. ///
  195. /// On some systems (e.g. Darwin based systems), a set of libraries that are
  196. /// common to most processes may be put in a single region of memory and
  197. /// mapped into every process, this is called the shared cache, as a
  198. /// performance optimization.
  199. ///
  200. /// Many targets will not have the concept of a shared cache.
  201. ///
  202. /// Depending on how the DynamicLoader gathers information about the shared
  203. /// cache, it may be able to only return basic information - like the UUID
  204. /// of the cache - or it may be able to return additional information about
  205. /// the cache.
  206. ///
  207. /// \param[out] base_address
  208. /// The base address (load address) of the shared cache.
  209. /// LLDB_INVALID_ADDRESS if it cannot be determined.
  210. ///
  211. /// \param[out] uuid
  212. /// The UUID of the shared cache, if it can be determined.
  213. /// If the UUID cannot be fetched, IsValid() will be false.
  214. ///
  215. /// \param[out] using_shared_cache
  216. /// If this process is using a shared cache.
  217. /// If unknown, eLazyBoolCalculate is returned.
  218. ///
  219. /// \param[out] private_shared_cache
  220. /// A LazyBool indicating whether this process is using a
  221. /// private shared cache.
  222. /// If this information cannot be fetched, eLazyBoolCalculate.
  223. ///
  224. /// \return
  225. /// Returns false if this DynamicLoader cannot gather information
  226. /// about the shared cache / has no concept of a shared cache.
  227. virtual bool GetSharedCacheInformation(lldb::addr_t &base_address, UUID &uuid,
  228. LazyBool &using_shared_cache,
  229. LazyBool &private_shared_cache) {
  230. base_address = LLDB_INVALID_ADDRESS;
  231. uuid.Clear();
  232. using_shared_cache = eLazyBoolCalculate;
  233. private_shared_cache = eLazyBoolCalculate;
  234. return false;
  235. }
  236. /// Return whether the dynamic loader is fully initialized and it's safe to
  237. /// call its APIs.
  238. ///
  239. /// On some systems (e.g. Darwin based systems), lldb will get notified by
  240. /// the dynamic loader before it itself finished initializing and it's not
  241. /// safe to call certain APIs or SPIs.
  242. virtual bool IsFullyInitialized() { return true; }
  243. protected:
  244. // Utility methods for derived classes
  245. /// Checks to see if the target module has changed, updates the target
  246. /// accordingly and returns the target executable module.
  247. lldb::ModuleSP GetTargetExecutable();
  248. /// Updates the load address of every allocatable section in \p module.
  249. ///
  250. /// \param module The module to traverse.
  251. ///
  252. /// \param link_map_addr The virtual address of the link map for the @p
  253. /// module.
  254. ///
  255. /// \param base_addr The virtual base address \p module is loaded at.
  256. virtual void UpdateLoadedSections(lldb::ModuleSP module,
  257. lldb::addr_t link_map_addr,
  258. lldb::addr_t base_addr,
  259. bool base_addr_is_offset);
  260. // Utility method so base classes can share implementation of
  261. // UpdateLoadedSections
  262. void UpdateLoadedSectionsCommon(lldb::ModuleSP module, lldb::addr_t base_addr,
  263. bool base_addr_is_offset);
  264. /// Removes the loaded sections from the target in \p module.
  265. ///
  266. /// \param module The module to traverse.
  267. virtual void UnloadSections(const lldb::ModuleSP module);
  268. // Utility method so base classes can share implementation of UnloadSections
  269. void UnloadSectionsCommon(const lldb::ModuleSP module);
  270. const lldb_private::SectionList *
  271. GetSectionListFromModule(const lldb::ModuleSP module) const;
  272. // Read an unsigned int of the given size from memory at the given addr.
  273. // Return -1 if the read fails, otherwise return the result as an int64_t.
  274. int64_t ReadUnsignedIntWithSizeInBytes(lldb::addr_t addr, int size_in_bytes);
  275. // Read a pointer from memory at the given addr. Return LLDB_INVALID_ADDRESS
  276. // if the read fails.
  277. lldb::addr_t ReadPointer(lldb::addr_t addr);
  278. // Calls into the Process protected method LoadOperatingSystemPlugin:
  279. void LoadOperatingSystemPlugin(bool flush);
  280. // Member variables.
  281. Process
  282. *m_process; ///< The process that this dynamic loader plug-in is tracking.
  283. };
  284. } // namespace lldb_private
  285. #endif // LLDB_TARGET_DYNAMICLOADER_H