blob: 188e2a20975c925af18706adcdba61f25471a6e4 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- IOChannel.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#ifndef lldb_IOChannel_h_
11#define lldb_IOChannel_h_
12
13#include <string>
14#include <queue>
15
16#include <editline/readline.h>
17#include <histedit.h>
Caroline Tice757500e2010-09-29 18:35:42 +000018#include <pthread.h>
19#include <sys/time.h>
Chris Lattner24943d22010-06-08 16:52:24 +000020
21#include "Driver.h"
22
23class IOChannel : public lldb::SBBroadcaster
24{
25public:
26 enum {
27 eBroadcastBitHasUserInput = (1 << 0),
28 eBroadcastBitUserInterrupt = (1 << 1),
29 eBroadcastBitThreadShouldExit = (1 << 2),
30 eBroadcastBitThreadDidExit = (1 << 3),
31 eBroadcastBitThreadDidStart = (1 << 4),
32 eBroadcastBitsSTDOUT = (1 << 5),
33 eBroadcastBitsSTDERR = (1 << 6),
34 eBroadcastBitsSTDIN = (1 << 7),
35 eAllEventBits = 0xffffffff
36 };
37
38 IOChannel (FILE *in,
39 FILE *out,
40 FILE *err,
41 Driver *driver = NULL);
42
43 virtual
44 ~IOChannel ();
45
46 bool
47 Start ();
48
49 bool
50 Stop ();
51
52 static void *
53 IOReadThread (void *);
54
55 void
56 Run ();
57
58 void
59 OutWrite (const char *buffer, size_t len);
60
61 void
62 ErrWrite (const char *buffer, size_t len);
63
64 bool
65 LibeditGetInput (std::string &);
66
67 void
68 SetPrompt ();
69
70 void
71 RefreshPrompt ();
72
73 void
74 AddCommandToQueue (const char *command);
75
76 bool
77 GetCommandFromQueue (std::string &cmd);
78
79 int
80 CommandQueueSize () const;
81
82 void
83 ClearCommandQueue ();
84
85 bool
86 CommandQueueIsEmpty () const;
87
88 const char *
89 GetPrompt ();
90
Greg Clayton54e7afa2010-07-09 20:39:50 +000091 static unsigned char
92 ElCompletionFn (EditLine *e, int ch);
Chris Lattner24943d22010-06-08 16:52:24 +000093
Caroline Tice757500e2010-09-29 18:35:42 +000094protected:
95
Chris Lattner24943d22010-06-08 16:52:24 +000096 bool
97 IsGettingCommand () const;
98
Caroline Tice757500e2010-09-29 18:35:42 +000099 void
100 SetGettingCommand (bool new_value);
101
102 uint64_t
103 Nanoseconds (const struct timeval &time_val) const;
104
105 uint64_t
106 ElapsedNanoSecondsSinceEnteringElGets ();
107
Chris Lattner24943d22010-06-08 16:52:24 +0000108private:
109
Caroline Tice757500e2010-09-29 18:35:42 +0000110 pthread_mutex_t m_output_mutex;
111 struct timeval m_enter_elgets_time;
112
Chris Lattner24943d22010-06-08 16:52:24 +0000113 Driver *m_driver;
114 lldb::thread_t m_read_thread;
115 bool m_read_thread_should_exit;
116 FILE *m_out_file;
117 FILE *m_err_file;
118 std::queue<std::string> m_command_queue;
119 const char *m_completion_key;
120
121 EditLine *m_edit_line;
122 History *m_history;
123 HistEvent m_history_event;
124 bool m_getting_command;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000125
Chris Lattner24943d22010-06-08 16:52:24 +0000126 void
127 HistorySaveLoad (bool save);
Greg Clayton54e7afa2010-07-09 20:39:50 +0000128
129 unsigned char
130 HandleCompletion (EditLine *e, int ch);
Chris Lattner24943d22010-06-08 16:52:24 +0000131};
132
Caroline Tice757500e2010-09-29 18:35:42 +0000133class IOLocker
134{
135public:
136
137 IOLocker (pthread_mutex_t &mutex);
138
139 ~IOLocker ();
140
141protected:
142
143 pthread_mutex_t *m_mutex_ptr;
144
145private:
146
147 IOLocker (const IOLocker&);
148 const IOLocker& operator= (const IOLocker&);
149};
150
Chris Lattner24943d22010-06-08 16:52:24 +0000151#endif // lldb_IOChannel_h_