VirtualBox

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

Last change on this file since 77412 was 77412, checked in by vboxsync, 6 years ago

VBoxDbg: Moved the title management to the VBoxDbgBaseWindow class, combining the approaches in the console and statistics windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.1 KB
Line 
1/* $Id: VBoxDbgConsole.cpp 77412 2019-02-21 18:25:37Z vboxsync $ */
2/** @file
3 * VBox Debugger GUI - Console.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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 /*
493 * Create the output text box.
494 */
495 m_pOutput = new VBoxDbgConsoleOutput(this, a_pVirtualBox);
496
497 /* try figure a suitable size */
498 QLabel *pLabel = new QLabel( "11111111111111111111111111111111111111111111111111111111111111111111111111111112222222222", this);
499 pLabel->setFont(m_pOutput->font());
500 QSize Size = pLabel->sizeHint();
501 delete pLabel;
502 Size.setWidth((int)(Size.width() * 1.10));
503 Size.setHeight(Size.width() / 2);
504 resize(Size);
505
506 /*
507 * Create the input combo box (with a label).
508 */
509 QHBoxLayout *pLayout = new QHBoxLayout();
510 //pLayout->setSizeConstraint(QLayout::SetMaximumSize);
511
512 pLabel = new QLabel(" Command ");
513 pLayout->addWidget(pLabel);
514 pLabel->setMaximumSize(pLabel->sizeHint());
515 pLabel->setAlignment(Qt::AlignCenter);
516
517 m_pInput = new VBoxDbgConsoleInput(NULL);
518 pLayout->addWidget(m_pInput);
519 m_pInput->setDuplicatesEnabled(false);
520 connect(m_pInput, SIGNAL(commandSubmitted(const QString &)), this, SLOT(commandSubmitted(const QString &)));
521
522# if 0//def Q_WS_MAC
523 pLabel = new QLabel(" ");
524 pLayout->addWidget(pLabel);
525 pLabel->setMaximumSize(20, m_pInput->sizeHint().height() + 6);
526 pLabel->setMinimumSize(20, m_pInput->sizeHint().height() + 6);
527# endif
528
529 QWidget *pHBox = new QWidget(this);
530 pHBox->setLayout(pLayout);
531
532 m_pInput->setEnabled(false); /* (we'll get a ready notification) */
533
534
535 /*
536 * Vertical layout box on the whole widget.
537 */
538 QVBoxLayout *pVLayout = new QVBoxLayout();
539 pVLayout->setContentsMargins(0, 0, 0, 0);
540 pVLayout->setSpacing(5);
541 pVLayout->addWidget(m_pOutput);
542 pVLayout->addWidget(pHBox);
543 setLayout(pVLayout);
544
545 /*
546 * The tab order is from input to output, not the other way around as it is by default.
547 */
548 setTabOrder(m_pInput, m_pOutput);
549 m_fInputRestoreFocus = true; /* hack */
550
551 /*
552 * Setup the timer.
553 */
554 m_pTimer = new QTimer(this);
555 connect(m_pTimer, SIGNAL(timeout()), SLOT(updateOutput()));
556
557 /*
558 * Init the backend structure.
559 */
560 m_Back.Core.pfnInput = backInput;
561 m_Back.Core.pfnRead = backRead;
562 m_Back.Core.pfnWrite = backWrite;
563 m_Back.Core.pfnSetReady = backSetReady;
564 m_Back.pSelf = this;
565
566 /*
567 * Create the critical section, the event semaphore and the debug console thread.
568 */
569 int rc = RTCritSectInit(&m_Lock);
570 AssertRC(rc);
571
572 rc = RTSemEventCreate(&m_EventSem);
573 AssertRC(rc);
574
575 rc = RTThreadCreate(&m_Thread, backThread, this, 0, RTTHREADTYPE_DEBUGGER, RTTHREADFLAGS_WAITABLE, "VBoxDbgC");
576 AssertRC(rc);
577 if (RT_FAILURE(rc))
578 m_Thread = NIL_RTTHREAD;
579
580 /*
581 * Shortcuts.
582 */
583 m_pFocusToInput = new QAction("", this);
584 m_pFocusToInput->setShortcut(QKeySequence("Ctrl+L"));
585 addAction(m_pFocusToInput);
586 connect(m_pFocusToInput, SIGNAL(triggered(bool)), this, SLOT(actFocusToInput()));
587
588 m_pFocusToOutput = new QAction("", this);
589 m_pFocusToOutput->setShortcut(QKeySequence("Ctrl+O"));
590 addAction(m_pFocusToOutput);
591 connect(m_pFocusToOutput, SIGNAL(triggered(bool)), this, SLOT(actFocusToOutput()));
592
593 addAction(m_pOutput->m_pBlackOnWhiteAction);
594 addAction(m_pOutput->m_pGreenOnBlackAction);
595 addAction(m_pOutput->m_pCourierFontAction);
596 addAction(m_pOutput->m_pMonospaceFontAction);
597}
598
599
600VBoxDbgConsole::~VBoxDbgConsole()
601{
602 Assert(isGUIThread());
603
604 /*
605 * Wait for the thread.
606 */
607 ASMAtomicWriteBool(&m_fTerminate, true);
608 RTSemEventSignal(m_EventSem);
609 if (m_Thread != NIL_RTTHREAD)
610 {
611 int rc = RTThreadWait(m_Thread, 15000, NULL);
612 AssertRC(rc);
613 m_Thread = NIL_RTTHREAD;
614 }
615
616 /*
617 * Free resources.
618 */
619 delete m_pTimer;
620 m_pTimer = NULL;
621 RTCritSectDelete(&m_Lock);
622 RTSemEventDestroy(m_EventSem);
623 m_EventSem = 0;
624 m_pOutput = NULL;
625 m_pInput = NULL;
626 if (m_pszInputBuf)
627 {
628 RTMemFree(m_pszInputBuf);
629 m_pszInputBuf = NULL;
630 }
631 m_cbInputBuf = 0;
632 m_cbInputBufAlloc = 0;
633
634 delete m_pFocusToInput;
635 m_pFocusToInput = NULL;
636 delete m_pFocusToOutput;
637 m_pFocusToOutput = NULL;
638}
639
640
641void
642VBoxDbgConsole::commandSubmitted(const QString &rCommand)
643{
644 Assert(isGUIThread());
645
646 lock();
647 RTSemEventSignal(m_EventSem);
648
649 QByteArray Utf8Array = rCommand.toUtf8();
650 const char *psz = Utf8Array.constData();
651 size_t cb = strlen(psz);
652
653 /*
654 * Make sure we've got space for the input.
655 */
656 if (cb + m_cbInputBuf >= m_cbInputBufAlloc)
657 {
658 size_t cbNew = RT_ALIGN_Z(cb + m_cbInputBufAlloc + 1, 128);
659 void *pv = RTMemRealloc(m_pszInputBuf, cbNew);
660 if (!pv)
661 {
662 unlock();
663 return;
664 }
665 m_pszInputBuf = (char *)pv;
666 m_cbInputBufAlloc = cbNew;
667 }
668
669 /*
670 * Add the input and output it.
671 */
672 memcpy(m_pszInputBuf + m_cbInputBuf, psz, cb);
673 m_cbInputBuf += cb;
674 m_pszInputBuf[m_cbInputBuf++] = '\n';
675
676 m_pOutput->appendText(rCommand + "\n", true /*fClearSelection*/);
677 m_pOutput->ensureCursorVisible();
678
679 m_fInputRestoreFocus = m_pInput->hasFocus(); /* dirty focus hack */
680 m_pInput->setEnabled(false);
681
682 Log(("VBoxDbgConsole::commandSubmitted: %s (input-enabled=%RTbool)\n", psz, m_pInput->isEnabled()));
683 unlock();
684}
685
686
687void
688VBoxDbgConsole::updateOutput()
689{
690 Assert(isGUIThread());
691
692 lock();
693 m_fUpdatePending = false;
694 if (m_cbOutputBuf)
695 {
696 m_pOutput->appendText(QString::fromUtf8((const char *)m_pszOutputBuf, (int)m_cbOutputBuf), false /*fClearSelection*/);
697 m_cbOutputBuf = 0;
698 }
699 unlock();
700}
701
702
703/**
704 * Lock the object.
705 */
706void
707VBoxDbgConsole::lock()
708{
709 RTCritSectEnter(&m_Lock);
710}
711
712
713/**
714 * Unlocks the object.
715 */
716void
717VBoxDbgConsole::unlock()
718{
719 RTCritSectLeave(&m_Lock);
720}
721
722
723
724/**
725 * Checks if there is input.
726 *
727 * @returns true if there is input ready.
728 * @returns false if there not input ready.
729 * @param pBack Pointer to VBoxDbgConsole::m_Back.
730 * @param cMillies Number of milliseconds to wait on input data.
731 */
732/*static*/ DECLCALLBACK(bool)
733VBoxDbgConsole::backInput(PDBGCBACK pBack, uint32_t cMillies)
734{
735 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
736 pThis->lock();
737
738 bool fRc = true;
739 if (!pThis->m_cbInputBuf)
740 {
741 /*
742 * Wait outside the lock for the requested time, then check again.
743 */
744 pThis->unlock();
745 RTSemEventWait(pThis->m_EventSem, cMillies);
746 pThis->lock();
747 fRc = pThis->m_cbInputBuf
748 || ASMAtomicUoReadBool(&pThis->m_fTerminate);
749 }
750
751 pThis->unlock();
752 return fRc;
753}
754
755
756/**
757 * Read input.
758 *
759 * @returns VBox status code.
760 * @param pBack Pointer to VBoxDbgConsole::m_Back.
761 * @param pvBuf Where to put the bytes we read.
762 * @param cbBuf Maximum nymber of bytes to read.
763 * @param pcbRead Where to store the number of bytes actually read.
764 * If NULL the entire buffer must be filled for a
765 * successful return.
766 */
767/*static*/ DECLCALLBACK(int)
768VBoxDbgConsole::backRead(PDBGCBACK pBack, void *pvBuf, size_t cbBuf, size_t *pcbRead)
769{
770 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
771 Assert(pcbRead); /** @todo implement this bit */
772 if (pcbRead)
773 *pcbRead = 0;
774
775 pThis->lock();
776 int rc = VINF_SUCCESS;
777 if (!ASMAtomicUoReadBool(&pThis->m_fTerminate))
778 {
779 if (pThis->m_cbInputBuf)
780 {
781 const char *psz = pThis->m_pszInputBuf;
782 size_t cbRead = RT_MIN(pThis->m_cbInputBuf, cbBuf);
783 memcpy(pvBuf, psz, cbRead);
784 psz += cbRead;
785 pThis->m_cbInputBuf -= cbRead;
786 if (*psz)
787 memmove(pThis->m_pszInputBuf, psz, pThis->m_cbInputBuf);
788 pThis->m_pszInputBuf[pThis->m_cbInputBuf] = '\0';
789 *pcbRead = cbRead;
790 }
791 }
792 else
793 rc = VERR_GENERAL_FAILURE;
794 pThis->unlock();
795 return rc;
796}
797
798
799/**
800 * Write (output).
801 *
802 * @returns VBox status code.
803 * @param pBack Pointer to VBoxDbgConsole::m_Back.
804 * @param pvBuf What to write.
805 * @param cbBuf Number of bytes to write.
806 * @param pcbWritten Where to store the number of bytes actually written.
807 * If NULL the entire buffer must be successfully written.
808 */
809/*static*/ DECLCALLBACK(int)
810VBoxDbgConsole::backWrite(PDBGCBACK pBack, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
811{
812 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
813 int rc = VINF_SUCCESS;
814
815 pThis->lock();
816 if (cbBuf + pThis->m_cbOutputBuf >= pThis->m_cbOutputBufAlloc)
817 {
818 size_t cbNew = RT_ALIGN_Z(cbBuf + pThis->m_cbOutputBufAlloc + 1, 1024);
819 void *pv = RTMemRealloc(pThis->m_pszOutputBuf, cbNew);
820 if (!pv)
821 {
822 pThis->unlock();
823 if (pcbWritten)
824 *pcbWritten = 0;
825 return VERR_NO_MEMORY;
826 }
827 pThis->m_pszOutputBuf = (char *)pv;
828 pThis->m_cbOutputBufAlloc = cbNew;
829 }
830
831 /*
832 * Add the output.
833 */
834 memcpy(pThis->m_pszOutputBuf + pThis->m_cbOutputBuf, pvBuf, cbBuf);
835 pThis->m_cbOutputBuf += cbBuf;
836 pThis->m_pszOutputBuf[pThis->m_cbOutputBuf] = '\0';
837 if (pcbWritten)
838 *pcbWritten = cbBuf;
839
840 if (ASMAtomicUoReadBool(&pThis->m_fTerminate))
841 rc = VERR_GENERAL_FAILURE;
842
843 /*
844 * Tell the GUI thread to draw this text.
845 * We cannot do it from here without frequent crashes.
846 */
847 if (!pThis->m_fUpdatePending)
848 QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(VBoxDbgConsoleEvent::kUpdate));
849
850 pThis->unlock();
851
852 return rc;
853}
854
855
856/*static*/ DECLCALLBACK(void)
857VBoxDbgConsole::backSetReady(PDBGCBACK pBack, bool fReady)
858{
859 VBoxDbgConsole *pThis = VBOXDBGCONSOLE_FROM_DBGCBACK(pBack);
860 if (fReady)
861 QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(VBoxDbgConsoleEvent::kInputEnable));
862}
863
864
865/**
866 * The Debugger Console Thread
867 *
868 * @returns VBox status code (ignored).
869 * @param Thread The thread handle.
870 * @param pvUser Pointer to the VBoxDbgConsole object.s
871 */
872/*static*/ DECLCALLBACK(int)
873VBoxDbgConsole::backThread(RTTHREAD Thread, void *pvUser)
874{
875 VBoxDbgConsole *pThis = (VBoxDbgConsole *)pvUser;
876 LogFlow(("backThread: Thread=%p pvUser=%p\n", (void *)Thread, pvUser));
877
878 NOREF(Thread);
879
880 /*
881 * Create and execute the console.
882 */
883 int rc = pThis->dbgcCreate(&pThis->m_Back.Core, 0);
884
885 ASMAtomicUoWriteBool(&pThis->m_fThreadTerminated, true);
886 if (!ASMAtomicUoReadBool(&pThis->m_fTerminate))
887 QApplication::postEvent(pThis, new VBoxDbgConsoleEvent(rc == VINF_SUCCESS
888 ? VBoxDbgConsoleEvent::kTerminatedUser
889 : VBoxDbgConsoleEvent::kTerminatedOther));
890 LogFlow(("backThread: returns %Rrc (m_fTerminate=%RTbool)\n", rc, ASMAtomicUoReadBool(&pThis->m_fTerminate)));
891 return rc;
892}
893
894
895bool
896VBoxDbgConsole::event(QEvent *pGenEvent)
897{
898 Assert(isGUIThread());
899 if (pGenEvent->type() == (QEvent::Type)VBoxDbgConsoleEvent::kEventNumber)
900 {
901 VBoxDbgConsoleEvent *pEvent = (VBoxDbgConsoleEvent *)pGenEvent;
902
903 switch (pEvent->command())
904 {
905 /* make update pending. */
906 case VBoxDbgConsoleEvent::kUpdate:
907 lock();
908 if (!m_fUpdatePending)
909 {
910 m_fUpdatePending = true;
911 m_pTimer->setSingleShot(true);
912 m_pTimer->start(10);
913 }
914 unlock();
915 break;
916
917 /* Re-enable the input field and restore focus. */
918 case VBoxDbgConsoleEvent::kInputEnable:
919 Log(("VBoxDbgConsole: kInputEnable (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
920 m_pInput->setEnabled(true);
921 if ( m_fInputRestoreFocus
922 && !m_pInput->hasFocus())
923 m_pInput->setFocus(); /* this is a hack. */
924 m_fInputRestoreFocus = false;
925 break;
926
927 /* The thread terminated by user command (exit, quit, bye). */
928 case VBoxDbgConsoleEvent::kTerminatedUser:
929 Log(("VBoxDbgConsole: kTerminatedUser (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
930 m_pInput->setEnabled(false);
931 close();
932 break;
933
934 /* The thread terminated for some unknown reason., disable input */
935 case VBoxDbgConsoleEvent::kTerminatedOther:
936 Log(("VBoxDbgConsole: kTerminatedOther (input-enabled=%RTbool)\n", m_pInput->isEnabled()));
937 m_pInput->setEnabled(false);
938 break;
939
940 /* paranoia */
941 default:
942 AssertMsgFailed(("command=%d\n", pEvent->command()));
943 break;
944 }
945 return true;
946 }
947
948 return VBoxDbgBaseWindow::event(pGenEvent);
949}
950
951
952void
953VBoxDbgConsole::keyReleaseEvent(QKeyEvent *pEvent)
954{
955 //RTAssertMsg2("VBoxDbgConsole::keyReleaseEvent: %d (%#x); mod=%#x\n", pEvent->key(), pEvent->key(), pEvent->modifiers());
956 switch (pEvent->key())
957 {
958 case Qt::Key_F5:
959 if (pEvent->modifiers() == 0)
960 commandSubmitted("g");
961 break;
962
963 case Qt::Key_F8:
964 if (pEvent->modifiers() == 0)
965 commandSubmitted("t");
966 break;
967
968 case Qt::Key_F10:
969 if (pEvent->modifiers() == 0)
970 commandSubmitted("p");
971 break;
972
973 case Qt::Key_F11:
974 if (pEvent->modifiers() == 0)
975 commandSubmitted("t");
976 else if (pEvent->modifiers() == Qt::ShiftModifier)
977 commandSubmitted("gu");
978 break;
979
980 case Qt::Key_Cancel: /* == break */
981 if (pEvent->modifiers() == Qt::ControlModifier)
982 commandSubmitted("stop");
983 break;
984 case Qt::Key_Delete:
985 if (pEvent->modifiers() == Qt::AltModifier)
986 commandSubmitted("stop");
987 break;
988 }
989}
990
991
992void
993VBoxDbgConsole::closeEvent(QCloseEvent *a_pCloseEvt)
994{
995 if (m_fThreadTerminated)
996 {
997 a_pCloseEvt->accept();
998 delete this;
999 }
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