1 | /* $Id: VBoxDbgConsole.cpp 38814 2011-09-21 12:45:15Z 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 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DBGG
|
---|
23 | #include "VBoxDbgConsole.h"
|
---|
24 |
|
---|
25 | #include <QLabel>
|
---|
26 | #include <QApplication>
|
---|
27 | #include <QFont>
|
---|
28 | #include <QLineEdit>
|
---|
29 | #include <QHBoxLayout>
|
---|
30 | #include <QAction>
|
---|
31 | #include <QContextMenuEvent>
|
---|
32 |
|
---|
33 | #include <VBox/dbg.h>
|
---|
34 | #include <VBox/vmm/cfgm.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 |
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include <iprt/tcp.h>
|
---|
39 | #include <VBox/log.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/alloc.h>
|
---|
43 | #include <iprt/string.h>
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | /*
|
---|
49 | *
|
---|
50 | * V B o x D b g C o n s o l e O u t p u t
|
---|
51 | * V B o x D b g C o n s o l e O u t p u t
|
---|
52 | * V B o x D b g C o n s o l e O u t p u t
|
---|
53 | *
|
---|
54 | *
|
---|
55 | */
|
---|
56 |
|
---|
57 |
|
---|
58 | VBoxDbgConsoleOutput::VBoxDbgConsoleOutput(QWidget *pParent/* = NULL*/, const char *pszName/* = NULL*/)
|
---|
59 | : QTextEdit(pParent), m_uCurLine(0), m_uCurPos(0), m_hGUIThread(RTThreadNativeSelf())
|
---|
60 | {
|
---|
61 | setReadOnly(true);
|
---|
62 | setUndoRedoEnabled(false);
|
---|
63 | setOverwriteMode(false);
|
---|
64 | setPlainText("");
|
---|
65 | setTextInteractionFlags(Qt::TextBrowserInteraction);
|
---|
66 | setAutoFormatting(QTextEdit::AutoAll);
|
---|
67 | setTabChangesFocus(true);
|
---|
68 | setAcceptRichText(false);
|
---|
69 |
|
---|
70 | #ifdef Q_WS_MAC
|
---|
71 | QFont Font("Monaco", 10, QFont::Normal, FALSE);
|
---|
72 | Font.setStyleStrategy(QFont::NoAntialias);
|
---|
73 | #else
|
---|
74 | QFont Font = font();
|
---|
75 | Font.setStyleHint(QFont::TypeWriter);
|
---|
76 | Font.setFamily("Courier [Monotype]");
|
---|
77 | #endif
|
---|
78 | setFont(Font);
|
---|
79 |
|
---|
80 | /* green on black */
|
---|
81 | QPalette Pal(palette());
|
---|
82 | Pal.setColor(QPalette::All, QPalette::Base, QColor(Qt::black));
|
---|
83 | setPalette(Pal);
|
---|
84 | setTextColor(QColor(qRgb(0, 0xe0, 0)));
|
---|
85 | NOREF(pszName);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | VBoxDbgConsoleOutput::~VBoxDbgConsoleOutput()
|
---|
90 | {
|
---|
91 | Assert(m_hGUIThread == RTThreadNativeSelf());
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | void
|
---|
96 | VBoxDbgConsoleOutput::appendText(const QString &rStr, bool fClearSelection)
|
---|
97 | {
|
---|
98 | Assert(m_hGUIThread == RTThreadNativeSelf());
|
---|
99 |
|
---|
100 | if (rStr.isEmpty() || rStr.isNull() || !rStr.length())
|
---|
101 | return;
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Insert all in one go and make sure it's visible.
|
---|
105 | *
|
---|
106 | * We need to move the cursor and unselect any selected text before
|
---|
107 | * inserting anything, otherwise, text will disappear.
|
---|
108 | */
|
---|
109 | QTextCursor Cursor = textCursor();
|
---|
110 | if (!fClearSelection && Cursor.hasSelection())
|
---|
111 | {
|
---|
112 | QTextCursor SavedCursor = Cursor;
|
---|
113 | Cursor.clearSelection();
|
---|
114 | Cursor.movePosition(QTextCursor::End);
|
---|
115 |
|
---|
116 | Cursor.insertText(rStr);
|
---|
117 |
|
---|
118 | setTextCursor(SavedCursor);
|
---|
119 | }
|
---|
120 | else
|
---|
121 | {
|
---|
122 | if (Cursor.hasSelection())
|
---|
123 | Cursor.clearSelection();
|
---|
124 | if (!Cursor.atEnd())
|
---|
125 | Cursor.movePosition(QTextCursor::End);
|
---|
126 |
|
---|
127 | Cursor.insertText(rStr);
|
---|
128 |
|
---|
129 | setTextCursor(Cursor);
|
---|
130 | ensureCursorVisible();
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | /*
|
---|
138 | *
|
---|
139 | * V B o x D b g C o n s o l e I n p u t
|
---|
140 | * V B o x D b g C o n s o l e I n p u t
|
---|
141 | * V B o x D b g C o n s o l e I n p u t
|
---|
142 | *
|
---|
143 | *
|
---|
144 | */
|
---|
145 |
|
---|
146 |
|
---|
147 | VBoxDbgConsoleInput::VBoxDbgConsoleInput(QWidget *pParent/* = NULL*/, const char *pszName/* = NULL*/)
|
---|
148 | : QComboBox(pParent), m_iBlankItem(0), m_hGUIThread(RTThreadNativeSelf())
|
---|
149 | {
|
---|
150 | insertItem(m_iBlankItem, "");
|
---|
151 | setEditable(true);
|
---|
152 | setInsertPolicy(NoInsert);
|
---|
153 | setAutoCompletion(false);
|
---|
154 | setMaxCount(50);
|
---|
155 | const QLineEdit *pEdit = lineEdit();
|
---|
156 | if (pEdit)
|
---|
157 | connect(pEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
|
---|
158 |
|
---|
159 | NOREF(pszName);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | VBoxDbgConsoleInput::~VBoxDbgConsoleInput()
|
---|
164 | {
|
---|
165 | Assert(m_hGUIThread == RTThreadNativeSelf());
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | void
|
---|
170 | VBoxDbgConsoleInput::setLineEdit(QLineEdit *pEdit)
|
---|
171 | {
|
---|
172 | Assert(m_hGUIThread == RTThreadNativeSelf());
|
---|
173 | QComboBox::setLineEdit(pEdit);
|
---|
174 | if (lineEdit() == pEdit && pEdit)
|
---|
175 | connect(pEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void
|
---|
180 | VBoxDbgConsoleInput::returnPressed()
|
---|
181 | {
|
---|
182 | Assert(m_hGUIThread == RTThreadNativeSelf());
|
---|
183 | /* deal with the current command. */
|
---|
184 | QString Str = currentText();
|
---|
185 | emit commandSubmitted(Str);
|
---|
186 |
|
---|
187 | /* update the history and clear the entry field */
|
---|
188 | QString PrevStr = m_iBlankItem > 0 ? itemText(m_iBlankItem - 1) : "";
|
---|
189 | if (PrevStr != Str)
|
---|
190 | {
|
---|
191 | setItemText(m_iBlankItem, Str);
|
---|
192 | if ( m_iBlankItem > 0
|
---|
193 | && m_iBlankItem >= maxCount() - 1)
|
---|
194 | removeItem(m_iBlankItem - maxCount() - 1);
|
---|
195 | insertItem(++m_iBlankItem, "");
|
---|
196 | }
|
---|
197 |
|
---|
198 | clearEditText();
|
---|
199 | setCurrentIndex(m_iBlankItem);
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 |
|
---|
206 |
|
---|
207 | /*
|
---|
208 | *
|
---|
209 | * V B o x D b g C o n s o l e
|
---|
210 | * V B o x D b g C o n s o l e
|
---|
211 | * V B o x D b g C o n s o l e
|
---|
212 | *
|
---|
213 | *
|
---|
214 | */
|
---|
215 |
|
---|
216 |
|
---|
217 | VBoxDbgConsole::VBoxDbgConsole(VBoxDbgGui *a_pDbgGui, QWidget *a_pParent/* = NULL*/)
|
---|
218 | : VBoxDbgBaseWindow(a_pDbgGui, a_pParent), m_pOutput(NULL), m_pInput(NULL), m_fInputRestoreFocus(false),
|
---|
219 | m_pszInputBuf(NULL), m_cbInputBuf(0), m_cbInputBufAlloc(0),
|
---|
220 | m_pszOutputBuf(NULL), m_cbOutputBuf(0), m_cbOutputBufAlloc(0),
|
---|
221 | m_pTimer(NULL), m_fUpdatePending(false), m_Thread(NIL_RTTHREAD), m_EventSem(NIL_RTSEMEVENT),
|
---|
222 | m_fTerminate(false), m_fThreadTerminated(false)
|
---|
223 | {
|
---|
224 | setWindowTitle("VBoxDbg - Console");
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * Create the output text box.
|
---|
228 | */
|
---|
229 | m_pOutput = new VBoxDbgConsoleOutput(this);
|
---|
230 |
|
---|
231 | /* try figure a suitable size */
|
---|
232 | QLabel *pLabel = new QLabel( "11111111111111111111111111111111111111111111111111111111111111111111111111111112222222222", this);
|
---|
233 | pLabel->setFont(m_pOutput->font());
|
---|
234 | QSize Size = pLabel->sizeHint();
|
---|
235 | delete pLabel;
|
---|
236 | Size.setWidth((int)(Size.width() * 1.10));
|
---|
237 | Size.setHeight(Size.width() / 2);
|
---|
238 | resize(Size);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Create the input combo box (with a label).
|
---|
242 | */
|
---|
243 | QHBoxLayout *pLayout = new QHBoxLayout();
|
---|
244 | //pLayout->setSizeConstraint(QLayout::SetMaximumSize);
|
---|
245 |
|
---|
246 | pLabel = new QLabel(" Command ");
|
---|
247 | pLayout->addWidget(pLabel);
|
---|
248 | pLabel->setMaximumSize(pLabel->sizeHint());
|
---|
249 | pLabel->setAlignment(Qt::AlignCenter);
|
---|
250 |
|
---|
251 | m_pInput = new VBoxDbgConsoleInput(NULL);
|
---|
252 | pLayout->addWidget(m_pInput);
|
---|
253 | m_pInput->setDuplicatesEnabled(false);
|
---|
254 | connect(m_pInput, SIGNAL(commandSubmitted(const QString &)), this, SLOT(commandSubmitted(const QString &)));
|
---|
255 |
|
---|
256 | # if 0//def Q_WS_MAC
|
---|
257 | pLabel = new QLabel(" ");
|
---|
258 | pLayout->addWidget(pLabel);
|
---|
259 | pLabel->setMaximumSize(20, m_pInput->sizeHint().height() + 6);
|
---|
260 | pLabel->setMinimumSize(20, m_pInput->sizeHint().height() + 6);
|
---|
261 | # endif
|
---|
262 |
|
---|
263 | QWidget *pHBox = new QWidget(this);
|
---|
264 | pHBox->setLayout(pLayout);
|
---|
265 |
|
---|
266 | m_pInput->setEnabled(false); /* (we'll get a ready notification) */
|
---|
267 |
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * Vertical layout box on the whole widget.
|
---|
271 | */
|
---|
272 | QVBoxLayout *pVLayout = new QVBoxLayout();
|
---|
273 | pVLayout->setContentsMargins(0, 0, 0, 0);
|
---|
274 | pVLayout->setSpacing(5);
|
---|
275 | pVLayout->addWidget(m_pOutput);
|
---|
276 | pVLayout->addWidget(pHBox);
|
---|
277 | setLayout(pVLayout);
|
---|
278 |
|
---|
279 | /*
|
---|
280 | * The tab order is from input to output, not the other way around as it is by default.
|
---|
281 | */
|
---|
282 | setTabOrder(m_pInput, m_pOutput);
|
---|
283 | m_fInputRestoreFocus = true; /* hack */
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Setup the timer.
|
---|
287 | */
|
---|
288 | m_pTimer = new QTimer(this);
|
---|
289 | connect(m_pTimer, SIGNAL(timeout()), SLOT(updateOutput()));
|
---|
290 |
|
---|
291 | /*
|
---|
292 | * Init the backend structure.
|
---|
293 | */
|
---|
294 | m_Back.Core.pfnInput = backInput;
|
---|
295 | m_Back.Core.pfnRead = backRead;
|
---|
296 | m_Back.Core.pfnWrite = backWrite;
|
---|
297 | m_Back.Core.pfnSetReady = backSetReady;
|
---|
298 | m_Back.pSelf = this;
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Create the critical section, the event semaphore and the debug console thread.
|
---|
302 | */
|
---|
303 | int rc = RTCritSectInit(&m_Lock);
|
---|
304 | AssertRC(rc);
|
---|
305 |
|
---|
306 | rc = RTSemEventCreate(&m_EventSem);
|
---|
307 | AssertRC(rc);
|
---|
308 |
|
---|
309 | rc = RTThreadCreate(&m_Thread, backThread, this, 0, RTTHREADTYPE_DEBUGGER, RTTHREADFLAGS_WAITABLE, "VBoxDbgC");
|
---|
310 | AssertRC(rc);
|
---|
311 | if (RT_FAILURE(rc))
|
---|
312 | m_Thread = NIL_RTTHREAD;
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Shortcuts.
|
---|
316 | */
|
---|
317 | m_pFocusToInput = new QAction("", this);
|
---|
318 | m_pFocusToInput->setShortcut(QKeySequence("Ctrl+L"));
|
---|
319 | addAction(m_pFocusToInput);
|
---|
320 | connect(m_pFocusToInput, SIGNAL(triggered(bool)), this, SLOT(actFocusToInput()));
|
---|
321 |
|
---|
322 | m_pFocusToOutput = new QAction("", this);
|
---|
323 | m_pFocusToOutput->setShortcut(QKeySequence("Ctrl+O"));
|
---|
324 | addAction(m_pFocusToOutput);
|
---|
325 | connect(m_pFocusToOutput, SIGNAL(triggered(bool)), this, SLOT(actFocusToOutput()));
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | VBoxDbgConsole::~VBoxDbgConsole()
|
---|
330 | {
|
---|
331 | Assert(isGUIThread());
|
---|
332 |
|
---|
333 | /*
|
---|
334 | * Wait for the thread.
|
---|
335 | */
|
---|
336 | ASMAtomicWriteBool(&m_fTerminate, true);
|
---|
337 | RTSemEventSignal(m_EventSem);
|
---|
338 | if (m_Thread != NIL_RTTHREAD)
|
---|
339 | {
|
---|
340 | int rc = RTThreadWait(m_Thread, 15000, NULL);
|
---|
341 | AssertRC(rc);
|
---|
342 | m_Thread = NIL_RTTHREAD;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /*
|
---|
346 | * Free resources.
|
---|
347 | */
|
---|
348 | delete m_pTimer;
|
---|
349 | m_pTimer = NULL;
|
---|
350 | RTCritSectDelete(&m_Lock);
|
---|
351 | RTSemEventDestroy(m_EventSem);
|
---|
352 | m_EventSem = 0;
|
---|
353 | m_pOutput = NULL;
|
---|
354 | m_pInput = NULL;
|
---|
355 | if (m_pszInputBuf)
|
---|
356 | {
|
---|
357 | RTMemFree(m_pszInputBuf);
|
---|
358 | m_pszInputBuf = NULL;
|
---|
359 | }
|
---|
360 | m_cbInputBuf = 0;
|
---|
361 | m_cbInputBufAlloc = 0;
|
---|
362 |
|
---|
363 | delete m_pFocusToInput;
|
---|
364 | m_pFocusToInput = NULL;
|
---|
365 | delete m_pFocusToOutput;
|
---|
366 | m_pFocusToOutput = NULL;
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | void
|
---|
371 | VBoxDbgConsole::commandSubmitted(const QString &rCommand)
|
---|
372 | {
|
---|
373 | Assert(isGUIThread());
|
---|
374 |
|
---|
375 | lock();
|
---|
376 | RTSemEventSignal(m_EventSem);
|
---|
377 |
|
---|
378 | QByteArray Utf8Array = rCommand.toUtf8();
|
---|
379 | const char *psz = Utf8Array.constData();
|
---|
380 | size_t cb = strlen(psz);
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Make sure we've got space for the input.
|
---|
384 | */
|
---|
385 | if (cb + m_cbInputBuf >= m_cbInputBufAlloc)
|
---|
386 | {
|
---|
387 | size_t cbNew = RT_ALIGN_Z(cb + m_cbInputBufAlloc + 1, 128);
|
---|
388 | void *pv = RTMemRealloc(m_pszInputBuf, cbNew);
|
---|
389 | if (!pv)
|
---|
390 | {
|
---|
391 | unlock();
|
---|
392 | return;
|
---|
393 | }
|
---|
394 | m_pszInputBuf = (char *)pv;
|
---|
395 | m_cbInputBufAlloc = cbNew;
|
---|
396 | }
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Add the input and output it.
|
---|
400 | */
|
---|
401 | memcpy(m_pszInputBuf + m_cbInputBuf, psz, cb);
|
---|
402 | m_cbInputBuf += cb;
|
---|
403 | m_pszInputBuf[m_cbInputBuf++] = '\n';
|
---|
404 |
|
---|
405 | m_pOutput->appendText(rCommand + "\n", true /*fClearSelection*/);
|
---|
406 | m_pOutput->ensureCursorVisible();
|
---|
407 |
|
---|
408 | m_fInputRestoreFocus = m_pInput->hasFocus(); /* dirty focus hack */
|
---|
409 | m_pInput->setEnabled(false);
|
---|
410 |
|
---|
411 | Log(("VBoxDbgConsole::commandSubmitted: %s (input-enabled=%RTbool)\n", psz, m_pInput->isEnabled()));
|
---|
412 | unlock();
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | void
|
---|
417 | VBoxDbgConsole::updateOutput()
|
---|
418 | {
|
---|
419 | Assert(isGUIThread());
|
---|
420 |
|
---|
421 | lock();
|
---|
422 | m_fUpdatePending = false;
|
---|
423 | if (m_cbOutputBuf)
|
---|
424 | {
|
---|
425 | m_pOutput->appendText(QString::fromUtf8((const char *)m_pszOutputBuf, (int)m_cbOutputBuf), false /*fClearSelection*/);
|
---|
426 | m_cbOutputBuf = 0;
|
---|
427 | }
|
---|
428 | unlock();
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Lock the object.
|
---|
434 | */
|
---|
435 | void
|
---|
436 | VBoxDbgConsole::lock()
|
---|
437 | {
|
---|
438 | RTCritSectEnter(&m_Lock);
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Unlocks the object.
|
---|
444 | */
|
---|
445 | void
|
---|
446 | VBoxDbgConsole::unlock()
|
---|
447 | {
|
---|
448 | RTCritSectLeave(&m_Lock);
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Checks if there is input.
|
---|
455 | *
|
---|
456 | * @returns true if there is input ready.
|
---|
457 | * @returns false if there not input ready.
|
---|
458 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
459 | * @param cMillies Number of milliseconds to wait on input data.
|
---|
460 | */
|
---|
461 | /*static*/ DECLCALLBACK(bool)
|
---|
462 | VBoxDbgConsole::backInput(PDBGCBACK pBack, uint32_t cMillies)
|
---|
463 | {
|
---|
464 | VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
|
---|
465 | pThis->lock();
|
---|
466 |
|
---|
467 | bool fRc = true;
|
---|
468 | if (!pThis->m_cbInputBuf)
|
---|
469 | {
|
---|
470 | /*
|
---|
471 | * Wait outside the lock for the requested time, then check again.
|
---|
472 | */
|
---|
473 | pThis->unlock();
|
---|
474 | RTSemEventWait(pThis->m_EventSem, cMillies);
|
---|
475 | pThis->lock();
|
---|
476 | fRc = pThis->m_cbInputBuf
|
---|
477 | || ASMAtomicUoReadBool(&pThis->m_fTerminate);
|
---|
478 | }
|
---|
479 |
|
---|
480 | pThis->unlock();
|
---|
481 | return fRc;
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | /**
|
---|
486 | * Read input.
|
---|
487 | *
|
---|
488 | * @returns VBox status code.
|
---|
489 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
490 | * @param pvBuf Where to put the bytes we read.
|
---|
491 | * @param cbBuf Maximum nymber of bytes to read.
|
---|
492 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
493 | * If NULL the entire buffer must be filled for a
|
---|
494 | * successful return.
|
---|
495 | */
|
---|
496 | /*static*/ DECLCALLBACK(int)
|
---|
497 | VBoxDbgConsole::backRead(PDBGCBACK pBack, void *pvBuf, size_t cbBuf, size_t *pcbRead)
|
---|
498 | {
|
---|
499 | VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
|
---|
500 | Assert(pcbRead); /** @todo implement this bit */
|
---|
501 | if (pcbRead)
|
---|
502 | *pcbRead = 0;
|
---|
503 |
|
---|
504 | pThis->lock();
|
---|
505 | int rc = VINF_SUCCESS;
|
---|
506 | if (!ASMAtomicUoReadBool(&pThis->m_fTerminate))
|
---|
507 | {
|
---|
508 | if (pThis->m_cbInputBuf)
|
---|
509 | {
|
---|
510 | const char *psz = pThis->m_pszInputBuf;
|
---|
511 | size_t cbRead = RT_MIN(pThis->m_cbInputBuf, cbBuf);
|
---|
512 | memcpy(pvBuf, psz, cbRead);
|
---|
513 | psz += cbRead;
|
---|
514 | pThis->m_cbInputBuf -= cbRead;
|
---|
515 | if (*psz)
|
---|
516 | memmove(pThis->m_pszInputBuf, psz, pThis->m_cbInputBuf);
|
---|
517 | pThis->m_pszInputBuf[pThis->m_cbInputBuf] = '\0';
|
---|
518 | *pcbRead = cbRead;
|
---|
519 | }
|
---|
520 | }
|
---|
521 | else
|
---|
522 | rc = VERR_GENERAL_FAILURE;
|
---|
523 | pThis->unlock();
|
---|
524 | return rc;
|
---|
525 | }
|
---|
526 |
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Write (output).
|
---|
530 | *
|
---|
531 | * @returns VBox status code.
|
---|
532 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
533 | * @param pvBuf What to write.
|
---|
534 | * @param cbBuf Number of bytes to write.
|
---|
535 | * @param pcbWritten Where to store the number of bytes actually written.
|
---|
536 | * If NULL the entire buffer must be successfully written.
|
---|
537 | */
|
---|
538 | /*static*/ DECLCALLBACK(int)
|
---|
539 | VBoxDbgConsole::backWrite(PDBGCBACK pBack, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
|
---|
540 | {
|
---|
541 | VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
|
---|
542 | int rc = VINF_SUCCESS;
|
---|
543 |
|
---|
544 | pThis->lock();
|
---|
545 | if (cbBuf + pThis->m_cbOutputBuf >= pThis->m_cbOutputBufAlloc)
|
---|
546 | {
|
---|
547 | size_t cbNew = RT_ALIGN_Z(cbBuf + pThis->m_cbOutputBufAlloc + 1, 1024);
|
---|
548 | void *pv = RTMemRealloc(pThis->m_pszOutputBuf, cbNew);
|
---|
549 | if (!pv)
|
---|
550 | {
|
---|
551 | pThis->unlock();
|
---|
552 | if (pcbWritten)
|
---|
553 | *pcbWritten = 0;
|
---|
554 | return VERR_NO_MEMORY;
|
---|
555 | }
|
---|
556 | pThis->m_pszOutputBuf = (char *)pv;
|
---|
557 | pThis->m_cbOutputBufAlloc = cbNew;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /*
|
---|
561 | * Add the output.
|
---|
562 | */
|
---|
563 | memcpy(pThis->m_pszOutputBuf + pThis->m_cbOutputBuf, pvBuf, cbBuf);
|
---|
564 | pThis->m_cbOutputBuf += cbBuf;
|
---|
565 | pThis->m_pszOutputBuf[pThis->m_cbOutputBuf] = '\0';
|
---|
566 | if (pcbWritten)
|
---|
567 | *pcbWritten = cbBuf;
|
---|
568 |
|
---|
569 | if (ASMAtomicUoReadBool(&pThis->m_fTerminate))
|
---|
570 | rc = VERR_GENERAL_FAILURE;
|
---|
571 |
|
---|
572 | /*
|
---|
573 | * Tell the GUI thread to draw this text.
|
---|
574 | * We cannot do it from here without frequent crashes.
|
---|
575 | */
|
---|
576 | if (!pThis->m_fUpdatePending)
|
---|
577 | QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(VBoxDbgConsoleEvent::kUpdate));
|
---|
578 |
|
---|
579 | pThis->unlock();
|
---|
580 |
|
---|
581 | return rc;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | /*static*/ DECLCALLBACK(void)
|
---|
586 | VBoxDbgConsole::backSetReady(PDBGCBACK pBack, bool fReady)
|
---|
587 | {
|
---|
588 | VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
|
---|
589 | if (fReady)
|
---|
590 | QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(VBoxDbgConsoleEvent::kInputEnable));
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * The Debugger Console Thread
|
---|
596 | *
|
---|
597 | * @returns VBox status code (ignored).
|
---|
598 | * @param Thread The thread handle.
|
---|
599 | * @param pvUser Pointer to the VBoxDbgConsole object.s
|
---|
600 | */
|
---|
601 | /*static*/ DECLCALLBACK(int)
|
---|
602 | VBoxDbgConsole::backThread(RTTHREAD Thread, void *pvUser)
|
---|
603 | {
|
---|
604 | VBoxDbgConsole *pThis = (VBoxDbgConsole *)pvUser;
|
---|
605 | LogFlow(("backThread: Thread=%p pvUser=%p\n", (void *)Thread, pvUser));
|
---|
606 |
|
---|
607 | NOREF(Thread);
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * Create and execute the console.
|
---|
611 | */
|
---|
612 | int rc = pThis->dbgcCreate(&pThis->m_Back.Core, 0);
|
---|
613 |
|
---|
614 | ASMAtomicUoWriteBool(&pThis->m_fThreadTerminated, true);
|
---|
615 | if (!ASMAtomicUoReadBool(&pThis->m_fTerminate))
|
---|
616 | QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(rc == VINF_SUCCESS
|
---|
617 | ? VBoxDbgConsoleEvent::kTerminatedUser
|
---|
618 | : VBoxDbgConsoleEvent::kTerminatedOther));
|
---|
619 | LogFlow(("backThread: returns %Rrc (m_fTerminate=%RTbool)\n", rc, ASMAtomicUoReadBool(&pThis->m_fTerminate)));
|
---|
620 | return rc;
|
---|
621 | }
|
---|
622 |
|
---|
623 |
|
---|
624 | bool
|
---|
625 | VBoxDbgConsole::event(QEvent *pGenEvent)
|
---|
626 | {
|
---|
627 | Assert(isGUIThread());
|
---|
628 | if (pGenEvent->type() == (QEvent::Type)VBoxDbgConsoleEvent::kEventNumber)
|
---|
629 | {
|
---|
630 | VBoxDbgConsoleEvent *pEvent = (VBoxDbgConsoleEvent *)pGenEvent;
|
---|
631 |
|
---|
632 | switch (pEvent->command())
|
---|
633 | {
|
---|
634 | /* make update pending. */
|
---|
635 | case VBoxDbgConsoleEvent::kUpdate:
|
---|
636 | lock();
|
---|
637 | if (!m_fUpdatePending)
|
---|
638 | {
|
---|
639 | m_fUpdatePending = true;
|
---|
640 | m_pTimer->setSingleShot(true);
|
---|
641 | m_pTimer->start(10);
|
---|
642 | }
|
---|
643 | unlock();
|
---|
644 | break;
|
---|
645 |
|
---|
646 | /* Re-enable the input field and restore focus. */
|
---|
647 | case VBoxDbgConsoleEvent::kInputEnable:
|
---|
648 | Log(("VBoxDbgConsole: kInputEnable (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
|
---|
649 | m_pInput->setEnabled(true);
|
---|
650 | if ( m_fInputRestoreFocus
|
---|
651 | && !m_pInput->hasFocus())
|
---|
652 | m_pInput->setFocus(); /* this is a hack. */
|
---|
653 | m_fInputRestoreFocus = false;
|
---|
654 | break;
|
---|
655 |
|
---|
656 | /* The thread terminated by user command (exit, quit, bye). */
|
---|
657 | case VBoxDbgConsoleEvent::kTerminatedUser:
|
---|
658 | Log(("VBoxDbgConsole: kTerminatedUser (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
|
---|
659 | m_pInput->setEnabled(false);
|
---|
660 | close();
|
---|
661 | break;
|
---|
662 |
|
---|
663 | /* The thread terminated for some unknown reason., disable input */
|
---|
664 | case VBoxDbgConsoleEvent::kTerminatedOther:
|
---|
665 | Log(("VBoxDbgConsole: kTerminatedOther (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
|
---|
666 | m_pInput->setEnabled(false);
|
---|
667 | break;
|
---|
668 |
|
---|
669 | /* paranoia */
|
---|
670 | default:
|
---|
671 | AssertMsgFailed(("command=%d\n", pEvent->command()));
|
---|
672 | break;
|
---|
673 | }
|
---|
674 | return true;
|
---|
675 | }
|
---|
676 |
|
---|
677 | return VBoxDbgBaseWindow::event(pGenEvent);
|
---|
678 | }
|
---|
679 |
|
---|
680 |
|
---|
681 | void
|
---|
682 | VBoxDbgConsole::closeEvent(QCloseEvent *a_pCloseEvt)
|
---|
683 | {
|
---|
684 | if (m_fThreadTerminated)
|
---|
685 | {
|
---|
686 | a_pCloseEvt->accept();
|
---|
687 | delete this;
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | void
|
---|
693 | VBoxDbgConsole::actFocusToInput()
|
---|
694 | {
|
---|
695 | if (!m_pInput->hasFocus())
|
---|
696 | m_pInput->setFocus(Qt::ShortcutFocusReason);
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | void
|
---|
701 | VBoxDbgConsole::actFocusToOutput()
|
---|
702 | {
|
---|
703 | if (!m_pOutput->hasFocus())
|
---|
704 | m_pOutput->setFocus(Qt::ShortcutFocusReason);
|
---|
705 | }
|
---|
706 |
|
---|