xref: /freebsd/contrib/llvm-project/lldb/include/lldb/API/SBProcess.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
9 #ifndef LLDB_API_SBPROCESS_H
10 #define LLDB_API_SBPROCESS_H
11 
12 #include "lldb/API/SBDefines.h"
13 #include "lldb/API/SBError.h"
14 #include "lldb/API/SBProcessInfo.h"
15 #include "lldb/API/SBQueue.h"
16 #include "lldb/API/SBTarget.h"
17 #include <cstdio>
18 
19 namespace lldb_private {
20 namespace python {
21 class SWIGBridge;
22 }
23 } // namespace lldb_private
24 
25 namespace lldb {
26 
27 class SBEvent;
28 
29 class LLDB_API SBProcess {
30 public:
31   /// Broadcaster event bits definitions.
FLAGS_ANONYMOUS_ENUM()32   FLAGS_ANONYMOUS_ENUM(){eBroadcastBitStateChanged = (1 << 0),
33                          eBroadcastBitInterrupt = (1 << 1),
34                          eBroadcastBitSTDOUT = (1 << 2),
35                          eBroadcastBitSTDERR = (1 << 3),
36                          eBroadcastBitProfileData = (1 << 4),
37                          eBroadcastBitStructuredData = (1 << 5)};
38 
39   SBProcess();
40 
41   SBProcess(const lldb::SBProcess &rhs);
42 
43   const lldb::SBProcess &operator=(const lldb::SBProcess &rhs);
44 
45   ~SBProcess();
46 
47   static const char *GetBroadcasterClassName();
48 
49   const char *GetPluginName();
50 
51   LLDB_DEPRECATED_FIXME("Use GetPluginName()", "GetPluginName()")
52   const char *GetShortPluginName();
53 
54   void Clear();
55 
56   explicit operator bool() const;
57 
58   bool IsValid() const;
59 
60   lldb::SBTarget GetTarget() const;
61 
62   lldb::ByteOrder GetByteOrder() const;
63 
64   size_t PutSTDIN(const char *src, size_t src_len);
65 
66   size_t GetSTDOUT(char *dst, size_t dst_len) const;
67 
68   size_t GetSTDERR(char *dst, size_t dst_len) const;
69 
70   size_t GetAsyncProfileData(char *dst, size_t dst_len) const;
71 
72 #ifndef SWIG
73   void ReportEventState(const lldb::SBEvent &event, FILE *out) const;
74 #endif
75 
76   void ReportEventState(const lldb::SBEvent &event, SBFile file) const;
77 
78   void ReportEventState(const lldb::SBEvent &event, FileSP BORROWED) const;
79 
80   void AppendEventStateReport(const lldb::SBEvent &event,
81                               lldb::SBCommandReturnObject &result);
82 
83   /// Remote connection related functions. These will fail if the
84   /// process is not in eStateConnected. They are intended for use
85   /// when connecting to an externally managed debugserver instance.
86   bool RemoteAttachToProcessWithID(lldb::pid_t pid, lldb::SBError &error);
87 
88   bool RemoteLaunch(char const **argv, char const **envp,
89                     const char *stdin_path, const char *stdout_path,
90                     const char *stderr_path, const char *working_directory,
91                     uint32_t launch_flags, bool stop_at_entry,
92                     lldb::SBError &error);
93 
94   // Thread related functions
95   uint32_t GetNumThreads();
96 
97   lldb::SBThread GetThreadAtIndex(size_t index);
98 
99   lldb::SBThread GetThreadByID(lldb::tid_t sb_thread_id);
100 
101   lldb::SBThread GetThreadByIndexID(uint32_t index_id);
102 
103   lldb::SBThread GetSelectedThread() const;
104 
105   // Function for lazily creating a thread using the current OS plug-in. This
106   // function will be removed in the future when there are APIs to create
107   // SBThread objects through the interface and add them to the process through
108   // the SBProcess API.
109   lldb::SBThread CreateOSPluginThread(lldb::tid_t tid, lldb::addr_t context);
110 
111   bool SetSelectedThread(const lldb::SBThread &thread);
112 
113   bool SetSelectedThreadByID(lldb::tid_t tid);
114 
115   bool SetSelectedThreadByIndexID(uint32_t index_id);
116 
117   // Queue related functions
118   uint32_t GetNumQueues();
119 
120   lldb::SBQueue GetQueueAtIndex(size_t index);
121 
122   // Stepping related functions
123 
124   lldb::StateType GetState();
125 
126   int GetExitStatus();
127 
128   const char *GetExitDescription();
129 
130   /// Gets the process ID
131   ///
132   /// Returns the process identifier for the process as it is known
133   /// on the system on which the process is running. For unix systems
134   /// this is typically the same as if you called "getpid()" in the
135   /// process.
136   ///
137   /// \return
138   ///     Returns LLDB_INVALID_PROCESS_ID if this object does not
139   ///     contain a valid process object, or if the process has not
140   ///     been launched. Returns a valid process ID if the process is
141   ///     valid.
142   lldb::pid_t GetProcessID();
143 
144   /// Gets the unique ID associated with this process object
145   ///
146   /// Unique IDs start at 1 and increment up with each new process
147   /// instance. Since starting a process on a system might always
148   /// create a process with the same process ID, there needs to be a
149   /// way to tell two process instances apart.
150   ///
151   /// \return
152   ///     Returns a non-zero integer ID if this object contains a
153   ///     valid process object, zero if this object does not contain
154   ///     a valid process object.
155   uint32_t GetUniqueID();
156 
157   uint32_t GetAddressByteSize() const;
158 
159   lldb::SBError Destroy();
160 
161   lldb::SBError Continue();
162 
163   lldb::SBError Stop();
164 
165   lldb::SBError Kill();
166 
167   lldb::SBError Detach();
168 
169   lldb::SBError Detach(bool keep_stopped);
170 
171   lldb::SBError Signal(int signal);
172 
173   lldb::SBUnixSignals GetUnixSignals();
174 
175   void SendAsyncInterrupt();
176 
177   uint32_t GetStopID(bool include_expression_stops = false);
178 
179   /// Gets the stop event corresponding to stop ID.
180   //
181   /// Note that it wasn't fully implemented and tracks only the stop
182   /// event for the last natural stop ID.
183   ///
184   /// \param [in] stop_id
185   ///   The ID of the stop event to return.
186   ///
187   /// \return
188   ///   The stop event corresponding to stop ID.
189   lldb::SBEvent GetStopEventForStopID(uint32_t stop_id);
190 
191   /// If the process is a scripted process, changes its state to the new state.
192   /// No-op otherwise.
193   ///
194   /// \param [in] new_state
195   ///   The new state that the scripted process should be set to.
196   ///
197   void ForceScriptedState(StateType new_state);
198 
199   size_t ReadMemory(addr_t addr, void *buf, size_t size, lldb::SBError &error);
200 
201   size_t WriteMemory(addr_t addr, const void *buf, size_t size,
202                      lldb::SBError &error);
203 
204   size_t ReadCStringFromMemory(addr_t addr, void *char_buf, size_t size,
205                                lldb::SBError &error);
206 
207   uint64_t ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
208                                   lldb::SBError &error);
209 
210   lldb::addr_t ReadPointerFromMemory(addr_t addr, lldb::SBError &error);
211 
212   lldb::SBAddressRangeList FindRangesInMemory(const void *buf, uint64_t size,
213                                               const SBAddressRangeList &ranges,
214                                               uint32_t alignment,
215                                               uint32_t max_matches,
216                                               SBError &error);
217 
218   lldb::addr_t FindInMemory(const void *buf, uint64_t size,
219                             const SBAddressRange &range, uint32_t alignment,
220                             SBError &error);
221 
222   // Events
223   static lldb::StateType GetStateFromEvent(const lldb::SBEvent &event);
224 
225   static bool GetRestartedFromEvent(const lldb::SBEvent &event);
226 
227   static size_t GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event);
228 
229   static const char *
230   GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event, size_t idx);
231 
232   static lldb::SBProcess GetProcessFromEvent(const lldb::SBEvent &event);
233 
234   static bool GetInterruptedFromEvent(const lldb::SBEvent &event);
235 
236   static lldb::SBStructuredData
237   GetStructuredDataFromEvent(const lldb::SBEvent &event);
238 
239   static bool EventIsProcessEvent(const lldb::SBEvent &event);
240 
241   static bool EventIsStructuredDataEvent(const lldb::SBEvent &event);
242 
243   lldb::SBBroadcaster GetBroadcaster() const;
244 
245   static const char *GetBroadcasterClass();
246 
247   bool GetDescription(lldb::SBStream &description);
248 
249   SBStructuredData GetExtendedCrashInformation();
250 
251   uint32_t GetNumSupportedHardwareWatchpoints(lldb::SBError &error) const;
252 
253   /// Load a shared library into this process.
254   ///
255   /// \param[in] remote_image_spec
256   ///     The path for the shared library on the target what you want
257   ///     to load.
258   ///
259   /// \param[out] error
260   ///     An error object that gets filled in with any errors that
261   ///     might occur when trying to load the shared library.
262   ///
263   /// \return
264   ///     A token that represents the shared library that can be
265   ///     later used to unload the shared library. A value of
266   ///     LLDB_INVALID_IMAGE_TOKEN will be returned if the shared
267   ///     library can't be opened.
268   uint32_t LoadImage(lldb::SBFileSpec &remote_image_spec, lldb::SBError &error);
269 
270   /// Load a shared library into this process.
271   ///
272   /// \param[in] local_image_spec
273   ///     The file spec that points to the shared library that you
274   ///     want to load if the library is located on the host. The
275   ///     library will be copied over to the location specified by
276   ///     remote_image_spec or into the current working directory with
277   ///     the same filename if the remote_image_spec isn't specified.
278   ///
279   /// \param[in] remote_image_spec
280   ///     If local_image_spec is specified then the location where the
281   ///     library should be copied over from the host. If
282   ///     local_image_spec isn't specified, then the path for the
283   ///     shared library on the target what you want to load.
284   ///
285   /// \param[out] error
286   ///     An error object that gets filled in with any errors that
287   ///     might occur when trying to load the shared library.
288   ///
289   /// \return
290   ///     A token that represents the shared library that can be
291   ///     later used to unload the shared library. A value of
292   ///     LLDB_INVALID_IMAGE_TOKEN will be returned if the shared
293   ///     library can't be opened.
294   uint32_t LoadImage(const lldb::SBFileSpec &local_image_spec,
295                      const lldb::SBFileSpec &remote_image_spec,
296                      lldb::SBError &error);
297 
298   /// Load a shared library into this process, starting with a
299   /// library name and a list of paths, searching along the list of
300   /// paths till you find a matching library.
301   ///
302   /// \param[in] image_spec
303   ///     The name of the shared library that you want to load.
304   ///     If image_spec is a relative path, the relative path will be
305   ///     appended to the search paths.
306   ///     If the image_spec is an absolute path, just the basename is used.
307   ///
308   /// \param[in] paths
309   ///     A list of paths to search for the library whose basename is
310   ///     local_spec.
311   ///
312   /// \param[out] loaded_path
313   ///     If the library was found along the paths, this will store the
314   ///     full path to the found library.
315   ///
316   /// \param[out] error
317   ///     An error object that gets filled in with any errors that
318   ///     might occur when trying to search for the shared library.
319   ///
320   /// \return
321   ///     A token that represents the shared library that can be
322   ///     later passed to UnloadImage. A value of
323   ///     LLDB_INVALID_IMAGE_TOKEN will be returned if the shared
324   ///     library can't be opened.
325   uint32_t LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,
326                                SBStringList &paths,
327                                lldb::SBFileSpec &loaded_path,
328                                lldb::SBError &error);
329 
330   lldb::SBError UnloadImage(uint32_t image_token);
331 
332   lldb::SBError SendEventData(const char *data);
333 
334   /// Return the number of different thread-origin extended backtraces
335   /// this process can support.
336   ///
337   /// When the process is stopped and you have an SBThread, lldb may be
338   /// able to show a backtrace of when that thread was originally created,
339   /// or the work item was enqueued to it (in the case of a libdispatch
340   /// queue).
341   ///
342   /// \return
343   ///   The number of thread-origin extended backtrace types that may be
344   ///   available.
345   uint32_t GetNumExtendedBacktraceTypes();
346 
347   /// Return the name of one of the thread-origin extended backtrace
348   /// methods.
349   ///
350   /// \param [in] idx
351   ///   The index of the name to return.  They will be returned in
352   ///   the order that the user will most likely want to see them.
353   ///   e.g. if the type at index 0 is not available for a thread,
354   ///   see if the type at index 1 provides an extended backtrace.
355   ///
356   /// \return
357   ///   The name at that index.
358   const char *GetExtendedBacktraceTypeAtIndex(uint32_t idx);
359 
360   lldb::SBThreadCollection GetHistoryThreads(addr_t addr);
361 
362   bool IsInstrumentationRuntimePresent(InstrumentationRuntimeType type);
363 
364   /// Save the state of the process in a core file.
365   ///
366   /// \param[in] file_name - The name of the file to save the core file to.
367   ///
368   /// \param[in] flavor - Specify the flavor of a core file plug-in to save.
369   /// Currently supported flavors include "mach-o" and "minidump"
370   ///
371   /// \param[in] core_style - Specify the style of a core file to save.
372   lldb::SBError SaveCore(const char *file_name, const char *flavor,
373                          SaveCoreStyle core_style);
374 
375   /// Save the state of the process with the a flavor that matches the
376   /// current process' main executable (if supported).
377   ///
378   /// \param[in] file_name - The name of the file to save the core file to.
379   lldb::SBError SaveCore(const char *file_name);
380 
381   /// Save the state of the process with the desired settings
382   /// as defined in the options object.
383   ///
384   /// \param[in] options - The options to use when saving the core file.
385   lldb::SBError SaveCore(SBSaveCoreOptions &options);
386 
387   /// Query the address load_addr and store the details of the memory
388   /// region that contains it in the supplied SBMemoryRegionInfo object.
389   /// To iterate over all memory regions use GetMemoryRegionList.
390   ///
391   /// \param[in] load_addr
392   ///     The address to be queried.
393   ///
394   /// \param[out] region_info
395   ///     A reference to an SBMemoryRegionInfo object that will contain
396   ///     the details of the memory region containing load_addr.
397   ///
398   /// \return
399   ///     An error object describes any errors that occurred while
400   ///     querying load_addr.
401   lldb::SBError GetMemoryRegionInfo(lldb::addr_t load_addr,
402                                     lldb::SBMemoryRegionInfo &region_info);
403 
404   /// Return the list of memory regions within the process.
405   ///
406   /// \return
407   ///     A list of all witin the process memory regions.
408   lldb::SBMemoryRegionInfoList GetMemoryRegions();
409 
410   /// Return information about the process.
411   ///
412   /// Valid process info will only be returned when the process is
413   /// alive, use SBProcessInfo::IsValid() to check returned info is
414   /// valid.
415   lldb::SBProcessInfo GetProcessInfo();
416 
417   /// Get the file specification for the core file that is currently being used
418   /// for the process. If the process is not loaded from a core file, then an
419   /// invalid file specification will be returned.
420   ///
421   /// \return
422   ///     The path to the core file for this target or an invalid file spec if
423   ///     the process isn't loaded from a core file.
424   lldb::SBFileSpec GetCoreFile();
425 
426   /// \{
427   /// \group Mask Address Methods
428   ///
429   /// \a type
430   /// All of the methods in this group take \a type argument
431   /// which is an AddressMaskType enum value.
432   /// There can be different address masks for code addresses and
433   /// data addresses, this argument can select which to get/set,
434   /// or to use when clearing non-addressable bits from an address.
435   /// This choice of mask can be important for example on AArch32
436   /// systems. Where instructions where instructions start on even addresses,
437   /// the 0th bit may be used to indicate that a function is thumb code.  On
438   /// such a target, the eAddressMaskTypeCode may clear the 0th bit from an
439   /// address to get the actual address Whereas eAddressMaskTypeData would not.
440   ///
441   /// \a addr_range
442   /// Many of the methods in this group take an \a addr_range argument
443   /// which is an AddressMaskRange enum value.
444   /// Needing to specify the address range is highly unusual, and the
445   /// default argument can be used in nearly all circumstances.
446   /// On some architectures (e.g., AArch64), it is possible to have
447   /// different page table setups for low and high memory, so different
448   /// numbers of bits relevant to addressing. It is possible to have
449   /// a program running in one half of memory and accessing the other
450   /// as heap, so we need to maintain two different sets of address masks
451   /// to debug this correctly.
452 
453   /// Get the current address mask that will be applied to addresses
454   /// before reading from memory.
455   ///
456   /// \param[in] type
457   ///     See \ref Mask Address Methods description of this argument.
458   ///     eAddressMaskTypeAny is often a suitable value when code and
459   ///     data masks are the same on a given target.
460   ///
461   /// \param[in] addr_range
462   ///     See \ref Mask Address Methods description of this argument.
463   ///     This will default to eAddressMaskRangeLow which is the
464   ///     only set of masks used normally.
465   ///
466   /// \return
467   ///     The address mask currently in use.  Bits which are not used
468   ///     for addressing will be set to 1 in the mask.
469   lldb::addr_t GetAddressMask(
470       lldb::AddressMaskType type,
471       lldb::AddressMaskRange addr_range = lldb::eAddressMaskRangeLow);
472 
473   /// Set the current address mask that can be applied to addresses
474   /// before reading from memory.
475   ///
476   /// \param[in] type
477   ///     See \ref Mask Address Methods description of this argument.
478   ///     eAddressMaskTypeAll is often a suitable value when the
479   ///     same mask is being set for both code and data.
480   ///
481   /// \param[in] mask
482   ///     The address mask to set.  Bits which are not used for addressing
483   ///     should be set to 1 in the mask.
484   ///
485   /// \param[in] addr_range
486   ///     See \ref Mask Address Methods description of this argument.
487   ///     This will default to eAddressMaskRangeLow which is the
488   ///     only set of masks used normally.
489   void SetAddressMask(
490       lldb::AddressMaskType type, lldb::addr_t mask,
491       lldb::AddressMaskRange addr_range = lldb::eAddressMaskRangeLow);
492 
493   /// Set the number of bits used for addressing in this Process.
494   ///
495   /// On Darwin and similar systems, the addressable bits are expressed
496   /// as the number of low order bits that are relevant to addressing,
497   /// instead of a more general address mask.
498   /// This method calculates the correct mask value for a given number
499   /// of low order addressable bits.
500   ///
501   /// \param[in] type
502   ///     See \ref Mask Address Methods description of this argument.
503   ///     eAddressMaskTypeAll is often a suitable value when the
504   ///     same mask is being set for both code and data.
505   ///
506   /// \param[in] num_bits
507   ///     Number of bits that are used for addressing.
508   ///     For example, a value of 42 indicates that the low 42 bits
509   ///     are relevant for addressing, and that higher-order bits may
510   ///     be used for various metadata like pointer authentication,
511   ///     Type Byte Ignore, etc.
512   ///
513   /// \param[in] addr_range
514   ///     See \ref Mask Address Methods description of this argument.
515   ///     This will default to eAddressMaskRangeLow which is the
516   ///     only set of masks used normally.
517   void
518   SetAddressableBits(AddressMaskType type, uint32_t num_bits,
519                      AddressMaskRange addr_range = lldb::eAddressMaskRangeLow);
520 
521   /// Clear the non-address bits of an \a addr value and return a
522   /// virtual address in memory.
523   ///
524   /// Bits that are not used in addressing may be used for other purposes;
525   /// pointer authentication, or metadata in the top byte, or the 0th bit
526   /// of armv7 code addresses to indicate arm/thumb are common examples.
527   ///
528   /// \param[in] addr
529   ///     The address that should be cleared of non-address bits.
530   ///
531   /// \param[in] type
532   ///     See \ref Mask Address Methods description of this argument.
533   ///     eAddressMaskTypeAny is the default value, correct when it
534   ///     is unknown if the address is a code or data address.
535   lldb::addr_t
536   FixAddress(lldb::addr_t addr,
537              lldb::AddressMaskType type = lldb::eAddressMaskTypeAny);
538   /// \}
539 
540   /// Allocate memory within the process.
541   ///
542   /// This function will allocate memory in the process's address space.
543   ///
544   /// \param[in] size
545   ///     The size of the allocation requested.
546   ///
547   /// \param[in] permissions
548   ///     Or together any of the lldb::Permissions bits.  The
549   ///     permissions on a given memory allocation can't be changed
550   ///     after allocation.  Note that a block that isn't set writable
551   ///     can still be written from lldb, just not by the process
552   ///     itself.
553   ///
554   /// \param[out] error
555   ///     An error object that gets filled in with any errors that
556   ///     might occur when trying allocate.
557   ///
558   /// \return
559   ///     The address of the allocated buffer in the process, or
560   ///     LLDB_INVALID_ADDRESS if the allocation failed.
561   lldb::addr_t AllocateMemory(size_t size, uint32_t permissions,
562                               lldb::SBError &error);
563 
564   /// Deallocate memory in the process.
565   ///
566   /// This function will deallocate memory in the process's address
567   /// space that was allocated with AllocateMemory.
568   ///
569   /// \param[in] ptr
570   ///     A return value from AllocateMemory, pointing to the memory you
571   ///     want to deallocate.
572   ///
573   /// \return
574   ///     An error object describes any errors that occurred while
575   ///     deallocating.
576   ///
577   lldb::SBError DeallocateMemory(lldb::addr_t ptr);
578 
579   lldb::SBScriptObject GetScriptedImplementation();
580 
581   void GetStatus(SBStream &status);
582 
583 protected:
584   friend class SBAddress;
585   friend class SBBreakpoint;
586   friend class SBBreakpointCallbackBaton;
587   friend class SBBreakpointLocation;
588   friend class SBCommandInterpreter;
589   friend class SBDebugger;
590   friend class SBExecutionContext;
591   friend class SBFunction;
592   friend class SBModule;
593   friend class SBPlatform;
594   friend class SBTarget;
595   friend class SBThread;
596   friend class SBValue;
597   friend class lldb_private::QueueImpl;
598 
599   friend class lldb_private::python::SWIGBridge;
600 
601   SBProcess(const lldb::ProcessSP &process_sp);
602 
603   lldb::ProcessSP GetSP() const;
604 
605   void SetSP(const lldb::ProcessSP &process_sp);
606 
607   lldb::ProcessWP m_opaque_wp;
608 };
609 
610 } // namespace lldb
611 
612 #endif // LLDB_API_SBPROCESS_H
613