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