VirtualBox

source: vbox/trunk/src/VBox/Debugger/DBGConsole.cpp@ 101107

Last change on this file since 101107 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.3 KB
Line 
1/* $Id: DBGConsole.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
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
26 */
27
28
29/** @page pg_dbgc DBGC - The Debug Console
30 *
31 * The debugger console is an early attempt to make some interactive
32 * debugging facilities for the VirtualBox VMM. It was initially only
33 * accessible thru a telnet session in debug builds. Later it was hastily built
34 * into the VBoxDbg module with a very simple Qt wrapper around it.
35 *
36 * The current state is that it's by default shipped with all standard
37 * VirtualBox builds. The GUI component is by default accessible in all
38 * non-release builds, while release builds require extra data, environment or
39 * command line options to make it visible.
40 *
41 * Now, even if we ship it with all standard builds we would like it to remain
42 * an optional feature that can be omitted when building VirtualBox. Therefore,
43 * all external code interfacing DBGC need to be enclosed in
44 * \#ifdef VBOX_WITH_DEBUGGER blocks. This is mandatory for components that
45 * register external commands.
46 *
47 *
48 * @section sec_dbgc_op Operation
49 *
50 * The console will process commands in a manner similar to the OS/2 and Windows
51 * kernel debuggers. This means ';' is a command separator and that when
52 * possible we'll use the same command names as these two uses. As an
53 * alternative we intent to provide a set of gdb-like commands as well and let
54 * the user decide which should take precedence.
55 *
56 *
57 * @subsection sec_dbg_op_numbers Numbers
58 *
59 * Numbers are hexadecimal unless specified with a prefix indicating
60 * elsewise. Prefixes:
61 * - '0x' - hexadecimal.
62 * - '0n' - decimal
63 * - '0t' - octal.
64 * - '0y' - binary.
65 *
66 * Some of the prefixes are a bit uncommon, the reason for this that the
67 * typical binary prefix '0b' can also be a hexadecimal value since no prefix or
68 * suffix is required for such values. Ditto for '0n' and '0' for decimal and
69 * octal.
70 *
71 * The '`' can be used in the numeric value to separate parts as the user
72 * wishes. Generally, though the debugger may use it in output as thousand
73 * separator in decimal numbers and 32-bit separator in hex numbers.
74 *
75 * For historical reasons, a 'h' suffix is suffered on hex numbers. Unlike most
76 * assemblers, a leading 0 before a-f is not required with the 'h' suffix.
77 *
78 * The prefix '0i' can be used instead of '0n', as it was the early decimal
79 * prefix employed by DBGC. It's being deprecated and may be removed later.
80 *
81 *
82 * @subsection sec_dbg_op_strings Strings and Symbols
83 *
84 * The debugger will try to guess, convert or promote what the type of an
85 * argument to a command, function or operator based on the input description of
86 * the receiver. If the user wants to make it clear to the debugger that
87 * something is a string, put it inside double quotes. Symbols should use
88 * single quotes, though we're current still a bit flexible on this point.
89 *
90 * If you need to put a quote character inside the quoted text, you escape it by
91 * repating it once: echo "printf(""hello world"");"
92 *
93 *
94 * @subsection sec_dbg_op_address Addressing modes
95 *
96 * - Default is flat. For compatibility '%' also means flat.
97 * - Segmented addresses are specified selector:offset.
98 * - Physical addresses are specified using '%%'.
99 * - The default target for the addressing is the guest context, the '#'
100 * will override this and set it to the host.
101 * Note that several operations won't work on host addresses.
102 *
103 * The '%', '%%' and '#' prefixes is implemented as unary operators, while ':'
104 * is a binary operator. Operator precedence takes care of evaluation order.
105 *
106 *
107 * @subsection sec_dbg_op_c_operators C/C++ Operators
108 *
109 * Most unary and binary arithmetic, comparison, logical and bitwise C/C++
110 * operators are supported by the debugger, with the same precedence rules of
111 * course. There is one notable change made due to the unary '%' and '%%'
112 * operators, and that is that the modulo (remainder) operator is called 'mod'
113 * instead of '%'. This saves a lot of trouble separating argument.
114 *
115 * There are no assignment operators. Instead some simple global variable space
116 * is provided thru the 'set' and 'unset' commands and the unary '$' operator.
117 *
118 *
119 * @subsection sec_dbg_op_registers Registers
120 *
121 * All registers and their sub-fields exposed by the DBGF API are accessible via
122 * the '\@' operator. A few CPU register are accessible directly (as symbols)
123 * without using the '\@' operator. Hypervisor registers are accessible by
124 * prefixing the register name with a dot ('.').
125 *
126 *
127 * @subsection sec_dbg_op_commands Commands
128 *
129 * Commands names are case sensitive. By convention they are lower cased, starts
130 * with a letter but may contain digits and underscores afterwards. Operators
131 * are not allowed in the name (not even part of it), as we would risk
132 * misunderstanding it otherwise.
133 *
134 * Commands returns a status code.
135 *
136 * The '.' prefix indicates the set of external commands. External commands are
137 * command registered by VMM components.
138 *
139 *
140 * @subsection sec_dbg_op_functions Functions
141 *
142 * Functions are similar to commands, but return a variable and can only be used
143 * as part of an expression making up the argument of a command, function,
144 * operator or language statement (if we get around to implement that).
145 *
146 *
147 * @section sec_dbgc_logging Logging
148 *
149 * The idea is to be able to pass thru debug and release logs to the console
150 * if the user so wishes. This feature requires some kind of hook into the
151 * logger instance and while this was sketched it hasn't yet been implemented
152 * (dbgcProcessLog and DBGC::fLog).
153 *
154 * This feature has not materialized and probably never will.
155 *
156 *
157 * @section sec_dbgc_linking Linking and API
158 *
159 * The DBGC code is linked into the VBoxVMM module.
160 *
161 * IMachineDebugger may one day be extended with a DBGC interface so we can work
162 * with DBGC remotely without requiring TCP. Some questions about callbacks
163 * (for output) and security (you may wish to restrict users from debugging a
164 * VM) needs to be answered first though.
165 */
166
167
168/*********************************************************************************************************************************
169* Header Files *
170*********************************************************************************************************************************/
171#define LOG_GROUP LOG_GROUP_DBGC
172#include <VBox/dbg.h>
173#include <VBox/vmm/cfgm.h>
174#include <VBox/vmm/dbgf.h>
175#include <VBox/vmm/vmapi.h> /* VMR3GetVM() */
176#include <VBox/vmm/hm.h> /* HMR3IsEnabled */
177#include <VBox/vmm/nem.h> /* NEMR3IsEnabled */
178#include <VBox/err.h>
179#include <VBox/log.h>
180
181#include <iprt/asm.h>
182#include <iprt/assert.h>
183#include <iprt/file.h>
184#include <iprt/mem.h>
185#include <iprt/path.h>
186#include <iprt/string.h>
187
188#include "DBGCInternal.h"
189#include "DBGPlugIns.h"
190
191
192/*********************************************************************************************************************************
193* Internal Functions *
194*********************************************************************************************************************************/
195static int dbgcProcessLog(PDBGC pDbgc);
196
197
198/**
199 * Resolves a symbol (or tries to do so at least).
200 *
201 * @returns 0 on success.
202 * @returns VBox status on failure.
203 * @param pDbgc The debug console instance.
204 * @param pszSymbol The symbol name.
205 * @param enmType The result type. Specifying DBGCVAR_TYPE_GC_FAR may
206 * cause failure, avoid it.
207 * @param pResult Where to store the result.
208 */
209int dbgcSymbolGet(PDBGC pDbgc, const char *pszSymbol, DBGCVARTYPE enmType, PDBGCVAR pResult)
210{
211 int rc;
212
213 /*
214 * Builtin?
215 */
216 PCDBGCSYM pSymDesc = dbgcLookupRegisterSymbol(pDbgc, pszSymbol);
217 if (pSymDesc)
218 {
219 if (!pSymDesc->pfnGet)
220 return VERR_DBGC_PARSE_WRITEONLY_SYMBOL;
221 return pSymDesc->pfnGet(pSymDesc, &pDbgc->CmdHlp, enmType, pResult);
222 }
223
224 /*
225 * A typical register? (Guest only)
226 */
227 static const char s_szSixLetterRegisters[] =
228 "rflags;eflags;"
229 ;
230 static const char s_szThreeLetterRegisters[] =
231 "eax;rax;" "r10;" "r8d;r8w;r8b;" "cr0;" "dr0;"
232 "ebx;rbx;" "r11;" "r9d;r9w;r8b;" "dr1;"
233 "ecx;rcx;" "r12;" "cr2;" "dr2;"
234 "edx;rdx;" "r13;" "cr3;" "dr3;"
235 "edi;rdi;dil;" "r14;" "cr4;" "dr4;"
236 "esi;rsi;sil;" "r15;" "cr8;"
237 "ebp;rbp;"
238 "esp;rsp;" "dr6;"
239 "rip;eip;" "dr7;"
240 "efl;"
241 ;
242 static const char s_szTwoLetterRegisters[] =
243 "ax;al;ah;" "r8;"
244 "bx;bl;bh;" "r9;"
245 "cx;cl;ch;" "cs;"
246 "dx;dl;dh;" "ds;"
247 "di;" "es;"
248 "si;" "fs;"
249 "bp;" "gs;"
250 "sp;" "ss;"
251 "ip;"
252 ;
253 const char *pszRegSym = *pszSymbol == '.' ? pszSymbol + 1 : pszSymbol;
254 size_t const cchRegSym = strlen(pszRegSym);
255 if ( (cchRegSym == 2 && strstr(s_szTwoLetterRegisters, pszRegSym))
256 || (cchRegSym == 3 && strstr(s_szThreeLetterRegisters, pszRegSym))
257 || (cchRegSym == 6 && strstr(s_szSixLetterRegisters, pszRegSym)))
258 {
259 if (!strchr(pszSymbol, ';'))
260 {
261 DBGCVAR Var;
262 DBGCVAR_INIT_SYMBOL(&Var, pszSymbol);
263 rc = dbgcOpRegister(pDbgc, &Var, DBGCVAR_CAT_ANY, pResult);
264 if (RT_SUCCESS(rc))
265 return DBGCCmdHlpConvert(&pDbgc->CmdHlp, pResult, enmType, false /*fConvSyms*/, pResult);
266 }
267 }
268
269 /*
270 * Ask PDM.
271 */
272 /** @todo resolve symbols using PDM. */
273
274 /*
275 * Ask the debug info manager.
276 */
277 RTDBGSYMBOL Symbol;
278 rc = DBGFR3AsSymbolByName(pDbgc->pUVM, pDbgc->hDbgAs, pszSymbol, &Symbol, NULL);
279 if (RT_SUCCESS(rc))
280 {
281 /*
282 * Default return is a flat gc address.
283 */
284 DBGCVAR_INIT_GC_FLAT(pResult, Symbol.Value);
285 if (Symbol.cb)
286 DBGCVAR_SET_RANGE(pResult, DBGCVAR_RANGE_BYTES, Symbol.cb);
287
288 switch (enmType)
289 {
290 /* nothing to do. */
291 case DBGCVAR_TYPE_GC_FLAT:
292 case DBGCVAR_TYPE_ANY:
293 return VINF_SUCCESS;
294
295 /* impossible at the moment. */
296 case DBGCVAR_TYPE_GC_FAR:
297 return VERR_DBGC_PARSE_CONVERSION_FAILED;
298
299 /* simply make it numeric. */
300 case DBGCVAR_TYPE_NUMBER:
301 pResult->enmType = DBGCVAR_TYPE_NUMBER;
302 pResult->u.u64Number = Symbol.Value;
303 return VINF_SUCCESS;
304
305 /* cast it. */
306 case DBGCVAR_TYPE_GC_PHYS:
307 case DBGCVAR_TYPE_HC_FLAT:
308 case DBGCVAR_TYPE_HC_PHYS:
309 return DBGCCmdHlpConvert(&pDbgc->CmdHlp, pResult, enmType, false /*fConvSyms*/, pResult);
310
311 default:
312 AssertMsgFailed(("Internal error enmType=%d\n", enmType));
313 return VERR_INVALID_PARAMETER;
314 }
315 }
316
317 return VERR_DBGC_PARSE_NOT_IMPLEMENTED;
318}
319
320
321/**
322 * Process all commands currently in the buffer.
323 *
324 * @returns VBox status code. Any error indicates the termination of the console session.
325 * @param pDbgc Debugger console instance data.
326 * @param fNoExecute Indicates that no commands should actually be executed.
327 */
328static int dbgcProcessCommands(PDBGC pDbgc, bool fNoExecute)
329{
330 /** @todo Replace this with a sh/ksh/csh/rexx like toplevel language that
331 * allows doing function, loops, if, cases, and such. */
332 int rc = VINF_SUCCESS;
333 while (pDbgc->cInputLines)
334 {
335 /*
336 * Empty the log buffer if we're hooking the log.
337 */
338 if (pDbgc->fLog)
339 {
340 rc = dbgcProcessLog(pDbgc);
341 if (RT_FAILURE(rc))
342 break;
343 }
344
345 if (pDbgc->iRead == pDbgc->iWrite)
346 {
347 AssertMsgFailed(("The input buffer is empty while cInputLines=%d!\n", pDbgc->cInputLines));
348 pDbgc->cInputLines = 0;
349 return 0;
350 }
351
352 /*
353 * Copy the command to the parse buffer.
354 */
355 char chQuote = 0;
356 char ch;
357 char *psz = &pDbgc->achInput[pDbgc->iRead];
358 char *pszTrg = &pDbgc->achScratch[0];
359 AssertCompile(sizeof(pDbgc->achScratch) > sizeof(pDbgc->achInput));
360 while ((ch = *psz++) != '\0')
361 {
362 /* ';' and '\n' are termination characters, except for when they are
363 inside quotes. So, track quoting. */
364 if (ch == '"' || ch == '\'')
365 chQuote = chQuote == ch ? 0 : chQuote == 0 ? ch : chQuote;
366 else if ((ch == ';' || ch == '\n') && chQuote == 0)
367 break;
368
369 *pszTrg = ch;
370
371 if (psz == &pDbgc->achInput[sizeof(pDbgc->achInput)])
372 psz = &pDbgc->achInput[0];
373
374 /** @todo r=bird: off by one issue here? */
375 if (psz == &pDbgc->achInput[pDbgc->iWrite])
376 {
377 AssertMsgFailed(("The buffer contains no commands while cInputLines=%d!\n", pDbgc->cInputLines));
378 pDbgc->cInputLines = 0;
379 return 0;
380 }
381
382 pszTrg++;
383 }
384 *pszTrg = '\0';
385
386 /*
387 * Advance the buffer.
388 */
389 pDbgc->iRead = psz - &pDbgc->achInput[0];
390 if (ch == '\n')
391 pDbgc->cInputLines--;
392
393 /*
394 * Parse and execute this command.
395 */
396 pDbgc->pszScratch = pszTrg + 1;
397 pDbgc->iArg = 0;
398 rc = dbgcEvalCommand(pDbgc, &pDbgc->achScratch[0], pszTrg - &pDbgc->achScratch[0], fNoExecute);
399 if ( rc == VERR_DBGC_QUIT
400 || rc == VWRN_DBGC_CMD_PENDING)
401 break;
402 rc = VINF_SUCCESS; /* ignore other statuses */
403 }
404
405 return rc;
406}
407
408
409/**
410 * Handle input buffer overflow.
411 *
412 * Will read any available input looking for a '\n' to reset the buffer on.
413 *
414 * @returns VBox status code.
415 * @param pDbgc Debugger console instance data.
416 */
417static int dbgcInputOverflow(PDBGC pDbgc)
418{
419 /*
420 * Assert overflow status and reset the input buffer.
421 */
422 if (!pDbgc->fInputOverflow)
423 {
424 pDbgc->fInputOverflow = true;
425 pDbgc->iRead = pDbgc->iWrite = 0;
426 pDbgc->cInputLines = 0;
427 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "Input overflow!!\n");
428 }
429
430 /*
431 * Eat input till no more or there is a '\n'.
432 * When finding a '\n' we'll continue normal processing.
433 */
434 while (pDbgc->pIo->pfnInput(pDbgc->pIo, 0))
435 {
436 size_t cbRead;
437 int rc = pDbgc->pIo->pfnRead(pDbgc->pIo, &pDbgc->achInput[0], sizeof(pDbgc->achInput) - 1, &cbRead);
438 if (RT_FAILURE(rc))
439 return rc;
440 char *psz = (char *)memchr(&pDbgc->achInput[0], '\n', cbRead);
441 if (psz)
442 {
443 pDbgc->fInputOverflow = false;
444 pDbgc->iRead = psz - &pDbgc->achInput[0] + 1;
445 pDbgc->iWrite = (unsigned)cbRead;
446 pDbgc->cInputLines = 0;
447 break;
448 }
449 }
450
451 return 0;
452}
453
454
455/**
456 * Read input and do some preprocessing.
457 *
458 * @returns VBox status code.
459 * In addition to the iWrite and achInput, cInputLines is maintained.
460 * In case of an input overflow the fInputOverflow flag will be set.
461 * @param pDbgc Debugger console instance data.
462 */
463static int dbgcInputRead(PDBGC pDbgc)
464{
465 /*
466 * We have ready input.
467 * Read it till we don't have any or we have a full input buffer.
468 */
469 int rc = 0;
470 do
471 {
472 /*
473 * More available buffer space?
474 */
475 size_t cbLeft;
476 if (pDbgc->iWrite > pDbgc->iRead)
477 cbLeft = sizeof(pDbgc->achInput) - pDbgc->iWrite - (pDbgc->iRead == 0);
478 else
479 cbLeft = pDbgc->iRead - pDbgc->iWrite - 1;
480 if (!cbLeft)
481 {
482 /* overflow? */
483 if (!pDbgc->cInputLines)
484 rc = dbgcInputOverflow(pDbgc);
485 break;
486 }
487
488 /*
489 * Read one char and interpret it.
490 */
491 char achRead[128];
492 size_t cbRead;
493 rc = pDbgc->pIo->pfnRead(pDbgc->pIo, &achRead[0], RT_MIN(cbLeft, sizeof(achRead)), &cbRead);
494 if (RT_FAILURE(rc))
495 return rc;
496 char *psz = &achRead[0];
497 while (cbRead-- > 0)
498 {
499 char ch = *psz++;
500 switch (ch)
501 {
502 /*
503 * Ignore.
504 */
505 case '\0':
506 case '\r':
507 case '\a':
508 break;
509
510 /*
511 * Backspace.
512 */
513 case '\b':
514 Log2(("DBGC: backspace\n"));
515 if (pDbgc->iRead != pDbgc->iWrite)
516 {
517 unsigned iWriteUndo = pDbgc->iWrite;
518 if (pDbgc->iWrite)
519 pDbgc->iWrite--;
520 else
521 pDbgc->iWrite = sizeof(pDbgc->achInput) - 1;
522
523 if (pDbgc->achInput[pDbgc->iWrite] == '\n')
524 pDbgc->iWrite = iWriteUndo;
525 }
526 break;
527
528 /*
529 * Add char to buffer.
530 */
531 case '\t':
532 case '\n':
533 case ';':
534 switch (ch)
535 {
536 case '\t': ch = ' '; break;
537 case '\n': pDbgc->cInputLines++; break;
538 }
539 RT_FALL_THRU();
540 default:
541 Log2(("DBGC: ch=%02x\n", (unsigned char)ch));
542 pDbgc->achInput[pDbgc->iWrite] = ch;
543 if (++pDbgc->iWrite >= sizeof(pDbgc->achInput))
544 pDbgc->iWrite = 0;
545 break;
546 }
547 }
548
549 /* Terminate it to make it easier to read in the debugger. */
550 pDbgc->achInput[pDbgc->iWrite] = '\0';
551 } while (pDbgc->pIo->pfnInput(pDbgc->pIo, 0));
552
553 return rc;
554}
555
556
557/**
558 * Reads input, parses it and executes commands on '\n'.
559 *
560 * @returns VBox status code.
561 * @param pDbgc Debugger console instance data.
562 * @param fNoExecute Indicates that no commands should actually be executed.
563 */
564int dbgcProcessInput(PDBGC pDbgc, bool fNoExecute)
565{
566 /*
567 * We know there's input ready, so let's read it first.
568 */
569 int rc = dbgcInputRead(pDbgc);
570 if (RT_FAILURE(rc))
571 return rc;
572
573 /*
574 * Now execute any ready commands.
575 */
576 if (pDbgc->cInputLines)
577 {
578 pDbgc->pIo->pfnSetReady(pDbgc->pIo, false);
579 pDbgc->fReady = false;
580 rc = dbgcProcessCommands(pDbgc, fNoExecute);
581 if (RT_SUCCESS(rc) && rc != VWRN_DBGC_CMD_PENDING)
582 pDbgc->fReady = true;
583
584 if ( RT_SUCCESS(rc)
585 && pDbgc->iRead == pDbgc->iWrite
586 && pDbgc->fReady)
587 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
588
589 if ( RT_SUCCESS(rc)
590 && pDbgc->fReady)
591 pDbgc->pIo->pfnSetReady(pDbgc->pIo, true);
592 }
593 /*
594 * else - we have incomplete line, so leave it in the buffer and
595 * wait for more input.
596 *
597 * Windows telnet client is in "character at a time" mode by
598 * default and putty sends eol as a separate packet that will be
599 * most likely read separately from the command line it
600 * terminates.
601 */
602
603 return rc;
604}
605
606
607/**
608 * Gets the event context identifier string.
609 * @returns Read only string.
610 * @param enmCtx The context.
611 */
612DECLHIDDEN(const char *) dbgcGetEventCtx(DBGFEVENTCTX enmCtx)
613{
614 switch (enmCtx)
615 {
616 case DBGFEVENTCTX_RAW: return "raw";
617 case DBGFEVENTCTX_REM: return "rem";
618 case DBGFEVENTCTX_HM: return "hwaccl";
619 case DBGFEVENTCTX_HYPER: return "hyper";
620 case DBGFEVENTCTX_OTHER: return "other";
621
622 case DBGFEVENTCTX_INVALID: return "!Invalid Event Ctx!";
623 default:
624 AssertMsgFailed(("enmCtx=%d\n", enmCtx));
625 return "!Unknown Event Ctx!";
626 }
627}
628
629
630/**
631 * Looks up a generic debug event.
632 *
633 * @returns Pointer to DBGCSXEVT structure if found, otherwise NULL.
634 * @param enmType The possibly generic event to find the descriptor for.
635 */
636DECLHIDDEN(PCDBGCSXEVT) dbgcEventLookup(DBGFEVENTTYPE enmType)
637{
638 uint32_t i = g_cDbgcSxEvents;
639 while (i-- > 0)
640 if (g_aDbgcSxEvents[i].enmType == enmType)
641 return &g_aDbgcSxEvents[i];
642 return NULL;
643}
644
645
646/**
647 * Processes debugger events.
648 *
649 * @returns VBox status code.
650 * @param pDbgc DBGC Instance data.
651 * @param pEvent Pointer to event data.
652 */
653static int dbgcProcessEvent(PDBGC pDbgc, PCDBGFEVENT pEvent)
654{
655 /*
656 * Flush log first.
657 */
658 if (pDbgc->fLog)
659 {
660 int rc = dbgcProcessLog(pDbgc);
661 if (RT_FAILURE(rc))
662 return rc;
663 }
664
665 /*
666 * Process the event.
667 */
668 pDbgc->pszScratch = &pDbgc->achInput[0];
669 pDbgc->iArg = 0;
670 bool fPrintPrompt = true;
671 int rc = VINF_SUCCESS;
672 VMCPUID const idCpuSaved = pDbgc->idCpu;
673 switch (pEvent->enmType)
674 {
675 /*
676 * The first part is events we have initiated with commands.
677 */
678 case DBGFEVENT_HALT_DONE:
679 {
680 /** @todo add option to suppress this on CPUs that aren't selected (like
681 * fRegTerse). */
682 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: CPU %u has halted! (%s)\n",
683 pEvent->idCpu, pEvent->idCpu, dbgcGetEventCtx(pEvent->enmCtx));
684 if (RT_SUCCESS(rc))
685 rc = DBGCCmdHlpRegPrintf(&pDbgc->CmdHlp, pEvent->idCpu, -1, pDbgc->fRegTerse);
686 break;
687 }
688
689
690 /*
691 * The second part is events which can occur at any time.
692 */
693 case DBGFEVENT_FATAL_ERROR:
694 {
695 pDbgc->idCpu = pEvent->idCpu;
696 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbf event/%u: Fatal error! (%s)\n",
697 pEvent->idCpu, dbgcGetEventCtx(pEvent->enmCtx));
698 if (RT_SUCCESS(rc))
699 rc = DBGCCmdHlpRegPrintf(&pDbgc->CmdHlp, pEvent->idCpu, -1, pDbgc->fRegTerse);
700 break;
701 }
702
703 case DBGFEVENT_BREAKPOINT:
704 case DBGFEVENT_BREAKPOINT_IO:
705 case DBGFEVENT_BREAKPOINT_MMIO:
706 case DBGFEVENT_BREAKPOINT_HYPER:
707 {
708 pDbgc->idCpu = pEvent->idCpu;
709 rc = dbgcBpExec(pDbgc, pEvent->u.Bp.hBp);
710 switch (rc)
711 {
712 case VERR_DBGC_BP_NOT_FOUND:
713 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: Unknown breakpoint %u! (%s)\n",
714 pEvent->idCpu, pEvent->u.Bp.hBp, dbgcGetEventCtx(pEvent->enmCtx));
715 break;
716
717 case VINF_DBGC_BP_NO_COMMAND:
718 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: Breakpoint %u! (%s)\n",
719 pEvent->idCpu, pEvent->u.Bp.hBp, dbgcGetEventCtx(pEvent->enmCtx));
720 break;
721
722 case VINF_BUFFER_OVERFLOW:
723 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: Breakpoint %u! Command too long to execute! (%s)\n",
724 pEvent->idCpu, pEvent->u.Bp.hBp, dbgcGetEventCtx(pEvent->enmCtx));
725 break;
726
727 default:
728 break;
729 }
730 if (RT_SUCCESS(rc) && DBGFR3IsHalted(pDbgc->pUVM, pEvent->idCpu))
731 {
732 rc = DBGCCmdHlpRegPrintf(&pDbgc->CmdHlp, pEvent->idCpu, -1, pDbgc->fRegTerse);
733
734 /* Set the resume flag to ignore the breakpoint when resuming execution. */
735 if ( RT_SUCCESS(rc)
736 && pEvent->enmType == DBGFEVENT_BREAKPOINT)
737 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r eflags.rf = 1");
738 }
739 else
740 pDbgc->idCpu = idCpuSaved;
741 break;
742 }
743
744 case DBGFEVENT_STEPPED:
745 case DBGFEVENT_STEPPED_HYPER:
746 {
747 if (!pDbgc->cMultiStepsLeft || pEvent->idCpu != idCpuSaved)
748 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: Single step! (%s)\n",
749 pEvent->idCpu, dbgcGetEventCtx(pEvent->enmCtx));
750 else
751 pDbgc->cMultiStepsLeft -= 1;
752 if (RT_SUCCESS(rc))
753 {
754 if (pDbgc->fStepTraceRegs)
755 rc = DBGCCmdHlpRegPrintf(&pDbgc->CmdHlp, pEvent->idCpu, -1, pDbgc->fRegTerse);
756 else
757 {
758 char szCmd[80];
759 if (DBGFR3CpuIsIn64BitCode(pDbgc->pUVM, pDbgc->idCpu))
760 rc = DBGFR3RegPrintf(pDbgc->pUVM, pDbgc->idCpu, szCmd, sizeof(szCmd), "u %016VR{rip} L 0");
761 else if (DBGFR3CpuIsInV86Code(pDbgc->pUVM, pDbgc->idCpu))
762 rc = DBGFR3RegPrintf(pDbgc->pUVM, pDbgc->idCpu, szCmd, sizeof(szCmd), "uv86 %04VR{cs}:%08VR{eip} L 0");
763 else
764 rc = DBGFR3RegPrintf(pDbgc->pUVM, pDbgc->idCpu, szCmd, sizeof(szCmd), "u %04VR{cs}:%08VR{eip} L 0");
765 if (RT_SUCCESS(rc))
766 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "%s", szCmd);
767 }
768 }
769
770 /* If multi-stepping, take the next step: */
771 if (pDbgc->cMultiStepsLeft > 0 && pEvent->idCpu == idCpuSaved)
772 {
773 int rc2 = DBGFR3StepEx(pDbgc->pUVM, pDbgc->idCpu, DBGF_STEP_F_INTO, NULL, NULL, 0, pDbgc->uMultiStepStrideLength);
774 if (RT_SUCCESS(rc2))
775 fPrintPrompt = false;
776 else
777 DBGCCmdHlpFailRc(&pDbgc->CmdHlp, pDbgc->pMultiStepCmd, rc2, "DBGFR3StepEx(,,DBGF_STEP_F_INTO,) failed");
778 }
779 else
780 pDbgc->idCpu = pEvent->idCpu;
781 break;
782 }
783
784 case DBGFEVENT_ASSERTION_HYPER:
785 {
786 pDbgc->idCpu = pEvent->idCpu;
787 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
788 "\ndbgf event/%u: Hypervisor Assertion! (%s)\n"
789 "%s"
790 "%s"
791 "\n",
792 pEvent->idCpu,
793 dbgcGetEventCtx(pEvent->enmCtx),
794 pEvent->u.Assert.pszMsg1,
795 pEvent->u.Assert.pszMsg2);
796 if (RT_SUCCESS(rc))
797 rc = DBGCCmdHlpRegPrintf(&pDbgc->CmdHlp, pEvent->idCpu, -1, pDbgc->fRegTerse);
798 break;
799 }
800
801 case DBGFEVENT_DEV_STOP:
802 {
803 pDbgc->idCpu = pEvent->idCpu;
804 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
805 "\n"
806 "dbgf event/%u: DBGFSTOP (%s)\n"
807 "File: %s\n"
808 "Line: %d\n"
809 "Function: %s\n",
810 pEvent->idCpu,
811 dbgcGetEventCtx(pEvent->enmCtx),
812 pEvent->u.Src.pszFile,
813 pEvent->u.Src.uLine,
814 pEvent->u.Src.pszFunction);
815 if (RT_SUCCESS(rc) && pEvent->u.Src.pszMessage && *pEvent->u.Src.pszMessage)
816 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
817 "Message: %s\n",
818 pEvent->u.Src.pszMessage);
819 if (RT_SUCCESS(rc))
820 rc = DBGCCmdHlpRegPrintf(&pDbgc->CmdHlp, pEvent->idCpu, -1, pDbgc->fRegTerse);
821 break;
822 }
823
824
825 case DBGFEVENT_INVALID_COMMAND:
826 {
827 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Invalid command event!\n");
828 break;
829 }
830
831 case DBGFEVENT_POWERING_OFF:
832 {
833 pDbgc->fReady = false;
834 pDbgc->pIo->pfnSetReady(pDbgc->pIo, false);
835 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\nVM is powering off!\n");
836 fPrintPrompt = false;
837 rc = VERR_GENERAL_FAILURE;
838 break;
839 }
840
841
842 default:
843 {
844 /*
845 * Probably a generic event. Look it up to find its name.
846 */
847 PCDBGCSXEVT pEvtDesc = dbgcEventLookup(pEvent->enmType);
848 if (pEvtDesc)
849 {
850 if (pEvtDesc->enmKind == kDbgcSxEventKind_Interrupt)
851 {
852 Assert(pEvtDesc->pszDesc);
853 Assert(pEvent->u.Generic.cArgs == 1);
854 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: %s no %#llx! (%s)\n",
855 pEvent->idCpu, pEvtDesc->pszDesc, pEvent->u.Generic.auArgs[0], pEvtDesc->pszName);
856 }
857 else if (pEvtDesc->fFlags & DBGCSXEVT_F_BUGCHECK)
858 {
859 Assert(pEvent->u.Generic.cArgs >= 5);
860 char szDetails[512];
861 DBGFR3FormatBugCheck(pDbgc->pUVM, szDetails, sizeof(szDetails), pEvent->u.Generic.auArgs[0],
862 pEvent->u.Generic.auArgs[1], pEvent->u.Generic.auArgs[2],
863 pEvent->u.Generic.auArgs[3], pEvent->u.Generic.auArgs[4]);
864 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: %s %s%s!\n%s", pEvent->idCpu,
865 pEvtDesc->pszName, pEvtDesc->pszDesc ? "- " : "",
866 pEvtDesc->pszDesc ? pEvtDesc->pszDesc : "", szDetails);
867 }
868 else if ( (pEvtDesc->fFlags & DBGCSXEVT_F_TAKE_ARG)
869 || pEvent->u.Generic.cArgs > 1
870 || ( pEvent->u.Generic.cArgs == 1
871 && pEvent->u.Generic.auArgs[0] != 0))
872 {
873 if (pEvtDesc->pszDesc)
874 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: %s - %s!",
875 pEvent->idCpu, pEvtDesc->pszName, pEvtDesc->pszDesc);
876 else
877 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: %s!",
878 pEvent->idCpu, pEvtDesc->pszName);
879 if (pEvent->u.Generic.cArgs <= 1)
880 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " arg=%#llx\n", pEvent->u.Generic.auArgs[0]);
881 else
882 {
883 for (uint32_t i = 0; i < pEvent->u.Generic.cArgs; i++)
884 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " args[%u]=%#llx", i, pEvent->u.Generic.auArgs[i]);
885 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n");
886 }
887 }
888 else
889 {
890 if (pEvtDesc->pszDesc)
891 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: %s - %s!\n",
892 pEvent->idCpu, pEvtDesc->pszName, pEvtDesc->pszDesc);
893 else
894 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event/%u: %s!\n",
895 pEvent->idCpu, pEvtDesc->pszName);
896 }
897 }
898 else
899 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Unknown event %d on CPU %u!\n",
900 pEvent->enmType, pEvent->idCpu);
901 break;
902 }
903 }
904
905 /*
906 * Prompt, anyone?
907 */
908 if (fPrintPrompt && RT_SUCCESS(rc))
909 {
910 /** @todo add CPU indicator to the prompt if an SMP VM? */
911 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
912 pDbgc->fReady = true;
913 if (RT_SUCCESS(rc))
914 pDbgc->pIo->pfnSetReady(pDbgc->pIo, true);
915 pDbgc->cMultiStepsLeft = 0;
916 }
917
918 return rc;
919}
920
921
922/**
923 * Prints any log lines from the log buffer.
924 *
925 * The caller must not call function this unless pDbgc->fLog is set.
926 *
927 * @returns VBox status code. (output related)
928 * @param pDbgc Debugger console instance data.
929 */
930static int dbgcProcessLog(PDBGC pDbgc)
931{
932 /** @todo */
933 NOREF(pDbgc);
934 return 0;
935}
936
937/** @callback_method_impl{FNRTDBGCFGLOG} */
938static DECLCALLBACK(void) dbgcDbgCfgLogCallback(RTDBGCFG hDbgCfg, uint32_t iLevel, const char *pszMsg, void *pvUser)
939{
940 /** @todo Add symbol noise setting. */
941 NOREF(hDbgCfg); NOREF(iLevel);
942 PDBGC pDbgc = (PDBGC)pvUser;
943 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s", pszMsg);
944}
945
946
947/**
948 * Run the debugger console.
949 *
950 * @returns VBox status code.
951 * @param pDbgc Pointer to the debugger console instance data.
952 */
953int dbgcRun(PDBGC pDbgc)
954{
955 /*
956 * We're ready for commands now.
957 */
958 pDbgc->fReady = true;
959 pDbgc->pIo->pfnSetReady(pDbgc->pIo, true);
960
961 /*
962 * Main Debugger Loop.
963 *
964 * This loop will either block on waiting for input or on waiting on
965 * debug events. If we're forwarding the log we cannot wait for long
966 * before we must flush the log.
967 */
968 int rc;
969 for (;;)
970 {
971 rc = VERR_SEM_OUT_OF_TURN;
972 if (pDbgc->pUVM)
973 rc = DBGFR3QueryWaitable(pDbgc->pUVM);
974
975 if (RT_SUCCESS(rc))
976 {
977 /*
978 * Wait for a debug event.
979 */
980 DBGFEVENT Event;
981 rc = DBGFR3EventWait(pDbgc->pUVM, pDbgc->fLog ? 1 : 32, &Event);
982 if (RT_SUCCESS(rc))
983 {
984 rc = dbgcProcessEvent(pDbgc, &Event);
985 if (RT_FAILURE(rc))
986 break;
987 }
988 else if (rc != VERR_TIMEOUT)
989 break;
990
991 /*
992 * Check for input.
993 */
994 if (pDbgc->pIo->pfnInput(pDbgc->pIo, 0))
995 {
996 rc = dbgcProcessInput(pDbgc, false /* fNoExecute */);
997 if (RT_FAILURE(rc))
998 break;
999 }
1000 }
1001 else if (rc == VERR_SEM_OUT_OF_TURN)
1002 {
1003 /*
1004 * Wait for input. If Logging is enabled we'll only wait very briefly.
1005 */
1006 if (pDbgc->pIo->pfnInput(pDbgc->pIo, pDbgc->fLog ? 1 : 1000))
1007 {
1008 rc = dbgcProcessInput(pDbgc, false /* fNoExecute */);
1009 if (RT_FAILURE(rc))
1010 break;
1011 }
1012 }
1013 else
1014 break;
1015
1016 /*
1017 * Forward log output.
1018 */
1019 if (pDbgc->fLog)
1020 {
1021 rc = dbgcProcessLog(pDbgc);
1022 if (RT_FAILURE(rc))
1023 break;
1024 }
1025 }
1026
1027 return rc;
1028}
1029
1030
1031/**
1032 * Run the init scripts, if present.
1033 *
1034 * @param pDbgc The console instance.
1035 */
1036static void dbgcRunInitScripts(PDBGC pDbgc)
1037{
1038 /*
1039 * Do the global one, if it exists.
1040 */
1041 if ( pDbgc->pszGlobalInitScript
1042 && *pDbgc->pszGlobalInitScript != '\0'
1043 && RTFileExists(pDbgc->pszGlobalInitScript))
1044 dbgcEvalScript(pDbgc, pDbgc->pszGlobalInitScript, true /*fAnnounce*/);
1045
1046 /*
1047 * Then do the local one, if it exists.
1048 */
1049 if ( pDbgc->pszLocalInitScript
1050 && *pDbgc->pszLocalInitScript != '\0'
1051 && RTFileExists(pDbgc->pszLocalInitScript))
1052 dbgcEvalScript(pDbgc, pDbgc->pszLocalInitScript, true /*fAnnounce*/);
1053}
1054
1055
1056/**
1057 * Reads the CFGM configuration of the DBGC.
1058 *
1059 * Popuplates the PDBGC::pszHistoryFile, PDBGC::pszGlobalInitScript and
1060 * PDBGC::pszLocalInitScript members.
1061 *
1062 * @returns VBox status code.
1063 * @param pDbgc The console instance.
1064 * @param pUVM The user mode VM handle.
1065 */
1066static int dbgcReadConfig(PDBGC pDbgc, PUVM pUVM)
1067{
1068 /*
1069 * Get and validate the configuration node.
1070 */
1071 PCFGMNODE pNode = CFGMR3GetChild(CFGMR3GetRootU(pUVM), "DBGC");
1072 int rc = CFGMR3ValidateConfig(pNode, "/DBGC/",
1073 "Enabled|"
1074 "HistoryFile|"
1075 "LocalInitScript|"
1076 "GlobalInitScript|",
1077 "*", "DBGC", 0);
1078 AssertRCReturn(rc, rc);
1079
1080 /*
1081 * Query the values.
1082 */
1083 char szHomeDefault[RTPATH_MAX];
1084 rc = RTPathUserHome(szHomeDefault, sizeof(szHomeDefault) - 32);
1085 AssertLogRelRCReturn(rc, rc);
1086 size_t cchHome = strlen(szHomeDefault);
1087
1088 /** @cfgm{/DBGC/HistoryFile, string, ${HOME}/.vboxdbgc-history}
1089 * The command history file of the VBox debugger. */
1090 rc = RTPathAppend(szHomeDefault, sizeof(szHomeDefault), ".vboxdbgc-history");
1091 AssertLogRelRCReturn(rc, rc);
1092
1093 char szPath[RTPATH_MAX];
1094 rc = CFGMR3QueryStringDef(pNode, "HistoryFile", szPath, sizeof(szPath), szHomeDefault);
1095 AssertLogRelRCReturn(rc, rc);
1096
1097 pDbgc->pszHistoryFile = RTStrDup(szPath);
1098 AssertReturn(pDbgc->pszHistoryFile, VERR_NO_STR_MEMORY);
1099
1100 /** @cfgm{/DBGC/GlobalInitFile, string, ${HOME}/.vboxdbgc-init}
1101 * The global init script of the VBox debugger. */
1102 szHomeDefault[cchHome] = '\0';
1103 rc = RTPathAppend(szHomeDefault, sizeof(szHomeDefault), ".vboxdbgc-init");
1104 AssertLogRelRCReturn(rc, rc);
1105
1106 rc = CFGMR3QueryStringDef(pNode, "GlobalInitScript", szPath, sizeof(szPath), szHomeDefault);
1107 AssertLogRelRCReturn(rc, rc);
1108
1109 pDbgc->pszGlobalInitScript = RTStrDup(szPath);
1110 AssertReturn(pDbgc->pszGlobalInitScript, VERR_NO_STR_MEMORY);
1111
1112 /** @cfgm{/DBGC/LocalInitFile, string, none}
1113 * The VM local init script of the VBox debugger. */
1114 rc = CFGMR3QueryString(pNode, "LocalInitScript", szPath, sizeof(szPath));
1115 if (RT_SUCCESS(rc))
1116 {
1117 pDbgc->pszLocalInitScript = RTStrDup(szPath);
1118 AssertReturn(pDbgc->pszLocalInitScript, VERR_NO_STR_MEMORY);
1119 }
1120 else
1121 {
1122 AssertLogRelReturn(rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT, rc);
1123 pDbgc->pszLocalInitScript = NULL;
1124 }
1125
1126 return VINF_SUCCESS;
1127}
1128
1129
1130/**
1131 * @copydoc DBGC::pfnOutput
1132 */
1133static DECLCALLBACK(int) dbgcOutputNative(void *pvUser, const char *pachChars, size_t cbChars)
1134{
1135 PDBGC pDbgc = (PDBGC)pvUser;
1136 return pDbgc->pIo->pfnWrite(pDbgc->pIo, pachChars, cbChars, NULL /*pcbWritten*/);
1137}
1138
1139
1140/**
1141 * Creates a a new instance.
1142 *
1143 * @returns VBox status code.
1144 * @param ppDbgc Where to store the pointer to the instance data.
1145 * @param pIo Pointer to the I/O callback table.
1146 * @param fFlags The flags.
1147 */
1148int dbgcCreate(PDBGC *ppDbgc, PCDBGCIO pIo, unsigned fFlags)
1149{
1150 /*
1151 * Validate input.
1152 */
1153 AssertPtrReturn(pIo, VERR_INVALID_POINTER);
1154 AssertMsgReturn(!fFlags, ("%#x", fFlags), VERR_INVALID_PARAMETER);
1155
1156 /*
1157 * Allocate and initialize.
1158 */
1159 PDBGC pDbgc = (PDBGC)RTMemAllocZ(sizeof(*pDbgc));
1160 if (!pDbgc)
1161 return VERR_NO_MEMORY;
1162
1163 dbgcInitCmdHlp(pDbgc);
1164 pDbgc->pIo = pIo;
1165 pDbgc->pfnOutput = dbgcOutputNative;
1166 pDbgc->pvOutputUser = pDbgc;
1167 pDbgc->pVM = NULL;
1168 pDbgc->pUVM = NULL;
1169 pDbgc->idCpu = 0;
1170 pDbgc->hDbgAs = DBGF_AS_GLOBAL;
1171 pDbgc->pszEmulation = "CodeView/WinDbg";
1172 pDbgc->paEmulationCmds = &g_aCmdsCodeView[0];
1173 pDbgc->cEmulationCmds = g_cCmdsCodeView;
1174 pDbgc->paEmulationFuncs = &g_aFuncsCodeView[0];
1175 pDbgc->cEmulationFuncs = g_cFuncsCodeView;
1176 //pDbgc->fLog = false;
1177 pDbgc->fRegTerse = true;
1178 pDbgc->fStepTraceRegs = true;
1179 //pDbgc->cPagingHierarchyDumps = 0;
1180 //pDbgc->DisasmPos = {0};
1181 //pDbgc->SourcePos = {0};
1182 //pDbgc->DumpPos = {0};
1183 pDbgc->pLastPos = &pDbgc->DisasmPos;
1184 //pDbgc->cbDumpElement = 0;
1185 //pDbgc->cVars = 0;
1186 //pDbgc->paVars = NULL;
1187 //pDbgc->pPlugInHead = NULL;
1188 //pDbgc->pFirstBp = NULL;
1189 RTListInit(&pDbgc->LstTraceFlowMods);
1190 //pDbgc->abSearch = {0};
1191 //pDbgc->cbSearch = 0;
1192 pDbgc->cbSearchUnit = 1;
1193 pDbgc->cMaxSearchHits = 1;
1194 //pDbgc->SearchAddr = {0};
1195 //pDbgc->cbSearchRange = 0;
1196
1197 //pDbgc->uInputZero = 0;
1198 //pDbgc->iRead = 0;
1199 //pDbgc->iWrite = 0;
1200 //pDbgc->cInputLines = 0;
1201 //pDbgc->fInputOverflow = false;
1202 pDbgc->fReady = true;
1203 pDbgc->pszScratch = &pDbgc->achScratch[0];
1204 //pDbgc->iArg = 0;
1205 //pDbgc->rcOutput = 0;
1206 //pDbgc->rcCmd = 0;
1207
1208 //pDbgc->pszHistoryFile = NULL;
1209 //pDbgc->pszGlobalInitScript = NULL;
1210 //pDbgc->pszLocalInitScript = NULL;
1211
1212 dbgcEvalInit();
1213
1214 *ppDbgc = pDbgc;
1215 return VINF_SUCCESS;
1216}
1217
1218/**
1219 * Destroys a DBGC instance created by dbgcCreate.
1220 *
1221 * @param pDbgc Pointer to the debugger console instance data.
1222 */
1223void dbgcDestroy(PDBGC pDbgc)
1224{
1225 AssertPtr(pDbgc);
1226
1227 /* Disable log hook. */
1228 if (pDbgc->fLog)
1229 {
1230
1231 }
1232
1233 /* Detach from the VM. */
1234 if (pDbgc->pUVM)
1235 DBGFR3Detach(pDbgc->pUVM);
1236
1237 /* Free config strings. */
1238 RTStrFree(pDbgc->pszGlobalInitScript);
1239 pDbgc->pszGlobalInitScript = NULL;
1240 RTStrFree(pDbgc->pszLocalInitScript);
1241 pDbgc->pszLocalInitScript = NULL;
1242 RTStrFree(pDbgc->pszHistoryFile);
1243 pDbgc->pszHistoryFile = NULL;
1244
1245 /* Finally, free the instance memory. */
1246 RTMemFree(pDbgc);
1247}
1248
1249
1250/**
1251 * Make a console instance.
1252 *
1253 * This will not return until either an 'exit' command is issued or a error code
1254 * indicating connection loss is encountered.
1255 *
1256 * @returns VINF_SUCCESS if console termination caused by the 'exit' command.
1257 * @returns The VBox status code causing the console termination.
1258 *
1259 * @param pUVM The user mode VM handle.
1260 * @param pIo Pointer to the I/O callback structure. This must contain
1261 * a full set of function pointers to service the console.
1262 * @param fFlags Reserved, must be zero.
1263 * @remarks A forced termination of the console is easiest done by forcing the
1264 * callbacks to return fatal failures.
1265 */
1266DBGDECL(int) DBGCCreate(PUVM pUVM, PCDBGCIO pIo, unsigned fFlags)
1267{
1268 /*
1269 * Validate input.
1270 */
1271 AssertPtrNullReturn(pUVM, VERR_INVALID_VM_HANDLE);
1272 PVM pVM = NULL;
1273 if (pUVM)
1274 {
1275 pVM = VMR3GetVM(pUVM);
1276 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
1277 }
1278
1279 /*
1280 * Allocate and initialize instance data
1281 */
1282 PDBGC pDbgc;
1283 int rc = dbgcCreate(&pDbgc, pIo, fFlags);
1284 if (RT_FAILURE(rc))
1285 return rc;
1286 if (!HMR3IsEnabled(pUVM) && !NEMR3IsEnabled(pUVM))
1287 pDbgc->hDbgAs = DBGF_AS_RC_AND_GC_GLOBAL;
1288
1289 /*
1290 * Print welcome message.
1291 */
1292 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
1293 "Welcome to the VirtualBox Debugger!\n");
1294
1295 /*
1296 * Attach to the specified VM.
1297 */
1298 if (RT_SUCCESS(rc) && pUVM)
1299 {
1300 rc = dbgcReadConfig(pDbgc, pUVM);
1301 if (RT_SUCCESS(rc))
1302 {
1303 rc = DBGFR3Attach(pUVM);
1304 if (RT_SUCCESS(rc))
1305 {
1306 pDbgc->pVM = pVM;
1307 pDbgc->pUVM = pUVM;
1308 pDbgc->idCpu = 0;
1309 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
1310 "Current VM is %08x, CPU #%u\n" /** @todo get and print the VM name! */
1311 , pDbgc->pVM, pDbgc->idCpu);
1312 }
1313 else
1314 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to attach to VM %p\n", pDbgc->pVM);
1315 }
1316 else
1317 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "Error reading configuration\n");
1318 }
1319
1320 /*
1321 * Load plugins.
1322 */
1323 if (RT_SUCCESS(rc))
1324 {
1325 if (pVM)
1326 DBGFR3PlugInLoadAll(pDbgc->pUVM);
1327 dbgcEventInit(pDbgc);
1328 dbgcRunInitScripts(pDbgc);
1329
1330 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
1331 if (RT_SUCCESS(rc))
1332 {
1333 /*
1334 * Set debug config log callback.
1335 */
1336 RTDBGCFG hDbgCfg = DBGFR3AsGetConfig(pUVM);
1337 if ( hDbgCfg != NIL_RTDBGCFG
1338 && RTDbgCfgRetain(hDbgCfg) != UINT32_MAX)
1339 {
1340 int rc2 = RTDbgCfgSetLogCallback(hDbgCfg, dbgcDbgCfgLogCallback, pDbgc);
1341 if (RT_FAILURE(rc2))
1342 {
1343 hDbgCfg = NIL_RTDBGCFG;
1344 RTDbgCfgRelease(hDbgCfg);
1345 }
1346 }
1347 else
1348 hDbgCfg = NIL_RTDBGCFG;
1349
1350
1351 /*
1352 * Run the debugger main loop.
1353 */
1354 rc = dbgcRun(pDbgc);
1355
1356
1357 /*
1358 * Remove debug config log callback.
1359 */
1360 if (hDbgCfg != NIL_RTDBGCFG)
1361 {
1362 RTDbgCfgSetLogCallback(hDbgCfg, NULL, NULL);
1363 RTDbgCfgRelease(hDbgCfg);
1364 }
1365 }
1366 dbgcEventTerm(pDbgc);
1367 }
1368 else
1369 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\nDBGCCreate error: %Rrc\n", rc);
1370
1371
1372 /*
1373 * Cleanup console debugger session.
1374 */
1375 dbgcDestroy(pDbgc);
1376 return rc == VERR_DBGC_QUIT ? VINF_SUCCESS : rc;
1377}
1378
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