DBGCEval.cpp revision 10bea85296ddd1bd4ed6fca7f9bdf386d071ffb6
/* $Id$ */
/** @file
* DBGC - Debugger Console, command evaluator.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DBGC
#include "DBGCInternal.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Bitmap where set bits indicates the characters the may start an operator name. */
/**
* Initializes g_bmOperatorChars.
*/
void dbgcEvalInit(void)
{
}
/**
* Checks whether the character may be the start of an operator.
*
* @param ch The character.
*/
{
}
{
/*
* Removing any quoting and escapings.
*/
{
return VERR_PARSE_UNBALANCED_QUOTE;
cchExpr--;
pszExpr++;
/** @todo string unescaping. */
}
/*
* Make the argument.
*/
return VINF_SUCCESS;
}
{
/*
* Convert to number.
*/
char ch;
{
unsigned u = ch - '0';
if (u < 10 && u < uBase)
else
return VERR_PARSE_INVALID_NUMBER;
/* check for overflow - ARG!!! How to detect overflow correctly!?!?!? */
return VERR_PARSE_NUMBER_TOO_BIG;
/* next */
pszExpr++;
}
/*
* Initialize the argument.
*/
return VINF_SUCCESS;
}
/**
* Match variable and variable descriptor, promoting the variable if necessary.
*
* @returns VBox status code.
* @param pDbgc Debug console instanace.
* @param pVar Variable.
* @param pVarDesc Variable descriptor.
*/
{
/*
* (If match or promoted to match, return, else break.)
*/
switch (pVarDesc->enmCategory)
{
/*
* Anything goes
*/
case DBGCVAR_CAT_ANY:
return VINF_SUCCESS;
/*
* Pointer with and without range.
* We can try resolve strings and symbols as symbols and promote
* numbers to flat GC pointers.
*/
return VERR_PARSE_NO_RANGE_ALLOWED;
/* fallthru */
case DBGCVAR_CAT_POINTER:
{
case DBGCVAR_TYPE_GC_FLAT:
case DBGCVAR_TYPE_GC_FAR:
case DBGCVAR_TYPE_GC_PHYS:
case DBGCVAR_TYPE_HC_FLAT:
case DBGCVAR_TYPE_HC_PHYS:
return VINF_SUCCESS;
case DBGCVAR_TYPE_SYMBOL:
case DBGCVAR_TYPE_STRING:
{
if (RT_SUCCESS(rc))
{
/* deal with range */
{
}
return rc;
}
break;
}
case DBGCVAR_TYPE_NUMBER:
{
}
return VINF_SUCCESS;
default:
break;
}
break;
/*
* GC pointer with and without range.
* We can try resolve strings and symbols as symbols and
* promote numbers to flat GC pointers.
*/
return VERR_PARSE_NO_RANGE_ALLOWED;
/* fallthru */
case DBGCVAR_CAT_GC_POINTER:
{
case DBGCVAR_TYPE_GC_FLAT:
case DBGCVAR_TYPE_GC_FAR:
case DBGCVAR_TYPE_GC_PHYS:
return VINF_SUCCESS;
case DBGCVAR_TYPE_HC_FLAT:
case DBGCVAR_TYPE_HC_PHYS:
return VERR_PARSE_CONVERSION_FAILED;
case DBGCVAR_TYPE_SYMBOL:
case DBGCVAR_TYPE_STRING:
{
if (RT_SUCCESS(rc))
{
/* deal with range */
{
}
return rc;
}
break;
}
case DBGCVAR_TYPE_NUMBER:
{
return VINF_SUCCESS;
}
default:
break;
}
break;
/*
* Number with or without a range.
* Numbers can be resolved from symbols, but we cannot demote a pointer
* to a number.
*/
return VERR_PARSE_NO_RANGE_ALLOWED;
/* fallthru */
case DBGCVAR_CAT_NUMBER:
{
case DBGCVAR_TYPE_NUMBER:
return VINF_SUCCESS;
case DBGCVAR_TYPE_SYMBOL:
case DBGCVAR_TYPE_STRING:
{
if (RT_SUCCESS(rc))
{
return rc;
}
break;
}
default:
break;
}
break;
/*
* Strings can easily be made from symbols (and of course strings).
* We could consider reformatting the addresses and numbers into strings later...
*/
case DBGCVAR_CAT_STRING:
{
case DBGCVAR_TYPE_SYMBOL:
/* fallthru */
case DBGCVAR_TYPE_STRING:
return VINF_SUCCESS;
default:
break;
}
break;
/*
* Symol is pretty much the same thing as a string (at least until we actually implement it).
*/
case DBGCVAR_CAT_SYMBOL:
{
case DBGCVAR_TYPE_STRING:
/* fallthru */
case DBGCVAR_TYPE_SYMBOL:
return VINF_SUCCESS;
default:
break;
}
break;
/*
* Anything else is illegal.
*/
default:
break;
}
return VERR_PARSE_NO_ARGUMENT_MATCH;
}
/**
* Matches a set of variables with a description set.
*
* This is typically used for routine arguments before a call. The effects in
* addition to the validation, is that some variables might be propagated to
* other types in order to match the description. The following transformations
* are supported:
* - String reinterpreted as a symbol and resolved to a number or pointer.
* - Number to a pointer.
* - Pointer to a number.
*
* @returns VBox status code. Modified @a paVars on success.
*/
{
/*
* Just do basic min / max checks first.
*/
return VERR_PARSE_TOO_FEW_ARGUMENTS;
return VERR_PARSE_TOO_MANY_ARGUMENTS;
/*
* Match the descriptors and actual variables.
*/
unsigned cCurDesc = 0;
unsigned iVar = 0;
unsigned iVarDesc = 0;
{
/* walk the descriptors */
return VERR_PARSE_TOO_MANY_ARGUMENTS;
{
iVarDesc++;
return VERR_PARSE_TOO_MANY_ARGUMENTS;
cCurDesc = 0;
}
/*
* Skip thru optional arguments until we find something which matches
* or can easily be promoted to what the descriptor want.
*/
for (;;)
{
if (RT_SUCCESS(rc))
{
cCurDesc++;
break;
}
/* can we advance? */
cCurDesc = 0;
}
/* next var */
iVar++;
}
/*
* Check that the rest of the descriptors are optional.
*/
{
return VERR_PARSE_TOO_FEW_ARGUMENTS;
cCurDesc = 0;
/* next */
iVarDesc++;
}
return VINF_SUCCESS;
}
/**
* Evaluates one argument with respect to unary operators.
*
* @returns VBox status code. pResult contains the result on success.
*
* @param pDbgc Debugger console instance data.
* @param pszExpr The expression string.
* @param cchExpr The length of the expression.
* @param enmCategory The target category for the result.
* @param pResult Where to store the result of the expression evaluation.
*/
static int dbgcEvalSubUnary(PDBGC pDbgc, char *pszExpr, size_t cchExpr, DBGCVARCAT enmCategory, PDBGCVAR pResult)
{
/*
* The state of the expression is now such that it will start by zero or more
* unary operators and being followed by an expression of some kind.
* The expression is either plain or in parenthesis.
*
* Being in a lazy, recursive mode today, the parsing is done as simple as possible. :-)
* ASSUME: unary operators are all of equal precedence.
*/
int rc = VINF_SUCCESS;
if (pOp)
{
/* binary operators means syntax error. */
return VERR_PARSE_UNEXPECTED_OPERATOR;
/*
* If the next expression (the one following the unary operator) is in a
* parenthesis a full eval is needed. If not the unary eval will suffice.
*/
/* calc and strip next expr. */
while (RT_C_IS_BLANK(*pszExpr2))
pszExpr2++;
if (!*pszExpr2)
else
{
if (*pszExpr2 == '(')
else
if (RT_SUCCESS(rc))
}
}
else
{
/*
* Didn't find any operators, so it we have to check if this can be an
* function call before assuming numeric or string expression.
*
* (ASSUMPTIONS:)
* A function name only contains alphanumerical chars and it can not start
* with a numerical character.
* Immediately following the name is a parenthesis which must over
* the remaining part of the expression.
*/
{
pszFunEnd++;
if (*pszFunEnd != '(')
}
if (pszFunEnd)
{
/*
* Ok, it's a function call.
*/
if (fExternal)
if (!pFun)
return VERR_PARSE_FUNCTION_NOT_FOUND;
if (!pFun->pResultDesc)
return VERR_PARSE_NOT_A_FUNCTION;
/*
* Parse the expression in parenthesis.
*/
/** @todo implement multiple arguments. */
if (!rc)
{
rc = dbgcEvalSubMatchVars(pDbgc, pFun->cArgsMin, pFun->cArgsMax, pFun->paArgDescs, pFun->cArgDescs, &Arg, 1);
if (!rc)
}
}
else if ( enmCategory == DBGCVAR_CAT_STRING
|| enmCategory == DBGCVAR_CAT_SYMBOL)
else
{
/*
* Didn't find any operators, so it must be a plain expression.
* This might be numeric or a string expression.
*/
/// @todo 0b doesn't work as a binary prefix, we confuse it with 0bf8:0123 and stuff.
//else if (ch == '0' && (ch2 == 'b' || ch2 == 'b'))
// rc = dbgcEvalSubNum(pszExpr + 2, 2, pResult);
else
{
/*
* Hexadecimal number or a string?
*/
while (RT_C_IS_XDIGIT(*psz))
psz++;
if (!*psz)
{
*psz = '\0';
}
else
}
}
}
return rc;
}
/**
* Evaluates one argument.
*
* @returns VBox status code.
*
* @param pDbgc Debugger console instance data.
* @param pszExpr The expression string.
* @param enmCategory The target category for the result.
* @param pResult Where to store the result of the expression evaluation.
*/
int dbgcEvalSub(PDBGC pDbgc, char *pszExpr, size_t cchExpr, DBGCVARCAT enmCategory, PDBGCVAR pResult)
{
/*
* First we need to remove blanks in both ends.
* ASSUMES: There is no quoting unless the entire expression is a string.
*/
/* stripping. */
while (RT_C_IS_BLANK(*pszExpr))
if (!*pszExpr)
return VERR_PARSE_EMPTY_ARGUMENT;
/* it there is any kind of quoting in the expression, it's string meat. */
/*
* Check if there are any parenthesis which needs removing.
*/
{
do
{
unsigned cPar = 1;
char ch;
{
if (ch == '(')
cPar++;
else if (ch == ')')
{
if (cPar <= 0)
cPar--;
break;
}
/* next */
psz++;
}
if (ch)
break;
/* remove the parenthesis. */
pszExpr++;
cchExpr -= 2;
/* strip blanks. */
while (RT_C_IS_BLANK(*pszExpr))
if (!*pszExpr)
return VERR_PARSE_EMPTY_ARGUMENT;
}
/* tabs to spaces. */
*psz = ' ';
/*
* Now, we need to look for the binary operator with the lowest precedence.
*
* If there are no operators we're left with a simple expression which we
* evaluate with respect to unary operators
*/
char *pszOpSplit = NULL;
unsigned cBinaryOps = 0;
unsigned cPar = 0;
char ch;
char chPrev = ' ';
bool fBinary = false;
{
//Log2(("ch=%c cPar=%d fBinary=%d\n", ch, cPar, fBinary));
/*
* Parenthesis.
*/
if (ch == '(')
{
cPar++;
fBinary = false;
}
else if (ch == ')')
{
if (cPar <= 0)
cPar--;
fBinary = true;
}
/*
* Potential operator.
*/
{
: NULL;
if (pOp)
{
/* If not the right kind of operator we've got a syntax error. */
return VERR_PARSE_UNEXPECTED_OPERATOR;
/*
* Update the parse state and skip the operator.
*/
if (!pOpSplit)
{
pszOpSplit = psz;
}
else if (fBinary)
{
cBinaryOps++;
{
pszOpSplit = psz;
}
}
fBinary = false;
}
else
fBinary = true;
}
/* next */
psz++;
} /* parse loop. */
/*
* Either we found an operator to divide the expression by
* or we didn't find any. In the first case it's divide and
* conquer. In the latter it's a single expression which
* needs dealing with its unary operators if any.
*/
int rc;
if ( cBinaryOps
{
/* process 1st sub expression. */
*pszOpSplit = '\0';
if (RT_SUCCESS(rc))
{
/* process 2nd sub expression. */
if (RT_SUCCESS(rc))
/* apply the operator. */
}
}
else if (cBinaryOps)
{
/* process sub expression. */
if (RT_SUCCESS(rc))
/* apply the operator. */
}
else
/* plain expression or using unary operators perhaps with parentheses. */
return rc;
}
/**
* Parses the arguments of one command.
*
* @returns VBox statuc code. On parser errors the index of the troublesome
* argument is indicated by *pcArg.
*
* @param pDbgc Debugger console instance data.
* @param pCmd Pointer to the command descriptor.
* @param pszArg Pointer to the arguments to parse.
* @param paArgs Where to store the parsed arguments.
* @param cArgs Size of the paArgs array.
* @param pcArgs Where to store the number of arguments. In the event
* of an error this is (ab)used to store the index of the
* offending argument.
*/
static int dbgcProcessArguments(PDBGC pDbgc, PCDBGCCMD pCmd, char *pszArgs, PDBGCVAR paArgs, unsigned cArgs, unsigned *pcArgs)
{
/*
* Check if we have any argument and if the command takes any.
*/
*pcArgs = 0;
/* strip leading blanks. */
pszArgs++;
if (!*pszArgs)
{
return VINF_SUCCESS;
return VERR_PARSE_TOO_FEW_ARGUMENTS;
}
/** @todo fixme - foo() doesn't work. */
return VERR_PARSE_TOO_MANY_ARGUMENTS;
/*
* This is a hack, it's "temporary" and should go away "when" the parser is
* modified to match arguments while parsing.
*/
&& cArgs >= 1)
{
*pcArgs = 1;
}
/*
* The parse loop.
*/
*pcArgs = 0;
do
{
/*
* Can we have another argument?
*/
return VERR_PARSE_TOO_MANY_ARGUMENTS;
return VERR_PARSE_ARGUMENT_OVERFLOW;
/*
* Find the end of the argument.
*/
int cPar = 0;
char chQuote = '\0';
char ch;
bool fBinary = false;
for (;;)
{
/*
* Check for the end.
*/
{
if (chQuote)
return VERR_PARSE_UNBALANCED_QUOTE;
if (cPar)
break;
}
/*
* When quoted we ignore everything but the quotation char.
* We use the REXX way of escaping the quotation char, i.e. double occurrence.
*/
{
if (chQuote)
{
/* end quote? */
{
psz++; /* skip the escaped quote char */
else
}
}
else
}
/*
* Parenthesis can of course be nested.
*/
else if (ch == '(')
{
cPar++;
fBinary = false;
}
else if (ch == ')')
{
if (!cPar)
cPar--;
fBinary = true;
}
{
/*
* Encountering blanks may mean the end of it all. A binary operator
* will force continued parsing.
*/
if (RT_C_IS_BLANK(*psz))
{
while (RT_C_IS_BLANK(*psz))
psz++;
break; /* the end. */
psz++;
fBinary = false;
continue;
}
/*
* Look for operators without a space up front.
*/
if (dbgcIsOpChar(*psz))
{
if (pOp)
{
{
/** @todo this is a parsing error really. */
break; /* the end. */
}
psz++;
fBinary = false;
continue;
}
}
fBinary = true;
}
/* next char */
psz++;
}
*pszEnd = '\0';
/* (psz = next char to process) */
/*
* Parse and evaluate the argument.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Next.
*/
pArg++;
(*pcArgs)++;
pszArgs++;
} while (*pszArgs);
/*
* Match the arguments.
*/
return dbgcEvalSubMatchVars(pDbgc, pCmd->cArgsMin, pCmd->cArgsMax, pCmd->paArgDescs, pCmd->cArgDescs, pArg0, pArg - pArg0);
}
/**
* Evaluate one command.
*
* @returns VBox status code. This is also stored in DBGC::rcCmd.
*
* @param pDbgc Debugger console instance data.
* @param pszCmd Pointer to the command.
* @param cchCmd Length of the command.
* @param fNoExecute Indicates that no commands should actually be executed.
*
*/
{
char *pszCmdInput = pszCmd;
/*
* Skip blanks.
*/
while (RT_C_IS_BLANK(*pszCmd))
/* external command? */
if (fExternal)
/*
* Find arguments.
*/
while (RT_C_IS_ALNUM(*pszArgs))
pszArgs++;
{
}
/*
* Find the command.
*/
{
}
/*
* Parse arguments (if any).
*/
unsigned cArgs;
int rc = dbgcProcessArguments(pDbgc, pCmd, pszArgs, &pDbgc->aArgs[pDbgc->iArg], RT_ELEMENTS(pDbgc->aArgs) - pDbgc->iArg, &cArgs);
if (RT_SUCCESS(rc))
{
/*
* Execute the command.
*/
if (!fNoExecute)
if (rc == VERR_DBGC_COMMAND_FAILED)
rc = VINF_SUCCESS;
}
else
{
/* report parse / eval error. */
switch (rc)
{
"Syntax error: Too few arguments. Minimum is %d for command '%s'.\n", pCmd->cArgsMin, pCmd->pszCmd);
break;
"Syntax error: Too many arguments. Maximum is %d for command '%s'.\n", pCmd->cArgsMax, pCmd->pszCmd);
break;
"Syntax error: Too many arguments.\n");
break;
"Syntax error: Unbalanced quote (argument %d).\n", cArgs);
break;
"Syntax error: Unbalanced parenthesis (argument %d).\n", cArgs);
break;
"Syntax error: An argument or subargument contains nothing useful (argument %d).\n", cArgs);
break;
"Syntax error: Invalid operator usage (argument %d).\n", cArgs);
break;
"Syntax error: Ivalid numeric value (argument %d). If a string was the intention, then quote it.\n", cArgs);
break;
"Error: Numeric overflow (argument %d).\n", cArgs);
break;
"Error: Invalid operation attempted (argument %d).\n", cArgs);
break;
"Error: Function not found (argument %d).\n", cArgs);
break;
"Error: The function specified is not a function (argument %d).\n", cArgs);
break;
case VERR_PARSE_NO_MEMORY:
"Error: Out memory in the regular heap! Expect odd stuff to happen...\n", cArgs);
break;
"Error: Incorrect argument type (argument %d?).\n", cArgs);
break;
"Error: An undefined variable was referenced (argument %d).\n", cArgs);
break;
"Error: A conversion between two types failed (argument %d).\n", cArgs);
break;
"Error: You hit a debugger feature which isn't implemented yet (argument %d).\n", cArgs);
break;
"Error: Couldn't satisfy a request for a specific result type (argument %d). (Usually applies to symbols)\n", cArgs);
break;
"Error: Cannot get symbol, it's set only (argument %d).\n", cArgs);
break;
case VERR_DBGC_COMMAND_FAILED:
break;
default:
break;
}
}
return rc;
}