1 //===-- RNBContext.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 // Created by Greg Clayton on 12/12/07. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBCONTEXT_H 14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBCONTEXT_H 15 16 #include "DNBError.h" 17 #include "PThreadEvent.h" 18 #include "RNBDefs.h" 19 #include <string> 20 #include <vector> 21 22 class RNBContext { 23 public: 24 enum { 25 event_proc_state_changed = 0x001, 26 event_proc_thread_running = 0x002, // Sticky 27 event_proc_thread_exiting = 0x004, 28 event_proc_stdio_available = 0x008, 29 event_proc_profile_data = 0x010, 30 event_read_packet_available = 0x020, 31 event_read_thread_running = 0x040, // Sticky 32 event_read_thread_exiting = 0x080, 33 event_darwin_log_data_available = 0x100, 34 35 normal_event_bits = event_proc_state_changed | event_proc_thread_exiting | 36 event_proc_stdio_available | event_proc_profile_data | 37 event_read_packet_available | 38 event_read_thread_exiting | 39 event_darwin_log_data_available, 40 41 sticky_event_bits = event_proc_thread_running | event_read_thread_running, 42 43 all_event_bits = sticky_event_bits | normal_event_bits 44 } event_t; 45 // Constructors and Destructors RNBContext()46 RNBContext() 47 : m_pid(INVALID_NUB_PROCESS), m_pid_stop_count(0), 48 m_events(0, all_event_bits), m_pid_pthread(), m_launch_status(), 49 m_arg_vec(), m_env_vec(), m_detach_on_error(false) {} 50 51 virtual ~RNBContext(); 52 ProcessID()53 nub_process_t ProcessID() const { return m_pid; } HasValidProcessID()54 bool HasValidProcessID() const { return m_pid != INVALID_NUB_PROCESS; } 55 void SetProcessID(nub_process_t pid); GetProcessStopCount()56 nub_size_t GetProcessStopCount() const { return m_pid_stop_count; } SetProcessStopCount(nub_size_t count)57 bool SetProcessStopCount(nub_size_t count) { 58 // Returns true if this class' notion of the PID state changed 59 if (m_pid_stop_count == count) 60 return false; // Didn't change 61 m_pid_stop_count = count; 62 return true; // The stop count has changed. 63 } 64 65 bool ProcessStateRunning() const; Events()66 PThreadEvent &Events() { return m_events; } AllEventBits()67 nub_event_t AllEventBits() const { return all_event_bits; } NormalEventBits()68 nub_event_t NormalEventBits() const { return normal_event_bits; } StickyEventBits()69 nub_event_t StickyEventBits() const { return sticky_event_bits; } 70 const char *EventsAsString(nub_event_t events, std::string &s); 71 ArgumentCount()72 size_t ArgumentCount() const { return m_arg_vec.size(); } 73 const char *ArgumentAtIndex(size_t index); PushArgument(const char * arg)74 void PushArgument(const char *arg) { 75 if (arg) 76 m_arg_vec.push_back(arg); 77 } ClearArgv()78 void ClearArgv() { m_arg_vec.erase(m_arg_vec.begin(), m_arg_vec.end()); } 79 EnvironmentCount()80 size_t EnvironmentCount() const { return m_env_vec.size(); } 81 const char *EnvironmentAtIndex(size_t index); PushEnvironment(const char * arg)82 void PushEnvironment(const char *arg) { 83 if (arg) 84 m_env_vec.push_back(arg); 85 } 86 void PushEnvironmentIfNeeded(const char *arg); ClearEnvironment()87 void ClearEnvironment() { 88 m_env_vec.erase(m_env_vec.begin(), m_env_vec.end()); 89 } LaunchStatus()90 DNBError &LaunchStatus() { return m_launch_status; } 91 const char *LaunchStatusAsString(std::string &s); LaunchFlavor()92 nub_launch_flavor_t LaunchFlavor() const { return m_launch_flavor; } SetLaunchFlavor(nub_launch_flavor_t flavor)93 void SetLaunchFlavor(nub_launch_flavor_t flavor) { m_launch_flavor = flavor; } 94 GetWorkingDirectory()95 const char *GetWorkingDirectory() const { 96 if (!m_working_directory.empty()) 97 return m_working_directory.c_str(); 98 return NULL; 99 } 100 101 bool SetWorkingDirectory(const char *path); 102 GetSTDIN()103 std::string &GetSTDIN() { return m_stdin; } GetSTDOUT()104 std::string &GetSTDOUT() { return m_stdout; } GetSTDERR()105 std::string &GetSTDERR() { return m_stderr; } GetWorkingDir()106 std::string &GetWorkingDir() { return m_working_dir; } 107 GetSTDINPath()108 const char *GetSTDINPath() { 109 return m_stdin.empty() ? NULL : m_stdin.c_str(); 110 } GetSTDOUTPath()111 const char *GetSTDOUTPath() { 112 return m_stdout.empty() ? NULL : m_stdout.c_str(); 113 } GetSTDERRPath()114 const char *GetSTDERRPath() { 115 return m_stderr.empty() ? NULL : m_stderr.c_str(); 116 } GetWorkingDirPath()117 const char *GetWorkingDirPath() { 118 return m_working_dir.empty() ? NULL : m_working_dir.c_str(); 119 } 120 PushProcessEvent(const char * p)121 void PushProcessEvent(const char *p) { m_process_event.assign(p); } GetProcessEvent()122 const char *GetProcessEvent() { return m_process_event.c_str(); } 123 SetDetachOnError(bool detach)124 void SetDetachOnError(bool detach) { m_detach_on_error = detach; } GetDetachOnError()125 bool GetDetachOnError() { return m_detach_on_error; } 126 SetUnmaskSignals(bool unmask_signals)127 void SetUnmaskSignals(bool unmask_signals) { 128 m_unmask_signals = unmask_signals; 129 } GetUnmaskSignals()130 bool GetUnmaskSignals() { return m_unmask_signals; } 131 132 protected: 133 // Classes that inherit from RNBContext can see and modify these 134 nub_process_t m_pid; 135 std::string m_stdin; 136 std::string m_stdout; 137 std::string m_stderr; 138 std::string m_working_dir; 139 nub_size_t m_pid_stop_count; 140 PThreadEvent m_events; // Threaded events that we can wait for 141 pthread_t m_pid_pthread; 142 nub_launch_flavor_t m_launch_flavor; // How to launch our inferior process 143 DNBError 144 m_launch_status; // This holds the status from the last launch attempt. 145 std::vector<std::string> m_arg_vec; 146 std::vector<std::string> 147 m_env_vec; // This will be unparsed - entries FOO=value 148 std::string m_working_directory; 149 std::string m_process_event; 150 bool m_detach_on_error; 151 152 void StartProcessStatusThread(); 153 void StopProcessStatusThread(); 154 static void *ThreadFunctionProcessStatus(void *arg); 155 bool m_unmask_signals; 156 157 private: 158 RNBContext(const RNBContext &rhs) = delete; 159 RNBContext &operator=(const RNBContext &rhs) = delete; 160 }; 161 162 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBCONTEXT_H 163