blob: 4b5f5ae2cc95654d5a89148f6347c49667c16f1a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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; }
98protected:
99 //------------------------------------------------------------------
100 // Classes that inherit from RNBContext can see and modify these
101 //------------------------------------------------------------------
102 nub_process_t m_pid;
103 nub_size_t m_pid_stop_count;
104 PThreadEvent m_events; // Threaded events that we can wait for
105 pthread_t m_pid_pthread;
106 nub_launch_flavor_t m_launch_flavor; // How to launch our inferior process
107 DNBError m_launch_status; // This holds the status from the last launch attempt.
108 std::vector<std::string> m_arg_vec;
109 std::vector<std::string> m_env_vec; // This will be unparsed - entries FOO=value
110
111 void StartProcessStatusThread();
112 void StopProcessStatusThread();
113 static void* ThreadFunctionProcessStatus(void *arg);
114
115private:
116 //------------------------------------------------------------------
117 // Outlaw copy and assignment operators
118 //------------------------------------------------------------------
119 RNBContext(const RNBContext& rhs);
120 RNBContext& operator=(const RNBContext& rhs);
121};
122
123#endif // #ifndef __RNBContext_h__