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 46 RNBContext() = default; 47 virtual ~RNBContext(); 48 49 nub_process_t ProcessID() const { return m_pid; } 50 bool HasValidProcessID() const { return m_pid != INVALID_NUB_PROCESS; } 51 void SetProcessID(nub_process_t pid); 52 nub_size_t GetProcessStopCount() const { return m_pid_stop_count; } 53 bool SetProcessStopCount(nub_size_t count) { 54 // Returns true if this class' notion of the PID state changed 55 if (m_pid_stop_count == count) 56 return false; // Didn't change 57 m_pid_stop_count = count; 58 return true; // The stop count has changed. 59 } 60 61 bool ProcessStateRunning() const; 62 PThreadEvent &Events() { return m_events; } 63 nub_event_t AllEventBits() const { return all_event_bits; } 64 nub_event_t NormalEventBits() const { return normal_event_bits; } 65 nub_event_t StickyEventBits() const { return sticky_event_bits; } 66 const char *EventsAsString(nub_event_t events, std::string &s); 67 68 size_t ArgumentCount() const { return m_arg_vec.size(); } 69 const char *ArgumentAtIndex(size_t index); 70 void PushArgument(const char *arg) { 71 if (arg) 72 m_arg_vec.push_back(arg); 73 } 74 void ClearArgv() { m_arg_vec.erase(m_arg_vec.begin(), m_arg_vec.end()); } 75 76 size_t EnvironmentCount() const { return m_env_vec.size(); } 77 const char *EnvironmentAtIndex(size_t index); 78 void PushEnvironment(const char *arg) { 79 if (arg) 80 m_env_vec.push_back(arg); 81 } 82 void PushEnvironmentIfNeeded(const char *arg); 83 void ClearEnvironment() { 84 m_env_vec.erase(m_env_vec.begin(), m_env_vec.end()); 85 } 86 DNBError &LaunchStatus() { return m_launch_status; } 87 const char *LaunchStatusAsString(std::string &s); 88 nub_launch_flavor_t LaunchFlavor() const { return m_launch_flavor; } 89 void SetLaunchFlavor(nub_launch_flavor_t flavor) { m_launch_flavor = flavor; } 90 91 const char *GetWorkingDirectory() const { 92 if (!m_working_directory.empty()) 93 return m_working_directory.c_str(); 94 return NULL; 95 } 96 97 bool SetWorkingDirectory(const char *path); 98 99 std::string &GetSTDIN() { return m_stdin; } 100 std::string &GetSTDOUT() { return m_stdout; } 101 std::string &GetSTDERR() { return m_stderr; } 102 std::string &GetWorkingDir() { return m_working_dir; } 103 104 const char *GetSTDINPath() { 105 return m_stdin.empty() ? NULL : m_stdin.c_str(); 106 } 107 const char *GetSTDOUTPath() { 108 return m_stdout.empty() ? NULL : m_stdout.c_str(); 109 } 110 const char *GetSTDERRPath() { 111 return m_stderr.empty() ? NULL : m_stderr.c_str(); 112 } 113 const char *GetWorkingDirPath() { 114 return m_working_dir.empty() ? NULL : m_working_dir.c_str(); 115 } 116 117 void PushProcessEvent(const char *p) { m_process_event.assign(p); } 118 const char *GetProcessEvent() { return m_process_event.c_str(); } 119 120 void SetDetachOnError(bool detach) { m_detach_on_error = detach; } 121 bool GetDetachOnError() { return m_detach_on_error; } 122 123 void SetUnmaskSignals(bool unmask_signals) { 124 m_unmask_signals = unmask_signals; 125 } 126 bool GetUnmaskSignals() { return m_unmask_signals; } 127 128 protected: 129 // Classes that inherit from RNBContext can see and modify these 130 nub_process_t m_pid = INVALID_NUB_PROCESS; 131 std::string m_stdin; 132 std::string m_stdout; 133 std::string m_stderr; 134 std::string m_working_dir; 135 nub_size_t m_pid_stop_count = 0; 136 /// Threaded events that we can wait for. 137 PThreadEvent m_events{0, all_event_bits}; 138 pthread_t m_pid_pthread; 139 /// How to launch our inferior process. 140 nub_launch_flavor_t m_launch_flavor = eLaunchFlavorDefault; 141 /// This holds the status from the last launch attempt. 142 DNBError m_launch_status; 143 std::vector<std::string> m_arg_vec; 144 /// This will be unparsed entries FOO=value 145 std::vector<std::string> m_env_vec; 146 std::string m_working_directory; 147 std::string m_process_event; 148 bool m_detach_on_error = false; 149 bool m_unmask_signals = false; 150 151 void StartProcessStatusThread(); 152 void StopProcessStatusThread(); 153 static void *ThreadFunctionProcessStatus(void *arg); 154 155 private: 156 RNBContext(const RNBContext &rhs) = delete; 157 RNBContext &operator=(const RNBContext &rhs) = delete; 158 }; 159 160 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBCONTEXT_H 161