SBProcess.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //===-- SBProcess.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_API_SBPROCESS_H
  9. #define LLDB_API_SBPROCESS_H
  10. #include "lldb/API/SBDefines.h"
  11. #include "lldb/API/SBError.h"
  12. #include "lldb/API/SBProcessInfo.h"
  13. #include "lldb/API/SBQueue.h"
  14. #include "lldb/API/SBTarget.h"
  15. #include <cstdio>
  16. namespace lldb {
  17. class SBEvent;
  18. class LLDB_API SBProcess {
  19. public:
  20. /// Broadcaster event bits definitions.
  21. FLAGS_ANONYMOUS_ENUM(){eBroadcastBitStateChanged = (1 << 0),
  22. eBroadcastBitInterrupt = (1 << 1),
  23. eBroadcastBitSTDOUT = (1 << 2),
  24. eBroadcastBitSTDERR = (1 << 3),
  25. eBroadcastBitProfileData = (1 << 4),
  26. eBroadcastBitStructuredData = (1 << 5)};
  27. SBProcess();
  28. SBProcess(const lldb::SBProcess &rhs);
  29. const lldb::SBProcess &operator=(const lldb::SBProcess &rhs);
  30. SBProcess(const lldb::ProcessSP &process_sp);
  31. ~SBProcess();
  32. static const char *GetBroadcasterClassName();
  33. const char *GetPluginName();
  34. // DEPRECATED: use GetPluginName()
  35. const char *GetShortPluginName();
  36. void Clear();
  37. explicit operator bool() const;
  38. bool IsValid() const;
  39. lldb::SBTarget GetTarget() const;
  40. lldb::ByteOrder GetByteOrder() const;
  41. size_t PutSTDIN(const char *src, size_t src_len);
  42. size_t GetSTDOUT(char *dst, size_t dst_len) const;
  43. size_t GetSTDERR(char *dst, size_t dst_len) const;
  44. size_t GetAsyncProfileData(char *dst, size_t dst_len) const;
  45. void ReportEventState(const lldb::SBEvent &event, FILE *out) const;
  46. void ReportEventState(const lldb::SBEvent &event, SBFile file) const;
  47. void ReportEventState(const lldb::SBEvent &event, FileSP file) const;
  48. void AppendEventStateReport(const lldb::SBEvent &event,
  49. lldb::SBCommandReturnObject &result);
  50. /// Remote connection related functions. These will fail if the
  51. /// process is not in eStateConnected. They are intended for use
  52. /// when connecting to an externally managed debugserver instance.
  53. bool RemoteAttachToProcessWithID(lldb::pid_t pid, lldb::SBError &error);
  54. bool RemoteLaunch(char const **argv, char const **envp,
  55. const char *stdin_path, const char *stdout_path,
  56. const char *stderr_path, const char *working_directory,
  57. uint32_t launch_flags, bool stop_at_entry,
  58. lldb::SBError &error);
  59. // Thread related functions
  60. uint32_t GetNumThreads();
  61. lldb::SBThread GetThreadAtIndex(size_t index);
  62. lldb::SBThread GetThreadByID(lldb::tid_t sb_thread_id);
  63. lldb::SBThread GetThreadByIndexID(uint32_t index_id);
  64. lldb::SBThread GetSelectedThread() const;
  65. // Function for lazily creating a thread using the current OS plug-in. This
  66. // function will be removed in the future when there are APIs to create
  67. // SBThread objects through the interface and add them to the process through
  68. // the SBProcess API.
  69. lldb::SBThread CreateOSPluginThread(lldb::tid_t tid, lldb::addr_t context);
  70. bool SetSelectedThread(const lldb::SBThread &thread);
  71. bool SetSelectedThreadByID(lldb::tid_t tid);
  72. bool SetSelectedThreadByIndexID(uint32_t index_id);
  73. // Queue related functions
  74. uint32_t GetNumQueues();
  75. lldb::SBQueue GetQueueAtIndex(size_t index);
  76. // Stepping related functions
  77. lldb::StateType GetState();
  78. int GetExitStatus();
  79. const char *GetExitDescription();
  80. /// Gets the process ID
  81. ///
  82. /// Returns the process identifier for the process as it is known
  83. /// on the system on which the process is running. For unix systems
  84. /// this is typically the same as if you called "getpid()" in the
  85. /// process.
  86. ///
  87. /// \return
  88. /// Returns LLDB_INVALID_PROCESS_ID if this object does not
  89. /// contain a valid process object, or if the process has not
  90. /// been launched. Returns a valid process ID if the process is
  91. /// valid.
  92. lldb::pid_t GetProcessID();
  93. /// Gets the unique ID associated with this process object
  94. ///
  95. /// Unique IDs start at 1 and increment up with each new process
  96. /// instance. Since starting a process on a system might always
  97. /// create a process with the same process ID, there needs to be a
  98. /// way to tell two process instances apart.
  99. ///
  100. /// \return
  101. /// Returns a non-zero integer ID if this object contains a
  102. /// valid process object, zero if this object does not contain
  103. /// a valid process object.
  104. uint32_t GetUniqueID();
  105. uint32_t GetAddressByteSize() const;
  106. lldb::SBError Destroy();
  107. lldb::SBError Continue();
  108. lldb::SBError Stop();
  109. lldb::SBError Kill();
  110. lldb::SBError Detach();
  111. lldb::SBError Detach(bool keep_stopped);
  112. lldb::SBError Signal(int signal);
  113. lldb::SBUnixSignals GetUnixSignals();
  114. void SendAsyncInterrupt();
  115. uint32_t GetStopID(bool include_expression_stops = false);
  116. /// Gets the stop event corresponding to stop ID.
  117. //
  118. /// Note that it wasn't fully implemented and tracks only the stop
  119. /// event for the last natural stop ID.
  120. ///
  121. /// \param [in] stop_id
  122. /// The ID of the stop event to return.
  123. ///
  124. /// \return
  125. /// The stop event corresponding to stop ID.
  126. lldb::SBEvent GetStopEventForStopID(uint32_t stop_id);
  127. size_t ReadMemory(addr_t addr, void *buf, size_t size, lldb::SBError &error);
  128. size_t WriteMemory(addr_t addr, const void *buf, size_t size,
  129. lldb::SBError &error);
  130. size_t ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
  131. lldb::SBError &error);
  132. uint64_t ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
  133. lldb::SBError &error);
  134. lldb::addr_t ReadPointerFromMemory(addr_t addr, lldb::SBError &error);
  135. // Events
  136. static lldb::StateType GetStateFromEvent(const lldb::SBEvent &event);
  137. static bool GetRestartedFromEvent(const lldb::SBEvent &event);
  138. static size_t GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event);
  139. static const char *
  140. GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event, size_t idx);
  141. static lldb::SBProcess GetProcessFromEvent(const lldb::SBEvent &event);
  142. static bool GetInterruptedFromEvent(const lldb::SBEvent &event);
  143. static lldb::SBStructuredData
  144. GetStructuredDataFromEvent(const lldb::SBEvent &event);
  145. static bool EventIsProcessEvent(const lldb::SBEvent &event);
  146. static bool EventIsStructuredDataEvent(const lldb::SBEvent &event);
  147. lldb::SBBroadcaster GetBroadcaster() const;
  148. static const char *GetBroadcasterClass();
  149. bool GetDescription(lldb::SBStream &description);
  150. SBStructuredData GetExtendedCrashInformation();
  151. /// Start Tracing with the given SBTraceOptions.
  152. ///
  153. /// \param[in] options
  154. /// Class containing trace options like trace buffer size, meta
  155. /// data buffer size, TraceType and any custom parameters
  156. /// {formatted as a JSON Dictionary}. In case of errors in
  157. /// formatting, an error would be reported.
  158. /// It must be noted that tracing options such as buffer sizes
  159. /// or other custom parameters passed maybe invalid for some
  160. /// trace technologies. In such cases the trace implementations
  161. /// could choose to either throw an error or could round off to
  162. /// the nearest valid options to start tracing if the passed
  163. /// value is not supported. To obtain the actual used trace
  164. /// options please use the GetTraceConfig API. For the custom
  165. /// parameters, only the parameters recognized by the target
  166. /// would be used and others would be ignored.
  167. ///
  168. /// \param[out] error
  169. /// An error explaining what went wrong.
  170. ///
  171. /// \return
  172. /// A SBTrace instance, which should be used
  173. /// to get the trace data or other trace related operations.
  174. lldb::SBTrace StartTrace(SBTraceOptions &options, lldb::SBError &error);
  175. uint32_t GetNumSupportedHardwareWatchpoints(lldb::SBError &error) const;
  176. /// Load a shared library into this process.
  177. ///
  178. /// \param[in] remote_image_spec
  179. /// The path for the shared library on the target what you want
  180. /// to load.
  181. ///
  182. /// \param[out] error
  183. /// An error object that gets filled in with any errors that
  184. /// might occur when trying to load the shared library.
  185. ///
  186. /// \return
  187. /// A token that represents the shared library that can be
  188. /// later used to unload the shared library. A value of
  189. /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared
  190. /// library can't be opened.
  191. uint32_t LoadImage(lldb::SBFileSpec &remote_image_spec, lldb::SBError &error);
  192. /// Load a shared library into this process.
  193. ///
  194. /// \param[in] local_image_spec
  195. /// The file spec that points to the shared library that you
  196. /// want to load if the library is located on the host. The
  197. /// library will be copied over to the location specified by
  198. /// remote_image_spec or into the current working directory with
  199. /// the same filename if the remote_image_spec isn't specified.
  200. ///
  201. /// \param[in] remote_image_spec
  202. /// If local_image_spec is specified then the location where the
  203. /// library should be copied over from the host. If
  204. /// local_image_spec isn't specified, then the path for the
  205. /// shared library on the target what you want to load.
  206. ///
  207. /// \param[out] error
  208. /// An error object that gets filled in with any errors that
  209. /// might occur when trying to load the shared library.
  210. ///
  211. /// \return
  212. /// A token that represents the shared library that can be
  213. /// later used to unload the shared library. A value of
  214. /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared
  215. /// library can't be opened.
  216. uint32_t LoadImage(const lldb::SBFileSpec &local_image_spec,
  217. const lldb::SBFileSpec &remote_image_spec,
  218. lldb::SBError &error);
  219. /// Load a shared library into this process, starting with a
  220. /// library name and a list of paths, searching along the list of
  221. /// paths till you find a matching library.
  222. ///
  223. /// \param[in] image_spec
  224. /// The name of the shared library that you want to load.
  225. /// If image_spec is a relative path, the relative path will be
  226. /// appended to the search paths.
  227. /// If the image_spec is an absolute path, just the basename is used.
  228. ///
  229. /// \param[in] paths
  230. /// A list of paths to search for the library whose basename is
  231. /// local_spec.
  232. ///
  233. /// \param[out] loaded_path
  234. /// If the library was found along the paths, this will store the
  235. /// full path to the found library.
  236. ///
  237. /// \param[out] error
  238. /// An error object that gets filled in with any errors that
  239. /// might occur when trying to search for the shared library.
  240. ///
  241. /// \return
  242. /// A token that represents the shared library that can be
  243. /// later passed to UnloadImage. A value of
  244. /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared
  245. /// library can't be opened.
  246. uint32_t LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,
  247. SBStringList &paths,
  248. lldb::SBFileSpec &loaded_path,
  249. lldb::SBError &error);
  250. lldb::SBError UnloadImage(uint32_t image_token);
  251. lldb::SBError SendEventData(const char *data);
  252. /// Return the number of different thread-origin extended backtraces
  253. /// this process can support.
  254. ///
  255. /// When the process is stopped and you have an SBThread, lldb may be
  256. /// able to show a backtrace of when that thread was originally created,
  257. /// or the work item was enqueued to it (in the case of a libdispatch
  258. /// queue).
  259. ///
  260. /// \return
  261. /// The number of thread-origin extended backtrace types that may be
  262. /// available.
  263. uint32_t GetNumExtendedBacktraceTypes();
  264. /// Return the name of one of the thread-origin extended backtrace
  265. /// methods.
  266. ///
  267. /// \param [in] idx
  268. /// The index of the name to return. They will be returned in
  269. /// the order that the user will most likely want to see them.
  270. /// e.g. if the type at index 0 is not available for a thread,
  271. /// see if the type at index 1 provides an extended backtrace.
  272. ///
  273. /// \return
  274. /// The name at that index.
  275. const char *GetExtendedBacktraceTypeAtIndex(uint32_t idx);
  276. lldb::SBThreadCollection GetHistoryThreads(addr_t addr);
  277. bool IsInstrumentationRuntimePresent(InstrumentationRuntimeType type);
  278. /// Save the state of the process in a core file (or mini dump on Windows).
  279. lldb::SBError SaveCore(const char *file_name);
  280. /// Query the address load_addr and store the details of the memory
  281. /// region that contains it in the supplied SBMemoryRegionInfo object.
  282. /// To iterate over all memory regions use GetMemoryRegionList.
  283. ///
  284. /// \param[in] load_addr
  285. /// The address to be queried.
  286. ///
  287. /// \param[out] region_info
  288. /// A reference to an SBMemoryRegionInfo object that will contain
  289. /// the details of the memory region containing load_addr.
  290. ///
  291. /// \return
  292. /// An error object describes any errors that occurred while
  293. /// querying load_addr.
  294. lldb::SBError GetMemoryRegionInfo(lldb::addr_t load_addr,
  295. lldb::SBMemoryRegionInfo &region_info);
  296. /// Return the list of memory regions within the process.
  297. ///
  298. /// \return
  299. /// A list of all witin the process memory regions.
  300. lldb::SBMemoryRegionInfoList GetMemoryRegions();
  301. /// Return information about the process.
  302. ///
  303. /// Valid process info will only be returned when the process is
  304. /// alive, use SBProcessInfo::IsValid() to check returned info is
  305. /// valid.
  306. lldb::SBProcessInfo GetProcessInfo();
  307. protected:
  308. friend class SBAddress;
  309. friend class SBBreakpoint;
  310. friend class SBBreakpointLocation;
  311. friend class SBCommandInterpreter;
  312. friend class SBDebugger;
  313. friend class SBExecutionContext;
  314. friend class SBFunction;
  315. friend class SBModule;
  316. friend class SBTarget;
  317. friend class SBThread;
  318. friend class SBValue;
  319. friend class lldb_private::QueueImpl;
  320. lldb::ProcessSP GetSP() const;
  321. void SetSP(const lldb::ProcessSP &process_sp);
  322. lldb::ProcessWP m_opaque_wp;
  323. };
  324. } // namespace lldb
  325. #endif // LLDB_API_SBPROCESS_H