VirtualBox

source: vbox/trunk/src/VBox/Debugger/DBGCEval.cpp@ 57358

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

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.1 KB
Line 
1/* $Id: DBGCEval.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, command evaluator.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGC
23#include <VBox/dbg.h>
24#include <VBox/err.h>
25#include <VBox/log.h>
26
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/mem.h>
30#include <iprt/string.h>
31#include <iprt/ctype.h>
32
33#include "DBGCInternal.h"
34
35/** Rewrite in progress. */
36#define BETTER_ARGUMENT_MATCHING
37
38
39/*********************************************************************************************************************************
40* Global Variables *
41*********************************************************************************************************************************/
42/** Bitmap where set bits indicates the characters the may start an operator name. */
43static uint32_t g_bmOperatorChars[256 / (4*8)];
44
45
46/*********************************************************************************************************************************
47* Internal Functions *
48*********************************************************************************************************************************/
49static int dbgcCheckAndTypePromoteArgument(PDBGC pDbgc, DBGCVARCAT enmCategory, PDBGCVAR pArg);
50static int dbgcProcessArguments(PDBGC pDbgc, const char *pszCmdOrFunc,
51 uint32_t const cArgsMin, uint32_t const cArgsMax,
52 PCDBGCVARDESC const paVarDescs, uint32_t const cVarDescs,
53 char *pszArgs, unsigned *piArg, unsigned *pcArgs);
54
55
56
57/**
58 * Initializes g_bmOperatorChars.
59 */
60void dbgcEvalInit(void)
61{
62 memset(g_bmOperatorChars, 0, sizeof(g_bmOperatorChars));
63 for (unsigned iOp = 0; iOp < g_cDbgcOps; iOp++)
64 ASMBitSet(&g_bmOperatorChars[0], (uint8_t)g_aDbgcOps[iOp].szName[0]);
65}
66
67
68/**
69 * Checks whether the character may be the start of an operator.
70 *
71 * @returns true/false.
72 * @param ch The character.
73 */
74DECLINLINE(bool) dbgcIsOpChar(char ch)
75{
76 return ASMBitTest(&g_bmOperatorChars[0], (uint8_t)ch);
77}
78
79
80/**
81 * Returns the amount of free scratch space.
82 *
83 * @returns Number of unallocated bytes.
84 * @param pDbgc The DBGC instance.
85 */
86size_t dbgcGetFreeScratchSpace(PDBGC pDbgc)
87{
88 return sizeof(pDbgc->achScratch) - (pDbgc->pszScratch - &pDbgc->achScratch[0]);
89}
90
91
92/**
93 * Allocates a string from the scratch space.
94 *
95 * @returns Pointer to the allocated string buffer, NULL if out of space.
96 * @param pDbgc The DBGC instance.
97 * @param cbRequested The number of bytes to allocate.
98 */
99char *dbgcAllocStringScatch(PDBGC pDbgc, size_t cbRequested)
100{
101 if (cbRequested > dbgcGetFreeScratchSpace(pDbgc))
102 return NULL;
103 char *psz = pDbgc->pszScratch;
104 pDbgc->pszScratch += cbRequested;
105 return psz;
106}
107
108
109/**
110 * Evals an expression into a string or symbol (single quotes).
111 *
112 * The string memory is allocated from the scratch buffer.
113 *
114 * @returns VBox status code.
115 * @param pDbgc The DBGC instance.
116 * @param pachExpr The string/symbol expression.
117 * @param cchExpr The length of the expression.
118 * @param pArg Where to return the string.
119 */
120static int dbgcEvalSubString(PDBGC pDbgc, const char *pachExpr, size_t cchExpr, PDBGCVAR pArg)
121{
122 Log2(("dbgcEvalSubString: cchExpr=%d pachExpr=%.*s\n", cchExpr, cchExpr, pachExpr));
123
124 /*
125 * Allocate scratch space for the string.
126 */
127 char *pszCopy = dbgcAllocStringScatch(pDbgc, cchExpr + 1);
128 if (!pszCopy)
129 return VERR_DBGC_PARSE_NO_SCRATCH;
130
131 /*
132 * Removing any quoting and escapings.
133 */
134 char const chQuote = *pachExpr;
135 if (chQuote == '"' || chQuote == '\'')
136 {
137 if (pachExpr[--cchExpr] != chQuote)
138 return VERR_DBGC_PARSE_UNBALANCED_QUOTE;
139
140 cchExpr--;
141 pachExpr++;
142 if (!memchr(pachExpr, chQuote, cchExpr))
143 memcpy(pszCopy, pachExpr, cchExpr);
144 else
145 {
146 size_t offSrc = 0;
147 size_t offDst = 0;
148 while (offSrc < cchExpr)
149 {
150 char const ch = pachExpr[offSrc++];
151 if (ch == chQuote)
152 {
153 if (pachExpr[offSrc] != ch)
154 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
155 offSrc++;
156 }
157 pszCopy[offDst++] = ch;
158 }
159 }
160 }
161 else
162 memcpy(pszCopy, pachExpr, cchExpr);
163 pszCopy[cchExpr] = '\0';
164
165 /*
166 * Make the argument.
167 */
168 pArg->pDesc = NULL;
169 pArg->pNext = NULL;
170 pArg->enmType = chQuote == '"' ? DBGCVAR_TYPE_STRING : DBGCVAR_TYPE_SYMBOL;
171 pArg->u.pszString = pszCopy;
172 pArg->enmRangeType = DBGCVAR_RANGE_BYTES;
173 pArg->u64Range = cchExpr;
174
175 NOREF(pDbgc);
176 return VINF_SUCCESS;
177}
178
179
180static int dbgcEvalSubNum(const char *pachExpr, size_t cchExpr, unsigned uBase, PDBGCVAR pArg)
181{
182 Log2(("dbgcEvalSubNum: uBase=%d pachExpr=%.*s\n", uBase, cchExpr, pachExpr));
183
184 /*
185 * Empty expressions cannot be valid numbers.
186 */
187 if (!cchExpr)
188 return VERR_DBGC_PARSE_INVALID_NUMBER;
189
190 /*
191 * Convert to number.
192 */
193 uint64_t u64 = 0;
194 while (cchExpr-- > 0)
195 {
196 char const ch = *pachExpr;
197 uint64_t u64Prev = u64;
198 unsigned u = ch - '0';
199 if (u < 10 && u < uBase)
200 u64 = u64 * uBase + u;
201 else if (ch >= 'a' && (u = ch - ('a' - 10)) < uBase)
202 u64 = u64 * uBase + u;
203 else if (ch >= 'A' && (u = ch - ('A' - 10)) < uBase)
204 u64 = u64 * uBase + u;
205 else
206 return VERR_DBGC_PARSE_INVALID_NUMBER;
207
208 /* check for overflow - ARG!!! How to detect overflow correctly!?!?!? */
209 if (u64Prev != u64 / uBase)
210 return VERR_DBGC_PARSE_NUMBER_TOO_BIG;
211
212 /* next */
213 pachExpr++;
214 }
215
216 /*
217 * Initialize the argument.
218 */
219 pArg->pDesc = NULL;
220 pArg->pNext = NULL;
221 pArg->enmType = DBGCVAR_TYPE_NUMBER;
222 pArg->u.u64Number = u64;
223 pArg->enmRangeType = DBGCVAR_RANGE_NONE;
224 pArg->u64Range = 0;
225
226 return VINF_SUCCESS;
227}
228
229
230/**
231 * dbgcEvalSubUnary worker that handles simple numeric or pointer expressions.
232 *
233 * @returns VBox status code. pResult contains the result on success.
234 * @param pDbgc Debugger console instance data.
235 * @param pszExpr The expression string.
236 * @param cchExpr The length of the expression.
237 * @param enmCategory The desired type category (for range / no range).
238 * @param pResult Where to store the result of the expression evaluation.
239 */
240static int dbgcEvalSubNumericOrPointer(PDBGC pDbgc, char *pszExpr, size_t cchExpr, DBGCVARCAT enmCategory,
241 PDBGCVAR pResult)
242{
243 char const ch = pszExpr[0];
244 char const ch2 = pszExpr[1];
245
246 /* 0x<hex digits> */
247 if (ch == '0' && (ch2 == 'x' || ch2 == 'X'))
248 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 16, pResult);
249
250 /* <hex digits>h */
251 if (RT_C_IS_XDIGIT(*pszExpr) && (pszExpr[cchExpr - 1] == 'h' || pszExpr[cchExpr - 1] == 'H'))
252 {
253 pszExpr[cchExpr] = '\0';
254 return dbgcEvalSubNum(pszExpr, cchExpr - 1, 16, pResult);
255 }
256
257 /* 0i<decimal digits> */
258 if (ch == '0' && ch2 == 'i')
259 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 10, pResult);
260
261 /* 0t<octal digits> */
262 if (ch == '0' && ch2 == 't')
263 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 8, pResult);
264
265 /* 0y<binary digits> */
266 if (ch == '0' && ch2 == 'y')
267 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 10, pResult);
268
269 /* Hex number? */
270 unsigned off = 0;
271 while (off < cchExpr && (RT_C_IS_XDIGIT(pszExpr[off]) || pszExpr[off] == '`'))
272 off++;
273 if (off == cchExpr)
274 return dbgcEvalSubNum(pszExpr, cchExpr, 16, pResult);
275
276 /*
277 * Some kind of symbol? Rejected double quoted strings, only unquoted
278 * and single quoted strings will be considered as symbols.
279 */
280 DBGCVARTYPE enmType;
281 bool fStripRange = false;
282 switch (enmCategory)
283 {
284 case DBGCVAR_CAT_POINTER_NUMBER: enmType = DBGCVAR_TYPE_NUMBER; break;
285 case DBGCVAR_CAT_POINTER_NUMBER_NO_RANGE: enmType = DBGCVAR_TYPE_NUMBER; fStripRange = true; break;
286 case DBGCVAR_CAT_POINTER: enmType = DBGCVAR_TYPE_NUMBER; break;
287 case DBGCVAR_CAT_POINTER_NO_RANGE: enmType = DBGCVAR_TYPE_NUMBER; fStripRange = true; break;
288 case DBGCVAR_CAT_GC_POINTER: enmType = DBGCVAR_TYPE_GC_FLAT; break;
289 case DBGCVAR_CAT_GC_POINTER_NO_RANGE: enmType = DBGCVAR_TYPE_GC_FLAT; fStripRange = true; break;
290 case DBGCVAR_CAT_NUMBER: enmType = DBGCVAR_TYPE_NUMBER; break;
291 case DBGCVAR_CAT_NUMBER_NO_RANGE: enmType = DBGCVAR_TYPE_NUMBER; fStripRange = true; break;
292 default:
293 AssertFailedReturn(VERR_DBGC_PARSE_NOT_IMPLEMENTED);
294 }
295
296 char const chQuote = *pszExpr;
297 if (chQuote == '"')
298 return VERR_DBGC_PARSE_INVALID_NUMBER;
299
300 if (chQuote == '\'')
301 {
302 if (pszExpr[cchExpr - 1] != chQuote)
303 return VERR_DBGC_PARSE_UNBALANCED_QUOTE;
304 pszExpr[cchExpr - 1] = '\0';
305 pszExpr++;
306 }
307
308 int rc = dbgcSymbolGet(pDbgc, pszExpr, enmType, pResult);
309 if (RT_SUCCESS(rc))
310 {
311 if (fStripRange)
312 {
313 pResult->enmRangeType = DBGCVAR_RANGE_NONE;
314 pResult->u64Range = 0;
315 }
316 }
317 else if (rc == VERR_DBGC_PARSE_NOT_IMPLEMENTED)
318 rc = VERR_DBGC_PARSE_INVALID_NUMBER;
319 return rc;
320}
321
322
323/**
324 * dbgcEvalSubUnary worker that handles simple DBGCVAR_CAT_ANY expressions.
325 *
326 * @returns VBox status code. pResult contains the result on success.
327 * @param pDbgc Debugger console instance data.
328 * @param pszExpr The expression string.
329 * @param cchExpr The length of the expression.
330 * @param pResult Where to store the result of the expression evaluation.
331 */
332static int dbgcEvalSubUnaryAny(PDBGC pDbgc, char *pszExpr, size_t cchExpr, PDBGCVAR pResult)
333{
334 char const ch = pszExpr[0];
335 char const ch2 = pszExpr[1];
336 unsigned off = 2;
337
338 /* 0x<hex digits> */
339 if (ch == '0' && (ch2 == 'x' || ch2 == 'X'))
340 {
341 while (RT_C_IS_XDIGIT(pszExpr[off]) || pszExpr[off] == '`')
342 off++;
343 if (off == cchExpr)
344 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 16, pResult);
345 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
346 }
347
348 /* <hex digits>h */
349 if (RT_C_IS_XDIGIT(*pszExpr) && (pszExpr[cchExpr - 1] == 'h' || pszExpr[cchExpr - 1] == 'H'))
350 {
351 cchExpr--;
352 while (off < cchExpr && (RT_C_IS_XDIGIT(pszExpr[off]) || pszExpr[off] == '`'))
353 off++;
354 if (off == cchExpr)
355 {
356 pszExpr[cchExpr] = '\0';
357 return dbgcEvalSubNum(pszExpr, cchExpr, 16, pResult);
358 }
359 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr + 1, pResult);
360 }
361
362 /* 0n<decimal digits> or 0i<decimal digits> */
363 if (ch == '0' && (ch2 == 'n' || ch2 == 'i'))
364 {
365 while (RT_C_IS_DIGIT(pszExpr[off]) || pszExpr[off] == '`')
366 off++;
367 if (off == cchExpr)
368 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 10, pResult);
369 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
370 }
371
372 /* 0t<octal digits> */
373 if (ch == '0' && ch2 == 't')
374 {
375 while (RT_C_IS_ODIGIT(pszExpr[off]) || pszExpr[off] == '`')
376 off++;
377 if (off == cchExpr)
378 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 8, pResult);
379 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
380 }
381
382 /* 0y<binary digits> */
383 if (ch == '0' && ch2 == 'y')
384 {
385 while (pszExpr[off] == '0' || pszExpr[off] == '1' || pszExpr[off] == '`')
386 off++;
387 if (off == cchExpr)
388 return dbgcEvalSubNum(pszExpr + 2, cchExpr - 2, 10, pResult);
389 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
390 }
391
392 /* Ok, no prefix of suffix. Is it a hex number after all? If not it must
393 be a string. */
394 off = 0;
395 while (RT_C_IS_XDIGIT(pszExpr[off]) || pszExpr[off] == '`')
396 off++;
397 if (off == cchExpr)
398 return dbgcEvalSubNum(pszExpr, cchExpr, 16, pResult);
399 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
400}
401
402
403/**
404 * Handles a call.
405 *
406 * @returns VBox status code. pResult contains the result on success.
407 * @param pDbgc The DBGC instance.
408 * @param pszFuncNm The function name.
409 * @param cchFuncNm The length of the function name.
410 * @param fExternal Whether it's an external name.
411 * @param pszArgs The start of the arguments (after parenthesis).
412 * @param cchArgs The length for the argument (exclusing
413 * parentesis).
414 * @param enmCategory The desired category of the result (ignored).
415 * @param pResult The result.
416 */
417static int dbgcEvalSubCall(PDBGC pDbgc, char *pszFuncNm, size_t cchFuncNm, bool fExternal, char *pszArgs, size_t cchArgs,
418 DBGCVARCAT enmCategory, PDBGCVAR pResult)
419{
420 /*
421 * Lookup the function.
422 */
423 PCDBGCFUNC pFunc = dbgcFunctionLookup(pDbgc, pszFuncNm, cchFuncNm, fExternal);
424 if (!pFunc)
425 return VERR_DBGC_PARSE_FUNCTION_NOT_FOUND;
426
427 /*
428 * Parse the arguments.
429 */
430 unsigned cArgs;
431 unsigned iArg;
432 pszArgs[cchArgs] = '\0';
433 int rc = dbgcProcessArguments(pDbgc, pFunc->pszFuncNm,
434 pFunc->cArgsMin, pFunc->cArgsMax, pFunc->paArgDescs, pFunc->cArgDescs,
435 pszArgs, &iArg, &cArgs);
436 if (RT_SUCCESS(rc))
437 rc = pFunc->pfnHandler(pFunc, &pDbgc->CmdHlp, pDbgc->pUVM, &pDbgc->aArgs[iArg], cArgs, pResult);
438 pDbgc->iArg = iArg;
439 return rc;
440}
441
442
443/**
444 * Evaluates one argument with respect to unary operators.
445 *
446 * @returns VBox status code. pResult contains the result on success.
447 *
448 * @param pDbgc Debugger console instance data.
449 * @param pszExpr The expression string.
450 * @param cchExpr The length of the expression.
451 * @param enmCategory The target category for the result.
452 * @param pResult Where to store the result of the expression evaluation.
453 */
454static int dbgcEvalSubUnary(PDBGC pDbgc, char *pszExpr, size_t cchExpr, DBGCVARCAT enmCategory, PDBGCVAR pResult)
455{
456 Log2(("dbgcEvalSubUnary: cchExpr=%d pszExpr=%s\n", cchExpr, pszExpr));
457
458 /*
459 * The state of the expression is now such that it will start by zero or more
460 * unary operators and being followed by an expression of some kind.
461 * The expression is either plain or in parenthesis.
462 *
463 * Being in a lazy, recursive mode today, the parsing is done as simple as possible. :-)
464 * ASSUME: unary operators are all of equal precedence.
465 */
466 int rc = VINF_SUCCESS;
467 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, pszExpr, false, ' ');
468 if (pOp)
469 {
470 /* binary operators means syntax error. */
471 if (pOp->fBinary)
472 return VERR_DBGC_PARSE_UNEXPECTED_OPERATOR;
473
474 /*
475 * If the next expression (the one following the unary operator) is in a
476 * parenthesis a full eval is needed. If not the unary eval will suffice.
477 */
478 /* calc and strip next expr. */
479 char *pszExpr2 = pszExpr + pOp->cchName;
480 while (RT_C_IS_BLANK(*pszExpr2))
481 pszExpr2++;
482
483 if (*pszExpr2)
484 {
485 DBGCVAR Arg;
486 if (*pszExpr2 == '(')
487 rc = dbgcEvalSub(pDbgc, pszExpr2, cchExpr - (pszExpr2 - pszExpr), pOp->enmCatArg1, &Arg);
488 else
489 rc = dbgcEvalSubUnary(pDbgc, pszExpr2, cchExpr - (pszExpr2 - pszExpr), pOp->enmCatArg1, &Arg);
490 if (RT_SUCCESS(rc))
491 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOp->enmCatArg1, &Arg);
492 if (RT_SUCCESS(rc))
493 rc = pOp->pfnHandlerUnary(pDbgc, &Arg, enmCategory, pResult);
494 }
495 else
496 rc = VERR_DBGC_PARSE_EMPTY_ARGUMENT;
497 return rc;
498 }
499
500 /*
501 * Could this be a function call?
502 *
503 * ASSUMPTIONS:
504 * - A function name only contains alphanumerical chars and it can not
505 * start with a numerical character.
506 * - Immediately following the name is a parenthesis which must cover
507 * the remaining part of the expression.
508 */
509 bool fExternal = *pszExpr == '.';
510 char *pszFun = fExternal ? pszExpr + 1 : pszExpr;
511 char *pszFunEnd = NULL;
512 if (pszExpr[cchExpr - 1] == ')' && RT_C_IS_ALPHA(*pszFun))
513 {
514 pszFunEnd = pszExpr + 1;
515 while (*pszFunEnd != '(' && RT_C_IS_ALNUM(*pszFunEnd))
516 pszFunEnd++;
517 if (*pszFunEnd != '(')
518 pszFunEnd = NULL;
519 }
520 if (pszFunEnd)
521 {
522 size_t cchFunNm = pszFunEnd - pszFun;
523 return dbgcEvalSubCall(pDbgc, pszFun, cchFunNm, fExternal, pszFunEnd + 1, cchExpr - cchFunNm - fExternal - 2,
524 enmCategory, pResult);
525 }
526
527 /*
528 * Assuming plain expression.
529 * Didn't find any operators, so it must be a plain expression.
530 * Go by desired category first, then if anythings go, try guess.
531 */
532 switch (enmCategory)
533 {
534 case DBGCVAR_CAT_ANY:
535 return dbgcEvalSubUnaryAny(pDbgc, pszExpr, cchExpr, pResult);
536
537 case DBGCVAR_CAT_POINTER_NUMBER:
538 case DBGCVAR_CAT_POINTER_NUMBER_NO_RANGE:
539 case DBGCVAR_CAT_POINTER:
540 case DBGCVAR_CAT_POINTER_NO_RANGE:
541 case DBGCVAR_CAT_GC_POINTER:
542 case DBGCVAR_CAT_GC_POINTER_NO_RANGE:
543 case DBGCVAR_CAT_NUMBER:
544 case DBGCVAR_CAT_NUMBER_NO_RANGE:
545 /* Pointers will be promoted later. */
546 return dbgcEvalSubNumericOrPointer(pDbgc, pszExpr, cchExpr, enmCategory, pResult);
547
548 case DBGCVAR_CAT_STRING:
549 case DBGCVAR_CAT_SYMBOL:
550 /* Symbols will be promoted later. */
551 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
552
553 case DBGCVAR_CAT_OPTION:
554 case DBGCVAR_CAT_OPTION_STRING:
555 case DBGCVAR_CAT_OPTION_NUMBER:
556 return VERR_DBGC_PARSE_NOT_IMPLEMENTED;
557 }
558
559 AssertMsgFailed(("enmCategory=%d\n", enmCategory));
560 return VERR_NOT_IMPLEMENTED;
561}
562
563
564/**
565 * Evaluates one argument.
566 *
567 * @returns VBox status code.
568 *
569 * @param pDbgc Debugger console instance data.
570 * @param pszExpr The expression string.
571 * @param enmCategory The target category for the result.
572 * @param pResult Where to store the result of the expression evaluation.
573 */
574int dbgcEvalSub(PDBGC pDbgc, char *pszExpr, size_t cchExpr, DBGCVARCAT enmCategory, PDBGCVAR pResult)
575{
576 Log2(("dbgcEvalSub: cchExpr=%d pszExpr=%s\n", cchExpr, pszExpr));
577
578 /*
579 * First we need to remove blanks in both ends.
580 * ASSUMES: There is no quoting unless the entire expression is a string.
581 */
582
583 /* stripping. */
584 while (cchExpr > 0 && RT_C_IS_BLANK(pszExpr[cchExpr - 1]))
585 pszExpr[--cchExpr] = '\0';
586 while (RT_C_IS_BLANK(*pszExpr))
587 pszExpr++, cchExpr--;
588 if (!*pszExpr)
589 return VERR_DBGC_PARSE_EMPTY_ARGUMENT;
590
591 /*
592 * Check if there are any parenthesis which needs removing.
593 */
594 if (pszExpr[0] == '(' && pszExpr[cchExpr - 1] == ')')
595 {
596 do
597 {
598 unsigned cPar = 1;
599 char *psz = pszExpr + 1;
600 char ch;
601 while ((ch = *psz) != '\0')
602 {
603 if (ch == '(')
604 cPar++;
605 else if (ch == ')')
606 {
607 if (cPar <= 0)
608 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
609 cPar--;
610 if (cPar == 0 && psz[1]) /* If not at end, there's nothing to do. */
611 break;
612 }
613 /* next */
614 psz++;
615 }
616 if (ch)
617 break;
618
619 /* remove the parenthesis. */
620 pszExpr++;
621 cchExpr -= 2;
622 pszExpr[cchExpr] = '\0';
623
624 /* strip blanks. */
625 while (cchExpr > 0 && RT_C_IS_BLANK(pszExpr[cchExpr - 1]))
626 pszExpr[--cchExpr] = '\0';
627 while (RT_C_IS_BLANK(*pszExpr))
628 pszExpr++, cchExpr--;
629 if (!*pszExpr)
630 return VERR_DBGC_PARSE_EMPTY_ARGUMENT;
631 } while (pszExpr[0] == '(' && pszExpr[cchExpr - 1] == ')');
632 }
633
634 /*
635 * Now, we need to look for the binary operator with the lowest precedence.
636 *
637 * If there are no operators we're left with a simple expression which we
638 * evaluate with respect to unary operators
639 */
640 char *pszOpSplit = NULL;
641 PCDBGCOP pOpSplit = NULL;
642 unsigned cBinaryOps = 0;
643 unsigned cPar = 0;
644 unsigned cchWord = 0;
645 char chQuote = '\0';
646 char chPrev = ' ';
647 bool fBinary = false;
648 char *psz = pszExpr;
649 char ch;
650
651 while ((ch = *psz) != '\0')
652 {
653 /*
654 * String quoting.
655 */
656 if (chQuote)
657 {
658 if (ch == chQuote)
659 {
660 if (psz[1] == chQuote)
661 {
662 psz++; /* escaped quote */
663 cchWord++;
664 }
665 else
666 {
667 chQuote = '\0';
668 fBinary = true;
669 cchWord = 0;
670 }
671 }
672 else
673 cchWord++;
674 }
675 else if (ch == '"' || ch == '\'')
676 {
677 if (fBinary || cchWord)
678 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
679 chQuote = ch;
680 }
681 /*
682 * Parentheses.
683 */
684 else if (ch == '(')
685 {
686 if (!cPar && fBinary && !cchWord)
687 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
688 cPar++;
689 fBinary = false;
690 cchWord = 0;
691 }
692 else if (ch == ')')
693 {
694 if (cPar <= 0)
695 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
696 cPar--;
697 fBinary = true;
698 cchWord = 0;
699 }
700 /*
701 * Potential operator.
702 */
703 else if (cPar == 0 && !RT_C_IS_BLANK(ch))
704 {
705 PCDBGCOP pOp = dbgcIsOpChar(ch)
706 ? dbgcOperatorLookup(pDbgc, psz, fBinary, chPrev)
707 : NULL;
708 if (pOp)
709 {
710 /* If not the right kind of operator we've got a syntax error. */
711 if (pOp->fBinary != fBinary)
712 return VERR_DBGC_PARSE_UNEXPECTED_OPERATOR;
713
714 /*
715 * Update the parse state and skip the operator.
716 */
717 if (!pOpSplit)
718 {
719 pOpSplit = pOp;
720 pszOpSplit = psz;
721 cBinaryOps = fBinary;
722 }
723 else if (fBinary)
724 {
725 cBinaryOps++;
726 if (pOp->iPrecedence >= pOpSplit->iPrecedence)
727 {
728 pOpSplit = pOp;
729 pszOpSplit = psz;
730 }
731 }
732
733 psz += pOp->cchName - 1;
734 fBinary = false;
735 cchWord = 0;
736 }
737 else if (fBinary && !cchWord)
738 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
739 else
740 {
741 fBinary = true;
742 cchWord++;
743 }
744 }
745 else if (cPar == 0 && RT_C_IS_BLANK(ch))
746 cchWord++;
747
748 /* next */
749 psz++;
750 chPrev = ch;
751 } /* parse loop. */
752
753 if (chQuote)
754 return VERR_DBGC_PARSE_UNBALANCED_QUOTE;
755
756 /*
757 * Either we found an operator to divide the expression by or we didn't
758 * find any. In the first case it's divide and conquer. In the latter
759 * it's a single expression which needs dealing with its unary operators
760 * if any.
761 */
762 int rc;
763 if ( cBinaryOps
764 && pOpSplit->fBinary)
765 {
766 /* process 1st sub expression. */
767 *pszOpSplit = '\0';
768 DBGCVAR Arg1;
769 rc = dbgcEvalSub(pDbgc, pszExpr, pszOpSplit - pszExpr, pOpSplit->enmCatArg1, &Arg1);
770 if (RT_SUCCESS(rc))
771 {
772 /* process 2nd sub expression. */
773 char *psz2 = pszOpSplit + pOpSplit->cchName;
774 DBGCVAR Arg2;
775 rc = dbgcEvalSub(pDbgc, psz2, cchExpr - (psz2 - pszExpr), pOpSplit->enmCatArg2, &Arg2);
776 if (RT_SUCCESS(rc))
777 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOpSplit->enmCatArg1, &Arg1);
778 if (RT_SUCCESS(rc))
779 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOpSplit->enmCatArg2, &Arg2);
780 if (RT_SUCCESS(rc))
781 rc = pOpSplit->pfnHandlerBinary(pDbgc, &Arg1, &Arg2, pResult);
782 }
783 }
784 else if (cBinaryOps)
785 {
786 /* process sub expression. */
787 pszOpSplit += pOpSplit->cchName;
788 DBGCVAR Arg;
789 rc = dbgcEvalSub(pDbgc, pszOpSplit, cchExpr - (pszOpSplit - pszExpr), pOpSplit->enmCatArg1, &Arg);
790 if (RT_SUCCESS(rc))
791 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOpSplit->enmCatArg1, &Arg);
792 if (RT_SUCCESS(rc))
793 rc = pOpSplit->pfnHandlerUnary(pDbgc, &Arg, enmCategory, pResult);
794 }
795 else
796 /* plain expression, qutoed string, or using unary operators perhaps with parentheses. */
797 rc = dbgcEvalSubUnary(pDbgc, pszExpr, cchExpr, enmCategory, pResult);
798
799 return rc;
800}
801
802
803/**
804 * Worker for dbgcProcessArguments that performs type checking and promoptions.
805 *
806 * @returns VBox status code.
807 *
808 * @param pDbgc Debugger console instance data.
809 * @param enmCategory The target category for the result.
810 * @param pArg The argument to check and promote.
811 */
812static int dbgcCheckAndTypePromoteArgument(PDBGC pDbgc, DBGCVARCAT enmCategory, PDBGCVAR pArg)
813{
814 switch (enmCategory)
815 {
816 /*
817 * Anything goes
818 */
819 case DBGCVAR_CAT_ANY:
820 return VINF_SUCCESS;
821
822 /*
823 * Pointer with and without range.
824 * We can try resolve strings and symbols as symbols and promote
825 * numbers to flat GC pointers.
826 */
827 case DBGCVAR_CAT_POINTER_NO_RANGE:
828 case DBGCVAR_CAT_POINTER_NUMBER_NO_RANGE:
829 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
830 return VERR_DBGC_PARSE_NO_RANGE_ALLOWED;
831 /* fallthru */
832 case DBGCVAR_CAT_POINTER:
833 case DBGCVAR_CAT_POINTER_NUMBER:
834 switch (pArg->enmType)
835 {
836 case DBGCVAR_TYPE_GC_FLAT:
837 case DBGCVAR_TYPE_GC_FAR:
838 case DBGCVAR_TYPE_GC_PHYS:
839 case DBGCVAR_TYPE_HC_FLAT:
840 case DBGCVAR_TYPE_HC_PHYS:
841 return VINF_SUCCESS;
842
843 case DBGCVAR_TYPE_SYMBOL:
844 case DBGCVAR_TYPE_STRING:
845 {
846 DBGCVAR Var;
847 int rc = dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_GC_FLAT, &Var);
848 if (RT_SUCCESS(rc))
849 {
850 /* deal with range */
851 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
852 {
853 Var.enmRangeType = pArg->enmRangeType;
854 Var.u64Range = pArg->u64Range;
855 }
856 else if (enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
857 Var.enmRangeType = DBGCVAR_RANGE_NONE;
858 *pArg = Var;
859 }
860 return rc;
861 }
862
863 case DBGCVAR_TYPE_NUMBER:
864 if ( enmCategory != DBGCVAR_CAT_POINTER_NUMBER
865 && enmCategory != DBGCVAR_CAT_POINTER_NUMBER_NO_RANGE)
866 {
867 RTGCPTR GCPtr = (RTGCPTR)pArg->u.u64Number;
868 pArg->enmType = DBGCVAR_TYPE_GC_FLAT;
869 pArg->u.GCFlat = GCPtr;
870 }
871 return VINF_SUCCESS;
872
873 default:
874 AssertMsgFailedReturn(("Invalid type %d\n", pArg->enmType), VERR_DBGC_PARSE_INCORRECT_ARG_TYPE);
875 }
876 break; /* (not reached) */
877
878 /*
879 * GC pointer with and without range.
880 * We can try resolve strings and symbols as symbols and
881 * promote numbers to flat GC pointers.
882 */
883 case DBGCVAR_CAT_GC_POINTER_NO_RANGE:
884 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
885 return VERR_DBGC_PARSE_NO_RANGE_ALLOWED;
886 /* fallthru */
887 case DBGCVAR_CAT_GC_POINTER:
888 switch (pArg->enmType)
889 {
890 case DBGCVAR_TYPE_GC_FLAT:
891 case DBGCVAR_TYPE_GC_FAR:
892 case DBGCVAR_TYPE_GC_PHYS:
893 return VINF_SUCCESS;
894
895 case DBGCVAR_TYPE_HC_FLAT:
896 case DBGCVAR_TYPE_HC_PHYS:
897 return VERR_DBGC_PARSE_CONVERSION_FAILED;
898
899 case DBGCVAR_TYPE_SYMBOL:
900 case DBGCVAR_TYPE_STRING:
901 {
902 DBGCVAR Var;
903 int rc = dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_GC_FLAT, &Var);
904 if (RT_SUCCESS(rc))
905 {
906 /* deal with range */
907 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
908 {
909 Var.enmRangeType = pArg->enmRangeType;
910 Var.u64Range = pArg->u64Range;
911 }
912 else if (enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
913 Var.enmRangeType = DBGCVAR_RANGE_NONE;
914 *pArg = Var;
915 }
916 return rc;
917 }
918
919 case DBGCVAR_TYPE_NUMBER:
920 {
921 RTGCPTR GCPtr = (RTGCPTR)pArg->u.u64Number;
922 pArg->enmType = DBGCVAR_TYPE_GC_FLAT;
923 pArg->u.GCFlat = GCPtr;
924 return VINF_SUCCESS;
925 }
926
927 default:
928 AssertMsgFailedReturn(("Invalid type %d\n", pArg->enmType), VERR_DBGC_PARSE_INCORRECT_ARG_TYPE);
929 }
930 break; /* (not reached) */
931
932 /*
933 * Number with or without a range.
934 * Numbers can be resolved from symbols, but we cannot demote a pointer
935 * to a number.
936 */
937 case DBGCVAR_CAT_NUMBER_NO_RANGE:
938 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
939 return VERR_DBGC_PARSE_NO_RANGE_ALLOWED;
940 /* fallthru */
941 case DBGCVAR_CAT_NUMBER:
942 switch (pArg->enmType)
943 {
944 case DBGCVAR_TYPE_GC_FLAT:
945 case DBGCVAR_TYPE_GC_FAR:
946 case DBGCVAR_TYPE_GC_PHYS:
947 case DBGCVAR_TYPE_HC_FLAT:
948 case DBGCVAR_TYPE_HC_PHYS:
949 return VERR_DBGC_PARSE_INCORRECT_ARG_TYPE;
950
951 case DBGCVAR_TYPE_NUMBER:
952 return VINF_SUCCESS;
953
954 case DBGCVAR_TYPE_SYMBOL:
955 case DBGCVAR_TYPE_STRING:
956 {
957 DBGCVAR Var;
958 int rc = dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_NUMBER, &Var);
959 if (RT_SUCCESS(rc))
960 {
961 /* deal with range */
962 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
963 {
964 Var.enmRangeType = pArg->enmRangeType;
965 Var.u64Range = pArg->u64Range;
966 }
967 else if (enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
968 Var.enmRangeType = DBGCVAR_RANGE_NONE;
969 *pArg = Var;
970 }
971 return rc;
972 }
973
974 default:
975 AssertMsgFailedReturn(("Invalid type %d\n", pArg->enmType), VERR_DBGC_PARSE_INCORRECT_ARG_TYPE);
976 }
977 break; /* (not reached) */
978
979 /*
980 * Symbols and strings are basically the same thing for the time being.
981 */
982 case DBGCVAR_CAT_STRING:
983 case DBGCVAR_CAT_SYMBOL:
984 {
985 switch (pArg->enmType)
986 {
987 case DBGCVAR_TYPE_STRING:
988 if (enmCategory == DBGCVAR_CAT_SYMBOL)
989 pArg->enmType = DBGCVAR_TYPE_SYMBOL;
990 return VINF_SUCCESS;
991
992 case DBGCVAR_TYPE_SYMBOL:
993 if (enmCategory == DBGCVAR_CAT_STRING)
994 pArg->enmType = DBGCVAR_TYPE_STRING;
995 return VINF_SUCCESS;
996 default:
997 break;
998 }
999
1000 /* Stringify numeric and pointer values. */
1001 size_t cbScratch = sizeof(pDbgc->achScratch) - (pDbgc->pszScratch - &pDbgc->achScratch[0]);
1002 size_t cch = pDbgc->CmdHlp.pfnStrPrintf(&pDbgc->CmdHlp, pDbgc->pszScratch, cbScratch, "%Dv", pArg);
1003 if (cch + 1 >= cbScratch)
1004 return VERR_DBGC_PARSE_NO_SCRATCH;
1005
1006 pArg->enmType = enmCategory == DBGCVAR_CAT_STRING ? DBGCVAR_TYPE_STRING : DBGCVAR_TYPE_SYMBOL;
1007 pArg->u.pszString = pDbgc->pszScratch;
1008 pArg->enmRangeType = DBGCVAR_RANGE_BYTES;
1009 pArg->u64Range = cch;
1010
1011 pDbgc->pszScratch += cch + 1;
1012 return VINF_SUCCESS;
1013 }
1014
1015 /*
1016 * These are not yet implemented.
1017 */
1018 case DBGCVAR_CAT_OPTION:
1019 case DBGCVAR_CAT_OPTION_STRING:
1020 case DBGCVAR_CAT_OPTION_NUMBER:
1021 AssertMsgFailedReturn(("Not implemented enmCategory=%d\n", enmCategory), VERR_DBGC_PARSE_NOT_IMPLEMENTED);
1022
1023 default:
1024 AssertMsgFailedReturn(("Bad enmCategory=%d\n", enmCategory), VERR_DBGC_PARSE_NOT_IMPLEMENTED);
1025 }
1026}
1027
1028
1029/**
1030 * Parses the arguments of one command.
1031 *
1032 * @returns VBox statuc code. On parser errors the index of the troublesome
1033 * argument is indicated by *pcArg.
1034 *
1035 * @param pDbgc Debugger console instance data.
1036 * @param pszCmdOrFunc The name of the function or command. (For logging.)
1037 * @param cArgsMin See DBGCCMD::cArgsMin and DBGCFUNC::cArgsMin.
1038 * @param cArgsMax See DBGCCMD::cArgsMax and DBGCFUNC::cArgsMax.
1039 * @param paVarDescs See DBGCCMD::paVarDescs and DBGCFUNC::paVarDescs.
1040 * @param cVarDescs See DBGCCMD::cVarDescs and DBGCFUNC::cVarDescs.
1041 * @param pszArg Pointer to the arguments to parse.
1042 * @param piArg Where to return the index of the first argument in
1043 * DBGC::aArgs. Always set. Caller must restore DBGC::iArg
1044 * to this value when done, even on failure.
1045 * @param pcArgs Where to store the number of arguments. In the event
1046 * of an error this is (ab)used to store the index of the
1047 * offending argument.
1048 */
1049static int dbgcProcessArguments(PDBGC pDbgc, const char *pszCmdOrFunc,
1050 uint32_t const cArgsMin, uint32_t const cArgsMax,
1051 PCDBGCVARDESC const paVarDescs, uint32_t const cVarDescs,
1052 char *pszArgs, unsigned *piArg, unsigned *pcArgs)
1053{
1054 Log2(("dbgcProcessArguments: pszCmdOrFunc=%s pszArgs='%s'\n", pszCmdOrFunc, pszArgs));
1055
1056 /*
1057 * Check if we have any argument and if the command takes any.
1058 */
1059 *piArg = pDbgc->iArg;
1060 *pcArgs = 0;
1061 /* strip leading blanks. */
1062 while (*pszArgs && RT_C_IS_BLANK(*pszArgs))
1063 pszArgs++;
1064 if (!*pszArgs)
1065 {
1066 if (!cArgsMin)
1067 return VINF_SUCCESS;
1068 return VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS;
1069 }
1070 if (!cArgsMax)
1071 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1072
1073 /*
1074 * The parse loop.
1075 */
1076 PDBGCVAR pArg = &pDbgc->aArgs[pDbgc->iArg];
1077 PCDBGCVARDESC pPrevDesc = NULL;
1078 unsigned cCurDesc = 0;
1079 unsigned iVar = 0;
1080 unsigned iVarDesc = 0;
1081 *pcArgs = 0;
1082 do
1083 {
1084 /*
1085 * Can we have another argument?
1086 */
1087 if (*pcArgs >= cArgsMax)
1088 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1089 if (pDbgc->iArg >= RT_ELEMENTS(pDbgc->aArgs))
1090 return VERR_DBGC_PARSE_ARGUMENT_OVERFLOW;
1091 if (iVarDesc >= cVarDescs)
1092 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1093
1094 /* Walk argument descriptors. */
1095 if (cCurDesc >= paVarDescs[iVarDesc].cTimesMax)
1096 {
1097 iVarDesc++;
1098 if (iVarDesc >= cVarDescs)
1099 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1100 cCurDesc = 0;
1101 }
1102
1103 /*
1104 * Find the end of the argument. This is just rough splitting,
1105 * dbgcEvalSub will do stricter syntax checking later on.
1106 */
1107 int cPar = 0;
1108 char chQuote = '\0';
1109 char *pszEnd = NULL;
1110 char *psz = pszArgs;
1111 char ch;
1112 bool fBinary = false;
1113 for (;;)
1114 {
1115 /*
1116 * Check for the end.
1117 */
1118 if ((ch = *psz) == '\0')
1119 {
1120 if (chQuote)
1121 return VERR_DBGC_PARSE_UNBALANCED_QUOTE;
1122 if (cPar)
1123 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
1124 pszEnd = psz;
1125 break;
1126 }
1127 /*
1128 * When quoted we ignore everything but the quotation char.
1129 * We use the REXX way of escaping the quotation char, i.e. double occurrence.
1130 */
1131 else if (chQuote)
1132 {
1133 if (ch == chQuote)
1134 {
1135 if (psz[1] == chQuote)
1136 psz++; /* skip the escaped quote char */
1137 else
1138 {
1139 chQuote = '\0'; /* end of quoted string. */
1140 fBinary = true;
1141 }
1142 }
1143 }
1144 else if (ch == '\'' || ch == '"')
1145 {
1146 if (fBinary)
1147 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
1148 chQuote = ch;
1149 }
1150 /*
1151 * Parenthesis can of course be nested.
1152 */
1153 else if (ch == '(')
1154 {
1155 cPar++;
1156 fBinary = false;
1157 }
1158 else if (ch == ')')
1159 {
1160 if (!cPar)
1161 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
1162 cPar--;
1163 fBinary = true;
1164 }
1165 else if (!cPar)
1166 {
1167 /*
1168 * Encountering a comma is a definite end of parameter.
1169 */
1170 if (ch == ',')
1171 {
1172 pszEnd = psz++;
1173 break;
1174 }
1175
1176 /*
1177 * Encountering blanks may mean the end of it all. A binary
1178 * operator will force continued parsing.
1179 */
1180 if (RT_C_IS_BLANK(ch))
1181 {
1182 pszEnd = psz++; /* in case it's the end. */
1183 while (RT_C_IS_BLANK(*psz))
1184 psz++;
1185
1186 if (*psz == ',')
1187 {
1188 psz++;
1189 break;
1190 }
1191
1192 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, psz, fBinary, ' ');
1193 if (!pOp || pOp->fBinary != fBinary)
1194 break; /* the end. */
1195
1196 psz += pOp->cchName;
1197 while (RT_C_IS_BLANK(*psz)) /* skip blanks so we don't get here again */
1198 psz++;
1199 fBinary = false;
1200 continue;
1201 }
1202
1203 /*
1204 * Look for operators without a space up front.
1205 */
1206 if (dbgcIsOpChar(ch))
1207 {
1208 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, psz, fBinary, ' ');
1209 if (pOp)
1210 {
1211 if (pOp->fBinary != fBinary)
1212 {
1213 pszEnd = psz;
1214 /** @todo this is a parsing error really. */
1215 break; /* the end. */
1216 }
1217 psz += pOp->cchName;
1218 while (RT_C_IS_BLANK(*psz)) /* skip blanks so we don't get here again */
1219 psz++;
1220 fBinary = false;
1221 continue;
1222 }
1223 }
1224 fBinary = true;
1225 }
1226
1227 /* next char */
1228 psz++;
1229 }
1230 *pszEnd = '\0';
1231 /* (psz = next char to process) */
1232 size_t cchArgs = strlen(pszArgs);
1233
1234 /*
1235 * Try optional arguments until we find something which matches
1236 * or can easily be promoted to what the descriptor want.
1237 */
1238 for (;;)
1239 {
1240 char *pszArgsCopy = (char *)RTMemDup(pszArgs, cchArgs + 1);
1241 if (!pszArgsCopy)
1242 return VERR_DBGC_PARSE_NO_MEMORY;
1243
1244 int rc = dbgcEvalSub(pDbgc, pszArgs, cchArgs, paVarDescs[iVarDesc].enmCategory, pArg);
1245 if (RT_SUCCESS(rc))
1246 rc = dbgcCheckAndTypePromoteArgument(pDbgc, paVarDescs[iVarDesc].enmCategory, pArg);
1247 if (RT_SUCCESS(rc))
1248 {
1249 pArg->pDesc = pPrevDesc = &paVarDescs[iVarDesc];
1250 cCurDesc++;
1251 RTMemFree(pszArgsCopy);
1252 break;
1253 }
1254
1255 memcpy(pszArgs, pszArgsCopy, cchArgs + 1);
1256 RTMemFree(pszArgsCopy);
1257
1258 /* Continue searching optional descriptors? */
1259 if ( rc != VERR_DBGC_PARSE_INCORRECT_ARG_TYPE
1260 && rc != VERR_DBGC_PARSE_INVALID_NUMBER
1261 && rc != VERR_DBGC_PARSE_NO_RANGE_ALLOWED
1262 )
1263 return rc;
1264
1265 /* Try advance to the next descriptor. */
1266 if (paVarDescs[iVarDesc].cTimesMin > cCurDesc)
1267 return rc;
1268 iVarDesc++;
1269 if (!cCurDesc)
1270 while ( iVarDesc < cVarDescs
1271 && (paVarDescs[iVarDesc].fFlags & DBGCVD_FLAGS_DEP_PREV))
1272 iVarDesc++;
1273 if (iVarDesc >= cVarDescs)
1274 return rc;
1275 cCurDesc = 0;
1276 }
1277
1278 /*
1279 * Next argument.
1280 */
1281 iVar++;
1282 pArg++;
1283 pDbgc->iArg++;
1284 *pcArgs += 1;
1285 pszArgs = psz;
1286 while (*pszArgs && RT_C_IS_BLANK(*pszArgs))
1287 pszArgs++;
1288 } while (*pszArgs);
1289
1290 /*
1291 * Check that the rest of the argument descriptors indicate optional args.
1292 */
1293 if (iVarDesc < cVarDescs)
1294 {
1295 if (cCurDesc < paVarDescs[iVarDesc].cTimesMin)
1296 return VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS;
1297 iVarDesc++;
1298 while (iVarDesc < cVarDescs)
1299 {
1300 if (paVarDescs[iVarDesc].cTimesMin)
1301 return VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS;
1302 iVarDesc++;
1303 }
1304 }
1305
1306 return VINF_SUCCESS;
1307}
1308
1309
1310/**
1311 * Evaluate one command.
1312 *
1313 * @returns VBox status code. This is also stored in DBGC::rcCmd.
1314 *
1315 * @param pDbgc Debugger console instance data.
1316 * @param pszCmd Pointer to the command.
1317 * @param cchCmd Length of the command.
1318 * @param fNoExecute Indicates that no commands should actually be executed.
1319 */
1320int dbgcEvalCommand(PDBGC pDbgc, char *pszCmd, size_t cchCmd, bool fNoExecute)
1321{
1322 char *pszCmdInput = pszCmd;
1323
1324 /*
1325 * Skip blanks.
1326 */
1327 while (RT_C_IS_BLANK(*pszCmd))
1328 pszCmd++, cchCmd--;
1329
1330 /* external command? */
1331 bool const fExternal = *pszCmd == '.';
1332 if (fExternal)
1333 pszCmd++, cchCmd--;
1334
1335 /*
1336 * Find arguments.
1337 */
1338 char *pszArgs = pszCmd;
1339 while (RT_C_IS_ALNUM(*pszArgs) || *pszArgs == '_')
1340 pszArgs++;
1341 if ( (*pszArgs && !RT_C_IS_BLANK(*pszArgs))
1342 || !RT_C_IS_ALPHA(*pszCmd))
1343 {
1344 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Syntax error: Invalid command '%s'!\n", pszCmdInput);
1345 return pDbgc->rcCmd = VERR_DBGC_PARSE_INVALD_COMMAND_NAME;
1346 }
1347
1348 /*
1349 * Find the command.
1350 */
1351 PCDBGCCMD pCmd = dbgcCommandLookup(pDbgc, pszCmd, pszArgs - pszCmd, fExternal);
1352 if (!pCmd)
1353 {
1354 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Syntax error: Unknown command '%s'!\n", pszCmdInput);
1355 return pDbgc->rcCmd = VERR_DBGC_PARSE_COMMAND_NOT_FOUND;
1356 }
1357
1358 /*
1359 * Parse arguments (if any).
1360 */
1361 unsigned iArg;
1362 unsigned cArgs;
1363 int rc = dbgcProcessArguments(pDbgc, pCmd->pszCmd,
1364 pCmd->cArgsMin, pCmd->cArgsMax, pCmd->paArgDescs, pCmd->cArgDescs,
1365 pszArgs, &iArg, &cArgs);
1366 if (RT_SUCCESS(rc))
1367 {
1368 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
1369
1370 /*
1371 * Execute the command.
1372 */
1373 if (!fNoExecute)
1374 rc = pCmd->pfnHandler(pCmd, &pDbgc->CmdHlp, pDbgc->pUVM, &pDbgc->aArgs[iArg], cArgs);
1375 pDbgc->rcCmd = rc;
1376 pDbgc->iArg = iArg;
1377 if (rc == VERR_DBGC_COMMAND_FAILED)
1378 rc = VINF_SUCCESS;
1379 }
1380 else
1381 {
1382 pDbgc->rcCmd = rc;
1383 pDbgc->iArg = iArg;
1384
1385 /* report parse / eval error. */
1386 switch (rc)
1387 {
1388 case VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS:
1389 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1390 "Syntax error: Too few arguments. Minimum is %d for command '%s'.\n", pCmd->cArgsMin, pCmd->pszCmd);
1391 break;
1392 case VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS:
1393 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1394 "Syntax error: Too many arguments. Maximum is %d for command '%s'.\n", pCmd->cArgsMax, pCmd->pszCmd);
1395 break;
1396 case VERR_DBGC_PARSE_ARGUMENT_OVERFLOW:
1397 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1398 "Syntax error: Too many arguments.\n");
1399 break;
1400 case VERR_DBGC_PARSE_UNBALANCED_QUOTE:
1401 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1402 "Syntax error: Unbalanced quote (argument %d).\n", cArgs);
1403 break;
1404 case VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS:
1405 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1406 "Syntax error: Unbalanced parenthesis (argument %d).\n", cArgs);
1407 break;
1408 case VERR_DBGC_PARSE_EMPTY_ARGUMENT:
1409 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1410 "Syntax error: An argument or subargument contains nothing useful (argument %d).\n", cArgs);
1411 break;
1412 case VERR_DBGC_PARSE_UNEXPECTED_OPERATOR:
1413 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1414 "Syntax error: Invalid operator usage (argument %d).\n", cArgs);
1415 break;
1416 case VERR_DBGC_PARSE_INVALID_NUMBER:
1417 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1418 "Syntax error: Invalid numeric value (argument %d). If a string was the intention, then quote it.\n", cArgs);
1419 break;
1420 case VERR_DBGC_PARSE_NUMBER_TOO_BIG:
1421 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1422 "Error: Numeric overflow (argument %d).\n", cArgs);
1423 break;
1424 case VERR_DBGC_PARSE_INVALID_OPERATION:
1425 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1426 "Error: Invalid operation attempted (argument %d).\n", cArgs);
1427 break;
1428 case VERR_DBGC_PARSE_FUNCTION_NOT_FOUND:
1429 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1430 "Error: Function not found (argument %d).\n", cArgs);
1431 break;
1432 case VERR_DBGC_PARSE_NOT_A_FUNCTION:
1433 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1434 "Error: The function specified is not a function (argument %d).\n", cArgs);
1435 break;
1436 case VERR_DBGC_PARSE_NO_MEMORY:
1437 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1438 "Error: Out memory in the regular heap! Expect odd stuff to happen...\n");
1439 break;
1440 case VERR_DBGC_PARSE_INCORRECT_ARG_TYPE:
1441 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1442 "Error: Incorrect argument type (argument %d?).\n", cArgs);
1443 break;
1444 case VERR_DBGC_PARSE_VARIABLE_NOT_FOUND:
1445 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1446 "Error: An undefined variable was referenced (argument %d).\n", cArgs);
1447 break;
1448 case VERR_DBGC_PARSE_CONVERSION_FAILED:
1449 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1450 "Error: A conversion between two types failed (argument %d).\n", cArgs);
1451 break;
1452 case VERR_DBGC_PARSE_NOT_IMPLEMENTED:
1453 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1454 "Error: You hit a debugger feature which isn't implemented yet (argument %d).\n", cArgs);
1455 break;
1456 case VERR_DBGC_PARSE_BAD_RESULT_TYPE:
1457 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1458 "Error: Couldn't satisfy a request for a specific result type (argument %d). (Usually applies to symbols)\n", cArgs);
1459 break;
1460 case VERR_DBGC_PARSE_WRITEONLY_SYMBOL:
1461 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1462 "Error: Cannot get symbol, it's set only (argument %d).\n", cArgs);
1463 break;
1464
1465 case VERR_DBGC_COMMAND_FAILED:
1466 break;
1467
1468 default:
1469 {
1470 PCRTSTATUSMSG pErr = RTErrGet(rc);
1471 if (strncmp(pErr->pszDefine, RT_STR_TUPLE("Unknown Status")))
1472 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Error: %s (%d) - %s\n", pErr->pszDefine, rc, pErr->pszMsgFull);
1473 else
1474 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Error: Unknown error %d (%#x)!\n", rc, rc);
1475 break;
1476 }
1477 }
1478 }
1479
1480 return rc;
1481}
1482
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