VirtualBox

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

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

DBGC,Main: Global and per-VM debugger console init script support.

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