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