2005-01-06 01:34:41 +00:00
|
|
|
/*-
|
2017-11-30 15:48:35 +00:00
|
|
|
* SPDX-License-Identifier: MIT-CMU
|
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1991,1990 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* Permission to use, copy, modify and distribute this software and its
|
|
|
|
* documentation is hereby granted, provided that both the copyright
|
|
|
|
* notice and this permission notice appear in all copies of the
|
|
|
|
* software, derivative works or modified versions, and any portions
|
|
|
|
* thereof, and that both notices appear in supporting documentation.
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
|
|
|
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
|
1993-06-12 14:58:17 +00:00
|
|
|
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
|
|
|
|
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* Carnegie Mellon requests users of this software to return to
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* any improvements or extensions that they make and grant Carnegie the
|
|
|
|
* rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Author: David B. Golub, Carnegie Mellon University
|
|
|
|
* Date: 7/90
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Lexical analyzer.
|
|
|
|
*/
|
2003-06-10 22:09:23 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-08-13 03:50:34 +00:00
|
|
|
#include <sys/param.h>
|
Add a simple scripting facility to DDB(4), allowing the user to
define a set of named scripts. Each script consists of a list of DDB
commands separated by ";"s that will be executed verbatim. No higher
level language constructs, such as branching, are provided for:
scripts are executed by sequentially injecting commands into the DDB
input buffer.
Four new commands are present in DDB: "run" to run a specific script,
"script" to define or print a script, "scripts" to list currently
defined scripts, and "unscript" to delete a script, modeled on shell
alias commands. Scripts may also be manipulated using sysctls in the
debug.ddb.scripting MIB space, although users will prefer to use the
soon-to-be-added ddb(8) tool for usability reasons.
Scripts with certain names are automatically executed on various DDB
events, such as entering the debugger via a panic, a witness error,
watchdog, breakpoint, sysctl, serial break, etc, allowing customized
handling.
MFC after: 3 months
2007-12-26 09:33:19 +00:00
|
|
|
#include <sys/libkern.h>
|
2019-09-09 16:31:14 +00:00
|
|
|
#include <sys/lock.h>
|
1995-12-10 19:08:32 +00:00
|
|
|
|
1994-08-13 03:50:34 +00:00
|
|
|
#include <ddb/ddb.h>
|
1993-06-12 14:58:17 +00:00
|
|
|
#include <ddb/db_lex.h>
|
|
|
|
|
Add a simple scripting facility to DDB(4), allowing the user to
define a set of named scripts. Each script consists of a list of DDB
commands separated by ";"s that will be executed verbatim. No higher
level language constructs, such as branching, are provided for:
scripts are executed by sequentially injecting commands into the DDB
input buffer.
Four new commands are present in DDB: "run" to run a specific script,
"script" to define or print a script, "scripts" to list currently
defined scripts, and "unscript" to delete a script, modeled on shell
alias commands. Scripts may also be manipulated using sysctls in the
debug.ddb.scripting MIB space, although users will prefer to use the
soon-to-be-added ddb(8) tool for usability reasons.
Scripts with certain names are automatically executed on various DDB
events, such as entering the debugger via a panic, a witness error,
watchdog, breakpoint, sysctl, serial break, etc, allowing customized
handling.
MFC after: 3 months
2007-12-26 09:33:19 +00:00
|
|
|
static char db_line[DB_MAXLINE];
|
1995-12-10 13:32:43 +00:00
|
|
|
static char * db_lp, *db_endlp;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2019-09-09 16:31:14 +00:00
|
|
|
static int db_lex(int);
|
2002-03-20 05:14:42 +00:00
|
|
|
static void db_flush_line(void);
|
|
|
|
static int db_read_char(void);
|
|
|
|
static void db_unread_char(int);
|
1994-08-18 22:36:09 +00:00
|
|
|
|
1993-06-12 14:58:17 +00:00
|
|
|
int
|
2014-10-12 18:01:52 +00:00
|
|
|
db_read_line(void)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = db_readline(db_line, sizeof(db_line));
|
|
|
|
if (i == 0)
|
|
|
|
return (0); /* EOI */
|
|
|
|
db_lp = db_line;
|
|
|
|
db_endlp = db_lp + i;
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
Add a simple scripting facility to DDB(4), allowing the user to
define a set of named scripts. Each script consists of a list of DDB
commands separated by ";"s that will be executed verbatim. No higher
level language constructs, such as branching, are provided for:
scripts are executed by sequentially injecting commands into the DDB
input buffer.
Four new commands are present in DDB: "run" to run a specific script,
"script" to define or print a script, "scripts" to list currently
defined scripts, and "unscript" to delete a script, modeled on shell
alias commands. Scripts may also be manipulated using sysctls in the
debug.ddb.scripting MIB space, although users will prefer to use the
soon-to-be-added ddb(8) tool for usability reasons.
Scripts with certain names are automatically executed on various DDB
events, such as entering the debugger via a panic, a witness error,
watchdog, breakpoint, sysctl, serial break, etc, allowing customized
handling.
MFC after: 3 months
2007-12-26 09:33:19 +00:00
|
|
|
/*
|
|
|
|
* Simulate a line of input into DDB.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
db_inject_line(const char *command)
|
|
|
|
{
|
|
|
|
|
|
|
|
strlcpy(db_line, command, sizeof(db_line));
|
|
|
|
db_lp = db_line;
|
|
|
|
db_endlp = db_lp + strlen(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In rare cases, we may want to pull the remainder of the line input
|
|
|
|
* verbatim, rather than lexing it. For example, when assigning literal
|
|
|
|
* values associated with scripts. In that case, return a static pointer to
|
|
|
|
* the current location in the input buffer. The caller must be aware that
|
|
|
|
* the contents are not stable if other lex/input calls are made.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
db_get_line(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (db_lp);
|
|
|
|
}
|
|
|
|
|
1995-11-29 10:25:50 +00:00
|
|
|
static void
|
1993-06-12 14:58:17 +00:00
|
|
|
db_flush_line()
|
|
|
|
{
|
|
|
|
db_lp = db_line;
|
|
|
|
db_endlp = db_line;
|
|
|
|
}
|
|
|
|
|
1995-11-29 10:25:50 +00:00
|
|
|
static int db_look_char = 0;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1995-11-29 10:25:50 +00:00
|
|
|
static int
|
2014-10-12 18:01:52 +00:00
|
|
|
db_read_char(void)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (db_look_char != 0) {
|
|
|
|
c = db_look_char;
|
|
|
|
db_look_char = 0;
|
|
|
|
}
|
|
|
|
else if (db_lp >= db_endlp)
|
|
|
|
c = -1;
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1993-06-12 14:58:17 +00:00
|
|
|
c = *db_lp++;
|
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
1995-11-29 10:25:50 +00:00
|
|
|
static void
|
1993-06-12 14:58:17 +00:00
|
|
|
db_unread_char(c)
|
1993-11-25 01:38:01 +00:00
|
|
|
int c;
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
db_look_char = c;
|
|
|
|
}
|
|
|
|
|
1995-11-29 10:25:50 +00:00
|
|
|
static int db_look_token = 0;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
void
|
2019-09-09 16:31:14 +00:00
|
|
|
db_unread_token(int t)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
db_look_token = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-09-09 16:31:14 +00:00
|
|
|
db_read_token_flags(int flags)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
2019-09-09 16:31:14 +00:00
|
|
|
MPASS((flags & ~(DRT_VALID_FLAGS_MASK)) == 0);
|
|
|
|
|
1993-06-12 14:58:17 +00:00
|
|
|
if (db_look_token) {
|
|
|
|
t = db_look_token;
|
|
|
|
db_look_token = 0;
|
|
|
|
}
|
|
|
|
else
|
2019-09-09 16:31:14 +00:00
|
|
|
t = db_lex(flags);
|
1993-06-12 14:58:17 +00:00
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
1998-07-08 09:11:43 +00:00
|
|
|
db_expr_t db_tok_number;
|
1993-06-12 14:58:17 +00:00
|
|
|
char db_tok_string[TOK_STRING_SIZE];
|
|
|
|
|
1998-07-08 09:11:43 +00:00
|
|
|
db_expr_t db_radix = 16;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
void
|
2014-10-12 18:01:52 +00:00
|
|
|
db_flush_lex(void)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
db_flush_line();
|
|
|
|
db_look_char = 0;
|
|
|
|
db_look_token = 0;
|
|
|
|
}
|
|
|
|
|
1994-08-18 22:36:09 +00:00
|
|
|
static int
|
2019-09-09 16:31:14 +00:00
|
|
|
db_lex(int flags)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2019-09-09 16:31:14 +00:00
|
|
|
int c, n, radix_mode;
|
2019-09-09 16:32:23 +00:00
|
|
|
bool lex_wspace, lex_hex_numbers;
|
2019-09-09 16:31:14 +00:00
|
|
|
|
|
|
|
switch (flags & DRT_RADIX_MASK) {
|
|
|
|
case DRT_DEFAULT_RADIX:
|
|
|
|
radix_mode = -1;
|
|
|
|
break;
|
|
|
|
case DRT_OCTAL:
|
|
|
|
radix_mode = 8;
|
|
|
|
break;
|
|
|
|
case DRT_DECIMAL:
|
|
|
|
radix_mode = 10;
|
|
|
|
break;
|
|
|
|
case DRT_HEXADECIMAL:
|
|
|
|
radix_mode = 16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lex_wspace = ((flags & DRT_WSPACE) != 0);
|
2019-09-09 16:32:23 +00:00
|
|
|
lex_hex_numbers = ((flags & DRT_HEX) != 0);
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
c = db_read_char();
|
2019-09-09 16:31:14 +00:00
|
|
|
for (n = 0; c <= ' ' || c > '~'; n++) {
|
1993-06-12 14:58:17 +00:00
|
|
|
if (c == '\n' || c == -1)
|
|
|
|
return (tEOL);
|
|
|
|
c = db_read_char();
|
|
|
|
}
|
2019-09-09 16:31:14 +00:00
|
|
|
if (lex_wspace && n != 0) {
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tWSPACE);
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2019-09-09 16:32:23 +00:00
|
|
|
if ((c >= '0' && c <= '9') ||
|
|
|
|
(lex_hex_numbers &&
|
|
|
|
((c >= 'a' && c <= 'f') ||
|
|
|
|
(c >= 'A' && c <= 'F')))) {
|
1993-06-12 14:58:17 +00:00
|
|
|
/* number */
|
1993-11-25 01:38:01 +00:00
|
|
|
int r, digit = 0;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2019-09-09 16:31:14 +00:00
|
|
|
if (radix_mode != -1)
|
|
|
|
r = radix_mode;
|
2019-09-09 16:32:23 +00:00
|
|
|
else if (c != '0')
|
1993-06-12 14:58:17 +00:00
|
|
|
r = db_radix;
|
|
|
|
else {
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == 'O' || c == 'o')
|
|
|
|
r = 8;
|
|
|
|
else if (c == 'T' || c == 't')
|
|
|
|
r = 10;
|
|
|
|
else if (c == 'X' || c == 'x')
|
|
|
|
r = 16;
|
|
|
|
else {
|
|
|
|
r = db_radix;
|
|
|
|
db_unread_char(c);
|
|
|
|
}
|
|
|
|
c = db_read_char();
|
|
|
|
}
|
|
|
|
db_tok_number = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (c >= '0' && c <= ((r == 8) ? '7' : '9'))
|
|
|
|
digit = c - '0';
|
|
|
|
else if (r == 16 && ((c >= 'A' && c <= 'F') ||
|
|
|
|
(c >= 'a' && c <= 'f'))) {
|
|
|
|
if (c >= 'a')
|
|
|
|
digit = c - 'a' + 10;
|
|
|
|
else if (c >= 'A')
|
|
|
|
digit = c - 'A' + 10;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
db_tok_number = db_tok_number * r + digit;
|
|
|
|
c = db_read_char();
|
|
|
|
}
|
|
|
|
if ((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
(c == '_'))
|
|
|
|
{
|
|
|
|
db_error("Bad character in number\n");
|
|
|
|
db_flush_lex();
|
|
|
|
return (tEOF);
|
|
|
|
}
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tNUMBER);
|
|
|
|
}
|
|
|
|
if ((c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
c == '_' || c == '\\')
|
|
|
|
{
|
|
|
|
/* string */
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
cp = db_tok_string;
|
|
|
|
if (c == '\\') {
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == '\n' || c == -1)
|
|
|
|
db_error("Bad escape\n");
|
|
|
|
}
|
|
|
|
*cp++ = c;
|
|
|
|
while (1) {
|
|
|
|
c = db_read_char();
|
|
|
|
if ((c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= '0' && c <= '9') ||
|
2002-03-10 17:08:24 +00:00
|
|
|
c == '_' || c == '\\' || c == ':' || c == '.')
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
if (c == '\\') {
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == '\n' || c == -1)
|
|
|
|
db_error("Bad escape\n");
|
|
|
|
}
|
|
|
|
*cp++ = c;
|
|
|
|
if (cp == db_tok_string+sizeof(db_tok_string)) {
|
|
|
|
db_error("String too long\n");
|
|
|
|
db_flush_lex();
|
|
|
|
return (tEOF);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*cp = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tIDENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '+':
|
|
|
|
return (tPLUS);
|
|
|
|
case '-':
|
|
|
|
return (tMINUS);
|
|
|
|
case '.':
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == '.')
|
|
|
|
return (tDOTDOT);
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tDOT);
|
|
|
|
case '*':
|
|
|
|
return (tSTAR);
|
|
|
|
case '/':
|
|
|
|
return (tSLASH);
|
|
|
|
case '=':
|
2016-05-16 19:42:38 +00:00
|
|
|
c = db_read_char();
|
|
|
|
if (c == '=')
|
|
|
|
return (tLOG_EQ);
|
|
|
|
db_unread_char(c);
|
1993-06-12 14:58:17 +00:00
|
|
|
return (tEQ);
|
|
|
|
case '%':
|
|
|
|
return (tPCT);
|
|
|
|
case '#':
|
|
|
|
return (tHASH);
|
|
|
|
case '(':
|
|
|
|
return (tLPAREN);
|
|
|
|
case ')':
|
|
|
|
return (tRPAREN);
|
|
|
|
case ',':
|
|
|
|
return (tCOMMA);
|
|
|
|
case '"':
|
|
|
|
return (tDITTO);
|
|
|
|
case '$':
|
|
|
|
return (tDOLLAR);
|
|
|
|
case '!':
|
2016-05-16 19:42:38 +00:00
|
|
|
c = db_read_char();
|
|
|
|
if (c == '='){
|
|
|
|
return (tLOG_NOT_EQ);
|
|
|
|
}
|
|
|
|
db_unread_char(c);
|
1993-06-12 14:58:17 +00:00
|
|
|
return (tEXCL);
|
2019-09-09 16:32:23 +00:00
|
|
|
case ':':
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == ':')
|
|
|
|
return (tCOLONCOLON);
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tCOLON);
|
Add a simple scripting facility to DDB(4), allowing the user to
define a set of named scripts. Each script consists of a list of DDB
commands separated by ";"s that will be executed verbatim. No higher
level language constructs, such as branching, are provided for:
scripts are executed by sequentially injecting commands into the DDB
input buffer.
Four new commands are present in DDB: "run" to run a specific script,
"script" to define or print a script, "scripts" to list currently
defined scripts, and "unscript" to delete a script, modeled on shell
alias commands. Scripts may also be manipulated using sysctls in the
debug.ddb.scripting MIB space, although users will prefer to use the
soon-to-be-added ddb(8) tool for usability reasons.
Scripts with certain names are automatically executed on various DDB
events, such as entering the debugger via a panic, a witness error,
watchdog, breakpoint, sysctl, serial break, etc, allowing customized
handling.
MFC after: 3 months
2007-12-26 09:33:19 +00:00
|
|
|
case ';':
|
|
|
|
return (tSEMI);
|
2016-05-16 19:42:38 +00:00
|
|
|
case '&':
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == '&')
|
|
|
|
return (tLOG_AND);
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tBIT_AND);
|
|
|
|
case '|':
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == '|')
|
|
|
|
return (tLOG_OR);
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tBIT_OR);
|
1993-06-12 14:58:17 +00:00
|
|
|
case '<':
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == '<')
|
|
|
|
return (tSHIFT_L);
|
2016-05-16 19:42:38 +00:00
|
|
|
if (c == '=')
|
|
|
|
return (tLESS_EQ);
|
1993-06-12 14:58:17 +00:00
|
|
|
db_unread_char(c);
|
2016-05-16 19:42:38 +00:00
|
|
|
return (tLESS);
|
1993-06-12 14:58:17 +00:00
|
|
|
case '>':
|
|
|
|
c = db_read_char();
|
|
|
|
if (c == '>')
|
|
|
|
return (tSHIFT_R);
|
2016-05-16 19:42:38 +00:00
|
|
|
if (c == '=')
|
|
|
|
return (tGREATER_EQ);
|
1993-06-12 14:58:17 +00:00
|
|
|
db_unread_char(c);
|
2016-05-16 19:42:38 +00:00
|
|
|
return (tGREATER);
|
|
|
|
case '?':
|
|
|
|
return (tQUESTION);
|
|
|
|
case '~':
|
|
|
|
return (tBIT_NOT);
|
1993-06-12 14:58:17 +00:00
|
|
|
case -1:
|
|
|
|
return (tEOF);
|
|
|
|
}
|
|
|
|
db_printf("Bad character\n");
|
|
|
|
db_flush_lex();
|
|
|
|
return (tEOF);
|
|
|
|
}
|