1 | /* $Id: DBGConsole.cpp 73348 2018-07-25 09:25:45Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGC - Debugger Console.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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/cfgm.h>
|
---|
164 | #include <VBox/vmm/dbgf.h>
|
---|
165 | #include <VBox/vmm/vmapi.h> /* VMR3GetVM() */
|
---|
166 | #include <VBox/vmm/hm.h> /* HMR3IsEnabled */
|
---|
167 | #include <VBox/vmm/nem.h> /* NEMR3IsEnabled */
|
---|
168 | #include <VBox/err.h>
|
---|
169 | #include <VBox/log.h>
|
---|
170 |
|
---|
171 | #include <iprt/asm.h>
|
---|
172 | #include <iprt/assert.h>
|
---|
173 | #include <iprt/file.h>
|
---|
174 | #include <iprt/mem.h>
|
---|
175 | #include <iprt/path.h>
|
---|
176 | #include <iprt/string.h>
|
---|
177 |
|
---|
178 | #include "DBGCInternal.h"
|
---|
179 | #include "DBGPlugIns.h"
|
---|
180 |
|
---|
181 |
|
---|
182 | /*********************************************************************************************************************************
|
---|
183 | * Internal Functions *
|
---|
184 | *********************************************************************************************************************************/
|
---|
185 | static int dbgcProcessLog(PDBGC pDbgc);
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Resolves a symbol (or tries to do so at least).
|
---|
190 | *
|
---|
191 | * @returns 0 on success.
|
---|
192 | * @returns VBox status on failure.
|
---|
193 | * @param pDbgc The debug console instance.
|
---|
194 | * @param pszSymbol The symbol name.
|
---|
195 | * @param enmType The result type. Specifying DBGCVAR_TYPE_GC_FAR may
|
---|
196 | * cause failure, avoid it.
|
---|
197 | * @param pResult Where to store the result.
|
---|
198 | */
|
---|
199 | int dbgcSymbolGet(PDBGC pDbgc, const char *pszSymbol, DBGCVARTYPE enmType, PDBGCVAR pResult)
|
---|
200 | {
|
---|
201 | int rc;
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * Builtin?
|
---|
205 | */
|
---|
206 | PCDBGCSYM pSymDesc = dbgcLookupRegisterSymbol(pDbgc, pszSymbol);
|
---|
207 | if (pSymDesc)
|
---|
208 | {
|
---|
209 | if (!pSymDesc->pfnGet)
|
---|
210 | return VERR_DBGC_PARSE_WRITEONLY_SYMBOL;
|
---|
211 | return pSymDesc->pfnGet(pSymDesc, &pDbgc->CmdHlp, enmType, pResult);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * A typical register? (Guest only)
|
---|
216 | */
|
---|
217 | static const char s_szSixLetterRegisters[] =
|
---|
218 | "rflags;eflags;"
|
---|
219 | ;
|
---|
220 | static const char s_szThreeLetterRegisters[] =
|
---|
221 | "eax;rax;" "r10;" "r8d;r8w;r8b;" "cr0;" "dr0;"
|
---|
222 | "ebx;rbx;" "r11;" "r9d;r9w;r8b;" "dr1;"
|
---|
223 | "ecx;rcx;" "r12;" "cr2;" "dr2;"
|
---|
224 | "edx;rdx;" "r13;" "cr3;" "dr3;"
|
---|
225 | "edi;rdi;dil;" "r14;" "cr4;" "dr4;"
|
---|
226 | "esi;rsi;sil;" "r15;" "cr8;"
|
---|
227 | "ebp;rbp;"
|
---|
228 | "esp;rsp;" "dr6;"
|
---|
229 | "rip;eip;" "dr7;"
|
---|
230 | "efl;"
|
---|
231 | ;
|
---|
232 | static const char s_szTwoLetterRegisters[] =
|
---|
233 | "ax;al;ah;" "r8;"
|
---|
234 | "bx;bl;bh;" "r9;"
|
---|
235 | "cx;cl;ch;" "cs;"
|
---|
236 | "dx;dl;dh;" "ds;"
|
---|
237 | "di;" "es;"
|
---|
238 | "si;" "fs;"
|
---|
239 | "bp;" "gs;"
|
---|
240 | "sp;" "ss;"
|
---|
241 | "ip;"
|
---|
242 | ;
|
---|
243 | const char *pszRegSym = *pszSymbol == '.' ? pszSymbol + 1 : pszSymbol;
|
---|
244 | size_t const cchRegSym = strlen(pszRegSym);
|
---|
245 | if ( (cchRegSym == 2 && strstr(s_szTwoLetterRegisters, pszRegSym))
|
---|
246 | || (cchRegSym == 3 && strstr(s_szThreeLetterRegisters, pszRegSym))
|
---|
247 | || (cchRegSym == 6 && strstr(s_szSixLetterRegisters, pszRegSym)))
|
---|
248 | {
|
---|
249 | if (!strchr(pszSymbol, ';'))
|
---|
250 | {
|
---|
251 | DBGCVAR Var;
|
---|
252 | DBGCVAR_INIT_SYMBOL(&Var, pszSymbol);
|
---|
253 | rc = dbgcOpRegister(pDbgc, &Var, DBGCVAR_CAT_ANY, pResult);
|
---|
254 | if (RT_SUCCESS(rc))
|
---|
255 | return DBGCCmdHlpConvert(&pDbgc->CmdHlp, pResult, enmType, false /*fConvSyms*/, pResult);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Ask PDM.
|
---|
261 | */
|
---|
262 | /** @todo resolve symbols using PDM. */
|
---|
263 |
|
---|
264 | /*
|
---|
265 | * Ask the debug info manager.
|
---|
266 | */
|
---|
267 | RTDBGSYMBOL Symbol;
|
---|
268 | rc = DBGFR3AsSymbolByName(pDbgc->pUVM, pDbgc->hDbgAs, pszSymbol, &Symbol, NULL);
|
---|
269 | if (RT_SUCCESS(rc))
|
---|
270 | {
|
---|
271 | /*
|
---|
272 | * Default return is a flat gc address.
|
---|
273 | */
|
---|
274 | DBGCVAR_INIT_GC_FLAT(pResult, Symbol.Value);
|
---|
275 | if (Symbol.cb)
|
---|
276 | DBGCVAR_SET_RANGE(pResult, DBGCVAR_RANGE_BYTES, Symbol.cb);
|
---|
277 |
|
---|
278 | switch (enmType)
|
---|
279 | {
|
---|
280 | /* nothing to do. */
|
---|
281 | case DBGCVAR_TYPE_GC_FLAT:
|
---|
282 | case DBGCVAR_TYPE_ANY:
|
---|
283 | return VINF_SUCCESS;
|
---|
284 |
|
---|
285 | /* impossible at the moment. */
|
---|
286 | case DBGCVAR_TYPE_GC_FAR:
|
---|
287 | return VERR_DBGC_PARSE_CONVERSION_FAILED;
|
---|
288 |
|
---|
289 | /* simply make it numeric. */
|
---|
290 | case DBGCVAR_TYPE_NUMBER:
|
---|
291 | pResult->enmType = DBGCVAR_TYPE_NUMBER;
|
---|
292 | pResult->u.u64Number = Symbol.Value;
|
---|
293 | return VINF_SUCCESS;
|
---|
294 |
|
---|
295 | /* cast it. */
|
---|
296 | case DBGCVAR_TYPE_GC_PHYS:
|
---|
297 | case DBGCVAR_TYPE_HC_FLAT:
|
---|
298 | case DBGCVAR_TYPE_HC_PHYS:
|
---|
299 | return DBGCCmdHlpConvert(&pDbgc->CmdHlp, pResult, enmType, false /*fConvSyms*/, pResult);
|
---|
300 |
|
---|
301 | default:
|
---|
302 | AssertMsgFailed(("Internal error enmType=%d\n", enmType));
|
---|
303 | return VERR_INVALID_PARAMETER;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | return VERR_DBGC_PARSE_NOT_IMPLEMENTED;
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Process all commands currently in the buffer.
|
---|
313 | *
|
---|
314 | * @returns VBox status code. Any error indicates the termination of the console session.
|
---|
315 | * @param pDbgc Debugger console instance data.
|
---|
316 | * @param fNoExecute Indicates that no commands should actually be executed.
|
---|
317 | */
|
---|
318 | static int dbgcProcessCommands(PDBGC pDbgc, bool fNoExecute)
|
---|
319 | {
|
---|
320 | /** @todo Replace this with a sh/ksh/csh/rexx like toplevel language that
|
---|
321 | * allows doing function, loops, if, cases, and such. */
|
---|
322 | int rc = VINF_SUCCESS;
|
---|
323 | while (pDbgc->cInputLines)
|
---|
324 | {
|
---|
325 | /*
|
---|
326 | * Empty the log buffer if we're hooking the log.
|
---|
327 | */
|
---|
328 | if (pDbgc->fLog)
|
---|
329 | {
|
---|
330 | rc = dbgcProcessLog(pDbgc);
|
---|
331 | if (RT_FAILURE(rc))
|
---|
332 | break;
|
---|
333 | }
|
---|
334 |
|
---|
335 | if (pDbgc->iRead == pDbgc->iWrite)
|
---|
336 | {
|
---|
337 | AssertMsgFailed(("The input buffer is empty while cInputLines=%d!\n", pDbgc->cInputLines));
|
---|
338 | pDbgc->cInputLines = 0;
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * Copy the command to the parse buffer.
|
---|
344 | */
|
---|
345 | char ch;
|
---|
346 | char *psz = &pDbgc->achInput[pDbgc->iRead];
|
---|
347 | char *pszTrg = &pDbgc->achScratch[0];
|
---|
348 | while ((*pszTrg = ch = *psz++) != ';' && ch != '\n' )
|
---|
349 | {
|
---|
350 | if (psz == &pDbgc->achInput[sizeof(pDbgc->achInput)])
|
---|
351 | psz = &pDbgc->achInput[0];
|
---|
352 |
|
---|
353 | if (psz == &pDbgc->achInput[pDbgc->iWrite])
|
---|
354 | {
|
---|
355 | AssertMsgFailed(("The buffer contains no commands while cInputLines=%d!\n", pDbgc->cInputLines));
|
---|
356 | pDbgc->cInputLines = 0;
|
---|
357 | return 0;
|
---|
358 | }
|
---|
359 |
|
---|
360 | pszTrg++;
|
---|
361 | }
|
---|
362 | *pszTrg = '\0';
|
---|
363 |
|
---|
364 | /*
|
---|
365 | * Advance the buffer.
|
---|
366 | */
|
---|
367 | pDbgc->iRead = psz - &pDbgc->achInput[0];
|
---|
368 | if (ch == '\n')
|
---|
369 | pDbgc->cInputLines--;
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Parse and execute this command.
|
---|
373 | */
|
---|
374 | pDbgc->pszScratch = pszTrg + 1;
|
---|
375 | pDbgc->iArg = 0;
|
---|
376 | rc = dbgcEvalCommand(pDbgc, &pDbgc->achScratch[0], pszTrg - &pDbgc->achScratch[0] - 1, fNoExecute);
|
---|
377 | if ( rc == VERR_DBGC_QUIT
|
---|
378 | || rc == VWRN_DBGC_CMD_PENDING)
|
---|
379 | break;
|
---|
380 | rc = VINF_SUCCESS; /* ignore other statuses */
|
---|
381 | }
|
---|
382 |
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Handle input buffer overflow.
|
---|
389 | *
|
---|
390 | * Will read any available input looking for a '\n' to reset the buffer on.
|
---|
391 | *
|
---|
392 | * @returns VBox status code.
|
---|
393 | * @param pDbgc Debugger console instance data.
|
---|
394 | */
|
---|
395 | static int dbgcInputOverflow(PDBGC pDbgc)
|
---|
396 | {
|
---|
397 | /*
|
---|
398 | * Assert overflow status and reset the input buffer.
|
---|
399 | */
|
---|
400 | if (!pDbgc->fInputOverflow)
|
---|
401 | {
|
---|
402 | pDbgc->fInputOverflow = true;
|
---|
403 | pDbgc->iRead = pDbgc->iWrite = 0;
|
---|
404 | pDbgc->cInputLines = 0;
|
---|
405 | pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "Input overflow!!\n");
|
---|
406 | }
|
---|
407 |
|
---|
408 | /*
|
---|
409 | * Eat input till no more or there is a '\n'.
|
---|
410 | * When finding a '\n' we'll continue normal processing.
|
---|
411 | */
|
---|
412 | while (pDbgc->pBack->pfnInput(pDbgc->pBack, 0))
|
---|
413 | {
|
---|
414 | size_t cbRead;
|
---|
415 | int rc = pDbgc->pBack->pfnRead(pDbgc->pBack, &pDbgc->achInput[0], sizeof(pDbgc->achInput) - 1, &cbRead);
|
---|
416 | if (RT_FAILURE(rc))
|
---|
417 | return rc;
|
---|
418 | char *psz = (char *)memchr(&pDbgc->achInput[0], '\n', cbRead);
|
---|
419 | if (psz)
|
---|
420 | {
|
---|
421 | pDbgc->fInputOverflow = false;
|
---|
422 | pDbgc->iRead = psz - &pDbgc->achInput[0] + 1;
|
---|
423 | pDbgc->iWrite = (unsigned)cbRead;
|
---|
424 | pDbgc->cInputLines = 0;
|
---|
425 | break;
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Read input and do some preprocessing.
|
---|
435 | *
|
---|
436 | * @returns VBox status code.
|
---|
437 | * In addition to the iWrite and achInput, cInputLines is maintained.
|
---|
438 | * In case of an input overflow the fInputOverflow flag will be set.
|
---|
439 | * @param pDbgc Debugger console instance data.
|
---|
440 | */
|
---|
441 | static int dbgcInputRead(PDBGC pDbgc)
|
---|
442 | {
|
---|
443 | /*
|
---|
444 | * We have ready input.
|
---|
445 | * Read it till we don't have any or we have a full input buffer.
|
---|
446 | */
|
---|
447 | int rc = 0;
|
---|
448 | do
|
---|
449 | {
|
---|
450 | /*
|
---|
451 | * More available buffer space?
|
---|
452 | */
|
---|
453 | size_t cbLeft;
|
---|
454 | if (pDbgc->iWrite > pDbgc->iRead)
|
---|
455 | cbLeft = sizeof(pDbgc->achInput) - pDbgc->iWrite - (pDbgc->iRead == 0);
|
---|
456 | else
|
---|
457 | cbLeft = pDbgc->iRead - pDbgc->iWrite - 1;
|
---|
458 | if (!cbLeft)
|
---|
459 | {
|
---|
460 | /* overflow? */
|
---|
461 | if (!pDbgc->cInputLines)
|
---|
462 | rc = dbgcInputOverflow(pDbgc);
|
---|
463 | break;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /*
|
---|
467 | * Read one char and interpret it.
|
---|
468 | */
|
---|
469 | char achRead[128];
|
---|
470 | size_t cbRead;
|
---|
471 | rc = pDbgc->pBack->pfnRead(pDbgc->pBack, &achRead[0], RT_MIN(cbLeft, sizeof(achRead)), &cbRead);
|
---|
472 | if (RT_FAILURE(rc))
|
---|
473 | return rc;
|
---|
474 | char *psz = &achRead[0];
|
---|
475 | while (cbRead-- > 0)
|
---|
476 | {
|
---|
477 | char ch = *psz++;
|
---|
478 | switch (ch)
|
---|
479 | {
|
---|
480 | /*
|
---|
481 | * Ignore.
|
---|
482 | */
|
---|
483 | case '\0':
|
---|
484 | case '\r':
|
---|
485 | case '\a':
|
---|
486 | break;
|
---|
487 |
|
---|
488 | /*
|
---|
489 | * Backspace.
|
---|
490 | */
|
---|
491 | case '\b':
|
---|
492 | Log2(("DBGC: backspace\n"));
|
---|
493 | if (pDbgc->iRead != pDbgc->iWrite)
|
---|
494 | {
|
---|
495 | unsigned iWriteUndo = pDbgc->iWrite;
|
---|
496 | if (pDbgc->iWrite)
|
---|
497 | pDbgc->iWrite--;
|
---|
498 | else
|
---|
499 | pDbgc->iWrite = sizeof(pDbgc->achInput) - 1;
|
---|
500 |
|
---|
501 | if (pDbgc->achInput[pDbgc->iWrite] == '\n')
|
---|
502 | pDbgc->iWrite = iWriteUndo;
|
---|
503 | }
|
---|
504 | break;
|
---|
505 |
|
---|
506 | /*
|
---|
507 | * Add char to buffer.
|
---|
508 | */
|
---|
509 | case '\t':
|
---|
510 | case '\n':
|
---|
511 | case ';':
|
---|
512 | switch (ch)
|
---|
513 | {
|
---|
514 | case '\t': ch = ' '; break;
|
---|
515 | case '\n': pDbgc->cInputLines++; break;
|
---|
516 | }
|
---|
517 | default:
|
---|
518 | Log2(("DBGC: ch=%02x\n", (unsigned char)ch));
|
---|
519 | pDbgc->achInput[pDbgc->iWrite] = ch;
|
---|
520 | if (++pDbgc->iWrite >= sizeof(pDbgc->achInput))
|
---|
521 | pDbgc->iWrite = 0;
|
---|
522 | break;
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | /* Terminate it to make it easier to read in the debugger. */
|
---|
527 | pDbgc->achInput[pDbgc->iWrite] = '\0';
|
---|
528 | } while (pDbgc->pBack->pfnInput(pDbgc->pBack, 0));
|
---|
529 |
|
---|
530 | return rc;
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Reads input, parses it and executes commands on '\n'.
|
---|
536 | *
|
---|
537 | * @returns VBox status code.
|
---|
538 | * @param pDbgc Debugger console instance data.
|
---|
539 | * @param fNoExecute Indicates that no commands should actually be executed.
|
---|
540 | */
|
---|
541 | int dbgcProcessInput(PDBGC pDbgc, bool fNoExecute)
|
---|
542 | {
|
---|
543 | /*
|
---|
544 | * We know there's input ready, so let's read it first.
|
---|
545 | */
|
---|
546 | int rc = dbgcInputRead(pDbgc);
|
---|
547 | if (RT_FAILURE(rc))
|
---|
548 | return rc;
|
---|
549 |
|
---|
550 | /*
|
---|
551 | * Now execute any ready commands.
|
---|
552 | */
|
---|
553 | if (pDbgc->cInputLines)
|
---|
554 | {
|
---|
555 | pDbgc->pBack->pfnSetReady(pDbgc->pBack, false);
|
---|
556 | pDbgc->fReady = false;
|
---|
557 | rc = dbgcProcessCommands(pDbgc, fNoExecute);
|
---|
558 | if (RT_SUCCESS(rc) && rc != VWRN_DBGC_CMD_PENDING)
|
---|
559 | pDbgc->fReady = true;
|
---|
560 |
|
---|
561 | if ( RT_SUCCESS(rc)
|
---|
562 | && pDbgc->iRead == pDbgc->iWrite
|
---|
563 | && pDbgc->fReady)
|
---|
564 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
|
---|
565 |
|
---|
566 | if ( RT_SUCCESS(rc)
|
---|
567 | && pDbgc->fReady)
|
---|
568 | pDbgc->pBack->pfnSetReady(pDbgc->pBack, true);
|
---|
569 | }
|
---|
570 | /*
|
---|
571 | * else - we have incomplete line, so leave it in the buffer and
|
---|
572 | * wait for more input.
|
---|
573 | *
|
---|
574 | * Windows telnet client is in "character at a time" mode by
|
---|
575 | * default and putty sends eol as a separate packet that will be
|
---|
576 | * most likely read separately from the command line it
|
---|
577 | * terminates.
|
---|
578 | */
|
---|
579 |
|
---|
580 | return rc;
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | /**
|
---|
585 | * Gets the event context identifier string.
|
---|
586 | * @returns Read only string.
|
---|
587 | * @param enmCtx The context.
|
---|
588 | */
|
---|
589 | static const char *dbgcGetEventCtx(DBGFEVENTCTX enmCtx)
|
---|
590 | {
|
---|
591 | switch (enmCtx)
|
---|
592 | {
|
---|
593 | case DBGFEVENTCTX_RAW: return "raw";
|
---|
594 | case DBGFEVENTCTX_REM: return "rem";
|
---|
595 | case DBGFEVENTCTX_HM: return "hwaccl";
|
---|
596 | case DBGFEVENTCTX_HYPER: return "hyper";
|
---|
597 | case DBGFEVENTCTX_OTHER: return "other";
|
---|
598 |
|
---|
599 | case DBGFEVENTCTX_INVALID: return "!Invalid Event Ctx!";
|
---|
600 | default:
|
---|
601 | AssertMsgFailed(("enmCtx=%d\n", enmCtx));
|
---|
602 | return "!Unknown Event Ctx!";
|
---|
603 | }
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | /**
|
---|
608 | * Looks up a generic debug event.
|
---|
609 | *
|
---|
610 | * @returns Pointer to DBGCSXEVT structure if found, otherwise NULL.
|
---|
611 | * @param enmType The possibly generic event to find the descriptor for.
|
---|
612 | */
|
---|
613 | static PCDBGCSXEVT dbgcEventLookup(DBGFEVENTTYPE enmType)
|
---|
614 | {
|
---|
615 | uint32_t i = g_cDbgcSxEvents;
|
---|
616 | while (i-- > 0)
|
---|
617 | if (g_aDbgcSxEvents[i].enmType == enmType)
|
---|
618 | return &g_aDbgcSxEvents[i];
|
---|
619 | return NULL;
|
---|
620 | }
|
---|
621 |
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * Processes debugger events.
|
---|
625 | *
|
---|
626 | * @returns VBox status code.
|
---|
627 | * @param pDbgc DBGC Instance data.
|
---|
628 | * @param pEvent Pointer to event data.
|
---|
629 | */
|
---|
630 | static int dbgcProcessEvent(PDBGC pDbgc, PCDBGFEVENT pEvent)
|
---|
631 | {
|
---|
632 | /*
|
---|
633 | * Flush log first.
|
---|
634 | */
|
---|
635 | if (pDbgc->fLog)
|
---|
636 | {
|
---|
637 | int rc = dbgcProcessLog(pDbgc);
|
---|
638 | if (RT_FAILURE(rc))
|
---|
639 | return rc;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /*
|
---|
643 | * Process the event.
|
---|
644 | */
|
---|
645 | pDbgc->pszScratch = &pDbgc->achInput[0];
|
---|
646 | pDbgc->iArg = 0;
|
---|
647 | bool fPrintPrompt = true;
|
---|
648 | int rc = VINF_SUCCESS;
|
---|
649 | switch (pEvent->enmType)
|
---|
650 | {
|
---|
651 | /*
|
---|
652 | * The first part is events we have initiated with commands.
|
---|
653 | */
|
---|
654 | case DBGFEVENT_HALT_DONE:
|
---|
655 | {
|
---|
656 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: VM %p is halted! (%s)\n",
|
---|
657 | pDbgc->pVM, dbgcGetEventCtx(pEvent->enmCtx));
|
---|
658 | pDbgc->fRegCtxGuest = true; /* we're always in guest context when halted. */
|
---|
659 | if (RT_SUCCESS(rc))
|
---|
660 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
|
---|
661 | break;
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | /*
|
---|
666 | * The second part is events which can occur at any time.
|
---|
667 | */
|
---|
668 | case DBGFEVENT_FATAL_ERROR:
|
---|
669 | {
|
---|
670 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbf event: Fatal error! (%s)\n",
|
---|
671 | dbgcGetEventCtx(pEvent->enmCtx));
|
---|
672 | pDbgc->fRegCtxGuest = false; /* fatal errors are always in hypervisor. */
|
---|
673 | if (RT_SUCCESS(rc))
|
---|
674 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
|
---|
675 | break;
|
---|
676 | }
|
---|
677 |
|
---|
678 | case DBGFEVENT_BREAKPOINT:
|
---|
679 | case DBGFEVENT_BREAKPOINT_IO:
|
---|
680 | case DBGFEVENT_BREAKPOINT_MMIO:
|
---|
681 | case DBGFEVENT_BREAKPOINT_HYPER:
|
---|
682 | {
|
---|
683 | bool fRegCtxGuest = pDbgc->fRegCtxGuest;
|
---|
684 | pDbgc->fRegCtxGuest = pEvent->enmType != DBGFEVENT_BREAKPOINT_HYPER;
|
---|
685 |
|
---|
686 | rc = dbgcBpExec(pDbgc, pEvent->u.Bp.iBp);
|
---|
687 | switch (rc)
|
---|
688 | {
|
---|
689 | case VERR_DBGC_BP_NOT_FOUND:
|
---|
690 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Unknown breakpoint %u! (%s)\n",
|
---|
691 | pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
|
---|
692 | break;
|
---|
693 |
|
---|
694 | case VINF_DBGC_BP_NO_COMMAND:
|
---|
695 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! (%s)\n",
|
---|
696 | pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
|
---|
697 | break;
|
---|
698 |
|
---|
699 | case VINF_BUFFER_OVERFLOW:
|
---|
700 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! Command too long to execute! (%s)\n",
|
---|
701 | pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
|
---|
702 | break;
|
---|
703 |
|
---|
704 | default:
|
---|
705 | break;
|
---|
706 | }
|
---|
707 | if (RT_SUCCESS(rc) && DBGFR3IsHalted(pDbgc->pUVM))
|
---|
708 | {
|
---|
709 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
|
---|
710 |
|
---|
711 | /* Set the resume flag to ignore the breakpoint when resuming execution. */
|
---|
712 | if ( RT_SUCCESS(rc)
|
---|
713 | && pEvent->enmType == DBGFEVENT_BREAKPOINT)
|
---|
714 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r eflags.rf = 1");
|
---|
715 | }
|
---|
716 | else
|
---|
717 | pDbgc->fRegCtxGuest = fRegCtxGuest;
|
---|
718 | break;
|
---|
719 | }
|
---|
720 |
|
---|
721 | case DBGFEVENT_STEPPED:
|
---|
722 | case DBGFEVENT_STEPPED_HYPER:
|
---|
723 | {
|
---|
724 | pDbgc->fRegCtxGuest = pEvent->enmType == DBGFEVENT_STEPPED;
|
---|
725 |
|
---|
726 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Single step! (%s)\n", dbgcGetEventCtx(pEvent->enmCtx));
|
---|
727 | if (RT_SUCCESS(rc))
|
---|
728 | {
|
---|
729 | if (pDbgc->fStepTraceRegs)
|
---|
730 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
|
---|
731 | else
|
---|
732 | {
|
---|
733 | char szCmd[80];
|
---|
734 | if (!pDbgc->fRegCtxGuest)
|
---|
735 | rc = DBGFR3RegPrintf(pDbgc->pUVM, pDbgc->idCpu | DBGFREG_HYPER_VMCPUID, szCmd, sizeof(szCmd),
|
---|
736 | "u %VR{cs}:%VR{eip} L 0");
|
---|
737 | else if (DBGFR3CpuIsIn64BitCode(pDbgc->pUVM, pDbgc->idCpu))
|
---|
738 | rc = DBGFR3RegPrintf(pDbgc->pUVM, pDbgc->idCpu, szCmd, sizeof(szCmd), "u %016VR{rip} L 0");
|
---|
739 | else if (DBGFR3CpuIsInV86Code(pDbgc->pUVM, pDbgc->idCpu))
|
---|
740 | rc = DBGFR3RegPrintf(pDbgc->pUVM, pDbgc->idCpu, szCmd, sizeof(szCmd), "uv86 %04VR{cs}:%08VR{eip} L 0");
|
---|
741 | else
|
---|
742 | rc = DBGFR3RegPrintf(pDbgc->pUVM, pDbgc->idCpu, szCmd, sizeof(szCmd), "u %04VR{cs}:%08VR{eip} L 0");
|
---|
743 | if (RT_SUCCESS(rc))
|
---|
744 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "%s", szCmd);
|
---|
745 | }
|
---|
746 | }
|
---|
747 | break;
|
---|
748 | }
|
---|
749 |
|
---|
750 | case DBGFEVENT_ASSERTION_HYPER:
|
---|
751 | {
|
---|
752 | pDbgc->fRegCtxGuest = false;
|
---|
753 |
|
---|
754 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
|
---|
755 | "\ndbgf event: Hypervisor Assertion! (%s)\n"
|
---|
756 | "%s"
|
---|
757 | "%s"
|
---|
758 | "\n",
|
---|
759 | dbgcGetEventCtx(pEvent->enmCtx),
|
---|
760 | pEvent->u.Assert.pszMsg1,
|
---|
761 | pEvent->u.Assert.pszMsg2);
|
---|
762 | if (RT_SUCCESS(rc))
|
---|
763 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
|
---|
764 | break;
|
---|
765 | }
|
---|
766 |
|
---|
767 | case DBGFEVENT_DEV_STOP:
|
---|
768 | {
|
---|
769 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
|
---|
770 | "\n"
|
---|
771 | "dbgf event: DBGFSTOP (%s)\n"
|
---|
772 | "File: %s\n"
|
---|
773 | "Line: %d\n"
|
---|
774 | "Function: %s\n",
|
---|
775 | dbgcGetEventCtx(pEvent->enmCtx),
|
---|
776 | pEvent->u.Src.pszFile,
|
---|
777 | pEvent->u.Src.uLine,
|
---|
778 | pEvent->u.Src.pszFunction);
|
---|
779 | if (RT_SUCCESS(rc) && pEvent->u.Src.pszMessage && *pEvent->u.Src.pszMessage)
|
---|
780 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
|
---|
781 | "Message: %s\n",
|
---|
782 | pEvent->u.Src.pszMessage);
|
---|
783 | if (RT_SUCCESS(rc))
|
---|
784 | rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
|
---|
785 | break;
|
---|
786 | }
|
---|
787 |
|
---|
788 |
|
---|
789 | case DBGFEVENT_INVALID_COMMAND:
|
---|
790 | {
|
---|
791 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Invalid command event!\n");
|
---|
792 | break;
|
---|
793 | }
|
---|
794 |
|
---|
795 | case DBGFEVENT_POWERING_OFF:
|
---|
796 | {
|
---|
797 | pDbgc->fReady = false;
|
---|
798 | pDbgc->pBack->pfnSetReady(pDbgc->pBack, false);
|
---|
799 | pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\nVM is powering off!\n");
|
---|
800 | fPrintPrompt = false;
|
---|
801 | rc = VERR_GENERAL_FAILURE;
|
---|
802 | break;
|
---|
803 | }
|
---|
804 |
|
---|
805 |
|
---|
806 | default:
|
---|
807 | {
|
---|
808 | /*
|
---|
809 | * Probably a generic event. Look it up to find its name.
|
---|
810 | */
|
---|
811 | PCDBGCSXEVT pEvtDesc = dbgcEventLookup(pEvent->enmType);
|
---|
812 | if (pEvtDesc)
|
---|
813 | {
|
---|
814 | if (pEvtDesc->enmKind == kDbgcSxEventKind_Interrupt)
|
---|
815 | {
|
---|
816 | Assert(pEvtDesc->pszDesc);
|
---|
817 | Assert(pEvent->u.Generic.cArgs == 1);
|
---|
818 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s no %#llx! (%s)\n",
|
---|
819 | pEvtDesc->pszDesc, pEvent->u.Generic.auArgs[0], pEvtDesc->pszName);
|
---|
820 | }
|
---|
821 | else if (pEvtDesc->fFlags & DBGCSXEVT_F_BUGCHECK)
|
---|
822 | {
|
---|
823 | Assert(pEvent->u.Generic.cArgs >= 5);
|
---|
824 | char szDetails[512];
|
---|
825 | DBGFR3FormatBugCheck(pDbgc->pUVM, szDetails, sizeof(szDetails), pEvent->u.Generic.auArgs[0],
|
---|
826 | pEvent->u.Generic.auArgs[1], pEvent->u.Generic.auArgs[2],
|
---|
827 | pEvent->u.Generic.auArgs[3], pEvent->u.Generic.auArgs[4]);
|
---|
828 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s %s%s!\n%s", pEvtDesc->pszName,
|
---|
829 | pEvtDesc->pszDesc ? "- " : "", pEvtDesc->pszDesc ? pEvtDesc->pszDesc : "",
|
---|
830 | szDetails);
|
---|
831 | }
|
---|
832 | else if ( (pEvtDesc->fFlags & DBGCSXEVT_F_TAKE_ARG)
|
---|
833 | || pEvent->u.Generic.cArgs > 1
|
---|
834 | || ( pEvent->u.Generic.cArgs == 1
|
---|
835 | && pEvent->u.Generic.auArgs[0] != 0))
|
---|
836 | {
|
---|
837 | if (pEvtDesc->pszDesc)
|
---|
838 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s - %s!",
|
---|
839 | pEvtDesc->pszName, pEvtDesc->pszDesc);
|
---|
840 | else
|
---|
841 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s!", pEvtDesc->pszName);
|
---|
842 | if (pEvent->u.Generic.cArgs <= 1)
|
---|
843 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " arg=%u%#llx\n", pEvent->u.Generic.auArgs[0]);
|
---|
844 | else
|
---|
845 | {
|
---|
846 | for (uint32_t i = 0; i < pEvent->u.Generic.cArgs; i++)
|
---|
847 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " args[%u]=%#llx", i, pEvent->u.Generic.auArgs[i]);
|
---|
848 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n");
|
---|
849 | }
|
---|
850 | }
|
---|
851 | else
|
---|
852 | {
|
---|
853 | if (pEvtDesc->pszDesc)
|
---|
854 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s - %s!\n",
|
---|
855 | pEvtDesc->pszName, pEvtDesc->pszDesc);
|
---|
856 | else
|
---|
857 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s!\n", pEvtDesc->pszName);
|
---|
858 | }
|
---|
859 | }
|
---|
860 | else
|
---|
861 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Unknown event %d!\n", pEvent->enmType);
|
---|
862 | break;
|
---|
863 | }
|
---|
864 | }
|
---|
865 |
|
---|
866 | /*
|
---|
867 | * Prompt, anyone?
|
---|
868 | */
|
---|
869 | if (fPrintPrompt && RT_SUCCESS(rc))
|
---|
870 | {
|
---|
871 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
|
---|
872 | pDbgc->fReady = true;
|
---|
873 | if (RT_SUCCESS(rc))
|
---|
874 | pDbgc->pBack->pfnSetReady(pDbgc->pBack, true);
|
---|
875 | }
|
---|
876 |
|
---|
877 | return rc;
|
---|
878 | }
|
---|
879 |
|
---|
880 |
|
---|
881 | /**
|
---|
882 | * Prints any log lines from the log buffer.
|
---|
883 | *
|
---|
884 | * The caller must not call function this unless pDbgc->fLog is set.
|
---|
885 | *
|
---|
886 | * @returns VBox status code. (output related)
|
---|
887 | * @param pDbgc Debugger console instance data.
|
---|
888 | */
|
---|
889 | static int dbgcProcessLog(PDBGC pDbgc)
|
---|
890 | {
|
---|
891 | /** @todo */
|
---|
892 | NOREF(pDbgc);
|
---|
893 | return 0;
|
---|
894 | }
|
---|
895 |
|
---|
896 | /** @callback_method_impl{FNRTDBGCFGLOG} */
|
---|
897 | static DECLCALLBACK(void) dbgcDbgCfgLogCallback(RTDBGCFG hDbgCfg, uint32_t iLevel, const char *pszMsg, void *pvUser)
|
---|
898 | {
|
---|
899 | /** @todo Add symbol noise setting. */
|
---|
900 | NOREF(hDbgCfg); NOREF(iLevel);
|
---|
901 | PDBGC pDbgc = (PDBGC)pvUser;
|
---|
902 | pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s", pszMsg);
|
---|
903 | }
|
---|
904 |
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Run the debugger console.
|
---|
908 | *
|
---|
909 | * @returns VBox status code.
|
---|
910 | * @param pDbgc Pointer to the debugger console instance data.
|
---|
911 | */
|
---|
912 | int dbgcRun(PDBGC pDbgc)
|
---|
913 | {
|
---|
914 | /*
|
---|
915 | * We're ready for commands now.
|
---|
916 | */
|
---|
917 | pDbgc->fReady = true;
|
---|
918 | pDbgc->pBack->pfnSetReady(pDbgc->pBack, true);
|
---|
919 |
|
---|
920 | /*
|
---|
921 | * Main Debugger Loop.
|
---|
922 | *
|
---|
923 | * This loop will either block on waiting for input or on waiting on
|
---|
924 | * debug events. If we're forwarding the log we cannot wait for long
|
---|
925 | * before we must flush the log.
|
---|
926 | */
|
---|
927 | int rc;
|
---|
928 | for (;;)
|
---|
929 | {
|
---|
930 | rc = VERR_SEM_OUT_OF_TURN;
|
---|
931 | if (pDbgc->pUVM)
|
---|
932 | rc = DBGFR3QueryWaitable(pDbgc->pUVM);
|
---|
933 |
|
---|
934 | if (RT_SUCCESS(rc))
|
---|
935 | {
|
---|
936 | /*
|
---|
937 | * Wait for a debug event.
|
---|
938 | */
|
---|
939 | PCDBGFEVENT pEvent;
|
---|
940 | rc = DBGFR3EventWait(pDbgc->pUVM, pDbgc->fLog ? 1 : 32, &pEvent);
|
---|
941 | if (RT_SUCCESS(rc))
|
---|
942 | {
|
---|
943 | rc = dbgcProcessEvent(pDbgc, pEvent);
|
---|
944 | if (RT_FAILURE(rc))
|
---|
945 | break;
|
---|
946 | }
|
---|
947 | else if (rc != VERR_TIMEOUT)
|
---|
948 | break;
|
---|
949 |
|
---|
950 | /*
|
---|
951 | * Check for input.
|
---|
952 | */
|
---|
953 | if (pDbgc->pBack->pfnInput(pDbgc->pBack, 0))
|
---|
954 | {
|
---|
955 | rc = dbgcProcessInput(pDbgc, false /* fNoExecute */);
|
---|
956 | if (RT_FAILURE(rc))
|
---|
957 | break;
|
---|
958 | }
|
---|
959 | }
|
---|
960 | else if (rc == VERR_SEM_OUT_OF_TURN)
|
---|
961 | {
|
---|
962 | /*
|
---|
963 | * Wait for input. If Logging is enabled we'll only wait very briefly.
|
---|
964 | */
|
---|
965 | if (pDbgc->pBack->pfnInput(pDbgc->pBack, pDbgc->fLog ? 1 : 1000))
|
---|
966 | {
|
---|
967 | rc = dbgcProcessInput(pDbgc, false /* fNoExecute */);
|
---|
968 | if (RT_FAILURE(rc))
|
---|
969 | break;
|
---|
970 | }
|
---|
971 | }
|
---|
972 | else
|
---|
973 | break;
|
---|
974 |
|
---|
975 | /*
|
---|
976 | * Forward log output.
|
---|
977 | */
|
---|
978 | if (pDbgc->fLog)
|
---|
979 | {
|
---|
980 | rc = dbgcProcessLog(pDbgc);
|
---|
981 | if (RT_FAILURE(rc))
|
---|
982 | break;
|
---|
983 | }
|
---|
984 | }
|
---|
985 |
|
---|
986 | return rc;
|
---|
987 | }
|
---|
988 |
|
---|
989 |
|
---|
990 | /**
|
---|
991 | * Run the init scripts, if present.
|
---|
992 | *
|
---|
993 | * @param pDbgc The console instance.
|
---|
994 | */
|
---|
995 | static void dbgcRunInitScripts(PDBGC pDbgc)
|
---|
996 | {
|
---|
997 | /*
|
---|
998 | * Do the global one, if it exists.
|
---|
999 | */
|
---|
1000 | if ( pDbgc->pszGlobalInitScript
|
---|
1001 | && *pDbgc->pszGlobalInitScript != '\0'
|
---|
1002 | && RTFileExists(pDbgc->pszGlobalInitScript))
|
---|
1003 | dbgcEvalScript(pDbgc, pDbgc->pszGlobalInitScript, true /*fAnnounce*/);
|
---|
1004 |
|
---|
1005 | /*
|
---|
1006 | * Then do the local one, if it exists.
|
---|
1007 | */
|
---|
1008 | if ( pDbgc->pszLocalInitScript
|
---|
1009 | && *pDbgc->pszLocalInitScript != '\0'
|
---|
1010 | && RTFileExists(pDbgc->pszLocalInitScript))
|
---|
1011 | dbgcEvalScript(pDbgc, pDbgc->pszLocalInitScript, true /*fAnnounce*/);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | /**
|
---|
1016 | * Reads the CFGM configuration of the DBGC.
|
---|
1017 | *
|
---|
1018 | * Popuplates the PDBGC::pszHistoryFile, PDBGC::pszGlobalInitScript and
|
---|
1019 | * PDBGC::pszLocalInitScript members.
|
---|
1020 | *
|
---|
1021 | * @returns VBox status code.
|
---|
1022 | * @param pDbgc The console instance.
|
---|
1023 | * @param pUVM The user mode VM handle.
|
---|
1024 | */
|
---|
1025 | static int dbgcReadConfig(PDBGC pDbgc, PUVM pUVM)
|
---|
1026 | {
|
---|
1027 | /*
|
---|
1028 | * Get and validate the configuration node.
|
---|
1029 | */
|
---|
1030 | PCFGMNODE pNode = CFGMR3GetChild(CFGMR3GetRootU(pUVM), "DBGC");
|
---|
1031 | int rc = CFGMR3ValidateConfig(pNode, "/DBGC/",
|
---|
1032 | "Enabled|"
|
---|
1033 | "HistoryFile|"
|
---|
1034 | "LocalInitScript|"
|
---|
1035 | "GlobalInitScript",
|
---|
1036 | "", "DBGC", 0);
|
---|
1037 | AssertRCReturn(rc, rc);
|
---|
1038 |
|
---|
1039 | /*
|
---|
1040 | * Query the values.
|
---|
1041 | */
|
---|
1042 | char szHomeDefault[RTPATH_MAX];
|
---|
1043 | rc = RTPathUserHome(szHomeDefault, sizeof(szHomeDefault) - 32);
|
---|
1044 | AssertLogRelRCReturn(rc, rc);
|
---|
1045 | size_t cchHome = strlen(szHomeDefault);
|
---|
1046 |
|
---|
1047 | /** @cfgm{/DBGC/HistoryFile, string, ${HOME}/.vboxdbgc-history}
|
---|
1048 | * The command history file of the VBox debugger. */
|
---|
1049 | rc = RTPathAppend(szHomeDefault, sizeof(szHomeDefault), ".vboxdbgc-history");
|
---|
1050 | AssertLogRelRCReturn(rc, rc);
|
---|
1051 |
|
---|
1052 | char szPath[RTPATH_MAX];
|
---|
1053 | rc = CFGMR3QueryStringDef(pNode, "HistoryFile", szPath, sizeof(szPath), szHomeDefault);
|
---|
1054 | AssertLogRelRCReturn(rc, rc);
|
---|
1055 |
|
---|
1056 | pDbgc->pszHistoryFile = RTStrDup(szPath);
|
---|
1057 | AssertReturn(pDbgc->pszHistoryFile, VERR_NO_STR_MEMORY);
|
---|
1058 |
|
---|
1059 | /** @cfgm{/DBGC/GlobalInitFile, string, ${HOME}/.vboxdbgc-init}
|
---|
1060 | * The global init script of the VBox debugger. */
|
---|
1061 | szHomeDefault[cchHome] = '\0';
|
---|
1062 | rc = RTPathAppend(szHomeDefault, sizeof(szHomeDefault), ".vboxdbgc-init");
|
---|
1063 | AssertLogRelRCReturn(rc, rc);
|
---|
1064 |
|
---|
1065 | rc = CFGMR3QueryStringDef(pNode, "GlobalInitScript", szPath, sizeof(szPath), szHomeDefault);
|
---|
1066 | AssertLogRelRCReturn(rc, rc);
|
---|
1067 |
|
---|
1068 | pDbgc->pszGlobalInitScript = RTStrDup(szPath);
|
---|
1069 | AssertReturn(pDbgc->pszGlobalInitScript, VERR_NO_STR_MEMORY);
|
---|
1070 |
|
---|
1071 | /** @cfgm{/DBGC/LocalInitFile, string, none}
|
---|
1072 | * The VM local init script of the VBox debugger. */
|
---|
1073 | rc = CFGMR3QueryString(pNode, "LocalInitScript", szPath, sizeof(szPath));
|
---|
1074 | if (RT_SUCCESS(rc))
|
---|
1075 | {
|
---|
1076 | pDbgc->pszLocalInitScript = RTStrDup(szPath);
|
---|
1077 | AssertReturn(pDbgc->pszLocalInitScript, VERR_NO_STR_MEMORY);
|
---|
1078 | }
|
---|
1079 | else
|
---|
1080 | {
|
---|
1081 | AssertLogRelReturn(rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT, rc);
|
---|
1082 | pDbgc->pszLocalInitScript = NULL;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | return VINF_SUCCESS;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 |
|
---|
1089 |
|
---|
1090 | /**
|
---|
1091 | * Creates a a new instance.
|
---|
1092 | *
|
---|
1093 | * @returns VBox status code.
|
---|
1094 | * @param ppDbgc Where to store the pointer to the instance data.
|
---|
1095 | * @param pBack Pointer to the backend.
|
---|
1096 | * @param fFlags The flags.
|
---|
1097 | */
|
---|
1098 | int dbgcCreate(PDBGC *ppDbgc, PDBGCBACK pBack, unsigned fFlags)
|
---|
1099 | {
|
---|
1100 | /*
|
---|
1101 | * Validate input.
|
---|
1102 | */
|
---|
1103 | AssertPtrReturn(pBack, VERR_INVALID_POINTER);
|
---|
1104 | AssertMsgReturn(!fFlags, ("%#x", fFlags), VERR_INVALID_PARAMETER);
|
---|
1105 |
|
---|
1106 | /*
|
---|
1107 | * Allocate and initialize.
|
---|
1108 | */
|
---|
1109 | PDBGC pDbgc = (PDBGC)RTMemAllocZ(sizeof(*pDbgc));
|
---|
1110 | if (!pDbgc)
|
---|
1111 | return VERR_NO_MEMORY;
|
---|
1112 |
|
---|
1113 | dbgcInitCmdHlp(pDbgc);
|
---|
1114 | pDbgc->pBack = pBack;
|
---|
1115 | pDbgc->pVM = NULL;
|
---|
1116 | pDbgc->pUVM = NULL;
|
---|
1117 | pDbgc->idCpu = 0;
|
---|
1118 | pDbgc->hDbgAs = DBGF_AS_GLOBAL;
|
---|
1119 | pDbgc->pszEmulation = "CodeView/WinDbg";
|
---|
1120 | pDbgc->paEmulationCmds = &g_aCmdsCodeView[0];
|
---|
1121 | pDbgc->cEmulationCmds = g_cCmdsCodeView;
|
---|
1122 | pDbgc->paEmulationFuncs = &g_aFuncsCodeView[0];
|
---|
1123 | pDbgc->cEmulationFuncs = g_cFuncsCodeView;
|
---|
1124 | //pDbgc->fLog = false;
|
---|
1125 | pDbgc->fRegCtxGuest = true;
|
---|
1126 | pDbgc->fRegTerse = true;
|
---|
1127 | pDbgc->fStepTraceRegs = true;
|
---|
1128 | //pDbgc->cPagingHierarchyDumps = 0;
|
---|
1129 | //pDbgc->DisasmPos = {0};
|
---|
1130 | //pDbgc->SourcePos = {0};
|
---|
1131 | //pDbgc->DumpPos = {0};
|
---|
1132 | pDbgc->pLastPos = &pDbgc->DisasmPos;
|
---|
1133 | //pDbgc->cbDumpElement = 0;
|
---|
1134 | //pDbgc->cVars = 0;
|
---|
1135 | //pDbgc->paVars = NULL;
|
---|
1136 | //pDbgc->pPlugInHead = NULL;
|
---|
1137 | //pDbgc->pFirstBp = NULL;
|
---|
1138 | //pDbgc->abSearch = {0};
|
---|
1139 | //pDbgc->cbSearch = 0;
|
---|
1140 | pDbgc->cbSearchUnit = 1;
|
---|
1141 | pDbgc->cMaxSearchHits = 1;
|
---|
1142 | //pDbgc->SearchAddr = {0};
|
---|
1143 | //pDbgc->cbSearchRange = 0;
|
---|
1144 |
|
---|
1145 | //pDbgc->uInputZero = 0;
|
---|
1146 | //pDbgc->iRead = 0;
|
---|
1147 | //pDbgc->iWrite = 0;
|
---|
1148 | //pDbgc->cInputLines = 0;
|
---|
1149 | //pDbgc->fInputOverflow = false;
|
---|
1150 | pDbgc->fReady = true;
|
---|
1151 | pDbgc->pszScratch = &pDbgc->achScratch[0];
|
---|
1152 | //pDbgc->iArg = 0;
|
---|
1153 | //pDbgc->rcOutput = 0;
|
---|
1154 | //pDbgc->rcCmd = 0;
|
---|
1155 |
|
---|
1156 | //pDbgc->pszHistoryFile = NULL;
|
---|
1157 | //pDbgc->pszGlobalInitScript = NULL;
|
---|
1158 | //pDbgc->pszLocalInitScript = NULL;
|
---|
1159 |
|
---|
1160 | dbgcEvalInit();
|
---|
1161 |
|
---|
1162 | *ppDbgc = pDbgc;
|
---|
1163 | return VINF_SUCCESS;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | /**
|
---|
1167 | * Destroys a DBGC instance created by dbgcCreate.
|
---|
1168 | *
|
---|
1169 | * @param pDbgc Pointer to the debugger console instance data.
|
---|
1170 | */
|
---|
1171 | void dbgcDestroy(PDBGC pDbgc)
|
---|
1172 | {
|
---|
1173 | AssertPtr(pDbgc);
|
---|
1174 |
|
---|
1175 | /* Disable log hook. */
|
---|
1176 | if (pDbgc->fLog)
|
---|
1177 | {
|
---|
1178 |
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | /* Detach from the VM. */
|
---|
1182 | if (pDbgc->pUVM)
|
---|
1183 | DBGFR3Detach(pDbgc->pUVM);
|
---|
1184 |
|
---|
1185 | /* Free config strings. */
|
---|
1186 | RTStrFree(pDbgc->pszGlobalInitScript);
|
---|
1187 | pDbgc->pszGlobalInitScript = NULL;
|
---|
1188 | RTStrFree(pDbgc->pszLocalInitScript);
|
---|
1189 | pDbgc->pszLocalInitScript = NULL;
|
---|
1190 | RTStrFree(pDbgc->pszHistoryFile);
|
---|
1191 | pDbgc->pszHistoryFile = NULL;
|
---|
1192 |
|
---|
1193 | /* Finally, free the instance memory. */
|
---|
1194 | RTMemFree(pDbgc);
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 |
|
---|
1198 | /**
|
---|
1199 | * Make a console instance.
|
---|
1200 | *
|
---|
1201 | * This will not return until either an 'exit' command is issued or a error code
|
---|
1202 | * indicating connection loss is encountered.
|
---|
1203 | *
|
---|
1204 | * @returns VINF_SUCCESS if console termination caused by the 'exit' command.
|
---|
1205 | * @returns The VBox status code causing the console termination.
|
---|
1206 | *
|
---|
1207 | * @param pUVM The user mode VM handle.
|
---|
1208 | * @param pBack Pointer to the backend structure. This must contain
|
---|
1209 | * a full set of function pointers to service the console.
|
---|
1210 | * @param fFlags Reserved, must be zero.
|
---|
1211 | * @remarks A forced termination of the console is easiest done by forcing the
|
---|
1212 | * callbacks to return fatal failures.
|
---|
1213 | */
|
---|
1214 | DBGDECL(int) DBGCCreate(PUVM pUVM, PDBGCBACK pBack, unsigned fFlags)
|
---|
1215 | {
|
---|
1216 | /*
|
---|
1217 | * Validate input.
|
---|
1218 | */
|
---|
1219 | AssertPtrNullReturn(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
1220 | PVM pVM = NULL;
|
---|
1221 | if (pUVM)
|
---|
1222 | {
|
---|
1223 | pVM = VMR3GetVM(pUVM);
|
---|
1224 | AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /*
|
---|
1228 | * Allocate and initialize instance data
|
---|
1229 | */
|
---|
1230 | PDBGC pDbgc;
|
---|
1231 | int rc = dbgcCreate(&pDbgc, pBack, fFlags);
|
---|
1232 | if (RT_FAILURE(rc))
|
---|
1233 | return rc;
|
---|
1234 | if (!HMR3IsEnabled(pUVM) && !NEMR3IsEnabled(pUVM))
|
---|
1235 | pDbgc->hDbgAs = DBGF_AS_RC_AND_GC_GLOBAL;
|
---|
1236 |
|
---|
1237 | /*
|
---|
1238 | * Print welcome message.
|
---|
1239 | */
|
---|
1240 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
|
---|
1241 | "Welcome to the VirtualBox Debugger!\n");
|
---|
1242 |
|
---|
1243 | /*
|
---|
1244 | * Attach to the specified VM.
|
---|
1245 | */
|
---|
1246 | if (RT_SUCCESS(rc) && pUVM)
|
---|
1247 | {
|
---|
1248 | rc = dbgcReadConfig(pDbgc, pUVM);
|
---|
1249 | if (RT_SUCCESS(rc))
|
---|
1250 | {
|
---|
1251 | rc = DBGFR3Attach(pUVM);
|
---|
1252 | if (RT_SUCCESS(rc))
|
---|
1253 | {
|
---|
1254 | pDbgc->pVM = pVM;
|
---|
1255 | pDbgc->pUVM = pUVM;
|
---|
1256 | pDbgc->idCpu = 0;
|
---|
1257 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
|
---|
1258 | "Current VM is %08x, CPU #%u\n" /** @todo get and print the VM name! */
|
---|
1259 | , pDbgc->pVM, pDbgc->idCpu);
|
---|
1260 | }
|
---|
1261 | else
|
---|
1262 | rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to attach to VM %p\n", pDbgc->pVM);
|
---|
1263 | }
|
---|
1264 | else
|
---|
1265 | rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "Error reading configuration\n");
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | /*
|
---|
1269 | * Load plugins.
|
---|
1270 | */
|
---|
1271 | if (RT_SUCCESS(rc))
|
---|
1272 | {
|
---|
1273 | if (pVM)
|
---|
1274 | DBGFR3PlugInLoadAll(pDbgc->pUVM);
|
---|
1275 | dbgcEventInit(pDbgc);
|
---|
1276 | dbgcRunInitScripts(pDbgc);
|
---|
1277 |
|
---|
1278 | rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
|
---|
1279 | if (RT_SUCCESS(rc))
|
---|
1280 | {
|
---|
1281 | /*
|
---|
1282 | * Set debug config log callback.
|
---|
1283 | */
|
---|
1284 | RTDBGCFG hDbgCfg = DBGFR3AsGetConfig(pUVM);
|
---|
1285 | if ( hDbgCfg != NIL_RTDBGCFG
|
---|
1286 | && RTDbgCfgRetain(hDbgCfg) != UINT32_MAX)
|
---|
1287 | {
|
---|
1288 | int rc2 = RTDbgCfgSetLogCallback(hDbgCfg, dbgcDbgCfgLogCallback, pDbgc);
|
---|
1289 | if (RT_FAILURE(rc2))
|
---|
1290 | {
|
---|
1291 | hDbgCfg = NIL_RTDBGCFG;
|
---|
1292 | RTDbgCfgRelease(hDbgCfg);
|
---|
1293 | }
|
---|
1294 | }
|
---|
1295 | else
|
---|
1296 | hDbgCfg = NIL_RTDBGCFG;
|
---|
1297 |
|
---|
1298 |
|
---|
1299 | /*
|
---|
1300 | * Run the debugger main loop.
|
---|
1301 | */
|
---|
1302 | rc = dbgcRun(pDbgc);
|
---|
1303 |
|
---|
1304 |
|
---|
1305 | /*
|
---|
1306 | * Remove debug config log callback.
|
---|
1307 | */
|
---|
1308 | if (hDbgCfg != NIL_RTDBGCFG)
|
---|
1309 | {
|
---|
1310 | RTDbgCfgSetLogCallback(hDbgCfg, NULL, NULL);
|
---|
1311 | RTDbgCfgRelease(hDbgCfg);
|
---|
1312 | }
|
---|
1313 | }
|
---|
1314 | dbgcEventTerm(pDbgc);
|
---|
1315 | }
|
---|
1316 | else
|
---|
1317 | pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\nDBGCCreate error: %Rrc\n", rc);
|
---|
1318 |
|
---|
1319 |
|
---|
1320 | /*
|
---|
1321 | * Cleanup console debugger session.
|
---|
1322 | */
|
---|
1323 | dbgcDestroy(pDbgc);
|
---|
1324 | return rc == VERR_DBGC_QUIT ? VINF_SUCCESS : rc;
|
---|
1325 | }
|
---|
1326 |
|
---|