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 lldb::SBError ContinueInDirection(lldb::RunDirection direction); 163 164 lldb::SBError Stop(); 165 166 lldb::SBError Kill(); 167 168 lldb::SBError Detach(); 169 170 lldb::SBError Detach(bool keep_stopped); 171 172 lldb::SBError Signal(int signal); 173 174 lldb::SBUnixSignals GetUnixSignals(); 175 176 void SendAsyncInterrupt(); 177 178 uint32_t GetStopID(bool include_expression_stops = false); 179 180 /// Gets the stop event corresponding to stop ID. 181 // 182 /// Note that it wasn't fully implemented and tracks only the stop 183 /// event for the last natural stop ID. 184 /// 185 /// \param [in] stop_id 186 /// The ID of the stop event to return. 187 /// 188 /// \return 189 /// The stop event corresponding to stop ID. 190 lldb::SBEvent GetStopEventForStopID(uint32_t stop_id); 191 192 /// If the process is a scripted process, changes its state to the new state. 193 /// No-op otherwise. 194 /// 195 /// \param [in] new_state 196 /// The new state that the scripted process should be set to. 197 /// 198 void ForceScriptedState(StateType new_state); 199 200 size_t ReadMemory(addr_t addr, void *buf, size_t size, lldb::SBError &error); 201 202 size_t WriteMemory(addr_t addr, const void *buf, size_t size, 203 lldb::SBError &error); 204 205 size_t ReadCStringFromMemory(addr_t addr, void *char_buf, size_t size, 206 lldb::SBError &error); 207 208 uint64_t ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size, 209 lldb::SBError &error); 210 211 lldb::addr_t ReadPointerFromMemory(addr_t addr, lldb::SBError &error); 212 213 lldb::SBAddressRangeList FindRangesInMemory(const void *buf, uint64_t size, 214 const SBAddressRangeList &ranges, 215 uint32_t alignment, 216 uint32_t max_matches, 217 SBError &error); 218 219 lldb::addr_t FindInMemory(const void *buf, uint64_t size, 220 const SBAddressRange &range, uint32_t alignment, 221 SBError &error); 222 223 // Events 224 static lldb::StateType GetStateFromEvent(const lldb::SBEvent &event); 225 226 static bool GetRestartedFromEvent(const lldb::SBEvent &event); 227 228 static size_t GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event); 229 230 static const char * 231 GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event, size_t idx); 232 233 static lldb::SBProcess GetProcessFromEvent(const lldb::SBEvent &event); 234 235 static bool GetInterruptedFromEvent(const lldb::SBEvent &event); 236 237 static lldb::SBStructuredData 238 GetStructuredDataFromEvent(const lldb::SBEvent &event); 239 240 static bool EventIsProcessEvent(const lldb::SBEvent &event); 241 242 static bool EventIsStructuredDataEvent(const lldb::SBEvent &event); 243 244 lldb::SBBroadcaster GetBroadcaster() const; 245 246 static const char *GetBroadcasterClass(); 247 248 bool GetDescription(lldb::SBStream &description); 249 250 SBStructuredData GetExtendedCrashInformation(); 251 252 uint32_t GetNumSupportedHardwareWatchpoints(lldb::SBError &error) const; 253 254 /// Load a shared library into this process. 255 /// 256 /// \param[in] remote_image_spec 257 /// The path for the shared library on the target what you want 258 /// to load. 259 /// 260 /// \param[out] error 261 /// An error object that gets filled in with any errors that 262 /// might occur when trying to load the shared library. 263 /// 264 /// \return 265 /// A token that represents the shared library that can be 266 /// later used to unload the shared library. A value of 267 /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared 268 /// library can't be opened. 269 uint32_t LoadImage(lldb::SBFileSpec &remote_image_spec, lldb::SBError &error); 270 271 /// Load a shared library into this process. 272 /// 273 /// \param[in] local_image_spec 274 /// The file spec that points to the shared library that you 275 /// want to load if the library is located on the host. The 276 /// library will be copied over to the location specified by 277 /// remote_image_spec or into the current working directory with 278 /// the same filename if the remote_image_spec isn't specified. 279 /// 280 /// \param[in] remote_image_spec 281 /// If local_image_spec is specified then the location where the 282 /// library should be copied over from the host. If 283 /// local_image_spec isn't specified, then the path for the 284 /// shared library on the target what you want to load. 285 /// 286 /// \param[out] error 287 /// An error object that gets filled in with any errors that 288 /// might occur when trying to load the shared library. 289 /// 290 /// \return 291 /// A token that represents the shared library that can be 292 /// later used to unload the shared library. A value of 293 /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared 294 /// library can't be opened. 295 uint32_t LoadImage(const lldb::SBFileSpec &local_image_spec, 296 const lldb::SBFileSpec &remote_image_spec, 297 lldb::SBError &error); 298 299 /// Load a shared library into this process, starting with a 300 /// library name and a list of paths, searching along the list of 301 /// paths till you find a matching library. 302 /// 303 /// \param[in] image_spec 304 /// The name of the shared library that you want to load. 305 /// If image_spec is a relative path, the relative path will be 306 /// appended to the search paths. 307 /// If the image_spec is an absolute path, just the basename is used. 308 /// 309 /// \param[in] paths 310 /// A list of paths to search for the library whose basename is 311 /// local_spec. 312 /// 313 /// \param[out] loaded_path 314 /// If the library was found along the paths, this will store the 315 /// full path to the found library. 316 /// 317 /// \param[out] error 318 /// An error object that gets filled in with any errors that 319 /// might occur when trying to search for the shared library. 320 /// 321 /// \return 322 /// A token that represents the shared library that can be 323 /// later passed to UnloadImage. A value of 324 /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared 325 /// library can't be opened. 326 uint32_t LoadImageUsingPaths(const lldb::SBFileSpec &image_spec, 327 SBStringList &paths, 328 lldb::SBFileSpec &loaded_path, 329 lldb::SBError &error); 330 331 lldb::SBError UnloadImage(uint32_t image_token); 332 333 lldb::SBError SendEventData(const char *data); 334 335 /// Return the number of different thread-origin extended backtraces 336 /// this process can support. 337 /// 338 /// When the process is stopped and you have an SBThread, lldb may be 339 /// able to show a backtrace of when that thread was originally created, 340 /// or the work item was enqueued to it (in the case of a libdispatch 341 /// queue). 342 /// 343 /// \return 344 /// The number of thread-origin extended backtrace types that may be 345 /// available. 346 uint32_t GetNumExtendedBacktraceTypes(); 347 348 /// Return the name of one of the thread-origin extended backtrace 349 /// methods. 350 /// 351 /// \param [in] idx 352 /// The index of the name to return. They will be returned in 353 /// the order that the user will most likely want to see them. 354 /// e.g. if the type at index 0 is not available for a thread, 355 /// see if the type at index 1 provides an extended backtrace. 356 /// 357 /// \return 358 /// The name at that index. 359 const char *GetExtendedBacktraceTypeAtIndex(uint32_t idx); 360 361 lldb::SBThreadCollection GetHistoryThreads(addr_t addr); 362 363 bool IsInstrumentationRuntimePresent(InstrumentationRuntimeType type); 364 365 /// Save the state of the process in a core file. 366 /// 367 /// \param[in] file_name - The name of the file to save the core file to. 368 /// 369 /// \param[in] flavor - Specify the flavor of a core file plug-in to save. 370 /// Currently supported flavors include "mach-o" and "minidump" 371 /// 372 /// \param[in] core_style - Specify the style of a core file to save. 373 lldb::SBError SaveCore(const char *file_name, const char *flavor, 374 SaveCoreStyle core_style); 375 376 /// Save the state of the process with the a flavor that matches the 377 /// current process' main executable (if supported). 378 /// 379 /// \param[in] file_name - The name of the file to save the core file to. 380 lldb::SBError SaveCore(const char *file_name); 381 382 /// Save the state of the process with the desired settings 383 /// as defined in the options object. 384 /// 385 /// \param[in] options - The options to use when saving the core file. 386 lldb::SBError SaveCore(SBSaveCoreOptions &options); 387 388 /// Query the address load_addr and store the details of the memory 389 /// region that contains it in the supplied SBMemoryRegionInfo object. 390 /// To iterate over all memory regions use GetMemoryRegionList. 391 /// 392 /// \param[in] load_addr 393 /// The address to be queried. 394 /// 395 /// \param[out] region_info 396 /// A reference to an SBMemoryRegionInfo object that will contain 397 /// the details of the memory region containing load_addr. 398 /// 399 /// \return 400 /// An error object describes any errors that occurred while 401 /// querying load_addr. 402 lldb::SBError GetMemoryRegionInfo(lldb::addr_t load_addr, 403 lldb::SBMemoryRegionInfo ®ion_info); 404 405 /// Return the list of memory regions within the process. 406 /// 407 /// \return 408 /// A list of all witin the process memory regions. 409 lldb::SBMemoryRegionInfoList GetMemoryRegions(); 410 411 /// Return information about the process. 412 /// 413 /// Valid process info will only be returned when the process is 414 /// alive, use SBProcessInfo::IsValid() to check returned info is 415 /// valid. 416 lldb::SBProcessInfo GetProcessInfo(); 417 418 /// Get the file specification for the core file that is currently being used 419 /// for the process. If the process is not loaded from a core file, then an 420 /// invalid file specification will be returned. 421 /// 422 /// \return 423 /// The path to the core file for this target or an invalid file spec if 424 /// the process isn't loaded from a core file. 425 lldb::SBFileSpec GetCoreFile(); 426 427 /// \{ 428 /// \group Mask Address Methods 429 /// 430 /// \a type 431 /// All of the methods in this group take \a type argument 432 /// which is an AddressMaskType enum value. 433 /// There can be different address masks for code addresses and 434 /// data addresses, this argument can select which to get/set, 435 /// or to use when clearing non-addressable bits from an address. 436 /// This choice of mask can be important for example on AArch32 437 /// systems. Where instructions where instructions start on even addresses, 438 /// the 0th bit may be used to indicate that a function is thumb code. On 439 /// such a target, the eAddressMaskTypeCode may clear the 0th bit from an 440 /// address to get the actual address Whereas eAddressMaskTypeData would not. 441 /// 442 /// \a addr_range 443 /// Many of the methods in this group take an \a addr_range argument 444 /// which is an AddressMaskRange enum value. 445 /// Needing to specify the address range is highly unusual, and the 446 /// default argument can be used in nearly all circumstances. 447 /// On some architectures (e.g., AArch64), it is possible to have 448 /// different page table setups for low and high memory, so different 449 /// numbers of bits relevant to addressing. It is possible to have 450 /// a program running in one half of memory and accessing the other 451 /// as heap, so we need to maintain two different sets of address masks 452 /// to debug this correctly. 453 454 /// Get the current address mask that will be applied to addresses 455 /// before reading from memory. 456 /// 457 /// \param[in] type 458 /// See \ref Mask Address Methods description of this argument. 459 /// eAddressMaskTypeAny is often a suitable value when code and 460 /// data masks are the same on a given target. 461 /// 462 /// \param[in] addr_range 463 /// See \ref Mask Address Methods description of this argument. 464 /// This will default to eAddressMaskRangeLow which is the 465 /// only set of masks used normally. 466 /// 467 /// \return 468 /// The address mask currently in use. Bits which are not used 469 /// for addressing will be set to 1 in the mask. 470 lldb::addr_t GetAddressMask( 471 lldb::AddressMaskType type, 472 lldb::AddressMaskRange addr_range = lldb::eAddressMaskRangeLow); 473 474 /// Set the current address mask that can be applied to addresses 475 /// before reading from memory. 476 /// 477 /// \param[in] type 478 /// See \ref Mask Address Methods description of this argument. 479 /// eAddressMaskTypeAll is often a suitable value when the 480 /// same mask is being set for both code and data. 481 /// 482 /// \param[in] mask 483 /// The address mask to set. Bits which are not used for addressing 484 /// should be set to 1 in the mask. 485 /// 486 /// \param[in] addr_range 487 /// See \ref Mask Address Methods description of this argument. 488 /// This will default to eAddressMaskRangeLow which is the 489 /// only set of masks used normally. 490 void SetAddressMask( 491 lldb::AddressMaskType type, lldb::addr_t mask, 492 lldb::AddressMaskRange addr_range = lldb::eAddressMaskRangeLow); 493 494 /// Set the number of bits used for addressing in this Process. 495 /// 496 /// On Darwin and similar systems, the addressable bits are expressed 497 /// as the number of low order bits that are relevant to addressing, 498 /// instead of a more general address mask. 499 /// This method calculates the correct mask value for a given number 500 /// of low order addressable bits. 501 /// 502 /// \param[in] type 503 /// See \ref Mask Address Methods description of this argument. 504 /// eAddressMaskTypeAll is often a suitable value when the 505 /// same mask is being set for both code and data. 506 /// 507 /// \param[in] num_bits 508 /// Number of bits that are used for addressing. 509 /// For example, a value of 42 indicates that the low 42 bits 510 /// are relevant for addressing, and that higher-order bits may 511 /// be used for various metadata like pointer authentication, 512 /// Type Byte Ignore, etc. 513 /// 514 /// \param[in] addr_range 515 /// See \ref Mask Address Methods description of this argument. 516 /// This will default to eAddressMaskRangeLow which is the 517 /// only set of masks used normally. 518 void 519 SetAddressableBits(AddressMaskType type, uint32_t num_bits, 520 AddressMaskRange addr_range = lldb::eAddressMaskRangeLow); 521 522 /// Clear the non-address bits of an \a addr value and return a 523 /// virtual address in memory. 524 /// 525 /// Bits that are not used in addressing may be used for other purposes; 526 /// pointer authentication, or metadata in the top byte, or the 0th bit 527 /// of armv7 code addresses to indicate arm/thumb are common examples. 528 /// 529 /// \param[in] addr 530 /// The address that should be cleared of non-address bits. 531 /// 532 /// \param[in] type 533 /// See \ref Mask Address Methods description of this argument. 534 /// eAddressMaskTypeAny is the default value, correct when it 535 /// is unknown if the address is a code or data address. 536 lldb::addr_t 537 FixAddress(lldb::addr_t addr, 538 lldb::AddressMaskType type = lldb::eAddressMaskTypeAny); 539 /// \} 540 541 /// Allocate memory within the process. 542 /// 543 /// This function will allocate memory in the process's address space. 544 /// 545 /// \param[in] size 546 /// The size of the allocation requested. 547 /// 548 /// \param[in] permissions 549 /// Or together any of the lldb::Permissions bits. The 550 /// permissions on a given memory allocation can't be changed 551 /// after allocation. Note that a block that isn't set writable 552 /// can still be written from lldb, just not by the process 553 /// itself. 554 /// 555 /// \param[out] error 556 /// An error object that gets filled in with any errors that 557 /// might occur when trying allocate. 558 /// 559 /// \return 560 /// The address of the allocated buffer in the process, or 561 /// LLDB_INVALID_ADDRESS if the allocation failed. 562 lldb::addr_t AllocateMemory(size_t size, uint32_t permissions, 563 lldb::SBError &error); 564 565 /// Deallocate memory in the process. 566 /// 567 /// This function will deallocate memory in the process's address 568 /// space that was allocated with AllocateMemory. 569 /// 570 /// \param[in] ptr 571 /// A return value from AllocateMemory, pointing to the memory you 572 /// want to deallocate. 573 /// 574 /// \return 575 /// An error object describes any errors that occurred while 576 /// deallocating. 577 /// 578 lldb::SBError DeallocateMemory(lldb::addr_t ptr); 579 580 lldb::SBScriptObject GetScriptedImplementation(); 581 582 void GetStatus(SBStream &status); 583 584 protected: 585 friend class SBAddress; 586 friend class SBBreakpoint; 587 friend class SBBreakpointCallbackBaton; 588 friend class SBBreakpointLocation; 589 friend class SBCommandInterpreter; 590 friend class SBSaveCoreOptions; 591 friend class SBDebugger; 592 friend class SBExecutionContext; 593 friend class SBFunction; 594 friend class SBModule; 595 friend class SBPlatform; 596 friend class SBTarget; 597 friend class SBThread; 598 friend class SBValue; 599 friend class lldb_private::QueueImpl; 600 601 friend class lldb_private::python::SWIGBridge; 602 603 SBProcess(const lldb::ProcessSP &process_sp); 604 605 lldb::ProcessSP GetSP() const; 606 607 void SetSP(const lldb::ProcessSP &process_sp); 608 609 lldb::ProcessWP m_opaque_wp; 610 }; 611 612 } // namespace lldb 613 614 #endif // LLDB_API_SBPROCESS_H 615