VirtualBox

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

Last change on this file since 68155 was 65118, checked in by vboxsync, 8 years ago

Debugger: doxygen fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.8 KB
Line 
1/* $Id: DBGCEval.cpp 65118 2017-01-04 17:09:04Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, command evaluator.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 RT_NOREF1(enmCategory);
423
424 /*
425 * Lookup the function.
426 */
427 PCDBGCFUNC pFunc = dbgcFunctionLookup(pDbgc, pszFuncNm, cchFuncNm, fExternal);
428 if (!pFunc)
429 return VERR_DBGC_PARSE_FUNCTION_NOT_FOUND;
430
431 /*
432 * Parse the arguments.
433 */
434 unsigned cArgs;
435 unsigned iArg;
436 pszArgs[cchArgs] = '\0';
437 int rc = dbgcProcessArguments(pDbgc, pFunc->pszFuncNm,
438 pFunc->cArgsMin, pFunc->cArgsMax, pFunc->paArgDescs, pFunc->cArgDescs,
439 pszArgs, &iArg, &cArgs);
440 if (RT_SUCCESS(rc))
441 rc = pFunc->pfnHandler(pFunc, &pDbgc->CmdHlp, pDbgc->pUVM, &pDbgc->aArgs[iArg], cArgs, pResult);
442 pDbgc->iArg = iArg;
443 return rc;
444}
445
446
447/**
448 * Evaluates one argument with respect to unary operators.
449 *
450 * @returns VBox status code. pResult contains the result on success.
451 *
452 * @param pDbgc Debugger console instance data.
453 * @param pszExpr The expression string.
454 * @param cchExpr The length of the expression.
455 * @param enmCategory The target category for the result.
456 * @param pResult Where to store the result of the expression evaluation.
457 */
458static int dbgcEvalSubUnary(PDBGC pDbgc, char *pszExpr, size_t cchExpr, DBGCVARCAT enmCategory, PDBGCVAR pResult)
459{
460 Log2(("dbgcEvalSubUnary: cchExpr=%d pszExpr=%s\n", cchExpr, pszExpr));
461
462 /*
463 * The state of the expression is now such that it will start by zero or more
464 * unary operators and being followed by an expression of some kind.
465 * The expression is either plain or in parenthesis.
466 *
467 * Being in a lazy, recursive mode today, the parsing is done as simple as possible. :-)
468 * ASSUME: unary operators are all of equal precedence.
469 */
470 int rc = VINF_SUCCESS;
471 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, pszExpr, false, ' ');
472 if (pOp)
473 {
474 /* binary operators means syntax error. */
475 if (pOp->fBinary)
476 return VERR_DBGC_PARSE_UNEXPECTED_OPERATOR;
477
478 /*
479 * If the next expression (the one following the unary operator) is in a
480 * parenthesis a full eval is needed. If not the unary eval will suffice.
481 */
482 /* calc and strip next expr. */
483 char *pszExpr2 = pszExpr + pOp->cchName;
484 while (RT_C_IS_BLANK(*pszExpr2))
485 pszExpr2++;
486
487 if (*pszExpr2)
488 {
489 DBGCVAR Arg;
490 if (*pszExpr2 == '(')
491 rc = dbgcEvalSub(pDbgc, pszExpr2, cchExpr - (pszExpr2 - pszExpr), pOp->enmCatArg1, &Arg);
492 else
493 rc = dbgcEvalSubUnary(pDbgc, pszExpr2, cchExpr - (pszExpr2 - pszExpr), pOp->enmCatArg1, &Arg);
494 if (RT_SUCCESS(rc))
495 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOp->enmCatArg1, &Arg);
496 if (RT_SUCCESS(rc))
497 rc = pOp->pfnHandlerUnary(pDbgc, &Arg, enmCategory, pResult);
498 }
499 else
500 rc = VERR_DBGC_PARSE_EMPTY_ARGUMENT;
501 return rc;
502 }
503
504 /*
505 * Could this be a function call?
506 *
507 * ASSUMPTIONS:
508 * - A function name only contains alphanumerical chars and it can not
509 * start with a numerical character.
510 * - Immediately following the name is a parenthesis which must cover
511 * the remaining part of the expression.
512 */
513 bool fExternal = *pszExpr == '.';
514 char *pszFun = fExternal ? pszExpr + 1 : pszExpr;
515 char *pszFunEnd = NULL;
516 if (pszExpr[cchExpr - 1] == ')' && RT_C_IS_ALPHA(*pszFun))
517 {
518 pszFunEnd = pszExpr + 1;
519 while (*pszFunEnd != '(' && RT_C_IS_ALNUM(*pszFunEnd))
520 pszFunEnd++;
521 if (*pszFunEnd != '(')
522 pszFunEnd = NULL;
523 }
524 if (pszFunEnd)
525 {
526 size_t cchFunNm = pszFunEnd - pszFun;
527 return dbgcEvalSubCall(pDbgc, pszFun, cchFunNm, fExternal, pszFunEnd + 1, cchExpr - cchFunNm - fExternal - 2,
528 enmCategory, pResult);
529 }
530
531 /*
532 * Assuming plain expression.
533 * Didn't find any operators, so it must be a plain expression.
534 * Go by desired category first, then if anythings go, try guess.
535 */
536 switch (enmCategory)
537 {
538 case DBGCVAR_CAT_ANY:
539 return dbgcEvalSubUnaryAny(pDbgc, pszExpr, cchExpr, pResult);
540
541 case DBGCVAR_CAT_POINTER_NUMBER:
542 case DBGCVAR_CAT_POINTER_NUMBER_NO_RANGE:
543 case DBGCVAR_CAT_POINTER:
544 case DBGCVAR_CAT_POINTER_NO_RANGE:
545 case DBGCVAR_CAT_GC_POINTER:
546 case DBGCVAR_CAT_GC_POINTER_NO_RANGE:
547 case DBGCVAR_CAT_NUMBER:
548 case DBGCVAR_CAT_NUMBER_NO_RANGE:
549 /* Pointers will be promoted later. */
550 return dbgcEvalSubNumericOrPointer(pDbgc, pszExpr, cchExpr, enmCategory, pResult);
551
552 case DBGCVAR_CAT_STRING:
553 case DBGCVAR_CAT_SYMBOL:
554 /* Symbols will be promoted later. */
555 return dbgcEvalSubString(pDbgc, pszExpr, cchExpr, pResult);
556
557 case DBGCVAR_CAT_OPTION:
558 case DBGCVAR_CAT_OPTION_STRING:
559 case DBGCVAR_CAT_OPTION_NUMBER:
560 return VERR_DBGC_PARSE_NOT_IMPLEMENTED;
561 }
562
563 AssertMsgFailed(("enmCategory=%d\n", enmCategory));
564 return VERR_NOT_IMPLEMENTED;
565}
566
567
568/**
569 * Evaluates one argument.
570 *
571 * @returns VBox status code.
572 *
573 * @param pDbgc Debugger console instance data.
574 * @param pszExpr The expression string.
575 * @param cchExpr The size of the expression string.
576 * @param enmCategory The target category for the result.
577 * @param pResult Where to store the result of the expression evaluation.
578 */
579int dbgcEvalSub(PDBGC pDbgc, char *pszExpr, size_t cchExpr, DBGCVARCAT enmCategory, PDBGCVAR pResult)
580{
581 Log2(("dbgcEvalSub: cchExpr=%d pszExpr=%s\n", cchExpr, pszExpr));
582
583 /*
584 * First we need to remove blanks in both ends.
585 * ASSUMES: There is no quoting unless the entire expression is a string.
586 */
587
588 /* stripping. */
589 while (cchExpr > 0 && RT_C_IS_BLANK(pszExpr[cchExpr - 1]))
590 pszExpr[--cchExpr] = '\0';
591 while (RT_C_IS_BLANK(*pszExpr))
592 pszExpr++, cchExpr--;
593 if (!*pszExpr)
594 return VERR_DBGC_PARSE_EMPTY_ARGUMENT;
595
596 /*
597 * Check if there are any parenthesis which needs removing.
598 */
599 if (pszExpr[0] == '(' && pszExpr[cchExpr - 1] == ')')
600 {
601 do
602 {
603 unsigned cPar = 1;
604 char *psz = pszExpr + 1;
605 char ch;
606 while ((ch = *psz) != '\0')
607 {
608 if (ch == '(')
609 cPar++;
610 else if (ch == ')')
611 {
612 if (cPar <= 0)
613 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
614 cPar--;
615 if (cPar == 0 && psz[1]) /* If not at end, there's nothing to do. */
616 break;
617 }
618 /* next */
619 psz++;
620 }
621 if (ch)
622 break;
623
624 /* remove the parenthesis. */
625 pszExpr++;
626 cchExpr -= 2;
627 pszExpr[cchExpr] = '\0';
628
629 /* strip blanks. */
630 while (cchExpr > 0 && RT_C_IS_BLANK(pszExpr[cchExpr - 1]))
631 pszExpr[--cchExpr] = '\0';
632 while (RT_C_IS_BLANK(*pszExpr))
633 pszExpr++, cchExpr--;
634 if (!*pszExpr)
635 return VERR_DBGC_PARSE_EMPTY_ARGUMENT;
636 } while (pszExpr[0] == '(' && pszExpr[cchExpr - 1] == ')');
637 }
638
639 /*
640 * Now, we need to look for the binary operator with the lowest precedence.
641 *
642 * If there are no operators we're left with a simple expression which we
643 * evaluate with respect to unary operators
644 */
645 char *pszOpSplit = NULL;
646 PCDBGCOP pOpSplit = NULL;
647 unsigned cBinaryOps = 0;
648 unsigned cPar = 0;
649 unsigned cchWord = 0;
650 char chQuote = '\0';
651 char chPrev = ' ';
652 bool fBinary = false;
653 char *psz = pszExpr;
654 char ch;
655
656 while ((ch = *psz) != '\0')
657 {
658 /*
659 * String quoting.
660 */
661 if (chQuote)
662 {
663 if (ch == chQuote)
664 {
665 if (psz[1] == chQuote)
666 {
667 psz++; /* escaped quote */
668 cchWord++;
669 }
670 else
671 {
672 chQuote = '\0';
673 fBinary = true;
674 cchWord = 0;
675 }
676 }
677 else
678 cchWord++;
679 }
680 else if (ch == '"' || ch == '\'')
681 {
682 if (fBinary || cchWord)
683 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
684 chQuote = ch;
685 }
686 /*
687 * Parentheses.
688 */
689 else if (ch == '(')
690 {
691 if (!cPar && fBinary && !cchWord)
692 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
693 cPar++;
694 fBinary = false;
695 cchWord = 0;
696 }
697 else if (ch == ')')
698 {
699 if (cPar <= 0)
700 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
701 cPar--;
702 fBinary = true;
703 cchWord = 0;
704 }
705 /*
706 * Potential operator.
707 */
708 else if (cPar == 0 && !RT_C_IS_BLANK(ch))
709 {
710 PCDBGCOP pOp = dbgcIsOpChar(ch)
711 ? dbgcOperatorLookup(pDbgc, psz, fBinary, chPrev)
712 : NULL;
713 if (pOp)
714 {
715 /* If not the right kind of operator we've got a syntax error. */
716 if (pOp->fBinary != fBinary)
717 return VERR_DBGC_PARSE_UNEXPECTED_OPERATOR;
718
719 /*
720 * Update the parse state and skip the operator.
721 */
722 if (!pOpSplit)
723 {
724 pOpSplit = pOp;
725 pszOpSplit = psz;
726 cBinaryOps = fBinary;
727 }
728 else if (fBinary)
729 {
730 cBinaryOps++;
731 if (pOp->iPrecedence >= pOpSplit->iPrecedence)
732 {
733 pOpSplit = pOp;
734 pszOpSplit = psz;
735 }
736 }
737
738 psz += pOp->cchName - 1;
739 fBinary = false;
740 cchWord = 0;
741 }
742 else if (fBinary && !cchWord)
743 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
744 else
745 {
746 fBinary = true;
747 cchWord++;
748 }
749 }
750 else if (cPar == 0 && RT_C_IS_BLANK(ch))
751 cchWord++;
752
753 /* next */
754 psz++;
755 chPrev = ch;
756 } /* parse loop. */
757
758 if (chQuote)
759 return VERR_DBGC_PARSE_UNBALANCED_QUOTE;
760
761 /*
762 * Either we found an operator to divide the expression by or we didn't
763 * find any. In the first case it's divide and conquer. In the latter
764 * it's a single expression which needs dealing with its unary operators
765 * if any.
766 */
767 int rc;
768 if ( cBinaryOps
769 && pOpSplit->fBinary)
770 {
771 /* process 1st sub expression. */
772 *pszOpSplit = '\0';
773 DBGCVAR Arg1;
774 rc = dbgcEvalSub(pDbgc, pszExpr, pszOpSplit - pszExpr, pOpSplit->enmCatArg1, &Arg1);
775 if (RT_SUCCESS(rc))
776 {
777 /* process 2nd sub expression. */
778 char *psz2 = pszOpSplit + pOpSplit->cchName;
779 DBGCVAR Arg2;
780 rc = dbgcEvalSub(pDbgc, psz2, cchExpr - (psz2 - pszExpr), pOpSplit->enmCatArg2, &Arg2);
781 if (RT_SUCCESS(rc))
782 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOpSplit->enmCatArg1, &Arg1);
783 if (RT_SUCCESS(rc))
784 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOpSplit->enmCatArg2, &Arg2);
785 if (RT_SUCCESS(rc))
786 rc = pOpSplit->pfnHandlerBinary(pDbgc, &Arg1, &Arg2, pResult);
787 }
788 }
789 else if (cBinaryOps)
790 {
791 /* process sub expression. */
792 pszOpSplit += pOpSplit->cchName;
793 DBGCVAR Arg;
794 rc = dbgcEvalSub(pDbgc, pszOpSplit, cchExpr - (pszOpSplit - pszExpr), pOpSplit->enmCatArg1, &Arg);
795 if (RT_SUCCESS(rc))
796 rc = dbgcCheckAndTypePromoteArgument(pDbgc, pOpSplit->enmCatArg1, &Arg);
797 if (RT_SUCCESS(rc))
798 rc = pOpSplit->pfnHandlerUnary(pDbgc, &Arg, enmCategory, pResult);
799 }
800 else
801 /* plain expression, qutoed string, or using unary operators perhaps with parentheses. */
802 rc = dbgcEvalSubUnary(pDbgc, pszExpr, cchExpr, enmCategory, pResult);
803
804 return rc;
805}
806
807
808/**
809 * Worker for dbgcProcessArguments that performs type checking and promoptions.
810 *
811 * @returns VBox status code.
812 *
813 * @param pDbgc Debugger console instance data.
814 * @param enmCategory The target category for the result.
815 * @param pArg The argument to check and promote.
816 */
817static int dbgcCheckAndTypePromoteArgument(PDBGC pDbgc, DBGCVARCAT enmCategory, PDBGCVAR pArg)
818{
819 switch (enmCategory)
820 {
821 /*
822 * Anything goes
823 */
824 case DBGCVAR_CAT_ANY:
825 return VINF_SUCCESS;
826
827 /*
828 * Pointer with and without range.
829 * We can try resolve strings and symbols as symbols and promote
830 * numbers to flat GC pointers.
831 */
832 case DBGCVAR_CAT_POINTER_NO_RANGE:
833 case DBGCVAR_CAT_POINTER_NUMBER_NO_RANGE:
834 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
835 return VERR_DBGC_PARSE_NO_RANGE_ALLOWED;
836 /* fallthru */
837 case DBGCVAR_CAT_POINTER:
838 case DBGCVAR_CAT_POINTER_NUMBER:
839 switch (pArg->enmType)
840 {
841 case DBGCVAR_TYPE_GC_FLAT:
842 case DBGCVAR_TYPE_GC_FAR:
843 case DBGCVAR_TYPE_GC_PHYS:
844 case DBGCVAR_TYPE_HC_FLAT:
845 case DBGCVAR_TYPE_HC_PHYS:
846 return VINF_SUCCESS;
847
848 case DBGCVAR_TYPE_SYMBOL:
849 case DBGCVAR_TYPE_STRING:
850 {
851 DBGCVAR Var;
852 int rc = dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_GC_FLAT, &Var);
853 if (RT_SUCCESS(rc))
854 {
855 /* deal with range */
856 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
857 {
858 Var.enmRangeType = pArg->enmRangeType;
859 Var.u64Range = pArg->u64Range;
860 }
861 else if (enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
862 Var.enmRangeType = DBGCVAR_RANGE_NONE;
863 *pArg = Var;
864 }
865 return rc;
866 }
867
868 case DBGCVAR_TYPE_NUMBER:
869 if ( enmCategory != DBGCVAR_CAT_POINTER_NUMBER
870 && enmCategory != DBGCVAR_CAT_POINTER_NUMBER_NO_RANGE)
871 {
872 RTGCPTR GCPtr = (RTGCPTR)pArg->u.u64Number;
873 pArg->enmType = DBGCVAR_TYPE_GC_FLAT;
874 pArg->u.GCFlat = GCPtr;
875 }
876 return VINF_SUCCESS;
877
878 default:
879 AssertMsgFailedReturn(("Invalid type %d\n", pArg->enmType), VERR_DBGC_PARSE_INCORRECT_ARG_TYPE);
880 }
881 break; /* (not reached) */
882
883 /*
884 * GC pointer with and without range.
885 * We can try resolve strings and symbols as symbols and
886 * promote numbers to flat GC pointers.
887 */
888 case DBGCVAR_CAT_GC_POINTER_NO_RANGE:
889 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
890 return VERR_DBGC_PARSE_NO_RANGE_ALLOWED;
891 /* fallthru */
892 case DBGCVAR_CAT_GC_POINTER:
893 switch (pArg->enmType)
894 {
895 case DBGCVAR_TYPE_GC_FLAT:
896 case DBGCVAR_TYPE_GC_FAR:
897 case DBGCVAR_TYPE_GC_PHYS:
898 return VINF_SUCCESS;
899
900 case DBGCVAR_TYPE_HC_FLAT:
901 case DBGCVAR_TYPE_HC_PHYS:
902 return VERR_DBGC_PARSE_CONVERSION_FAILED;
903
904 case DBGCVAR_TYPE_SYMBOL:
905 case DBGCVAR_TYPE_STRING:
906 {
907 DBGCVAR Var;
908 int rc = dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_GC_FLAT, &Var);
909 if (RT_SUCCESS(rc))
910 {
911 /* deal with range */
912 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
913 {
914 Var.enmRangeType = pArg->enmRangeType;
915 Var.u64Range = pArg->u64Range;
916 }
917 else if (enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
918 Var.enmRangeType = DBGCVAR_RANGE_NONE;
919 *pArg = Var;
920 }
921 return rc;
922 }
923
924 case DBGCVAR_TYPE_NUMBER:
925 {
926 RTGCPTR GCPtr = (RTGCPTR)pArg->u.u64Number;
927 pArg->enmType = DBGCVAR_TYPE_GC_FLAT;
928 pArg->u.GCFlat = GCPtr;
929 return VINF_SUCCESS;
930 }
931
932 default:
933 AssertMsgFailedReturn(("Invalid type %d\n", pArg->enmType), VERR_DBGC_PARSE_INCORRECT_ARG_TYPE);
934 }
935 break; /* (not reached) */
936
937 /*
938 * Number with or without a range.
939 * Numbers can be resolved from symbols, but we cannot demote a pointer
940 * to a number.
941 */
942 case DBGCVAR_CAT_NUMBER_NO_RANGE:
943 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
944 return VERR_DBGC_PARSE_NO_RANGE_ALLOWED;
945 /* fallthru */
946 case DBGCVAR_CAT_NUMBER:
947 switch (pArg->enmType)
948 {
949 case DBGCVAR_TYPE_GC_FLAT:
950 case DBGCVAR_TYPE_GC_FAR:
951 case DBGCVAR_TYPE_GC_PHYS:
952 case DBGCVAR_TYPE_HC_FLAT:
953 case DBGCVAR_TYPE_HC_PHYS:
954 return VERR_DBGC_PARSE_INCORRECT_ARG_TYPE;
955
956 case DBGCVAR_TYPE_NUMBER:
957 return VINF_SUCCESS;
958
959 case DBGCVAR_TYPE_SYMBOL:
960 case DBGCVAR_TYPE_STRING:
961 {
962 DBGCVAR Var;
963 int rc = dbgcSymbolGet(pDbgc, pArg->u.pszString, DBGCVAR_TYPE_NUMBER, &Var);
964 if (RT_SUCCESS(rc))
965 {
966 /* deal with range */
967 if (pArg->enmRangeType != DBGCVAR_RANGE_NONE)
968 {
969 Var.enmRangeType = pArg->enmRangeType;
970 Var.u64Range = pArg->u64Range;
971 }
972 else if (enmCategory == DBGCVAR_CAT_POINTER_NO_RANGE)
973 Var.enmRangeType = DBGCVAR_RANGE_NONE;
974 *pArg = Var;
975 }
976 return rc;
977 }
978
979 default:
980 AssertMsgFailedReturn(("Invalid type %d\n", pArg->enmType), VERR_DBGC_PARSE_INCORRECT_ARG_TYPE);
981 }
982 break; /* (not reached) */
983
984 /*
985 * Symbols and strings are basically the same thing for the time being.
986 */
987 case DBGCVAR_CAT_STRING:
988 case DBGCVAR_CAT_SYMBOL:
989 {
990 switch (pArg->enmType)
991 {
992 case DBGCVAR_TYPE_STRING:
993 if (enmCategory == DBGCVAR_CAT_SYMBOL)
994 pArg->enmType = DBGCVAR_TYPE_SYMBOL;
995 return VINF_SUCCESS;
996
997 case DBGCVAR_TYPE_SYMBOL:
998 if (enmCategory == DBGCVAR_CAT_STRING)
999 pArg->enmType = DBGCVAR_TYPE_STRING;
1000 return VINF_SUCCESS;
1001 default:
1002 break;
1003 }
1004
1005 /* Stringify numeric and pointer values. */
1006 size_t cbScratch = sizeof(pDbgc->achScratch) - (pDbgc->pszScratch - &pDbgc->achScratch[0]);
1007 size_t cch = pDbgc->CmdHlp.pfnStrPrintf(&pDbgc->CmdHlp, pDbgc->pszScratch, cbScratch, "%Dv", pArg);
1008 if (cch + 1 >= cbScratch)
1009 return VERR_DBGC_PARSE_NO_SCRATCH;
1010
1011 pArg->enmType = enmCategory == DBGCVAR_CAT_STRING ? DBGCVAR_TYPE_STRING : DBGCVAR_TYPE_SYMBOL;
1012 pArg->u.pszString = pDbgc->pszScratch;
1013 pArg->enmRangeType = DBGCVAR_RANGE_BYTES;
1014 pArg->u64Range = cch;
1015
1016 pDbgc->pszScratch += cch + 1;
1017 return VINF_SUCCESS;
1018 }
1019
1020 /*
1021 * These are not yet implemented.
1022 */
1023 case DBGCVAR_CAT_OPTION:
1024 case DBGCVAR_CAT_OPTION_STRING:
1025 case DBGCVAR_CAT_OPTION_NUMBER:
1026 AssertMsgFailedReturn(("Not implemented enmCategory=%d\n", enmCategory), VERR_DBGC_PARSE_NOT_IMPLEMENTED);
1027
1028 default:
1029 AssertMsgFailedReturn(("Bad enmCategory=%d\n", enmCategory), VERR_DBGC_PARSE_NOT_IMPLEMENTED);
1030 }
1031}
1032
1033
1034/**
1035 * Parses the arguments of one command.
1036 *
1037 * @returns VBox statuc code. On parser errors the index of the troublesome
1038 * argument is indicated by *pcArg.
1039 *
1040 * @param pDbgc Debugger console instance data.
1041 * @param pszCmdOrFunc The name of the function or command. (For logging.)
1042 * @param cArgsMin See DBGCCMD::cArgsMin and DBGCFUNC::cArgsMin.
1043 * @param cArgsMax See DBGCCMD::cArgsMax and DBGCFUNC::cArgsMax.
1044 * @param paVarDescs See DBGCCMD::paVarDescs and DBGCFUNC::paVarDescs.
1045 * @param cVarDescs See DBGCCMD::cVarDescs and DBGCFUNC::cVarDescs.
1046 * @param pszArgs Pointer to the arguments to parse.
1047 * @param piArg Where to return the index of the first argument in
1048 * DBGC::aArgs. Always set. Caller must restore DBGC::iArg
1049 * to this value when done, even on failure.
1050 * @param pcArgs Where to store the number of arguments. In the event
1051 * of an error this is (ab)used to store the index of the
1052 * offending argument.
1053 */
1054static int dbgcProcessArguments(PDBGC pDbgc, const char *pszCmdOrFunc,
1055 uint32_t const cArgsMin, uint32_t const cArgsMax,
1056 PCDBGCVARDESC const paVarDescs, uint32_t const cVarDescs,
1057 char *pszArgs, unsigned *piArg, unsigned *pcArgs)
1058{
1059 RT_NOREF1(pszCmdOrFunc);
1060 Log2(("dbgcProcessArguments: pszCmdOrFunc=%s pszArgs='%s'\n", pszCmdOrFunc, pszArgs));
1061
1062 /*
1063 * Check if we have any argument and if the command takes any.
1064 */
1065 *piArg = pDbgc->iArg;
1066 *pcArgs = 0;
1067 /* strip leading blanks. */
1068 while (*pszArgs && RT_C_IS_BLANK(*pszArgs))
1069 pszArgs++;
1070 if (!*pszArgs)
1071 {
1072 if (!cArgsMin)
1073 return VINF_SUCCESS;
1074 return VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS;
1075 }
1076 if (!cArgsMax)
1077 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1078
1079 /*
1080 * The parse loop.
1081 */
1082 PDBGCVAR pArg = &pDbgc->aArgs[pDbgc->iArg];
1083 PCDBGCVARDESC pPrevDesc = NULL;
1084 unsigned cCurDesc = 0;
1085 unsigned iVar = 0;
1086 unsigned iVarDesc = 0;
1087 *pcArgs = 0;
1088 do
1089 {
1090 /*
1091 * Can we have another argument?
1092 */
1093 if (*pcArgs >= cArgsMax)
1094 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1095 if (pDbgc->iArg >= RT_ELEMENTS(pDbgc->aArgs))
1096 return VERR_DBGC_PARSE_ARGUMENT_OVERFLOW;
1097 if (iVarDesc >= cVarDescs)
1098 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1099
1100 /* Walk argument descriptors. */
1101 if (cCurDesc >= paVarDescs[iVarDesc].cTimesMax)
1102 {
1103 iVarDesc++;
1104 if (iVarDesc >= cVarDescs)
1105 return VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS;
1106 cCurDesc = 0;
1107 }
1108
1109 /*
1110 * Find the end of the argument. This is just rough splitting,
1111 * dbgcEvalSub will do stricter syntax checking later on.
1112 */
1113 int cPar = 0;
1114 char chQuote = '\0';
1115 char *pszEnd = NULL;
1116 char *psz = pszArgs;
1117 char ch;
1118 bool fBinary = false;
1119 for (;;)
1120 {
1121 /*
1122 * Check for the end.
1123 */
1124 if ((ch = *psz) == '\0')
1125 {
1126 if (chQuote)
1127 return VERR_DBGC_PARSE_UNBALANCED_QUOTE;
1128 if (cPar)
1129 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
1130 pszEnd = psz;
1131 break;
1132 }
1133 /*
1134 * When quoted we ignore everything but the quotation char.
1135 * We use the REXX way of escaping the quotation char, i.e. double occurrence.
1136 */
1137 else if (chQuote)
1138 {
1139 if (ch == chQuote)
1140 {
1141 if (psz[1] == chQuote)
1142 psz++; /* skip the escaped quote char */
1143 else
1144 {
1145 chQuote = '\0'; /* end of quoted string. */
1146 fBinary = true;
1147 }
1148 }
1149 }
1150 else if (ch == '\'' || ch == '"')
1151 {
1152 if (fBinary)
1153 return VERR_DBGC_PARSE_EXPECTED_BINARY_OP;
1154 chQuote = ch;
1155 }
1156 /*
1157 * Parenthesis can of course be nested.
1158 */
1159 else if (ch == '(')
1160 {
1161 cPar++;
1162 fBinary = false;
1163 }
1164 else if (ch == ')')
1165 {
1166 if (!cPar)
1167 return VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS;
1168 cPar--;
1169 fBinary = true;
1170 }
1171 else if (!cPar)
1172 {
1173 /*
1174 * Encountering a comma is a definite end of parameter.
1175 */
1176 if (ch == ',')
1177 {
1178 pszEnd = psz++;
1179 break;
1180 }
1181
1182 /*
1183 * Encountering blanks may mean the end of it all. A binary
1184 * operator will force continued parsing.
1185 */
1186 if (RT_C_IS_BLANK(ch))
1187 {
1188 pszEnd = psz++; /* in case it's the end. */
1189 while (RT_C_IS_BLANK(*psz))
1190 psz++;
1191
1192 if (*psz == ',')
1193 {
1194 psz++;
1195 break;
1196 }
1197
1198 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, psz, fBinary, ' ');
1199 if (!pOp || pOp->fBinary != fBinary)
1200 break; /* the end. */
1201
1202 psz += pOp->cchName;
1203 while (RT_C_IS_BLANK(*psz)) /* skip blanks so we don't get here again */
1204 psz++;
1205 fBinary = false;
1206 continue;
1207 }
1208
1209 /*
1210 * Look for operators without a space up front.
1211 */
1212 if (dbgcIsOpChar(ch))
1213 {
1214 PCDBGCOP pOp = dbgcOperatorLookup(pDbgc, psz, fBinary, ' ');
1215 if (pOp)
1216 {
1217 if (pOp->fBinary != fBinary)
1218 {
1219 pszEnd = psz;
1220 /** @todo this is a parsing error really. */
1221 break; /* the end. */
1222 }
1223 psz += pOp->cchName;
1224 while (RT_C_IS_BLANK(*psz)) /* skip blanks so we don't get here again */
1225 psz++;
1226 fBinary = false;
1227 continue;
1228 }
1229 }
1230 fBinary = true;
1231 }
1232
1233 /* next char */
1234 psz++;
1235 }
1236 *pszEnd = '\0';
1237 /* (psz = next char to process) */
1238 size_t cchArgs = strlen(pszArgs);
1239
1240 /*
1241 * Try optional arguments until we find something which matches
1242 * or can easily be promoted to what the descriptor want.
1243 */
1244 for (;;)
1245 {
1246 char *pszArgsCopy = (char *)RTMemDup(pszArgs, cchArgs + 1);
1247 if (!pszArgsCopy)
1248 return VERR_DBGC_PARSE_NO_MEMORY;
1249
1250 int rc = dbgcEvalSub(pDbgc, pszArgs, cchArgs, paVarDescs[iVarDesc].enmCategory, pArg);
1251 if (RT_SUCCESS(rc))
1252 rc = dbgcCheckAndTypePromoteArgument(pDbgc, paVarDescs[iVarDesc].enmCategory, pArg);
1253 if (RT_SUCCESS(rc))
1254 {
1255 pArg->pDesc = pPrevDesc = &paVarDescs[iVarDesc];
1256 cCurDesc++;
1257 RTMemFree(pszArgsCopy);
1258 break;
1259 }
1260
1261 memcpy(pszArgs, pszArgsCopy, cchArgs + 1);
1262 RTMemFree(pszArgsCopy);
1263
1264 /* Continue searching optional descriptors? */
1265 if ( rc != VERR_DBGC_PARSE_INCORRECT_ARG_TYPE
1266 && rc != VERR_DBGC_PARSE_INVALID_NUMBER
1267 && rc != VERR_DBGC_PARSE_NO_RANGE_ALLOWED
1268 )
1269 return rc;
1270
1271 /* Try advance to the next descriptor. */
1272 if (paVarDescs[iVarDesc].cTimesMin > cCurDesc)
1273 return rc;
1274 iVarDesc++;
1275 if (!cCurDesc)
1276 while ( iVarDesc < cVarDescs
1277 && (paVarDescs[iVarDesc].fFlags & DBGCVD_FLAGS_DEP_PREV))
1278 iVarDesc++;
1279 if (iVarDesc >= cVarDescs)
1280 return rc;
1281 cCurDesc = 0;
1282 }
1283
1284 /*
1285 * Next argument.
1286 */
1287 iVar++;
1288 pArg++;
1289 pDbgc->iArg++;
1290 *pcArgs += 1;
1291 pszArgs = psz;
1292 while (*pszArgs && RT_C_IS_BLANK(*pszArgs))
1293 pszArgs++;
1294 } while (*pszArgs);
1295
1296 /*
1297 * Check that the rest of the argument descriptors indicate optional args.
1298 */
1299 if (iVarDesc < cVarDescs)
1300 {
1301 if (cCurDesc < paVarDescs[iVarDesc].cTimesMin)
1302 return VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS;
1303 iVarDesc++;
1304 while (iVarDesc < cVarDescs)
1305 {
1306 if (paVarDescs[iVarDesc].cTimesMin)
1307 return VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS;
1308 iVarDesc++;
1309 }
1310 }
1311
1312 return VINF_SUCCESS;
1313}
1314
1315
1316/**
1317 * Evaluate one command.
1318 *
1319 * @returns VBox status code. This is also stored in DBGC::rcCmd.
1320 *
1321 * @param pDbgc Debugger console instance data.
1322 * @param pszCmd Pointer to the command.
1323 * @param cchCmd Length of the command.
1324 * @param fNoExecute Indicates that no commands should actually be executed.
1325 */
1326int dbgcEvalCommand(PDBGC pDbgc, char *pszCmd, size_t cchCmd, bool fNoExecute)
1327{
1328 char *pszCmdInput = pszCmd;
1329
1330 /*
1331 * Skip blanks.
1332 */
1333 while (RT_C_IS_BLANK(*pszCmd))
1334 pszCmd++, cchCmd--;
1335
1336 /* external command? */
1337 bool const fExternal = *pszCmd == '.';
1338 if (fExternal)
1339 pszCmd++, cchCmd--;
1340
1341 /*
1342 * Find arguments.
1343 */
1344 char *pszArgs = pszCmd;
1345 while (RT_C_IS_ALNUM(*pszArgs) || *pszArgs == '_')
1346 pszArgs++;
1347 if ( (*pszArgs && !RT_C_IS_BLANK(*pszArgs))
1348 || !RT_C_IS_ALPHA(*pszCmd))
1349 {
1350 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Syntax error: Invalid command '%s'!\n", pszCmdInput);
1351 return pDbgc->rcCmd = VERR_DBGC_PARSE_INVALD_COMMAND_NAME;
1352 }
1353
1354 /*
1355 * Find the command.
1356 */
1357 PCDBGCCMD pCmd = dbgcCommandLookup(pDbgc, pszCmd, pszArgs - pszCmd, fExternal);
1358 if (!pCmd)
1359 {
1360 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Syntax error: Unknown command '%s'!\n", pszCmdInput);
1361 return pDbgc->rcCmd = VERR_DBGC_PARSE_COMMAND_NOT_FOUND;
1362 }
1363
1364 /*
1365 * Parse arguments (if any).
1366 */
1367 unsigned iArg;
1368 unsigned cArgs;
1369 int rc = dbgcProcessArguments(pDbgc, pCmd->pszCmd,
1370 pCmd->cArgsMin, pCmd->cArgsMax, pCmd->paArgDescs, pCmd->cArgDescs,
1371 pszArgs, &iArg, &cArgs);
1372 if (RT_SUCCESS(rc))
1373 {
1374 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
1375
1376 /*
1377 * Execute the command.
1378 */
1379 if (!fNoExecute)
1380 rc = pCmd->pfnHandler(pCmd, &pDbgc->CmdHlp, pDbgc->pUVM, &pDbgc->aArgs[iArg], cArgs);
1381 pDbgc->rcCmd = rc;
1382 pDbgc->iArg = iArg;
1383 if (rc == VERR_DBGC_COMMAND_FAILED)
1384 rc = VINF_SUCCESS;
1385 }
1386 else
1387 {
1388 pDbgc->rcCmd = rc;
1389 pDbgc->iArg = iArg;
1390
1391 /* report parse / eval error. */
1392 switch (rc)
1393 {
1394 case VERR_DBGC_PARSE_TOO_FEW_ARGUMENTS:
1395 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1396 "Syntax error: Too few arguments. Minimum is %d for command '%s'.\n", pCmd->cArgsMin, pCmd->pszCmd);
1397 break;
1398 case VERR_DBGC_PARSE_TOO_MANY_ARGUMENTS:
1399 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1400 "Syntax error: Too many arguments. Maximum is %d for command '%s'.\n", pCmd->cArgsMax, pCmd->pszCmd);
1401 break;
1402 case VERR_DBGC_PARSE_ARGUMENT_OVERFLOW:
1403 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1404 "Syntax error: Too many arguments.\n");
1405 break;
1406 case VERR_DBGC_PARSE_UNBALANCED_QUOTE:
1407 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1408 "Syntax error: Unbalanced quote (argument %d).\n", cArgs);
1409 break;
1410 case VERR_DBGC_PARSE_UNBALANCED_PARENTHESIS:
1411 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1412 "Syntax error: Unbalanced parenthesis (argument %d).\n", cArgs);
1413 break;
1414 case VERR_DBGC_PARSE_EMPTY_ARGUMENT:
1415 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1416 "Syntax error: An argument or subargument contains nothing useful (argument %d).\n", cArgs);
1417 break;
1418 case VERR_DBGC_PARSE_UNEXPECTED_OPERATOR:
1419 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1420 "Syntax error: Invalid operator usage (argument %d).\n", cArgs);
1421 break;
1422 case VERR_DBGC_PARSE_INVALID_NUMBER:
1423 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1424 "Syntax error: Invalid numeric value (argument %d). If a string was the intention, then quote it.\n", cArgs);
1425 break;
1426 case VERR_DBGC_PARSE_NUMBER_TOO_BIG:
1427 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1428 "Error: Numeric overflow (argument %d).\n", cArgs);
1429 break;
1430 case VERR_DBGC_PARSE_INVALID_OPERATION:
1431 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1432 "Error: Invalid operation attempted (argument %d).\n", cArgs);
1433 break;
1434 case VERR_DBGC_PARSE_FUNCTION_NOT_FOUND:
1435 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1436 "Error: Function not found (argument %d).\n", cArgs);
1437 break;
1438 case VERR_DBGC_PARSE_NOT_A_FUNCTION:
1439 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1440 "Error: The function specified is not a function (argument %d).\n", cArgs);
1441 break;
1442 case VERR_DBGC_PARSE_NO_MEMORY:
1443 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1444 "Error: Out memory in the regular heap! Expect odd stuff to happen...\n");
1445 break;
1446 case VERR_DBGC_PARSE_INCORRECT_ARG_TYPE:
1447 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1448 "Error: Incorrect argument type (argument %d?).\n", cArgs);
1449 break;
1450 case VERR_DBGC_PARSE_VARIABLE_NOT_FOUND:
1451 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1452 "Error: An undefined variable was referenced (argument %d).\n", cArgs);
1453 break;
1454 case VERR_DBGC_PARSE_CONVERSION_FAILED:
1455 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1456 "Error: A conversion between two types failed (argument %d).\n", cArgs);
1457 break;
1458 case VERR_DBGC_PARSE_NOT_IMPLEMENTED:
1459 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1460 "Error: You hit a debugger feature which isn't implemented yet (argument %d).\n", cArgs);
1461 break;
1462 case VERR_DBGC_PARSE_BAD_RESULT_TYPE:
1463 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1464 "Error: Couldn't satisfy a request for a specific result type (argument %d). (Usually applies to symbols)\n", cArgs);
1465 break;
1466 case VERR_DBGC_PARSE_WRITEONLY_SYMBOL:
1467 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp,
1468 "Error: Cannot get symbol, it's set only (argument %d).\n", cArgs);
1469 break;
1470
1471 case VERR_DBGC_COMMAND_FAILED:
1472 break;
1473
1474 default:
1475 {
1476 PCRTSTATUSMSG pErr = RTErrGet(rc);
1477 if (strncmp(pErr->pszDefine, RT_STR_TUPLE("Unknown Status")))
1478 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Error: %s (%d) - %s\n", pErr->pszDefine, rc, pErr->pszMsgFull);
1479 else
1480 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Error: Unknown error %d (%#x)!\n", rc, rc);
1481 break;
1482 }
1483 }
1484 }
1485
1486 return rc;
1487}
1488
1489
1490/**
1491 * Loads the script in @a pszFilename and executes the commands within.
1492 *
1493 * @returns VBox status code. Will complain about error to console.
1494 * @param pDbgc Debugger console instance data.
1495 * @param pszFilename The path to the script file.
1496 * @param fAnnounce Whether to announce the script.
1497 */
1498int dbgcEvalScript(PDBGC pDbgc, const char *pszFilename, bool fAnnounce)
1499{
1500 FILE *pFile = fopen(pszFilename, "r");
1501 if (!pFile)
1502 return DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Failed to open '%s'.\n", pszFilename);
1503 if (fAnnounce)
1504 DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "Running script '%s'...\n", pszFilename);
1505
1506 /*
1507 * Execute it line by line.
1508 */
1509 int rc = VINF_SUCCESS;
1510 unsigned iLine = 0;
1511 char szLine[8192];
1512 while (fgets(szLine, sizeof(szLine), pFile))
1513 {
1514 /* check that the line isn't too long. */
1515 char *pszEnd = strchr(szLine, '\0');
1516 if (pszEnd == &szLine[sizeof(szLine) - 1])
1517 {
1518 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "runscript error: Line #%u is too long\n", iLine);
1519 break;
1520 }
1521 iLine++;
1522
1523 /* strip leading blanks and check for comment / blank line. */
1524 char *psz = RTStrStripL(szLine);
1525 if ( *psz == '\0'
1526 || *psz == '\n'
1527 || *psz == '#')
1528 continue;
1529
1530 /* strip trailing blanks and check for empty line (\r case). */
1531 while ( pszEnd > psz
1532 && RT_C_IS_SPACE(pszEnd[-1])) /* RT_C_IS_SPACE includes \n and \r normally. */
1533 *--pszEnd = '\0';
1534
1535 /** @todo check for Control-C / Cancel at this point... */
1536
1537 /*
1538 * Execute the command.
1539 *
1540 * This is a bit wasteful with scratch space btw., can fix it later.
1541 * The whole return code crap should be fixed too, so that it's possible
1542 * to know whether a command succeeded (RT_SUCCESS()) or failed, and
1543 * more importantly why it failed.
1544 */
1545 /** @todo optimize this. */
1546 rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "%s", psz);
1547 if (RT_FAILURE(rc))
1548 {
1549 if (rc == VERR_BUFFER_OVERFLOW)
1550 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "runscript error: Line #%u is too long (exec overflowed)\n", iLine);
1551 break;
1552 }
1553 if (rc == VWRN_DBGC_CMD_PENDING)
1554 {
1555 rc = DBGCCmdHlpPrintf(&pDbgc->CmdHlp, "runscript error: VWRN_DBGC_CMD_PENDING on line #%u, script terminated\n", iLine);
1556 break;
1557 }
1558 }
1559
1560 fclose(pFile);
1561 return rc;
1562}
1563
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