[31510] | 1 | /* $Id: VBoxDbgConsole.h 103491 2024-02-21 12:36:24Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBox Debugger GUI - Console.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[31510] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[31510] | 26 | */
|
---|
| 27 |
|
---|
[76564] | 28 | #ifndef DEBUGGER_INCLUDED_SRC_VBoxDbgConsole_h
|
---|
| 29 | #define DEBUGGER_INCLUDED_SRC_VBoxDbgConsole_h
|
---|
[76518] | 30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 31 | # pragma once
|
---|
| 32 | #endif
|
---|
[31510] | 33 |
|
---|
[103491] | 34 | //#define VBOXDBG_WITH_CONSOLE_OUTPUT_AS_QPLAINTEXT
|
---|
| 35 |
|
---|
[31510] | 36 | #include "VBoxDbgBase.h"
|
---|
| 37 |
|
---|
| 38 | #include <QTextEdit>
|
---|
[103491] | 39 | #ifdef VBOXDBG_WITH_CONSOLE_OUTPUT_AS_QPLAINTEXT
|
---|
| 40 | # include <QPlainTextEdit>
|
---|
| 41 | #endif
|
---|
[31510] | 42 | #include <QComboBox>
|
---|
| 43 | #include <QTimer>
|
---|
| 44 | #include <QEvent>
|
---|
[72197] | 45 | #include <QActionGroup>
|
---|
[31510] | 46 |
|
---|
| 47 | #include <iprt/critsect.h>
|
---|
| 48 | #include <iprt/semaphore.h>
|
---|
| 49 | #include <iprt/thread.h>
|
---|
| 50 |
|
---|
[65522] | 51 | // VirtualBox COM interfaces declarations (generated header)
|
---|
| 52 | #ifdef VBOX_WITH_XPCOM
|
---|
| 53 | # include <VirtualBox_XPCOM.h>
|
---|
| 54 | #else
|
---|
[92906] | 55 | # include <iprt/win/windows.h> /* Include via cleanup wrapper before VirtualBox.h includes it via rpc.h. */
|
---|
[65522] | 56 | # include <VirtualBox.h>
|
---|
| 57 | #endif
|
---|
[31510] | 58 |
|
---|
[65522] | 59 |
|
---|
[103491] | 60 | class VBoxDbgConsoleOutput
|
---|
| 61 | #ifdef VBOXDBG_WITH_CONSOLE_OUTPUT_AS_QPLAINTEXT
|
---|
| 62 | : public QPlainTextEdit
|
---|
| 63 | #else
|
---|
| 64 | : public QTextEdit
|
---|
| 65 | #endif
|
---|
[31510] | 66 | {
|
---|
| 67 | Q_OBJECT
|
---|
| 68 |
|
---|
| 69 | public:
|
---|
| 70 | /**
|
---|
| 71 | * Constructor.
|
---|
| 72 | *
|
---|
| 73 | * @param pParent Parent Widget.
|
---|
[65523] | 74 | * @param pVirtualBox VirtualBox object for storing extra data.
|
---|
[31510] | 75 | * @param pszName Widget name.
|
---|
| 76 | */
|
---|
[65521] | 77 | VBoxDbgConsoleOutput(QWidget *pParent = NULL, IVirtualBox *pVirtualBox = NULL, const char *pszName = NULL);
|
---|
[31510] | 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Destructor
|
---|
| 81 | */
|
---|
| 82 | virtual ~VBoxDbgConsoleOutput();
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Appends text.
|
---|
| 86 | * This differs from QTextEdit::append() in that it won't start on a new paragraph
|
---|
| 87 | * unless the previous char was a newline ('\n').
|
---|
| 88 | *
|
---|
[48946] | 89 | * @param rStr The text string to append.
|
---|
| 90 | * @param fClearSelection Whether to clear selected text before appending.
|
---|
[38813] | 91 | * If @c false the selection and window position
|
---|
| 92 | * are preserved.
|
---|
[31510] | 93 | */
|
---|
[38813] | 94 | virtual void appendText(const QString &rStr, bool fClearSelection);
|
---|
[31510] | 95 |
|
---|
[52002] | 96 | /** The action to switch to black-on-white color scheme. */
|
---|
| 97 | QAction *m_pBlackOnWhiteAction;
|
---|
| 98 | /** The action to switch to green-on-black color scheme. */
|
---|
| 99 | QAction *m_pGreenOnBlackAction;
|
---|
| 100 |
|
---|
[103491] | 101 | /** The action to switch to system default fixed font. */
|
---|
| 102 | QAction *m_pDefaultFontAction;
|
---|
| 103 | /** The action to switch to Monospace font. */
|
---|
| 104 | QAction *m_pMonospaceFontAction;
|
---|
[52002] | 105 | /** The action to switch to Courier font. */
|
---|
| 106 | QAction *m_pCourierFontAction;
|
---|
| 107 |
|
---|
[31510] | 108 | protected:
|
---|
[52002] | 109 | typedef enum { kGreenOnBlack, kBlackOnWhite } VBoxDbgConsoleColor;
|
---|
[103491] | 110 | typedef enum { kFontType_SystemDefault, kFontType_Monospace, kFontType_Courier } VBoxDbgConsoleFontType;
|
---|
[52002] | 111 |
|
---|
| 112 | /**
|
---|
| 113 | * Context menu event.
|
---|
| 114 | * This adds custom menu items for the output view.
|
---|
| 115 | *
|
---|
| 116 | * @param pEvent Pointer to the event.
|
---|
| 117 | */
|
---|
| 118 | virtual void contextMenuEvent(QContextMenuEvent *pEvent);
|
---|
| 119 |
|
---|
[72197] | 120 | /**
|
---|
| 121 | * Sets the color scheme.
|
---|
| 122 | *
|
---|
| 123 | * @param enmScheme The new color scheme.
|
---|
| 124 | * @param fSaveIt Whether to save it.
|
---|
| 125 | */
|
---|
| 126 | void setColorScheme(VBoxDbgConsoleColor enmScheme, bool fSaveIt);
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Sets the font type / family.
|
---|
| 130 | *
|
---|
| 131 | * @param enmFontType The font type.
|
---|
| 132 | * @param fSaveIt Whether to save it.
|
---|
| 133 | */
|
---|
| 134 | void setFontType(VBoxDbgConsoleFontType enmFontType, bool fSaveIt);
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Sets the font size.
|
---|
| 138 | *
|
---|
| 139 | * @param uFontSize The new font size in points.
|
---|
| 140 | * @param fSaveIt Whether to save it.
|
---|
| 141 | */
|
---|
| 142 | void setFontSize(uint32_t uFontSize, bool fSaveIt);
|
---|
| 143 |
|
---|
| 144 |
|
---|
[31510] | 145 | /** The current line (paragraph) number. */
|
---|
| 146 | unsigned m_uCurLine;
|
---|
| 147 | /** The position in the current line. */
|
---|
| 148 | unsigned m_uCurPos;
|
---|
| 149 | /** The handle to the GUI thread. */
|
---|
| 150 | RTNATIVETHREAD m_hGUIThread;
|
---|
[52002] | 151 | /** The current color scheme (foreground on background). */
|
---|
| 152 | VBoxDbgConsoleColor m_enmColorScheme;
|
---|
[65521] | 153 | /** The IVirtualBox object */
|
---|
| 154 | IVirtualBox *m_pVirtualBox;
|
---|
[52002] | 155 |
|
---|
[72197] | 156 | /** Array of font size actions 6..22pt. */
|
---|
| 157 | QAction *m_apFontSizeActions[22 - 6 + 1];
|
---|
| 158 | /** Action group for m_apFontSizeActions. */
|
---|
| 159 | QActionGroup *m_pActionFontSizeGroup;
|
---|
| 160 |
|
---|
| 161 | /** The minimum font size. */
|
---|
| 162 | static const uint32_t s_uMinFontSize;
|
---|
| 163 |
|
---|
[52002] | 164 | private slots:
|
---|
| 165 | /**
|
---|
[72197] | 166 | * Selects color scheme
|
---|
[52002] | 167 | */
|
---|
[72197] | 168 | void sltSelectColorScheme();
|
---|
[52002] | 169 |
|
---|
| 170 | /**
|
---|
[72197] | 171 | * Selects font type.
|
---|
[52002] | 172 | */
|
---|
[72197] | 173 | void sltSelectFontType();
|
---|
[52002] | 174 |
|
---|
| 175 | /**
|
---|
[72197] | 176 | * Selects font size.
|
---|
[52002] | 177 | */
|
---|
[72197] | 178 | void sltSelectFontSize();
|
---|
[31510] | 179 | };
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * The Debugger Console Input widget.
|
---|
| 184 | *
|
---|
[65066] | 185 | * This is a combobox which only responds to \<return\>.
|
---|
[31510] | 186 | */
|
---|
| 187 | class VBoxDbgConsoleInput : public QComboBox
|
---|
| 188 | {
|
---|
| 189 | Q_OBJECT
|
---|
| 190 |
|
---|
| 191 | public:
|
---|
| 192 | /**
|
---|
| 193 | * Constructor.
|
---|
| 194 | *
|
---|
| 195 | * @param pParent Parent Widget.
|
---|
| 196 | * @param pszName Widget name.
|
---|
| 197 | */
|
---|
| 198 | VBoxDbgConsoleInput(QWidget *pParent = NULL, const char *pszName = NULL);
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * Destructor
|
---|
| 202 | */
|
---|
| 203 | virtual ~VBoxDbgConsoleInput();
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
| 206 | * We overload this method to get signaled upon returnPressed().
|
---|
| 207 | *
|
---|
| 208 | * See QComboBox::setLineEdit for full description.
|
---|
| 209 | * @param pEdit The new line edit widget.
|
---|
| 210 | * @remark This won't be called during the constructor.
|
---|
| 211 | */
|
---|
| 212 | virtual void setLineEdit(QLineEdit *pEdit);
|
---|
| 213 |
|
---|
| 214 | signals:
|
---|
| 215 | /**
|
---|
| 216 | * New command submitted.
|
---|
| 217 | */
|
---|
| 218 | void commandSubmitted(const QString &rCommand);
|
---|
| 219 |
|
---|
| 220 | private slots:
|
---|
| 221 | /**
|
---|
| 222 | * Returned was pressed.
|
---|
| 223 | *
|
---|
| 224 | * Will emit commandSubmitted().
|
---|
| 225 | */
|
---|
| 226 | void returnPressed();
|
---|
| 227 |
|
---|
| 228 | protected:
|
---|
| 229 | /** The handle to the GUI thread. */
|
---|
| 230 | RTNATIVETHREAD m_hGUIThread;
|
---|
| 231 | };
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * The Debugger Console.
|
---|
| 236 | */
|
---|
| 237 | class VBoxDbgConsole : public VBoxDbgBaseWindow
|
---|
| 238 | {
|
---|
| 239 | Q_OBJECT
|
---|
| 240 |
|
---|
| 241 | public:
|
---|
| 242 | /**
|
---|
| 243 | * Constructor.
|
---|
| 244 | *
|
---|
| 245 | * @param a_pDbgGui Pointer to the debugger gui object.
|
---|
| 246 | * @param a_pParent Parent Widget.
|
---|
[65523] | 247 | * @param a_pVirtualBox VirtualBox object for storing extra data.
|
---|
[31510] | 248 | */
|
---|
[65521] | 249 | VBoxDbgConsole(VBoxDbgGui *a_pDbgGui, QWidget *a_pParent = NULL, IVirtualBox *a_pVirtualBox = NULL);
|
---|
[31510] | 250 |
|
---|
| 251 | /**
|
---|
| 252 | * Destructor
|
---|
| 253 | */
|
---|
| 254 | virtual ~VBoxDbgConsole();
|
---|
| 255 |
|
---|
| 256 | protected slots:
|
---|
| 257 | /**
|
---|
| 258 | * Handler called when a command is submitted.
|
---|
| 259 | * (Enter or return pressed in the combo box.)
|
---|
| 260 | *
|
---|
| 261 | * @param rCommand The submitted command.
|
---|
| 262 | */
|
---|
| 263 | void commandSubmitted(const QString &rCommand);
|
---|
| 264 |
|
---|
| 265 | /**
|
---|
| 266 | * Updates the output with what's currently in the output buffer.
|
---|
| 267 | * This is called by a timer or a User event posted by the debugger thread.
|
---|
| 268 | */
|
---|
| 269 | void updateOutput();
|
---|
| 270 |
|
---|
| 271 | /**
|
---|
| 272 | * Changes the focus to the input field.
|
---|
| 273 | */
|
---|
| 274 | void actFocusToInput();
|
---|
| 275 |
|
---|
| 276 | /**
|
---|
| 277 | * Changes the focus to the output viewer widget.
|
---|
| 278 | */
|
---|
| 279 | void actFocusToOutput();
|
---|
| 280 |
|
---|
| 281 | protected:
|
---|
| 282 | /**
|
---|
| 283 | * Override the closeEvent so we can choose delete the window when
|
---|
| 284 | * it is closed.
|
---|
| 285 | *
|
---|
| 286 | * @param a_pCloseEvt The close event.
|
---|
| 287 | */
|
---|
| 288 | virtual void closeEvent(QCloseEvent *a_pCloseEvt);
|
---|
| 289 |
|
---|
| 290 | /**
|
---|
| 291 | * Lock the object.
|
---|
| 292 | */
|
---|
| 293 | void lock();
|
---|
| 294 |
|
---|
| 295 | /**
|
---|
| 296 | * Unlocks the object.
|
---|
| 297 | */
|
---|
| 298 | void unlock();
|
---|
| 299 |
|
---|
| 300 | protected:
|
---|
| 301 | /** @name Debug Console Backend.
|
---|
| 302 | * @{
|
---|
| 303 | */
|
---|
| 304 |
|
---|
| 305 |
|
---|
| 306 | /**
|
---|
| 307 | * Checks if there is input.
|
---|
| 308 | *
|
---|
| 309 | * @returns true if there is input ready.
|
---|
| 310 | * @returns false if there not input ready.
|
---|
| 311 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
| 312 | * @param cMillies Number of milliseconds to wait on input data.
|
---|
| 313 | */
|
---|
[86327] | 314 | static DECLCALLBACK(bool) backInput(PCDBGCIO pIo, uint32_t cMillies);
|
---|
[31510] | 315 |
|
---|
| 316 | /**
|
---|
| 317 | * Read input.
|
---|
| 318 | *
|
---|
| 319 | * @returns VBox status code.
|
---|
| 320 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
| 321 | * @param pvBuf Where to put the bytes we read.
|
---|
| 322 | * @param cbBuf Maximum nymber of bytes to read.
|
---|
| 323 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
| 324 | * If NULL the entire buffer must be filled for a
|
---|
| 325 | * successful return.
|
---|
| 326 | */
|
---|
[86327] | 327 | static DECLCALLBACK(int) backRead(PCDBGCIO pIo, void *pvBuf, size_t cbBuf, size_t *pcbRead);
|
---|
[31510] | 328 |
|
---|
| 329 | /**
|
---|
| 330 | * Write (output).
|
---|
| 331 | *
|
---|
| 332 | * @returns VBox status code.
|
---|
| 333 | * @param pBack Pointer to VBoxDbgConsole::m_Back.
|
---|
| 334 | * @param pvBuf What to write.
|
---|
| 335 | * @param cbBuf Number of bytes to write.
|
---|
| 336 | * @param pcbWritten Where to store the number of bytes actually written.
|
---|
| 337 | * If NULL the entire buffer must be successfully written.
|
---|
| 338 | */
|
---|
[86327] | 339 | static DECLCALLBACK(int) backWrite(PCDBGCIO pIo, const void *pvBuf, size_t cbBuf, size_t *pcbWritten);
|
---|
[31510] | 340 |
|
---|
| 341 | /**
|
---|
[86329] | 342 | * @copydoc DBGCIO::pfnSetReady
|
---|
[31510] | 343 | */
|
---|
[86327] | 344 | static DECLCALLBACK(void) backSetReady(PCDBGCIO pIo, bool fReady);
|
---|
[31510] | 345 |
|
---|
| 346 | /**
|
---|
| 347 | * The Debugger Console Thread
|
---|
| 348 | *
|
---|
| 349 | * @returns VBox status code (ignored).
|
---|
| 350 | * @param Thread The thread handle.
|
---|
| 351 | * @param pvUser Pointer to the VBoxDbgConsole object.s
|
---|
| 352 | */
|
---|
| 353 | static DECLCALLBACK(int) backThread(RTTHREAD Thread, void *pvUser);
|
---|
| 354 |
|
---|
| 355 | /** @} */
|
---|
| 356 |
|
---|
| 357 | protected:
|
---|
| 358 | /**
|
---|
| 359 | * Processes GUI command posted by the console thread.
|
---|
| 360 | *
|
---|
| 361 | * Qt3 isn't thread safe on any platform, meaning there is no locking, so, as
|
---|
| 362 | * a result we have to be very careful. All operations on objects which we share
|
---|
| 363 | * with the main thread has to be posted to it so it can perform it.
|
---|
| 364 | */
|
---|
| 365 | bool event(QEvent *pEvent);
|
---|
| 366 |
|
---|
[64690] | 367 | /**
|
---|
| 368 | * For implementing keyboard shortcuts.
|
---|
| 369 | *
|
---|
| 370 | * @param pEvent The key event.
|
---|
| 371 | */
|
---|
| 372 | void keyReleaseEvent(QKeyEvent *pEvent);
|
---|
| 373 |
|
---|
[31510] | 374 | protected:
|
---|
| 375 | /** The output widget. */
|
---|
| 376 | VBoxDbgConsoleOutput *m_pOutput;
|
---|
| 377 | /** The input widget. */
|
---|
| 378 | VBoxDbgConsoleInput *m_pInput;
|
---|
| 379 | /** A hack to restore focus to the combobox after a command execution. */
|
---|
| 380 | bool m_fInputRestoreFocus;
|
---|
| 381 | /** The input buffer. */
|
---|
| 382 | char *m_pszInputBuf;
|
---|
| 383 | /** The amount of input in the buffer. */
|
---|
| 384 | size_t m_cbInputBuf;
|
---|
| 385 | /** The allocated size of the buffer. */
|
---|
| 386 | size_t m_cbInputBufAlloc;
|
---|
| 387 |
|
---|
| 388 | /** The output buffer. */
|
---|
| 389 | char *m_pszOutputBuf;
|
---|
| 390 | /** The amount of output in the buffer. */
|
---|
| 391 | size_t m_cbOutputBuf;
|
---|
| 392 | /** The allocated size of the buffer. */
|
---|
| 393 | size_t m_cbOutputBufAlloc;
|
---|
| 394 | /** The timer object used to process output in a delayed fashion. */
|
---|
| 395 | QTimer *m_pTimer;
|
---|
| 396 | /** Set when an output update is pending. */
|
---|
| 397 | bool volatile m_fUpdatePending;
|
---|
| 398 |
|
---|
| 399 | /** The debugger console thread. */
|
---|
| 400 | RTTHREAD m_Thread;
|
---|
| 401 | /** The event semaphore used to signal the debug console thread about input. */
|
---|
| 402 | RTSEMEVENT m_EventSem;
|
---|
| 403 | /** The critical section used to lock the object. */
|
---|
| 404 | RTCRITSECT m_Lock;
|
---|
| 405 | /** When set the thread will cause the debug console thread to terminate. */
|
---|
| 406 | bool volatile m_fTerminate;
|
---|
| 407 | /** Has the thread terminated?
|
---|
| 408 | * Used to do the right thing in closeEvent; the console is dead if the
|
---|
| 409 | * thread has terminated. */
|
---|
| 410 | bool volatile m_fThreadTerminated;
|
---|
| 411 |
|
---|
| 412 | /** The debug console backend structure.
|
---|
[86327] | 413 | * Use VBOXDBGCONSOLE_FROM_DBGCIO to convert the DBGCIO pointer to a object pointer. */
|
---|
[31510] | 414 | struct VBoxDbgConsoleBack
|
---|
| 415 | {
|
---|
[86327] | 416 | DBGCIO Core;
|
---|
[31510] | 417 | VBoxDbgConsole *pSelf;
|
---|
| 418 | } m_Back;
|
---|
| 419 |
|
---|
| 420 | /**
|
---|
| 421 | * Converts a pointer to VBoxDbgConsole::m_Back to VBoxDbgConsole pointer.
|
---|
| 422 | * @todo find a better way because offsetof is undefined on objects and g++ gets very noisy because of that.
|
---|
| 423 | */
|
---|
[86327] | 424 | # define VBOXDBGCONSOLE_FROM_DBGCIO(pIo) ( ((struct VBoxDbgConsoleBack *)(pBack))->pSelf )
|
---|
[31510] | 425 |
|
---|
| 426 | /** Change focus to the input field. */
|
---|
| 427 | QAction *m_pFocusToInput;
|
---|
| 428 | /** Change focus to the output viewer widget. */
|
---|
| 429 | QAction *m_pFocusToOutput;
|
---|
| 430 | };
|
---|
| 431 |
|
---|
| 432 |
|
---|
| 433 | /**
|
---|
| 434 | * Simple event class for push certain operations over
|
---|
| 435 | * onto the GUI thread.
|
---|
| 436 | */
|
---|
| 437 | class VBoxDbgConsoleEvent : public QEvent
|
---|
| 438 | {
|
---|
| 439 | public:
|
---|
| 440 | typedef enum { kUpdate, kInputEnable, kTerminatedUser, kTerminatedOther } VBoxDbgConsoleEventType;
|
---|
| 441 | enum { kEventNumber = QEvent::User + 42 };
|
---|
| 442 |
|
---|
| 443 | VBoxDbgConsoleEvent(VBoxDbgConsoleEventType enmCommand)
|
---|
| 444 | : QEvent((QEvent::Type)kEventNumber), m_enmCommand(enmCommand)
|
---|
| 445 | {
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | VBoxDbgConsoleEventType command() const
|
---|
| 449 | {
|
---|
| 450 | return m_enmCommand;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | private:
|
---|
| 454 | VBoxDbgConsoleEventType m_enmCommand;
|
---|
| 455 | };
|
---|
| 456 |
|
---|
| 457 |
|
---|
[76564] | 458 | #endif /* !DEBUGGER_INCLUDED_SRC_VBoxDbgConsole_h */
|
---|
[31510] | 459 |
|
---|