VirtualBox

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

Last change on this file since 56328 was 56296, checked in by vboxsync, 9 years ago

Debugger: Updated (C) year.

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