VirtualBox

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

Last change on this file since 58141 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.9 KB
Line 
1/* $Id: DBGConsole.cpp 57358 2015-08-14 15:16:38Z 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 /*
567 * else - we have incomplete line, so leave it in the buffer and
568 * wait for more input.
569 *
570 * Windows telnet client is in "character at a time" mode by
571 * default and putty sends eol as a separate packet that will be
572 * most likely read separately from the command line it
573 * terminates.
574 */
575
576 return rc;
577}
578
579
580/**
581 * Gets the event context identifier string.
582 * @returns Read only string.
583 * @param enmCtx The context.
584 */
585static const char *dbgcGetEventCtx(DBGFEVENTCTX enmCtx)
586{
587 switch (enmCtx)
588 {
589 case DBGFEVENTCTX_RAW: return "raw";
590 case DBGFEVENTCTX_REM: return "rem";
591 case DBGFEVENTCTX_HM: return "hwaccl";
592 case DBGFEVENTCTX_HYPER: return "hyper";
593 case DBGFEVENTCTX_OTHER: return "other";
594
595 case DBGFEVENTCTX_INVALID: return "!Invalid Event Ctx!";
596 default:
597 AssertMsgFailed(("enmCtx=%d\n", enmCtx));
598 return "!Unknown Event Ctx!";
599 }
600}
601
602
603/**
604 * Processes debugger events.
605 *
606 * @returns VBox status.
607 * @param pDbgc DBGC Instance data.
608 * @param pEvent Pointer to event data.
609 */
610static int dbgcProcessEvent(PDBGC pDbgc, PCDBGFEVENT pEvent)
611{
612 /*
613 * Flush log first.
614 */
615 if (pDbgc->fLog)
616 {
617 int rc = dbgcProcessLog(pDbgc);
618 if (RT_FAILURE(rc))
619 return rc;
620 }
621
622 /*
623 * Process the event.
624 */
625 pDbgc->pszScratch = &pDbgc->achInput[0];
626 pDbgc->iArg = 0;
627 bool fPrintPrompt = true;
628 int rc = VINF_SUCCESS;
629 switch (pEvent->enmType)
630 {
631 /*
632 * The first part is events we have initiated with commands.
633 */
634 case DBGFEVENT_HALT_DONE:
635 {
636 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: VM %p is halted! (%s)\n",
637 pDbgc->pVM, dbgcGetEventCtx(pEvent->enmCtx));
638 pDbgc->fRegCtxGuest = true; /* we're always in guest context when halted. */
639 if (RT_SUCCESS(rc))
640 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
641 break;
642 }
643
644
645 /*
646 * The second part is events which can occur at any time.
647 */
648 case DBGFEVENT_FATAL_ERROR:
649 {
650 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbf event: Fatal error! (%s)\n",
651 dbgcGetEventCtx(pEvent->enmCtx));
652 pDbgc->fRegCtxGuest = false; /* fatal errors are always in hypervisor. */
653 if (RT_SUCCESS(rc))
654 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
655 break;
656 }
657
658 case DBGFEVENT_BREAKPOINT:
659 case DBGFEVENT_BREAKPOINT_HYPER:
660 {
661 bool fRegCtxGuest = pDbgc->fRegCtxGuest;
662 pDbgc->fRegCtxGuest = pEvent->enmType == DBGFEVENT_BREAKPOINT;
663
664 rc = dbgcBpExec(pDbgc, pEvent->u.Bp.iBp);
665 switch (rc)
666 {
667 case VERR_DBGC_BP_NOT_FOUND:
668 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Unknown breakpoint %u! (%s)\n",
669 pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
670 break;
671
672 case VINF_DBGC_BP_NO_COMMAND:
673 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! (%s)\n",
674 pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
675 break;
676
677 case VINF_BUFFER_OVERFLOW:
678 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! Command too long to execute! (%s)\n",
679 pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
680 break;
681
682 default:
683 break;
684 }
685 if (RT_SUCCESS(rc) && DBGFR3IsHalted(pDbgc->pUVM))
686 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
687 else
688 pDbgc->fRegCtxGuest = fRegCtxGuest;
689 break;
690 }
691
692 case DBGFEVENT_STEPPED:
693 case DBGFEVENT_STEPPED_HYPER:
694 {
695 pDbgc->fRegCtxGuest = pEvent->enmType == DBGFEVENT_STEPPED;
696
697 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Single step! (%s)\n", dbgcGetEventCtx(pEvent->enmCtx));
698 if (RT_SUCCESS(rc))
699 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
700 break;
701 }
702
703 case DBGFEVENT_ASSERTION_HYPER:
704 {
705 pDbgc->fRegCtxGuest = false;
706
707 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
708 "\ndbgf event: Hypervisor Assertion! (%s)\n"
709 "%s"
710 "%s"
711 "\n",
712 dbgcGetEventCtx(pEvent->enmCtx),
713 pEvent->u.Assert.pszMsg1,
714 pEvent->u.Assert.pszMsg2);
715 if (RT_SUCCESS(rc))
716 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
717 break;
718 }
719
720 case DBGFEVENT_DEV_STOP:
721 {
722 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
723 "\n"
724 "dbgf event: DBGFSTOP (%s)\n"
725 "File: %s\n"
726 "Line: %d\n"
727 "Function: %s\n",
728 dbgcGetEventCtx(pEvent->enmCtx),
729 pEvent->u.Src.pszFile,
730 pEvent->u.Src.uLine,
731 pEvent->u.Src.pszFunction);
732 if (RT_SUCCESS(rc) && pEvent->u.Src.pszMessage && *pEvent->u.Src.pszMessage)
733 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
734 "Message: %s\n",
735 pEvent->u.Src.pszMessage);
736 if (RT_SUCCESS(rc))
737 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
738 break;
739 }
740
741
742 case DBGFEVENT_INVALID_COMMAND:
743 {
744 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Invalid command event!\n");
745 break;
746 }
747
748 case DBGFEVENT_POWERING_OFF:
749 {
750 pDbgc->fReady = false;
751 pDbgc->pBack->pfnSetReady(pDbgc->pBack, false);
752 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\nVM is powering off!\n");
753 fPrintPrompt = false;
754 rc = VERR_GENERAL_FAILURE;
755 break;
756 }
757
758
759 default:
760 {
761 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Unknown event %d!\n", pEvent->enmType);
762 break;
763 }
764 }
765
766 /*
767 * Prompt, anyone?
768 */
769 if (fPrintPrompt && RT_SUCCESS(rc))
770 {
771 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
772 pDbgc->fReady = true;
773 if (RT_SUCCESS(rc))
774 pDbgc->pBack->pfnSetReady(pDbgc->pBack, true);
775 }
776
777 return rc;
778}
779
780
781/**
782 * Prints any log lines from the log buffer.
783 *
784 * The caller must not call function this unless pDbgc->fLog is set.
785 *
786 * @returns VBox status. (output related)
787 * @param pDbgc Debugger console instance data.
788 */
789static int dbgcProcessLog(PDBGC pDbgc)
790{
791 /** @todo */
792 NOREF(pDbgc);
793 return 0;
794}
795
796/** @callback_method_impl{FNRTDBGCFGLOG} */
797static DECLCALLBACK(void) dbgcDbgCfgLogCallback(RTDBGCFG hDbgCfg, uint32_t iLevel, const char *pszMsg, void *pvUser)
798{
799 /** @todo Add symbol noise setting. */
800 NOREF(hDbgCfg); NOREF(iLevel);
801 PDBGC pDbgc = (PDBGC)pvUser;
802 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s", pszMsg);
803}
804
805
806/**
807 * Run the debugger console.
808 *
809 * @returns VBox status.
810 * @param pDbgc Pointer to the debugger console instance data.
811 */
812int dbgcRun(PDBGC pDbgc)
813{
814 /*
815 * We're ready for commands now.
816 */
817 pDbgc->fReady = true;
818 pDbgc->pBack->pfnSetReady(pDbgc->pBack, true);
819
820 /*
821 * Main Debugger Loop.
822 *
823 * This loop will either block on waiting for input or on waiting on
824 * debug events. If we're forwarding the log we cannot wait for long
825 * before we must flush the log.
826 */
827 int rc;
828 for (;;)
829 {
830 rc = VERR_SEM_OUT_OF_TURN;
831 if (pDbgc->pUVM)
832 rc = DBGFR3QueryWaitable(pDbgc->pUVM);
833
834 if (RT_SUCCESS(rc))
835 {
836 /*
837 * Wait for a debug event.
838 */
839 PCDBGFEVENT pEvent;
840 rc = DBGFR3EventWait(pDbgc->pUVM, pDbgc->fLog ? 1 : 32, &pEvent);
841 if (RT_SUCCESS(rc))
842 {
843 rc = dbgcProcessEvent(pDbgc, pEvent);
844 if (RT_FAILURE(rc))
845 break;
846 }
847 else if (rc != VERR_TIMEOUT)
848 break;
849
850 /*
851 * Check for input.
852 */
853 if (pDbgc->pBack->pfnInput(pDbgc->pBack, 0))
854 {
855 rc = dbgcProcessInput(pDbgc, false /* fNoExecute */);
856 if (RT_FAILURE(rc))
857 break;
858 }
859 }
860 else if (rc == VERR_SEM_OUT_OF_TURN)
861 {
862 /*
863 * Wait for input. If Logging is enabled we'll only wait very briefly.
864 */
865 if (pDbgc->pBack->pfnInput(pDbgc->pBack, pDbgc->fLog ? 1 : 1000))
866 {
867 rc = dbgcProcessInput(pDbgc, false /* fNoExecute */);
868 if (RT_FAILURE(rc))
869 break;
870 }
871 }
872 else
873 break;
874
875 /*
876 * Forward log output.
877 */
878 if (pDbgc->fLog)
879 {
880 rc = dbgcProcessLog(pDbgc);
881 if (RT_FAILURE(rc))
882 break;
883 }
884 }
885
886 return rc;
887}
888
889
890/**
891 * Creates a a new instance.
892 *
893 * @returns VBox status code.
894 * @param ppDbgc Where to store the pointer to the instance data.
895 * @param pBack Pointer to the backend.
896 * @param fFlags The flags.
897 */
898int dbgcCreate(PDBGC *ppDbgc, PDBGCBACK pBack, unsigned fFlags)
899{
900 /*
901 * Validate input.
902 */
903 AssertPtrReturn(pBack, VERR_INVALID_POINTER);
904 AssertMsgReturn(!fFlags, ("%#x", fFlags), VERR_INVALID_PARAMETER);
905
906 /*
907 * Allocate and initialize.
908 */
909 PDBGC pDbgc = (PDBGC)RTMemAllocZ(sizeof(*pDbgc));
910 if (!pDbgc)
911 return VERR_NO_MEMORY;
912
913 dbgcInitCmdHlp(pDbgc);
914 pDbgc->pBack = pBack;
915 pDbgc->pVM = NULL;
916 pDbgc->pUVM = NULL;
917 pDbgc->idCpu = 0;
918 pDbgc->hDbgAs = DBGF_AS_GLOBAL;
919 pDbgc->pszEmulation = "CodeView/WinDbg";
920 pDbgc->paEmulationCmds = &g_aCmdsCodeView[0];
921 pDbgc->cEmulationCmds = g_cCmdsCodeView;
922 pDbgc->paEmulationFuncs = &g_aFuncsCodeView[0];
923 pDbgc->cEmulationFuncs = g_cFuncsCodeView;
924 //pDbgc->fLog = false;
925 pDbgc->fRegCtxGuest = true;
926 pDbgc->fRegTerse = true;
927 //pDbgc->cPagingHierarchyDumps = 0;
928 //pDbgc->DisasmPos = {0};
929 //pDbgc->SourcePos = {0};
930 //pDbgc->DumpPos = {0};
931 pDbgc->pLastPos = &pDbgc->DisasmPos;
932 //pDbgc->cbDumpElement = 0;
933 //pDbgc->cVars = 0;
934 //pDbgc->paVars = NULL;
935 //pDbgc->pPlugInHead = NULL;
936 //pDbgc->pFirstBp = NULL;
937 //pDbgc->abSearch = {0};
938 //pDbgc->cbSearch = 0;
939 pDbgc->cbSearchUnit = 1;
940 pDbgc->cMaxSearchHits = 1;
941 //pDbgc->SearchAddr = {0};
942 //pDbgc->cbSearchRange = 0;
943
944 //pDbgc->uInputZero = 0;
945 //pDbgc->iRead = 0;
946 //pDbgc->iWrite = 0;
947 //pDbgc->cInputLines = 0;
948 //pDbgc->fInputOverflow = false;
949 pDbgc->fReady = true;
950 pDbgc->pszScratch = &pDbgc->achScratch[0];
951 //pDbgc->iArg = 0;
952 //pDbgc->rcOutput = 0;
953 //pDbgc->rcCmd = 0;
954
955 dbgcEvalInit();
956
957 *ppDbgc = pDbgc;
958 return VINF_SUCCESS;
959}
960
961/**
962 * Destroys a DBGC instance created by dbgcCreate.
963 *
964 * @param pDbgc Pointer to the debugger console instance data.
965 */
966void dbgcDestroy(PDBGC pDbgc)
967{
968 AssertPtr(pDbgc);
969
970 /* Disable log hook. */
971 if (pDbgc->fLog)
972 {
973
974 }
975
976 /* Detach from the VM. */
977 if (pDbgc->pUVM)
978 DBGFR3Detach(pDbgc->pUVM);
979
980 /* finally, free the instance memory. */
981 RTMemFree(pDbgc);
982}
983
984
985/**
986 * Make a console instance.
987 *
988 * This will not return until either an 'exit' command is issued or a error code
989 * indicating connection loss is encountered.
990 *
991 * @returns VINF_SUCCESS if console termination caused by the 'exit' command.
992 * @returns The VBox status code causing the console termination.
993 *
994 * @param pUVM The user mode VM handle.
995 * @param pBack Pointer to the backend structure. This must contain
996 * a full set of function pointers to service the console.
997 * @param fFlags Reserved, must be zero.
998 * @remark A forced termination of the console is easiest done by forcing the
999 * callbacks to return fatal failures.
1000 */
1001DBGDECL(int) DBGCCreate(PUVM pUVM, PDBGCBACK pBack, unsigned fFlags)
1002{
1003 /*
1004 * Validate input.
1005 */
1006 AssertPtrNullReturn(pUVM, VERR_INVALID_VM_HANDLE);
1007 PVM pVM = NULL;
1008 if (pUVM)
1009 {
1010 pVM = VMR3GetVM(pUVM);
1011 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
1012 }
1013
1014 /*
1015 * Allocate and initialize instance data
1016 */
1017 PDBGC pDbgc;
1018 int rc = dbgcCreate(&pDbgc, pBack, fFlags);
1019 if (RT_FAILURE(rc))
1020 return rc;
1021 if (!HMR3IsEnabled(pUVM))
1022 pDbgc->hDbgAs = DBGF_AS_RC_AND_GC_GLOBAL;
1023
1024 /*
1025 * Print welcome message.
1026 */
1027 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
1028 "Welcome to the VirtualBox Debugger!\n");
1029
1030 /*
1031 * Attach to the specified VM.
1032 */
1033 if (RT_SUCCESS(rc) && pUVM)
1034 {
1035 rc = DBGFR3Attach(pUVM);
1036 if (RT_SUCCESS(rc))
1037 {
1038 pDbgc->pVM = pVM;
1039 pDbgc->pUVM = pUVM;
1040 pDbgc->idCpu = 0;
1041 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
1042 "Current VM is %08x, CPU #%u\n" /** @todo get and print the VM name! */
1043 , pDbgc->pVM, pDbgc->idCpu);
1044 }
1045 else
1046 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to attach to VM %p\n", pDbgc->pVM);
1047 }
1048
1049 /*
1050 * Load plugins.
1051 */
1052 if (RT_SUCCESS(rc))
1053 {
1054 if (pVM)
1055 DBGFR3PlugInLoadAll(pDbgc->pUVM);
1056 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
1057 if (RT_SUCCESS(rc))
1058 {
1059 /*
1060 * Set debug config log callback.
1061 */
1062 RTDBGCFG hDbgCfg = DBGFR3AsGetConfig(pUVM);
1063 if ( hDbgCfg != NIL_RTDBGCFG
1064 && RTDbgCfgRetain(hDbgCfg) != UINT32_MAX)
1065 {
1066 int rc2 = RTDbgCfgSetLogCallback(hDbgCfg, dbgcDbgCfgLogCallback, pDbgc);
1067 if (RT_FAILURE(rc2))
1068 {
1069 hDbgCfg = NIL_RTDBGCFG;
1070 RTDbgCfgRelease(hDbgCfg);
1071 }
1072 }
1073 else
1074 hDbgCfg = NIL_RTDBGCFG;
1075
1076
1077 /*
1078 * Run the debugger main loop.
1079 */
1080 rc = dbgcRun(pDbgc);
1081
1082
1083 /*
1084 * Remove debug config log callback.
1085 */
1086 if (hDbgCfg != NIL_RTDBGCFG)
1087 {
1088 RTDbgCfgSetLogCallback(hDbgCfg, NULL, NULL);
1089 RTDbgCfgRelease(hDbgCfg);
1090 }
1091 }
1092 }
1093 else
1094 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\nDBGCCreate error: %Rrc\n", rc);
1095
1096
1097 /*
1098 * Cleanup console debugger session.
1099 */
1100 dbgcDestroy(pDbgc);
1101 return rc == VERR_DBGC_QUIT ? VINF_SUCCESS : rc;
1102}
1103
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