
221220000 张三 Linux zzy 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux 20:14:34 up 10:54, 2 users, load average: 0.37, 0.15, 0.05
215 lines
5.2 KiB
C
215 lines
5.2 KiB
C
/***************************************************************************************
|
|
* 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.
|
|
***************************************************************************************/
|
|
|
|
#include <isa.h>
|
|
#include <cpu/cpu.h>
|
|
#include <readline/readline.h>
|
|
#include <readline/history.h>
|
|
#include <memory/vaddr.h>
|
|
#include "sdb.h"
|
|
|
|
static int is_batch_mode = false;
|
|
|
|
void init_regex();
|
|
void init_wp_pool();
|
|
|
|
/* We use the `readline' library to provide more flexibility to read from stdin. */
|
|
static char* rl_gets() {
|
|
static char *line_read = NULL;
|
|
|
|
if (line_read) {
|
|
free(line_read);
|
|
line_read = NULL;
|
|
}
|
|
|
|
line_read = readline("(nemu) ");
|
|
|
|
if (line_read && *line_read) {
|
|
add_history(line_read);
|
|
}
|
|
|
|
return line_read;
|
|
}
|
|
|
|
/* print the help message, when name is NULL will print all of it */
|
|
static bool p_help(char *name);
|
|
|
|
static int cmd_c(char *args) {
|
|
cpu_exec(UINT64_MAX);
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_q(char *args) {
|
|
set_nemu_state(NEMU_END, 0, 0);
|
|
return -1;
|
|
}
|
|
|
|
static int cmd_help(char *args);
|
|
|
|
static int cmd_si(char *args) {
|
|
/* extract the first argument */
|
|
char *arg = strtok(NULL, " ");
|
|
if (arg == NULL) {
|
|
arg = "1";
|
|
}
|
|
cpu_exec(atoi(arg));
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_info(char *args) {
|
|
char *arg = strtok(NULL, " ");
|
|
if (arg[0] == 'r') {
|
|
isa_reg_display();
|
|
} else if (arg[0] == 'w') {
|
|
printf("Watchpoint:\n");
|
|
} else {
|
|
p_help("info");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_x(char *args) {
|
|
char *arg0 = strtok(NULL, " ");
|
|
char *arg1 = strtok(NULL, " ");
|
|
if (arg0 == NULL || arg1 == NULL) {
|
|
goto END;
|
|
}
|
|
|
|
vaddr_t addr = 0;
|
|
if (!sscanf(arg1, FMT_PADDR, &addr)) {
|
|
goto END;
|
|
}
|
|
|
|
size_t N = atoi(arg0);
|
|
if (N <= 0) N = 1;
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
printf(FMT_PADDR": "FMT_WORD"\n", addr, vaddr_read(addr, 4));
|
|
addr += 4;
|
|
}
|
|
return 0;
|
|
END:
|
|
p_help("x");
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_p(char *args) {
|
|
bool success = false;
|
|
expr(args, &success);
|
|
return success;
|
|
}
|
|
|
|
static int cmd_w(char *args) { return 0; }
|
|
|
|
static int cmd_d(char *args) { return 0; }
|
|
|
|
static struct {
|
|
const char *name;
|
|
const char *description;
|
|
int (*handler) (char *);
|
|
} cmd_table [] = {
|
|
{ "help", "Display information about all supported commands", cmd_help },
|
|
{ "c", "Continue the execution of the program", cmd_c },
|
|
{ "q", "Exit NEMU", cmd_q },
|
|
{ "si", "si [N]: To make a program pause after executing N (default 1) instructions", cmd_si },
|
|
{ "info", "info r: Display registers stats, info w: Display watch points value", cmd_info },
|
|
{ "x", "x N EXPR: To evaluate an expression EXPR and output the result as "\
|
|
"a starting memory address in hexadecimal format, "\
|
|
"followed by the next N consecutive 4-byte addresses in hexadecimal form", cmd_x },
|
|
{ "p", "p EXPR: To evaluate an expression EXPR and output the result", cmd_p },
|
|
{ "w", "w EXPR: To pause the program when the value of an expression EXPR changes, "\
|
|
"you can create a monitoring loop that checks the value of EXPR at regular intervals. "\
|
|
"When the value changes, the program will pause.", cmd_w },
|
|
{ "d", "d N: To remove a watchpoint with a specific index N", cmd_d },
|
|
/* TODO: Add more commands */
|
|
|
|
};
|
|
|
|
#define NR_CMD ARRLEN(cmd_table)
|
|
|
|
static bool p_help(char *name) {
|
|
for (int i = 0; i < NR_CMD; i ++) {
|
|
if (name == NULL) {
|
|
printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
|
|
continue;
|
|
}
|
|
if (strcmp(name, cmd_table[i].name) == 0) {
|
|
printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
|
|
return true;
|
|
}
|
|
}
|
|
return true ? name == NULL : false;
|
|
}
|
|
|
|
static int cmd_help(char *args) {
|
|
/* extract the first argument */
|
|
char *arg = strtok(NULL, " ");
|
|
|
|
if (p_help(arg) == false) {
|
|
printf("Unknown command '%s'\n", arg);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void sdb_set_batch_mode() {
|
|
is_batch_mode = true;
|
|
}
|
|
|
|
void sdb_mainloop() {
|
|
if (is_batch_mode) {
|
|
cmd_c(NULL);
|
|
return;
|
|
}
|
|
|
|
for (char *str; (str = rl_gets()) != NULL; ) {
|
|
char *str_end = str + strlen(str);
|
|
|
|
/* extract the first token as the command */
|
|
char *cmd = strtok(str, " ");
|
|
if (cmd == NULL) { continue; }
|
|
|
|
/* 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;
|
|
for (i = 0; i < NR_CMD; i ++) {
|
|
if (strcmp(cmd, cmd_table[i].name) == 0) {
|
|
if (cmd_table[i].handler(args) < 0) { return; }
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (i == NR_CMD) { printf("Unknown command '%s'\n", cmd); }
|
|
}
|
|
}
|
|
|
|
void init_sdb() {
|
|
/* Compile the regular expressions. */
|
|
init_regex();
|
|
|
|
/* Initialize the watchpoint pool. */
|
|
init_wp_pool();
|
|
}
|