2023-12-20 16:20:36 +00:00
|
|
|
/***************************************************************************************
|
2024-01-11 12:11:42 +00:00
|
|
|
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
|
|
|
*
|
|
|
|
* NEMU is licensed under Mulan PSL v2.
|
|
|
|
* You can use this software according to the terms and conditions of the Mulan
|
|
|
|
*PSL v2. You may obtain a copy of Mulan PSL v2 at:
|
|
|
|
* http://license.coscl.org.cn/MulanPSL2
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
|
|
|
|
*KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
|
|
*NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the Mulan PSL v2 for more details.
|
|
|
|
***************************************************************************************/
|
2023-12-20 16:20:36 +00:00
|
|
|
|
2024-01-11 12:11:42 +00:00
|
|
|
#include "sdb.h"
|
2024-01-11 14:23:16 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "sys/types.h"
|
2023-12-20 16:20:36 +00:00
|
|
|
#include <cpu/cpu.h>
|
2024-01-11 14:23:16 +00:00
|
|
|
#include <errno.h>
|
2024-01-11 12:11:42 +00:00
|
|
|
#include <isa.h>
|
2024-01-11 14:31:30 +00:00
|
|
|
#include <memory/paddr.h>
|
2023-12-20 16:20:36 +00:00
|
|
|
#include <readline/history.h>
|
2024-01-11 12:11:42 +00:00
|
|
|
#include <readline/readline.h>
|
2024-01-11 14:23:16 +00:00
|
|
|
#include <stdint.h>
|
2023-12-20 16:20:36 +00:00
|
|
|
|
|
|
|
static int is_batch_mode = false;
|
|
|
|
|
2024-01-11 12:11:42 +00:00
|
|
|
// command handlers
|
|
|
|
static int cmd_help(char *args);
|
|
|
|
static int cmd_c(char *args);
|
|
|
|
static int cmd_q(char *args);
|
2024-01-11 14:23:16 +00:00
|
|
|
static int cmd_x(char *args);
|
2024-01-11 12:11:42 +00:00
|
|
|
static int cmd_si(char *args);
|
|
|
|
static int cmd_info(char *args);
|
|
|
|
static int cmd_info_r(char *args);
|
|
|
|
static int cmd_info_w(char *args);
|
|
|
|
|
|
|
|
static struct CMDTable {
|
|
|
|
const char *name;
|
|
|
|
const char *description;
|
|
|
|
int (*handler)(char *);
|
2024-01-11 12:49:42 +00:00
|
|
|
struct CMDTable *subcommand;
|
2024-01-11 12:11:42 +00:00
|
|
|
int nr_subcommand;
|
2024-01-11 12:49:42 +00:00
|
|
|
} cmd_info_table[] =
|
|
|
|
{
|
|
|
|
{"r", "List all registers and their contents", cmd_info_r, NULL, 0},
|
|
|
|
{"w", "Status of specified watchpoints", cmd_info_w, NULL, 0},
|
|
|
|
},
|
|
|
|
cmd_table[] = {
|
|
|
|
{"help", "Display information about all supported commands", cmd_help,
|
|
|
|
NULL, 0},
|
|
|
|
{"c", "Continue the execution of the program", cmd_c, NULL, 0},
|
|
|
|
{"q", "Exit NEMU", cmd_q, NULL, 0},
|
2024-01-11 14:25:29 +00:00
|
|
|
{"x", "Examine content of physical memory address", cmd_x, NULL, 0},
|
2024-01-11 12:49:42 +00:00
|
|
|
{"si", "Execute next [n] program line", cmd_si, NULL, 0},
|
|
|
|
{"info", "Print information of registers or watchpoints", cmd_info,
|
|
|
|
cmd_info_table, ARRLEN(cmd_info_table)},
|
2024-01-11 12:11:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NR_CMD ARRLEN(cmd_table)
|
|
|
|
|
2023-12-20 16:20:36 +00:00
|
|
|
void init_regex();
|
|
|
|
void init_wp_pool();
|
|
|
|
|
2024-01-11 12:11:42 +00:00
|
|
|
/* We use the `readline' library to provide more flexibility to read from stdin.
|
|
|
|
*/
|
|
|
|
static char *rl_gets() {
|
2023-12-20 16:20:36 +00:00
|
|
|
static char *line_read = NULL;
|
|
|
|
|
|
|
|
if (line_read) {
|
|
|
|
free(line_read);
|
|
|
|
line_read = NULL;
|
|
|
|
}
|
|
|
|
|
2024-01-11 14:27:22 +00:00
|
|
|
line_read = readline("\e[1;34m(nemu)\e[0m ");
|
2023-12-20 16:20:36 +00:00
|
|
|
|
|
|
|
if (line_read && *line_read) {
|
|
|
|
add_history(line_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line_read;
|
|
|
|
}
|
|
|
|
|
2024-01-11 14:23:16 +00:00
|
|
|
/* Extract Integer from a string. Can handle hex, binary and decimal numbers.
|
|
|
|
* Print error if meet any error.
|
|
|
|
* Return `UINTMAX_MAX` if the string is invalid or number exceed the limit of
|
|
|
|
* uint.
|
|
|
|
*/
|
|
|
|
static word_t parse_uint(const char *arg, bool *success) {
|
2024-01-11 14:25:29 +00:00
|
|
|
if (arg == NULL) {
|
|
|
|
puts("Invalid uint argument.");
|
|
|
|
*success = false;
|
|
|
|
return 0;
|
|
|
|
}
|
2024-01-11 14:23:16 +00:00
|
|
|
int base = 10;
|
|
|
|
int token_length = strnlen(arg, 34);
|
|
|
|
if (token_length > 2) {
|
|
|
|
if (arg[0] == '0' && (arg[1] == 'b' || arg[1] == 'B')) {
|
|
|
|
base = 2;
|
|
|
|
arg = arg + 2;
|
|
|
|
} else if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X')) {
|
|
|
|
base = 16;
|
|
|
|
arg = arg + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char *endptr;
|
|
|
|
uintmax_t n = strtoumax(arg, &endptr, base);
|
|
|
|
if (errno == ERANGE || n > WORD_T_MAX) {
|
|
|
|
printf("%s is exceed the limit of uint\n", arg);
|
|
|
|
*success = false;
|
|
|
|
return 0;
|
|
|
|
} else if (arg == endptr) {
|
|
|
|
puts("Invalid uint argument.");
|
|
|
|
*success = false;
|
|
|
|
return 0;
|
|
|
|
} else if (n > WORD_T_MAX) {
|
|
|
|
*success = false;
|
|
|
|
return WORD_T_MAX;
|
|
|
|
} else {
|
|
|
|
*success = true;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 14:35:40 +00:00
|
|
|
static paddr_t parse_expr(const char *arg, bool *success) {
|
2024-01-11 14:23:16 +00:00
|
|
|
if (arg == NULL) {
|
|
|
|
puts("Invalid expr argument.");
|
|
|
|
*success = false;
|
|
|
|
return 0;
|
2024-01-11 14:35:40 +00:00
|
|
|
} else {
|
|
|
|
bool res = false;
|
|
|
|
// FIXME: We cannot use `parse_uint` here, it accept `-1234` as input
|
|
|
|
paddr_t addr = parse_uint(arg, &res);
|
|
|
|
*success = res;
|
|
|
|
return addr;
|
2024-01-11 14:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 16:20:36 +00:00
|
|
|
static int cmd_c(char *args) {
|
|
|
|
cpu_exec(-1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_q(char *args) {
|
2024-01-11 09:35:39 +00:00
|
|
|
nemu_state.state = NEMU_QUIT;
|
2024-01-11 09:32:50 +00:00
|
|
|
return -1;
|
2023-12-20 16:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 10:12:27 +00:00
|
|
|
/* Single stepping
|
|
|
|
* <step>: execute <step> step
|
|
|
|
*/
|
2024-01-11 09:50:33 +00:00
|
|
|
static int cmd_si(char *args) {
|
2024-01-11 12:18:53 +00:00
|
|
|
char *arg = strtok(NULL, " ");
|
2024-01-11 10:12:27 +00:00
|
|
|
if (arg == NULL) {
|
|
|
|
cpu_exec(1);
|
|
|
|
} else {
|
2024-01-11 14:23:16 +00:00
|
|
|
bool res = false;
|
|
|
|
word_t n = parse_uint(arg, &res);
|
|
|
|
if (!res)
|
|
|
|
goto wrong_usage;
|
|
|
|
cpu_exec(n);
|
2024-01-11 10:12:27 +00:00
|
|
|
}
|
2024-01-11 09:50:33 +00:00
|
|
|
return 0;
|
2024-01-11 14:23:16 +00:00
|
|
|
|
|
|
|
wrong_usage:
|
|
|
|
printf("Invalid argument for command si: %s\n", args);
|
|
|
|
printf("Usage: si [N: uint]\n");
|
|
|
|
return 0;
|
2024-01-11 09:50:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 12:49:42 +00:00
|
|
|
static int cmd_info_r(char *args) {
|
|
|
|
isa_reg_display();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-12-20 16:20:36 +00:00
|
|
|
|
2024-01-11 14:23:16 +00:00
|
|
|
static int cmd_info_w(char *args) {
|
|
|
|
printf("Not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_x(char *args) {
|
|
|
|
char *arg = strtok(NULL, " ");
|
|
|
|
bool res = false;
|
|
|
|
word_t n = parse_uint(arg, &res);
|
|
|
|
if (!res)
|
|
|
|
goto wrong_usage;
|
|
|
|
arg = strtok(NULL, " ");
|
|
|
|
word_t addr = parse_expr(arg, &res);
|
|
|
|
if (!res)
|
|
|
|
goto wrong_usage;
|
2024-01-11 14:31:30 +00:00
|
|
|
for (paddr_t paddr = addr; paddr < addr + n;
|
2024-01-11 14:40:01 +00:00
|
|
|
paddr += WORD_BYTES) {
|
|
|
|
word_t value = paddr_read(addr, WORD_BYTES);
|
2024-01-11 14:41:21 +00:00
|
|
|
printf(FMT_PADDR FMT_WORD "\n", paddr, value);
|
2024-01-11 14:23:16 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
wrong_usage:
|
|
|
|
printf("Invalid argument for command si: %s\n", args);
|
|
|
|
printf("Usage: x [N: uint] [EXPR: <expr>]\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2023-12-20 16:20:36 +00:00
|
|
|
|
2024-01-11 12:11:42 +00:00
|
|
|
static int cmd_info(char *args) {
|
2024-01-11 14:23:16 +00:00
|
|
|
char *arg = strtok(NULL, " ");
|
2024-01-11 12:11:42 +00:00
|
|
|
int i;
|
2024-01-11 14:28:54 +00:00
|
|
|
if (arg == NULL) {
|
|
|
|
goto wrong_usage;
|
|
|
|
return 0;
|
|
|
|
}
|
2024-01-11 14:30:34 +00:00
|
|
|
for (i = 0; i < ARRLEN(cmd_info_table); i++) {
|
2024-01-11 12:11:42 +00:00
|
|
|
if (strcmp(arg, cmd_info_table[i].name) == 0) {
|
|
|
|
cmd_info_table[i].handler(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2024-01-11 14:28:54 +00:00
|
|
|
|
|
|
|
wrong_usage:
|
|
|
|
printf("Invalid argument for command info: %s\n", args);
|
|
|
|
printf("Usage: info [r | w]\n");
|
2024-01-11 12:11:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:49:42 +00:00
|
|
|
static int cmd_help_print(char *args, struct CMDTable *cur_cmd_table,
|
|
|
|
int cur_nr_cmd) {
|
2024-01-11 12:11:42 +00:00
|
|
|
int i;
|
2024-01-11 12:22:35 +00:00
|
|
|
char *arg = strtok(NULL, " ");
|
2024-01-11 12:11:42 +00:00
|
|
|
if (arg == NULL) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
2024-01-11 12:25:29 +00:00
|
|
|
for (i = 0; i < cur_nr_cmd; i++) {
|
2024-01-11 12:11:42 +00:00
|
|
|
if (strcmp(arg, cur_cmd_table[i].name) == 0) {
|
|
|
|
printf("%s ", cur_cmd_table[i].name);
|
2024-01-11 12:49:42 +00:00
|
|
|
if (cmd_help_print(arg, cur_cmd_table[i].subcommand,
|
|
|
|
cur_cmd_table[i].nr_subcommand) == -1) {
|
2024-01-11 12:12:07 +00:00
|
|
|
printf("-- %s\n", cur_cmd_table[i].description);
|
2024-01-11 12:11:42 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 16:20:36 +00:00
|
|
|
|
|
|
|
static int cmd_help(char *args) {
|
|
|
|
/* extract the first argument */
|
|
|
|
char *arg = strtok(NULL, " ");
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (arg == NULL) {
|
|
|
|
/* no argument given */
|
2024-01-11 12:11:42 +00:00
|
|
|
for (i = 0; i < NR_CMD; i++) {
|
2024-01-11 12:18:53 +00:00
|
|
|
printf("%s -- %s\n", cmd_table[i].name, cmd_table[i].description);
|
2023-12-20 16:20:36 +00:00
|
|
|
}
|
2024-01-11 12:11:42 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < NR_CMD; i++) {
|
2023-12-20 16:20:36 +00:00
|
|
|
if (strcmp(arg, cmd_table[i].name) == 0) {
|
2024-01-11 12:11:42 +00:00
|
|
|
printf("%s ", cmd_table[i].name);
|
2024-01-11 12:49:42 +00:00
|
|
|
if (cmd_help_print(args, cmd_table[i].subcommand,
|
|
|
|
cmd_table[i].nr_subcommand) == -1) {
|
2024-01-11 12:12:07 +00:00
|
|
|
printf("-- %s\n", cmd_table[i].description);
|
2024-01-11 12:28:18 +00:00
|
|
|
// Print available subcommands
|
|
|
|
for (int j = 0; j < cmd_table[i].nr_subcommand; j++) {
|
2024-01-11 12:30:13 +00:00
|
|
|
struct CMDTable *sub_cmd_table = cmd_table[i].subcommand;
|
2024-01-11 12:49:42 +00:00
|
|
|
printf(" > %s -- %s\n", sub_cmd_table[j].name,
|
|
|
|
sub_cmd_table[j].description);
|
2024-01-11 12:28:18 +00:00
|
|
|
}
|
2024-01-11 12:11:42 +00:00
|
|
|
}
|
2023-12-20 16:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Unknown command '%s'\n", arg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:11:42 +00:00
|
|
|
void sdb_set_batch_mode() { is_batch_mode = true; }
|
2023-12-20 16:20:36 +00:00
|
|
|
|
|
|
|
void sdb_mainloop() {
|
|
|
|
if (is_batch_mode) {
|
|
|
|
cmd_c(NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:11:42 +00:00
|
|
|
for (char *str; (str = rl_gets()) != NULL;) {
|
2023-12-20 16:20:36 +00:00
|
|
|
char *str_end = str + strlen(str);
|
|
|
|
|
|
|
|
/* extract the first token as the command */
|
|
|
|
char *cmd = strtok(str, " ");
|
2024-01-11 12:11:42 +00:00
|
|
|
if (cmd == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-20 16:20:36 +00:00
|
|
|
|
|
|
|
/* treat the remaining string as the arguments,
|
|
|
|
* which may need further parsing
|
|
|
|
*/
|
|
|
|
char *args = cmd + strlen(cmd) + 1;
|
|
|
|
if (args >= str_end) {
|
|
|
|
args = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEVICE
|
|
|
|
extern void sdl_clear_event_queue();
|
|
|
|
sdl_clear_event_queue();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int i;
|
2024-01-11 12:11:42 +00:00
|
|
|
for (i = 0; i < NR_CMD; i++) {
|
2023-12-20 16:20:36 +00:00
|
|
|
if (strcmp(cmd, cmd_table[i].name) == 0) {
|
2024-01-11 12:11:42 +00:00
|
|
|
if (cmd_table[i].handler(args) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-20 16:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:11:42 +00:00
|
|
|
if (i == NR_CMD) {
|
|
|
|
printf("Unknown command '%s'\n", cmd);
|
|
|
|
}
|
2023-12-20 16:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_sdb() {
|
|
|
|
/* Compile the regular expressions. */
|
|
|
|
init_regex();
|
|
|
|
|
|
|
|
/* Initialize the watchpoint pool. */
|
|
|
|
init_wp_pool();
|
|
|
|
}
|