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