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