VirtualBox

source: vbox/trunk/src/VBox/Debugger/VBoxDbgConsole.cpp@ 85416

Last change on this file since 85416 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.2 KB
Line 
1/* $Id: VBoxDbgConsole.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * VBox Debugger GUI - Console.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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#include "VBoxDbgGui.h"
25
26#include <QLabel>
27#include <QApplication>
28#include <QFont>
29#include <QLineEdit>
30#include <QHBoxLayout>
31#include <QAction>
32#include <QContextMenuEvent>
33#include <QMenu>
34
35#include <VBox/dbg.h>
36#include <VBox/vmm/cfgm.h>
37#include <iprt/errcore.h>
38
39#include <iprt/thread.h>
40#include <iprt/tcp.h>
41#include <VBox/log.h>
42#include <iprt/assert.h>
43#include <iprt/asm.h>
44#include <iprt/alloc.h>
45#include <iprt/string.h>
46
47#include <VBox/com/string.h>
48
49
50
51/*
52 *
53 * V B o x D b g C o n s o l e O u t p u t
54 * V B o x D b g C o n s o l e O u t p u t
55 * V B o x D b g C o n s o l e O u t p u t
56 *
57 *
58 */
59
60/*static*/ const uint32_t VBoxDbgConsoleOutput::s_uMinFontSize = 6;
61
62
63VBoxDbgConsoleOutput::VBoxDbgConsoleOutput(QWidget *pParent/* = NULL*/, IVirtualBox *a_pVirtualBox /* = NULL */,
64 const char *pszName/* = NULL*/)
65 : QTextEdit(pParent), m_uCurLine(0), m_uCurPos(0), m_hGUIThread(RTThreadNativeSelf()), m_pVirtualBox(a_pVirtualBox)
66{
67 setReadOnly(true);
68 setUndoRedoEnabled(false);
69 setOverwriteMode(false);
70 setPlainText("");
71 setTextInteractionFlags(Qt::TextBrowserInteraction);
72 setAutoFormatting(QTextEdit::AutoAll);
73 setTabChangesFocus(true);
74 setAcceptRichText(false);
75
76 /*
77 * Create actions for color-scheme menu items.
78 */
79 m_pGreenOnBlackAction = new QAction(tr("Green On Black"), this);
80 m_pGreenOnBlackAction->setCheckable(true);
81 m_pGreenOnBlackAction->setShortcut(Qt::ControlModifier + Qt::Key_1);
82 m_pGreenOnBlackAction->setData((int)kGreenOnBlack);
83 connect(m_pGreenOnBlackAction, SIGNAL(triggered()), this, SLOT(sltSelectColorScheme()));
84
85 m_pBlackOnWhiteAction = new QAction(tr("Black On White"), this);
86 m_pBlackOnWhiteAction->setCheckable(true);
87 m_pBlackOnWhiteAction->setShortcut(Qt::ControlModifier + Qt::Key_2);
88 m_pBlackOnWhiteAction->setData((int)kBlackOnWhite);
89 connect(m_pBlackOnWhiteAction, SIGNAL(triggered()), this, SLOT(sltSelectColorScheme()));
90
91 /* Create action group for grouping of exclusive color-scheme menu items. */
92 QActionGroup *pActionColorGroup = new QActionGroup(this);
93 pActionColorGroup->addAction(m_pGreenOnBlackAction);
94 pActionColorGroup->addAction(m_pBlackOnWhiteAction);
95 pActionColorGroup->setExclusive(true);
96
97 /*
98 * Create actions for font menu items.
99 */
100 m_pCourierFontAction = new QAction(tr("Courier"), this);
101 m_pCourierFontAction->setCheckable(true);
102 m_pCourierFontAction->setShortcut(Qt::ControlModifier + Qt::Key_D);
103 m_pCourierFontAction->setData((int)kFontType_Courier);
104 connect(m_pCourierFontAction, SIGNAL(triggered()), this, SLOT(sltSelectFontType()));
105
106 m_pMonospaceFontAction = new QAction(tr("Monospace"), this);
107 m_pMonospaceFontAction->setCheckable(true);
108 m_pMonospaceFontAction->setShortcut(Qt::ControlModifier + Qt::Key_M);
109 m_pMonospaceFontAction->setData((int)kFontType_Monospace);
110 connect(m_pMonospaceFontAction, SIGNAL(triggered()), this, SLOT(sltSelectFontType()));
111
112 /* Create action group for grouping of exclusive font menu items. */
113 QActionGroup *pActionFontGroup = new QActionGroup(this);
114 pActionFontGroup->addAction(m_pCourierFontAction);
115 pActionFontGroup->addAction(m_pMonospaceFontAction);
116 pActionFontGroup->setExclusive(true);
117
118 /*
119 * Create actions for font size menu.
120 */
121 uint32_t const uDefaultFontSize = font().pointSize();
122 m_pActionFontSizeGroup = new QActionGroup(this);
123 for (uint32_t i = 0; i < RT_ELEMENTS(m_apFontSizeActions); i++)
124 {
125 char szTitle[32];
126 RTStrPrintf(szTitle, sizeof(szTitle), s_uMinFontSize + i != uDefaultFontSize ? "%upt" : "%upt (default)",
127 s_uMinFontSize + i);
128 m_apFontSizeActions[i] = new QAction(tr(szTitle), this);
129 m_apFontSizeActions[i]->setCheckable(true);
130 m_apFontSizeActions[i]->setData(i + s_uMinFontSize);
131 connect(m_apFontSizeActions[i], SIGNAL(triggered()), this, SLOT(sltSelectFontSize()));
132 m_pActionFontSizeGroup->addAction(m_apFontSizeActions[i]);
133 }
134
135 /*
136 * Set the defaults (which syncs with the menu item checked state).
137 */
138 /* color scheme: */
139 com::Bstr bstrColor;
140 HRESULT hrc = m_pVirtualBox ? m_pVirtualBox->GetExtraData(com::Bstr("DbgConsole/ColorScheme").raw(), bstrColor.asOutParam()) : E_FAIL;
141 if ( SUCCEEDED(hrc)
142 && bstrColor.compareUtf8("blackonwhite", com::Bstr::CaseInsensitive) == 0)
143 setColorScheme(kBlackOnWhite, false /*fSaveIt*/);
144 else
145 setColorScheme(kGreenOnBlack, false /*fSaveIt*/);
146
147 /* font: */
148 com::Bstr bstrFont;
149 hrc = m_pVirtualBox ? m_pVirtualBox->GetExtraData(com::Bstr("DbgConsole/Font").raw(), bstrFont.asOutParam()) : E_FAIL;
150 if ( SUCCEEDED(hrc)
151 && bstrFont.compareUtf8("monospace", com::Bstr::CaseInsensitive) == 0)
152 setFontType(kFontType_Monospace, false /*fSaveIt*/);
153 else
154 setFontType(kFontType_Courier, false /*fSaveIt*/);
155
156 /* font size: */
157 com::Bstr bstrFontSize;
158 hrc = m_pVirtualBox ? m_pVirtualBox->GetExtraData(com::Bstr("DbgConsole/FontSize").raw(), bstrFontSize.asOutParam()) : E_FAIL;
159 if (SUCCEEDED(hrc))
160 {
161 com::Utf8Str strFontSize(bstrFontSize);
162 uint32_t uFontSizePrf = strFontSize.strip().toUInt32();
163 if ( uFontSizePrf - s_uMinFontSize < (uint32_t)RT_ELEMENTS(m_apFontSizeActions)
164 && uFontSizePrf != uDefaultFontSize)
165 setFontSize(uFontSizePrf, false /*fSaveIt*/);
166 }
167
168 NOREF(pszName);
169}
170
171
172VBoxDbgConsoleOutput::~VBoxDbgConsoleOutput()
173{
174 Assert(m_hGUIThread == RTThreadNativeSelf());
175 if (m_pVirtualBox)
176 {
177 m_pVirtualBox->Release();
178 m_pVirtualBox = NULL;
179 }
180}
181
182
183void
184VBoxDbgConsoleOutput::contextMenuEvent(QContextMenuEvent *pEvent)
185{
186 /*
187 * Create the context menu and add the menu items.
188 */
189 QMenu *pMenu = createStandardContextMenu();
190 pMenu->addSeparator();
191
192 QMenu *pColorMenu = pMenu->addMenu(tr("Co&lor Scheme"));
193 pColorMenu->addAction(m_pGreenOnBlackAction);
194 pColorMenu->addAction(m_pBlackOnWhiteAction);
195
196 QMenu *pFontMenu = pMenu->addMenu(tr("&Font Family"));
197 pFontMenu->addAction(m_pCourierFontAction);
198 pFontMenu->addAction(m_pMonospaceFontAction);
199
200 QMenu *pFontSize = pMenu->addMenu(tr("Font &Size"));
201 for (unsigned i = 0; i < RT_ELEMENTS(m_apFontSizeActions); i++)
202 pFontSize->addAction(m_apFontSizeActions[i]);
203
204 pMenu->exec(pEvent->globalPos());
205 delete pMenu;
206}
207
208
209void
210VBoxDbgConsoleOutput::setColorScheme(VBoxDbgConsoleColor enmScheme, bool fSaveIt)
211{
212 const char *pszSetting;
213 QAction *pAction;
214 switch (enmScheme)
215 {
216 case kGreenOnBlack:
217 setStyleSheet("QTextEdit { background-color: black; color: rgb(0, 224, 0) }");
218 pszSetting = "GreenOnBlack";
219 pAction = m_pGreenOnBlackAction;
220 break;
221 case kBlackOnWhite:
222 setStyleSheet("QTextEdit { background-color: white; color: black }");
223 pszSetting = "BlackOnWhite";
224 pAction = m_pBlackOnWhiteAction;
225 break;
226 default:
227 AssertFailedReturnVoid();
228 }
229
230 m_enmColorScheme = kGreenOnBlack;
231
232 /* When going through a slot, the action is typically checked already by Qt. */
233 if (!pAction->isChecked())
234 pAction->setChecked(true);
235
236 /* Make this setting persistent. */
237 if (m_pVirtualBox && fSaveIt)
238 m_pVirtualBox->SetExtraData(com::Bstr("DbgConsole/ColorScheme").raw(), com::Bstr(pszSetting).raw());
239}
240
241
242void
243VBoxDbgConsoleOutput::setFontType(VBoxDbgConsoleFontType enmFontType, bool fSaveIt)
244{
245 QFont Font = font();
246 QAction *pAction;
247 const char *pszSetting;
248 switch (enmFontType)
249 {
250 case kFontType_Courier:
251#ifdef Q_WS_MAC
252 Font = QFont("Monaco", Font.pointSize(), QFont::Normal, FALSE);
253 Font.setStyleStrategy(QFont::NoAntialias);
254#else
255 Font.setStyleHint(QFont::TypeWriter);
256 Font.setFamily("Courier [Monotype]");
257#endif
258 pszSetting = "Courier";
259 pAction = m_pCourierFontAction;
260 break;
261
262 case kFontType_Monospace:
263 Font.setStyleHint(QFont::TypeWriter);
264 Font.setStyleStrategy(QFont::PreferAntialias);
265 Font.setFamily("Monospace [Monotype]");
266 pszSetting = "Monospace";
267 pAction = m_pMonospaceFontAction;
268 break;
269
270 default:
271 AssertFailedReturnVoid();
272 }
273
274 setFont(Font);
275
276 /* When going through a slot, the action is typically checked already by Qt. */
277 if (!pAction->isChecked())
278 pAction->setChecked(true);
279
280 /* Make this setting persistent. */
281 if (m_pVirtualBox && fSaveIt)
282 m_pVirtualBox->SetExtraData(com::Bstr("DbgConsole/Font").raw(), com::Bstr(pszSetting).raw());
283}
284
285
286void
287VBoxDbgConsoleOutput::setFontSize(uint32_t uFontSize, bool fSaveIt)
288{
289 uint32_t idxAction = uFontSize - s_uMinFontSize;
290 if (idxAction < (uint32_t)RT_ELEMENTS(m_apFontSizeActions))
291 {
292 if (!m_apFontSizeActions[idxAction]->isChecked())
293 m_apFontSizeActions[idxAction]->setChecked(true);
294
295 QFont Font = font();
296 Font.setPointSize(uFontSize);
297 setFont(Font);
298
299 /* Make this setting persistent if requested. */
300 if (fSaveIt && m_pVirtualBox)
301 m_pVirtualBox->SetExtraData(com::Bstr("DbgConsole/FontSize").raw(), com::BstrFmt("%u", uFontSize).raw());
302 }
303}
304
305
306void
307VBoxDbgConsoleOutput::sltSelectColorScheme()
308{
309 QAction *pAction = qobject_cast<QAction *>(sender());
310 if (pAction)
311 setColorScheme((VBoxDbgConsoleColor)pAction->data().toInt(), true /*fSaveIt*/);
312}
313
314
315void
316VBoxDbgConsoleOutput::sltSelectFontType()
317{
318 QAction *pAction = qobject_cast<QAction *>(sender());
319 if (pAction)
320 setFontType((VBoxDbgConsoleFontType)pAction->data().toInt(), true /*fSaveIt*/);
321}
322
323
324void
325VBoxDbgConsoleOutput::sltSelectFontSize()
326{
327 QAction *pAction = qobject_cast<QAction *>(sender());
328 if (pAction)
329 setFontSize(pAction->data().toUInt(), true /*fSaveIt*/);
330}
331
332
333void
334VBoxDbgConsoleOutput::appendText(const QString &rStr, bool fClearSelection)
335{
336 Assert(m_hGUIThread == RTThreadNativeSelf());
337
338 if (rStr.isEmpty() || rStr.isNull() || !rStr.length())
339 return;
340
341 /*
342 * Insert all in one go and make sure it's visible.
343 *
344 * We need to move the cursor and unselect any selected text before
345 * inserting anything, otherwise, text will disappear.
346 */
347 QTextCursor Cursor = textCursor();
348 if (!fClearSelection && Cursor.hasSelection())
349 {
350 QTextCursor SavedCursor = Cursor;
351 Cursor.clearSelection();
352 Cursor.movePosition(QTextCursor::End);
353
354 Cursor.insertText(rStr);
355
356 setTextCursor(SavedCursor);
357 }
358 else
359 {
360 if (Cursor.hasSelection())
361 Cursor.clearSelection();
362 if (!Cursor.atEnd())
363 Cursor.movePosition(QTextCursor::End);
364
365 Cursor.insertText(rStr);
366
367 setTextCursor(Cursor);
368 ensureCursorVisible();
369 }
370}
371
372
373
374
375/*
376 *
377 * V B o x D b g C o n s o l e I n p u t
378 * V B o x D b g C o n s o l e I n p u t
379 * V B o x D b g C o n s o l e I n p u t
380 *
381 *
382 */
383
384
385VBoxDbgConsoleInput::VBoxDbgConsoleInput(QWidget *pParent/* = NULL*/, const char *pszName/* = NULL*/)
386 : QComboBox(pParent), m_hGUIThread(RTThreadNativeSelf())
387{
388 addItem(""); /* invariant: empty command line is the last item */
389
390 setEditable(true);
391 setInsertPolicy(NoInsert);
392 setAutoCompletion(false);
393 setMaxCount(50);
394 const QLineEdit *pEdit = lineEdit();
395 if (pEdit)
396 connect(pEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
397
398 NOREF(pszName);
399}
400
401
402VBoxDbgConsoleInput::~VBoxDbgConsoleInput()
403{
404 Assert(m_hGUIThread == RTThreadNativeSelf());
405}
406
407
408void
409VBoxDbgConsoleInput::setLineEdit(QLineEdit *pEdit)
410{
411 Assert(m_hGUIThread == RTThreadNativeSelf());
412 QComboBox::setLineEdit(pEdit);
413 if (lineEdit() == pEdit && pEdit)
414 connect(pEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
415}
416
417
418void
419VBoxDbgConsoleInput::returnPressed()
420{
421 Assert(m_hGUIThread == RTThreadNativeSelf());
422
423 QString strCommand = currentText();
424 /** @todo trim whitespace? */
425 if (strCommand.isEmpty())
426 return;
427
428 /* deal with the current command. */
429 emit commandSubmitted(strCommand);
430
431
432 /*
433 * Add current command to history.
434 */
435 bool fNeedsAppending = true;
436
437 /* invariant: empty line at the end */
438 int iLastItem = count() - 1;
439 Assert(itemText(iLastItem).isEmpty());
440
441 /* have previous command? check duplicate. */
442 if (iLastItem > 0)
443 {
444 const QString strPrevCommand(itemText(iLastItem - 1));
445 if (strCommand == strPrevCommand)
446 fNeedsAppending = false;
447 }
448
449 if (fNeedsAppending)
450 {
451 /* history full? drop the oldest command. */
452 if (count() == maxCount())
453 {
454 removeItem(0);
455 --iLastItem;
456 }
457
458 /* insert before the empty line. */
459 insertItem(iLastItem, strCommand);
460 }
461
462 /* invariant: empty line at the end */
463 int iNewLastItem = count() - 1;
464 Assert(itemText(iNewLastItem).isEmpty());
465
466 /* select empty line to present "new" command line to the user */
467 setCurrentIndex(iNewLastItem);
468}
469
470
471
472
473
474
475/*
476 *
477 * V B o x D b g C o n s o l e
478 * V B o x D b g C o n s o l e
479 * V B o x D b g C o n s o l e
480 *
481 *
482 */
483
484
485VBoxDbgConsole::VBoxDbgConsole(VBoxDbgGui *a_pDbgGui, QWidget *a_pParent/* = NULL*/, IVirtualBox *a_pVirtualBox/* = NULL */)
486 : VBoxDbgBaseWindow(a_pDbgGui, a_pParent, "Console"), m_pOutput(NULL), m_pInput(NULL), m_fInputRestoreFocus(false),
487 m_pszInputBuf(NULL), m_cbInputBuf(0), m_cbInputBufAlloc(0),
488 m_pszOutputBuf(NULL), m_cbOutputBuf(0), m_cbOutputBufAlloc(0),
489 m_pTimer(NULL), m_fUpdatePending(false), m_Thread(NIL_RTTHREAD), m_EventSem(NIL_RTSEMEVENT),
490 m_fTerminate(false), m_fThreadTerminated(false)
491{
492 /* Delete dialog on close: */
493 setAttribute(Qt::WA_DeleteOnClose);
494
495 /*
496 * Create the output text box.
497 */
498 m_pOutput = new VBoxDbgConsoleOutput(this, a_pVirtualBox);
499
500 /* try figure a suitable size */
501 QLabel *pLabel = new QLabel( "11111111111111111111111111111111111111111111111111111111111111111111111111111112222222222", this);
502 pLabel->setFont(m_pOutput->font());
503 QSize Size = pLabel->sizeHint();
504 delete pLabel;
505 Size.setWidth((int)(Size.width() * 1.10));
506 Size.setHeight(Size.width() / 2);
507 resize(Size);
508
509 /*
510 * Create the input combo box (with a label).
511 */
512 QHBoxLayout *pLayout = new QHBoxLayout();
513 //pLayout->setSizeConstraint(QLayout::SetMaximumSize);
514
515 pLabel = new QLabel(" Command ");
516 pLayout->addWidget(pLabel);
517 pLabel->setMaximumSize(pLabel->sizeHint());
518 pLabel->setAlignment(Qt::AlignCenter);
519
520 m_pInput = new VBoxDbgConsoleInput(NULL);
521 pLayout->addWidget(m_pInput);
522 m_pInput->setDuplicatesEnabled(false);
523 connect(m_pInput, SIGNAL(commandSubmitted(const QString &)), this, SLOT(commandSubmitted(const QString &)));
524
525# if 0//def Q_WS_MAC
526 pLabel = new QLabel(" ");
527 pLayout->addWidget(pLabel);
528 pLabel->setMaximumSize(20, m_pInput->sizeHint().height() + 6);
529 pLabel->setMinimumSize(20, m_pInput->sizeHint().height() + 6);
530# endif
531
532 QWidget *pHBox = new QWidget(this);
533 pHBox->setLayout(pLayout);
534
535 m_pInput->setEnabled(false); /* (we'll get a ready notification) */
536
537
538 /*
539 * Vertical layout box on the whole widget.
540 */
541 QVBoxLayout *pVLayout = new QVBoxLayout();
542 pVLayout->setContentsMargins(0, 0, 0, 0);
543 pVLayout->setSpacing(5);
544 pVLayout->addWidget(m_pOutput);
545 pVLayout->addWidget(pHBox);
546 setLayout(pVLayout);
547
548 /*
549 * The tab order is from input to output, not the other way around as it is by default.
550 */
551 setTabOrder(m_pInput, m_pOutput);
552 m_fInputRestoreFocus = true; /* hack */
553
554 /*
555 * Setup the timer.
556 */
557 m_pTimer = new QTimer(this);
558 connect(m_pTimer, SIGNAL(timeout()), SLOT(updateOutput()));
559
560 /*
561 * Init the backend structure.
562 */
563 m_Back.Core.pfnInput = backInput;
564 m_Back.Core.pfnRead = backRead;
565 m_Back.Core.pfnWrite = backWrite;
566 m_Back.Core.pfnSetReady = backSetReady;
567 m_Back.pSelf = this;
568
569 /*
570 * Create the critical section, the event semaphore and the debug console thread.
571 */
572 int rc = RTCritSectInit(&m_Lock);
573 AssertRC(rc);
574
575 rc = RTSemEventCreate(&m_EventSem);
576 AssertRC(rc);
577
578 rc = RTThreadCreate(&m_Thread, backThread, this, 0, RTTHREADTYPE_DEBUGGER, RTTHREADFLAGS_WAITABLE, "VBoxDbgC");
579 AssertRC(rc);
580 if (RT_FAILURE(rc))
581 m_Thread = NIL_RTTHREAD;
582
583 /*
584 * Shortcuts.
585 */
586 m_pFocusToInput = new QAction("", this);
587 m_pFocusToInput->setShortcut(QKeySequence("Ctrl+L"));
588 addAction(m_pFocusToInput);
589 connect(m_pFocusToInput, SIGNAL(triggered(bool)), this, SLOT(actFocusToInput()));
590
591 m_pFocusToOutput = new QAction("", this);
592 m_pFocusToOutput->setShortcut(QKeySequence("Ctrl+O"));
593 addAction(m_pFocusToOutput);
594 connect(m_pFocusToOutput, SIGNAL(triggered(bool)), this, SLOT(actFocusToOutput()));
595
596 addAction(m_pOutput->m_pBlackOnWhiteAction);
597 addAction(m_pOutput->m_pGreenOnBlackAction);
598 addAction(m_pOutput->m_pCourierFontAction);
599 addAction(m_pOutput->m_pMonospaceFontAction);
600}
601
602
603VBoxDbgConsole::~VBoxDbgConsole()
604{
605 Assert(isGUIThread());
606
607 /*
608 * Wait for the thread.
609 */
610 ASMAtomicWriteBool(&m_fTerminate, true);
611 RTSemEventSignal(m_EventSem);
612 if (m_Thread != NIL_RTTHREAD)
613 {
614 int rc = RTThreadWait(m_Thread, 15000, NULL);
615 AssertRC(rc);
616 m_Thread = NIL_RTTHREAD;
617 }
618
619 /*
620 * Free resources.
621 */
622 delete m_pTimer;
623 m_pTimer = NULL;
624 RTCritSectDelete(&m_Lock);
625 RTSemEventDestroy(m_EventSem);
626 m_EventSem = 0;
627 m_pOutput = NULL;
628 m_pInput = NULL;
629 if (m_pszInputBuf)
630 {
631 RTMemFree(m_pszInputBuf);
632 m_pszInputBuf = NULL;
633 }
634 m_cbInputBuf = 0;
635 m_cbInputBufAlloc = 0;
636
637 delete m_pFocusToInput;
638 m_pFocusToInput = NULL;
639 delete m_pFocusToOutput;
640 m_pFocusToOutput = NULL;
641}
642
643
644void
645VBoxDbgConsole::commandSubmitted(const QString &rCommand)
646{
647 Assert(isGUIThread());
648
649 lock();
650 RTSemEventSignal(m_EventSem);
651
652 QByteArray Utf8Array = rCommand.toUtf8();
653 const char *psz = Utf8Array.constData();
654 size_t cb = strlen(psz);
655
656 /*
657 * Make sure we've got space for the input.
658 */
659 if (cb + m_cbInputBuf >= m_cbInputBufAlloc)
660 {
661 size_t cbNew = RT_ALIGN_Z(cb + m_cbInputBufAlloc + 1, 128);
662 void *pv = RTMemRealloc(m_pszInputBuf, cbNew);
663 if (!pv)
664 {
665 unlock();
666 return;
667 }
668 m_pszInputBuf = (char *)pv;
669 m_cbInputBufAlloc = cbNew;
670 }
671
672 /*
673 * Add the input and output it.
674 */
675 memcpy(m_pszInputBuf + m_cbInputBuf, psz, cb);
676 m_cbInputBuf += cb;
677 m_pszInputBuf[m_cbInputBuf++] = '\n';
678
679 m_pOutput->appendText(rCommand + "\n", true /*fClearSelection*/);
680 m_pOutput->ensureCursorVisible();
681
682 m_fInputRestoreFocus = m_pInput->hasFocus(); /* dirty focus hack */
683 m_pInput->setEnabled(false);
684
685 Log(("VBoxDbgConsole::commandSubmitted: %s (input-enabled=%RTbool)\n", psz, m_pInput->isEnabled()));
686 unlock();
687}
688
689
690void
691VBoxDbgConsole::updateOutput()
692{
693 Assert(isGUIThread());
694
695 lock();
696 m_fUpdatePending = false;
697 if (m_cbOutputBuf)
698 {
699 m_pOutput->appendText(QString::fromUtf8((const char *)m_pszOutputBuf, (int)m_cbOutputBuf), false /*fClearSelection*/);
700 m_cbOutputBuf = 0;
701 }
702 unlock();
703}
704
705
706/**
707 * Lock the object.
708 */
709void
710VBoxDbgConsole::lock()
711{
712 RTCritSectEnter(&m_Lock);
713}
714
715
716/**
717 * Unlocks the object.
718 */
719void
720VBoxDbgConsole::unlock()
721{
722 RTCritSectLeave(&m_Lock);
723}
724
725
726
727/**
728 * Checks if there is input.
729 *
730 * @returns true if there is input ready.
731 * @returns false if there not input ready.
732 * @param pBack Pointer to VBoxDbgConsole::m_Back.
733 * @param cMillies Number of milliseconds to wait on input data.
734 */
735/*static*/ DECLCALLBACK(bool)
736VBoxDbgConsole::backInput(PDBGCBACK pBack, uint32_t cMillies)
737{
738 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
739 pThis->lock();
740
741 bool fRc = true;
742 if (!pThis->m_cbInputBuf)
743 {
744 /*
745 * Wait outside the lock for the requested time, then check again.
746 */
747 pThis->unlock();
748 RTSemEventWait(pThis->m_EventSem, cMillies);
749 pThis->lock();
750 fRc = pThis->m_cbInputBuf
751 || ASMAtomicUoReadBool(&pThis->m_fTerminate);
752 }
753
754 pThis->unlock();
755 return fRc;
756}
757
758
759/**
760 * Read input.
761 *
762 * @returns VBox status code.
763 * @param pBack Pointer to VBoxDbgConsole::m_Back.
764 * @param pvBuf Where to put the bytes we read.
765 * @param cbBuf Maximum nymber of bytes to read.
766 * @param pcbRead Where to store the number of bytes actually read.
767 * If NULL the entire buffer must be filled for a
768 * successful return.
769 */
770/*static*/ DECLCALLBACK(int)
771VBoxDbgConsole::backRead(PDBGCBACK pBack, void *pvBuf, size_t cbBuf, size_t *pcbRead)
772{
773 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
774 Assert(pcbRead); /** @todo implement this bit */
775 if (pcbRead)
776 *pcbRead = 0;
777
778 pThis->lock();
779 int rc = VINF_SUCCESS;
780 if (!ASMAtomicUoReadBool(&pThis->m_fTerminate))
781 {
782 if (pThis->m_cbInputBuf)
783 {
784 const char *psz = pThis->m_pszInputBuf;
785 size_t cbRead = RT_MIN(pThis->m_cbInputBuf, cbBuf);
786 memcpy(pvBuf, psz, cbRead);
787 psz += cbRead;
788 pThis->m_cbInputBuf -= cbRead;
789 if (*psz)
790 memmove(pThis->m_pszInputBuf, psz, pThis->m_cbInputBuf);
791 pThis->m_pszInputBuf[pThis->m_cbInputBuf] = '\0';
792 *pcbRead = cbRead;
793 }
794 }
795 else
796 rc = VERR_GENERAL_FAILURE;
797 pThis->unlock();
798 return rc;
799}
800
801
802/**
803 * Write (output).
804 *
805 * @returns VBox status code.
806 * @param pBack Pointer to VBoxDbgConsole::m_Back.
807 * @param pvBuf What to write.
808 * @param cbBuf Number of bytes to write.
809 * @param pcbWritten Where to store the number of bytes actually written.
810 * If NULL the entire buffer must be successfully written.
811 */
812/*static*/ DECLCALLBACK(int)
813VBoxDbgConsole::backWrite(PDBGCBACK pBack, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
814{
815 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
816 int rc = VINF_SUCCESS;
817
818 pThis->lock();
819 if (cbBuf + pThis->m_cbOutputBuf >= pThis->m_cbOutputBufAlloc)
820 {
821 size_t cbNew = RT_ALIGN_Z(cbBuf + pThis->m_cbOutputBufAlloc + 1, 1024);
822 void *pv = RTMemRealloc(pThis->m_pszOutputBuf, cbNew);
823 if (!pv)
824 {
825 pThis->unlock();
826 if (pcbWritten)
827 *pcbWritten = 0;
828 return VERR_NO_MEMORY;
829 }
830 pThis->m_pszOutputBuf = (char *)pv;
831 pThis->m_cbOutputBufAlloc = cbNew;
832 }
833
834 /*
835 * Add the output.
836 */
837 memcpy(pThis->m_pszOutputBuf + pThis->m_cbOutputBuf, pvBuf, cbBuf);
838 pThis->m_cbOutputBuf += cbBuf;
839 pThis->m_pszOutputBuf[pThis->m_cbOutputBuf] = '\0';
840 if (pcbWritten)
841 *pcbWritten = cbBuf;
842
843 if (ASMAtomicUoReadBool(&pThis->m_fTerminate))
844 rc = VERR_GENERAL_FAILURE;
845
846 /*
847 * Tell the GUI thread to draw this text.
848 * We cannot do it from here without frequent crashes.
849 */
850 if (!pThis->m_fUpdatePending)
851 QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(VBoxDbgConsoleEvent::kUpdate));
852
853 pThis->unlock();
854
855 return rc;
856}
857
858
859/*static*/ DECLCALLBACK(void)
860VBoxDbgConsole::backSetReady(PDBGCBACK pBack, bool fReady)
861{
862 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
863 if (fReady)
864 QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(VBoxDbgConsoleEvent::kInputEnable));
865}
866
867
868/**
869 * The Debugger Console Thread
870 *
871 * @returns VBox status code (ignored).
872 * @param Thread The thread handle.
873 * @param pvUser Pointer to the VBoxDbgConsole object.s
874 */
875/*static*/ DECLCALLBACK(int)
876VBoxDbgConsole::backThread(RTTHREAD Thread, void *pvUser)
877{
878 VBoxDbgConsole *pThis = (VBoxDbgConsole *)pvUser;
879 LogFlow(("backThread: Thread=%p pvUser=%p\n", (void *)Thread, pvUser));
880
881 NOREF(Thread);
882
883 /*
884 * Create and execute the console.
885 */
886 int rc = pThis->dbgcCreate(&pThis->m_Back.Core, 0);
887
888 ASMAtomicUoWriteBool(&pThis->m_fThreadTerminated, true);
889 if (!ASMAtomicUoReadBool(&pThis->m_fTerminate))
890 QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(rc == VINF_SUCCESS
891 ? VBoxDbgConsoleEvent::kTerminatedUser
892 : VBoxDbgConsoleEvent::kTerminatedOther));
893 LogFlow(("backThread: returns %Rrc (m_fTerminate=%RTbool)\n", rc, ASMAtomicUoReadBool(&pThis->m_fTerminate)));
894 return rc;
895}
896
897
898bool
899VBoxDbgConsole::event(QEvent *pGenEvent)
900{
901 Assert(isGUIThread());
902 if (pGenEvent->type() == (QEvent::Type)VBoxDbgConsoleEvent::kEventNumber)
903 {
904 VBoxDbgConsoleEvent *pEvent = (VBoxDbgConsoleEvent *)pGenEvent;
905
906 switch (pEvent->command())
907 {
908 /* make update pending. */
909 case VBoxDbgConsoleEvent::kUpdate:
910 lock();
911 if (!m_fUpdatePending)
912 {
913 m_fUpdatePending = true;
914 m_pTimer->setSingleShot(true);
915 m_pTimer->start(10);
916 }
917 unlock();
918 break;
919
920 /* Re-enable the input field and restore focus. */
921 case VBoxDbgConsoleEvent::kInputEnable:
922 Log(("VBoxDbgConsole: kInputEnable (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
923 m_pInput->setEnabled(true);
924 if ( m_fInputRestoreFocus
925 && !m_pInput->hasFocus())
926 m_pInput->setFocus(); /* this is a hack. */
927 m_fInputRestoreFocus = false;
928 break;
929
930 /* The thread terminated by user command (exit, quit, bye). */
931 case VBoxDbgConsoleEvent::kTerminatedUser:
932 Log(("VBoxDbgConsole: kTerminatedUser (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
933 m_pInput->setEnabled(false);
934 close();
935 break;
936
937 /* The thread terminated for some unknown reason., disable input */
938 case VBoxDbgConsoleEvent::kTerminatedOther:
939 Log(("VBoxDbgConsole: kTerminatedOther (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
940 m_pInput->setEnabled(false);
941 break;
942
943 /* paranoia */
944 default:
945 AssertMsgFailed(("command=%d\n", pEvent->command()));
946 break;
947 }
948 return true;
949 }
950
951 return VBoxDbgBaseWindow::event(pGenEvent);
952}
953
954
955void
956VBoxDbgConsole::keyReleaseEvent(QKeyEvent *pEvent)
957{
958 //RTAssertMsg2("VBoxDbgConsole::keyReleaseEvent: %d (%#x); mod=%#x\n", pEvent->key(), pEvent->key(), pEvent->modifiers());
959 switch (pEvent->key())
960 {
961 case Qt::Key_F5:
962 if (pEvent->modifiers() == 0)
963 commandSubmitted("g");
964 break;
965
966 case Qt::Key_F8:
967 if (pEvent->modifiers() == 0)
968 commandSubmitted("t");
969 break;
970
971 case Qt::Key_F10:
972 if (pEvent->modifiers() == 0)
973 commandSubmitted("p");
974 break;
975
976 case Qt::Key_F11:
977 if (pEvent->modifiers() == 0)
978 commandSubmitted("t");
979 else if (pEvent->modifiers() == Qt::ShiftModifier)
980 commandSubmitted("gu");
981 break;
982
983 case Qt::Key_Cancel: /* == break */
984 if (pEvent->modifiers() == Qt::ControlModifier)
985 commandSubmitted("stop");
986 break;
987 case Qt::Key_Delete:
988 if (pEvent->modifiers() == Qt::AltModifier)
989 commandSubmitted("stop");
990 break;
991 }
992}
993
994
995void
996VBoxDbgConsole::closeEvent(QCloseEvent *a_pCloseEvt)
997{
998 if (m_fThreadTerminated)
999 a_pCloseEvt->accept();
1000}
1001
1002
1003void
1004VBoxDbgConsole::actFocusToInput()
1005{
1006 if (!m_pInput->hasFocus())
1007 m_pInput->setFocus(Qt::ShortcutFocusReason);
1008}
1009
1010
1011void
1012VBoxDbgConsole::actFocusToOutput()
1013{
1014 if (!m_pOutput->hasFocus())
1015 m_pOutput->setFocus(Qt::ShortcutFocusReason);
1016}
1017
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette