1 | /* $Id: VBoxDbgConsole.h 43324 2012-09-13 21:33:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Debugger GUI - Console.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ___Debugger_VBoxDbgConsole_h
|
---|
19 | #define ___Debugger_VBoxDbgConsole_h
|
---|
20 |
|
---|
21 | #include "VBoxDbgBase.h"
|
---|
22 |
|
---|
23 | #include <QTextEdit>
|
---|
24 | #include <QComboBox>
|
---|
25 | #include <QTimer>
|
---|
26 | #include <QEvent>
|
---|
27 |
|
---|
28 | #include <iprt/critsect.h>
|
---|
29 | #include <iprt/semaphore.h>
|
---|
30 | #include <iprt/thread.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | class VBoxDbgConsoleOutput : public QTextEdit
|
---|
34 | {
|
---|
35 | Q_OBJECT
|
---|
36 |
|
---|
37 | public:
|
---|
38 | /**
|
---|
39 | * Constructor.
|
---|
40 | *
|
---|
41 | * @param pParent Parent Widget.
|
---|
42 | * @param pszName Widget name.
|
---|
43 | */
|
---|
44 | VBoxDbgConsoleOutput(QWidget *pParent = NULL, const char *pszName = NULL);
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Destructor
|
---|
48 | */
|
---|
49 | virtual ~VBoxDbgConsoleOutput();
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Appends text.
|
---|
53 | * This differs from QTextEdit::append() in that it won't start on a new paragraph
|
---|
54 | * unless the previous char was a newline ('\n').
|
---|
55 | *
|
---|
56 | * @param rStr The text string to append.
|
---|
57 | * @param fClearSelection Whether to clear selected text before appending.
|
---|
58 | * If @c false the selection and window position
|
---|
59 | * are preserved.
|
---|
60 | */
|
---|
61 | virtual void appendText(const QString &rStr, bool fClearSelection);
|
---|
62 |
|
---|
63 | protected:
|
---|
64 | /** The current line (paragraph) number. */
|
---|
65 | unsigned m_uCurLine;
|
---|
66 | /** The position in the current line. */
|
---|
67 | unsigned m_uCurPos;
|
---|
68 | /** The handle to the GUI thread. */
|
---|
69 | RTNATIVETHREAD m_hGUIThread;
|
---|
70 | };
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * The Debugger Console Input widget.
|
---|
75 | *
|
---|
76 | * This is a combobox which only responds to <return>.
|
---|
77 | */
|
---|
78 | class VBoxDbgConsoleInput : public QComboBox
|
---|
79 | {
|
---|
80 | Q_OBJECT
|
---|
81 |
|
---|
82 | public:
|
---|
83 | /**
|
---|
84 | * Constructor.
|
---|
85 | *
|
---|
86 | * @param pParent Parent Widget.
|
---|
87 | * @param pszName Widget name.
|
---|
88 | */
|
---|
89 | VBoxDbgConsoleInput(QWidget *pParent = NULL, const char *pszName = NULL);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Destructor
|
---|
93 | */
|
---|
94 | virtual ~VBoxDbgConsoleInput();
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * We overload this method to get signaled upon returnPressed().
|
---|
98 | *
|
---|
99 | * See QComboBox::setLineEdit for full description.
|
---|
100 | * @param pEdit The new line edit widget.
|
---|
101 | * @remark This won't be called during the constructor.
|
---|
102 | */
|
---|
103 | virtual void setLineEdit(QLineEdit *pEdit);
|
---|
104 |
|
---|
105 | signals:
|
---|
106 | /**
|
---|
107 | * New command submitted.
|
---|
108 | */
|
---|
109 | void commandSubmitted(const QString &rCommand);
|
---|
110 |
|
---|
111 | private slots:
|
---|
112 | /**
|
---|
113 | * Returned was pressed.
|
---|
114 | *
|
---|
115 | * Will emit commandSubmitted().
|
---|
116 | */
|
---|
117 | void returnPressed();
|
---|
118 |
|
---|
119 | protected:
|
---|
120 | /** The handle to the GUI thread. */
|
---|
121 | RTNATIVETHREAD m_hGUIThread;
|
---|
122 | };
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * The Debugger Console.
|
---|
127 | */
|
---|
128 | class VBoxDbgConsole : public VBoxDbgBaseWindow
|
---|
129 | {
|
---|
130 | Q_OBJECT
|
---|
131 |
|
---|
132 | public:
|
---|
133 | /**
|
---|
134 | * Constructor.
|
---|
135 | *
|
---|
136 | * @param a_pDbgGui Pointer to the debugger gui object.
|
---|
137 | * @param a_pParent Parent Widget.
|
---|
138 | */
|
---|
139 | VBoxDbgConsole(VBoxDbgGui *a_pDbgGui, QWidget *a_pParent = NULL);
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Destructor
|
---|
143 | */
|
---|
144 | virtual ~VBoxDbgConsole();
|
---|
145 |
|
---|
146 | protected slots:
|
---|
147 | /**
|
---|
148 | * Handler called when a command is submitted.
|
---|
149 | * (Enter or return pressed in the combo box.)
|
---|
150 | *
|
---|
151 | * @param rCommand The submitted command.
|
---|
152 | */
|
---|
153 | void commandSubmitted(const QString &rCommand);
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Updates the output with what's currently in the output buffer.
|
---|
157 | * This is called by a timer or a User event posted by the debugger thread.
|
---|
158 | */
|
---|
159 | void updateOutput();
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Changes the focus to the input field.
|
---|
163 | */
|
---|
164 | void actFocusToInput();
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Changes the focus to the output viewer widget.
|
---|
168 | */
|
---|
169 | void actFocusToOutput();
|
---|
170 |
|
---|
171 | protected:
|
---|
172 | /**
|
---|
173 | * Override the closeEvent so we can choose delete the window when
|
---|
174 | * it is closed.
|
---|
175 | *
|
---|
176 | * @param a_pCloseEvt The close event.
|
---|
177 | */
|
---|
178 | virtual void closeEvent(QCloseEvent *a_pCloseEvt);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Lock the object.
|
---|
182 | */
|
---|
183 | void lock();
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Unlocks the object.
|
---|
187 | */
|
---|
188 | void unlock();
|
---|
189 |
|
---|
190 | protected:
|
---|
191 | /** @name Debug Console Backend.
|
---|
192 | * @{
|
---|
193 | */
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Checks if there is input.
|
---|
198 | *
|
---|
199 | * @returns true if there is input ready.
|
---|
200 | * @returns false if there not input ready.
|
---|
201 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
202 | * @param cMillies Number of milliseconds to wait on input data.
|
---|
203 | */
|
---|
204 | static DECLCALLBACK(bool) backInput(PDBGCBACK pBack, uint32_t cMillies);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Read input.
|
---|
208 | *
|
---|
209 | * @returns VBox status code.
|
---|
210 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
211 | * @param pvBuf Where to put the bytes we read.
|
---|
212 | * @param cbBuf Maximum nymber of bytes to read.
|
---|
213 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
214 | * If NULL the entire buffer must be filled for a
|
---|
215 | * successful return.
|
---|
216 | */
|
---|
217 | static DECLCALLBACK(int) backRead(PDBGCBACK pBack, void *pvBuf, size_t cbBuf, size_t *pcbRead);
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Write (output).
|
---|
221 | *
|
---|
222 | * @returns VBox status code.
|
---|
223 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
224 | * @param pvBuf What to write.
|
---|
225 | * @param cbBuf Number of bytes to write.
|
---|
226 | * @param pcbWritten Where to store the number of bytes actually written.
|
---|
227 | * If NULL the entire buffer must be successfully written.
|
---|
228 | */
|
---|
229 | static DECLCALLBACK(int) backWrite(PDBGCBACK pBack, const void *pvBuf, size_t cbBuf, size_t *pcbWritten);
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * @copydoc FNDBGCBACKSETREADY
|
---|
233 | */
|
---|
234 | static DECLCALLBACK(void) backSetReady(PDBGCBACK pBack, bool fReady);
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * The Debugger Console Thread
|
---|
238 | *
|
---|
239 | * @returns VBox status code (ignored).
|
---|
240 | * @param Thread The thread handle.
|
---|
241 | * @param pvUser Pointer to the VBoxDbgConsole object.s
|
---|
242 | */
|
---|
243 | static DECLCALLBACK(int) backThread(RTTHREAD Thread, void *pvUser);
|
---|
244 |
|
---|
245 | /** @} */
|
---|
246 |
|
---|
247 | protected:
|
---|
248 | /**
|
---|
249 | * Processes GUI command posted by the console thread.
|
---|
250 | *
|
---|
251 | * Qt3 isn't thread safe on any platform, meaning there is no locking, so, as
|
---|
252 | * a result we have to be very careful. All operations on objects which we share
|
---|
253 | * with the main thread has to be posted to it so it can perform it.
|
---|
254 | */
|
---|
255 | bool event(QEvent *pEvent);
|
---|
256 |
|
---|
257 | protected:
|
---|
258 | /** The output widget. */
|
---|
259 | VBoxDbgConsoleOutput *m_pOutput;
|
---|
260 | /** The input widget. */
|
---|
261 | VBoxDbgConsoleInput *m_pInput;
|
---|
262 | /** A hack to restore focus to the combobox after a command execution. */
|
---|
263 | bool m_fInputRestoreFocus;
|
---|
264 | /** The input buffer. */
|
---|
265 | char *m_pszInputBuf;
|
---|
266 | /** The amount of input in the buffer. */
|
---|
267 | size_t m_cbInputBuf;
|
---|
268 | /** The allocated size of the buffer. */
|
---|
269 | size_t m_cbInputBufAlloc;
|
---|
270 |
|
---|
271 | /** The output buffer. */
|
---|
272 | char *m_pszOutputBuf;
|
---|
273 | /** The amount of output in the buffer. */
|
---|
274 | size_t m_cbOutputBuf;
|
---|
275 | /** The allocated size of the buffer. */
|
---|
276 | size_t m_cbOutputBufAlloc;
|
---|
277 | /** The timer object used to process output in a delayed fashion. */
|
---|
278 | QTimer *m_pTimer;
|
---|
279 | /** Set when an output update is pending. */
|
---|
280 | bool volatile m_fUpdatePending;
|
---|
281 |
|
---|
282 | /** The debugger console thread. */
|
---|
283 | RTTHREAD m_Thread;
|
---|
284 | /** The event semaphore used to signal the debug console thread about input. */
|
---|
285 | RTSEMEVENT m_EventSem;
|
---|
286 | /** The critical section used to lock the object. */
|
---|
287 | RTCRITSECT m_Lock;
|
---|
288 | /** When set the thread will cause the debug console thread to terminate. */
|
---|
289 | bool volatile m_fTerminate;
|
---|
290 | /** Has the thread terminated?
|
---|
291 | * Used to do the right thing in closeEvent; the console is dead if the
|
---|
292 | * thread has terminated. */
|
---|
293 | bool volatile m_fThreadTerminated;
|
---|
294 |
|
---|
295 | /** The debug console backend structure.
|
---|
296 | * Use VBOXDBGCONSOLE_FROM_DBGCBACK to convert the DBGCBACK pointer to a object pointer. */
|
---|
297 | struct VBoxDbgConsoleBack
|
---|
298 | {
|
---|
299 | DBGCBACK Core;
|
---|
300 | VBoxDbgConsole *pSelf;
|
---|
301 | } m_Back;
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Converts a pointer to VBoxDbgConsole::m_Back to VBoxDbgConsole pointer.
|
---|
305 | * @todo find a better way because offsetof is undefined on objects and g++ gets very noisy because of that.
|
---|
306 | */
|
---|
307 | # define VBOXDBGCONSOLE_FROM_DBGCBACK(pBack) ( ((struct VBoxDbgConsoleBack *)(pBack))->pSelf )
|
---|
308 |
|
---|
309 | /** Change focus to the input field. */
|
---|
310 | QAction *m_pFocusToInput;
|
---|
311 | /** Change focus to the output viewer widget. */
|
---|
312 | QAction *m_pFocusToOutput;
|
---|
313 | };
|
---|
314 |
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Simple event class for push certain operations over
|
---|
318 | * onto the GUI thread.
|
---|
319 | */
|
---|
320 | class VBoxDbgConsoleEvent : public QEvent
|
---|
321 | {
|
---|
322 | public:
|
---|
323 | typedef enum { kUpdate, kInputEnable, kTerminatedUser, kTerminatedOther } VBoxDbgConsoleEventType;
|
---|
324 | enum { kEventNumber = QEvent::User + 42 };
|
---|
325 |
|
---|
326 | VBoxDbgConsoleEvent(VBoxDbgConsoleEventType enmCommand)
|
---|
327 | : QEvent((QEvent::Type)kEventNumber), m_enmCommand(enmCommand)
|
---|
328 | {
|
---|
329 | }
|
---|
330 |
|
---|
331 | VBoxDbgConsoleEventType command() const
|
---|
332 | {
|
---|
333 | return m_enmCommand;
|
---|
334 | }
|
---|
335 |
|
---|
336 | private:
|
---|
337 | VBoxDbgConsoleEventType m_enmCommand;
|
---|
338 | };
|
---|
339 |
|
---|
340 |
|
---|
341 | #endif
|
---|
342 |
|
---|