VirtualBox

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

Last change on this file since 28 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 302.1 KB
Line 
1/** @file
2 *
3 * Debugger Console.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/** @page pg_dbgc DBGC - The Debug Console
24 *
25 * The debugger console is a first attempt to make some interactive
26 * debugging facilities for the VirtualBox backend (i.e. the VM). At a later
27 * stage we'll make a fancy gui around this, but for the present a telnet (or
28 * serial terminal) will have to suffice.
29 *
30 * The debugger is only built into the VM with debug builds or when
31 * VBOX_WITH_DEBUGGER is defined. There might be need for \#ifdef'ing on this
32 * define to enable special debugger hooks, but the general approach is to
33 * make generic interfaces. The individual components also can register
34 * external commands, and such code must be within \#ifdef.
35 *
36 *
37 * @section sec_dbgc_op Operation (intentions)
38 *
39 * The console will process commands in a manner similar to the OS/2 and
40 * windows kernel debuggers. This means ';' is a command separator and
41 * that when possible we'll use the same command names as these two uses.
42 *
43 *
44 * @subsection sec_dbg_op_numbers Numbers
45 *
46 * Numbers are hexadecimal unless specified with a prefix indicating
47 * elsewise. Prefixes:
48 * - '0x' - hexadecimal.
49 * - '0i' - decimal
50 * - '0t' - octal.
51 * - '0y' - binary.
52 *
53 *
54 * @subsection sec_dbg_op_address Addressing modes
55 *
56 * - Default is flat. For compatability '%' also means flat.
57 * - Segmented addresses are specified selector:offset.
58 * - Physical addresses are specified using '%%'.
59 * - The default target for the addressing is the guest context, the '#'
60 * will override this and set it to the host.
61 *
62 *
63 * @subsection sec_dbg_op_evalution Evaluation
64 *
65 * As time permits support will be implemented support for a subset of the C
66 * binary operators, starting with '+', '-', '*' and '/'. Support for variables
67 * are provided thru commands 'set' and 'unset' and the unary operator '$'. The
68 * unary '@' operator will indicate function calls. The debugger needs a set of
69 * memory read functions, but we might later extend this to allow registration of
70 * external functions too.
71 *
72 * A special command '?' will then be added which evalutates a given expression
73 * and prints it in all the different formats.
74 *
75 *
76 * @subsection sec_dbg_op_registers Registers
77 *
78 * Registers are addressed using their name. Some registers which have several fields
79 * (like gdtr) will have separate names indicating the different fields. The default
80 * register set is the guest one. To access the hypervisor register one have to
81 * prefix the register names with '.'.
82 *
83 *
84 * @subsection sec_dbg_op_commands Commands
85 *
86 * The commands are all lowercase, case sensitive, and starting with a letter. We will
87 * later add some special commands ('?' for evaulation) and perhaps command classes ('.', '!')
88 *
89 *
90 * @section sec_dbg_tasks Tasks
91 *
92 * To implement DBGT and instrument VMM for basic state inspection and log
93 * viewing, the follwing task must be executed:
94 *
95 * -# Basic threading layer in RT.
96 * -# Basic tcpip server abstration in RT.
97 * -# Write DBGC.
98 * -# Write DBCTCP.
99 * -# Integrate with VMM and the rest.
100 * -# Start writing DBGF (VMM).
101 */
102
103
104
105
106/*******************************************************************************
107* Header Files *
108*******************************************************************************/
109#define LOG_GROUP LOG_GROUP_DBGC
110#include <VBox/dbg.h>
111#include <VBox/dbgf.h>
112#include <VBox/vm.h>
113#include <VBox/vmm.h>
114#include <VBox/mm.h>
115#include <VBox/pgm.h>
116#include <VBox/selm.h>
117#include <VBox/dis.h>
118#include <VBox/param.h>
119#include <VBox/err.h>
120#include <VBox/log.h>
121
122#include <iprt/alloc.h>
123#include <iprt/alloca.h>
124#include <iprt/string.h>
125#include <iprt/assert.h>
126#include <iprt/ctype.h>
127
128#include <stdlib.h>
129#include <stdio.h>
130
131/* to err.h! */
132#define VERR_DBGC_QUIT (-11999)
133#define VERR_PARSE_FIRST (-11000)
134#define VERR_PARSE_TOO_FEW_ARGUMENTS (VERR_PARSE_FIRST - 0)
135#define VERR_PARSE_TOO_MANY_ARGUMENTS (VERR_PARSE_FIRST - 1)
136#define VERR_PARSE_ARGUMENT_OVERFLOW (VERR_PARSE_FIRST - 2)
137#define VERR_PARSE_ARGUMENT_TYPE_MISMATCH (VERR_PARSE_FIRST - 3)
138#define VERR_PARSE_NO_RANGE_ALLOWED (VERR_PARSE_FIRST - 4)
139#define VERR_PARSE_UNBALANCED_QUOTE (VERR_PARSE_FIRST - 5)
140#define VERR_PARSE_UNBALANCED_PARENTHESIS (VERR_PARSE_FIRST - 6)
141#define VERR_PARSE_EMPTY_ARGUMENT (VERR_PARSE_FIRST - 7)
142#define VERR_PARSE_UNEXPECTED_OPERATOR (VERR_PARSE_FIRST - 8)
143#define VERR_PARSE_INVALID_NUMBER (VERR_PARSE_FIRST - 9)
144#define VERR_PARSE_NUMBER_TOO_BIG (VERR_PARSE_FIRST - 10)
145#define VERR_PARSE_INVALID_OPERATION (VERR_PARSE_FIRST - 11)
146#define VERR_PARSE_FUNCTION_NOT_FOUND (VERR_PARSE_FIRST - 12)
147#define VERR_PARSE_NOT_A_FUNCTION (VERR_PARSE_FIRST - 13)
148#define VERR_PARSE_NO_MEMORY (VERR_PARSE_FIRST - 14)
149#define VERR_PARSE_INCORRECT_ARG_TYPE (VERR_PARSE_FIRST - 15)
150#define VERR_PARSE_VARIABLE_NOT_FOUND (VERR_PARSE_FIRST - 16)
151#define VERR_PARSE_CONVERSION_FAILED (VERR_PARSE_FIRST - 17)
152#define VERR_PARSE_NOT_IMPLEMENTED (VERR_PARSE_FIRST - 18)
153#define VERR_PARSE_BAD_RESULT_TYPE (VERR_PARSE_FIRST - 19)
154#define VERR_PARSE_WRITEONLY_SYMBOL (VERR_PARSE_FIRST - 20)
155#define VERR_PARSE_NO_ARGUMENT_MATCH (VERR_PARSE_FIRST - 21)
156#define VERR_PARSE_LAST (VERR_PARSE_FIRST - 30)
157
158#define VWRN_DBGC_CMD_PENDING 12000
159#define VWRN_DBGC_ALREADY_REGISTERED 12001
160#define VERR_DBGC_COMMANDS_NOT_REGISTERED (-12002)
161#define VERR_DBGC_BP_NOT_FOUND (-12003)
162#define VERR_DBGC_BP_EXISTS (-12004)
163#define VINF_DBGC_BP_NO_COMMAND 12005
164
165
166
167/*******************************************************************************
168* Defined Constants And Macros *
169*******************************************************************************/
170/** Makes a DBGC variable type pair.
171 * Typically used by binary operators. */
172#define BINARY_TYPE_PAIR(type1, type2) (type1 | (type2 << 16))
173
174
175/*******************************************************************************
176* Structures and Typedefs *
177*******************************************************************************/
178
179/**
180 * Debugger console per breakpoint data.
181 */
182typedef struct DBGCBP
183{
184 /** Pointer to the next breakpoint in the list. */
185 struct DBGCBP *pNext;
186 /** The breakpoint identifier. */
187 RTUINT iBp;
188 /** The size of the command. */
189 size_t cchCmd;
190 /** The command to execute when the breakpoint is hit. */
191 char szCmd[1];
192} DBGCBP;
193/** Pointer to a breakpoint. */
194typedef DBGCBP *PDBGCBP;
195
196
197/**
198 * Named variable.
199 *
200 * Always allocated from heap in one signle block.
201 */
202typedef struct DBGCNAMEDVAR
203{
204 /** The variable. */
205 DBGCVAR Var;
206 /** It's name. */
207 char szName[1];
208} DBGCNAMEDVAR;
209/** Pointer to named variable. */
210typedef DBGCNAMEDVAR *PDBGCNAMEDVAR;
211
212
213/**
214 * Debugger console status
215 */
216typedef enum DBGCSTATUS
217{
218 /** Normal status, .*/
219 DBGC_HALTED
220
221} DBGCSTATUS;
222
223
224/**
225 * Debugger console instance data.
226 */
227typedef struct DBGC
228{
229 /** Command helpers. */
230 DBGCCMDHLP CmdHlp;
231 /** Pointer to backend callback structure. */
232 PDBGCBACK pBack;
233 /** Log indicator. (If set we're writing the log to the console.) */
234 bool fLog;
235 /** Pointer to the current VM. */
236 PVM pVM;
237 /** Indicates whether or we're ready for input. */
238 bool fReady;
239
240 /** Indicates whether we're in guest (true) or hypervisor (false) register context. */
241 bool fRegCtxGuest;
242 /** Indicates whether the register are terse or sparse. */
243 bool fRegTerse;
244
245 /** Input buffer. */
246 char achInput[2048];
247 /** To ease debugging. */
248 unsigned uInputZero;
249 /** Write index in the input buffer. */
250 unsigned iWrite;
251 /** Read index in the input buffer. */
252 unsigned iRead;
253 /** The number of lines in the buffer. */
254 unsigned cInputLines;
255 /** Indicates that we have a buffer overflow condition.
256 * This means that input is ignored up to the next newline. */
257 bool fInputOverflow;
258
259 /** Scratch buffer position. */
260 char *pszScratch;
261 /** Scratch buffer. */
262 char achScratch[16384];
263 /** Argument array position. */
264 unsigned iArg;
265 /** Array of argument variables. */
266 DBGCVAR aArgs[100];
267
268 /** rc from last dbgcHlpPrintfV(). */
269 int rcOutput;
270
271 /** Number of variables in papVars. */
272 unsigned cVars;
273 /** Array of global variables.
274 * Global variables can be referenced using the $ operator and set
275 * and unset using command with those names. */
276 PDBGCNAMEDVAR *papVars;
277
278 /** Current dissassembler position. */
279 DBGCVAR DisasmPos;
280 /** Current source position. (flat GC) */
281 DBGCVAR SourcePos;
282 /** Current memory dump position. */
283 DBGCVAR DumpPos;
284 /** Size of the previous dump element. */
285 unsigned cbDumpElement;
286
287 /** The list of breakpoints. (singly linked) */
288 PDBGCBP pFirstBp;
289} DBGC;
290/** Pointer to debugger console instance data. */
291typedef DBGC *PDBGC;
292
293/** Converts a Command Helper pointer to a pointer to DBGC instance data. */
294#define DBGC_CMDHLP2DBGC(pCmdHlp) ( (PDBGC)((uintptr_t)(pCmdHlp) - RT_OFFSETOF(DBGC, CmdHlp)) )
295
296
297/**
298 * Chunk of external commands.
299 */
300typedef struct DBGCEXTCMDS
301{
302 /** Number of commands descriptors. */
303 unsigned cCmds;
304 /** Pointer to array of command descriptors. */
305 PCDBGCCMD paCmds;
306 /** Pointer to the next chunk. */
307 struct DBGCEXTCMDS *pNext;
308} DBGCEXTCMDS;
309/** Pointer to chunk of external commands. */
310typedef DBGCEXTCMDS *PDBGCEXTCMDS;
311
312
313
314/**
315 * Unary operator handler function.
316 *
317 * @returns 0 on success.
318 * @returns VBox evaluation / parsing error code on failure.
319 * The caller does the bitching.
320 * @param pDbgc Debugger console instance data.
321 * @param pArg The argument.
322 * @param pResult Where to store the result.
323 */
324typedef DECLCALLBACK(int) FNDBGCOPUNARY(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
325/** Pointer to a unary operator handler function. */
326typedef FNDBGCOPUNARY *PFNDBGCOPUNARY;
327
328
329/**
330 * Binary operator handler function.
331 *
332 * @returns 0 on success.
333 * @returns VBox evaluation / parsing error code on failure.
334 * The caller does the bitching.
335 * @param pDbgc Debugger console instance data.
336 * @param pArg1 The first argument.
337 * @param pArg2 The 2nd argument.
338 * @param pResult Where to store the result.
339 */
340typedef DECLCALLBACK(int) FNDBGCOPBINARY(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
341/** Pointer to a binary operator handler function. */
342typedef FNDBGCOPBINARY *PFNDBGCOPBINARY;
343
344
345/**
346 * Operator descriptor.
347 */
348typedef struct DBGCOP
349{
350 /** Operator mnemonic. */
351 char szName[4];
352 /** Length of name. */
353 const unsigned cchName;
354 /** Whether or not this is a binary operator.
355 * Unary operators are evaluated right-to-left while binary are left-to-right. */
356 bool fBinary;
357 /** Precedence level. */
358 unsigned iPrecedence;
359 /** Unary operator handler. */
360 PFNDBGCOPUNARY pfnHandlerUnary;
361 /** Binary operator handler. */
362 PFNDBGCOPBINARY pfnHandlerBinary;
363 /** Operator description. */
364 const char *pszDescription;
365} DBGCOP;
366/** Pointer to an operator descriptor. */
367typedef DBGCOP *PDBGCOP;
368/** Pointer to a const operator descriptor. */
369typedef const DBGCOP *PCDBGCOP;
370
371
372
373/** Pointer to symbol descriptor. */
374typedef struct DBGCSYM *PDBGCSYM;
375/** Pointer to const symbol descriptor. */
376typedef const struct DBGCSYM *PCDBGCSYM;
377
378/**
379 * Get builtin symbol.
380 *
381 * @returns 0 on success.
382 * @returns VBox evaluation / parsing error code on failure.
383 * The caller does the bitching.
384 * @param pSymDesc Pointer to the symbol descriptor.
385 * @param pCmdHlp Pointer to the command callback structure.
386 * @param enmType The result type.
387 * @param pResult Where to store the result.
388 */
389typedef DECLCALLBACK(int) FNDBGCSYMGET(PCDBGCSYM pSymDesc, PDBGCCMDHLP pCmdHlp, DBGCVARTYPE enmType, PDBGCVAR pResult);
390/** Pointer to get function for a builtin symbol. */
391typedef FNDBGCSYMGET *PFNDBGCSYMGET;
392
393/**
394 * Set builtin symbol.
395 *
396 * @returns 0 on success.
397 * @returns VBox evaluation / parsing error code on failure.
398 * The caller does the bitching.
399 * @param pSymDesc Pointer to the symbol descriptor.
400 * @param pCmdHlp Pointer to the command callback structure.
401 * @param pValue The value to assign the symbol.
402 */
403typedef DECLCALLBACK(int) FNDBGCSYMSET(PCDBGCSYM pSymDesc, PDBGCCMDHLP pCmdHlp, PCDBGCVAR pValue);
404/** Pointer to set function for a builtin symbol. */
405typedef FNDBGCSYMSET *PFNDBGCSYMSET;
406
407
408/**
409 * Symbol description (for builtin symbols).
410 */
411typedef struct DBGCSYM
412{
413 /** Symbol name. */
414 const char *pszName;
415 /** Get function. */
416 PFNDBGCSYMGET pfnGet;
417 /** Set function. (NULL if readonly) */
418 PFNDBGCSYMSET pfnSet;
419 /** User data. */
420 unsigned uUser;
421} DBGCSYM;
422
423
424/*******************************************************************************
425* Internal Functions *
426*******************************************************************************/
427static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
428static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
429static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
430static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
431static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
432static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
433static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
434static DECLCALLBACK(int) dbgcCmdHelp(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
435static DECLCALLBACK(int) dbgcCmdQuit(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
436static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
437static DECLCALLBACK(int) dbgcCmdStop(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
438static DECLCALLBACK(int) dbgcCmdDisasm(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
439static DECLCALLBACK(int) dbgcCmdSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
440static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
441static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
442static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
443static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
444static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
445static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
446static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
447static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
448static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
449static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
450static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
451static DECLCALLBACK(int) dbgcCmdInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
452static DECLCALLBACK(int) dbgcCmdLog(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
453static DECLCALLBACK(int) dbgcCmdLogDest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
454static DECLCALLBACK(int) dbgcCmdLogFlags(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
455static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
456static DECLCALLBACK(int) dbgcCmdFormat(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
457static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
458static DECLCALLBACK(int) dbgcCmdLoadSyms(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
459static DECLCALLBACK(int) dbgcCmdSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
460static DECLCALLBACK(int) dbgcCmdUnset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
461static DECLCALLBACK(int) dbgcCmdLoadVars(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
462static DECLCALLBACK(int) dbgcCmdShowVars(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
463static DECLCALLBACK(int) dbgcCmdHarakiri(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
464
465static DECLCALLBACK(int) dbgcOpMinus(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
466static DECLCALLBACK(int) dbgcOpPluss(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
467static DECLCALLBACK(int) dbgcOpBooleanNot(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
468static DECLCALLBACK(int) dbgcOpBitwiseNot(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
469static DECLCALLBACK(int) dbgcOpAddrFlat(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
470static DECLCALLBACK(int) dbgcOpAddrPhys(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
471static DECLCALLBACK(int) dbgcOpAddrHost(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
472static DECLCALLBACK(int) dbgcOpAddrHostPhys(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
473static DECLCALLBACK(int) dbgcOpVar(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult);
474
475static DECLCALLBACK(int) dbgcOpAddrFar(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
476static DECLCALLBACK(int) dbgcOpMult(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
477static DECLCALLBACK(int) dbgcOpDiv(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
478static DECLCALLBACK(int) dbgcOpMod(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
479static DECLCALLBACK(int) dbgcOpAdd(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
480static DECLCALLBACK(int) dbgcOpSub(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
481static DECLCALLBACK(int) dbgcOpBitwiseShiftLeft(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
482static DECLCALLBACK(int) dbgcOpBitwiseShiftRight(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
483static DECLCALLBACK(int) dbgcOpBitwiseAnd(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
484static DECLCALLBACK(int) dbgcOpBitwiseXor(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
485static DECLCALLBACK(int) dbgcOpBitwiseOr(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
486static DECLCALLBACK(int) dbgcOpBooleanAnd(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
487static DECLCALLBACK(int) dbgcOpBooleanOr(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
488static DECLCALLBACK(int) dbgcOpRangeLength(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
489static DECLCALLBACK(int) dbgcOpRangeLengthBytes(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
490static DECLCALLBACK(int) dbgcOpRangeTo(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult);
491
492static DECLCALLBACK(int) dbgcSymGetReg(PCDBGCSYM pSymDesc, PDBGCCMDHLP pCmdHlp, DBGCVARTYPE enmType, PDBGCVAR pResult);
493static DECLCALLBACK(int) dbgcSymSetReg(PCDBGCSYM pSymDesc, PDBGCCMDHLP pCmdHlp, PCDBGCVAR pValue);
494
495static void dbgcVarSetGCFlat(PDBGCVAR pVar, RTGCPTR GCFlat);
496static void dbgcVarSetGCFlatByteRange(PDBGCVAR pVar, RTGCPTR GCFlat, uint64_t cb);
497static void dbgcVarSetVar(PDBGCVAR pVar, PCDBGCVAR pVar2);
498static void dbgcVarSetNoRange(PDBGCVAR pVar);
499static void dbgcVarSetByteRange(PDBGCVAR pVar, uint64_t cb);
500static int dbgcVarToDbgfAddr(PDBGC pDbgc, PCDBGCVAR pVar, PDBGFADDRESS pAddress);
501
502static int dbgcBpAdd(PDBGC pDbgc, RTUINT iBp, const char *pszCmd);
503static int dbgcBpUpdate(PDBGC pDbgc, RTUINT iBp, const char *pszCmd);
504static int dbgcBpDelete(PDBGC pDbgc, RTUINT iBp);
505static PDBGCBP dbgcBpGet(PDBGC pDbgc, RTUINT iBp);
506static int dbgcBpExec(PDBGC pDbgc, RTUINT iBp);
507
508static PCDBGCSYM dbgcLookupRegisterSymbol(PDBGC pDbgc, const char *pszSymbol);
509static int dbgcSymbolGet(PDBGC pDbgc, const char *pszSymbol, DBGCVARTYPE enmType, PDBGCVAR pResult);
510static int dbgcEvalSub(PDBGC pDbgc, char *pszExpr, size_t cchExpr, PDBGCVAR pResult);
511static int dbgcProcessCommand(PDBGC pDbgc, char *pszCmd, size_t cchCmd);
512
513
514/*******************************************************************************
515* Global Variables *
516*******************************************************************************/
517/**
518 * Pointer to head of the list of external commands.
519 */
520static PDBGCEXTCMDS g_pExtCmdsHead; /** @todo rw protect g_pExtCmdsHead! */
521/** Locks the g_pExtCmdsHead list for reading. */
522#define DBGCEXTCMDS_LOCK_RD() do { } while (0)
523/** Locks the g_pExtCmdsHead list for writing. */
524#define DBGCEXTCMDS_LOCK_WR() do { } while (0)
525/** UnLocks the g_pExtCmdsHead list after reading. */
526#define DBGCEXTCMDS_UNLOCK_RD() do { } while (0)
527/** UnLocks the g_pExtCmdsHead list after writing. */
528#define DBGCEXTCMDS_UNLOCK_WR() do { } while (0)
529
530
531/** One argument of any kind. */
532static const DBGCVARDESC g_aArgAny[] =
533{
534 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
535 { 0, 1, DBGCVAR_CAT_ANY, 0, "var", "Any type of argument." },
536};
537
538/** Multiple string arguments (min 1). */
539static const DBGCVARDESC g_aArgMultiStr[] =
540{
541 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
542 { 1, ~0, DBGCVAR_CAT_STRING, 0, "strings", "One or more strings." },
543};
544
545/** Filename string. */
546static const DBGCVARDESC g_aArgFilename[] =
547{
548 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
549 { 1, 1, DBGCVAR_CAT_STRING, 0, "path", "Filename string." },
550};
551
552
553/** 'ba' arguments. */
554static const DBGCVARDESC g_aArgBrkAcc[] =
555{
556 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
557 { 1, 1, DBGCVAR_CAT_STRING, 0, "access", "The access type: x=execute, rw=read/write (alias r), w=write, i=not implemented." },
558 { 1, 1, DBGCVAR_CAT_NUMBER, 0, "size", "The access size: 1, 2, 4, or 8. 'x' access requires 1, and 8 requires amd64 long mode." },
559 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
560 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
561 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
562 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
563};
564
565
566/** 'bc', 'bd', 'be' arguments. */
567static const DBGCVARDESC g_aArgBrks[] =
568{
569 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
570 { 0, ~0, DBGCVAR_CAT_NUMBER, 0, "#bp", "Breakpoint number." },
571 { 0, 1, DBGCVAR_CAT_STRING, 0, "all", "All breakpoints." },
572};
573
574
575/** 'bp' arguments. */
576static const DBGCVARDESC g_aArgBrkSet[] =
577{
578 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
579 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
580 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
581 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
582 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
583};
584
585
586/** 'br' arguments. */
587static const DBGCVARDESC g_aArgBrkREM[] =
588{
589 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
590 { 1, 1, DBGCVAR_CAT_GC_POINTER, 0, "address", "The address." },
591 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "passes", "The number of passes before we trigger the breakpoint. (0 is default)" },
592 { 0, 1, DBGCVAR_CAT_NUMBER, DBGCVD_FLAGS_DEP_PREV, "max passes", "The number of passes after which we stop triggering the breakpoint. (~0 is default)" },
593 { 0, 1, DBGCVAR_CAT_STRING, 0, "cmds", "String of commands to be executed when the breakpoint is hit. Quote it!" },
594};
595
596
597/** 'd?' arguments. */
598static const DBGCVARDESC g_aArgDumpMem[] =
599{
600 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
601 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start dumping memory." },
602};
603
604
605/** 'dpd*' arguments. */
606static const DBGCVARDESC g_aArgDumpPD[] =
607{
608 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
609 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "index", "Index into the page directory." },
610 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from. Range is applied to the page directory." },
611};
612
613
614/** 'dpda' arguments. */
615static const DBGCVARDESC g_aArgDumpPDAddr[] =
616{
617 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
618 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page directory entry to start dumping from." },
619};
620
621
622/** 'dpt?' arguments. */
623static const DBGCVARDESC g_aArgDumpPT[] =
624{
625 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
626 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address which page directory entry to start dumping from." },
627};
628
629
630/** 'dpta' arguments. */
631static const DBGCVARDESC g_aArgDumpPTAddr[] =
632{
633 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
634 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address of the page table entry to start dumping from." },
635};
636
637
638/** 'help' arguments. */
639static const DBGCVARDESC g_aArgHelp[] =
640{
641 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
642 { 0, ~0, DBGCVAR_CAT_STRING, 0, "cmd/op", "Zero or more command or operator names." },
643};
644
645
646/** 'info' arguments. */
647static const DBGCVARDESC g_aArgInfo[] =
648{
649 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
650 { 1, 1, DBGCVAR_CAT_STRING, 0, "info", "The name of the info to display." },
651 { 0, 1, DBGCVAR_CAT_STRING, 0, "args", "String arguments to the handler." },
652};
653
654
655
656/** 'ln' arguments. */
657static const DBGCVARDESC g_aArgListNear[] =
658{
659 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
660 { 0, ~0, DBGCVAR_CAT_POINTER, 0, "address", "Address of the symbol to look up." },
661 { 0, ~0, DBGCVAR_CAT_SYMBOL, 0, "symbol", "Symbol to lookup." },
662};
663
664/** 'ln' return. */
665static const DBGCVARDESC g_RetListNear =
666{
667 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "The last resolved symbol/address with adjusted range."
668};
669
670
671/** loadsyms arguments. */
672static const DBGCVARDESC g_aArgLoadSyms[] =
673{
674 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
675 { 1, 1, DBGCVAR_CAT_STRING, 0, "path", "Filename string." },
676 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "delta", "Delta to add to the loaded symbols. (optional)" },
677 { 0, 1, DBGCVAR_CAT_STRING, 0, "module name", "Module name." },
678 { 0, 1, DBGCVAR_CAT_POINTER, DBGCVD_FLAGS_DEP_PREV, "module address", "Module address." },
679 { 0, 1, DBGCVAR_CAT_NUMBER, 0, "module size", "The module size. (optional)" },
680};
681
682
683/** log arguments. */
684static const DBGCVARDESC g_aArgLog[] =
685{
686 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
687 { 1, 1, DBGCVAR_CAT_STRING, 0, "groups", "Group modifier string (quote it!)." }
688};
689
690
691/** logdest arguments. */
692static const DBGCVARDESC g_aArgLogDest[] =
693{
694 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
695 { 1, 1, DBGCVAR_CAT_STRING, 0, "dests", "Destination modifier string (quote it!)." }
696};
697
698
699/** logflags arguments. */
700static const DBGCVARDESC g_aArgLogFlags[] =
701{
702 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
703 { 1, 1, DBGCVAR_CAT_STRING, 0, "flags", "Flag modifier string (quote it!)." }
704};
705
706
707/** 'm' argument. */
708static const DBGCVARDESC g_aArgMemoryInfo[] =
709{
710 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
711 { 1, 1, DBGCVAR_CAT_POINTER, 0, "address", "Pointer to obtain info about." },
712};
713
714
715/** 'r' arguments. */
716static const DBGCVARDESC g_aArgReg[] =
717{
718 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
719 { 0, 1, DBGCVAR_CAT_SYMBOL, 0, "register", "Register to show or set." },
720 { 0, 1, DBGCVAR_CAT_NUMBER_NO_RANGE, DBGCVD_FLAGS_DEP_PREV, "value", "New register value." },
721};
722
723
724/** 's' arguments. */
725static const DBGCVARDESC g_aArgSource[] =
726{
727 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
728 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start looking for source lines." },
729};
730
731
732/** 'set' arguments */
733static const DBGCVARDESC g_aArgSet[] =
734{
735 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
736 { 1, 1, DBGCVAR_CAT_STRING, 0, "var", "Variable name." },
737 { 1, 1, DBGCVAR_CAT_ANY, 0, "value", "Value to assign to the variable." },
738};
739
740
741/** 'u' arguments. */
742static const DBGCVARDESC g_aArgDisasm[] =
743{
744 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
745 { 0, 1, DBGCVAR_CAT_POINTER, 0, "address", "Address where to start disassembling." },
746};
747
748
749
750
751
752/** Command descriptors. */
753static const DBGCCMD g_aCmds[] =
754{
755 /* pszCmd, cArgsMin, cArgsMax, paArgDescs, cArgDescs, pResultDesc, fFlags, pfnHandler pszSyntax, ....pszDescription */
756 { "ba", 3, 6, &g_aArgBrkAcc[0], ELEMENTS(g_aArgBrkAcc), NULL, 0, dbgcCmdBrkAccess, "<access> <size> <address> [passes [max passes]] [cmds]",
757 "Sets a data access breakpoint." },
758 { "bc", 1, ~0, &g_aArgBrks[0], ELEMENTS(g_aArgBrks), NULL, 0, dbgcCmdBrkClear, "all | <bp#> [bp# []]", "Enabled a set of breakpoints." },
759 { "bd", 1, ~0, &g_aArgBrks[0], ELEMENTS(g_aArgBrks), NULL, 0, dbgcCmdBrkDisable, "all | <bp#> [bp# []]", "Disables a set of breakpoints." },
760 { "be", 1, ~0, &g_aArgBrks[0], ELEMENTS(g_aArgBrks), NULL, 0, dbgcCmdBrkEnable, "all | <bp#> [bp# []]", "Enabled a set of breakpoints." },
761 { "bl", 0, 0, NULL, 0, NULL, 0, dbgcCmdBrkList, "", "Lists all the breakpoints." },
762 { "bp", 1, 4, &g_aArgBrkSet[0], ELEMENTS(g_aArgBrkSet), NULL, 0, dbgcCmdBrkSet, "<address> [passes [max passes]] [cmds]",
763 "Sets a breakpoint (int 3)." },
764 { "br", 1, 4, &g_aArgBrkREM[0], ELEMENTS(g_aArgBrkREM), NULL, 0, dbgcCmdBrkREM, "<address> [passes [max passes]] [cmds]",
765 "Sets a recompiler specific breakpoint." },
766 { "bye", 0, 0, NULL, 0, NULL, 0, dbgcCmdQuit, "", "Exits the debugger." },
767 { "d", 0, 1, &g_aArgDumpMem[0], ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory using last element size." },
768 { "da", 0, 1, &g_aArgDumpMem[0], ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory as ascii string." },
769 { "db", 0, 1, &g_aArgDumpMem[0], ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in bytes." },
770 { "dd", 0, 1, &g_aArgDumpMem[0], ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in double words." },
771 { "dpd", 0, 1, &g_aArgDumpPD[0], ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the default context." },
772 { "dpda", 0, 1, &g_aArgDumpPDAddr[0],ELEMENTS(g_aArgDumpPDAddr),NULL, 0, dbgcCmdDumpPageDir, "[addr]", "Dumps specified page directory." },
773 { "dpdb", 1, 1, &g_aArgDumpPD[0], ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDirBoth, "[addr] [index]", "Dumps page directory entries of the guest and the hypervisor. " },
774 { "dpdg", 0, 1, &g_aArgDumpPD[0], ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the guest." },
775 { "dpdh", 0, 1, &g_aArgDumpPD[0], ELEMENTS(g_aArgDumpPD), NULL, 0, dbgcCmdDumpPageDir, "[addr] [index]", "Dumps page directory entries of the hypervisor. " },
776 { "dpt", 1, 1, &g_aArgDumpPT[0], ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the default context." },
777 { "dpta", 1, 1, &g_aArgDumpPTAddr[0],ELEMENTS(g_aArgDumpPTAddr), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps specified page table." },
778 { "dptb", 1, 1, &g_aArgDumpPT[0], ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTableBoth,"<addr>", "Dumps page table entries of the guest and the hypervisor." },
779 { "dptg", 1, 1, &g_aArgDumpPT[0], ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the guest." },
780 { "dpth", 1, 1, &g_aArgDumpPT[0], ELEMENTS(g_aArgDumpPT), NULL, 0, dbgcCmdDumpPageTable,"<addr>", "Dumps page table entries of the hypervisor." },
781 { "dq", 0, 1, &g_aArgDumpMem[0], ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in quad words." },
782 { "dw", 0, 1, &g_aArgDumpMem[0], ELEMENTS(g_aArgDumpMem), NULL, 0, dbgcCmdDumpMem, "[addr]", "Dump memory in words." },
783 { "exit", 0, 0, NULL, 0, NULL, 0, dbgcCmdQuit, "", "Exits the debugger." },
784 { "format", 1, 1, &g_aArgAny[0], ELEMENTS(g_aArgAny), NULL, 0, dbgcCmdFormat, "", "Evaluates an expression and formats it." },
785 { "g", 0, 0, NULL, 0, NULL, 0, dbgcCmdGo, "", "Continue execution." },
786 { "harakiri", 0, 0, NULL, 0, NULL, 0, dbgcCmdHarakiri, "", "Kills debugger process." },
787 { "help", 0, ~0, &g_aArgHelp[0], ELEMENTS(g_aArgHelp), NULL, 0, dbgcCmdHelp, "[cmd/op [..]]", "Display help. For help about info items try 'help info'." },
788 { "info", 1, 2, &g_aArgInfo[0], ELEMENTS(g_aArgInfo), NULL, 0, dbgcCmdInfo, "<info> [args]", "Display info register in the DBGF." },
789 { "k", 0, 0, NULL, 0, NULL, 0, dbgcCmdStack, "", "Callstack." },
790 { "kg", 0, 0, NULL, 0, NULL, 0, dbgcCmdStack, "", "Callstack - guest." },
791 { "kh", 0, 0, NULL, 0, NULL, 0, dbgcCmdStack, "", "Callstack - hypervisor." },
792 { "ln", 0, ~0, &g_aArgListNear[0], ELEMENTS(g_aArgListNear), &g_RetListNear, 0, dbgcCmdListNear, "[addr/sym [..]]", "List symbols near to the address. Default address is CS:EIP." },
793 { "loadsyms", 1, 5, &g_aArgLoadSyms[0], ELEMENTS(g_aArgLoadSyms), NULL, 0, dbgcCmdLoadSyms, "<filename> [delta] [module] [module address]", "Loads symbols from a text file. Optionally giving a delta and a module." },
794 { "loadvars", 1, 1, &g_aArgFilename[0], ELEMENTS(g_aArgFilename), NULL, 0, dbgcCmdLoadVars, "<filename>", "Load variables from file. One per line, same as the args to the set command." },
795 { "log", 1, 1, &g_aArgLog[0], ELEMENTS(g_aArgLog), NULL, 0, dbgcCmdLog, "<group string>", "Modifies the logging group settings (VBOX_LOG)" },
796 { "logdest", 1, 1, &g_aArgLogDest[0], ELEMENTS(g_aArgLogDest), NULL, 0, dbgcCmdLogDest, "<dest string>", "Modifies the logging destination (VBOX_LOG_DEST)." },
797 { "logflags", 1, 1, &g_aArgLogFlags[0], ELEMENTS(g_aArgLogFlags), NULL, 0, dbgcCmdLogFlags, "<flags string>", "Modifies the logging flags (VBOX_LOG_FLAGS)." },
798 { "m", 1, 1, &g_aArgMemoryInfo[0],ELEMENTS(g_aArgMemoryInfo),NULL, 0, dbgcCmdMemoryInfo, "<addr>", "Display information about that piece of memory." },
799 { "quit", 0, 0, NULL, 0, NULL, 0, dbgcCmdQuit, "", "Exits the debugger." },
800 { "r", 0, 2, &g_aArgReg[0], ELEMENTS(g_aArgReg), NULL, 0, dbgcCmdReg, "[reg [newval]]", "Show or set register(s) - active reg set." },
801 { "rg", 0, 2, &g_aArgReg[0], ELEMENTS(g_aArgReg), NULL, 0, dbgcCmdRegGuest, "[reg [newval]]", "Show or set register(s) - guest reg set." },
802 { "rh", 0, 2, &g_aArgReg[0], ELEMENTS(g_aArgReg), NULL, 0, dbgcCmdRegHyper, "[reg [newval]]", "Show or set register(s) - hypervisor reg set." },
803 { "rt", 0, 0, NULL, 0, NULL, 0, dbgcCmdRegTerse, "", "Toggles terse / verbose register info." },
804 { "s", 0, 1, &g_aArgSource[0], ELEMENTS(g_aArgSource), NULL, 0, dbgcCmdSource, "[addr]", "Source." },
805 { "set", 2, 2, &g_aArgSet[0], ELEMENTS(g_aArgSet), NULL, 0, dbgcCmdSet, "<var> <value>", "Sets a global variable." },
806 { "showvars", 0, 0, NULL, 0, NULL, 0, dbgcCmdShowVars, "", "List all the defined variables." },
807 { "stop", 0, 0, NULL, 0, NULL, 0, dbgcCmdStop, "", "Stop execution." },
808 { "t", 0, 0, NULL, 0, NULL, 0, dbgcCmdTrace, "", "Instruction trace (step into)." },
809 { "u", 0, 1, &g_aArgDisasm[0], ELEMENTS(g_aArgDisasm), NULL, 0, dbgcCmdDisasm, "[addr]", "Disassemble." },
810 { "unset", 1, ~0, &g_aArgMultiStr[0], ELEMENTS(g_aArgMultiStr), NULL, 0, dbgcCmdUnset, "<var1> [var1..[varN]]", "Unsets (delete) one or more global variables." },
811};
812
813
814/** Operators. */
815static const DBGCOP g_aOps[] =
816{
817 /* szName is initialized as a 4 char array because of M$C elsewise optimizing it away in /Ox mode (the 'const char' vs 'char' problem). */
818 /* szName, cchName, fBinary, iPrecedence, pfnHandlerUnary, pfnHandlerBitwise */
819 { {'-'}, 1, false, 1, dbgcOpMinus, NULL, "Unary minus." },
820 { {'+'}, 1, false, 1, dbgcOpPluss, NULL, "Unary pluss." },
821 { {'!'}, 1, false, 1, dbgcOpBooleanNot, NULL, "Boolean not." },
822 { {'~'}, 1, false, 1, dbgcOpBitwiseNot, NULL, "Bitwise complement." },
823 { {'%'}, 1, false, 1, dbgcOpAddrFlat, NULL, "Flat address." },
824 { {'%','%'}, 2, false, 1, dbgcOpAddrPhys, NULL, "Physical address." },
825 { {'#'}, 1, false, 1, dbgcOpAddrHost, NULL, "Flat host address." },
826 { {'#','%','%'}, 3, false, 1, dbgcOpAddrHostPhys, NULL, "Physical host address." },
827 { {'$'}, 1, false, 1, dbgcOpVar, NULL, "Reference a variable." },
828 { {':'}, 1, true, 9, NULL, dbgcOpAddrFar, "Far pointer." },
829 { {'*'}, 1, true, 10, NULL, dbgcOpMult, "Multiplication." },
830 { {'/'}, 1, true, 11, NULL, dbgcOpDiv, "Division." },
831 { {'%'}, 1, true, 12, NULL, dbgcOpMod, "Modulus." },
832 { {'+'}, 1, true, 13, NULL, dbgcOpAdd, "Addition." },
833 { {'-'}, 1, true, 14, NULL, dbgcOpSub, "Subtraction." },
834 { {'<','<'}, 2, true, 15, NULL, dbgcOpBitwiseShiftLeft, "Bitwise left shift." },
835 { {'>','>'}, 2, true, 16, NULL, dbgcOpBitwiseShiftRight, "Bitwise right shift." },
836 { {'&'}, 1, true, 17, NULL, dbgcOpBitwiseAnd, "Bitwise and." },
837 { {'^'}, 1, true, 18, NULL, dbgcOpBitwiseXor, "Bitwise exclusiv or." },
838 { {'|'}, 1, true, 19, NULL, dbgcOpBitwiseOr, "Bitwise inclusive or." },
839 { {'&','&'}, 2, true, 20, NULL, dbgcOpBooleanAnd, "Boolean and." },
840 { {'|','|'}, 2, true, 21, NULL, dbgcOpBooleanOr, "Boolean or." },
841 { {'L'}, 1, true, 22, NULL, dbgcOpRangeLength, "Range elements." },
842 { {'L','B'}, 2, true, 23, NULL, dbgcOpRangeLengthBytes, "Range bytes." },
843 { {'T'}, 1, true, 24, NULL, dbgcOpRangeTo, "Range to." }
844};
845
846/** Register symbol uUser value.
847 * @{
848 */
849/** If set the register set is the hypervisor and not the guest one. */
850#define SYMREG_FLAGS_HYPER BIT(20)
851/** If set a far conversion of the value will use the high 16 bit for the selector.
852 * If clear the low 16 bit will be used. */
853#define SYMREG_FLAGS_HIGH_SEL BIT(21)
854/** The shift value to calc the size of a register symbol from the uUser value. */
855#define SYMREG_SIZE_SHIFT (24)
856/** Get the offset */
857#define SYMREG_OFFSET(uUser) (uUser & ((1 << 20) - 1))
858/** Get the size. */
859#define SYMREG_SIZE(uUser) ((uUser >> SYMREG_SIZE_SHIFT) & 0xff)
860/** 1 byte. */
861#define SYMREG_SIZE_1 ( 1 << SYMREG_SIZE_SHIFT)
862/** 2 byte. */
863#define SYMREG_SIZE_2 ( 2 << SYMREG_SIZE_SHIFT)
864/** 4 byte. */
865#define SYMREG_SIZE_4 ( 4 << SYMREG_SIZE_SHIFT)
866/** 6 byte. */
867#define SYMREG_SIZE_6 ( 6 << SYMREG_SIZE_SHIFT)
868/** 8 byte. */
869#define SYMREG_SIZE_8 ( 8 << SYMREG_SIZE_SHIFT)
870/** 12 byte. */
871#define SYMREG_SIZE_12 (12 << SYMREG_SIZE_SHIFT)
872/** 16 byte. */
873#define SYMREG_SIZE_16 (16 << SYMREG_SIZE_SHIFT)
874/** @} */
875
876/** Builtin Symbols.
877 * ASSUMES little endian register representation!
878 */
879static const DBGCSYM g_aSyms[] =
880{
881 { "eax", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) | SYMREG_SIZE_4 },
882 { "ax", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) | SYMREG_SIZE_2 },
883 { "al", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) | SYMREG_SIZE_1 },
884 { "ah", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) + 1 | SYMREG_SIZE_1 },
885
886 { "ebx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) | SYMREG_SIZE_4 },
887 { "bx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) | SYMREG_SIZE_2 },
888 { "bl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) | SYMREG_SIZE_1 },
889 { "bh", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) + 1 | SYMREG_SIZE_1 },
890
891 { "ecx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) | SYMREG_SIZE_4 },
892 { "cx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) | SYMREG_SIZE_2 },
893 { "cl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) | SYMREG_SIZE_1 },
894 { "ch", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) + 1 | SYMREG_SIZE_1 },
895
896 { "edx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) | SYMREG_SIZE_4 },
897 { "dx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) | SYMREG_SIZE_2 },
898 { "dl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) | SYMREG_SIZE_1 },
899 { "dh", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) + 1 | SYMREG_SIZE_1 },
900
901 { "edi", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edi) | SYMREG_SIZE_4 },
902 { "di", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edi) | SYMREG_SIZE_2 },
903
904 { "esi", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esi) | SYMREG_SIZE_4 },
905 { "si", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esi) | SYMREG_SIZE_2 },
906
907 { "ebp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebp) | SYMREG_SIZE_4 },
908 { "bp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebp) | SYMREG_SIZE_2 },
909
910 { "esp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esp) | SYMREG_SIZE_4 },
911 { "sp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esp) | SYMREG_SIZE_2 },
912
913 { "eip", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eip) | SYMREG_SIZE_4 },
914 { "ip", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eip) | SYMREG_SIZE_2 },
915
916 { "efl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_4 },
917 { "eflags", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_4 },
918 { "fl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_2 },
919 { "flags", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_2 },
920
921 { "cs", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cs) | SYMREG_SIZE_2 },
922 { "ds", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ds) | SYMREG_SIZE_2 },
923 { "es", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, es) | SYMREG_SIZE_2 },
924 { "fs", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, fs) | SYMREG_SIZE_2 },
925 { "gs", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gs) | SYMREG_SIZE_2 },
926 { "ss", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ss) | SYMREG_SIZE_2 },
927
928 { "cr0", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr0) | SYMREG_SIZE_4 },
929 { "cr2", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr2) | SYMREG_SIZE_4 },
930 { "cr3", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr3) | SYMREG_SIZE_4 },
931 { "cr4", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr4) | SYMREG_SIZE_4 },
932
933 { "tr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, tr) | SYMREG_SIZE_2 },
934 { "ldtr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ldtr) | SYMREG_SIZE_2 },
935
936 { "gdtr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gdtr) | SYMREG_SIZE_6 },
937 { "gdtr.limit", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gdtr.cbGdt)| SYMREG_SIZE_2 },
938 { "gdtr.base", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gdtr.pGdt)| SYMREG_SIZE_4 },
939
940 { "idtr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, idtr) | SYMREG_SIZE_6 },
941 { "idtr.limit", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, idtr.cbIdt)| SYMREG_SIZE_2 },
942 { "idtr.base", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, idtr.pIdt)| SYMREG_SIZE_4 },
943
944 /* hypervisor */
945
946 {".eax", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
947 {".ax", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
948 {".al", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
949 {".ah", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eax) + 1 | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
950
951 {".ebx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
952 {".bx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
953 {".bl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
954 {".bh", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebx) + 1 | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
955
956 {".ecx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
957 {".cx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
958 {".cl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
959 {".ch", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ecx) + 1 | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
960
961 {".edx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
962 {".dx", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
963 {".dl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
964 {".dh", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edx) + 1 | SYMREG_SIZE_1 | SYMREG_FLAGS_HYPER },
965
966 {".edi", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edi) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
967 {".di", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, edi) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
968
969 {".esi", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esi) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
970 {".si", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esi) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
971
972 {".ebp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebp) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
973 {".bp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ebp) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
974
975 {".esp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esp) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
976 {".sp", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, esp) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
977
978 {".eip", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eip) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
979 {".ip", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eip) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
980
981 {".efl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
982 {".eflags", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
983 {".fl", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
984 {".flags", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, eflags) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
985
986 {".cs", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cs) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
987 {".ds", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ds) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
988 {".es", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, es) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
989 {".fs", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, fs) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
990 {".gs", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gs) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
991 {".ss", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ss) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
992
993 {".cr0", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr0) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
994 {".cr2", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr2) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
995 {".cr3", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr3) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
996 {".cr4", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, cr4) | SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
997
998 {".tr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, tr) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
999 {".ldtr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, ldtr) | SYMREG_SIZE_2 | SYMREG_FLAGS_HYPER },
1000
1001 {".gdtr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gdtr) | SYMREG_SIZE_6 | SYMREG_FLAGS_HYPER },
1002 {".gdtr.limit", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gdtr.cbGdt)| SYMREG_SIZE_2| SYMREG_FLAGS_HYPER },
1003 {".gdtr.base", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, gdtr.pGdt)| SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
1004
1005 {".idtr", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, idtr) | SYMREG_SIZE_6 | SYMREG_FLAGS_HYPER },
1006 {".idtr.limit", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, idtr.cbIdt)| SYMREG_SIZE_2| SYMREG_FLAGS_HYPER },
1007 {".idtr.base", dbgcSymGetReg, dbgcSymSetReg, offsetof(CPUMCTX, idtr.pIdt)| SYMREG_SIZE_4 | SYMREG_FLAGS_HYPER },
1008
1009};
1010
1011
1012/**
1013 * Prints full command help.
1014 */
1015static int dbgcPrintHelp(PDBGCCMDHLP pCmdHlp, PCDBGCCMD pCmd, bool fExternal)
1016{
1017 int rc;
1018
1019 /* the command */
1020 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1021 "%s%-*s %-30s %s",
1022 fExternal ? "." : "",
1023 fExternal ? 10 : 11,
1024 pCmd->pszCmd,
1025 pCmd->pszSyntax,
1026 pCmd->pszDescription);
1027 if (!pCmd->cArgsMin && pCmd->cArgsMin == pCmd->cArgsMax)
1028 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <no args>\n");
1029 else if (pCmd->cArgsMin == pCmd->cArgsMax)
1030 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <%u args>\n", pCmd->cArgsMin);
1031 else if (pCmd->cArgsMax == ~0U)
1032 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <%u+ args>\n", pCmd->cArgsMin);
1033 else
1034 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <%u to %u args>\n", pCmd->cArgsMin, pCmd->cArgsMax);
1035
1036 /* argument descriptions. */
1037 for (unsigned i = 0; i < pCmd->cArgDescs; i++)
1038 {
1039 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1040 " %-12s %s",
1041 pCmd->paArgDescs[i].pszName,
1042 pCmd->paArgDescs[i].pszDescription);
1043 if (!pCmd->paArgDescs[i].cTimesMin)
1044 {
1045 if (pCmd->paArgDescs[i].cTimesMax == ~0U)
1046 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <optional+>\n");
1047 else
1048 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <optional-%u>\n", pCmd->paArgDescs[i].cTimesMax);
1049 }
1050 else
1051 {
1052 if (pCmd->paArgDescs[i].cTimesMax == ~0U)
1053 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <%u+>\n", pCmd->paArgDescs[i].cTimesMin);
1054 else
1055 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " <%u-%u>\n", pCmd->paArgDescs[i].cTimesMin, pCmd->paArgDescs[i].cTimesMax);
1056 }
1057 }
1058 return rc;
1059}
1060
1061/**
1062 * The 'help' command.
1063 *
1064 * @returns VBox status.
1065 * @param pCmd Pointer to the command descriptor (as registered).
1066 * @param pCmdHlp Pointer to command helper functions.
1067 * @param pVM Pointer to the current VM (if any).
1068 * @param paArgs Pointer to (readonly) array of arguments.
1069 * @param cArgs Number of arguments in the array.
1070 */
1071static DECLCALLBACK(int) dbgcCmdHelp(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1072{
1073 int rc = VINF_SUCCESS;
1074 unsigned i;
1075 if (!cArgs)
1076 {
1077 /*
1078 * All the stuff.
1079 */
1080 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1081 "VirtualBox Debugger\n"
1082 "-------------------\n"
1083 "\n"
1084 "Commands and Functions:\n");
1085 for (i = 0; i < ELEMENTS(g_aCmds); i++)
1086 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1087 "%-11s %-30s %s\n",
1088 g_aCmds[i].pszCmd,
1089 g_aCmds[i].pszSyntax,
1090 g_aCmds[i].pszDescription);
1091
1092 if (g_pExtCmdsHead)
1093 {
1094 DBGCEXTCMDS_LOCK_RD();
1095 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1096 "\n"
1097 "External Commands and Functions:\n");
1098 for (PDBGCEXTCMDS pExtCmd = g_pExtCmdsHead; pExtCmd; pExtCmd = pExtCmd->pNext)
1099 for (i = 0; i < pExtCmd->cCmds; i++)
1100 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1101 ".%-10s %-30s %s\n",
1102 pExtCmd->paCmds[i].pszCmd,
1103 pExtCmd->paCmds[i].pszSyntax,
1104 pExtCmd->paCmds[i].pszDescription);
1105 DBGCEXTCMDS_UNLOCK_RD();
1106 }
1107
1108 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1109 "\n"
1110 "Operators:\n");
1111 unsigned iPrecedence = 0;
1112 unsigned cLeft = ELEMENTS(g_aOps);
1113 while (cLeft > 0)
1114 {
1115 for (i = 0; i < ELEMENTS(g_aOps); i++)
1116 if (g_aOps[i].iPrecedence == iPrecedence)
1117 {
1118 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1119 "%-10s %s %s\n",
1120 g_aOps[i].szName,
1121 g_aOps[i].fBinary ? "Binary" : "Unary ",
1122 g_aOps[i].pszDescription);
1123 cLeft--;
1124 }
1125 iPrecedence++;
1126 }
1127 }
1128 else
1129 {
1130 /*
1131 * Search for the arguments (strings).
1132 */
1133 for (unsigned iArg = 0; iArg < cArgs; iArg++)
1134 {
1135 Assert(paArgs[iArg].enmType == DBGCVAR_TYPE_STRING);
1136
1137 /* lookup in the command list */
1138 bool fFound = false;
1139 for (i = 0; i < ELEMENTS(g_aCmds); i++)
1140 if (!strcmp(g_aCmds[i].pszCmd, paArgs[iArg].u.pszString))
1141 {
1142 rc = dbgcPrintHelp(pCmdHlp, &g_aCmds[i], false);
1143 fFound = true;
1144 break;
1145 }
1146
1147 /* external commands */
1148 if ( !fFound
1149 && g_pExtCmdsHead
1150 && paArgs[iArg].u.pszString[0] == '.')
1151 {
1152 DBGCEXTCMDS_LOCK_RD();
1153 for (PDBGCEXTCMDS pExtCmd = g_pExtCmdsHead; pExtCmd; pExtCmd = pExtCmd->pNext)
1154 for (i = 0; i < pExtCmd->cCmds; i++)
1155 if (!strcmp(pExtCmd->paCmds[i].pszCmd, paArgs[iArg].u.pszString + 1))
1156 {
1157 rc = dbgcPrintHelp(pCmdHlp, &g_aCmds[i], true);
1158 fFound = true;
1159 break;
1160 }
1161 DBGCEXTCMDS_UNLOCK_RD();
1162 }
1163
1164 /* operators */
1165 if (!fFound && strlen(paArgs[iArg].u.pszString) < sizeof(g_aOps[i].szName))
1166 {
1167 for (i = 0; i < ELEMENTS(g_aOps); i++)
1168 if (!strcmp(g_aOps[i].szName, paArgs[iArg].u.pszString))
1169 {
1170 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1171 "%-10s %s %s\n",
1172 g_aOps[i].szName,
1173 g_aOps[i].fBinary ? "Binary" : "Unary ",
1174 g_aOps[i].pszDescription);
1175 fFound = true;
1176 break;
1177 }
1178 }
1179
1180 /* found? */
1181 if (!fFound)
1182 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
1183 "error: '%s' was not found!\n",
1184 paArgs[iArg].u.pszString);
1185 } /* foreach argument */
1186 }
1187
1188 NOREF(pCmd);
1189 NOREF(pVM);
1190 NOREF(pResult);
1191 return rc;
1192}
1193
1194
1195/**
1196 * The 'quit', 'exit' and 'bye' commands.
1197 *
1198 * @returns VBox status.
1199 * @param pCmd Pointer to the command descriptor (as registered).
1200 * @param pCmdHlp Pointer to command helper functions.
1201 * @param pVM Pointer to the current VM (if any).
1202 * @param paArgs Pointer to (readonly) array of arguments.
1203 * @param cArgs Number of arguments in the array.
1204 */
1205static DECLCALLBACK(int) dbgcCmdQuit(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1206{
1207 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Quitting console...\n");
1208 NOREF(pCmd);
1209 NOREF(pVM);
1210 NOREF(paArgs);
1211 NOREF(cArgs);
1212 NOREF(pResult);
1213 return VERR_DBGC_QUIT;
1214}
1215
1216
1217/**
1218 * The 'go' command.
1219 *
1220 * @returns VBox status.
1221 * @param pCmd Pointer to the command descriptor (as registered).
1222 * @param pCmdHlp Pointer to command helper functions.
1223 * @param pVM Pointer to the current VM (if any).
1224 * @param paArgs Pointer to (readonly) array of arguments.
1225 * @param cArgs Number of arguments in the array.
1226 */
1227static DECLCALLBACK(int) dbgcCmdGo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1228{
1229 /*
1230 * Check if the VM is halted or not before trying to resume it.
1231 */
1232 if (!DBGFR3IsHalted(pVM))
1233 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "warning: The VM is already running...\n");
1234 else
1235 {
1236 int rc = DBGFR3Resume(pVM);
1237 if (VBOX_FAILURE(rc))
1238 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Executing DBGFR3Resume().");
1239 }
1240
1241 NOREF(pCmd);
1242 NOREF(paArgs);
1243 NOREF(cArgs);
1244 NOREF(pResult);
1245 return 0;
1246}
1247
1248/**
1249 * The 'stop' command.
1250 *
1251 * @returns VBox status.
1252 * @param pCmd Pointer to the command descriptor (as registered).
1253 * @param pCmdHlp Pointer to command helper functions.
1254 * @param pVM Pointer to the current VM (if any).
1255 * @param paArgs Pointer to (readonly) array of arguments.
1256 * @param cArgs Number of arguments in the array.
1257 */
1258static DECLCALLBACK(int) dbgcCmdStop(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1259{
1260 /*
1261 * Check if the VM is halted or not before trying to halt it.
1262 */
1263 int rc;
1264 if (DBGFR3IsHalted(pVM))
1265 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "warning: The VM is already halted...\n");
1266 else
1267 {
1268 rc = DBGFR3Halt(pVM);
1269 if (VBOX_SUCCESS(rc))
1270 rc = VWRN_DBGC_CMD_PENDING;
1271 else
1272 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Executing DBGFR3Halt().");
1273 }
1274
1275 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
1276 return rc;
1277}
1278
1279
1280/**
1281 * The 'ba' command.
1282 *
1283 * @returns VBox status.
1284 * @param pCmd Pointer to the command descriptor (as registered).
1285 * @param pCmdHlp Pointer to command helper functions.
1286 * @param pVM Pointer to the current VM (if any).
1287 * @param paArgs Pointer to (readonly) array of arguments.
1288 * @param cArgs Number of arguments in the array.
1289 */
1290static DECLCALLBACK(int) dbgcCmdBrkAccess(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
1291{
1292 /*
1293 * Interpret access type.
1294 */
1295 if ( !strchr("xrwi", paArgs[0].u.pszString[0])
1296 || paArgs[0].u.pszString[1])
1297 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid access type '%s' for '%s'. Valid types are 'e', 'r', 'w' and 'i'.\n",
1298 paArgs[0].u.pszString, pCmd->pszCmd);
1299 uint8_t fType = 0;
1300 switch (paArgs[0].u.pszString[0])
1301 {
1302 case 'x': fType = X86_DR7_RW_EO; break;
1303 case 'r': fType = X86_DR7_RW_RW; break;
1304 case 'w': fType = X86_DR7_RW_WO; break;
1305 case 'i': fType = X86_DR7_RW_IO; break;
1306 }
1307
1308 /*
1309 * Validate size.
1310 */
1311 if (fType == X86_DR7_RW_EO && paArgs[1].u.u64Number != 1)
1312 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid access size %RX64 for '%s'. 'x' access type requires size 1!\n",
1313 paArgs[1].u.u64Number, pCmd->pszCmd);
1314 switch (paArgs[1].u.u64Number)
1315 {
1316 case 1:
1317 case 2:
1318 case 4:
1319 break;
1320 /*case 8: - later*/
1321 default:
1322 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid access size %RX64 for '%s'. 1, 2 or 4!\n",
1323 paArgs[1].u.u64Number, pCmd->pszCmd);
1324 }
1325 uint8_t cb = (uint8_t)paArgs[1].u.u64Number;
1326
1327 /*
1328 * Convert the pointer to a DBGF address.
1329 */
1330 DBGFADDRESS Address;
1331 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[2], &Address);
1332 if (VBOX_FAILURE(rc))
1333 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Couldn't convert '%DV' to a DBGF address, rc=%Vrc.\n", &paArgs[2], rc);
1334
1335 /*
1336 * Pick out the optional arguments.
1337 */
1338 uint64_t iHitTrigger = 0;
1339 uint64_t iHitDisable = ~0;
1340 const char *pszCmds = NULL;
1341 unsigned iArg = 3;
1342 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1343 {
1344 iHitTrigger = paArgs[iArg].u.u64Number;
1345 iArg++;
1346 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1347 {
1348 iHitDisable = paArgs[iArg].u.u64Number;
1349 iArg++;
1350 }
1351 }
1352 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1353 {
1354 pszCmds = paArgs[iArg].u.pszString;
1355 iArg++;
1356 }
1357
1358 /*
1359 * Try set the breakpoint.
1360 */
1361 RTUINT iBp;
1362 rc = DBGFR3BpSetReg(pVM, &Address, iHitTrigger, iHitDisable, fType, cb, &iBp);
1363 if (VBOX_SUCCESS(rc))
1364 {
1365 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1366 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1367 if (VBOX_SUCCESS(rc))
1368 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Set access breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
1369 if (rc == VERR_DBGC_BP_EXISTS)
1370 {
1371 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1372 if (VBOX_SUCCESS(rc))
1373 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Updated access breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
1374 }
1375 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
1376 AssertRC(rc2);
1377 }
1378 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Failed to set access breakpoint at %VGv, rc=%Vrc.\n", Address.FlatPtr, rc);
1379}
1380
1381
1382/**
1383 * The 'bc' command.
1384 *
1385 * @returns VBox status.
1386 * @param pCmd Pointer to the command descriptor (as registered).
1387 * @param pCmdHlp Pointer to command helper functions.
1388 * @param pVM Pointer to the current VM (if any).
1389 * @param paArgs Pointer to (readonly) array of arguments.
1390 * @param cArgs Number of arguments in the array.
1391 */
1392static DECLCALLBACK(int) dbgcCmdBrkClear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
1393{
1394 /*
1395 * Enumerate the arguments.
1396 */
1397 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1398 int rc = VINF_SUCCESS;
1399 for (unsigned iArg = 0; iArg < cArgs && VBOX_SUCCESS(rc); iArg++)
1400 {
1401 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
1402 {
1403 /* one */
1404 RTUINT iBp = (RTUINT)paArgs[iArg].u.u64Number;
1405 if (iBp != paArgs[iArg].u.u64Number)
1406 {
1407 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Breakpoint id %RX64 is too large!\n", paArgs[iArg].u.u64Number);
1408 break;
1409 }
1410 int rc2 = DBGFR3BpClear(pVM, iBp);
1411 if (VBOX_FAILURE(rc2))
1412 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc2, "DBGFR3BpClear failed for breakpoint %u!\n", iBp);
1413 if (VBOX_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
1414 dbgcBpDelete(pDbgc, iBp);
1415 }
1416 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
1417 {
1418 /* all */
1419 PDBGCBP pBp = pDbgc->pFirstBp;
1420 while (pBp)
1421 {
1422 RTUINT iBp = pBp->iBp;
1423 pBp = pBp->pNext;
1424
1425 int rc2 = DBGFR3BpClear(pVM, iBp);
1426 if (VBOX_FAILURE(rc2))
1427 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc2, "DBGFR3BpClear failed for breakpoint %u!\n", iBp);
1428 if (VBOX_SUCCESS(rc2) || rc2 == VERR_DBGF_BP_NOT_FOUND)
1429 dbgcBpDelete(pDbgc, iBp);
1430 }
1431 }
1432 else
1433 {
1434 /* invalid parameter */
1435 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid argument '%s' to '%s'!\n", paArgs[iArg].u.pszString, pCmd->pszCmd);
1436 break;
1437 }
1438 }
1439 return rc;
1440}
1441
1442
1443/**
1444 * The 'bd' command.
1445 *
1446 * @returns VBox status.
1447 * @param pCmd Pointer to the command descriptor (as registered).
1448 * @param pCmdHlp Pointer to command helper functions.
1449 * @param pVM Pointer to the current VM (if any).
1450 * @param paArgs Pointer to (readonly) array of arguments.
1451 * @param cArgs Number of arguments in the array.
1452 */
1453static DECLCALLBACK(int) dbgcCmdBrkDisable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
1454{
1455 /*
1456 * Enumerate the arguments.
1457 */
1458 int rc = VINF_SUCCESS;
1459 for (unsigned iArg = 0; iArg < cArgs && VBOX_SUCCESS(rc); iArg++)
1460 {
1461 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
1462 {
1463 /* one */
1464 RTUINT iBp = (RTUINT)paArgs[iArg].u.u64Number;
1465 if (iBp != paArgs[iArg].u.u64Number)
1466 {
1467 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Breakpoint id %RX64 is too large!\n", paArgs[iArg].u.u64Number);
1468 break;
1469 }
1470 rc = DBGFR3BpDisable(pVM, iBp);
1471 if (VBOX_FAILURE(rc))
1472 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpDisable failed for breakpoint %u!\n", iBp);
1473 }
1474 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
1475 {
1476 /* all */
1477 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1478 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
1479 {
1480 rc = DBGFR3BpDisable(pVM, pBp->iBp);
1481 if (VBOX_FAILURE(rc))
1482 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpDisable failed for breakpoint %u!\n", pBp->iBp);
1483 }
1484 }
1485 else
1486 {
1487 /* invalid parameter */
1488 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid argument '%s' to '%s'!\n", paArgs[iArg].u.pszString, pCmd->pszCmd);
1489 break;
1490 }
1491 }
1492 return rc;
1493}
1494
1495
1496/**
1497 * The 'be' command.
1498 *
1499 * @returns VBox status.
1500 * @param pCmd Pointer to the command descriptor (as registered).
1501 * @param pCmdHlp Pointer to command helper functions.
1502 * @param pVM Pointer to the current VM (if any).
1503 * @param paArgs Pointer to (readonly) array of arguments.
1504 * @param cArgs Number of arguments in the array.
1505 */
1506static DECLCALLBACK(int) dbgcCmdBrkEnable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
1507{
1508 /*
1509 * Enumerate the arguments.
1510 */
1511 int rc = VINF_SUCCESS;
1512 for (unsigned iArg = 0; iArg < cArgs && VBOX_SUCCESS(rc); iArg++)
1513 {
1514 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
1515 {
1516 /* one */
1517 RTUINT iBp = (RTUINT)paArgs[iArg].u.u64Number;
1518 if (iBp != paArgs[iArg].u.u64Number)
1519 {
1520 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Breakpoint id %RX64 is too large!\n", paArgs[iArg].u.u64Number);
1521 break;
1522 }
1523 rc = DBGFR3BpEnable(pVM, iBp);
1524 if (VBOX_FAILURE(rc))
1525 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpEnable failed for breakpoint %u!\n", iBp);
1526 }
1527 else if (!strcmp(paArgs[iArg].u.pszString, "all"))
1528 {
1529 /* all */
1530 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1531 for (PDBGCBP pBp = pDbgc->pFirstBp; pBp; pBp = pBp->pNext)
1532 {
1533 rc = DBGFR3BpEnable(pVM, pBp->iBp);
1534 if (VBOX_FAILURE(rc))
1535 rc = pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpEnable failed for breakpoint %u!\n", pBp->iBp);
1536 }
1537 }
1538 else
1539 {
1540 /* invalid parameter */
1541 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid argument '%s' to '%s'!\n", paArgs[iArg].u.pszString, pCmd->pszCmd);
1542 break;
1543 }
1544 }
1545 return rc;
1546}
1547
1548
1549/**
1550 * Breakpoint enumeration callback function.
1551 *
1552 * @returns VBox status code. Any failure will stop the enumeration.
1553 * @param pVM The VM handle.
1554 * @param pvUser The user argument.
1555 * @param pBp Pointer to the breakpoint information. (readonly)
1556 */
1557static DECLCALLBACK(int) dbgcEnumBreakpointsCallback(PVM pVM, void *pvUser, PCDBGFBP pBp)
1558{
1559 PDBGC pDbgc = (PDBGC)pvUser;
1560 PDBGCBP pDbgcBp = dbgcBpGet(pDbgc, pBp->iBp);
1561
1562 /*
1563 * BP type and size.
1564 */
1565 char chType;
1566 char cb = 1;
1567 switch (pBp->enmType)
1568 {
1569 case DBGFBPTYPE_INT3:
1570 chType = 'p';
1571 break;
1572 case DBGFBPTYPE_REG:
1573 switch (pBp->u.Reg.fType)
1574 {
1575 case X86_DR7_RW_EO: chType = 'x'; break;
1576 case X86_DR7_RW_WO: chType = 'w'; break;
1577 case X86_DR7_RW_IO: chType = 'i'; break;
1578 case X86_DR7_RW_RW: chType = 'r'; break;
1579 default: chType = '?'; break;
1580
1581 }
1582 cb = pBp->u.Reg.cb;
1583 break;
1584 case DBGFBPTYPE_REM:
1585 chType = 'r';
1586 break;
1587 default:
1588 chType = '?';
1589 break;
1590 }
1591
1592 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%2u %c %d %c %VGv %04RX64 (%04RX64 to ",
1593 pBp->iBp, pBp->fEnabled ? 'e' : 'd', cb, chType,
1594 pBp->GCPtr, pBp->cHits, pBp->iHitTrigger);
1595 if (pBp->iHitDisable == ~(uint64_t)0)
1596 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "~0) ");
1597 else
1598 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%04RX64)");
1599
1600 /*
1601 * Try resolve the address.
1602 */
1603 DBGFSYMBOL Sym;
1604 RTGCINTPTR off;
1605 int rc = DBGFR3SymbolByAddr(pVM, pBp->GCPtr, &off, &Sym);
1606 if (VBOX_SUCCESS(rc))
1607 {
1608 if (!off)
1609 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s", Sym.szName);
1610 else if (off > 0)
1611 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s+%VGv", Sym.szName, off);
1612 else
1613 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "%s+%VGv", Sym.szName, -off);
1614 }
1615
1616 /*
1617 * The commands.
1618 */
1619 if (pDbgcBp)
1620 {
1621 if (pDbgcBp->cchCmd)
1622 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n cmds: '%s'\n",
1623 pDbgcBp->szCmd);
1624 else
1625 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n");
1626 }
1627 else
1628 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " [unknown bp]\n");
1629
1630 return VINF_SUCCESS;
1631}
1632
1633
1634/**
1635 * The 'bl' command.
1636 *
1637 * @returns VBox status.
1638 * @param pCmd Pointer to the command descriptor (as registered).
1639 * @param pCmdHlp Pointer to command helper functions.
1640 * @param pVM Pointer to the current VM (if any).
1641 * @param paArgs Pointer to (readonly) array of arguments.
1642 * @param cArgs Number of arguments in the array.
1643 */
1644static DECLCALLBACK(int) dbgcCmdBrkList(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR /*paArgs*/, unsigned /*cArgs*/, PDBGCVAR /*pResult*/)
1645{
1646 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1647
1648 /*
1649 * Enumerate the breakpoints.
1650 */
1651 int rc = DBGFR3BpEnum(pVM, dbgcEnumBreakpointsCallback, pDbgc);
1652 if (VBOX_FAILURE(rc))
1653 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3BpEnum failed.\n");
1654 return rc;
1655}
1656
1657
1658/**
1659 * The 'bp' command.
1660 *
1661 * @returns VBox status.
1662 * @param pCmd Pointer to the command descriptor (as registered).
1663 * @param pCmdHlp Pointer to command helper functions.
1664 * @param pVM Pointer to the current VM (if any).
1665 * @param paArgs Pointer to (readonly) array of arguments.
1666 * @param cArgs Number of arguments in the array.
1667 */
1668static DECLCALLBACK(int) dbgcCmdBrkSet(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
1669{
1670 /*
1671 * Convert the pointer to a DBGF address.
1672 */
1673 DBGFADDRESS Address;
1674 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1675 if (VBOX_FAILURE(rc))
1676 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Couldn't convert '%DV' to a DBGF address, rc=%Vrc.\n", &paArgs[0], rc);
1677
1678 /*
1679 * Pick out the optional arguments.
1680 */
1681 uint64_t iHitTrigger = 0;
1682 uint64_t iHitDisable = ~0;
1683 const char *pszCmds = NULL;
1684 unsigned iArg = 1;
1685 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1686 {
1687 iHitTrigger = paArgs[iArg].u.u64Number;
1688 iArg++;
1689 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1690 {
1691 iHitDisable = paArgs[iArg].u.u64Number;
1692 iArg++;
1693 }
1694 }
1695 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1696 {
1697 pszCmds = paArgs[iArg].u.pszString;
1698 iArg++;
1699 }
1700
1701 /*
1702 * Try set the breakpoint.
1703 */
1704 RTUINT iBp;
1705 rc = DBGFR3BpSet(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
1706 if (VBOX_SUCCESS(rc))
1707 {
1708 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1709 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1710 if (VBOX_SUCCESS(rc))
1711 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Set breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
1712 if (rc == VERR_DBGC_BP_EXISTS)
1713 {
1714 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1715 if (VBOX_SUCCESS(rc))
1716 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Updated breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
1717 }
1718 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
1719 AssertRC(rc2);
1720 }
1721 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Failed to set breakpoint at %VGv, rc=%Vrc.\n", Address.FlatPtr, rc);
1722}
1723
1724
1725/**
1726 * The 'br' command.
1727 *
1728 * @returns VBox status.
1729 * @param pCmd Pointer to the command descriptor (as registered).
1730 * @param pCmdHlp Pointer to command helper functions.
1731 * @param pVM Pointer to the current VM (if any).
1732 * @param paArgs Pointer to (readonly) array of arguments.
1733 * @param cArgs Number of arguments in the array.
1734 */
1735static DECLCALLBACK(int) dbgcCmdBrkREM(PCDBGCCMD /*pCmd*/, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR /*pResult*/)
1736{
1737 /*
1738 * Convert the pointer to a DBGF address.
1739 */
1740 DBGFADDRESS Address;
1741 int rc = pCmdHlp->pfnVarToDbgfAddr(pCmdHlp, &paArgs[0], &Address);
1742 if (VBOX_FAILURE(rc))
1743 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Couldn't convert '%DV' to a DBGF address, rc=%Vrc.\n", &paArgs[0], rc);
1744
1745 /*
1746 * Pick out the optional arguments.
1747 */
1748 uint64_t iHitTrigger = 0;
1749 uint64_t iHitDisable = ~0;
1750 const char *pszCmds = NULL;
1751 unsigned iArg = 1;
1752 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1753 {
1754 iHitTrigger = paArgs[iArg].u.u64Number;
1755 iArg++;
1756 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
1757 {
1758 iHitDisable = paArgs[iArg].u.u64Number;
1759 iArg++;
1760 }
1761 }
1762 if (iArg < cArgs && paArgs[iArg].enmType == DBGCVAR_TYPE_STRING)
1763 {
1764 pszCmds = paArgs[iArg].u.pszString;
1765 iArg++;
1766 }
1767
1768 /*
1769 * Try set the breakpoint.
1770 */
1771 RTUINT iBp;
1772 rc = DBGFR3BpSetREM(pVM, &Address, iHitTrigger, iHitDisable, &iBp);
1773 if (VBOX_SUCCESS(rc))
1774 {
1775 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1776 rc = dbgcBpAdd(pDbgc, iBp, pszCmds);
1777 if (VBOX_SUCCESS(rc))
1778 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Set REM breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
1779 if (rc == VERR_DBGC_BP_EXISTS)
1780 {
1781 rc = dbgcBpUpdate(pDbgc, iBp, pszCmds);
1782 if (VBOX_SUCCESS(rc))
1783 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Updated REM breakpoint %u at %VGv\n", iBp, Address.FlatPtr);
1784 }
1785 int rc2 = DBGFR3BpClear(pDbgc->pVM, iBp);
1786 AssertRC(rc2);
1787 }
1788 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Failed to set REM breakpoint at %VGv, rc=%Vrc.\n", Address.FlatPtr, rc);
1789}
1790
1791
1792/**
1793 * The 'u' command.
1794 *
1795 * @returns VBox status.
1796 * @param pCmd Pointer to the command descriptor (as registered).
1797 * @param pCmdHlp Pointer to command helper functions.
1798 * @param pVM Pointer to the current VM (if any).
1799 * @param paArgs Pointer to (readonly) array of arguments.
1800 * @param cArgs Number of arguments in the array.
1801 */
1802static DECLCALLBACK(int) dbgcCmdDisasm(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1803{
1804 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1805
1806 /*
1807 * Validate input.
1808 */
1809 if ( cArgs > 1
1810 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
1811 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
1812 if (!pVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1813 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Don't know where to start disassembling...\n");
1814 if (!pVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
1815 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: GC address but no VM.\n");
1816
1817 /*
1818 * Find address.
1819 */
1820 unsigned fFlags = DBGF_DISAS_FLAGS_NO_ADDRESS;
1821 if (!cArgs)
1822 {
1823 if (!DBGCVAR_ISPOINTER(pDbgc->DisasmPos.enmType))
1824 {
1825 pDbgc->DisasmPos.enmType = DBGCVAR_TYPE_GC_FAR;
1826 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVM) : CPUMGetHyperEIP(pVM);
1827 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVM) : CPUMGetHyperCS(pVM);
1828 if (pDbgc->fRegCtxGuest)
1829 fFlags |= DBGF_DISAS_FLAGS_CURRENT_GUEST;
1830 else
1831 fFlags |= DBGF_DISAS_FLAGS_CURRENT_HYPER;
1832 }
1833 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_NONE;
1834 }
1835 else
1836 pDbgc->DisasmPos = paArgs[0];
1837
1838 /*
1839 * Range.
1840 */
1841 switch (pDbgc->DisasmPos.enmRangeType)
1842 {
1843 case DBGCVAR_RANGE_NONE:
1844 pDbgc->DisasmPos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
1845 pDbgc->DisasmPos.u64Range = 10;
1846 break;
1847
1848 case DBGCVAR_RANGE_ELEMENTS:
1849 if (pDbgc->DisasmPos.u64Range > 2048)
1850 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
1851 break;
1852
1853 case DBGCVAR_RANGE_BYTES:
1854 if (pDbgc->DisasmPos.u64Range > 65536)
1855 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
1856 break;
1857
1858 default:
1859 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DisasmPos.enmRangeType);
1860 }
1861
1862 /*
1863 * Convert physical and host addresses to guest addresses.
1864 */
1865 int rc;
1866 switch (pDbgc->DisasmPos.enmType)
1867 {
1868 case DBGCVAR_TYPE_GC_FLAT:
1869 case DBGCVAR_TYPE_GC_FAR:
1870 break;
1871 case DBGCVAR_TYPE_GC_PHYS:
1872 case DBGCVAR_TYPE_HC_FLAT:
1873 case DBGCVAR_TYPE_HC_PHYS:
1874 case DBGCVAR_TYPE_HC_FAR:
1875 {
1876 DBGCVAR VarTmp;
1877 rc = pCmdHlp->pfnEval(pCmdHlp, &VarTmp, "%%(%Dv)", &pDbgc->DisasmPos);
1878 if (VBOX_FAILURE(rc))
1879 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: failed to evaluate '%%(%Dv)' -> %Vrc .\n", pDbgc->DisasmPos, rc);
1880 pDbgc->DisasmPos = VarTmp;
1881 break;
1882 }
1883 default: AssertFailed(); break;
1884 }
1885
1886 /*
1887 * Print address.
1888 * todo: Change to list near.
1889 */
1890#if 0
1891 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DisasmPos);
1892 if (VBOX_FAILURE(rc))
1893 return rc;
1894#endif
1895
1896 /*
1897 * Do the disassembling.
1898 */
1899 unsigned cTries = 32;
1900 int iRangeLeft = (int)pDbgc->DisasmPos.u64Range;
1901 if (iRangeLeft == 0) /* klugde for 'r'. */
1902 iRangeLeft = -1;
1903 for (;;)
1904 {
1905 /*
1906 * Disassemble the instruction.
1907 */
1908 char szDis[256];
1909 uint32_t cbInstr = 1;
1910 if (pDbgc->DisasmPos.enmType == DBGCVAR_TYPE_GC_FLAT)
1911 rc = DBGFR3DisasInstrEx(pVM, DBGF_SEL_FLAT, pDbgc->DisasmPos.u.GCFlat, fFlags, &szDis[0], sizeof(szDis), &cbInstr);
1912 else
1913 rc = DBGFR3DisasInstrEx(pVM, pDbgc->DisasmPos.u.GCFar.sel, pDbgc->DisasmPos.u.GCFar.off, fFlags, &szDis[0], sizeof(szDis), &cbInstr);
1914 if (VBOX_SUCCESS(rc))
1915 {
1916 /* print it */
1917 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%-16DV %s\n", &pDbgc->DisasmPos, &szDis[0]);
1918 if (VBOX_FAILURE(rc))
1919 return rc;
1920 }
1921 else
1922 {
1923 /* bitch. */
1924 int rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to disassemble instruction, skipping one byte.\n");
1925 if (VBOX_FAILURE(rc))
1926 return rc;
1927 if (cTries-- > 0)
1928 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Too many disassembly failures. Giving up.\n");
1929 cbInstr = 1;
1930 }
1931
1932 /* advance */
1933 if (iRangeLeft < 0) /* 'r' */
1934 break;
1935 if (pDbgc->DisasmPos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
1936 iRangeLeft--;
1937 else
1938 iRangeLeft -= cbInstr;
1939 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->DisasmPos, "%Dv + %x", &pDbgc->DisasmPos, cbInstr);
1940 if (VBOX_FAILURE(rc))
1941 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: %DV + %x\n", &pDbgc->DisasmPos, cbInstr);
1942 if (iRangeLeft <= 0)
1943 break;
1944 fFlags &= ~(DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_CURRENT_HYPER);
1945 }
1946
1947 NOREF(pCmd); NOREF(pResult);
1948 return 0;
1949}
1950
1951
1952/**
1953 * The 's' command.
1954 *
1955 * @returns VBox status.
1956 * @param pCmd Pointer to the command descriptor (as registered).
1957 * @param pCmdHlp Pointer to command helper functions.
1958 * @param pVM Pointer to the current VM (if any).
1959 * @param paArgs Pointer to (readonly) array of arguments.
1960 * @param cArgs Number of arguments in the array.
1961 */
1962static DECLCALLBACK(int) dbgcCmdSource(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1963{
1964 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
1965
1966 /*
1967 * Validate input.
1968 */
1969 if ( cArgs > 1
1970 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
1971 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
1972 if (!pVM && !cArgs && !DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1973 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Don't know where to start disassembling...\n");
1974 if (!pVM && cArgs && DBGCVAR_ISGCPOINTER(paArgs[0].enmType))
1975 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: GC address but no VM.\n");
1976
1977 /*
1978 * Find address.
1979 */
1980 if (!cArgs)
1981 {
1982 if (!DBGCVAR_ISPOINTER(pDbgc->SourcePos.enmType))
1983 {
1984 pDbgc->SourcePos.enmType = DBGCVAR_TYPE_GC_FAR;
1985 pDbgc->SourcePos.u.GCFar.off = pDbgc->fRegCtxGuest ? CPUMGetGuestEIP(pVM) : CPUMGetHyperEIP(pVM);
1986 pDbgc->SourcePos.u.GCFar.sel = pDbgc->fRegCtxGuest ? CPUMGetGuestCS(pVM) : CPUMGetHyperCS(pVM);
1987 }
1988 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_NONE;
1989 }
1990 else
1991 pDbgc->SourcePos = paArgs[0];
1992
1993 /*
1994 * Ensure the the source address is flat GC.
1995 */
1996 switch (pDbgc->SourcePos.enmType)
1997 {
1998 case DBGCVAR_TYPE_GC_FLAT:
1999 break;
2000 case DBGCVAR_TYPE_GC_PHYS:
2001 case DBGCVAR_TYPE_GC_FAR:
2002 case DBGCVAR_TYPE_HC_FLAT:
2003 case DBGCVAR_TYPE_HC_PHYS:
2004 case DBGCVAR_TYPE_HC_FAR:
2005 {
2006 int rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->SourcePos, "%%(%Dv)", &pDbgc->SourcePos);
2007 if (VBOX_FAILURE(rc))
2008 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Invalid address or address type. (rc=%d)\n", rc);
2009 break;
2010 }
2011 default: AssertFailed(); break;
2012 }
2013
2014 /*
2015 * Range.
2016 */
2017 switch (pDbgc->SourcePos.enmRangeType)
2018 {
2019 case DBGCVAR_RANGE_NONE:
2020 pDbgc->SourcePos.enmRangeType = DBGCVAR_RANGE_ELEMENTS;
2021 pDbgc->SourcePos.u64Range = 10;
2022 break;
2023
2024 case DBGCVAR_RANGE_ELEMENTS:
2025 if (pDbgc->SourcePos.u64Range > 2048)
2026 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many lines requested. Max is 2048 lines.\n");
2027 break;
2028
2029 case DBGCVAR_RANGE_BYTES:
2030 if (pDbgc->SourcePos.u64Range > 65536)
2031 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
2032 break;
2033
2034 default:
2035 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->SourcePos.enmRangeType);
2036 }
2037
2038 /*
2039 * Do the disassembling.
2040 */
2041 bool fFirst = 1;
2042 DBGFLINE LinePrev = { 0, 0, "" };
2043 int iRangeLeft = (int)pDbgc->SourcePos.u64Range;
2044 if (iRangeLeft == 0) /* klugde for 'r'. */
2045 iRangeLeft = -1;
2046 for (;;)
2047 {
2048 /*
2049 * Get line info.
2050 */
2051 DBGFLINE Line;
2052 RTGCINTPTR off;
2053 int rc = DBGFR3LineByAddr(pVM, pDbgc->SourcePos.u.GCFlat, &off, &Line);
2054 if (VBOX_FAILURE(rc))
2055 return VINF_SUCCESS;
2056
2057 unsigned cLines = 0;
2058 if (memcmp(&Line, &LinePrev, sizeof(Line)))
2059 {
2060 /*
2061 * Print filenamename
2062 */
2063 if (!fFirst && strcmp(Line.szFilename, LinePrev.szFilename))
2064 fFirst = true;
2065 if (fFirst)
2066 {
2067 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "[%s @ %d]\n", Line.szFilename, Line.uLineNo);
2068 if (VBOX_FAILURE(rc))
2069 return rc;
2070 }
2071
2072 /*
2073 * Try open the file and read the line.
2074 */
2075 FILE *phFile = fopen(Line.szFilename, "r");
2076 if (phFile)
2077 {
2078 /* Skip ahead to the desired line. */
2079 char szLine[4096];
2080 unsigned cBefore = fFirst ? RT_MIN(2, Line.uLineNo - 1) : Line.uLineNo - LinePrev.uLineNo - 1;
2081 if (cBefore > 7)
2082 cBefore = 0;
2083 unsigned cLeft = Line.uLineNo - cBefore;
2084 while (cLeft > 0)
2085 {
2086 szLine[0] = '\0';
2087 if (!fgets(szLine, sizeof(szLine), phFile))
2088 break;
2089 cLeft--;
2090 }
2091 if (!cLeft)
2092 {
2093 /* print the before lines */
2094 for (;;)
2095 {
2096 size_t cch = strlen(szLine);
2097 while (cch > 0 && (szLine[cch - 1] == '\r' || szLine[cch - 1] == '\n' || isspace(szLine[cch - 1])) )
2098 szLine[--cch] = '\0';
2099 if (cBefore-- <= 0)
2100 break;
2101
2102 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %4d: %s\n", Line.uLineNo - cBefore - 1, szLine);
2103 szLine[0] = '\0';
2104 fgets(szLine, sizeof(szLine), phFile);
2105 cLines++;
2106 }
2107 /* print the actual line */
2108 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08llx %4d: %s\n", Line.Address, Line.uLineNo, szLine);
2109 }
2110 fclose(phFile);
2111 if (VBOX_FAILURE(rc))
2112 return rc;
2113 fFirst = false;
2114 }
2115 else
2116 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Warning: couldn't open source file '%s'\n", Line.szFilename);
2117
2118 LinePrev = Line;
2119 }
2120
2121
2122 /*
2123 * Advance
2124 */
2125 if (iRangeLeft < 0) /* 'r' */
2126 break;
2127 if (pDbgc->SourcePos.enmRangeType == DBGCVAR_RANGE_ELEMENTS)
2128 iRangeLeft -= cLines;
2129 else
2130 iRangeLeft -= 1;
2131 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->SourcePos, "%Dv + %x", &pDbgc->SourcePos, 1);
2132 if (VBOX_FAILURE(rc))
2133 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: %Dv + %x\n", &pDbgc->SourcePos, 1);
2134 if (iRangeLeft <= 0)
2135 break;
2136 }
2137
2138 NOREF(pCmd); NOREF(pResult);
2139 return 0;
2140}
2141
2142
2143/**
2144 * The 'r' command.
2145 *
2146 * @returns VBox status.
2147 * @param pCmd Pointer to the command descriptor (as registered).
2148 * @param pCmdHlp Pointer to command helper functions.
2149 * @param pVM Pointer to the current VM (if any).
2150 * @param paArgs Pointer to (readonly) array of arguments.
2151 * @param cArgs Number of arguments in the array.
2152 */
2153static DECLCALLBACK(int) dbgcCmdReg(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2154{
2155 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2156
2157 if (pDbgc->fRegCtxGuest)
2158 return dbgcCmdRegGuest(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult);
2159 else
2160 return dbgcCmdRegHyper(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult);
2161}
2162
2163
2164/**
2165 * Common worker for the dbgcCmdReg*() commands.
2166 *
2167 * @returns VBox status.
2168 * @param pCmd Pointer to the command descriptor (as registered).
2169 * @param pCmdHlp Pointer to command helper functions.
2170 * @param pVM Pointer to the current VM (if any).
2171 * @param paArgs Pointer to (readonly) array of arguments.
2172 * @param cArgs Number of arguments in the array.
2173 * @param pszPrefix The symbol prefix.
2174 */
2175static DECLCALLBACK(int) dbgcCmdRegCommon(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult, const char *pszPrefix)
2176{
2177 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2178
2179 /*
2180 * cArgs == 0: Show all
2181 */
2182 if (cArgs == 0)
2183 {
2184 /*
2185 * Get register context.
2186 */
2187 int rc;
2188 PCPUMCTX pCtx;
2189 PCCPUMCTXCORE pCtxCore;
2190 if (!*pszPrefix)
2191 {
2192 rc = CPUMQueryGuestCtxPtr(pVM, &pCtx);
2193 pCtxCore = CPUMGetGuestCtxCore(pVM);
2194 }
2195 else
2196 {
2197 rc = CPUMQueryHyperCtxPtr(pVM, &pCtx);
2198 pCtxCore = CPUMGetHyperCtxCore(pVM);
2199 }
2200 if (VBOX_FAILURE(rc))
2201 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Getting register context\n");
2202
2203 /*
2204 * Format the flags.
2205 */
2206 static struct
2207 {
2208 const char *pszSet; const char *pszClear; uint32_t fFlag;
2209 } aFlags[] =
2210 {
2211 { "vip",NULL, X86_EFL_VIP },
2212 { "vif",NULL, X86_EFL_VIF },
2213 { "ac", NULL, X86_EFL_AC },
2214 { "vm", NULL, X86_EFL_VM },
2215 { "rf", NULL, X86_EFL_RF },
2216 { "nt", NULL, X86_EFL_NT },
2217 { "ov", "nv", X86_EFL_OF },
2218 { "dn", "up", X86_EFL_DF },
2219 { "ei", "di", X86_EFL_IF },
2220 { "tf", NULL, X86_EFL_TF },
2221 { "nt", "pl", X86_EFL_SF },
2222 { "nz", "zr", X86_EFL_ZF },
2223 { "ac", "na", X86_EFL_AF },
2224 { "po", "pe", X86_EFL_PF },
2225 { "cy", "nc", X86_EFL_CF },
2226 };
2227 char szEFlags[80];
2228 char *psz = szEFlags;
2229 uint32_t efl = pCtxCore->eflags.u32;
2230 for (unsigned i = 0; i < ELEMENTS(aFlags); i++)
2231 {
2232 const char *pszAdd = aFlags[i].fFlag & efl ? aFlags[i].pszSet : aFlags[i].pszClear;
2233 if (pszAdd)
2234 {
2235 strcpy(psz, pszAdd);
2236 psz += strlen(pszAdd);
2237 *psz++ = ' ';
2238 }
2239 }
2240 psz[-1] = '\0';
2241
2242
2243 /*
2244 * Format the registers.
2245 */
2246 if (pDbgc->fRegTerse)
2247 {
2248 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2249 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
2250 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
2251 "%scs=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %sss=%04x %seflags=%08x\n",
2252 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->edi, pszPrefix, pCtxCore->esi,
2253 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 34 : 31, szEFlags,
2254 pszPrefix, (RTSEL)pCtxCore->cs, pszPrefix, (RTSEL)pCtxCore->ds, pszPrefix, (RTSEL)pCtxCore->es,
2255 pszPrefix, (RTSEL)pCtxCore->fs, pszPrefix, (RTSEL)pCtxCore->gs, pszPrefix, (RTSEL)pCtxCore->ss, pszPrefix, efl);
2256 }
2257 else
2258 {
2259 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2260 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
2261 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
2262 "%scs={%04x base=%08x limit=%08x flags=%08x} %sdr0=%08x %sdr1=%08x\n"
2263 "%sds={%04x base=%08x limit=%08x flags=%08x} %sdr2=%08x %sdr3=%08x\n"
2264 "%ses={%04x base=%08x limit=%08x flags=%08x} %sdr4=%08x %sdr5=%08x\n"
2265 "%sfs={%04x base=%08x limit=%08x flags=%08x} %sdr6=%08x %sdr7=%08x\n"
2266 "%sgs={%04x base=%08x limit=%08x flags=%08x} %scr0=%08x %scr2=%08x\n"
2267 "%sss={%04x base=%08x limit=%08x flags=%08x} %scr3=%08x %scr4=%08x\n"
2268 "%sgdtr=%08x:%04x %sidtr=%08x:%04x %seflags=%08x\n"
2269 "%sldtr={%04x base=%08x limit=%08x flags=%08x}\n"
2270 "%str ={%04x base=%08x limit=%08x flags=%08x}\n"
2271 "%sSysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
2272 "%sFCW=%04x %sFSW=%04x %sFTW=%04x\n"
2273 ,
2274 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->edi, pszPrefix, pCtxCore->esi,
2275 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
2276 pszPrefix, (RTSEL)pCtxCore->cs, pCtx->csHid.u32Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u, pszPrefix, pCtx->dr0, pszPrefix, pCtx->dr1,
2277 pszPrefix, (RTSEL)pCtxCore->ds, pCtx->dsHid.u32Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u, pszPrefix, pCtx->dr2, pszPrefix, pCtx->dr3,
2278 pszPrefix, (RTSEL)pCtxCore->es, pCtx->esHid.u32Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u, pszPrefix, pCtx->dr4, pszPrefix, pCtx->dr5,
2279 pszPrefix, (RTSEL)pCtxCore->fs, pCtx->fsHid.u32Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u, pszPrefix, pCtx->dr6, pszPrefix, pCtx->dr7,
2280 pszPrefix, (RTSEL)pCtxCore->gs, pCtx->gsHid.u32Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u, pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2,
2281 pszPrefix, (RTSEL)pCtxCore->ss, pCtx->ssHid.u32Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
2282 pszPrefix, pCtx->gdtr.pGdt,pCtx->gdtr.cbGdt, pszPrefix, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, pszPrefix, pCtxCore->eflags,
2283 pszPrefix, (RTSEL)pCtx->ldtr, pCtx->ldtrHid.u32Base, pCtx->ldtrHid.u32Limit, pCtx->ldtrHid.Attr.u,
2284 pszPrefix, (RTSEL)pCtx->tr, pCtx->trHid.u32Base, pCtx->trHid.u32Limit, pCtx->trHid.Attr.u,
2285 pszPrefix, pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp,
2286 pszPrefix, pCtx->fpu.FCW, pszPrefix, pCtx->fpu.FSW, pszPrefix, pCtx->fpu.FTW);
2287 }
2288
2289 /*
2290 * Disassemble one instruction at cs:eip.
2291 */
2292 return pCmdHlp->pfnExec(pCmdHlp, "u %04x:%08x L 0", pCtx->cs, pCtx->eip);
2293 }
2294
2295 /*
2296 * cArgs == 1: Show the register.
2297 * cArgs == 2: Modify the register.
2298 */
2299 if ( cArgs == 1
2300 || cArgs == 2)
2301 {
2302 /* locate the register symbol. */
2303 const char *pszReg = paArgs[0].u.pszString;
2304 if ( *pszPrefix
2305 && pszReg[0] != *pszPrefix)
2306 {
2307 /* prepend the prefix. */
2308 char *psz = (char *)alloca(strlen(pszReg) + 2);
2309 psz[0] = *pszPrefix;
2310 strcpy(psz + 1, paArgs[0].u.pszString);
2311 pszReg = psz;
2312 }
2313 PCDBGCSYM pSym = dbgcLookupRegisterSymbol(pDbgc, pszReg);
2314 if (!pSym)
2315 return pCmdHlp->pfnVBoxError(pCmdHlp, VERR_INVALID_PARAMETER /* VERR_DBGC_INVALID_REGISTER */, "Invalid register name '%s'.\n", pszReg);
2316
2317 /* show the register */
2318 if (cArgs == 1)
2319 {
2320 DBGCVAR Var;
2321 memset(&Var, 0, sizeof(Var));
2322 int rc = pSym->pfnGet(pSym, pCmdHlp, DBGCVAR_TYPE_NUMBER, &Var);
2323 if (VBOX_FAILURE(rc))
2324 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Failed getting value for register '%s'.\n", pszReg);
2325 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s=%Dv\n", pszReg, &Var);
2326 }
2327
2328 /* change the register */
2329 int rc = pSym->pfnSet(pSym, pCmdHlp, &paArgs[1]);
2330 if (VBOX_FAILURE(rc))
2331 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Failed setting value for register '%s'.\n", pszReg);
2332 return VINF_SUCCESS;
2333 }
2334
2335
2336 NOREF(pCmd); NOREF(paArgs); NOREF(pResult);
2337 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Huh? cArgs=%d Expected 0, 1 or 2!\n", cArgs);
2338}
2339
2340
2341/**
2342 * The 'rg' command.
2343 *
2344 * @returns VBox status.
2345 * @param pCmd Pointer to the command descriptor (as registered).
2346 * @param pCmdHlp Pointer to command helper functions.
2347 * @param pVM Pointer to the current VM (if any).
2348 * @param paArgs Pointer to (readonly) array of arguments.
2349 * @param cArgs Number of arguments in the array.
2350 */
2351static DECLCALLBACK(int) dbgcCmdRegGuest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2352{
2353 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult, "");
2354}
2355
2356
2357/**
2358 * The 'rh' command.
2359 *
2360 * @returns VBox status.
2361 * @param pCmd Pointer to the command descriptor (as registered).
2362 * @param pCmdHlp Pointer to command helper functions.
2363 * @param pVM Pointer to the current VM (if any).
2364 * @param paArgs Pointer to (readonly) array of arguments.
2365 * @param cArgs Number of arguments in the array.
2366 */
2367static DECLCALLBACK(int) dbgcCmdRegHyper(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2368{
2369 return dbgcCmdRegCommon(pCmd, pCmdHlp, pVM, paArgs, cArgs, pResult, ".");
2370}
2371
2372
2373/**
2374 * The 'rt' command.
2375 *
2376 * @returns VBox status.
2377 * @param pCmd Pointer to the command descriptor (as registered).
2378 * @param pCmdHlp Pointer to command helper functions.
2379 * @param pVM Pointer to the current VM (if any).
2380 * @param paArgs Pointer to (readonly) array of arguments.
2381 * @param cArgs Number of arguments in the array.
2382 */
2383static DECLCALLBACK(int) dbgcCmdRegTerse(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2384{
2385 NOREF(pCmd); NOREF(pVM); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
2386
2387 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2388 pDbgc->fRegTerse = !pDbgc->fRegTerse;
2389 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, pDbgc->fRegTerse ? "info: Terse register info.\n" : "info: Verbose register info.\n");
2390}
2391
2392
2393/**
2394 * The 't' command.
2395 *
2396 * @returns VBox status.
2397 * @param pCmd Pointer to the command descriptor (as registered).
2398 * @param pCmdHlp Pointer to command helper functions.
2399 * @param pVM Pointer to the current VM (if any).
2400 * @param paArgs Pointer to (readonly) array of arguments.
2401 * @param cArgs Number of arguments in the array.
2402 */
2403static DECLCALLBACK(int) dbgcCmdTrace(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2404{
2405 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2406
2407 int rc = DBGFR3Step(pVM);
2408 if (VBOX_SUCCESS(rc))
2409 pDbgc->fReady = false;
2410 else
2411 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to single step VM %p\n", pDbgc->pVM);
2412
2413 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
2414 return rc;
2415}
2416
2417
2418/**
2419 * The 'k', 'kg' and 'kh' commands.
2420 *
2421 * @returns VBox status.
2422 * @param pCmd Pointer to the command descriptor (as registered).
2423 * @param pCmdHlp Pointer to command helper functions.
2424 * @param pVM Pointer to the current VM (if any).
2425 * @param paArgs Pointer to (readonly) array of arguments.
2426 * @param cArgs Number of arguments in the array.
2427 */
2428static DECLCALLBACK(int) dbgcCmdStack(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2429{
2430 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2431
2432 /*
2433 * Figure which context we're called for.
2434 */
2435 bool fGuest = pCmd->pszCmd[1] == 'g'
2436 || (!pCmd->pszCmd[1] && pDbgc->fRegCtxGuest);
2437
2438
2439 DBGFSTACKFRAME Frame;
2440 memset(&Frame, 0, sizeof(Frame));
2441 int rc;
2442 if (fGuest)
2443 rc = DBGFR3StackWalkBeginGuest(pVM, &Frame);
2444 else
2445 rc = DBGFR3StackWalkBeginHyper(pVM, &Frame);
2446 if (VBOX_FAILURE(rc))
2447 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to begin stack walk, rc=%Vrc\n", rc);
2448
2449 /*
2450 * Print header.
2451 * 12345678 12345678 0023:87654321 12345678 87654321 12345678 87654321 symbol
2452 */
2453 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP / Symbol [line]\n");
2454 if (VBOX_FAILURE(rc))
2455 return rc;
2456 do
2457 {
2458 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
2459 (uint32_t)Frame.AddrFrame.off,
2460 (uint32_t)Frame.AddrReturnFrame.off,
2461 (uint32_t)Frame.AddrReturnPC.Sel,
2462 (uint32_t)Frame.AddrReturnPC.off,
2463 Frame.Args.au32[0],
2464 Frame.Args.au32[1],
2465 Frame.Args.au32[2],
2466 Frame.Args.au32[3]);
2467 if (VBOX_FAILURE(rc))
2468 return rc;
2469 if (!Frame.pSymPC)
2470 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %RTsel:%08RGv", Frame.AddrPC.Sel, Frame.AddrPC.off);
2471 else
2472 {
2473 RTGCINTPTR offDisp = Frame.AddrPC.FlatPtr - Frame.pSymPC->Value; /** @todo this isn't 100% correct for segemnted stuff. */
2474 if (offDisp > 0)
2475 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s+%llx", Frame.pSymPC->szName, (int64_t)offDisp);
2476 else if (offDisp < 0)
2477 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s-%llx", Frame.pSymPC->szName, -(int64_t)offDisp);
2478 else
2479 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " %s", Frame.pSymPC->szName);
2480 }
2481 if (VBOX_SUCCESS(rc) && Frame.pLinePC)
2482 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " [%s @ 0i%d]", Frame.pLinePC->szFilename, Frame.pLinePC->uLineNo);
2483 if (VBOX_SUCCESS(rc))
2484 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2485 if (VBOX_FAILURE(rc))
2486 return rc;
2487
2488 /* next */
2489 rc = DBGFR3StackWalkNext(pVM, &Frame);
2490 } while (VBOX_SUCCESS(rc));
2491
2492 NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
2493 return VINF_SUCCESS;
2494}
2495
2496
2497
2498/**
2499 * The 'dd', 'dw' and 'db' commands.
2500 *
2501 * @returns VBox status.
2502 * @param pCmd Pointer to the command descriptor (as registered).
2503 * @param pCmdHlp Pointer to command helper functions.
2504 * @param pVM Pointer to the current VM (if any).
2505 * @param paArgs Pointer to (readonly) array of arguments.
2506 * @param cArgs Number of arguments in the array.
2507 */
2508static DECLCALLBACK(int) dbgcCmdDumpMem(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2509{
2510 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2511
2512 /*
2513 * Validate input.
2514 */
2515 if ( cArgs > 1
2516 || (cArgs == 1 && !DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2517 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2518 if (!pVM)
2519 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2520
2521 /*
2522 * Figure out the element size.
2523 */
2524 size_t cbElement;
2525 bool fAscii = false;
2526 switch (pCmd->pszCmd[1])
2527 {
2528 default:
2529 case 'b': cbElement = 1; break;
2530 case 'w': cbElement = 2; break;
2531 case 'd': cbElement = 4; break;
2532 case 'q': cbElement = 8; break;
2533 case 'a':
2534 cbElement = 1;
2535 fAscii = true;
2536 break;
2537 case '\0':
2538 fAscii = !!(pDbgc->cbDumpElement & 0x80000000);
2539 cbElement = pDbgc->cbDumpElement & 0x7fffffff;
2540 if (!cbElement)
2541 cbElement = 1;
2542 break;
2543 }
2544
2545 /*
2546 * Find address.
2547 */
2548 if (!cArgs)
2549 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_NONE;
2550 else
2551 pDbgc->DumpPos = paArgs[0];
2552
2553 /*
2554 * Range.
2555 */
2556 switch (pDbgc->DumpPos.enmRangeType)
2557 {
2558 case DBGCVAR_RANGE_NONE:
2559 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2560 pDbgc->DumpPos.u64Range = 0x60;
2561 break;
2562
2563 case DBGCVAR_RANGE_ELEMENTS:
2564 if (pDbgc->DumpPos.u64Range > 2048)
2565 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: Too many elements requested. Max is 2048 elements.\n");
2566 pDbgc->DumpPos.enmRangeType = DBGCVAR_RANGE_BYTES;
2567 pDbgc->DumpPos.u64Range = (cbElement ? cbElement : 1) * pDbgc->DumpPos.u64Range;
2568 break;
2569
2570 case DBGCVAR_RANGE_BYTES:
2571 if (pDbgc->DumpPos.u64Range > 65536)
2572 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The requested range is too big. Max is 64KB.\n");
2573 break;
2574
2575 default:
2576 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: Unknown range type %d.\n", pDbgc->DumpPos.enmRangeType);
2577 }
2578
2579 /*
2580 * Do the dumping.
2581 */
2582 pDbgc->cbDumpElement = cbElement | (fAscii << 31);
2583 int cbLeft = (int)pDbgc->DumpPos.u64Range;
2584 uint8_t u8Prev = '\0';
2585 for (;;)
2586 {
2587 /*
2588 * Read memory.
2589 */
2590 char achBuffer[16];
2591 size_t cbReq = RT_MIN((int)sizeof(achBuffer), cbLeft);
2592 size_t cb = RT_MIN((int)sizeof(achBuffer), cbLeft);
2593 int rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &achBuffer, cbReq, &pDbgc->DumpPos, &cb);
2594 if (VBOX_FAILURE(rc))
2595 {
2596 if (u8Prev && u8Prev != '\n')
2597 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2598 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &pDbgc->DumpPos);
2599 }
2600
2601 /*
2602 * Display it.
2603 */
2604 memset(&achBuffer[cb], 0, sizeof(achBuffer) - cb);
2605 if (!fAscii)
2606 {
2607 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:", &pDbgc->DumpPos);
2608 unsigned i;
2609 for (i = 0; i < cb; i += cbElement)
2610 {
2611 const char *pszSpace = " ";
2612 if (cbElement <= 2 && i == 8 && !fAscii)
2613 pszSpace = "-";
2614 switch (cbElement)
2615 {
2616 case 1: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%02x", pszSpace, *(uint8_t *)&achBuffer[i]); break;
2617 case 2: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%04x", pszSpace, *(uint16_t *)&achBuffer[i]); break;
2618 case 4: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%08x", pszSpace, *(uint32_t *)&achBuffer[i]); break;
2619 case 8: pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%s%016llx", pszSpace, *(uint64_t *)&achBuffer[i]); break;
2620 }
2621 }
2622
2623 /* chars column */
2624 if (pDbgc->cbDumpElement == 1)
2625 {
2626 while (i < sizeof(achBuffer))
2627 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2628 pCmdHlp->pfnPrintf(pCmdHlp, NULL, " ");
2629 for (i = 0; i < cb; i += cbElement)
2630 {
2631 uint8_t u8 = *(uint8_t *)&achBuffer[i];
2632 if (isprint(u8) && u8 < 127)
2633 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2634 else
2635 pCmdHlp->pfnPrintf(pCmdHlp, NULL, ".");
2636 }
2637 }
2638 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2639 }
2640 else
2641 {
2642 /*
2643 * We print up to the first zero and stop there.
2644 * Only printables + '\t' and '\n' are printed.
2645 */
2646 if (!u8Prev)
2647 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV:\n", &pDbgc->DumpPos);
2648 uint8_t u8 = '\0';
2649 unsigned i;
2650 for (i = 0; i < cb; i++)
2651 {
2652 u8Prev = u8;
2653 u8 = *(uint8_t *)&achBuffer[i];
2654 if ( u8 < 127
2655 && ( isprint(u8)
2656 || u8 == '\t'
2657 || u8 == '\n'))
2658 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%c", u8);
2659 else if (!u8)
2660 break;
2661 else
2662 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\\x%x", u8);
2663 }
2664 if (u8 == '\0')
2665 cbLeft = cb = i + 1;
2666 if (cbLeft - cb <= 0 && u8Prev != '\n')
2667 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
2668 }
2669
2670 /*
2671 * Advance
2672 */
2673 cbLeft -= cb;
2674 rc = pCmdHlp->pfnEval(pCmdHlp, &pDbgc->DumpPos, "%Dv + %x", &pDbgc->DumpPos, cb);
2675 if (VBOX_FAILURE(rc))
2676 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Expression: %Dv + %x\n", &pDbgc->DumpPos, cb);
2677 if (cbLeft <= 0)
2678 break;
2679 }
2680
2681 NOREF(pCmd); NOREF(pResult);
2682 return VINF_SUCCESS;
2683}
2684
2685
2686/**
2687 * The 'dpd*' commands.
2688 *
2689 * @returns VBox status.
2690 * @param pCmd Pointer to the command descriptor (as registered).
2691 * @param pCmdHlp Pointer to command helper functions.
2692 * @param pVM Pointer to the current VM (if any).
2693 * @param paArgs Pointer to (readonly) array of arguments.
2694 * @param cArgs Number of arguments in the array.
2695 */
2696static DECLCALLBACK(int) dbgcCmdDumpPageDir(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2697{
2698 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2699
2700 /*
2701 * Validate input.
2702 */
2703 if ( cArgs > 1
2704 || (cArgs == 1 && pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2705 || (cArgs == 1 && pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2706 )
2707 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2708 if (!pVM)
2709 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2710
2711
2712 /*
2713 * Where to start dumping page directory entries?
2714 */
2715 int rc;
2716 unsigned cEntriesMax = PAGE_SIZE / sizeof(VBOXPDE);
2717 unsigned cEntries = PAGE_SIZE / sizeof(VBOXPDE);
2718 unsigned off = ~0;
2719 uint32_t u32CR4 = X86_CR4_PSE;
2720 DBGCVAR VarAddr;
2721 if (cArgs == 0 || pCmd->pszCmd[3] != 'a')
2722 {
2723 /*
2724 * Get defaults.
2725 */
2726 off = 0;
2727 if ( pCmd->pszCmd[3] == 'g'
2728 || (pDbgc->fRegCtxGuest && (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')))
2729 {
2730 u32CR4 = CPUMGetGuestCR4(pVM);
2731 rc = pCmdHlp->pfnEval(pCmdHlp, &VarAddr, "%%%%cr3");
2732 }
2733 else
2734 {
2735 /** @todo fix hypervisor CR4 value! */
2736 //u32CR4 = CPUMGetHyperCR4(pVM);
2737 u32CR4 = X86_CR4_PGE | X86_CR4_PSE;
2738 rc = pCmdHlp->pfnEval(pCmdHlp, &VarAddr, "#%%%%.cr3");
2739 }
2740 if (VBOX_FAILURE(rc))
2741 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "(Getting (.)cr3.)");
2742
2743 if (cArgs > 0)
2744 {
2745 cEntries = 3;
2746 if (!DBGCVAR_ISPOINTER(paArgs[0].enmType))
2747 {
2748 /*
2749 * Add index.
2750 */
2751 rc = pCmdHlp->pfnEval(pCmdHlp, &VarAddr, "%DV + %#x", &VarAddr, paArgs[0].u.u64Number * sizeof(VBOXPTE));
2752 if (VBOX_FAILURE(rc))
2753 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%DV + %#x", &VarAddr, paArgs[0].u.u64Number * sizeof(VBOXPTE));
2754 if (paArgs[0].u.u64Number >= PAGE_SIZE / sizeof(VBOXPDE))
2755 off = ~0;
2756 else
2757 {
2758 off = (unsigned)paArgs[0].u.u64Number;
2759 cEntriesMax = PAGE_SIZE / sizeof(VBOXPDE) - off;
2760 }
2761 }
2762 else
2763 {
2764 /*
2765 * Pointer which we want the page directory entry for.
2766 * Start by making sure it's a GC pointer.
2767 */
2768 DBGCVAR VarTmp;
2769 rc = pCmdHlp->pfnEval(pCmdHlp, &VarTmp, "%%(%Dv)", &paArgs[0]);
2770 if (VBOX_FAILURE(rc))
2771 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%Dv) failed.", &paArgs[0]);
2772
2773 rc = pCmdHlp->pfnEval(pCmdHlp, &VarAddr, "%DV + %#x", &VarAddr, (VarTmp.u.GCFlat >> PGDIR_SHIFT) * sizeof(VBOXPTE));
2774 if (VBOX_FAILURE(rc))
2775 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%DV + %#x", &VarAddr, (VarTmp.u.GCFlat >> PGDIR_SHIFT) * sizeof(VBOXPTE));
2776 off = VarTmp.u.GCFlat >> PGDIR_SHIFT;
2777 cEntriesMax = PAGE_SIZE / sizeof(VBOXPDE) - off;
2778 }
2779 }
2780 }
2781 else
2782 VarAddr = paArgs[0];
2783
2784 /*
2785 * Range.
2786 */
2787 unsigned i = cArgs;
2788 while (i-- > 0)
2789 {
2790 if (paArgs[i].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
2791 {
2792 cEntries = (unsigned)RT_MIN(paArgs[i].u64Range, cEntriesMax);
2793 break;
2794 }
2795 else if (paArgs[i].enmRangeType == DBGCVAR_RANGE_BYTES)
2796 {
2797 cEntries = (unsigned)RT_MIN(paArgs[i].u64Range / sizeof(VBOXPDE), cEntriesMax);
2798 break;
2799 }
2800 }
2801
2802 /*
2803 * Dump loop.
2804 */
2805 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, off != ~0U ? "%DV (index %#x):\n" : "%DV:\n", &VarAddr, off);
2806 if (VBOX_FAILURE(rc))
2807 return rc;
2808 for (;;)
2809 {
2810 /*
2811 * Read.
2812 */
2813 VBOXPDE Pde;
2814 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarAddr, NULL);
2815 if (VBOX_FAILURE(rc))
2816 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &VarAddr);
2817
2818 /*
2819 * Display.
2820 */
2821 if (off != ~0U)
2822 {
2823 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%03x %08x: ", off, off << PGDIR_SHIFT);
2824 off++;
2825 }
2826 if ((u32CR4 & X86_CR4_PSE) && Pde.b.u1Size)
2827 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2828 "%08x big phys=%08x %s %s %s %s %s avl=%02x %s %s %s %s\n",
2829 Pde.u, Pde.b.u10PageNo << PGDIR_SHIFT, Pde.b.u1Present ? "p " : "np", Pde.b.u1Write ? "w" : "r",
2830 Pde.b.u1User ? "u" : "s", Pde.b.u1Accessed ? "a " : "na", Pde.b.u1Dirty ? "d " : "nd",
2831 Pde.b.u3Available, Pde.b.u1Global ? "G" : " ", Pde.b.u1WriteThru ? "pwt" : " ",
2832 Pde.b.u1CacheDisable ? "pcd" : " ", Pde.b.u1PAT ? "pat" : "");
2833 else
2834 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
2835 "%08x 4kb phys=%08x %s %s %s %s %s avl=%02x %s %s %s %s\n",
2836 Pde.u, Pde.n.u20PageNo << PAGE_SHIFT, Pde.n.u1Present ? "p " : "np", Pde.n.u1Write ? "w" : "r",
2837 Pde.n.u1User ? "u" : "s", Pde.n.u1Accessed ? "a " : "na", Pde.u & BIT(6) ? "6 " : " ",
2838 Pde.n.u3Available, Pde.u & BIT(8) ? "8" : " ", Pde.n.u1WriteThru ? "pwt" : " ",
2839 Pde.n.u1CacheDisable ? "pcd" : " ", Pde.u & BIT(7) ? "7" : "");
2840 if (VBOX_FAILURE(rc))
2841 return rc;
2842
2843 /*
2844 * Next
2845 */
2846 if (cEntries-- <= 1)
2847 break;
2848 rc = pCmdHlp->pfnEval(pCmdHlp, &VarAddr, "%DV + %#x", &VarAddr, sizeof(VBOXPDE));
2849 if (VBOX_FAILURE(rc))
2850 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%DV + %#x", &VarAddr, sizeof(VBOXPDE));
2851 }
2852
2853 NOREF(pResult);
2854 return VINF_SUCCESS;
2855}
2856
2857/**
2858 * The 'dpdb' command.
2859 *
2860 * @returns VBox status.
2861 * @param pCmd Pointer to the command descriptor (as registered).
2862 * @param pCmdHlp Pointer to command helper functions.
2863 * @param pVM Pointer to the current VM (if any).
2864 * @param paArgs Pointer to (readonly) array of arguments.
2865 * @param cArgs Number of arguments in the array.
2866 */
2867static DECLCALLBACK(int) dbgcCmdDumpPageDirBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2868{
2869 if (!pVM)
2870 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2871 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
2872 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
2873 if (VBOX_FAILURE(rc1))
2874 return rc1;
2875 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
2876 return rc2;
2877}
2878
2879
2880/**
2881 * The 'dpg*' commands.
2882 *
2883 * @returns VBox status.
2884 * @param pCmd Pointer to the command descriptor (as registered).
2885 * @param pCmdHlp Pointer to command helper functions.
2886 * @param pVM Pointer to the current VM (if any).
2887 * @param paArgs Pointer to (readonly) array of arguments.
2888 * @param cArgs Number of arguments in the array.
2889 */
2890static DECLCALLBACK(int) dbgcCmdDumpPageTable(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
2891{
2892 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
2893
2894 /*
2895 * Validate input.
2896 */
2897 if ( cArgs != 1
2898 || (pCmd->pszCmd[3] == 'a' && !DBGCVAR_ISPOINTER(paArgs[0].enmType))
2899 || (pCmd->pszCmd[3] != 'a' && !(paArgs[0].enmType == DBGCVAR_TYPE_NUMBER || DBGCVAR_ISPOINTER(paArgs[0].enmType)))
2900 )
2901 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. It might help to use the '%%' operator.\n");
2902 if (!pVM)
2903 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
2904
2905
2906 /*
2907 * Where to start dumping page directory entries?
2908 */
2909 int rc;
2910 unsigned cEntriesMax = PAGE_SIZE / sizeof(VBOXPDE);
2911 unsigned cEntries = PAGE_SIZE / sizeof(VBOXPDE);
2912 unsigned off = ~0;
2913 DBGCVAR VarGCPtr; /* only valid with off == ~0 */
2914 DBGCVAR VarPTEAddr;
2915 if (pCmd->pszCmd[3] != 'a')
2916 {
2917 /*
2918 * Get page directory and cr4.
2919 */
2920 bool fHyper;
2921 uint32_t u32CR4;
2922 off = 0;
2923 if ( pCmd->pszCmd[3] == 'g'
2924 || (pDbgc->fRegCtxGuest && (!pCmd->pszCmd[3] || pCmd->pszCmd[3] == 'a')))
2925 {
2926 u32CR4 = CPUMGetGuestCR4(pVM);
2927 rc = pCmdHlp->pfnEval(pCmdHlp, &VarPTEAddr, "%%%%cr3");
2928 fHyper = false;
2929 }
2930 else
2931 {
2932 /** @todo fix hypervisor CR4 value! */
2933 //u32CR4 = CPUMGetHyperCR4(pVM);
2934 u32CR4 = X86_CR4_PGE | X86_CR4_PSE;
2935 rc = pCmdHlp->pfnEval(pCmdHlp, &VarPTEAddr, "#%%%%.cr3");
2936 fHyper = true;
2937 }
2938 if (VBOX_FAILURE(rc))
2939 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "(Getting (.)cr3.)");
2940
2941 /*
2942 * Find page directory entry for the address.
2943 * Make sure it's a flat address first.
2944 */
2945 rc = pCmdHlp->pfnEval(pCmdHlp, &VarGCPtr, "%%(%Dv)", &paArgs[0]);
2946 if (VBOX_FAILURE(rc))
2947 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%Dv)", &paArgs[0]);
2948
2949 rc = pCmdHlp->pfnEval(pCmdHlp, &VarPTEAddr, "%Dv + %#x", &VarPTEAddr, (VarGCPtr.u.GCFlat >> PGDIR_SHIFT) * sizeof(VBOXPDE));
2950 if (VBOX_FAILURE(rc))
2951 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%Dv + %#x", &VarPTEAddr, (VarGCPtr.u.GCFlat >> PGDIR_SHIFT) * sizeof(VBOXPDE));
2952
2953
2954 /*
2955 * Now read the page directory entry for this GC address.
2956 */
2957 VBOXPDE Pde;
2958 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pde, sizeof(Pde), &VarPTEAddr, NULL);
2959 if (VBOX_FAILURE(rc))
2960 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &VarPTEAddr);
2961
2962 /*
2963 * Check for presentness and handle big using dpd[gh].
2964 */
2965 if ((u32CR4 & X86_CR4_PSE) && Pde.b.u1Size)
2966 return pCmdHlp->pfnExec(pCmdHlp, "dpd%s %Dv L3", &pCmd->pszCmd[3], &VarGCPtr);
2967
2968 if (!Pde.n.u1Present)
2969 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Page table for %Dv is not present.\n", &VarGCPtr);
2970
2971 /*
2972 * Calc page table address and setup offset and counts.
2973 */
2974 rc = pCmdHlp->pfnEval(pCmdHlp, &VarPTEAddr, fHyper ? "#%%%%%#x" : "%%%%%#x", Pde.u & X86_PDE_PG_MASK);
2975 if (VBOX_FAILURE(rc))
2976 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, fHyper ? "#%%%%%#x" : "%%%%%#x", Pde.u & X86_PDE_PG_MASK);
2977
2978 cEntries = 10;
2979 off = (VarGCPtr.u.GCFlat >> PAGE_SHIFT) & PTE_MASK;
2980 cEntriesMax = PAGE_SIZE / sizeof(VBOXPDE) - off;
2981 VarGCPtr.u.GCFlat &= ~PAGE_OFFSET_MASK; /* Make it page table base address. */
2982 }
2983 else
2984 VarPTEAddr = paArgs[0];
2985
2986 /*
2987 * Range.
2988 */
2989 unsigned i = cArgs;
2990 while (i-- > 0)
2991 {
2992 if (paArgs[i].enmRangeType == DBGCVAR_RANGE_ELEMENTS)
2993 {
2994 cEntries = (unsigned)RT_MIN(paArgs[i].u64Range, cEntriesMax);
2995 break;
2996 }
2997 else if (paArgs[i].enmRangeType == DBGCVAR_RANGE_BYTES)
2998 {
2999 cEntries = (unsigned)RT_MIN(paArgs[i].u64Range / sizeof(VBOXPDE), cEntriesMax);
3000 break;
3001 }
3002 }
3003
3004 /*
3005 * Dump loop.
3006 */
3007 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, off != ~0U ? "%DV (base %DV / index %#x):\n" : "%DV:\n", &VarPTEAddr, &VarGCPtr, off);
3008 if (VBOX_FAILURE(rc))
3009 return rc;
3010 rc = pCmdHlp->pfnEval(pCmdHlp, &VarPTEAddr, "%DV + %#x", &VarPTEAddr, sizeof(VBOXPTE) * off);
3011 if (VBOX_FAILURE(rc))
3012 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%DV + %#x", &VarPTEAddr, sizeof(VBOXPTE) * off);
3013 for (;;)
3014 {
3015 /*
3016 * Read.
3017 */
3018 VBOXPTE Pte;
3019 rc = pCmdHlp->pfnMemRead(pCmdHlp, pVM, &Pte, sizeof(Pte), &VarPTEAddr, NULL);
3020 if (VBOX_FAILURE(rc))
3021 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Reading memory at %DV.\n", &VarPTEAddr);
3022
3023 /*
3024 * Display.
3025 */
3026 if (off != ~0U)
3027 {
3028 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%03x %DV: ", off, &VarGCPtr);
3029 off++;
3030 }
3031 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3032 "%08x 4kb phys=%08x %s %s %s %s %s avl=%02x %s %s %s %s\n",
3033 Pte.u, Pte.n.u20PageNo << PAGE_SHIFT, Pte.n.u1Present ? "p " : "np", Pte.n.u1Write ? "w" : "r",
3034 Pte.n.u1User ? "u" : "s", Pte.n.u1Accessed ? "a " : "na", Pte.n.u1Dirty ? "d " : "nd",
3035 Pte.n.u3Available, Pte.n.u1Global ? "G" : " ", Pte.n.u1WriteThru ? "pwt" : " ",
3036 Pte.n.u1CacheDisable ? "pcd" : " ", Pte.n.u1PAT ? "pat" : " ");
3037 if (VBOX_FAILURE(rc))
3038 return rc;
3039
3040 /*
3041 * Next
3042 */
3043 if (cEntries-- <= 1)
3044 break;
3045 rc = pCmdHlp->pfnEval(pCmdHlp, &VarPTEAddr, "%DV + %#x", &VarPTEAddr, sizeof(VBOXPDE));
3046 if (VBOX_FAILURE(rc))
3047 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%DV + %#x", &VarPTEAddr, sizeof(VBOXPDE));
3048 rc = pCmdHlp->pfnEval(pCmdHlp, &VarGCPtr, "%DV + %#x", &VarGCPtr, PAGE_SIZE);
3049 if (VBOX_FAILURE(rc))
3050 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%DV + %#x", &VarGCPtr, sizeof(VBOXPDE));
3051 }
3052
3053 NOREF(pResult);
3054 return VINF_SUCCESS;
3055}
3056
3057
3058/**
3059 * The 'dptb' command.
3060 *
3061 * @returns VBox status.
3062 * @param pCmd Pointer to the command descriptor (as registered).
3063 * @param pCmdHlp Pointer to command helper functions.
3064 * @param pVM Pointer to the current VM (if any).
3065 * @param paArgs Pointer to (readonly) array of arguments.
3066 * @param cArgs Number of arguments in the array.
3067 */
3068static DECLCALLBACK(int) dbgcCmdDumpPageTableBoth(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3069{
3070 if (!pVM)
3071 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
3072 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
3073 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
3074 if (VBOX_FAILURE(rc1))
3075 return rc1;
3076 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
3077 return rc2;
3078}
3079
3080
3081/**
3082 * The 'm' command.
3083 *
3084 * @returns VBox status.
3085 * @param pCmd Pointer to the command descriptor (as registered).
3086 * @param pCmdHlp Pointer to command helper functions.
3087 * @param pVM Pointer to the current VM (if any).
3088 * @param paArgs Pointer to (readonly) array of arguments.
3089 * @param cArgs Number of arguments in the array.
3090 */
3091static DECLCALLBACK(int) dbgcCmdMemoryInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3092{
3093 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Address: %DV\n", &paArgs[0]);
3094 if (!pVM)
3095 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
3096 int rc1 = pCmdHlp->pfnExec(pCmdHlp, "dpdg %DV", &paArgs[0]);
3097 int rc2 = pCmdHlp->pfnExec(pCmdHlp, "dpdh %DV", &paArgs[0]);
3098 int rc3 = pCmdHlp->pfnExec(pCmdHlp, "dptg %DV", &paArgs[0]);
3099 int rc4 = pCmdHlp->pfnExec(pCmdHlp, "dpth %DV", &paArgs[0]);
3100 if (VBOX_FAILURE(rc1))
3101 return rc1;
3102 if (VBOX_FAILURE(rc2))
3103 return rc2;
3104 if (VBOX_FAILURE(rc3))
3105 return rc3;
3106 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
3107 return rc4;
3108}
3109
3110
3111/**
3112 * Print formatted string.
3113 *
3114 * @param pHlp Pointer to this structure.
3115 * @param pszFormat The format string.
3116 * @param ... Arguments.
3117 */
3118static DECLCALLBACK(void) dbgcCmdInfo_Printf(PCDBGFINFOHLP pHlp, const char *pszFormat, ...)
3119{
3120 PDBGCCMDHLP pCmdHlp = *(PDBGCCMDHLP *)(pHlp + 1);
3121 va_list args;
3122 va_start(args, pszFormat);
3123 pCmdHlp->pfnPrintfV(pCmdHlp, NULL, pszFormat, args);
3124 va_end(args);
3125}
3126
3127
3128/**
3129 * Print formatted string.
3130 *
3131 * @param pHlp Pointer to this structure.
3132 * @param pszFormat The format string.
3133 * @param args Argument list.
3134 */
3135static DECLCALLBACK(void) dbgcCmdInfo_PrintfV(PCDBGFINFOHLP pHlp, const char *pszFormat, va_list args)
3136{
3137 PDBGCCMDHLP pCmdHlp = *(PDBGCCMDHLP *)(pHlp + 1);
3138 pCmdHlp->pfnPrintfV(pCmdHlp, NULL, pszFormat, args);
3139}
3140
3141
3142/**
3143 * The 'info' command.
3144 *
3145 * @returns VBox status.
3146 * @param pCmd Pointer to the command descriptor (as registered).
3147 * @param pCmdHlp Pointer to command helper functions.
3148 * @param pVM Pointer to the current VM (if any).
3149 * @param paArgs Pointer to (readonly) array of arguments.
3150 * @param cArgs Number of arguments in the array.
3151 */
3152static DECLCALLBACK(int) dbgcCmdInfo(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3153{
3154 /*
3155 * Validate input.
3156 */
3157 if ( cArgs < 1
3158 || cArgs > 2
3159 || paArgs[0].enmType != DBGCVAR_TYPE_STRING
3160 || paArgs[cArgs - 1].enmType != DBGCVAR_TYPE_STRING)
3161 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "internal error: The parser doesn't do its job properly yet.. quote the string.\n");
3162 if (!pVM)
3163 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: No VM.\n");
3164
3165 /*
3166 * Dump it.
3167 */
3168 struct
3169 {
3170 DBGFINFOHLP Hlp;
3171 PDBGCCMDHLP pCmdHlp;
3172 } Hlp = { { dbgcCmdInfo_Printf, dbgcCmdInfo_PrintfV }, pCmdHlp };
3173 int rc = DBGFR3Info(pVM, paArgs[0].u.pszString, cArgs == 2 ? paArgs[1].u.pszString : NULL, &Hlp.Hlp);
3174 if (VBOX_FAILURE(rc))
3175 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3Info()\n");
3176
3177 NOREF(pCmd); NOREF(pResult);
3178 return 0;
3179}
3180
3181
3182/**
3183 * The 'log' command.
3184 *
3185 * @returns VBox status.
3186 * @param pCmd Pointer to the command descriptor (as registered).
3187 * @param pCmdHlp Pointer to command helper functions.
3188 * @param pVM Pointer to the current VM (if any).
3189 * @param paArgs Pointer to (readonly) array of arguments.
3190 * @param cArgs Number of arguments in the array.
3191 */
3192static DECLCALLBACK(int) dbgcCmdLog(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3193{
3194 int rc = DBGFR3LogModifyGroups(pVM, paArgs[0].u.pszString);
3195 if (VBOX_SUCCESS(rc))
3196 return VINF_SUCCESS;
3197 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
3198 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3LogModifyGroups(%p,'%s')\n", pVM, paArgs[0].u.pszString);
3199}
3200
3201
3202/**
3203 * The 'logdest' command.
3204 *
3205 * @returns VBox status.
3206 * @param pCmd Pointer to the command descriptor (as registered).
3207 * @param pCmdHlp Pointer to command helper functions.
3208 * @param pVM Pointer to the current VM (if any).
3209 * @param paArgs Pointer to (readonly) array of arguments.
3210 * @param cArgs Number of arguments in the array.
3211 */
3212static DECLCALLBACK(int) dbgcCmdLogDest(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3213{
3214 int rc = DBGFR3LogModifyDestinations(pVM, paArgs[0].u.pszString);
3215 if (VBOX_SUCCESS(rc))
3216 return VINF_SUCCESS;
3217 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
3218 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3LogModifyDestinations(%p,'%s')\n", pVM, paArgs[0].u.pszString);
3219}
3220
3221
3222/**
3223 * The 'logflags' command.
3224 *
3225 * @returns VBox status.
3226 * @param pCmd Pointer to the command descriptor (as registered).
3227 * @param pCmdHlp Pointer to command helper functions.
3228 * @param pVM Pointer to the current VM (if any).
3229 * @param paArgs Pointer to (readonly) array of arguments.
3230 * @param cArgs Number of arguments in the array.
3231 */
3232static DECLCALLBACK(int) dbgcCmdLogFlags(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3233{
3234 int rc = DBGFR3LogModifyFlags(pVM, paArgs[0].u.pszString);
3235 if (VBOX_SUCCESS(rc))
3236 return VINF_SUCCESS;
3237 NOREF(pCmd); NOREF(cArgs); NOREF(pResult);
3238 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3LogModifyFlags(%p,'%s')\n", pVM, paArgs[0].u.pszString);
3239}
3240
3241
3242/**
3243 * The 'format' command.
3244 *
3245 * @returns VBox status.
3246 * @param pCmd Pointer to the command descriptor (as registered).
3247 * @param pCmdHlp Pointer to command helper functions.
3248 * @param pVM Pointer to the current VM (if any).
3249 * @param paArgs Pointer to (readonly) array of arguments.
3250 * @param cArgs Number of arguments in the array.
3251 */
3252static DECLCALLBACK(int) dbgcCmdFormat(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3253{
3254 LogFlow(("dbgcCmdFormat\n"));
3255 static const char *apszRangeDesc[] =
3256 {
3257 "none", "bytes", "elements"
3258 };
3259 int rc;
3260
3261 for (unsigned iArg = 0; iArg < cArgs; iArg++)
3262 {
3263 switch (paArgs[iArg].enmType)
3264 {
3265 case DBGCVAR_TYPE_UNKNOWN:
3266 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3267 "Unknown variable type!\n");
3268 break;
3269 case DBGCVAR_TYPE_GC_FLAT:
3270 if (paArgs[iArg].enmRangeType != DBGCVAR_RANGE_NONE)
3271 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3272 "Guest flat address: %%%08x range %lld %s\n",
3273 paArgs[iArg].u.GCFlat,
3274 paArgs[iArg].u64Range,
3275 apszRangeDesc[paArgs[iArg].enmRangeType]);
3276 else
3277 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3278 "Guest flat address: %%%08x\n",
3279 paArgs[iArg].u.GCFlat);
3280 break;
3281 case DBGCVAR_TYPE_GC_FAR:
3282 if (paArgs[iArg].enmRangeType != DBGCVAR_RANGE_NONE)
3283 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3284 "Guest far address: %04x:%08x range %lld %s\n",
3285 paArgs[iArg].u.GCFar.sel,
3286 paArgs[iArg].u.GCFar.off,
3287 paArgs[iArg].u64Range,
3288 apszRangeDesc[paArgs[iArg].enmRangeType]);
3289 else
3290 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3291 "Guest far address: %04x:%08x\n",
3292 paArgs[iArg].u.GCFar.sel,
3293 paArgs[iArg].u.GCFar.off);
3294 break;
3295 case DBGCVAR_TYPE_GC_PHYS:
3296 if (paArgs[iArg].enmRangeType != DBGCVAR_RANGE_NONE)
3297 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3298 "Guest physical address: %%%%%08x range %lld %s\n",
3299 paArgs[iArg].u.GCPhys,
3300 paArgs[iArg].u64Range,
3301 apszRangeDesc[paArgs[iArg].enmRangeType]);
3302 else
3303 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3304 "Guest physical address: %%%%%08x\n",
3305 paArgs[iArg].u.GCPhys);
3306 break;
3307 case DBGCVAR_TYPE_HC_FLAT:
3308 if (paArgs[iArg].enmRangeType != DBGCVAR_RANGE_NONE)
3309 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3310 "Host flat address: %%%08x range %lld %s\n",
3311 paArgs[iArg].u.pvHCFlat,
3312 paArgs[iArg].u64Range,
3313 apszRangeDesc[paArgs[iArg].enmRangeType]);
3314 else
3315 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3316 "Host flat address: %%%08x\n",
3317 paArgs[iArg].u.pvHCFlat);
3318 break;
3319 case DBGCVAR_TYPE_HC_FAR:
3320 if (paArgs[iArg].enmRangeType != DBGCVAR_RANGE_NONE)
3321 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3322 "Host far address: %04x:%08x range %lld %s\n",
3323 paArgs[iArg].u.HCFar.sel,
3324 paArgs[iArg].u.HCFar.off,
3325 paArgs[iArg].u64Range,
3326 apszRangeDesc[paArgs[iArg].enmRangeType]);
3327 else
3328 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3329 "Host far address: %04x:%08x\n",
3330 paArgs[iArg].u.HCFar.sel,
3331 paArgs[iArg].u.HCFar.off);
3332 break;
3333 case DBGCVAR_TYPE_HC_PHYS:
3334 if (paArgs[iArg].enmRangeType != DBGCVAR_RANGE_NONE)
3335 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3336 "Host physical address: %VHp range %lld %s\n",
3337 paArgs[iArg].u.HCPhys,
3338 paArgs[iArg].u64Range,
3339 apszRangeDesc[paArgs[iArg].enmRangeType]);
3340 else
3341 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3342 "Host physical address: %VHp\n",
3343 paArgs[iArg].u.HCPhys);
3344 break;
3345
3346 case DBGCVAR_TYPE_STRING:
3347 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3348 "String, %lld bytes long: %s\n",
3349 paArgs[iArg].u64Range,
3350 paArgs[iArg].u.pszString);
3351 break;
3352
3353 case DBGCVAR_TYPE_NUMBER:
3354 if (paArgs[iArg].enmRangeType != DBGCVAR_RANGE_NONE)
3355 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3356 "Number: hex %llx dec 0i%lld oct 0t%llo range %lld %s\n",
3357 paArgs[iArg].u.u64Number,
3358 paArgs[iArg].u.u64Number,
3359 paArgs[iArg].u.u64Number,
3360 paArgs[iArg].u64Range,
3361 apszRangeDesc[paArgs[iArg].enmRangeType]);
3362 else
3363 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3364 "Number: hex %llx dec 0i%lld oct 0t%llo\n",
3365 paArgs[iArg].u.u64Number,
3366 paArgs[iArg].u.u64Number,
3367 paArgs[iArg].u.u64Number);
3368 break;
3369
3370 default:
3371 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3372 "Invalid argument type %d\n",
3373 paArgs[iArg].enmType);
3374 break;
3375 }
3376 } /* arg loop */
3377
3378 NOREF(pCmd); NOREF(pVM); NOREF(pResult);
3379 return 0;
3380}
3381
3382
3383/**
3384 * List near symbol.
3385 *
3386 * @returns VBox status code.
3387 * @param pCmdHlp Pointer to command helper functions.
3388 * @param pVM Pointer to the current VM (if any).
3389 * @param pArg Pointer to the address or symbol to lookup.
3390 */
3391static int dbgcDoListNear(PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pArg, PDBGCVAR pResult)
3392{
3393 dbgcVarSetGCFlat(pResult, 0);
3394
3395 DBGFSYMBOL Symbol;
3396 int rc;
3397 if (pArg->enmType == DBGCVAR_TYPE_SYMBOL)
3398 {
3399 /*
3400 * Lookup the symbol address.
3401 */
3402 rc = DBGFR3SymbolByName(pVM, pArg->u.pszString, &Symbol);
3403 if (VBOX_FAILURE(rc))
3404 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3SymbolByName(, %s,)\n", pArg->u.pszString);
3405
3406 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%VGv %s\n", (RTGCUINTPTR)Symbol.Value, Symbol.szName); /** @todo remove the RTUINGCPTR cast once DBGF got correct interfaces! */
3407 dbgcVarSetGCFlatByteRange(pResult, Symbol.Value, Symbol.cb);
3408 }
3409 else
3410 {
3411 /*
3412 * Convert it to a flat GC address and lookup that address.
3413 */
3414 DBGCVAR AddrVar;
3415 rc = pCmdHlp->pfnEval(pCmdHlp, &AddrVar, "%%(%DV)", pArg);
3416 if (VBOX_FAILURE(rc))
3417 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(%DV)\n", pArg);
3418
3419 dbgcVarSetVar(pResult, &AddrVar);
3420
3421 RTGCINTPTR offDisp = 0;
3422 rc = DBGFR3SymbolByAddr(pVM, AddrVar.u.GCFlat, &offDisp, &Symbol);
3423 if (VBOX_FAILURE(rc))
3424 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGFR3SymbolByAddr(, %VGv,,)\n", AddrVar.u.GCFlat);
3425
3426 if (!offDisp)
3427 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s", &AddrVar, Symbol.szName);
3428 else if (offDisp > 0)
3429 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s + %RGv", &AddrVar, Symbol.szName, offDisp);
3430 else
3431 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%DV %s - %RGv", &AddrVar, Symbol.szName, -offDisp);
3432 if ((RTGCINTPTR)Symbol.cb > -offDisp)
3433 {
3434 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, " LB %RGv\n", Symbol.cb + offDisp);
3435 dbgcVarSetByteRange(pResult, Symbol.cb + offDisp);
3436 }
3437 else
3438 {
3439 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\n");
3440 dbgcVarSetNoRange(pResult);
3441 }
3442 }
3443
3444 return rc;
3445}
3446
3447
3448/**
3449 * The 'ln' (listnear) command.
3450 *
3451 * @returns VBox status.
3452 * @param pCmd Pointer to the command descriptor (as registered).
3453 * @param pCmdHlp Pointer to command helper functions.
3454 * @param pVM Pointer to the current VM (if any).
3455 * @param paArgs Pointer to (readonly) array of arguments.
3456 * @param cArgs Number of arguments in the array.
3457 */
3458static DECLCALLBACK(int) dbgcCmdListNear(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3459{
3460 dbgcVarSetGCFlat(pResult, 0);
3461 if (!cArgs)
3462 {
3463 /*
3464 * Current cs:eip symbol.
3465 */
3466 DBGCVAR AddrVar;
3467 int rc = pCmdHlp->pfnEval(pCmdHlp, &AddrVar, "%%(cs:eip)");
3468 if (VBOX_FAILURE(rc))
3469 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "%%(cs:eip)\n");
3470 return dbgcDoListNear(pCmdHlp, pVM, &AddrVar, pResult);
3471 }
3472
3473 /*
3474 * Iterate arguments.
3475 */
3476 for (unsigned iArg = 0; iArg < cArgs; iArg++)
3477 {
3478 int rc = dbgcDoListNear(pCmdHlp, pVM, &paArgs[iArg], pResult);
3479 if (VBOX_FAILURE(rc))
3480 return rc;
3481 }
3482
3483 NOREF(pCmd); NOREF(pResult);
3484 return VINF_SUCCESS;
3485}
3486
3487
3488/**
3489 * The 'loadsyms' command.
3490 *
3491 * @returns VBox status.
3492 * @param pCmd Pointer to the command descriptor (as registered).
3493 * @param pCmdHlp Pointer to command helper functions.
3494 * @param pVM Pointer to the current VM (if any).
3495 * @param paArgs Pointer to (readonly) array of arguments.
3496 * @param cArgs Number of arguments in the array.
3497 */
3498static DECLCALLBACK(int) dbgcCmdLoadSyms(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3499{
3500 /*
3501 * Validate the parsing and make sense of the input.
3502 * This is a mess as usual because we don't trust the parser yet.
3503 */
3504 if ( cArgs < 1
3505 || paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3506 {
3507 AssertMsgFailed(("Parse error, first argument required to be string!\n"));
3508 return VERR_PARSE_INCORRECT_ARG_TYPE;
3509 }
3510 DBGCVAR AddrVar;
3511 RTGCUINTPTR Delta = 0;
3512 const char *pszModule = NULL;
3513 RTGCUINTPTR ModuleAddress = 0;
3514 unsigned cbModule = 0;
3515 if (cArgs > 1)
3516 {
3517 unsigned iArg = 1;
3518 if (paArgs[iArg].enmType == DBGCVAR_TYPE_NUMBER)
3519 {
3520 Delta = (RTGCUINTPTR)paArgs[iArg].u.u64Number;
3521 iArg++;
3522 }
3523 if (iArg < cArgs)
3524 {
3525 if (paArgs[iArg].enmType != DBGCVAR_TYPE_STRING)
3526 {
3527 AssertMsgFailed(("Parse error, module argument required to be string!\n"));
3528 return VERR_PARSE_INCORRECT_ARG_TYPE;
3529 }
3530 pszModule = paArgs[iArg].u.pszString;
3531 iArg++;
3532 if (iArg < cArgs)
3533 {
3534 if (DBGCVAR_ISPOINTER(paArgs[iArg].enmType))
3535 {
3536 AssertMsgFailed(("Parse error, module argument required to be GC pointer!\n"));
3537 return VERR_PARSE_INCORRECT_ARG_TYPE;
3538 }
3539 int rc = pCmdHlp->pfnEval(pCmdHlp, &AddrVar, "%%(%Dv)", &paArgs[iArg]);
3540 if (VBOX_FAILURE(rc))
3541 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Module address cast %%(%Dv) failed.", &paArgs[iArg]);
3542 ModuleAddress = paArgs[iArg].u.GCFlat;
3543 iArg++;
3544 if (iArg < cArgs)
3545 {
3546 if (paArgs[iArg].enmType != DBGCVAR_TYPE_NUMBER)
3547 {
3548 AssertMsgFailed(("Parse error, module argument required to be an interger!\n"));
3549 return VERR_PARSE_INCORRECT_ARG_TYPE;
3550 }
3551 cbModule = (unsigned)paArgs[iArg].u.u64Number;
3552 iArg++;
3553 if (iArg < cArgs)
3554 {
3555 AssertMsgFailed(("Parse error, too many arguments!\n"));
3556 return VERR_PARSE_TOO_MANY_ARGUMENTS;
3557 }
3558 }
3559 }
3560 }
3561 }
3562
3563 /*
3564 * Call the debug info manager about this loading...
3565 */
3566 int rc = DBGFR3ModuleLoad(pVM, paArgs[0].u.pszString, Delta, pszModule, ModuleAddress, cbModule);
3567 if (VBOX_FAILURE(rc))
3568 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "DBGInfoSymbolLoad(, '%s', %VGv, '%s', %VGv, 0)\n",
3569 paArgs[0].u.pszString, Delta, pszModule, ModuleAddress);
3570
3571 NOREF(pCmd); NOREF(pResult);
3572 return VINF_SUCCESS;
3573}
3574
3575
3576/**
3577 * The 'set' command.
3578 *
3579 * @returns VBox status.
3580 * @param pCmd Pointer to the command descriptor (as registered).
3581 * @param pCmdHlp Pointer to command helper functions.
3582 * @param pVM Pointer to the current VM (if any).
3583 * @param paArgs Pointer to (readonly) array of arguments.
3584 * @param cArgs Number of arguments in the array.
3585 */
3586static DECLCALLBACK(int) dbgcCmdSet(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3587{
3588 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3589
3590 /* parse sanity check. */
3591 AssertMsg(paArgs[0].enmType == DBGCVAR_TYPE_STRING, ("expected string not %d as first arg!\n", paArgs[0].enmType));
3592 if (paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3593 return VERR_PARSE_INCORRECT_ARG_TYPE;
3594
3595
3596 /*
3597 * A variable must start with an alpha chars and only contain alpha numerical chars.
3598 */
3599 const char *pszVar = paArgs[0].u.pszString;
3600 if (!isalpha(*pszVar) || *pszVar == '_')
3601 return pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3602 "syntax error: Invalid variable name '%s'. Variable names must match regex '[_a-zA-Z][_a-zA-Z0-9*'!", paArgs[0].u.pszString);
3603
3604 while (isalnum(*pszVar) || *pszVar == '_')
3605 *pszVar++;
3606 if (*pszVar)
3607 return pCmdHlp->pfnPrintf(pCmdHlp, NULL,
3608 "syntax error: Invalid variable name '%s'. Variable names must match regex '[_a-zA-Z][_a-zA-Z0-9*]'!", paArgs[0].u.pszString);
3609
3610
3611 /*
3612 * Calc variable size.
3613 */
3614 size_t cbVar = (size_t)paArgs[0].u64Range + sizeof(DBGCNAMEDVAR);
3615 if (paArgs[1].enmType == DBGCVAR_TYPE_STRING)
3616 cbVar += 1 + (size_t)paArgs[1].u64Range;
3617
3618 /*
3619 * Look for existing one.
3620 */
3621 pszVar = paArgs[0].u.pszString;
3622 for (unsigned iVar = 0; iVar < pDbgc->cVars; iVar++)
3623 {
3624 if (!strcmp(pszVar, pDbgc->papVars[iVar]->szName))
3625 {
3626 /*
3627 * Update existing variable.
3628 */
3629 void *pv = RTMemRealloc(pDbgc->papVars[iVar], cbVar);
3630 if (!pv)
3631 return VERR_PARSE_NO_MEMORY;
3632 PDBGCNAMEDVAR pVar = pDbgc->papVars[iVar] = (PDBGCNAMEDVAR)pv;
3633
3634 pVar->Var = paArgs[1];
3635 memcpy(pVar->szName, paArgs[0].u.pszString, (size_t)paArgs[0].u64Range + 1);
3636 if (paArgs[1].enmType == DBGCVAR_TYPE_STRING)
3637 pVar->Var.u.pszString = (char *)memcpy(&pVar->szName[paArgs[0].u64Range + 1], paArgs[1].u.pszString, (size_t)paArgs[1].u64Range + 1);
3638 return 0;
3639 }
3640 }
3641
3642 /*
3643 * Allocate another.
3644 */
3645 PDBGCNAMEDVAR pVar = (PDBGCNAMEDVAR)RTMemAlloc(cbVar);
3646
3647 pVar->Var = paArgs[1];
3648 memcpy(pVar->szName, pszVar, (size_t)paArgs[0].u64Range + 1);
3649 if (paArgs[1].enmType == DBGCVAR_TYPE_STRING)
3650 pVar->Var.u.pszString = (char *)memcpy(&pVar->szName[paArgs[0].u64Range + 1], paArgs[1].u.pszString, (size_t)paArgs[1].u64Range + 1);
3651
3652 /* need to reallocate the pointer array too? */
3653 if (!(pDbgc->cVars % 0x20))
3654 {
3655 void *pv = RTMemRealloc(pDbgc->papVars, (pDbgc->cVars + 0x20) * sizeof(pDbgc->papVars[0]));
3656 if (!pv)
3657 {
3658 RTMemFree(pVar);
3659 return VERR_PARSE_NO_MEMORY;
3660 }
3661 pDbgc->papVars = (PDBGCNAMEDVAR *)pv;
3662 }
3663 pDbgc->papVars[pDbgc->cVars++] = pVar;
3664
3665 NOREF(pCmd); NOREF(pVM); NOREF(cArgs); NOREF(pResult);
3666 return 0;
3667}
3668
3669
3670/**
3671 * The 'unset' command.
3672 *
3673 * @returns VBox status.
3674 * @param pCmd Pointer to the command descriptor (as registered).
3675 * @param pCmdHlp Pointer to command helper functions.
3676 * @param pVM Pointer to the current VM (if any).
3677 * @param paArgs Pointer to (readonly) array of arguments.
3678 * @param cArgs Number of arguments in the array.
3679 */
3680static DECLCALLBACK(int) dbgcCmdUnset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3681{
3682 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3683
3684 /*
3685 * Don't trust the parser.
3686 */
3687 for (unsigned i = 0; i < cArgs; i++)
3688 if (paArgs[i].enmType != DBGCVAR_TYPE_STRING)
3689 {
3690 AssertMsgFailed(("expected strings only. (arg=%d)!\n", i));
3691 return VERR_PARSE_INCORRECT_ARG_TYPE;
3692 }
3693
3694 /*
3695 * Iterate the variables and unset them.
3696 */
3697 for (unsigned iArg = 0; iArg < cArgs; iArg++)
3698 {
3699 const char *pszVar = paArgs[iArg].u.pszString;
3700
3701 /*
3702 * Look up the variable.
3703 */
3704 for (unsigned iVar = 0; iVar < pDbgc->cVars; iVar++)
3705 {
3706 if (!strcmp(pszVar, pDbgc->papVars[iVar]->szName))
3707 {
3708 /*
3709 * Shuffle the array removing this entry.
3710 */
3711 void *pvFree = pDbgc->papVars[iVar];
3712 if (iVar + 1 < pDbgc->cVars)
3713 memmove(&pDbgc->papVars[iVar],
3714 &pDbgc->papVars[iVar + 1],
3715 (pDbgc->cVars - iVar - 1) * sizeof(pDbgc->papVars[0]));
3716 pDbgc->papVars[--pDbgc->cVars] = NULL;
3717
3718 RTMemFree(pvFree);
3719 }
3720 } /* lookup */
3721 } /* arg loop */
3722
3723 NOREF(pCmd); NOREF(pVM); NOREF(pResult);
3724 return 0;
3725}
3726
3727
3728/**
3729 * The 'loadvars' command.
3730 *
3731 * @returns VBox status.
3732 * @param pCmd Pointer to the command descriptor (as registered).
3733 * @param pCmdHlp Pointer to command helper functions.
3734 * @param pVM Pointer to the current VM (if any).
3735 * @param paArgs Pointer to (readonly) array of arguments.
3736 * @param cArgs Number of arguments in the array.
3737 */
3738static DECLCALLBACK(int) dbgcCmdLoadVars(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3739{
3740 /*
3741 * Don't trust the parser.
3742 */
3743 if ( cArgs != 1
3744 || paArgs[0].enmType != DBGCVAR_TYPE_STRING)
3745 {
3746 AssertMsgFailed(("Expected one string exactly!\n"));
3747 return VERR_PARSE_INCORRECT_ARG_TYPE;
3748 }
3749
3750 /*
3751 * Iterate the variables and unset them.
3752 */
3753 FILE *pFile = fopen(paArgs[0].u.pszString, "r");
3754 if (pFile)
3755 {
3756 char szLine[4096];
3757 while (fgets(szLine, sizeof(szLine), pFile))
3758 {
3759 /* Strip it. */
3760 char *psz = szLine;
3761 while (isblank(*psz))
3762 psz++;
3763 int i = strlen(psz) - 1;
3764 while (i >= 0 && isspace(psz[i]))
3765 psz[i--] ='\0';
3766 /* Execute it if not comment or empty line. */
3767 if ( *psz != '\0'
3768 && *psz != '#'
3769 && *psz != ';')
3770 {
3771 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "dbg: set %s", psz);
3772 pCmdHlp->pfnExec(pCmdHlp, "set %s", psz);
3773 }
3774 }
3775 fclose(pFile);
3776 }
3777 else
3778 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Failed to open file '%s'.\n", paArgs[0].u.pszString);
3779
3780 NOREF(pCmd); NOREF(pVM); NOREF(pResult);
3781 return 0;
3782}
3783
3784
3785/**
3786 * The 'showvars' command.
3787 *
3788 * @returns VBox status.
3789 * @param pCmd Pointer to the command descriptor (as registered).
3790 * @param pCmdHlp Pointer to command helper functions.
3791 * @param pVM Pointer to the current VM (if any).
3792 * @param paArgs Pointer to (readonly) array of arguments.
3793 * @param cArgs Number of arguments in the array.
3794 */
3795static DECLCALLBACK(int) dbgcCmdShowVars(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3796{
3797 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3798
3799 for (unsigned iVar = 0; iVar < pDbgc->cVars; iVar++)
3800 {
3801 int rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "%-20s ", &pDbgc->papVars[iVar]->szName);
3802 if (!rc)
3803 rc = dbgcCmdFormat(pCmd, pCmdHlp, pVM, &pDbgc->papVars[iVar]->Var, 1, NULL);
3804 if (rc)
3805 return rc;
3806 }
3807
3808 NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
3809 return 0;
3810}
3811
3812
3813/**
3814 * The 'harakiri' command.
3815 *
3816 * @returns VBox status.
3817 * @param pCmd Pointer to the command descriptor (as registered).
3818 * @param pCmdHlp Pointer to command helper functions.
3819 * @param pVM Pointer to the current VM (if any).
3820 * @param paArgs Pointer to (readonly) array of arguments.
3821 * @param cArgs Number of arguments in the array.
3822 */
3823static DECLCALLBACK(int) dbgcCmdHarakiri(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3824{
3825 Log(("dbgcCmdHarakiri\n"));
3826 for (;;)
3827 exit(126);
3828 NOREF(pCmd); NOREF(pCmdHlp); NOREF(pVM); NOREF(paArgs); NOREF(cArgs); NOREF(pResult);
3829}
3830
3831
3832
3833
3834
3835//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
3836//
3837//
3838// B u l t i n S y m b o l s
3839//
3840//
3841//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
3842
3843
3844
3845/**
3846 * Get builtin register symbol.
3847 *
3848 * The uUser is special for these symbol descriptors. See the SYMREG_* \#defines.
3849 *
3850 * @returns 0 on success.
3851 * @returns VBox evaluation / parsing error code on failure.
3852 * The caller does the bitching.
3853 * @param pSymDesc Pointer to the symbol descriptor.
3854 * @param pCmdHlp Pointer to the command callback structure.
3855 * @param enmType The result type.
3856 * @param pResult Where to store the result.
3857 */
3858static DECLCALLBACK(int) dbgcSymGetReg(PCDBGCSYM pSymDesc, PDBGCCMDHLP pCmdHlp, DBGCVARTYPE enmType, PDBGCVAR pResult)
3859{
3860 LogFlow(("dbgcSymSetReg: pSymDesc->pszName=%d\n", pSymDesc->pszName));
3861
3862 /*
3863 * pVM is required.
3864 */
3865 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
3866 Assert(pDbgc->pVM);
3867
3868 /*
3869 * Get the right CPU context.
3870 */
3871 PCPUMCTX pCtx;
3872 int rc;
3873 if (!(pSymDesc->uUser & SYMREG_FLAGS_HYPER))
3874 rc = CPUMQueryGuestCtxPtr(pDbgc->pVM, &pCtx);
3875 else
3876 rc = CPUMQueryHyperCtxPtr(pDbgc->pVM, &pCtx);
3877 if (VBOX_FAILURE(rc))
3878 return rc;
3879
3880 /*
3881 * Get the value.
3882 */
3883 void *pvValue = (char *)pCtx + SYMREG_OFFSET(pSymDesc->uUser);
3884 uint64_t u64;
3885 switch (SYMREG_SIZE(pSymDesc->uUser))
3886 {
3887 case 1: u64 = *(uint8_t *)pvValue; break;
3888 case 2: u64 = *(uint16_t *)pvValue; break;
3889 case 4: u64 = *(uint32_t *)pvValue; break;
3890 case 6: u64 = *(uint32_t *)pvValue | ((uint64_t)*(uint16_t *)((char *)pvValue + sizeof(uint32_t)) << 32); break;
3891 case 8: u64 = *(uint64_t *)pvValue; break;
3892 default:
3893 return VERR_PARSE_NOT_IMPLEMENTED;
3894 }
3895
3896 /*
3897 * Construct the desired result.
3898 */
3899 if (enmType == DBGCVAR_TYPE_ANY)
3900 enmType = DBGCVAR_TYPE_NUMBER;
3901 pResult->pDesc = NULL;
3902 pResult->pNext = NULL;
3903 pResult->enmType = enmType;
3904 pResult->enmRangeType = DBGCVAR_RANGE_NONE;
3905 pResult->u64Range = 0;
3906
3907 switch (enmType)
3908 {
3909 case DBGCVAR_TYPE_GC_FLAT:
3910 pResult->u.GCFlat = (RTGCPTR)u64;
3911 break;
3912
3913 case DBGCVAR_TYPE_GC_FAR:
3914 switch (SYMREG_SIZE(pSymDesc->uUser))
3915 {
3916 case 4:
3917 if (!(pSymDesc->uUser & SYMREG_FLAGS_HIGH_SEL))
3918 {
3919 pResult->u.GCFar.off = (uint16_t)u64;
3920 pResult->u.GCFar.sel = (uint16_t)(u64 >> 16);
3921 }
3922 else
3923 {
3924 pResult->u.GCFar.sel = (uint16_t)u64;
3925 pResult->u.GCFar.off = (uint16_t)(u64 >> 16);
3926 }
3927 break;
3928 case 6:
3929 if (!(pSymDesc->uUser & SYMREG_FLAGS_HIGH_SEL))
3930 {
3931 pResult->u.GCFar.off = (uint32_t)u64;
3932 pResult->u.GCFar.sel = (uint16_t)(u64 >> 32);
3933 }
3934 else
3935 {
3936 pResult->u.GCFar.sel = (uint32_t)u64;
3937 pResult->u.GCFar.off = (uint16_t)(u64 >> 32);
3938 }
3939 break;
3940
3941 default:
3942 return VERR_PARSE_BAD_RESULT_TYPE;
3943 }
3944 break;
3945
3946 case DBGCVAR_TYPE_GC_PHYS:
3947 pResult->u.GCPhys = (RTGCPHYS)u64;
3948 break;
3949
3950 case DBGCVAR_TYPE_HC_FLAT:
3951 pResult->u.pvHCFlat = (void *)(uintptr_t)u64;
3952 break;
3953
3954 case DBGCVAR_TYPE_HC_FAR:
3955 switch (SYMREG_SIZE(pSymDesc->uUser))
3956 {
3957 case 4:
3958 if (!(pSymDesc->uUser & SYMREG_FLAGS_HIGH_SEL))
3959 {
3960 pResult->u.HCFar.off = (uint16_t)u64;
3961 pResult->u.HCFar.sel = (uint16_t)(u64 >> 16);
3962 }
3963 else
3964 {
3965 pResult->u.HCFar.sel = (uint16_t)u64;
3966 pResult->u.HCFar.off = (uint16_t)(u64 >> 16);
3967 }
3968 break;
3969 case 6:
3970 if (!(pSymDesc->uUser & SYMREG_FLAGS_HIGH_SEL))
3971 {
3972 pResult->u.HCFar.off = (uint32_t)u64;
3973 pResult->u.HCFar.sel = (uint16_t)(u64 >> 32);
3974 }
3975 else
3976 {
3977 pResult->u.HCFar.sel = (uint32_t)u64;
3978 pResult->u.HCFar.off = (uint16_t)(u64 >> 32);
3979 }
3980 break;
3981
3982 default:
3983 return VERR_PARSE_BAD_RESULT_TYPE;
3984 }
3985 break;
3986
3987 case DBGCVAR_TYPE_HC_PHYS:
3988 pResult->u.GCPhys = (RTGCPHYS)u64;
3989 break;
3990
3991 case DBGCVAR_TYPE_NUMBER:
3992 pResult->u.u64Number = u64;
3993 break;
3994
3995 case DBGCVAR_TYPE_STRING:
3996 case DBGCVAR_TYPE_UNKNOWN:
3997 default:
3998 return VERR_PARSE_BAD_RESULT_TYPE;
3999
4000 }
4001
4002 return 0;
4003}
4004
4005
4006/**
4007 * Set builtin register symbol.
4008 *
4009 * The uUser is special for these symbol descriptors. See the SYMREG_* #defines.
4010 *
4011 * @returns 0 on success.
4012 * @returns VBox evaluation / parsing error code on failure.
4013 * The caller does the bitching.
4014 * @param pSymDesc Pointer to the symbol descriptor.
4015 * @param pCmdHlp Pointer to the command callback structure.
4016 * @param pValue The value to assign the symbol.
4017 */
4018static DECLCALLBACK(int) dbgcSymSetReg(PCDBGCSYM pSymDesc, PDBGCCMDHLP pCmdHlp, PCDBGCVAR pValue)
4019{
4020 LogFlow(("dbgcSymSetReg: pSymDesc->pszName=%d\n", pSymDesc->pszName));
4021
4022 /*
4023 * pVM is required.
4024 */
4025 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
4026 Assert(pDbgc->pVM);
4027
4028 /*
4029 * Get the right CPU context.
4030 */
4031 PCPUMCTX pCtx;
4032 int rc;
4033 if (!(pSymDesc->uUser & SYMREG_FLAGS_HYPER))
4034 rc = CPUMQueryGuestCtxPtr(pDbgc->pVM, &pCtx);
4035 else
4036 rc = CPUMQueryHyperCtxPtr(pDbgc->pVM, &pCtx);
4037 if (VBOX_FAILURE(rc))
4038 return rc;
4039
4040 /*
4041 * Check the new value.
4042 */
4043 if (pValue->enmType != DBGCVAR_TYPE_NUMBER)
4044 return VERR_PARSE_ARGUMENT_TYPE_MISMATCH;
4045
4046 /*
4047 * Set the value.
4048 */
4049 void *pvValue = (char *)pCtx + SYMREG_OFFSET(pSymDesc->uUser);
4050 switch (SYMREG_SIZE(pSymDesc->uUser))
4051 {
4052 case 1:
4053 *(uint8_t *)pvValue = (uint8_t)pValue->u.u64Number;
4054 break;
4055 case 2:
4056 *(uint16_t *)pvValue = (uint16_t)pValue->u.u64Number;
4057 break;
4058 case 4:
4059 *(uint32_t *)pvValue = (uint32_t)pValue->u.u64Number;
4060 break;
4061 case 6:
4062 *(uint32_t *)pvValue = (uint32_t)pValue->u.u64Number;
4063 ((uint16_t *)pvValue)[3] = (uint16_t)(pValue->u.u64Number >> 32);
4064 break;
4065 case 8:
4066 *(uint64_t *)pvValue = pValue->u.u64Number;
4067 break;
4068 default:
4069 return VERR_PARSE_NOT_IMPLEMENTED;
4070 }
4071
4072 return VINF_SUCCESS;
4073}
4074
4075
4076
4077
4078
4079
4080
4081//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
4082//
4083//
4084// O p e r a t o r s
4085//
4086//
4087//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
4088
4089
4090/**
4091 * Minus (unary).
4092 *
4093 * @returns 0 on success.
4094 * @returns VBox evaluation / parsing error code on failure.
4095 * The caller does the bitching.
4096 * @param pDbgc Debugger console instance data.
4097 * @param pArg The argument.
4098 * @param pResult Where to store the result.
4099 */
4100static DECLCALLBACK(int) dbgcOpMinus(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4101{
4102// LogFlow(("dbgcOpMinus\n"));
4103 *pResult = *pArg;
4104 switch (pArg->enmType)
4105 {
4106 case DBGCVAR_TYPE_GC_FLAT:
4107 pResult->u.GCFlat = -(RTGCINTPTR)pResult->u.GCFlat;
4108 break;
4109 case DBGCVAR_TYPE_GC_FAR:
4110 pResult->u.GCFar.off = -(int32_t)pResult->u.GCFar.off;
4111 break;
4112 case DBGCVAR_TYPE_GC_PHYS:
4113 pResult->u.GCPhys = (RTGCPHYS) -(int64_t)pResult->u.GCPhys;
4114 break;
4115 case DBGCVAR_TYPE_HC_FLAT:
4116 pResult->u.pvHCFlat = (void *) -(intptr_t)pResult->u.pvHCFlat;
4117 break;
4118 case DBGCVAR_TYPE_HC_FAR:
4119 pResult->u.HCFar.off = -(int32_t)pResult->u.HCFar.off;
4120 break;
4121 case DBGCVAR_TYPE_HC_PHYS:
4122 pResult->u.HCPhys = (RTHCPHYS) -(int64_t)pResult->u.HCPhys;
4123 break;
4124 case DBGCVAR_TYPE_NUMBER:
4125 pResult->u.u64Number = -(int64_t)pResult->u.u64Number;
4126 break;
4127
4128 case DBGCVAR_TYPE_UNKNOWN:
4129 case DBGCVAR_TYPE_STRING:
4130 default:
4131 return VERR_PARSE_INCORRECT_ARG_TYPE;
4132 }
4133 NOREF(pDbgc);
4134 return 0;
4135}
4136
4137
4138/**
4139 * Pluss (unary).
4140 *
4141 * @returns 0 on success.
4142 * @returns VBox evaluation / parsing error code on failure.
4143 * The caller does the bitching.
4144 * @param pDbgc Debugger console instance data.
4145 * @param pArg The argument.
4146 * @param pResult Where to store the result.
4147 */
4148static DECLCALLBACK(int) dbgcOpPluss(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4149{
4150// LogFlow(("dbgcOpPluss\n"));
4151 *pResult = *pArg;
4152 switch (pArg->enmType)
4153 {
4154 case DBGCVAR_TYPE_GC_FLAT:
4155 case DBGCVAR_TYPE_GC_FAR:
4156 case DBGCVAR_TYPE_GC_PHYS:
4157 case DBGCVAR_TYPE_HC_FLAT:
4158 case DBGCVAR_TYPE_HC_FAR:
4159 case DBGCVAR_TYPE_HC_PHYS:
4160 case DBGCVAR_TYPE_NUMBER:
4161 break;
4162
4163 case DBGCVAR_TYPE_UNKNOWN:
4164 case DBGCVAR_TYPE_STRING:
4165 default:
4166 return VERR_PARSE_INCORRECT_ARG_TYPE;
4167 }
4168 NOREF(pDbgc);
4169 return 0;
4170}
4171
4172
4173/**
4174 * Boolean not (unary).
4175 *
4176 * @returns 0 on success.
4177 * @returns VBox evaluation / parsing error code on failure.
4178 * The caller does the bitching.
4179 * @param pDbgc Debugger console instance data.
4180 * @param pArg The argument.
4181 * @param pResult Where to store the result.
4182 */
4183static DECLCALLBACK(int) dbgcOpBooleanNot(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4184{
4185// LogFlow(("dbgcOpBooleanNot\n"));
4186 *pResult = *pArg;
4187 switch (pArg->enmType)
4188 {
4189 case DBGCVAR_TYPE_GC_FLAT:
4190 pResult->u.u64Number = !pResult->u.GCFlat;
4191 break;
4192 case DBGCVAR_TYPE_GC_FAR:
4193 pResult->u.u64Number = !pResult->u.GCFar.off && pResult->u.GCFar.sel <= 3;
4194 break;
4195 case DBGCVAR_TYPE_GC_PHYS:
4196 pResult->u.u64Number = !pResult->u.GCPhys;
4197 break;
4198 case DBGCVAR_TYPE_HC_FLAT:
4199 pResult->u.u64Number = !pResult->u.pvHCFlat;
4200 break;
4201 case DBGCVAR_TYPE_HC_FAR:
4202 pResult->u.u64Number = !pResult->u.HCFar.off && pResult->u.HCFar.sel <= 3;
4203 break;
4204 case DBGCVAR_TYPE_HC_PHYS:
4205 pResult->u.u64Number = !pResult->u.HCPhys;
4206 break;
4207 case DBGCVAR_TYPE_NUMBER:
4208 pResult->u.u64Number = !pResult->u.u64Number;
4209 break;
4210 case DBGCVAR_TYPE_STRING:
4211 pResult->u.u64Number = !pResult->u64Range;
4212 break;
4213
4214 case DBGCVAR_TYPE_UNKNOWN:
4215 default:
4216 return VERR_PARSE_INCORRECT_ARG_TYPE;
4217 }
4218 pResult->enmType = DBGCVAR_TYPE_NUMBER;
4219 NOREF(pDbgc);
4220 return 0;
4221}
4222
4223
4224/**
4225 * Bitwise not (unary).
4226 *
4227 * @returns 0 on success.
4228 * @returns VBox evaluation / parsing error code on failure.
4229 * The caller does the bitching.
4230 * @param pDbgc Debugger console instance data.
4231 * @param pArg The argument.
4232 * @param pResult Where to store the result.
4233 */
4234static DECLCALLBACK(int) dbgcOpBitwiseNot(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4235{
4236// LogFlow(("dbgcOpBitwiseNot\n"));
4237 *pResult = *pArg;
4238 switch (pArg->enmType)
4239 {
4240 case DBGCVAR_TYPE_GC_FLAT:
4241 pResult->u.GCFlat = ~pResult->u.GCFlat;
4242 break;
4243 case DBGCVAR_TYPE_GC_FAR:
4244 pResult->u.GCFar.off = ~pResult->u.GCFar.off;
4245 break;
4246 case DBGCVAR_TYPE_GC_PHYS:
4247 pResult->u.GCPhys = ~pResult->u.GCPhys;
4248 break;
4249 case DBGCVAR_TYPE_HC_FLAT:
4250 pResult->u.pvHCFlat = (void *)~(uintptr_t)pResult->u.pvHCFlat;
4251 break;
4252 case DBGCVAR_TYPE_HC_FAR:
4253 pResult->u.HCFar.off= ~pResult->u.HCFar.off;
4254 break;
4255 case DBGCVAR_TYPE_HC_PHYS:
4256 pResult->u.HCPhys = ~pResult->u.HCPhys;
4257 break;
4258 case DBGCVAR_TYPE_NUMBER:
4259 pResult->u.u64Number = ~pResult->u.u64Number;
4260 break;
4261
4262 case DBGCVAR_TYPE_UNKNOWN:
4263 case DBGCVAR_TYPE_STRING:
4264 default:
4265 return VERR_PARSE_INCORRECT_ARG_TYPE;
4266 }
4267 NOREF(pDbgc);
4268 return 0;
4269}
4270
4271
4272/**
4273 * Reference variable (unary).
4274 *
4275 * @returns 0 on success.
4276 * @returns VBox evaluation / parsing error code on failure.
4277 * The caller does the bitching.
4278 * @param pDbgc Debugger console instance data.
4279 * @param pArg The argument.
4280 * @param pResult Where to store the result.
4281 */
4282static DECLCALLBACK(int) dbgcOpVar(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4283{
4284// LogFlow(("dbgcOpVar: %s\n", pArg->u.pszString));
4285 /*
4286 * Parse sanity.
4287 */
4288 if (pArg->enmType != DBGCVAR_TYPE_STRING)
4289 return VERR_PARSE_INCORRECT_ARG_TYPE;
4290
4291 /*
4292 * Lookup the variable.
4293 */
4294 const char *pszVar = pArg->u.pszString;
4295 for (unsigned iVar = 0; iVar < pDbgc->cVars; iVar++)
4296 {
4297 if (!strcmp(pszVar, pDbgc->papVars[iVar]->szName))
4298 {
4299 *pResult = pDbgc->papVars[iVar]->Var;
4300 return 0;
4301 }
4302 }
4303
4304 return VERR_PARSE_VARIABLE_NOT_FOUND;
4305}
4306
4307
4308/**
4309 * Flat address (unary).
4310 *
4311 * @returns 0 on success.
4312 * @returns VBox evaluation / parsing error code on failure.
4313 * The caller does the bitching.
4314 * @param pDbgc Debugger console instance data.
4315 * @param pArg The argument.
4316 * @param pResult Where to store the result.
4317 */
4318static DECLCALLBACK(int) dbgcOpAddrFlat(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4319{
4320// LogFlow(("dbgcOpAddrFlat\n"));
4321 int rc;
4322 *pResult = *pArg;
4323 switch (pArg->enmType)
4324 {
4325 case DBGCVAR_TYPE_GC_FLAT:
4326 return 0;
4327
4328 case DBGCVAR_TYPE_GC_FAR:
4329 Assert(pDbgc->pVM);
4330 pResult->enmType = DBGCVAR_TYPE_GC_FLAT;
4331 rc = SELMToFlatEx(pDbgc->pVM, pResult->u.GCFar.sel, pResult->u.GCFar.off,
4332 SELMTOFLAT_FLAGS_NO_PL | SELMTOFLAT_FLAGS_HYPER, &pResult->u.GCFlat, NULL);
4333 if (VBOX_SUCCESS(rc))
4334 return 0;
4335 return VERR_PARSE_CONVERSION_FAILED;
4336
4337 case DBGCVAR_TYPE_GC_PHYS:
4338 //rc = MMR3PhysGCPhys2GCVirtEx(pDbgc->pVM, pResult->u.GCPhys, ..., &pResult->u.GCFlat); - yea, sure.
4339 return VERR_PARSE_INCORRECT_ARG_TYPE;
4340
4341 case DBGCVAR_TYPE_HC_FLAT:
4342 return 0;
4343
4344 case DBGCVAR_TYPE_HC_FAR:
4345 return VERR_PARSE_INCORRECT_ARG_TYPE;
4346
4347 case DBGCVAR_TYPE_HC_PHYS:
4348 Assert(pDbgc->pVM);
4349 pResult->enmType = DBGCVAR_TYPE_HC_FLAT;
4350 rc = MMR3HCPhys2HCVirt(pDbgc->pVM, pResult->u.HCPhys, &pResult->u.pvHCFlat);
4351 if (VBOX_SUCCESS(rc))
4352 return 0;
4353 return VERR_PARSE_CONVERSION_FAILED;
4354
4355 case DBGCVAR_TYPE_NUMBER:
4356 pResult->enmType = DBGCVAR_TYPE_GC_FLAT;
4357 pResult->u.GCFlat = (RTGCPTR)pResult->u.u64Number;
4358 return 0;
4359
4360 case DBGCVAR_TYPE_STRING:
4361 return dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_GC_FLAT, pResult);
4362
4363 case DBGCVAR_TYPE_UNKNOWN:
4364 default:
4365 return VERR_PARSE_INCORRECT_ARG_TYPE;
4366 }
4367}
4368
4369
4370/**
4371 * Physical address (unary).
4372 *
4373 * @returns 0 on success.
4374 * @returns VBox evaluation / parsing error code on failure.
4375 * The caller does the bitching.
4376 * @param pDbgc Debugger console instance data.
4377 * @param pArg The argument.
4378 * @param pResult Where to store the result.
4379 */
4380static DECLCALLBACK(int) dbgcOpAddrPhys(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4381{
4382// LogFlow(("dbgcOpAddrPhys\n"));
4383 int rc;
4384
4385 *pResult = *pArg;
4386 switch (pArg->enmType)
4387 {
4388 case DBGCVAR_TYPE_GC_FLAT:
4389 Assert(pDbgc->pVM);
4390 pResult->enmType = DBGCVAR_TYPE_GC_PHYS;
4391 rc = PGMPhysGCPtr2GCPhys(pDbgc->pVM, pArg->u.GCFlat, &pResult->u.GCPhys);
4392 if (VBOX_SUCCESS(rc))
4393 return 0;
4394 /** @todo more memory types! */
4395 return VERR_PARSE_CONVERSION_FAILED;
4396
4397 case DBGCVAR_TYPE_GC_FAR:
4398 Assert(pDbgc->pVM);
4399 rc = SELMToFlatEx(pDbgc->pVM, pResult->u.GCFar.sel, pResult->u.GCFar.off,
4400 SELMTOFLAT_FLAGS_NO_PL | SELMTOFLAT_FLAGS_HYPER, &pResult->u.GCFlat, NULL);
4401 if (VBOX_SUCCESS(rc))
4402 {
4403 pResult->enmType = DBGCVAR_TYPE_GC_PHYS;
4404 rc = PGMPhysGCPtr2GCPhys(pDbgc->pVM, pResult->u.GCFlat, &pResult->u.GCPhys);
4405 if (VBOX_SUCCESS(rc))
4406 return 0;
4407 /** @todo more memory types! */
4408 }
4409 return VERR_PARSE_CONVERSION_FAILED;
4410
4411 case DBGCVAR_TYPE_GC_PHYS:
4412 return 0;
4413
4414 case DBGCVAR_TYPE_HC_FLAT:
4415 Assert(pDbgc->pVM);
4416 pResult->enmType = DBGCVAR_TYPE_GC_PHYS;
4417 rc = PGMPhysHCPtr2GCPhys(pDbgc->pVM, pArg->u.pvHCFlat, &pResult->u.GCPhys);
4418 if (VBOX_SUCCESS(rc))
4419 return 0;
4420 /** @todo more memory types! */
4421 return VERR_PARSE_CONVERSION_FAILED;
4422
4423 case DBGCVAR_TYPE_HC_FAR:
4424 return VERR_PARSE_INCORRECT_ARG_TYPE;
4425
4426 case DBGCVAR_TYPE_HC_PHYS:
4427 return 0;
4428
4429 case DBGCVAR_TYPE_NUMBER:
4430 pResult->enmType = DBGCVAR_TYPE_GC_PHYS;
4431 pResult->u.GCPhys = (RTGCPHYS)pResult->u.u64Number;
4432 return 0;
4433
4434 case DBGCVAR_TYPE_STRING:
4435 return dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_GC_PHYS, pResult);
4436
4437 case DBGCVAR_TYPE_UNKNOWN:
4438 default:
4439 return VERR_PARSE_INCORRECT_ARG_TYPE;
4440 }
4441 return 0;
4442}
4443
4444
4445/**
4446 * Physical host address (unary).
4447 *
4448 * @returns 0 on success.
4449 * @returns VBox evaluation / parsing error code on failure.
4450 * The caller does the bitching.
4451 * @param pDbgc Debugger console instance data.
4452 * @param pArg The argument.
4453 * @param pResult Where to store the result.
4454 */
4455static DECLCALLBACK(int) dbgcOpAddrHostPhys(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4456{
4457// LogFlow(("dbgcOpAddrPhys\n"));
4458 int rc;
4459
4460 *pResult = *pArg;
4461 switch (pArg->enmType)
4462 {
4463 case DBGCVAR_TYPE_GC_FLAT:
4464 Assert(pDbgc->pVM);
4465 pResult->enmType = DBGCVAR_TYPE_HC_PHYS;
4466 rc = PGMPhysGCPtr2HCPhys(pDbgc->pVM, pArg->u.GCFlat, &pResult->u.HCPhys);
4467 if (VBOX_SUCCESS(rc))
4468 return 0;
4469 /** @todo more memory types. */
4470 return VERR_PARSE_CONVERSION_FAILED;
4471
4472 case DBGCVAR_TYPE_GC_FAR:
4473 Assert(pDbgc->pVM);
4474 rc = SELMToFlatEx(pDbgc->pVM, pResult->u.GCFar.sel, pResult->u.GCFar.off,
4475 SELMTOFLAT_FLAGS_NO_PL | SELMTOFLAT_FLAGS_HYPER, &pResult->u.GCFlat, NULL);
4476 if (VBOX_SUCCESS(rc))
4477 {
4478 pResult->enmType = DBGCVAR_TYPE_HC_PHYS;
4479 rc = PGMPhysGCPtr2HCPhys(pDbgc->pVM, pResult->u.GCFlat, &pResult->u.HCPhys);
4480 if (VBOX_SUCCESS(rc))
4481 return 0;
4482 /** @todo more memory types. */
4483 }
4484 return VERR_PARSE_CONVERSION_FAILED;
4485
4486 case DBGCVAR_TYPE_GC_PHYS:
4487 Assert(pDbgc->pVM);
4488 pResult->enmType = DBGCVAR_TYPE_HC_PHYS;
4489 rc = PGMPhysGCPhys2HCPhys(pDbgc->pVM, pArg->u.GCFlat, &pResult->u.HCPhys);
4490 if (VBOX_SUCCESS(rc))
4491 return 0;
4492 return VERR_PARSE_CONVERSION_FAILED;
4493
4494 case DBGCVAR_TYPE_HC_FLAT:
4495 Assert(pDbgc->pVM);
4496 pResult->enmType = DBGCVAR_TYPE_HC_PHYS;
4497 rc = PGMPhysHCPtr2HCPhys(pDbgc->pVM, pArg->u.pvHCFlat, &pResult->u.HCPhys);
4498 if (VBOX_SUCCESS(rc))
4499 return 0;
4500 /** @todo more memory types! */
4501 return VERR_PARSE_CONVERSION_FAILED;
4502
4503 case DBGCVAR_TYPE_HC_FAR:
4504 return VERR_PARSE_INCORRECT_ARG_TYPE;
4505
4506 case DBGCVAR_TYPE_HC_PHYS:
4507 return 0;
4508
4509 case DBGCVAR_TYPE_NUMBER:
4510 pResult->enmType = DBGCVAR_TYPE_HC_PHYS;
4511 pResult->u.HCPhys = (RTGCPHYS)pResult->u.u64Number;
4512 return 0;
4513
4514 case DBGCVAR_TYPE_STRING:
4515 return dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_HC_PHYS, pResult);
4516
4517 case DBGCVAR_TYPE_UNKNOWN:
4518 default:
4519 return VERR_PARSE_INCORRECT_ARG_TYPE;
4520 }
4521 return 0;
4522}
4523
4524
4525/**
4526 * Host address (unary).
4527 *
4528 * @returns 0 on success.
4529 * @returns VBox evaluation / parsing error code on failure.
4530 * The caller does the bitching.
4531 * @param pDbgc Debugger console instance data.
4532 * @param pArg The argument.
4533 * @param pResult Where to store the result.
4534 */
4535static DECLCALLBACK(int) dbgcOpAddrHost(PDBGC pDbgc, PCDBGCVAR pArg, PDBGCVAR pResult)
4536{
4537// LogFlow(("dbgcOpAddrHost\n"));
4538 int rc;
4539
4540 *pResult = *pArg;
4541 switch (pArg->enmType)
4542 {
4543 case DBGCVAR_TYPE_GC_FLAT:
4544 Assert(pDbgc->pVM);
4545 pResult->enmType = DBGCVAR_TYPE_HC_FLAT;
4546 rc = PGMPhysGCPtr2HCPtr(pDbgc->pVM, pArg->u.GCFlat, &pResult->u.pvHCFlat);
4547 if (VBOX_SUCCESS(rc))
4548 return 0;
4549 /** @todo more memory types. */
4550 return VERR_PARSE_CONVERSION_FAILED;
4551
4552 case DBGCVAR_TYPE_GC_FAR:
4553 Assert(pDbgc->pVM);
4554 rc = SELMToFlatEx(pDbgc->pVM, pResult->u.GCFar.sel, pResult->u.GCFar.off,
4555 SELMTOFLAT_FLAGS_NO_PL | SELMTOFLAT_FLAGS_HYPER, &pResult->u.GCFlat, NULL);
4556 if (VBOX_SUCCESS(rc))
4557 {
4558 pResult->enmType = DBGCVAR_TYPE_HC_FLAT;
4559 rc = PGMPhysGCPtr2HCPtr(pDbgc->pVM, pResult->u.GCFlat, &pResult->u.pvHCFlat);
4560 if (VBOX_SUCCESS(rc))
4561 return 0;
4562 /** @todo more memory types. */
4563 }
4564 return VERR_PARSE_CONVERSION_FAILED;
4565
4566 case DBGCVAR_TYPE_GC_PHYS:
4567 Assert(pDbgc->pVM);
4568 pResult->enmType = DBGCVAR_TYPE_HC_FLAT;
4569 rc = PGMPhysGCPhys2HCPtr(pDbgc->pVM, pArg->u.GCPhys, &pResult->u.pvHCFlat);
4570 if (VBOX_SUCCESS(rc))
4571 return 0;
4572 return VERR_PARSE_CONVERSION_FAILED;
4573
4574 case DBGCVAR_TYPE_HC_FLAT:
4575 return 0;
4576
4577 case DBGCVAR_TYPE_HC_FAR:
4578 case DBGCVAR_TYPE_HC_PHYS:
4579 /** @todo !*/
4580 return VERR_PARSE_CONVERSION_FAILED;
4581
4582 case DBGCVAR_TYPE_NUMBER:
4583 pResult->enmType = DBGCVAR_TYPE_HC_FLAT;
4584 pResult->u.pvHCFlat = (void *)(uintptr_t)pResult->u.u64Number;
4585 return 0;
4586
4587 case DBGCVAR_TYPE_STRING:
4588 return dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_HC_FLAT, pResult);
4589
4590 case DBGCVAR_TYPE_UNKNOWN:
4591 default:
4592 return VERR_PARSE_INCORRECT_ARG_TYPE;
4593 }
4594}
4595
4596/**
4597 * Bitwise not (unary).
4598 *
4599 * @returns 0 on success.
4600 * @returns VBox evaluation / parsing error code on failure.
4601 * The caller does the bitching.
4602 * @param pDbgc Debugger console instance data.
4603 * @param pArg The argument.
4604 * @param pResult Where to store the result.
4605 */
4606static DECLCALLBACK(int) dbgcOpAddrFar(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
4607{
4608// LogFlow(("dbgcOpAddrFar\n"));
4609 int rc;
4610
4611 switch (pArg1->enmType)
4612 {
4613 case DBGCVAR_TYPE_STRING:
4614 rc = dbgcSymbolGet(pDbgc, pArg1->u.pszString, DBGCVAR_TYPE_NUMBER, pResult);
4615 if (VBOX_FAILURE(rc))
4616 return rc;
4617 break;
4618 case DBGCVAR_TYPE_NUMBER:
4619 *pResult = *pArg1;
4620 break;
4621
4622 case DBGCVAR_TYPE_GC_FLAT:
4623 case DBGCVAR_TYPE_GC_FAR:
4624 case DBGCVAR_TYPE_GC_PHYS:
4625 case DBGCVAR_TYPE_HC_FLAT:
4626 case DBGCVAR_TYPE_HC_FAR:
4627 case DBGCVAR_TYPE_HC_PHYS:
4628 case DBGCVAR_TYPE_UNKNOWN:
4629 default:
4630 return VERR_PARSE_INCORRECT_ARG_TYPE;
4631 }
4632 pResult->u.GCFar.sel = (RTSEL)pResult->u.u64Number;
4633
4634 /* common code for the two types we support. */
4635 switch (pArg2->enmType)
4636 {
4637 case DBGCVAR_TYPE_GC_FLAT:
4638 pResult->u.GCFar.off = pArg2->u.GCFlat;
4639 pResult->enmType = DBGCVAR_TYPE_GC_FAR;
4640 break;
4641
4642 case DBGCVAR_TYPE_HC_FLAT:
4643 pResult->u.HCFar.off = pArg2->u.GCFlat;
4644 pResult->enmType = DBGCVAR_TYPE_GC_FAR;
4645 break;
4646
4647 case DBGCVAR_TYPE_NUMBER:
4648 pResult->u.GCFar.off = (RTGCPTR)pArg2->u.u64Number;
4649 pResult->enmType = DBGCVAR_TYPE_GC_FAR;
4650 break;
4651
4652 case DBGCVAR_TYPE_STRING:
4653 {
4654 DBGCVAR Var;
4655 rc = dbgcSymbolGet(pDbgc, pArg2->u.pszString, DBGCVAR_TYPE_NUMBER, &Var);
4656 if (VBOX_FAILURE(rc))
4657 return rc;
4658 pResult->u.GCFar.off = (RTGCPTR)Var.u.u64Number;
4659 pResult->enmType = DBGCVAR_TYPE_GC_FAR;
4660 break;
4661 }
4662
4663 case DBGCVAR_TYPE_GC_FAR:
4664 case DBGCVAR_TYPE_GC_PHYS:
4665 case DBGCVAR_TYPE_HC_FAR:
4666 case DBGCVAR_TYPE_HC_PHYS:
4667 case DBGCVAR_TYPE_UNKNOWN:
4668 default:
4669 return VERR_PARSE_INCORRECT_ARG_TYPE;
4670 }
4671 return 0;
4672
4673}
4674
4675
4676
4677/**
4678 * Multiplication operator (binary).
4679 *
4680 * @returns 0 on success.
4681 * @returns VBox evaluation / parsing error code on failure.
4682 * The caller does the bitching.
4683 * @param pDbgc Debugger console instance data.
4684 * @param pArg1 The first argument.
4685 * @param pArg2 The 2nd argument.
4686 * @param pResult Where to store the result.
4687 */
4688static DECLCALLBACK(int) dbgcOpMult(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
4689{
4690 LogFlow(("dbgcOpMult\n"));
4691 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
4692 return -1;
4693}
4694
4695
4696/**
4697 * Division operator (binary).
4698 *
4699 * @returns 0 on success.
4700 * @returns VBox evaluation / parsing error code on failure.
4701 * The caller does the bitching.
4702 * @param pDbgc Debugger console instance data.
4703 * @param pArg1 The first argument.
4704 * @param pArg2 The 2nd argument.
4705 * @param pResult Where to store the result.
4706 */
4707static DECLCALLBACK(int) dbgcOpDiv(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
4708{
4709 LogFlow(("dbgcOpDiv\n"));
4710 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
4711 return -1;
4712}
4713
4714
4715/**
4716 * Modulus operator (binary).
4717 *
4718 * @returns 0 on success.
4719 * @returns VBox evaluation / parsing error code on failure.
4720 * The caller does the bitching.
4721 * @param pDbgc Debugger console instance data.
4722 * @param pArg1 The first argument.
4723 * @param pArg2 The 2nd argument.
4724 * @param pResult Where to store the result.
4725 */
4726static DECLCALLBACK(int) dbgcOpMod(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
4727{
4728 LogFlow(("dbgcOpMod\n"));
4729 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
4730 return -1;
4731}
4732
4733
4734/**
4735 * Addition operator (binary).
4736 *
4737 * @returns 0 on success.
4738 * @returns VBox evaluation / parsing error code on failure.
4739 * The caller does the bitching.
4740 * @param pDbgc Debugger console instance data.
4741 * @param pArg1 The first argument.
4742 * @param pArg2 The 2nd argument.
4743 * @param pResult Where to store the result.
4744 */
4745static DECLCALLBACK(int) dbgcOpAdd(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
4746{
4747// LogFlow(("dbgcOpAdd\n"));
4748
4749 /*
4750 * An addition operation will return (when possible) the left side type in the
4751 * expression. We make an omission for numbers, where we'll take the right side
4752 * type instead. An expression where only the left hand side is a string we'll
4753 * use the right hand type assuming that the string is a symbol.
4754 */
4755 if ( (pArg1->enmType == DBGCVAR_TYPE_NUMBER && pArg2->enmType != DBGCVAR_TYPE_STRING)
4756 || (pArg1->enmType == DBGCVAR_TYPE_STRING && pArg2->enmType != DBGCVAR_TYPE_STRING))
4757 {
4758 PCDBGCVAR pTmp = pArg2;
4759 pArg2 = pArg1;
4760 pArg1 = pTmp;
4761 }
4762 DBGCVAR Sym1, Sym2;
4763 if (pArg1->enmType == DBGCVAR_TYPE_STRING)
4764 {
4765 int rc = dbgcSymbolGet(pDbgc, pArg1->u.pszString, DBGCVAR_TYPE_ANY, &Sym1);
4766 if (VBOX_FAILURE(rc))
4767 return rc;
4768 pArg1 = &Sym1;
4769
4770 rc = dbgcSymbolGet(pDbgc, pArg2->u.pszString, DBGCVAR_TYPE_ANY, &Sym2);
4771 if (VBOX_FAILURE(rc))
4772 return rc;
4773 pArg2 = &Sym2;
4774 }
4775
4776 int rc;
4777 DBGCVAR Var;
4778 DBGCVAR Var2;
4779 switch (pArg1->enmType)
4780 {
4781 /*
4782 * GC Flat
4783 */
4784 case DBGCVAR_TYPE_GC_FLAT:
4785 switch (pArg2->enmType)
4786 {
4787 case DBGCVAR_TYPE_HC_FLAT:
4788 case DBGCVAR_TYPE_HC_FAR:
4789 case DBGCVAR_TYPE_HC_PHYS:
4790 return VERR_PARSE_INVALID_OPERATION;
4791 default:
4792 *pResult = *pArg1;
4793 rc = dbgcOpAddrFlat(pDbgc, pArg2, &Var);
4794 if (VBOX_FAILURE(rc))
4795 return rc;
4796 pResult->u.GCFlat += pArg2->u.GCFlat;
4797 break;
4798 }
4799 break;
4800
4801 /*
4802 * GC Far
4803 */
4804 case DBGCVAR_TYPE_GC_FAR:
4805 switch (pArg2->enmType)
4806 {
4807 case DBGCVAR_TYPE_HC_FLAT:
4808 case DBGCVAR_TYPE_HC_FAR:
4809 case DBGCVAR_TYPE_HC_PHYS:
4810 return VERR_PARSE_INVALID_OPERATION;
4811 case DBGCVAR_TYPE_NUMBER:
4812 *pResult = *pArg1;
4813 pResult->u.GCFar.off += (RTGCPTR)pArg2->u.u64Number;
4814 break;
4815 default:
4816 rc = dbgcOpAddrFlat(pDbgc, pArg1, pResult);
4817 if (VBOX_FAILURE(rc))
4818 return rc;
4819 rc = dbgcOpAddrFlat(pDbgc, pArg2, &Var);
4820 if (VBOX_FAILURE(rc))
4821 return rc;
4822 pResult->u.GCFlat += pArg2->u.GCFlat;
4823 break;
4824 }
4825 break;
4826
4827 /*
4828 * GC Phys
4829 */
4830 case DBGCVAR_TYPE_GC_PHYS:
4831 switch (pArg2->enmType)
4832 {
4833 case DBGCVAR_TYPE_HC_FLAT:
4834 case DBGCVAR_TYPE_HC_FAR:
4835 case DBGCVAR_TYPE_HC_PHYS:
4836 return VERR_PARSE_INVALID_OPERATION;
4837 default:
4838 *pResult = *pArg1;
4839 rc = dbgcOpAddrPhys(pDbgc, pArg2, &Var);
4840 if (VBOX_FAILURE(rc))
4841 return rc;
4842 if (Var.enmType != DBGCVAR_TYPE_GC_PHYS)
4843 return VERR_PARSE_INVALID_OPERATION;
4844 pResult->u.GCPhys += Var.u.GCPhys;
4845 break;
4846 }
4847 break;
4848
4849 /*
4850 * HC Flat
4851 */
4852 case DBGCVAR_TYPE_HC_FLAT:
4853 *pResult = *pArg1;
4854 rc = dbgcOpAddrHost(pDbgc, pArg2, &Var2);
4855 if (VBOX_FAILURE(rc))
4856 return rc;
4857 rc = dbgcOpAddrFlat(pDbgc, &Var2, &Var);
4858 if (VBOX_FAILURE(rc))
4859 return rc;
4860 pResult->u.pvHCFlat = (char *)pResult->u.pvHCFlat + (uintptr_t)Var.u.pvHCFlat;
4861 break;
4862
4863 /*
4864 * HC Far
4865 */
4866 case DBGCVAR_TYPE_HC_FAR:
4867 switch (pArg2->enmType)
4868 {
4869 case DBGCVAR_TYPE_NUMBER:
4870 *pResult = *pArg1;
4871 pResult->u.HCFar.off += (uintptr_t)pArg2->u.u64Number;
4872 break;
4873
4874 default:
4875 rc = dbgcOpAddrFlat(pDbgc, pArg1, pResult);
4876 if (VBOX_FAILURE(rc))
4877 return rc;
4878 rc = dbgcOpAddrHost(pDbgc, pArg2, &Var2);
4879 if (VBOX_FAILURE(rc))
4880 return rc;
4881 rc = dbgcOpAddrFlat(pDbgc, &Var2, &Var);
4882 if (VBOX_FAILURE(rc))
4883 return rc;
4884 pResult->u.pvHCFlat = (char *)pResult->u.pvHCFlat + (uintptr_t)Var.u.pvHCFlat;
4885 break;
4886 }
4887 break;
4888
4889 /*
4890 * HC Phys
4891 */
4892 case DBGCVAR_TYPE_HC_PHYS:
4893 *pResult = *pArg1;
4894 rc = dbgcOpAddrHostPhys(pDbgc, pArg2, &Var);
4895 if (VBOX_FAILURE(rc))
4896 return rc;
4897 pResult->u.HCPhys += Var.u.HCPhys;
4898 break;
4899
4900 /*
4901 * Numbers (see start of function)
4902 */
4903 case DBGCVAR_TYPE_NUMBER:
4904 switch (pArg2->enmType)
4905 {
4906 case DBGCVAR_TYPE_STRING:
4907 rc = dbgcSymbolGet(pDbgc, pArg2->u.pszString, DBGCVAR_TYPE_NUMBER, &Var);
4908 if (VBOX_FAILURE(rc))
4909 return rc;
4910 case DBGCVAR_TYPE_NUMBER:
4911 pResult->u.u64Number += pArg2->u.u64Number;
4912 break;
4913 default:
4914 return VERR_PARSE_INVALID_OPERATION;
4915 }
4916 break;
4917
4918 default:
4919 return VERR_PARSE_INVALID_OPERATION;
4920
4921 }
4922 return 0;
4923}
4924
4925
4926/**
4927 * Subtration operator (binary).
4928 *
4929 * @returns 0 on success.
4930 * @returns VBox evaluation / parsing error code on failure.
4931 * The caller does the bitching.
4932 * @param pDbgc Debugger console instance data.
4933 * @param pArg1 The first argument.
4934 * @param pArg2 The 2nd argument.
4935 * @param pResult Where to store the result.
4936 */
4937static DECLCALLBACK(int) dbgcOpSub(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
4938{
4939// LogFlow(("dbgcOpSub\n"));
4940
4941 /*
4942 * An subtraction operation will return the left side type in the expression.
4943 * However, if the left hand side is a number and the right hand a pointer of
4944 * some kind we'll convert the left hand side to the same type as the right hand.
4945 * Any strings will be attempted resolved as symbols.
4946 */
4947 DBGCVAR Sym1, Sym2;
4948 if ( pArg2->enmType == DBGCVAR_TYPE_STRING
4949 && ( pArg1->enmType == DBGCVAR_TYPE_NUMBER
4950 || pArg1->enmType == DBGCVAR_TYPE_STRING))
4951 {
4952 int rc = dbgcSymbolGet(pDbgc, pArg2->u.pszString, DBGCVAR_TYPE_ANY, &Sym2);
4953 if (VBOX_FAILURE(rc))
4954 return rc;
4955 pArg2 = &Sym2;
4956 }
4957
4958 if (pArg1->enmType == DBGCVAR_TYPE_STRING)
4959 {
4960 DBGCVARTYPE enmType;
4961 switch (pArg2->enmType)
4962 {
4963 case DBGCVAR_TYPE_NUMBER:
4964 enmType = DBGCVAR_TYPE_ANY;
4965 break;
4966 case DBGCVAR_TYPE_GC_FLAT:
4967 case DBGCVAR_TYPE_GC_PHYS:
4968 case DBGCVAR_TYPE_HC_FLAT:
4969 case DBGCVAR_TYPE_HC_PHYS:
4970 enmType = pArg2->enmType;
4971 break;
4972 case DBGCVAR_TYPE_GC_FAR:
4973 enmType = DBGCVAR_TYPE_GC_FLAT;
4974 break;
4975 case DBGCVAR_TYPE_HC_FAR:
4976 enmType = DBGCVAR_TYPE_HC_FLAT;
4977 break;
4978
4979 default:
4980 case DBGCVAR_TYPE_STRING:
4981 AssertMsgFailed(("Can't happen\n"));
4982 enmType = DBGCVAR_TYPE_STRING;
4983 break;
4984 }
4985 if (enmType != DBGCVAR_TYPE_STRING)
4986 {
4987 int rc = dbgcSymbolGet(pDbgc, pArg1->u.pszString, DBGCVAR_TYPE_ANY, &Sym1);
4988 if (VBOX_FAILURE(rc))
4989 return rc;
4990 pArg1 = &Sym1;
4991 }
4992 }
4993 else if (pArg1->enmType == DBGCVAR_TYPE_NUMBER)
4994 {
4995 PFNDBGCOPUNARY pOp = NULL;
4996 switch (pArg2->enmType)
4997 {
4998 case DBGCVAR_TYPE_GC_FAR:
4999 case DBGCVAR_TYPE_GC_FLAT:
5000 pOp = dbgcOpAddrFlat;
5001 break;
5002 case DBGCVAR_TYPE_GC_PHYS:
5003 pOp = dbgcOpAddrPhys;
5004 break;
5005 case DBGCVAR_TYPE_HC_FAR:
5006 case DBGCVAR_TYPE_HC_FLAT:
5007 pOp = dbgcOpAddrHost;
5008 break;
5009 case DBGCVAR_TYPE_HC_PHYS:
5010 pOp = dbgcOpAddrHostPhys;
5011 break;
5012 case DBGCVAR_TYPE_NUMBER:
5013 break;
5014 default:
5015 case DBGCVAR_TYPE_STRING:
5016 AssertMsgFailed(("Can't happen\n"));
5017 break;
5018 }
5019 if (pOp)
5020 {
5021 int rc = pOp(pDbgc, pArg1, &Sym1);
5022 if (VBOX_FAILURE(rc))
5023 return rc;
5024 pArg1 = &Sym1;
5025 }
5026 }
5027
5028
5029 /*
5030 * Normal processing.
5031 */
5032 int rc;
5033 DBGCVAR Var;
5034 DBGCVAR Var2;
5035 switch (pArg1->enmType)
5036 {
5037 /*
5038 * GC Flat
5039 */
5040 case DBGCVAR_TYPE_GC_FLAT:
5041 switch (pArg2->enmType)
5042 {
5043 case DBGCVAR_TYPE_HC_FLAT:
5044 case DBGCVAR_TYPE_HC_FAR:
5045 case DBGCVAR_TYPE_HC_PHYS:
5046 return VERR_PARSE_INVALID_OPERATION;
5047 default:
5048 *pResult = *pArg1;
5049 rc = dbgcOpAddrFlat(pDbgc, pArg2, &Var);
5050 if (VBOX_FAILURE(rc))
5051 return rc;
5052 pResult->u.GCFlat -= pArg2->u.GCFlat;
5053 break;
5054 }
5055 break;
5056
5057 /*
5058 * GC Far
5059 */
5060 case DBGCVAR_TYPE_GC_FAR:
5061 switch (pArg2->enmType)
5062 {
5063 case DBGCVAR_TYPE_HC_FLAT:
5064 case DBGCVAR_TYPE_HC_FAR:
5065 case DBGCVAR_TYPE_HC_PHYS:
5066 return VERR_PARSE_INVALID_OPERATION;
5067 case DBGCVAR_TYPE_NUMBER:
5068 *pResult = *pArg1;
5069 pResult->u.GCFar.off -= (RTGCPTR)pArg2->u.u64Number;
5070 break;
5071 default:
5072 rc = dbgcOpAddrFlat(pDbgc, pArg1, pResult);
5073 if (VBOX_FAILURE(rc))
5074 return rc;
5075 rc = dbgcOpAddrFlat(pDbgc, pArg2, &Var);
5076 if (VBOX_FAILURE(rc))
5077 return rc;
5078 pResult->u.GCFlat -= pArg2->u.GCFlat;
5079 break;
5080 }
5081 break;
5082
5083 /*
5084 * GC Phys
5085 */
5086 case DBGCVAR_TYPE_GC_PHYS:
5087 switch (pArg2->enmType)
5088 {
5089 case DBGCVAR_TYPE_HC_FLAT:
5090 case DBGCVAR_TYPE_HC_FAR:
5091 case DBGCVAR_TYPE_HC_PHYS:
5092 return VERR_PARSE_INVALID_OPERATION;
5093 default:
5094 *pResult = *pArg1;
5095 rc = dbgcOpAddrPhys(pDbgc, pArg2, &Var);
5096 if (VBOX_FAILURE(rc))
5097 return rc;
5098 if (Var.enmType != DBGCVAR_TYPE_GC_PHYS)
5099 return VERR_PARSE_INVALID_OPERATION;
5100 pResult->u.GCPhys -= Var.u.GCPhys;
5101 break;
5102 }
5103 break;
5104
5105 /*
5106 * HC Flat
5107 */
5108 case DBGCVAR_TYPE_HC_FLAT:
5109 *pResult = *pArg1;
5110 rc = dbgcOpAddrHost(pDbgc, pArg2, &Var2);
5111 if (VBOX_FAILURE(rc))
5112 return rc;
5113 rc = dbgcOpAddrFlat(pDbgc, &Var2, &Var);
5114 if (VBOX_FAILURE(rc))
5115 return rc;
5116 pResult->u.pvHCFlat = (char *)pResult->u.pvHCFlat - (uintptr_t)Var.u.pvHCFlat;
5117 break;
5118
5119 /*
5120 * HC Far
5121 */
5122 case DBGCVAR_TYPE_HC_FAR:
5123 switch (pArg2->enmType)
5124 {
5125 case DBGCVAR_TYPE_NUMBER:
5126 *pResult = *pArg1;
5127 pResult->u.HCFar.off -= (uintptr_t)pArg2->u.u64Number;
5128 break;
5129
5130 default:
5131 rc = dbgcOpAddrFlat(pDbgc, pArg1, pResult);
5132 if (VBOX_FAILURE(rc))
5133 return rc;
5134 rc = dbgcOpAddrHost(pDbgc, pArg2, &Var2);
5135 if (VBOX_FAILURE(rc))
5136 return rc;
5137 rc = dbgcOpAddrFlat(pDbgc, &Var2, &Var);
5138 if (VBOX_FAILURE(rc))
5139 return rc;
5140 pResult->u.pvHCFlat = (char *)pResult->u.pvHCFlat - (uintptr_t)Var.u.pvHCFlat;
5141 break;
5142 }
5143 break;
5144
5145 /*
5146 * HC Phys
5147 */
5148 case DBGCVAR_TYPE_HC_PHYS:
5149 *pResult = *pArg1;
5150 rc = dbgcOpAddrHostPhys(pDbgc, pArg2, &Var);
5151 if (VBOX_FAILURE(rc))
5152 return rc;
5153 pResult->u.HCPhys -= Var.u.HCPhys;
5154 break;
5155
5156 /*
5157 * Numbers (see start of function)
5158 */
5159 case DBGCVAR_TYPE_NUMBER:
5160 switch (pArg2->enmType)
5161 {
5162 case DBGCVAR_TYPE_STRING:
5163 rc = dbgcSymbolGet(pDbgc, pArg2->u.pszString, DBGCVAR_TYPE_NUMBER, &Var);
5164 if (VBOX_FAILURE(rc))
5165 return rc;
5166 case DBGCVAR_TYPE_NUMBER:
5167 pResult->u.u64Number -= pArg2->u.u64Number;
5168 break;
5169 default:
5170 return VERR_PARSE_INVALID_OPERATION;
5171 }
5172 break;
5173
5174 default:
5175 return VERR_PARSE_INVALID_OPERATION;
5176
5177 }
5178 return 0;
5179}
5180
5181
5182/**
5183 * Bitwise shift left operator (binary).
5184 *
5185 * @returns 0 on success.
5186 * @returns VBox evaluation / parsing error code on failure.
5187 * The caller does the bitching.
5188 * @param pDbgc Debugger console instance data.
5189 * @param pArg1 The first argument.
5190 * @param pArg2 The 2nd argument.
5191 * @param pResult Where to store the result.
5192 */
5193static DECLCALLBACK(int) dbgcOpBitwiseShiftLeft(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5194{
5195 LogFlow(("dbgcOpBitwiseShiftLeft\n"));
5196 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
5197 return -1;
5198}
5199
5200
5201/**
5202 * Bitwise shift right operator (binary).
5203 *
5204 * @returns 0 on success.
5205 * @returns VBox evaluation / parsing error code on failure.
5206 * The caller does the bitching.
5207 * @param pDbgc Debugger console instance data.
5208 * @param pArg1 The first argument.
5209 * @param pArg2 The 2nd argument.
5210 * @param pResult Where to store the result.
5211 */
5212static DECLCALLBACK(int) dbgcOpBitwiseShiftRight(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5213{
5214 LogFlow(("dbgcOpBitwiseShiftRight\n"));
5215 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
5216 return -1;
5217}
5218
5219
5220/**
5221 * Bitwise and operator (binary).
5222 *
5223 * @returns 0 on success.
5224 * @returns VBox evaluation / parsing error code on failure.
5225 * The caller does the bitching.
5226 * @param pDbgc Debugger console instance data.
5227 * @param pArg1 The first argument.
5228 * @param pArg2 The 2nd argument.
5229 * @param pResult Where to store the result.
5230 */
5231static DECLCALLBACK(int) dbgcOpBitwiseAnd(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5232{
5233 LogFlow(("dbgcOpBitwiseAnd\n"));
5234 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
5235 return -1;
5236}
5237
5238
5239/**
5240 * Bitwise exclusive or operator (binary).
5241 *
5242 * @returns 0 on success.
5243 * @returns VBox evaluation / parsing error code on failure.
5244 * The caller does the bitching.
5245 * @param pDbgc Debugger console instance data.
5246 * @param pArg1 The first argument.
5247 * @param pArg2 The 2nd argument.
5248 * @param pResult Where to store the result.
5249 */
5250static DECLCALLBACK(int) dbgcOpBitwiseXor(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5251{
5252 LogFlow(("dbgcOpBitwiseXor\n"));
5253 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
5254 return -1;
5255}
5256
5257
5258/**
5259 * Bitwise inclusive or operator (binary).
5260 *
5261 * @returns 0 on success.
5262 * @returns VBox evaluation / parsing error code on failure.
5263 * The caller does the bitching.
5264 * @param pDbgc Debugger console instance data.
5265 * @param pArg1 The first argument.
5266 * @param pArg2 The 2nd argument.
5267 * @param pResult Where to store the result.
5268 */
5269static DECLCALLBACK(int) dbgcOpBitwiseOr(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5270{
5271 LogFlow(("dbgcOpBitwiseOr\n"));
5272 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
5273 return -1;
5274}
5275
5276
5277/**
5278 * Boolean and operator (binary).
5279 *
5280 * @returns 0 on success.
5281 * @returns VBox evaluation / parsing error code on failure.
5282 * The caller does the bitching.
5283 * @param pDbgc Debugger console instance data.
5284 * @param pArg1 The first argument.
5285 * @param pArg2 The 2nd argument.
5286 * @param pResult Where to store the result.
5287 */
5288static DECLCALLBACK(int) dbgcOpBooleanAnd(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5289{
5290 LogFlow(("dbgcOpBooleanAnd\n"));
5291 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
5292 return -1;
5293}
5294
5295
5296/**
5297 * Boolean or operator (binary).
5298 *
5299 * @returns 0 on success.
5300 * @returns VBox evaluation / parsing error code on failure.
5301 * The caller does the bitching.
5302 * @param pDbgc Debugger console instance data.
5303 * @param pArg1 The first argument.
5304 * @param pArg2 The 2nd argument.
5305 * @param pResult Where to store the result.
5306 */
5307static DECLCALLBACK(int) dbgcOpBooleanOr(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5308{
5309 LogFlow(("dbgcOpBooleanOr\n"));
5310 NOREF(pDbgc); NOREF(pArg1); NOREF(pArg2); NOREF(pResult);
5311 return -1;
5312}
5313
5314
5315/**
5316 * Range to operator (binary).
5317 *
5318 * @returns 0 on success.
5319 * @returns VBox evaluation / parsing error code on failure.
5320 * The caller does the bitching.
5321 * @param pDbgc Debugger console instance data.
5322 * @param pArg1 The first argument.
5323 * @param pArg2 The 2nd argument.
5324 * @param pResult Where to store the result.
5325 */
5326static DECLCALLBACK(int) dbgcOpRangeLength(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5327{
5328// LogFlow(("dbgcOpRangeLength\n"));
5329 /*
5330 * Make result. Strings needs to be resolved into symbols.
5331 */
5332 if (pArg1->enmType == DBGCVAR_TYPE_STRING)
5333 {
5334 int rc = dbgcSymbolGet(pDbgc, pArg1->u.pszString, DBGCVAR_TYPE_ANY, pResult);
5335 if (VBOX_FAILURE(rc))
5336 return rc;
5337 }
5338 else
5339 *pResult = *pArg1;
5340
5341 /*
5342 * Convert 2nd argument to element count.
5343 */
5344 pResult->enmRangeType = DBGCVAR_RANGE_ELEMENTS;
5345 switch (pArg2->enmType)
5346 {
5347 case DBGCVAR_TYPE_NUMBER:
5348 pResult->u64Range = pArg2->u.u64Number;
5349 break;
5350
5351 case DBGCVAR_TYPE_STRING:
5352 {
5353 int rc = dbgcSymbolGet(pDbgc, pArg1->u.pszString, DBGCVAR_TYPE_NUMBER, pResult);
5354 if (VBOX_FAILURE(rc))
5355 return rc;
5356 pResult->u64Range = pArg2->u.u64Number;
5357 break;
5358 }
5359
5360 default:
5361 return VERR_PARSE_INVALID_OPERATION;
5362 }
5363
5364 return VINF_SUCCESS;
5365}
5366
5367
5368/**
5369 * Range to operator (binary).
5370 *
5371 * @returns 0 on success.
5372 * @returns VBox evaluation / parsing error code on failure.
5373 * The caller does the bitching.
5374 * @param pDbgc Debugger console instance data.
5375 * @param pArg1 The first argument.
5376 * @param pArg2 The 2nd argument.
5377 * @param pResult Where to store the result.
5378 */
5379static DECLCALLBACK(int) dbgcOpRangeLengthBytes(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5380{
5381// LogFlow(("dbgcOpRangeLengthBytes\n"));
5382 int rc = dbgcOpRangeLength(pDbgc, pArg1, pArg2, pResult);
5383 if (VBOX_SUCCESS(rc))
5384 pResult->enmRangeType = DBGCVAR_RANGE_BYTES;
5385 return rc;
5386}
5387
5388
5389/**
5390 * Range to operator (binary).
5391 *
5392 * @returns 0 on success.
5393 * @returns VBox evaluation / parsing error code on failure.
5394 * The caller does the bitching.
5395 * @param pDbgc Debugger console instance data.
5396 * @param pArg1 The first argument.
5397 * @param pArg2 The 2nd argument.
5398 * @param pResult Where to store the result.
5399 */
5400static DECLCALLBACK(int) dbgcOpRangeTo(PDBGC pDbgc, PCDBGCVAR pArg1, PCDBGCVAR pArg2, PDBGCVAR pResult)
5401{
5402// LogFlow(("dbgcOpRangeTo\n"));
5403 /*
5404 * Calc number of bytes between the two args.
5405 */
5406 DBGCVAR Diff;
5407 int rc = dbgcOpSub(pDbgc, pArg2, pArg1, &Diff);
5408 if (VBOX_FAILURE(rc))
5409 return rc;
5410
5411 /*
5412 * Use the diff as the range of Arg1.
5413 */
5414 *pResult = *pArg1;
5415 pResult->enmRangeType = DBGCVAR_RANGE_BYTES;
5416 switch (Diff.enmType)
5417 {
5418 case DBGCVAR_TYPE_GC_FLAT:
5419 pResult->u64Range = (RTGCUINTPTR)Diff.u.GCFlat;
5420 break;
5421 case DBGCVAR_TYPE_GC_PHYS:
5422 pResult->u64Range = Diff.u.GCPhys;
5423 break;
5424 case DBGCVAR_TYPE_HC_FLAT:
5425 pResult->u64Range = (uintptr_t)Diff.u.pvHCFlat;
5426 break;
5427 case DBGCVAR_TYPE_HC_PHYS:
5428 pResult->u64Range = Diff.u.HCPhys;
5429 break;
5430 case DBGCVAR_TYPE_NUMBER:
5431 pResult->u64Range = Diff.u.u64Number;
5432 break;
5433
5434 case DBGCVAR_TYPE_GC_FAR:
5435 case DBGCVAR_TYPE_STRING:
5436 case DBGCVAR_TYPE_HC_FAR:
5437 default:
5438 AssertMsgFailed(("Impossible!\n"));
5439 return VERR_PARSE_INVALID_OPERATION;
5440 }
5441
5442 return 0;
5443}
5444
5445
5446
5447
5448
5449/**
5450 * Output callback.
5451 *
5452 * @returns number of bytes written.
5453 * @param pvArg User argument.
5454 * @param pachChars Pointer to an array of utf-8 characters.
5455 * @param cbChars Number of bytes in the character array pointed to by pachChars.
5456 */
5457static DECLCALLBACK(size_t) dbgcFormatOutput(void *pvArg, const char *pachChars, size_t cbChars)
5458{
5459 PDBGC pDbgc = (PDBGC)pvArg;
5460 if (cbChars)
5461 {
5462 int rc = pDbgc->pBack->pfnWrite(pDbgc->pBack, pachChars, cbChars, NULL);
5463 if (VBOX_FAILURE(rc))
5464 {
5465 pDbgc->rcOutput = rc;
5466 cbChars = 0;
5467 }
5468 }
5469
5470 return cbChars;
5471}
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
5482//
5483//
5484// C a l l b a c k H e l p e r s
5485//
5486//
5487//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
5488
5489
5490
5491/**
5492 * Command helper for writing text to the debug console.
5493 *
5494 * @returns VBox status.
5495 * @param pCmdHlp Pointer to the command callback structure.
5496 * @param pvBuf What to write.
5497 * @param cbBuf Number of bytes to write.
5498 * @param pcbWritten Where to store the number of bytes actually written.
5499 * If NULL the entire buffer must be successfully written.
5500 */
5501static DECLCALLBACK(int) dbgcHlpWrite(PDBGCCMDHLP pCmdHlp, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
5502{
5503 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5504 return pDbgc->pBack->pfnWrite(pDbgc->pBack, pvBuf, cbBuf, pcbWritten);
5505}
5506
5507
5508/**
5509 * Command helper for writing formatted text to the debug console.
5510 *
5511 * @returns VBox status.
5512 * @param pCmdHlp Pointer to the command callback structure.
5513 * @param pcb Where to store the number of bytes written.
5514 * @param pszFormat The format string.
5515 * This is using the log formatter, so it's format extensions can be used.
5516 * @param ... Arguments specified in the format string.
5517 */
5518static DECLCALLBACK(int) dbgcHlpPrintf(PDBGCCMDHLP pCmdHlp, size_t *pcbWritten, const char *pszFormat, ...)
5519{
5520 /*
5521 * Do the formatting and output.
5522 */
5523 va_list args;
5524 va_start(args, pszFormat);
5525 int rc = pCmdHlp->pfnPrintfV(pCmdHlp, pcbWritten, pszFormat, args);
5526 va_end(args);
5527
5528 return rc;
5529}
5530
5531/**
5532 * Callback to format non-standard format specifiers.
5533 *
5534 * @returns The number of bytes formatted.
5535 * @param pvArg Formatter argument.
5536 * @param pfnOutput Pointer to output function.
5537 * @param pvArgOutput Argument for the output function.
5538 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
5539 * after the format specifier.
5540 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
5541 * @param cchWidth Format Width. -1 if not specified.
5542 * @param cchPrecision Format Precision. -1 if not specified.
5543 * @param fFlags Flags (RTSTR_NTFS_*).
5544 * @param chArgSize The argument size specifier, 'l' or 'L'.
5545 */
5546static DECLCALLBACK(int) dbgcStringFormatter(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5547 const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
5548{
5549 NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags); NOREF(chArgSize); NOREF(pvArg);
5550 if (**ppszFormat != 'D')
5551 {
5552 (*ppszFormat)++;
5553 return 0;
5554 }
5555
5556 (*ppszFormat)++;
5557 switch (**ppszFormat)
5558 {
5559 /*
5560 * Print variable without range.
5561 * The argument is a const pointer to the variable.
5562 */
5563 case 'V':
5564 {
5565 (*ppszFormat)++;
5566 PCDBGCVAR pVar = va_arg(*pArgs, PCDBGCVAR);
5567 switch (pVar->enmType)
5568 {
5569 case DBGCVAR_TYPE_GC_FLAT:
5570 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%%%VGv", pVar->u.GCFlat);
5571 case DBGCVAR_TYPE_GC_FAR:
5572 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%04x:%08x", pVar->u.GCFar.sel, pVar->u.GCFar.off);
5573 case DBGCVAR_TYPE_GC_PHYS:
5574 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%%%%%VGp", pVar->u.GCPhys);
5575 case DBGCVAR_TYPE_HC_FLAT:
5576 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%%#%VHv", (uintptr_t)pVar->u.pvHCFlat);
5577 case DBGCVAR_TYPE_HC_FAR:
5578 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "#%04x:%08x", pVar->u.HCFar.sel, pVar->u.HCFar.off);
5579 case DBGCVAR_TYPE_HC_PHYS:
5580 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "#%%%%%VHp", pVar->u.HCPhys);
5581 case DBGCVAR_TYPE_STRING:
5582 return pfnOutput(pvArgOutput, pVar->u.pszString, (size_t)pVar->u64Range);
5583 case DBGCVAR_TYPE_NUMBER:
5584 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%llx", pVar->u.u64Number);
5585
5586 case DBGCVAR_TYPE_UNKNOWN:
5587 default:
5588 return pfnOutput(pvArgOutput, "??", 2);
5589 }
5590 }
5591
5592 /*
5593 * Print variable with range.
5594 * The argument is a const pointer to the variable.
5595 */
5596 case 'v':
5597 {
5598 (*ppszFormat)++;
5599 PCDBGCVAR pVar = va_arg(*pArgs, PCDBGCVAR);
5600
5601 char szRange[32];
5602 switch (pVar->enmRangeType)
5603 {
5604 case DBGCVAR_RANGE_NONE:
5605 szRange[0] = '\0';
5606 break;
5607 case DBGCVAR_RANGE_ELEMENTS:
5608 RTStrPrintf(szRange, sizeof(szRange), " L %llx", pVar->u64Range);
5609 break;
5610 case DBGCVAR_RANGE_BYTES:
5611 RTStrPrintf(szRange, sizeof(szRange), " LB %llx", pVar->u64Range);
5612 break;
5613 }
5614
5615 switch (pVar->enmType)
5616 {
5617 case DBGCVAR_TYPE_GC_FLAT:
5618 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%%%VGv%s", pVar->u.GCFlat, szRange);
5619 case DBGCVAR_TYPE_GC_FAR:
5620 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%04x:%08x%s", pVar->u.GCFar.sel, pVar->u.GCFar.off, szRange);
5621 case DBGCVAR_TYPE_GC_PHYS:
5622 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%%%%%VGp%s", pVar->u.GCPhys, szRange);
5623 case DBGCVAR_TYPE_HC_FLAT:
5624 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%%#%VHv%s", (uintptr_t)pVar->u.pvHCFlat, szRange);
5625 case DBGCVAR_TYPE_HC_FAR:
5626 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "#%04x:%08x%s", pVar->u.HCFar.sel, pVar->u.HCFar.off, szRange);
5627 case DBGCVAR_TYPE_HC_PHYS:
5628 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "#%%%%%VHp%s", pVar->u.HCPhys, szRange);
5629 case DBGCVAR_TYPE_STRING:
5630 return pfnOutput(pvArgOutput, pVar->u.pszString, (size_t)pVar->u64Range);
5631 case DBGCVAR_TYPE_NUMBER:
5632 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%llx%s", pVar->u.u64Number, szRange);
5633
5634 case DBGCVAR_TYPE_UNKNOWN:
5635 default:
5636 return pfnOutput(pvArgOutput, "??", 2);
5637 }
5638 }
5639
5640 default:
5641 AssertMsgFailed(("Invalid format type '%s'!\n", **ppszFormat));
5642 return 0;
5643 }
5644}
5645
5646
5647/**
5648 * Command helper for writing formatted text to the debug console.
5649 *
5650 * @returns VBox status.
5651 * @param pCmdHlp Pointer to the command callback structure.
5652 * @param pcb Where to store the number of bytes written.
5653 * @param pszFormat The format string.
5654 * This is using the log formatter, so it's format extensions can be used.
5655 * @param args Arguments specified in the format string.
5656 */
5657static DECLCALLBACK(int) dbgcHlpPrintfV(PDBGCCMDHLP pCmdHlp, size_t *pcbWritten, const char *pszFormat, va_list args)
5658{
5659 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5660
5661 /*
5662 * Do the formatting and output.
5663 */
5664 pDbgc->rcOutput = 0;
5665 size_t cb = RTStrFormatV(dbgcFormatOutput, pDbgc, dbgcStringFormatter, pDbgc, pszFormat, args);
5666
5667 if (pcbWritten)
5668 *pcbWritten = cb;
5669
5670 return pDbgc->rcOutput;
5671}
5672
5673
5674/**
5675 * Reports an error from a DBGF call.
5676 *
5677 * @returns VBox status code appropriate to return from a command.
5678 * @param pCmdHlp Pointer to command helpers.
5679 * @param rc The VBox status code returned by a DBGF call.
5680 * @param pszFormat Format string for additional messages. Can be NULL.
5681 * @param ... Format arguments, optional.
5682 */
5683static DECLCALLBACK(int) dbgcHlpVBoxErrorV(PDBGCCMDHLP pCmdHlp, int rc, const char *pszFormat, va_list args)
5684{
5685 switch (rc)
5686 {
5687 case VINF_SUCCESS:
5688 break;
5689
5690 default:
5691 rc = pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: %Vrc: %s", rc, pszFormat ? " " : "\n");
5692 if (VBOX_SUCCESS(rc) && pszFormat)
5693 rc = pCmdHlp->pfnPrintfV(pCmdHlp, NULL, pszFormat, args);
5694 break;
5695 }
5696 return rc;
5697}
5698
5699
5700/**
5701 * Reports an error from a DBGF call.
5702 *
5703 * @returns VBox status code appropriate to return from a command.
5704 * @param pCmdHlp Pointer to command helpers.
5705 * @param rc The VBox status code returned by a DBGF call.
5706 * @param pszFormat Format string for additional messages. Can be NULL.
5707 * @param ... Format arguments, optional.
5708 */
5709static DECLCALLBACK(int) dbgcHlpVBoxError(PDBGCCMDHLP pCmdHlp, int rc, const char *pszFormat, ...)
5710{
5711 va_list args;
5712 va_start(args, pszFormat);
5713 int rcRet = pCmdHlp->pfnVBoxErrorV(pCmdHlp, rc, pszFormat, args);
5714 va_end(args);
5715 return rcRet;
5716}
5717
5718
5719/**
5720 * Command helper for reading memory specified by a DBGC variable.
5721 *
5722 * @returns VBox status code appropriate to return from a command.
5723 * @param pCmdHlp Pointer to the command callback structure.
5724 * @param pVM VM handle if GC or physical HC address.
5725 * @param pvBuffer Where to store the read data.
5726 * @param cbRead Number of bytes to read.
5727 * @param pVarPointer DBGC variable specifying where to start reading.
5728 * @param pcbRead Where to store the number of bytes actually read.
5729 * This optional, but it's useful when read GC virtual memory where a
5730 * page in the requested range might not be present.
5731 * If not specified not-present failure or end of a HC physical page
5732 * will cause failure.
5733 */
5734static DECLCALLBACK(int) dbgcHlpMemRead(PDBGCCMDHLP pCmdHlp, PVM pVM, void *pvBuffer, size_t cbRead, PCDBGCVAR pVarPointer, size_t *pcbRead)
5735{
5736 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5737
5738 /*
5739 * Dummy check.
5740 */
5741 if (cbRead == 0)
5742 {
5743 if (*pcbRead)
5744 *pcbRead = 0;
5745 return VINF_SUCCESS;
5746 }
5747
5748 /*
5749 * Convert Far addresses getting size and the correct base address.
5750 */
5751 DBGCVAR Var = *pVarPointer;
5752 switch (pVarPointer->enmType)
5753 {
5754 case DBGCVAR_TYPE_GC_FAR:
5755 {
5756 uint32_t cb;
5757 Var.enmType = DBGCVAR_TYPE_GC_FLAT;
5758 int rc = SELMToFlatEx(pDbgc->pVM, Var.u.GCFar.sel, Var.u.GCFar.off,
5759 SELMTOFLAT_FLAGS_NO_PL | SELMTOFLAT_FLAGS_HYPER, &Var.u.GCFlat, &cb);
5760 if (VBOX_FAILURE(rc))
5761 return rc;
5762 if (cbRead > cb)
5763 {
5764 if (!pcbRead)
5765 return VERR_OUT_OF_SELECTOR_BOUNDS;
5766 cbRead = cb;
5767 }
5768 break;
5769 }
5770
5771 case DBGCVAR_TYPE_HC_FAR:
5772 /* not supported yet! */
5773 return VERR_NOT_IMPLEMENTED;
5774
5775 case DBGCVAR_TYPE_GC_FLAT:
5776 case DBGCVAR_TYPE_GC_PHYS:
5777 case DBGCVAR_TYPE_HC_FLAT:
5778 case DBGCVAR_TYPE_HC_PHYS:
5779 break;
5780
5781 default:
5782 return VERR_NOT_IMPLEMENTED;
5783 }
5784
5785
5786
5787 /*
5788 * Copy page by page.
5789 */
5790 size_t cbLeft = cbRead;
5791 for (;;)
5792 {
5793 /*
5794 * Calc read size.
5795 */
5796 size_t cb = RT_MIN(PAGE_SIZE, cbLeft);
5797 switch (pVarPointer->enmType)
5798 {
5799 case DBGCVAR_TYPE_GC_FLAT: cb = RT_MIN(cb, PAGE_SIZE - (Var.u.GCFlat & PAGE_OFFSET_MASK)); break;
5800 case DBGCVAR_TYPE_GC_PHYS: cb = RT_MIN(cb, PAGE_SIZE - (Var.u.GCPhys & PAGE_OFFSET_MASK)); break;
5801 case DBGCVAR_TYPE_HC_FLAT: cb = RT_MIN(cb, PAGE_SIZE - ((uintptr_t)Var.u.pvHCFlat & PAGE_OFFSET_MASK)); break;
5802 case DBGCVAR_TYPE_HC_PHYS: cb = RT_MIN(cb, PAGE_SIZE - ((size_t)Var.u.HCPhys & PAGE_OFFSET_MASK)); break; /* size_t: MSC has braindead loss of data warnings! */
5803 default: break;
5804 }
5805
5806 /*
5807 * Perform read.
5808 */
5809 int rc;
5810 switch (Var.enmType)
5811 {
5812 case DBGCVAR_TYPE_GC_FLAT:
5813 rc = MMR3ReadGCVirt(pVM, pvBuffer, Var.u.GCFlat, cb);
5814 break;
5815 case DBGCVAR_TYPE_GC_PHYS:
5816 rc = PGMPhysReadGCPhys(pVM, pvBuffer, Var.u.GCPhys, cb);
5817 break;
5818
5819 case DBGCVAR_TYPE_HC_PHYS:
5820 case DBGCVAR_TYPE_HC_FLAT:
5821 case DBGCVAR_TYPE_HC_FAR:
5822 {
5823 DBGCVAR Var2;
5824 rc = dbgcOpAddrFlat(pDbgc, &Var, &Var2);
5825 if (VBOX_SUCCESS(rc))
5826 {
5827 /** @todo protect this!!! */
5828 memcpy(pvBuffer, Var2.u.pvHCFlat, cb);
5829 rc = 0;
5830 }
5831 else
5832 rc = VERR_INVALID_POINTER;
5833 break;
5834 }
5835
5836 default:
5837 rc = VERR_PARSE_INCORRECT_ARG_TYPE;
5838 }
5839
5840 /*
5841 * Check for failure.
5842 */
5843 if (VBOX_FAILURE(rc))
5844 {
5845 if (pcbRead && (*pcbRead = cbRead - cbLeft) > 0)
5846 return VINF_SUCCESS;
5847 return rc;
5848 }
5849
5850 /*
5851 * Next.
5852 */
5853 cbLeft -= cb;
5854 if (!cbLeft)
5855 break;
5856 pvBuffer = (char *)pvBuffer + cb;
5857 rc = pCmdHlp->pfnEval(pCmdHlp, &Var, "%DV + %d", &Var, cb);
5858 if (VBOX_FAILURE(rc))
5859 {
5860 if (pcbRead && (*pcbRead = cbRead - cbLeft) > 0)
5861 return VINF_SUCCESS;
5862 return rc;
5863 }
5864 }
5865
5866 /*
5867 * Done
5868 */
5869 if (pcbRead)
5870 *pcbRead = cbRead;
5871 return 0;
5872}
5873
5874/**
5875 * Command helper for writing memory specified by a DBGC variable.
5876 *
5877 * @returns VBox status code appropriate to return from a command.
5878 * @param pCmdHlp Pointer to the command callback structure.
5879 * @param pVM VM handle if GC or physical HC address.
5880 * @param pvBuffer What to write.
5881 * @param cbWrite Number of bytes to write.
5882 * @param pVarPointer DBGC variable specifying where to start reading.
5883 * @param pcbWritten Where to store the number of bytes written.
5884 * This is optional. If NULL be aware that some of the buffer
5885 * might have been written to the specified address.
5886 */
5887static DECLCALLBACK(int) dbgcHlpMemWrite(PDBGCCMDHLP pCmdHlp, PVM pVM, const void *pvBuffer, size_t cbWrite, PCDBGCVAR pVarPointer, size_t *pcbWritten)
5888{
5889 NOREF(pCmdHlp); NOREF(pVM); NOREF(pvBuffer); NOREF(cbWrite); NOREF(pVarPointer); NOREF(pcbWritten);
5890 return VERR_NOT_IMPLEMENTED;
5891}
5892
5893
5894/**
5895 * Evaluates an expression.
5896 * (Hopefully the parser and functions are fully reentrant.)
5897 *
5898 * @returns VBox status code appropriate to return from a command.
5899 * @param pCmdHlp Pointer to the command callback structure.
5900 * @param pResult Where to store the result.
5901 * @param pszExpr The expression. Format string with the format DBGC extensions.
5902 * @param ... Format arguments.
5903 */
5904static DECLCALLBACK(int) dbgcHlpEval(PDBGCCMDHLP pCmdHlp, PDBGCVAR pResult, const char *pszExpr, ...)
5905{
5906 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5907
5908 /*
5909 * Format the expression.
5910 */
5911 char szExprFormatted[2048];
5912 va_list args;
5913 va_start(args, pszExpr);
5914 size_t cb = RTStrPrintfExV(dbgcStringFormatter, pDbgc, szExprFormatted, sizeof(szExprFormatted), pszExpr, args);
5915 va_end(args);
5916 /* ignore overflows. */
5917
5918 return dbgcEvalSub(pDbgc, &szExprFormatted[0], cb, pResult);
5919}
5920
5921
5922/**
5923 * Executes one command expression.
5924 * (Hopefully the parser and functions are fully reentrant.)
5925 *
5926 * @returns VBox status code appropriate to return from a command.
5927 * @param pCmdHlp Pointer to the command callback structure.
5928 * @param pszExpr The expression. Format string with the format DBGC extensions.
5929 * @param ... Format arguments.
5930 */
5931static DECLCALLBACK(int) dbgcHlpExec(PDBGCCMDHLP pCmdHlp, const char *pszExpr, ...)
5932{
5933 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5934 /* Save the scratch state. */
5935 char *pszScratch = pDbgc->pszScratch;
5936 unsigned iArg = pDbgc->iArg;
5937
5938 /*
5939 * Format the expression.
5940 */
5941 va_list args;
5942 va_start(args, pszExpr);
5943 size_t cbScratch = sizeof(pDbgc->achScratch) - (pDbgc->pszScratch - &pDbgc->achScratch[0]);
5944 size_t cb = RTStrPrintfExV(dbgcStringFormatter, pDbgc, pDbgc->pszScratch, cbScratch, pszExpr, args);
5945 va_end(args);
5946 if (cb >= cbScratch)
5947 return VERR_BUFFER_OVERFLOW;
5948
5949 /*
5950 * Execute the command.
5951 * We save and restore the arg index and scratch buffer pointer.
5952 */
5953 pDbgc->pszScratch = pDbgc->pszScratch + cb + 1;
5954 int rc = dbgcProcessCommand(pDbgc, pszScratch, cb);
5955
5956 /* Restore the scratch state. */
5957 pDbgc->iArg = iArg;
5958 pDbgc->pszScratch = pszScratch;
5959
5960 return rc;
5961}
5962
5963
5964/**
5965 * Converts a DBGC variable to a DBGF address structure.
5966 *
5967 * @returns VBox status code.
5968 * @param pCmdHlp Pointer to the command callback structure.
5969 * @param pVar The variable to convert.
5970 * @param pAddress The target address.
5971 */
5972static DECLCALLBACK(int) dbgcHlpVarToDbgfAddr(PDBGCCMDHLP pCmdHlp, PCDBGCVAR pVar, PDBGFADDRESS pAddress)
5973{
5974 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5975 return dbgcVarToDbgfAddr(pDbgc, pVar, pAddress);
5976}
5977
5978
5979/**
5980 * Converts a DBGC variable to a boolean.
5981 *
5982 * @returns VBox status code.
5983 * @param pCmdHlp Pointer to the command callback structure.
5984 * @param pVar The variable to convert.
5985 * @param pf Where to store the boolean.
5986 */
5987static DECLCALLBACK(int) dbgcHlpVarToBool(PDBGCCMDHLP pCmdHlp, PCDBGCVAR pVar, bool *pf)
5988{
5989 PDBGC pDbgc = DBGC_CMDHLP2DBGC(pCmdHlp);
5990 NOREF(pDbgc);
5991
5992 switch (pVar->enmType)
5993 {
5994 case DBGCVAR_TYPE_STRING:
5995 /** @todo add strcasecmp / stricmp wrappers to iprt/string.h. */
5996 if ( !strcmp(pVar->u.pszString, "true")
5997 || !strcmp(pVar->u.pszString, "True")
5998 || !strcmp(pVar->u.pszString, "TRUE")
5999 || !strcmp(pVar->u.pszString, "on")
6000 || !strcmp(pVar->u.pszString, "On")
6001 || !strcmp(pVar->u.pszString, "oN")
6002 || !strcmp(pVar->u.pszString, "ON")
6003 || !strcmp(pVar->u.pszString, "enabled")
6004 || !strcmp(pVar->u.pszString, "Enabled")
6005 || !strcmp(pVar->u.pszString, "DISABLED"))
6006 {
6007 *pf = true;
6008 return VINF_SUCCESS;
6009 }
6010 if ( !strcmp(pVar->u.pszString, "false")
6011 || !strcmp(pVar->u.pszString, "False")
6012 || !strcmp(pVar->u.pszString, "FALSE")
6013 || !strcmp(pVar->u.pszString, "off")
6014 || !strcmp(pVar->u.pszString, "Off")
6015 || !strcmp(pVar->u.pszString, "OFF")
6016 || !strcmp(pVar->u.pszString, "disabled")
6017 || !strcmp(pVar->u.pszString, "Disabled")
6018 || !strcmp(pVar->u.pszString, "DISABLED"))
6019 {
6020 *pf = false;
6021 return VINF_SUCCESS;
6022 }
6023 return VERR_PARSE_INCORRECT_ARG_TYPE; /** @todo better error code! */
6024
6025 case DBGCVAR_TYPE_GC_FLAT:
6026 case DBGCVAR_TYPE_GC_PHYS:
6027 case DBGCVAR_TYPE_HC_FLAT:
6028 case DBGCVAR_TYPE_HC_PHYS:
6029 case DBGCVAR_TYPE_NUMBER:
6030 *pf = pVar->u.u64Number != 0;
6031 return VINF_SUCCESS;
6032
6033 case DBGCVAR_TYPE_HC_FAR:
6034 case DBGCVAR_TYPE_GC_FAR:
6035 case DBGCVAR_TYPE_SYMBOL:
6036 default:
6037 return VERR_PARSE_INCORRECT_ARG_TYPE;
6038 }
6039}
6040
6041
6042
6043
6044
6045//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
6046//
6047//
6048// V a r i a b l e M a n i p u l a t i o n
6049//
6050//
6051//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
6052
6053
6054
6055/** @todo move me!*/
6056static void dbgcVarSetGCFlat(PDBGCVAR pVar, RTGCPTR GCFlat)
6057{
6058 if (pVar)
6059 {
6060 pVar->enmType = DBGCVAR_TYPE_GC_FLAT;
6061 pVar->u.GCFlat = GCFlat;
6062 pVar->enmRangeType = DBGCVAR_RANGE_NONE;
6063 pVar->u64Range = 0;
6064 }
6065}
6066
6067
6068/** @todo move me!*/
6069static void dbgcVarSetGCFlatByteRange(PDBGCVAR pVar, RTGCPTR GCFlat, uint64_t cb)
6070{
6071 if (pVar)
6072 {
6073 pVar->enmType = DBGCVAR_TYPE_GC_FLAT;
6074 pVar->u.GCFlat = GCFlat;
6075 pVar->enmRangeType = DBGCVAR_RANGE_BYTES;
6076 pVar->u64Range = cb;
6077 }
6078}
6079
6080
6081/** @todo move me!*/
6082static void dbgcVarSetVar(PDBGCVAR pVar, PCDBGCVAR pVar2)
6083{
6084 if (pVar)
6085 {
6086 if (pVar2)
6087 *pVar = *pVar2;
6088 else
6089 {
6090 pVar->enmType = DBGCVAR_TYPE_UNKNOWN;
6091 memset(&pVar->u, 0, sizeof(pVar->u));
6092 pVar->enmRangeType = DBGCVAR_RANGE_NONE;
6093 pVar->u64Range = 0;
6094 }
6095 }
6096}
6097
6098
6099/** @todo move me!*/
6100static void dbgcVarSetByteRange(PDBGCVAR pVar, uint64_t cb)
6101{
6102 if (pVar)
6103 {
6104 pVar->enmRangeType = DBGCVAR_RANGE_BYTES;
6105 pVar->u64Range = cb;
6106 }
6107}
6108
6109
6110/** @todo move me!*/
6111static void dbgcVarSetNoRange(PDBGCVAR pVar)
6112{
6113 if (pVar)
6114 {
6115 pVar->enmRangeType = DBGCVAR_RANGE_NONE;
6116 pVar->u64Range = 0;
6117 }
6118}
6119
6120
6121/**
6122 * Converts a DBGC variable to a DBGF address.
6123 *
6124 * @returns VBox status code.
6125 * @param pDbgc The DBGC instance.
6126 * @param pVar The variable.
6127 * @param pAddress Where to store the address.
6128 */
6129static int dbgcVarToDbgfAddr(PDBGC pDbgc, PCDBGCVAR pVar, PDBGFADDRESS pAddress)
6130{
6131 AssertReturn(pVar, VERR_INVALID_PARAMETER);
6132 switch (pVar->enmType)
6133 {
6134 case DBGCVAR_TYPE_GC_FLAT:
6135 DBGFR3AddrFromFlat(pDbgc->pVM, pAddress, pVar->u.GCFlat);
6136 return VINF_SUCCESS;
6137
6138 case DBGCVAR_TYPE_NUMBER:
6139 DBGFR3AddrFromFlat(pDbgc->pVM, pAddress, (RTGCUINTPTR)pVar->u.u64Number);
6140 return VINF_SUCCESS;
6141
6142 case DBGCVAR_TYPE_GC_FAR:
6143 return DBGFR3AddrFromSelOff(pDbgc->pVM, pAddress, pVar->u.GCFar.sel, pVar->u.GCFar.sel);
6144
6145 case DBGCVAR_TYPE_STRING:
6146 case DBGCVAR_TYPE_SYMBOL:
6147 {
6148 DBGCVAR Var;
6149 int rc = pDbgc->CmdHlp.pfnEval(&pDbgc->CmdHlp, &Var, "%%(%DV)", pVar);
6150 if (VBOX_FAILURE(rc))
6151 return rc;
6152 return dbgcVarToDbgfAddr(pDbgc, &Var, pAddress);
6153 }
6154
6155 case DBGCVAR_TYPE_GC_PHYS:
6156 case DBGCVAR_TYPE_HC_FLAT:
6157 case DBGCVAR_TYPE_HC_FAR:
6158 case DBGCVAR_TYPE_HC_PHYS:
6159 default:
6160 return VERR_PARSE_CONVERSION_FAILED;
6161 }
6162}
6163
6164
6165
6166//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
6167//
6168//
6169// B r e a k p o i n t M a n a g e m e n t
6170//
6171//
6172//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
6173
6174
6175/**
6176 * Adds a breakpoint to the DBGC breakpoint list.
6177 */
6178static int dbgcBpAdd(PDBGC pDbgc, RTUINT iBp, const char *pszCmd)
6179{
6180 /*
6181 * Check if it already exists.
6182 */
6183 PDBGCBP pBp = dbgcBpGet(pDbgc, iBp);
6184 if (pBp)
6185 return VERR_DBGC_BP_EXISTS;
6186
6187 /*
6188 * Add the breakpoint.
6189 */
6190 if (pszCmd)
6191 pszCmd = RTStrStripL(pszCmd);
6192 size_t cchCmd = pszCmd ? strlen(pszCmd) : 0;
6193 pBp = (PDBGCBP)RTMemAlloc(RT_OFFSETOF(DBGCBP, szCmd[cchCmd + 1]));
6194 if (!pBp)
6195 return VERR_NO_MEMORY;
6196 if (cchCmd)
6197 memcpy(pBp->szCmd, pszCmd, cchCmd + 1);
6198 else
6199 pBp->szCmd[0] = '\0';
6200 pBp->cchCmd = cchCmd;
6201 pBp->iBp = iBp;
6202 pBp->pNext = pDbgc->pFirstBp;
6203 pDbgc->pFirstBp = pBp;
6204
6205 return VINF_SUCCESS;
6206}
6207
6208/**
6209 * Updates the a breakpoint.
6210 *
6211 * @returns VBox status code.
6212 * @param pDbgc The DBGC instance.
6213 * @param iBp The breakpoint to update.
6214 * @param pszCmd The new command.
6215 */
6216static int dbgcBpUpdate(PDBGC pDbgc, RTUINT iBp, const char *pszCmd)
6217{
6218 /*
6219 * Find the breakpoint.
6220 */
6221 PDBGCBP pBp = dbgcBpGet(pDbgc, iBp);
6222 if (!pBp)
6223 return VERR_DBGC_BP_NOT_FOUND;
6224
6225 /*
6226 * Do we need to reallocate?
6227 */
6228 if (pszCmd)
6229 pszCmd = RTStrStripL(pszCmd);
6230 if (!pszCmd || !*pszCmd)
6231 pBp->szCmd[0] = '\0';
6232 else
6233 {
6234 size_t cchCmd = strlen(pszCmd);
6235 if (strlen(pBp->szCmd) >= cchCmd)
6236 {
6237 memcpy(pBp->szCmd, pszCmd, cchCmd + 1);
6238 pBp->cchCmd = cchCmd;
6239 }
6240 else
6241 {
6242 /*
6243 * Yes, let's do it the simple way...
6244 */
6245 int rc = dbgcBpDelete(pDbgc, iBp);
6246 AssertRC(rc);
6247 return dbgcBpAdd(pDbgc, iBp, pszCmd);
6248 }
6249 }
6250 return VINF_SUCCESS;
6251}
6252
6253
6254/**
6255 * Deletes a breakpoint.
6256 *
6257 * @returns VBox status code.
6258 * @param pDbgc The DBGC instance.
6259 * @param iBp The breakpoint to delete.
6260 */
6261static int dbgcBpDelete(PDBGC pDbgc, RTUINT iBp)
6262{
6263 /*
6264 * Search thru the list, when found unlink and free it.
6265 */
6266 PDBGCBP pBpPrev = NULL;
6267 PDBGCBP pBp = pDbgc->pFirstBp;
6268 for (; pBp; pBp = pBp->pNext)
6269 {
6270 if (pBp->iBp == iBp)
6271 {
6272 if (pBpPrev)
6273 pBpPrev->pNext = pBp->pNext;
6274 else
6275 pDbgc->pFirstBp = pBp->pNext;
6276 RTMemFree(pBp);
6277 return VINF_SUCCESS;
6278 }
6279 pBpPrev = pBp;
6280 }
6281
6282 return VERR_DBGC_BP_NOT_FOUND;
6283}
6284
6285
6286/**
6287 * Get a breakpoint.
6288 *
6289 * @returns Pointer to the breakpoint.
6290 * @returns NULL if the breakpoint wasn't found.
6291 * @param pDbgc The DBGC instance.
6292 * @param iBp The breakpoint to get.
6293 */
6294static PDBGCBP dbgcBpGet(PDBGC pDbgc, RTUINT iBp)
6295{
6296 /*
6297 * Enumerate the list.
6298 */
6299 PDBGCBP pBp = pDbgc->pFirstBp;
6300 for (; pBp; pBp = pBp->pNext)
6301 if (pBp->iBp == iBp)
6302 return pBp;
6303 return NULL;
6304}
6305
6306
6307/**
6308 * Executes the command of a breakpoint.
6309 *
6310 * @returns VINF_DBGC_BP_NO_COMMAND if there is no command associated with the breakpoint.
6311 * @returns VERR_DBGC_BP_NOT_FOUND if the breakpoint wasn't found.
6312 * @returns VERR_BUFFER_OVERFLOW if the is not enough space in the scratch buffer for the command.
6313 * @returns VBox status code from dbgcProcessCommand() other wise.
6314 * @param pDbgc The DBGC instance.
6315 * @param iBp The breakpoint to execute.
6316 */
6317static int dbgcBpExec(PDBGC pDbgc, RTUINT iBp)
6318{
6319 /*
6320 * Find the breakpoint.
6321 */
6322 PDBGCBP pBp = dbgcBpGet(pDbgc, iBp);
6323 if (!pBp)
6324 return VERR_DBGC_BP_NOT_FOUND;
6325
6326 /*
6327 * Anything to do?
6328 */
6329 if (!pBp->cchCmd)
6330 return VINF_DBGC_BP_NO_COMMAND;
6331
6332 /*
6333 * Execute the command.
6334 * This means copying it to the scratch buffer and process it as if it
6335 * were user input. We must save and restore the state of the scratch buffer.
6336 */
6337 /* Save the scratch state. */
6338 char *pszScratch = pDbgc->pszScratch;
6339 unsigned iArg = pDbgc->iArg;
6340
6341 /* Copy the command to the scratch buffer. */
6342 size_t cbScratch = sizeof(pDbgc->achScratch) - (pDbgc->pszScratch - &pDbgc->achScratch[0]);
6343 if (pBp->cchCmd >= cbScratch)
6344 return VERR_BUFFER_OVERFLOW;
6345 memcpy(pDbgc->pszScratch, pBp->szCmd, pBp->cchCmd + 1);
6346
6347 /* Execute the command. */
6348 pDbgc->pszScratch = pDbgc->pszScratch + pBp->cchCmd + 1;
6349 int rc = dbgcProcessCommand(pDbgc, pszScratch, pBp->cchCmd);
6350
6351 /* Restore the scratch state. */
6352 pDbgc->iArg = iArg;
6353 pDbgc->pszScratch = pszScratch;
6354
6355 return rc;
6356}
6357
6358
6359
6360
6361
6362//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
6363//
6364//
6365// I n p u t , p a r s i n g a n d l o g g i n g
6366//
6367//
6368//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
6369
6370
6371
6372/**
6373 * Prints any log lines from the log buffer.
6374 *
6375 * The caller must not call function this unless pDbgc->fLog is set.
6376 *
6377 * @returns VBox status. (output related)
6378 * @param pDbgc Debugger console instance data.
6379 */
6380static int dbgcProcessLog(PDBGC pDbgc)
6381{
6382 /** @todo */
6383 NOREF(pDbgc);
6384 return 0;
6385}
6386
6387
6388
6389/**
6390 * Handle input buffer overflow.
6391 *
6392 * Will read any available input looking for a '\n' to reset the buffer on.
6393 *
6394 * @returns VBox status.
6395 * @param pDbgc Debugger console instance data.
6396 */
6397static int dbgcInputOverflow(PDBGC pDbgc)
6398{
6399 /*
6400 * Assert overflow status and reset the input buffer.
6401 */
6402 if (!pDbgc->fInputOverflow)
6403 {
6404 pDbgc->fInputOverflow = true;
6405 pDbgc->iRead = pDbgc->iWrite = 0;
6406 pDbgc->cInputLines = 0;
6407 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "Input overflow!!\n");
6408 }
6409
6410 /*
6411 * Eat input till no more or there is a '\n'.
6412 * When finding a '\n' we'll continue normal processing.
6413 */
6414 while (pDbgc->pBack->pfnInput(pDbgc->pBack, 0))
6415 {
6416 size_t cbRead;
6417 int rc = pDbgc->pBack->pfnRead(pDbgc->pBack, &pDbgc->achInput[0], sizeof(pDbgc->achInput) - 1, &cbRead);
6418 if (VBOX_FAILURE(rc))
6419 return rc;
6420 char *psz = (char *)memchr(&pDbgc->achInput[0], '\n', cbRead);
6421 if (psz)
6422 {
6423 pDbgc->fInputOverflow = false;
6424 pDbgc->iRead = psz - &pDbgc->achInput[0] + 1;
6425 pDbgc->iWrite = cbRead;
6426 pDbgc->cInputLines = 0;
6427 break;
6428 }
6429 }
6430
6431 return 0;
6432}
6433
6434
6435
6436/**
6437 * Read input and do some preprocessing.
6438 *
6439 * @returns VBox status.
6440 * In addition to the iWrite and achInput, cInputLines is maintained.
6441 * In case of an input overflow the fInputOverflow flag will be set.
6442 * @param pDbgc Debugger console instance data.
6443 */
6444static int dbgcInputRead(PDBGC pDbgc)
6445{
6446 /*
6447 * We have ready input.
6448 * Read it till we don't have any or we have a full input buffer.
6449 */
6450 int rc = 0;
6451 do
6452 {
6453 /*
6454 * More available buffer space?
6455 */
6456 size_t cbLeft;
6457 if (pDbgc->iWrite > pDbgc->iRead)
6458 cbLeft = sizeof(pDbgc->achInput) - pDbgc->iWrite - (pDbgc->iRead == 0);
6459 else
6460 cbLeft = pDbgc->iRead - pDbgc->iWrite - 1;
6461 if (!cbLeft)
6462 {
6463 /* overflow? */
6464 if (!pDbgc->cInputLines)
6465 rc = dbgcInputOverflow(pDbgc);
6466 break;
6467 }
6468
6469 /*
6470 * Read one char and interpret it.
6471 */
6472 char achRead[128];
6473 size_t cbRead;
6474 rc = pDbgc->pBack->pfnRead(pDbgc->pBack, &achRead[0], RT_MIN(cbLeft, sizeof(achRead)), &cbRead);
6475 if (VBOX_FAILURE(rc))
6476 return rc;
6477 char *psz = &achRead[0];
6478 while (cbRead-- > 0)
6479 {
6480 char ch = *psz++;
6481 switch (ch)
6482 {
6483 /*
6484 * Ignore.
6485 */
6486 case '\0':
6487 case '\r':
6488 case '\a':
6489 break;
6490
6491 /*
6492 * Backspace.
6493 */
6494 case '\b':
6495 Log2(("DBGC: backspace\n"));
6496 if (pDbgc->iRead != pDbgc->iWrite)
6497 {
6498 unsigned iWriteUndo = pDbgc->iWrite;
6499 if (pDbgc->iWrite)
6500 pDbgc->iWrite--;
6501 else
6502 pDbgc->iWrite = sizeof(pDbgc->achInput) - 1;
6503
6504 if (pDbgc->achInput[pDbgc->iWrite] == '\n')
6505 pDbgc->iWrite = iWriteUndo;
6506 }
6507 break;
6508
6509 /*
6510 * Add char to buffer.
6511 */
6512 case '\t':
6513 case '\n':
6514 case ';':
6515 switch (ch)
6516 {
6517 case '\t': ch = ' '; break;
6518 case '\n': pDbgc->cInputLines++; break;
6519 }
6520 default:
6521 Log2(("DBGC: ch=%02x\n", (unsigned char)ch));
6522 pDbgc->achInput[pDbgc->iWrite] = ch;
6523 if (++pDbgc->iWrite >= sizeof(pDbgc->achInput))
6524 pDbgc->iWrite = 0;
6525 break;
6526 }
6527 }
6528
6529 /* Terminate it to make it easier to read in the debugger. */
6530 pDbgc->achInput[pDbgc->iWrite] = '\0';
6531 } while (pDbgc->pBack->pfnInput(pDbgc->pBack, 0));
6532
6533 return rc;
6534}
6535
6536
6537/**
6538 * Finds a builtin symbol.
6539 * @returns Pointer to symbol descriptor on success.
6540 * @returns NULL on failure.
6541 * @param pDbgc The debug console instance.
6542 * @param pszSymbol The symbol name.
6543 */
6544static PCDBGCSYM dbgcLookupRegisterSymbol(PDBGC pDbgc, const char *pszSymbol)
6545{
6546 for (unsigned iSym = 0; iSym < ELEMENTS(g_aSyms); iSym++)
6547 if (!strcmp(pszSymbol, g_aSyms[iSym].pszName))
6548 return &g_aSyms[iSym];
6549
6550 /** @todo externally registered symbols. */
6551 NOREF(pDbgc);
6552 return NULL;
6553}
6554
6555
6556/**
6557 * Resolves a symbol (or tries to do so at least).
6558 *
6559 * @returns 0 on success.
6560 * @returns VBox status on failure.
6561 * @param pDbgc The debug console instance.
6562 * @param pszSymbol The symbol name.
6563 * @param enmType The result type.
6564 * @param pResult Where to store the result.
6565 */
6566static int dbgcSymbolGet(PDBGC pDbgc, const char *pszSymbol, DBGCVARTYPE enmType, PDBGCVAR pResult)
6567{
6568 /*
6569 * Builtin?
6570 */
6571 PCDBGCSYM pSymDesc = dbgcLookupRegisterSymbol(pDbgc, pszSymbol);
6572 if (pSymDesc)
6573 {
6574 if (!pSymDesc->pfnGet)
6575 return VERR_PARSE_WRITEONLY_SYMBOL;
6576 return pSymDesc->pfnGet(pSymDesc, &pDbgc->CmdHlp, enmType, pResult);
6577 }
6578
6579
6580 /*
6581 * Ask PDM.
6582 */
6583 /** @todo resolve symbols using PDM. */
6584
6585
6586 /*
6587 * Ask the debug info manager.
6588 */
6589 DBGFSYMBOL Symbol;
6590 int rc = DBGFR3SymbolByName(pDbgc->pVM, pszSymbol, &Symbol);
6591 if (VBOX_SUCCESS(rc))
6592 {
6593 /*
6594 * Default return is a flat gc address.
6595 */
6596 memset(pResult, 0, sizeof(*pResult));
6597 pResult->enmRangeType = Symbol.cb ? DBGCVAR_RANGE_BYTES : DBGCVAR_RANGE_NONE;
6598 pResult->u64Range = Symbol.cb;
6599 pResult->enmType = DBGCVAR_TYPE_GC_FLAT;
6600 pResult->u.GCFlat = Symbol.Value;
6601 DBGCVAR VarTmp;
6602 switch (enmType)
6603 {
6604 /* nothing to do. */
6605 case DBGCVAR_TYPE_GC_FLAT:
6606 case DBGCVAR_TYPE_GC_FAR:
6607 case DBGCVAR_TYPE_ANY:
6608 return VINF_SUCCESS;
6609
6610 /* simply make it numeric. */
6611 case DBGCVAR_TYPE_NUMBER:
6612 pResult->enmType = DBGCVAR_TYPE_NUMBER;
6613 pResult->u.u64Number = Symbol.Value;
6614 return VINF_SUCCESS;
6615
6616 /* cast it. */
6617
6618 case DBGCVAR_TYPE_GC_PHYS:
6619 VarTmp = *pResult;
6620 return dbgcOpAddrPhys(pDbgc, &VarTmp, pResult);
6621
6622 case DBGCVAR_TYPE_HC_FAR:
6623 case DBGCVAR_TYPE_HC_FLAT:
6624 VarTmp = *pResult;
6625 return dbgcOpAddrHost(pDbgc, &VarTmp, pResult);
6626
6627 case DBGCVAR_TYPE_HC_PHYS:
6628 VarTmp = *pResult;
6629 return dbgcOpAddrHostPhys(pDbgc, &VarTmp, pResult);
6630
6631 default:
6632 AssertMsgFailed(("Internal error enmType=%d\n", enmType));
6633 return VERR_INVALID_PARAMETER;
6634 }
6635 }
6636
6637 return VERR_PARSE_NOT_IMPLEMENTED;
6638}
6639
6640
6641
6642/**
6643 * Finds a routine.
6644 *
6645 * @returns Pointer to the command descriptor.
6646 * If the request was for an external command, the caller is responsible for
6647 * unlocking the external command list.
6648 * @returns NULL if not found.
6649 * @param pDbgc The debug console instance.
6650 * @param pachName Pointer to the routine string (not terminated).
6651 * @param cchName Length of the routine name.
6652 * @param fExternal Whether or not the routine is external.
6653 */
6654static PCDBGCCMD dbgcRoutineLookup(PDBGC pDbgc, const char *pachName, size_t cchName, bool fExternal)
6655{
6656 if (!fExternal)
6657 {
6658 for (unsigned iCmd = 0; iCmd < ELEMENTS(g_aCmds); iCmd++)
6659 {
6660 if ( !strncmp(pachName, g_aCmds[iCmd].pszCmd, cchName)
6661 && !g_aCmds[iCmd].pszCmd[cchName])
6662 return &g_aCmds[iCmd];
6663 }
6664 }
6665 else
6666 {
6667 DBGCEXTCMDS_LOCK_RD();
6668 for (PDBGCEXTCMDS pExtCmds = g_pExtCmdsHead; pExtCmds; pExtCmds = pExtCmds->pNext)
6669 {
6670 for (unsigned iCmd = 0; iCmd < pExtCmds->cCmds; iCmd++)
6671 {
6672 if ( !strncmp(pachName, pExtCmds->paCmds[iCmd].pszCmd, cchName)
6673 && !pExtCmds->paCmds[iCmd].pszCmd[cchName])
6674 return &pExtCmds->paCmds[iCmd];
6675 }
6676 }
6677 DBGCEXTCMDS_UNLOCK_RD();
6678 }
6679
6680 NOREF(pDbgc);
6681 return NULL;
6682}
6683
6684
6685/**
6686 * Searches for an operator descriptor which matches the start of
6687 * the expression given us.
6688 *
6689 * @returns Pointer to the operator on success.
6690 * @param pDbgc The debug console instance.
6691 * @param pszExpr Pointer to the expression string which might start with an operator.
6692 * @param fPreferBinary Whether to favour binary or unary operators.
6693 * Caller must assert that it's the disired type! Both types will still
6694 * be returned, this is only for resolving duplicates.
6695 * @param chPrev The previous char. Some operators requires a blank in front of it.
6696 */
6697static PCDBGCOP dbgcOperatorLookup(PDBGC pDbgc, const char *pszExpr, bool fPreferBinary, char chPrev)
6698{
6699 PCDBGCOP pOp = NULL;
6700 for (unsigned iOp = 0; iOp < ELEMENTS(g_aOps); iOp++)
6701 {
6702 if ( g_aOps[iOp].szName[0] == pszExpr[0]
6703 && (!g_aOps[iOp].szName[1] || g_aOps[iOp].szName[1] == pszExpr[1])
6704 && (!g_aOps[iOp].szName[2] || g_aOps[iOp].szName[2] == pszExpr[2]))
6705 {
6706 /*
6707 * Check that we don't mistake it for some other operator which have more chars.
6708 */
6709 unsigned j;
6710 for (j = iOp + 1; j < ELEMENTS(g_aOps); j++)
6711 if ( g_aOps[j].cchName > g_aOps[iOp].cchName
6712 && g_aOps[j].szName[0] == pszExpr[0]
6713 && (!g_aOps[j].szName[1] || g_aOps[j].szName[1] == pszExpr[1])
6714 && (!g_aOps[j].szName[2] || g_aOps[j].szName[2] == pszExpr[2]) )
6715 break;
6716 if (j < ELEMENTS(g_aOps))
6717 continue; /* we'll catch it later. (for theoretical +,++,+++ cases.) */
6718 pOp = &g_aOps[iOp];
6719
6720 /*
6721 * Prefered type?
6722 */
6723 if (g_aOps[iOp].fBinary == fPreferBinary)
6724 break;
6725 }
6726 }
6727
6728 if (pOp)
6729 Log2(("dbgcOperatorLookup: pOp=%p %s\n", pOp, pOp->szName));
6730 NOREF(pDbgc); NOREF(chPrev);
6731 return pOp;
6732}
6733
6734
6735
6736static int dbgcEvalSubString(PDBGC pDbgc, char *pszExpr, size_t cchExpr, PDBGCVAR pArg)
6737{
6738 Log2(("dbgcEvalSubString: cchExpr=%d pszExpr=%s\n", cchExpr, pszExpr));
6739
6740 /*
6741 * Removing any quoting and escapings.
6742 */
6743 char ch = *pszExpr;
6744 if (ch == '"' || ch == '\'' || ch == '`')
6745 {
6746 if (pszExpr[--cchExpr] != ch)
6747 return VERR_PARSE_UNBALANCED_QUOTE;
6748 cchExpr--;
6749 pszExpr++;
6750
6751 /** @todo string unescaping. */
6752 }
6753 pszExpr[cchExpr] = '\0';
6754
6755 /*
6756 * Make the argument.
6757 */
6758 pArg->pDesc = NULL;
6759 pArg->pNext = NULL;
6760 pArg->enmType = DBGCVAR_TYPE_STRING;
6761 pArg->u.pszString = pszExpr;
6762 pArg->enmRangeType = DBGCVAR_RANGE_BYTES;
6763 pArg->u64Range = cchExpr;
6764
6765 NOREF(pDbgc);
6766 return 0;
6767}
6768
6769
6770static int dbgcEvalSubNum(char *pszExpr, unsigned uBase, PDBGCVAR pArg)
6771{
6772 Log2(("dbgcEvalSubNum: uBase=%d pszExpr=%s\n", uBase, pszExpr));
6773 /*
6774 * Convert to number.
6775 */
6776 uint64_t u64 = 0;
6777 char ch;
6778 while ((ch = *pszExpr) != '\0')
6779 {
6780 uint64_t u64Prev = u64;
6781 unsigned u = ch - '0';
6782 if (u < 10 && u < uBase)
6783 u64 = u64 * uBase + u;
6784 else if (ch >= 'a' && (u = ch - ('a' - 10)) < uBase)
6785 u64 = u64 * uBase + u;
6786 else if (ch >= 'A' && (u = ch - ('A' - 10)) < uBase)
6787 u64 = u64 * uBase + u;
6788 else
6789 return VERR_PARSE_INVALID_NUMBER;
6790
6791 /* check for overflow - ARG!!! How to detect overflow correctly!?!?!? */
6792 if (u64Prev != u64 / uBase)
6793 return VERR_PARSE_NUMBER_TOO_BIG;
6794
6795 /* next */
6796 pszExpr++;
6797 }
6798
6799 /*
6800 * Initialize the argument.
6801 */
6802 pArg->pDesc = NULL;
6803 pArg->pNext = NULL;
6804 pArg->enmType = DBGCVAR_TYPE_NUMBER;
6805 pArg->u.u64Number = u64;
6806 pArg->enmRangeType = DBGCVAR_RANGE_NONE;
6807 pArg->u64Range = 0;
6808
6809 return 0;
6810}
6811
6812
6813/**
6814 * Match variable and variable descriptor, promoting the variable if necessary.
6815 *
6816 * @returns VBox status code.
6817 * @param pDbgc Debug console instanace.
6818 * @param pVar Variable.
6819 * @param pVarDesc Variable descriptor.
6820 */
6821static int dbgcEvalSubMatchVar(PDBGC pDbgc, PDBGCVAR pVar, PCDBGCVARDESC pVarDesc)
6822{
6823 /*
6824 * (If match or promoted to match, return, else break.)
6825 */
6826 switch (pVarDesc->enmCategory)
6827 {
6828 /*
6829 * Anything goes
6830 */
6831 case DBGCVAR_CAT_ANY:
6832 return VINF_SUCCESS;
6833
6834 /*
6835 * Pointer with and without range.
6836 * We can try resolve strings and symbols as symbols and
6837 * promote numbers to flat GC pointers.
6838 */
6839 case DBGCVAR_CAT_POINTER_NO_RANGE:
6840 if (pVar->enmRangeType != DBGCVAR_RANGE_NONE)
6841 return VERR_PARSE_NO_RANGE_ALLOWED;
6842 /* fallthru */
6843 case DBGCVAR_CAT_POINTER:
6844 switch (pVar->enmType)
6845 {
6846 case DBGCVAR_TYPE_GC_FLAT:
6847 case DBGCVAR_TYPE_GC_FAR:
6848 case DBGCVAR_TYPE_GC_PHYS:
6849 case DBGCVAR_TYPE_HC_FLAT:
6850 case DBGCVAR_TYPE_HC_FAR:
6851 case DBGCVAR_TYPE_HC_PHYS:
6852 return VINF_SUCCESS;
6853
6854 case DBGCVAR_TYPE_SYMBOL:
6855 case DBGCVAR_TYPE_STRING:
6856 {
6857 DBGCVAR Var;
6858 int rc = dbgcSymbolGet(pDbgc, pVar->u.pszString, DBGCVAR_TYPE_GC_FLAT, &Var);
6859 if (VBOX_SUCCESS(rc))
6860 {
6861 /* deal with range */
6862 if (pVar->enmRangeType != DBGCVAR_RANGE_NONE)
6863 {
6864 Var.enmRangeType = pVar->enmRangeType;
6865 Var.u64Range = pVar->u64Range;
6866 }
6867 else if (pVarDesc->enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
6868 Var.enmRangeType = DBGCVAR_RANGE_NONE;
6869 *pVar = Var;
6870 return rc;
6871 }
6872 break;
6873 }
6874
6875 case DBGCVAR_TYPE_NUMBER:
6876 {
6877 RTGCPTR GCPtr = (RTGCPTR)pVar->u.u64Number;
6878 pVar->enmType = DBGCVAR_TYPE_GC_FLAT;
6879 pVar->u.GCFlat = GCPtr;
6880 return VINF_SUCCESS;
6881 }
6882
6883 default:
6884 break;
6885 }
6886 break;
6887
6888 /*
6889 * GC pointer with and without range.
6890 * We can try resolve strings and symbols as symbols and
6891 * promote numbers to flat GC pointers.
6892 */
6893 case DBGCVAR_CAT_GC_POINTER_NO_RANGE:
6894 if (pVar->enmRangeType != DBGCVAR_RANGE_NONE)
6895 return VERR_PARSE_NO_RANGE_ALLOWED;
6896 /* fallthru */
6897 case DBGCVAR_CAT_GC_POINTER:
6898 switch (pVar->enmType)
6899 {
6900 case DBGCVAR_TYPE_GC_FLAT:
6901 case DBGCVAR_TYPE_GC_FAR:
6902 case DBGCVAR_TYPE_GC_PHYS:
6903 return VINF_SUCCESS;
6904
6905 case DBGCVAR_TYPE_HC_FLAT:
6906 case DBGCVAR_TYPE_HC_FAR:
6907 case DBGCVAR_TYPE_HC_PHYS:
6908 return VERR_PARSE_CONVERSION_FAILED;
6909
6910 case DBGCVAR_TYPE_SYMBOL:
6911 case DBGCVAR_TYPE_STRING:
6912 {
6913 DBGCVAR Var;
6914 int rc = dbgcSymbolGet(pDbgc, pVar->u.pszString, DBGCVAR_TYPE_GC_FLAT, &Var);
6915 if (VBOX_SUCCESS(rc))
6916 {
6917 /* deal with range */
6918 if (pVar->enmRangeType != DBGCVAR_RANGE_NONE)
6919 {
6920 Var.enmRangeType = pVar->enmRangeType;
6921 Var.u64Range = pVar->u64Range;
6922 }
6923 else if (pVarDesc->enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
6924 Var.enmRangeType = DBGCVAR_RANGE_NONE;
6925 *pVar = Var;
6926 return rc;
6927 }
6928 break;
6929 }
6930
6931 case DBGCVAR_TYPE_NUMBER:
6932 {
6933 RTGCPTR GCPtr = (RTGCPTR)pVar->u.u64Number;
6934 pVar->enmType = DBGCVAR_TYPE_GC_FLAT;
6935 pVar->u.GCFlat = GCPtr;
6936 return VINF_SUCCESS;
6937 }
6938
6939 default:
6940 break;
6941 }
6942 break;
6943
6944 /*
6945 * Number with or without a range.
6946 * Numbers can be resolved from symbols, but we cannot demote a pointer
6947 * to a number.
6948 */
6949 case DBGCVAR_CAT_NUMBER_NO_RANGE:
6950 if (pVar->enmRangeType != DBGCVAR_RANGE_NONE)
6951 return VERR_PARSE_NO_RANGE_ALLOWED;
6952 /* fallthru */
6953 case DBGCVAR_CAT_NUMBER:
6954 switch (pVar->enmType)
6955 {
6956 case DBGCVAR_TYPE_NUMBER:
6957 return VINF_SUCCESS;
6958
6959 case DBGCVAR_TYPE_SYMBOL:
6960 case DBGCVAR_TYPE_STRING:
6961 {
6962 DBGCVAR Var;
6963 int rc = dbgcSymbolGet(pDbgc, pVar->u.pszString, DBGCVAR_TYPE_NUMBER, &Var);
6964 if (VBOX_SUCCESS(rc))
6965 {
6966 *pVar = Var;
6967 return rc;
6968 }
6969 break;
6970 }
6971 default:
6972 break;
6973 }
6974 break;
6975
6976 /*
6977 * Strings can easily be made from symbols (and of course strings).
6978 * We could consider reformatting the addresses and numbers into strings later...
6979 */
6980 case DBGCVAR_CAT_STRING:
6981 switch (pVar->enmType)
6982 {
6983 case DBGCVAR_TYPE_SYMBOL:
6984 pVar->enmType = DBGCVAR_TYPE_STRING;
6985 /* fallthru */
6986 case DBGCVAR_TYPE_STRING:
6987 return VINF_SUCCESS;
6988 default:
6989 break;
6990 }
6991 break;
6992
6993 /*
6994 * Symol is pretty much the same thing as a string (at least until we actually implement it).
6995 */
6996 case DBGCVAR_CAT_SYMBOL:
6997 switch (pVar->enmType)
6998 {
6999 case DBGCVAR_TYPE_STRING:
7000 pVar->enmType = DBGCVAR_TYPE_SYMBOL;
7001 /* fallthru */
7002 case DBGCVAR_TYPE_SYMBOL:
7003 return VINF_SUCCESS;
7004 default:
7005 break;
7006 }
7007 break;
7008
7009 /*
7010 * Anything else is illegal.
7011 */
7012 default:
7013 AssertMsgFailed(("enmCategory=%d\n", pVar->enmType));
7014 break;
7015 }
7016
7017 return VERR_PARSE_NO_ARGUMENT_MATCH;
7018}
7019
7020
7021/**
7022 * Matches a set of variables with a description set.
7023 *
7024 * This is typically used for routine arguments before a call. The effects in
7025 * addition to the validation, is that some variables might be propagated to
7026 * other types in order to match the description. The following transformations
7027 * are supported:
7028 * - String reinterpreted as a symbol and resolved to a number or pointer.
7029 * - Number to a pointer.
7030 * - Pointer to a number.
7031 * @returns 0 on success with paVars.
7032 * @returns VBox error code for match errors.
7033 */
7034static int dbgcEvalSubMatchVars(PDBGC pDbgc, unsigned cVarsMin, unsigned cVarsMax,
7035 PCDBGCVARDESC paVarDescs, unsigned cVarDescs,
7036 PDBGCVAR paVars, unsigned cVars)
7037{
7038 /*
7039 * Just do basic min / max checks first.
7040 */
7041 if (cVars < cVarsMin)
7042 return VERR_PARSE_TOO_FEW_ARGUMENTS;
7043 if (cVars > cVarsMax)
7044 return VERR_PARSE_TOO_MANY_ARGUMENTS;
7045
7046 /*
7047 * Match the descriptors and actual variables.
7048 */
7049 PCDBGCVARDESC pPrevDesc = NULL;
7050 unsigned cCurDesc = 0;
7051 unsigned iVar = 0;
7052 unsigned iVarDesc = 0;
7053 while (iVar < cVars)
7054 {
7055 /* walk the descriptors */
7056 if (iVarDesc >= cVarDescs)
7057 return VERR_PARSE_TOO_MANY_ARGUMENTS;
7058 if ( ( paVarDescs[iVarDesc].fFlags & DBGCVD_FLAGS_DEP_PREV
7059 && &paVarDescs[iVarDesc - 1] != pPrevDesc)
7060 || cCurDesc >= paVarDescs[iVarDesc].cTimesMax)
7061 {
7062 iVarDesc++;
7063 if (iVarDesc >= cVarDescs)
7064 return VERR_PARSE_TOO_MANY_ARGUMENTS;
7065 cCurDesc = 0;
7066 }
7067
7068 /*
7069 * Skip thru optional arguments until we find something which matches
7070 * or can easily be promoted to what the descriptor want.
7071 */
7072 for (;;)
7073 {
7074 int rc = dbgcEvalSubMatchVar(pDbgc, &paVars[iVar], &paVarDescs[iVarDesc]);
7075 if (VBOX_SUCCESS(rc))
7076 {
7077 paVars[iVar].pDesc = &paVarDescs[iVarDesc];
7078 cCurDesc++;
7079 break;
7080 }
7081
7082 /* can we advance? */
7083 if (paVarDescs[iVarDesc].cTimesMin > cCurDesc)
7084 return VERR_PARSE_ARGUMENT_TYPE_MISMATCH;
7085 if (++iVarDesc >= cVarDescs)
7086 return VERR_PARSE_ARGUMENT_TYPE_MISMATCH;
7087 cCurDesc = 0;
7088 }
7089
7090 /* next var */
7091 iVar++;
7092 }
7093
7094 /*
7095 * Check that the rest of the descriptors are optional.
7096 */
7097 while (iVarDesc < cVarDescs)
7098 {
7099 if (paVarDescs[iVarDesc].cTimesMin > cCurDesc)
7100 return VERR_PARSE_TOO_FEW_ARGUMENTS;
7101 cCurDesc = 0;
7102
7103 /* next */
7104 iVarDesc++;
7105 }
7106
7107 return 0;
7108}
7109
7110
7111/**
7112 * Evaluates one argument with though of unary operators.
7113 *
7114 * @returns 0 on success. pResult contains the result.
7115 * @returns VBox error code on parse or other evaluation error.
7116 *
7117 * @param pDbgc Debugger console instance data.
7118 * @param pszExpr The expression string.
7119 * @param pResult Where to store the result of the expression evaluation.
7120 */
7121static int dbgcEvalSubUnary(PDBGC pDbgc, char *pszExpr, size_t cchExpr, PDBGCVAR pResult)
7122{
7123 Log2(("dbgcEvalSubUnary: cchExpr=%d pszExpr=%s\n", cchExpr, pszExpr));
7124
7125 /*
7126 * The state of the expression is now such that it will start by zero or more
7127 * unary operators and being followed by an expression of some kind.
7128 * The expression is either plain or in parenthesis.
7129 *
7130 * Being in a lazy, recursive mode today, the parsing is done as simple as possible. :-)
7131 * ASSUME: unary operators are all of equal precedence.
7132 */
7133 int rc = 0;
7134 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, pszExpr, false, ' ');
7135 if (pOp)
7136 {
7137 /* binary operators means syntax error. */
7138 if (pOp->fBinary)
7139 return VERR_PARSE_UNEXPECTED_OPERATOR;
7140
7141 /*
7142 * If the next expression (the one following the unary operator) is in a
7143 * parenthesis a full eval is needed. If not the unary eval will suffice.
7144 */
7145 /* calc and strip next expr. */
7146 char *pszExpr2 = pszExpr + pOp->cchName;
7147 while (isblank(*pszExpr2))
7148 pszExpr2++;
7149
7150 if (!*pszExpr2)
7151 rc = VERR_PARSE_EMPTY_ARGUMENT;
7152 else
7153 {
7154 DBGCVAR Arg;
7155 if (*pszExpr2 == '(')
7156 rc = dbgcEvalSub(pDbgc, pszExpr2, cchExpr - (pszExpr2 - pszExpr), &Arg);
7157 else
7158 rc = dbgcEvalSubUnary(pDbgc, pszExpr2, cchExpr - (pszExpr2 - pszExpr), &Arg);
7159 if (VBOX_SUCCESS(rc))
7160 rc = pOp->pfnHandlerUnary(pDbgc, &Arg, pResult);
7161 }
7162 }
7163 else
7164 {
7165 /*
7166 * Didn't find any operators, so it we have to check if this can be an
7167 * function call before assuming numeric or string expression.
7168 *
7169 * (ASSUMPTIONS:)
7170 * A function name only contains alphanumerical chars and it can not start
7171 * with a numerical character.
7172 * Immediately following the name is a parenthesis which must over
7173 * the remaining part of the expression.
7174 */
7175 bool fExternal = *pszExpr == '.';
7176 char *pszFun = fExternal ? pszExpr + 1 : pszExpr;
7177 char *pszFunEnd = NULL;
7178 if (pszExpr[cchExpr - 1] == ')' && isalpha(*pszFun))
7179 {
7180 pszFunEnd = pszExpr + 1;
7181 while (*pszFunEnd != '(' && isalnum(*pszFunEnd))
7182 pszFunEnd++;
7183 if (*pszFunEnd != '(')
7184 pszFunEnd = NULL;
7185 }
7186
7187 if (pszFunEnd)
7188 {
7189 /*
7190 * Ok, it's a function call.
7191 */
7192 if (fExternal)
7193 pszExpr++, cchExpr--;
7194 PCDBGCCMD pFun = dbgcRoutineLookup(pDbgc, pszExpr, pszFunEnd - pszExpr, fExternal);
7195 if (!pFun)
7196 return VERR_PARSE_FUNCTION_NOT_FOUND;
7197 if (!pFun->pResultDesc)
7198 return VERR_PARSE_NOT_A_FUNCTION;
7199
7200 /*
7201 * Parse the expression in parenthesis.
7202 */
7203 cchExpr -= pszFunEnd - pszExpr;
7204 pszExpr = pszFunEnd;
7205 /** @todo implement multiple arguments. */
7206 DBGCVAR Arg;
7207 rc = dbgcEvalSub(pDbgc, pszExpr, cchExpr, &Arg);
7208 if (!rc)
7209 {
7210 rc = dbgcEvalSubMatchVars(pDbgc, pFun->cArgsMin, pFun->cArgsMax, pFun->paArgDescs, pFun->cArgDescs, &Arg, 1);
7211 if (!rc)
7212 rc = pFun->pfnHandler(pFun, &pDbgc->CmdHlp, pDbgc->pVM, &Arg, 1, pResult);
7213 }
7214 else if (rc == VERR_PARSE_EMPTY_ARGUMENT && pFun->cArgsMin == 0)
7215 rc = pFun->pfnHandler(pFun, &pDbgc->CmdHlp, pDbgc->pVM, NULL, 0, pResult);
7216 }
7217 else
7218 {
7219 /*
7220 * Didn't find any operators, so it must be a plain expression.
7221 * This might be numeric or a string expression.
7222 */
7223 char ch = pszExpr[0];
7224 char ch2 = pszExpr[1];
7225 if (ch == '0' && (ch2 == 'x' || ch2 == 'X'))
7226 rc = dbgcEvalSubNum(pszExpr + 2, 16, pResult);
7227 else if (ch == '0' && (ch2 == 'i' || ch2 == 'i'))
7228 rc = dbgcEvalSubNum(pszExpr + 2, 10, pResult);
7229 else if (ch == '0' && (ch2 == 't' || ch2 == 'T'))
7230 rc = dbgcEvalSubNum(pszExpr + 2, 8, pResult);
7231 else if (ch == '0' && (ch2 == 'b' || ch2 == 'b'))
7232 rc = dbgcEvalSubNum(pszExpr + 2, 2, pResult);
7233 else
7234 {
7235 /*
7236 * Hexadecimal number or a string?
7237 */
7238 char *psz = pszExpr;
7239 while (isxdigit(*psz))
7240 psz++;
7241 if (!*psz)
7242 rc = dbgcEvalSubNum(pszExpr, 16, pResult);
7243 else if ((*psz == 'h' || *psz == 'H') && !psz[1])
7244 {
7245 *psz = '\0';
7246 rc = dbgcEvalSubNum(pszExpr, 16, pResult);
7247 }
7248 else
7249 rc = dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
7250 }
7251 }
7252 }
7253
7254 return rc;
7255}
7256
7257
7258/**
7259 * Evaluates one argument.
7260 *
7261 * @returns 0 on success. pResult contains the result.
7262 * @returns VBox error code on parse or other evaluation error.
7263 *
7264 * @param pDbgc Debugger console instance data.
7265 * @param pszExpr The expression string.
7266 * @param pResult Where to store the result of the expression evaluation.
7267 */
7268static int dbgcEvalSub(PDBGC pDbgc, char *pszExpr, size_t cchExpr, PDBGCVAR pResult)
7269{
7270 Log2(("dbgcEvalSub: cchExpr=%d pszExpr=%s\n", cchExpr, pszExpr));
7271 /*
7272 * First we need to remove blanks in both ends.
7273 * ASSUMES: There is no quoting unless the entire expression is a string.
7274 */
7275
7276 /* stripping. */
7277 while (cchExpr > 0 && isblank(pszExpr[cchExpr - 1]))
7278 pszExpr[--cchExpr] = '\0';
7279 while (isblank(*pszExpr))
7280 pszExpr++, cchExpr--;
7281 if (!*pszExpr)
7282 return VERR_PARSE_EMPTY_ARGUMENT;
7283
7284 /* it there is any kind of quoting in the expression, it's string meat. */
7285 if (strpbrk(pszExpr, "\"'`"))
7286 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
7287
7288 /*
7289 * Check if there are any parenthesis which needs removing.
7290 */
7291 if (pszExpr[0] == '(' && pszExpr[cchExpr - 1] == ')')
7292 {
7293 do
7294 {
7295 unsigned cPar = 1;
7296 char *psz = pszExpr + 1;
7297 char ch;
7298 while ((ch = *psz) != '\0')
7299 {
7300 if (ch == '(')
7301 cPar++;
7302 else if (ch == ')')
7303 {
7304 if (cPar <= 0)
7305 return VERR_PARSE_UNBALANCED_PARENTHESIS;
7306 cPar--;
7307 if (cPar == 0 && psz[1]) /* If not at end, there's nothing to do. */
7308 break;
7309 }
7310 /* next */
7311 psz++;
7312 }
7313 if (ch)
7314 break;
7315
7316 /* remove the parenthesis. */
7317 pszExpr++;
7318 cchExpr -= 2;
7319 pszExpr[cchExpr] = '\0';
7320
7321 /* strip blanks. */
7322 while (cchExpr > 0 && isblank(pszExpr[cchExpr - 1]))
7323 pszExpr[--cchExpr] = '\0';
7324 while (isblank(*pszExpr))
7325 pszExpr++, cchExpr--;
7326 if (!*pszExpr)
7327 return VERR_PARSE_EMPTY_ARGUMENT;
7328 } while (pszExpr[0] == '(' && pszExpr[cchExpr - 1] == ')');
7329 }
7330
7331 /* tabs to spaces. */
7332 char *psz = pszExpr;
7333 while ((psz = strchr(psz, '\t')) != NULL)
7334 *psz = ' ';
7335
7336 /*
7337 * Now, we need to look for the binary operator with the lowest precedence.
7338 *
7339 * If there are no operators we're left with a simple expression which we
7340 * evaluate with respect to unary operators
7341 */
7342 char *pszOpSplit = NULL;
7343 PCDBGCOP pOpSplit = NULL;
7344 unsigned iOpSplit = 0;
7345 unsigned cBinaryOps = 0;
7346 unsigned cPar = 0;
7347 char ch;
7348 char chPrev = ' ';
7349 bool fBinary = false;
7350 psz = pszExpr;
7351
7352 while ((ch = *psz) != '\0')
7353 {
7354 //Log2(("ch=%c cPar=%d fBinary=%d\n", ch, cPar, fBinary));
7355 /*
7356 * Parenthesis.
7357 */
7358 if (ch == '(')
7359 {
7360 cPar++;
7361 fBinary = false;
7362 }
7363 else if (ch == ')')
7364 {
7365 if (cPar <= 0)
7366 return VERR_PARSE_UNBALANCED_PARENTHESIS;
7367 cPar++;
7368 fBinary = true;
7369 }
7370 /*
7371 * Potential operator.
7372 */
7373 else if (cPar == 0 && !isblank(ch))
7374 {
7375 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, psz, fBinary, chPrev);
7376 if (pOp)
7377 {
7378 /* If not the right kind of operator we've got a syntax error. */
7379 if (pOp->fBinary != fBinary)
7380 return VERR_PARSE_UNEXPECTED_OPERATOR;
7381
7382 /*
7383 * Update the parse state and skip the operator.
7384 */
7385 if (fBinary)
7386 {
7387 cBinaryOps++;
7388 if (iOpSplit < pOp->iPrecedence)
7389 {
7390 pOpSplit = pOp;
7391 pszOpSplit = psz;
7392 }
7393 }
7394 psz += pOp->cchName - 1;
7395 fBinary = false;
7396 }
7397 else
7398 fBinary = true;
7399 }
7400
7401 /* next */
7402 psz++;
7403 chPrev = ch;
7404 } /* parse loop. */
7405
7406
7407 /*
7408 * Either we found an operator to divide the expression by
7409 * or we didn't find any. In the first case it's divide and
7410 * conquer. In the latter it's a single expression which
7411 * needs dealing with its unary operators if any.
7412 */
7413 int rc;
7414 if (cBinaryOps)
7415 {
7416 /* process 1st sub expression. */
7417 *pszOpSplit = '\0';
7418 DBGCVAR Arg1;
7419 rc = dbgcEvalSub(pDbgc, pszExpr, pszOpSplit - pszExpr, &Arg1);
7420 if (VBOX_SUCCESS(rc))
7421 {
7422 /* process 2nd sub expression. */
7423 char *psz2 = pszOpSplit + pOpSplit->cchName;
7424 DBGCVAR Arg2;
7425 rc = dbgcEvalSub(pDbgc, psz2, cchExpr - (psz2 - pszExpr), &Arg2);
7426 if (VBOX_SUCCESS(rc))
7427 /* apply the operator. */
7428 rc = pOpSplit->pfnHandlerBinary(pDbgc, &Arg1, &Arg2, pResult);
7429 }
7430 }
7431 else
7432 /* plain expression or using unary operators perhaps with paratheses. */
7433 rc = dbgcEvalSubUnary(pDbgc, pszExpr, cchExpr, pResult);
7434
7435 return rc;
7436}
7437
7438
7439/**
7440 * Parses the arguments of one command.
7441 *
7442 * @returns 0 on success.
7443 * @returns VBox error code on parse error with *pcArg containing the argument causing trouble.
7444 * @param pDbgc Debugger console instance data.
7445 * @param pCmd Pointer to the command descriptor.
7446 * @param pszArg Pointer to the arguments to parse.
7447 * @param paArgs Where to store the parsed arguments.
7448 * @param cArgs Size of the paArgs array.
7449 * @param pcArgs Where to store the number of arguments.
7450 * In the event of an error this is used to store the index of the offending argument.
7451 */
7452static int dbgcProcessArguments(PDBGC pDbgc, PCDBGCCMD pCmd, char *pszArgs, PDBGCVAR paArgs, unsigned cArgs, unsigned *pcArgs)
7453{
7454 Log2(("dbgcProcessArguments: pCmd=%s pszArgs='%s'\n", pCmd->pszCmd, pszArgs));
7455 /*
7456 * Check if we have any argument and if the command takes any.
7457 */
7458 *pcArgs = 0;
7459 /* strip leading blanks. */
7460 while (*pszArgs && isblank(*pszArgs))
7461 pszArgs++;
7462 if (!*pszArgs)
7463 {
7464 if (!pCmd->cArgsMin)
7465 return 0;
7466 return VERR_PARSE_TOO_FEW_ARGUMENTS;
7467 }
7468 /** @todo fixme - foo() doesn't work. */
7469 if (!pCmd->cArgsMax)
7470 return VERR_PARSE_TOO_MANY_ARGUMENTS;
7471
7472 /*
7473 * This is a hack, it's "temporary" and should go away "when" the parser is
7474 * modified to match arguments while parsing.
7475 */
7476 if ( pCmd->cArgsMax == 1
7477 && pCmd->cArgsMin == 1
7478 && pCmd->cArgDescs == 1
7479 && pCmd->paArgDescs[0].enmCategory == DBGCVAR_CAT_STRING
7480 && cArgs >= 1)
7481 {
7482 *pcArgs = 1;
7483 RTStrStripR(pszArgs);
7484 return dbgcEvalSubString(pDbgc, pszArgs, strlen(pszArgs), &paArgs[0]);
7485 }
7486
7487
7488 /*
7489 * The parse loop.
7490 */
7491 PDBGCVAR pArg0 = &paArgs[0];
7492 PDBGCVAR pArg = pArg0;
7493 *pcArgs = 0;
7494 do
7495 {
7496 /*
7497 * Can we have another argument?
7498 */
7499 if (*pcArgs >= pCmd->cArgsMax)
7500 return VERR_PARSE_TOO_MANY_ARGUMENTS;
7501 if (pArg >= &paArgs[cArgs])
7502 return VERR_PARSE_ARGUMENT_OVERFLOW;
7503
7504 /*
7505 * Find the end of the argument.
7506 */
7507 int cPar = 0;
7508 char chQuote = '\0';
7509 char *pszEnd = NULL;
7510 char *psz = pszArgs;
7511 char ch;
7512 for (;;)
7513 {
7514 /*
7515 * Check for the end.
7516 */
7517 if ((ch = *psz) == '\0')
7518 {
7519 if (chQuote)
7520 return VERR_PARSE_UNBALANCED_QUOTE;
7521 if (cPar)
7522 return VERR_PARSE_UNBALANCED_PARENTHESIS;
7523 pszEnd = psz;
7524 break;
7525 }
7526 /*
7527 * When quoted we ignore everything but the quotation char.
7528 * We use the REXX way of escaping the quotation char, i.e. double occurence.
7529 */
7530 else if (ch == '\'' || ch == '"' || ch == '`')
7531 {
7532 if (chQuote)
7533 {
7534 /* end quote? */
7535 if (ch == chQuote)
7536 {
7537 if (psz[1] == ch)
7538 psz++; /* skip the escaped quote char */
7539 else
7540 chQuote = '\0'; /* end of quoted string. */
7541 }
7542 }
7543 else
7544 chQuote = ch; /* open new quote */
7545 }
7546 /*
7547 * Parenthesis can of course be nested.
7548 */
7549 else if (ch == '(')
7550 cPar++;
7551 else if (ch == ')')
7552 {
7553 if (!cPar)
7554 return VERR_PARSE_UNBALANCED_PARENTHESIS;
7555 cPar--;
7556 }
7557 /*
7558 * Encountering blanks may mean the end of it all. But of course not
7559 * while inside a quotation or paranthesis. A binary operator will
7560 * also force continued parsing.
7561 */
7562 else if (isblank(ch) && !cPar && !chQuote)
7563 {
7564 pszEnd = psz++; /* just in case. */
7565 while (isblank(*psz))
7566 psz++;
7567 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, psz, true, ' ');
7568 if (!pOp || !pOp->fBinary)
7569 break; /* the end. */
7570 if (pOp)
7571 psz += pOp->cchName;
7572
7573 while (isblank(*psz)) /* skip blanks so we don't get here again */
7574 psz++;
7575 continue;
7576 }
7577
7578 /* next char */
7579 psz++;
7580 }
7581 *pszEnd = '\0';
7582 /* (psz = next char to process) */
7583
7584 /*
7585 * Parse and evaluate the argument.
7586 */
7587 int rc = dbgcEvalSub(pDbgc, pszArgs, strlen(pszArgs), pArg);
7588 if (VBOX_FAILURE(rc))
7589 return rc;
7590
7591 /*
7592 * Next.
7593 */
7594 pArg++;
7595 (*pcArgs)++;
7596 pszArgs = psz;
7597 while (*pszArgs && isblank(*pszArgs))
7598 pszArgs++;
7599 } while (*pszArgs);
7600
7601 /*
7602 * Match the arguments.
7603 */
7604 return dbgcEvalSubMatchVars(pDbgc, pCmd->cArgsMin, pCmd->cArgsMax, pCmd->paArgDescs, pCmd->cArgDescs, pArg0, pArg - pArg0);
7605}
7606
7607
7608/**
7609 * Process one command.
7610 *
7611 * @returns VBox status code. Any error indicates the termination of the console session.
7612 * @param pDbgc Debugger console instance data.
7613 * @param pszCmd Pointer to the command.
7614 * @param cchCmd Length of the command.
7615 */
7616static int dbgcProcessCommand(PDBGC pDbgc, char *pszCmd, size_t cchCmd)
7617{
7618 char *pszCmdInput = pszCmd;
7619
7620 /*
7621 * Skip blanks.
7622 */
7623 while (isblank(*pszCmd))
7624 pszCmd++, cchCmd--;
7625
7626 /* external command? */
7627 bool fExternal = *pszCmd == '.';
7628 if (fExternal)
7629 pszCmd++, cchCmd--;
7630
7631 /*
7632 * Find arguments.
7633 */
7634 char *pszArgs = pszCmd;
7635 while (isalnum(*pszArgs))
7636 pszArgs++;
7637 if (*pszArgs && (!isblank(*pszArgs) || pszArgs == pszCmd))
7638 {
7639 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "Syntax error in command '%s'!\n", pszCmdInput);
7640 return 0;
7641 }
7642
7643 /*
7644 * Find the command.
7645 */
7646 PCDBGCCMD pCmd = dbgcRoutineLookup(pDbgc, pszCmd, pszArgs - pszCmd, fExternal);
7647 if (!pCmd || (pCmd->fFlags & DBGCCMD_FLAGS_FUNCTION))
7648 return pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "Unknown command '%s'!\n", pszCmdInput);
7649
7650 /*
7651 * Parse arguments (if any).
7652 */
7653 unsigned cArgs;
7654 int rc = dbgcProcessArguments(pDbgc, pCmd, pszArgs, &pDbgc->aArgs[pDbgc->iArg], ELEMENTS(pDbgc->aArgs) - pDbgc->iArg, &cArgs);
7655
7656 /*
7657 * Execute the command.
7658 */
7659 if (!rc)
7660 {
7661 rc = pCmd->pfnHandler(pCmd, &pDbgc->CmdHlp, pDbgc->pVM, &pDbgc->aArgs[0], cArgs, NULL);
7662 }
7663 else
7664 {
7665 /* report parse / eval error. */
7666 switch (rc)
7667 {
7668 case VERR_PARSE_TOO_FEW_ARGUMENTS:
7669 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7670 "Syntax error: Too few arguments. Minimum is %d for command '%s'.\n", pCmd->cArgsMin, pCmd->pszCmd);
7671 break;
7672 case VERR_PARSE_TOO_MANY_ARGUMENTS:
7673 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7674 "Syntax error: Too many arguments. Maximum is %d for command '%s'.\n", pCmd->cArgsMax, pCmd->pszCmd);
7675 break;
7676 case VERR_PARSE_ARGUMENT_OVERFLOW:
7677 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7678 "Syntax error: Too many arguments.\n");
7679 break;
7680 case VERR_PARSE_UNBALANCED_QUOTE:
7681 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7682 "Syntax error: Unbalanced quote (argument %d).\n", cArgs);
7683 break;
7684 case VERR_PARSE_UNBALANCED_PARENTHESIS:
7685 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7686 "Syntax error: Unbalanced parenthesis (argument %d).\n", cArgs);
7687 break;
7688 case VERR_PARSE_EMPTY_ARGUMENT:
7689 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7690 "Syntax error: An argument or subargument contains nothing useful (argument %d).\n", cArgs);
7691 break;
7692 case VERR_PARSE_UNEXPECTED_OPERATOR:
7693 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7694 "Syntax error: Invalid operator usage (argument %d).\n", cArgs);
7695 break;
7696 case VERR_PARSE_INVALID_NUMBER:
7697 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7698 "Syntax error: Ivalid numeric value (argument %d). If a string was the intention, then quote it.\n", cArgs);
7699 break;
7700 case VERR_PARSE_NUMBER_TOO_BIG:
7701 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7702 "Error: Numeric overflow (argument %d).\n", cArgs);
7703 break;
7704 case VERR_PARSE_INVALID_OPERATION:
7705 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7706 "Error: Invalid operation attempted (argument %d).\n", cArgs);
7707 break;
7708 case VERR_PARSE_FUNCTION_NOT_FOUND:
7709 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7710 "Error: Function not found (argument %d).\n", cArgs);
7711 break;
7712 case VERR_PARSE_NOT_A_FUNCTION:
7713 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7714 "Error: The function specified is not a function (argument %d).\n", cArgs);
7715 break;
7716 case VERR_PARSE_NO_MEMORY:
7717 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7718 "Error: Out memory in the regular heap! Expect odd stuff to happen...\n", cArgs);
7719 break;
7720 case VERR_PARSE_INCORRECT_ARG_TYPE:
7721 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7722 "Error: Incorrect argument type (argument %d?).\n", cArgs);
7723 break;
7724 case VERR_PARSE_VARIABLE_NOT_FOUND:
7725 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7726 "Error: An undefined variable was referenced (argument %d).\n", cArgs);
7727 break;
7728 case VERR_PARSE_CONVERSION_FAILED:
7729 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7730 "Error: A conversion between two types failed (argument %d).\n", cArgs);
7731 break;
7732 case VERR_PARSE_NOT_IMPLEMENTED:
7733 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7734 "Error: You hit a debugger feature which isn't implemented yet (argument %d).\n", cArgs);
7735 break;
7736 case VERR_PARSE_BAD_RESULT_TYPE:
7737 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7738 "Error: Couldn't satisfy a request for a specific result type (argument %d). (Usually applies to symbols)\n", cArgs);
7739 break;
7740 case VERR_PARSE_WRITEONLY_SYMBOL:
7741 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7742 "Error: Cannot get symbol, it's set only (argument %d).\n", cArgs);
7743 break;
7744
7745 default:
7746 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7747 "Error: Unknown error %d!\n", rc);
7748 return rc;
7749 }
7750
7751 /*
7752 * Parse errors are non fatal.
7753 */
7754 if (rc >= VERR_PARSE_FIRST && rc < VERR_PARSE_LAST)
7755 rc = 0;
7756 }
7757
7758 return rc;
7759}
7760
7761
7762/**
7763 * Process all commands current in the buffer.
7764 *
7765 * @returns VBox status code. Any error indicates the termination of the console session.
7766 * @param pDbgc Debugger console instance data.
7767 */
7768static int dbgcProcessCommands(PDBGC pDbgc)
7769{
7770 int rc = 0;
7771 while (pDbgc->cInputLines)
7772 {
7773 /*
7774 * Empty the log buffer if we're hooking the log.
7775 */
7776 if (pDbgc->fLog)
7777 {
7778 rc = dbgcProcessLog(pDbgc);
7779 if (VBOX_FAILURE(rc))
7780 break;
7781 }
7782
7783 if (pDbgc->iRead == pDbgc->iWrite)
7784 {
7785 AssertMsgFailed(("The input buffer is empty while cInputLines=%d!\n", pDbgc->cInputLines));
7786 pDbgc->cInputLines = 0;
7787 return 0;
7788 }
7789
7790 /*
7791 * Copy the command to the parse buffer.
7792 */
7793 char ch;
7794 char *psz = &pDbgc->achInput[pDbgc->iRead];
7795 char *pszTrg = &pDbgc->achScratch[0];
7796 while ((*pszTrg = ch = *psz++) != ';' && ch != '\n' )
7797 {
7798 if (psz == &pDbgc->achInput[sizeof(pDbgc->achInput)])
7799 psz = &pDbgc->achInput[0];
7800
7801 if (psz == &pDbgc->achInput[pDbgc->iWrite])
7802 {
7803 AssertMsgFailed(("The buffer contains no commands while cInputLines=%d!\n", pDbgc->cInputLines));
7804 pDbgc->cInputLines = 0;
7805 return 0;
7806 }
7807
7808 pszTrg++;
7809 }
7810 *pszTrg = '\0';
7811
7812 /*
7813 * Advance the buffer.
7814 */
7815 pDbgc->iRead = psz - &pDbgc->achInput[0];
7816 if (ch == '\n')
7817 pDbgc->cInputLines--;
7818
7819 /*
7820 * Parse and execute this command.
7821 */
7822 pDbgc->pszScratch = psz;
7823 pDbgc->iArg = 0;
7824 rc = dbgcProcessCommand(pDbgc, &pDbgc->achScratch[0], psz - &pDbgc->achScratch[0] - 1);
7825 if (rc)
7826 break;
7827 }
7828
7829 return rc;
7830}
7831
7832
7833/**
7834 * Reads input, parses it and executes commands on '\n'.
7835 *
7836 * @returns VBox status.
7837 * @param pDbgc Debugger console instance data.
7838 */
7839static int dbgcProcessInput(PDBGC pDbgc)
7840{
7841 /*
7842 * We know there's input ready, so let's read it first.
7843 */
7844 int rc = dbgcInputRead(pDbgc);
7845 if (VBOX_FAILURE(rc))
7846 return rc;
7847
7848 /*
7849 * Now execute any ready commands.
7850 */
7851 if (pDbgc->cInputLines)
7852 {
7853 /** @todo this fReady stuff is broken. */
7854 pDbgc->fReady = false;
7855 rc = dbgcProcessCommands(pDbgc);
7856 if (VBOX_SUCCESS(rc) && rc != VWRN_DBGC_CMD_PENDING)
7857 pDbgc->fReady = true;
7858 if ( VBOX_SUCCESS(rc)
7859 && pDbgc->iRead == pDbgc->iWrite
7860 && pDbgc->fReady)
7861 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
7862 }
7863
7864 return rc;
7865}
7866
7867
7868/**
7869 * Gets the event context identifier string.
7870 * @returns Read only string.
7871 * @param enmCtx The context.
7872 */
7873static const char *dbgcGetEventCtx(DBGFEVENTCTX enmCtx)
7874{
7875 switch (enmCtx)
7876 {
7877 case DBGFEVENTCTX_RAW: return "raw";
7878 case DBGFEVENTCTX_REM: return "rem";
7879 case DBGFEVENTCTX_HWACCL: return "hwaccl";
7880 case DBGFEVENTCTX_HYPER: return "hyper";
7881 case DBGFEVENTCTX_OTHER: return "other";
7882
7883 case DBGFEVENTCTX_INVALID: return "!Invalid Event Ctx!";
7884 default:
7885 AssertMsgFailed(("enmCtx=%d\n", enmCtx));
7886 return "!Unknown Event Ctx!";
7887 }
7888}
7889
7890
7891/**
7892 * Processes debugger events.
7893 *
7894 * @returns VBox status.
7895 * @param pDbgc DBGC Instance data.
7896 * @param pEvent Pointer to event data.
7897 */
7898static int dbgcProcessEvent(PDBGC pDbgc, PCDBGFEVENT pEvent)
7899{
7900 /*
7901 * Flush log first.
7902 */
7903 if (pDbgc->fLog)
7904 {
7905 int rc = dbgcProcessLog(pDbgc);
7906 if (VBOX_FAILURE(rc))
7907 return rc;
7908 }
7909
7910 /*
7911 * Process the event.
7912 */
7913 pDbgc->pszScratch = &pDbgc->achInput[0];
7914 pDbgc->iArg = 0;
7915 bool fPrintPrompt = true;
7916 int rc = VINF_SUCCESS;
7917 switch (pEvent->enmType)
7918 {
7919 /*
7920 * The first part is events we have initiated with commands.
7921 */
7922 case DBGFEVENT_HALT_DONE:
7923 {
7924 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: VM %p is halted! (%s)\n",
7925 pDbgc->pVM, dbgcGetEventCtx(pEvent->enmCtx));
7926 pDbgc->fRegCtxGuest = true; /* we're always in guest context when halted. */
7927 if (VBOX_SUCCESS(rc))
7928 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
7929 break;
7930 }
7931
7932
7933 /*
7934 * The second part is events which can occur at any time.
7935 */
7936 case DBGFEVENT_FATAL_ERROR:
7937 {
7938 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbf event: Fatal error! (%s)\n",
7939 dbgcGetEventCtx(pEvent->enmCtx));
7940 pDbgc->fRegCtxGuest = false; /* fatal errors are always in hypervisor. */
7941 if (VBOX_SUCCESS(rc))
7942 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
7943 break;
7944 }
7945
7946 case DBGFEVENT_BREAKPOINT:
7947 case DBGFEVENT_BREAKPOINT_HYPER:
7948 {
7949 bool fRegCtxGuest = pDbgc->fRegCtxGuest;
7950 pDbgc->fRegCtxGuest = pEvent->enmType == DBGFEVENT_BREAKPOINT;
7951
7952 rc = dbgcBpExec(pDbgc, pEvent->u.Bp.iBp);
7953 switch (rc)
7954 {
7955 case VERR_DBGC_BP_NOT_FOUND:
7956 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Unknown breakpoint %u! (%s)\n",
7957 pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
7958 break;
7959
7960 case VINF_DBGC_BP_NO_COMMAND:
7961 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! (%s)\n",
7962 pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
7963 break;
7964
7965 case VINF_BUFFER_OVERFLOW:
7966 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! Command too long to execute! (%s)\n",
7967 pEvent->u.Bp.iBp, dbgcGetEventCtx(pEvent->enmCtx));
7968 break;
7969
7970 default:
7971 break;
7972 }
7973 if (VBOX_SUCCESS(rc) && DBGFR3IsHalted(pDbgc->pVM))
7974 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
7975 else
7976 pDbgc->fRegCtxGuest = fRegCtxGuest;
7977 break;
7978 }
7979
7980 case DBGFEVENT_STEPPED:
7981 case DBGFEVENT_STEPPED_HYPER:
7982 {
7983 pDbgc->fRegCtxGuest = pEvent->enmType == DBGFEVENT_STEPPED;
7984
7985 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Single step! (%s)\n", dbgcGetEventCtx(pEvent->enmCtx));
7986 if (VBOX_SUCCESS(rc))
7987 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
7988 break;
7989 }
7990
7991 case DBGFEVENT_ASSERTION_HYPER:
7992 {
7993 pDbgc->fRegCtxGuest = false;
7994
7995 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
7996 "\ndbgf event: Hypervisor Assertion! (%s)\n"
7997 "%s"
7998 "%s"
7999 "\n",
8000 dbgcGetEventCtx(pEvent->enmCtx),
8001 pEvent->u.Assert.pszMsg1,
8002 pEvent->u.Assert.pszMsg2);
8003 if (VBOX_SUCCESS(rc))
8004 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
8005 break;
8006 }
8007
8008 case DBGFEVENT_DEV_STOP:
8009 {
8010 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
8011 "\n"
8012 "dbgf event: DBGFSTOP (%s)\n"
8013 "File: %s\n"
8014 "Line: %d\n"
8015 "Function: %s\n",
8016 dbgcGetEventCtx(pEvent->enmCtx),
8017 pEvent->u.Src.pszFile,
8018 pEvent->u.Src.uLine,
8019 pEvent->u.Src.pszFunction);
8020 if (VBOX_SUCCESS(rc) && pEvent->u.Src.pszMessage && *pEvent->u.Src.pszMessage)
8021 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
8022 "Message: %s\n",
8023 pEvent->u.Src.pszMessage);
8024 if (VBOX_SUCCESS(rc))
8025 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
8026 break;
8027 }
8028
8029
8030 case DBGFEVENT_INVALID_COMMAND:
8031 {
8032 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Invalid command event!\n");
8033 fPrintPrompt = !pDbgc->fReady;
8034 break;
8035 }
8036
8037 case DBGFEVENT_TERMINATING:
8038 {
8039 pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\nVM is terminating!\n");
8040 rc = VERR_GENERAL_FAILURE;
8041 break;
8042 }
8043
8044
8045 default:
8046 {
8047 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Unknown event %d!\n", pEvent->enmType);
8048 fPrintPrompt = !pDbgc->fReady;
8049 break;
8050 }
8051 }
8052
8053 /*
8054 * Prompt, anyone?
8055 */
8056 if (fPrintPrompt && VBOX_SUCCESS(rc))
8057 {
8058 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "VBoxDbg> ");
8059 }
8060
8061 return rc;
8062}
8063
8064
8065
8066
8067
8068/**
8069 * Make a console instance.
8070 *
8071 * This will not return until either an 'exit' command is issued or a error code
8072 * indicating connection loss is encountered.
8073 *
8074 * @returns VINF_SUCCESS if console termination caused by the 'exit' command.
8075 * @returns The VBox status code causing the console termination.
8076 *
8077 * @param pVM VM Handle.
8078 * @param pBack Pointer to the backend structure. This must contain
8079 * a full set of function pointers to service the console.
8080 * @param fFlags Reserved, must be zero.
8081 * @remark A forced termination of the console is easiest done by forcing the
8082 * callbacks to return fatal failures.
8083 */
8084DBGDECL(int) DBGCCreate(PVM pVM, PDBGCBACK pBack, unsigned fFlags)
8085{
8086 /*
8087 * Validate input.
8088 */
8089 AssertReturn(VALID_PTR(pVM), VERR_INVALID_PARAMETER);
8090 AssertReturn(VALID_PTR(pBack), VERR_INVALID_PARAMETER);
8091 AssertMsgReturn(!fFlags, ("%#x", fFlags), VERR_INVALID_PARAMETER);
8092
8093 /*
8094 * Allocate and initialize instance data
8095 */
8096 PDBGC pDbgc = (PDBGC)RTMemAllocZ(sizeof(*pDbgc));
8097 if (!pDbgc)
8098 return VERR_NO_MEMORY;
8099
8100 pDbgc->CmdHlp.pfnWrite = dbgcHlpWrite;
8101 pDbgc->CmdHlp.pfnPrintfV = dbgcHlpPrintfV;
8102 pDbgc->CmdHlp.pfnPrintf = dbgcHlpPrintf;
8103 pDbgc->CmdHlp.pfnVBoxErrorV = dbgcHlpVBoxErrorV;
8104 pDbgc->CmdHlp.pfnVBoxError = dbgcHlpVBoxError;
8105 pDbgc->CmdHlp.pfnMemRead = dbgcHlpMemRead;
8106 pDbgc->CmdHlp.pfnMemWrite = dbgcHlpMemWrite;
8107 pDbgc->CmdHlp.pfnEval = dbgcHlpEval;
8108 pDbgc->CmdHlp.pfnExec = dbgcHlpExec;
8109 pDbgc->CmdHlp.pfnVarToDbgfAddr = dbgcHlpVarToDbgfAddr;
8110 pDbgc->CmdHlp.pfnVarToBool = dbgcHlpVarToBool;
8111 pDbgc->pBack = pBack;
8112 pDbgc->pszScratch = &pDbgc->achScratch[0];
8113 pDbgc->fRegTerse = true;
8114 pDbgc->fRegCtxGuest = true;
8115 pDbgc->fLog = false;
8116 pDbgc->iRead = 0;
8117 pDbgc->iWrite = 0;
8118 pDbgc->fReady = true;
8119 pDbgc->fInputOverflow = false;
8120 pDbgc->cInputLines = 0;
8121
8122 /*
8123 * Print welcome message.
8124 */
8125 int rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
8126 "Welcome to the VirtualBox Debugger!\n");
8127 if (VBOX_FAILURE(rc))
8128 goto l_failure;
8129
8130 /*
8131 * Attach to the VM.
8132 */
8133 rc = DBGFR3Attach(pVM);
8134 if (VBOX_FAILURE(rc))
8135 {
8136 rc = pDbgc->CmdHlp.pfnVBoxError(&pDbgc->CmdHlp, rc, "When trying to attach to VM %p\n", pDbgc->pVM);
8137 goto l_failure;
8138 }
8139 pDbgc->pVM = pVM;
8140
8141 /*
8142 * Print commandline and auto select result.
8143 */
8144 rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
8145 "Current VM is %08x\n" /** @todo get and print the VM name! */
8146 "VBoxDbg> ",
8147 pDbgc->pVM);
8148 if (VBOX_FAILURE(rc))
8149 goto l_failure;
8150
8151 /*
8152 * Main Debugger Loop.
8153 *
8154 * This loop will either block on waiting for input or on waiting on
8155 * debug events. If we're forwarding the log we cannot wait for long
8156 * before we must flush the log.
8157 */
8158 for (rc = 0;;)
8159 {
8160 if (pDbgc->pVM && DBGFR3CanWait(pDbgc->pVM))
8161 {
8162 /*
8163 * Wait for a debug event.
8164 */
8165 PCDBGFEVENT pEvent;
8166 rc = DBGFR3EventWait(pDbgc->pVM, pDbgc->fLog ? 1 : 32, &pEvent);
8167 if (VBOX_SUCCESS(rc))
8168 {
8169 rc = dbgcProcessEvent(pDbgc, pEvent);
8170 if (VBOX_FAILURE(rc))
8171 break;
8172 }
8173 else if (rc != VERR_TIMEOUT)
8174 break;
8175
8176 /*
8177 * Check for input.
8178 */
8179 if (pBack->pfnInput(pDbgc->pBack, 0))
8180 {
8181 rc = dbgcProcessInput(pDbgc);
8182 if (VBOX_FAILURE(rc))
8183 break;
8184 }
8185 }
8186 else
8187 {
8188 /*
8189 * Wait for input. If Logging is enabled we'll only wait very briefly.
8190 */
8191 if (pBack->pfnInput(pDbgc->pBack, pDbgc->fLog ? 1 : 1000))
8192 {
8193 rc = dbgcProcessInput(pDbgc);
8194 if (VBOX_FAILURE(rc))
8195 break;
8196 }
8197 }
8198
8199 /*
8200 * Forward log output.
8201 */
8202 if (pDbgc->fLog)
8203 {
8204 rc = dbgcProcessLog(pDbgc);
8205 if (VBOX_FAILURE(rc))
8206 break;
8207 }
8208 }
8209
8210
8211l_failure:
8212 /*
8213 * Cleanup console debugger session.
8214 */
8215 /* Disable log hook. */
8216 if (pDbgc->fLog)
8217 {
8218
8219 }
8220
8221 /* Detach from the VM. */
8222 if (pDbgc->pVM)
8223 DBGFR3Detach(pDbgc->pVM);
8224
8225 /* finally, free the instance memory. */
8226 RTMemFree(pDbgc);
8227
8228 return rc;
8229}
8230
8231
8232
8233/**
8234 * Register one or more external commands.
8235 *
8236 * @returns VBox status.
8237 * @param paCommands Pointer to an array of command descriptors.
8238 * The commands must be unique. It's not possible
8239 * to register the same commands more than once.
8240 * @param cCommands Number of commands.
8241 */
8242DBGDECL(int) DBGCRegisterCommands(PCDBGCCMD paCommands, unsigned cCommands)
8243{
8244 /*
8245 * Lock the list.
8246 */
8247 DBGCEXTCMDS_LOCK_WR();
8248 PDBGCEXTCMDS pCur = g_pExtCmdsHead;
8249 while (pCur)
8250 {
8251 if (paCommands == pCur->paCmds)
8252 {
8253 DBGCEXTCMDS_UNLOCK_WR();
8254 AssertMsgFailed(("Attempt at re-registering %d command(s)!\n", cCommands));
8255 return VWRN_DBGC_ALREADY_REGISTERED;
8256 }
8257 pCur = pCur->pNext;
8258 }
8259
8260 /*
8261 * Allocate new chunk.
8262 */
8263 int rc = 0;
8264 pCur = (PDBGCEXTCMDS)RTMemAlloc(sizeof(*pCur));
8265 if (pCur)
8266 {
8267 pCur->cCmds = cCommands;
8268 pCur->paCmds = paCommands;
8269 pCur->pNext = g_pExtCmdsHead;
8270 g_pExtCmdsHead = pCur;
8271 }
8272 else
8273 rc = VERR_NO_MEMORY;
8274 DBGCEXTCMDS_UNLOCK_WR();
8275
8276 return rc;
8277}
8278
8279
8280/**
8281 * Deregister one or more external commands previously registered by
8282 * DBGCRegisterCommands().
8283 *
8284 * @returns VBox status.
8285 * @param paCommands Pointer to an array of command descriptors
8286 * as given to DBGCRegisterCommands().
8287 * @param cCommands Number of commands.
8288 */
8289DBGDECL(int) DBGCDeregisterCommands(PCDBGCCMD paCommands, unsigned cCommands)
8290{
8291 /*
8292 * Lock the list.
8293 */
8294 DBGCEXTCMDS_LOCK_WR();
8295 PDBGCEXTCMDS pPrev = NULL;
8296 PDBGCEXTCMDS pCur = g_pExtCmdsHead;
8297 while (pCur)
8298 {
8299 if (paCommands == pCur->paCmds)
8300 {
8301 if (pPrev)
8302 pPrev->pNext = pCur->pNext;
8303 else
8304 g_pExtCmdsHead = pCur->pNext;
8305 DBGCEXTCMDS_UNLOCK_WR();
8306
8307 RTMemFree(pCur);
8308 return VINF_SUCCESS;
8309 }
8310 pPrev = pCur;
8311 pCur = pCur->pNext;
8312 }
8313 DBGCEXTCMDS_UNLOCK_WR();
8314
8315 NOREF(cCommands);
8316 return VERR_DBGC_COMMANDS_NOT_REGISTERED;
8317}
8318
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