RNBContext.h revision 72e1c782ba1e4226da37af4722af608de9f39408
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
23class RNBContext
24{
25public:
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
108protected:
109    //------------------------------------------------------------------
110    // Classes that inherit from RNBContext can see and modify these
111    //------------------------------------------------------------------
112    nub_process_t   m_pid;
113    nub_size_t      m_pid_stop_count;
114    PThreadEvent    m_events;       // Threaded events that we can wait for
115    pthread_t       m_pid_pthread;
116    nub_launch_flavor_t m_launch_flavor;    // How to launch our inferior process
117    DNBError        m_launch_status;    // This holds the status from the last launch attempt.
118    std::vector<std::string> m_arg_vec;
119    std::vector<std::string> m_env_vec; // This will be unparsed - entries FOO=value
120    std::string     m_working_directory;
121
122    void    StartProcessStatusThread();
123    void    StopProcessStatusThread();
124    static void* ThreadFunctionProcessStatus(void *arg);
125
126private:
127    //------------------------------------------------------------------
128    // Outlaw copy and assignment operators
129    //------------------------------------------------------------------
130    RNBContext(const RNBContext& rhs);
131    RNBContext& operator=(const RNBContext& rhs);
132};
133
134#endif // #ifndef __RNBContext_h__
135