2024-04-09 09:03:21 +00:00
|
|
|
#ifndef _SDB_SDB_HEADER_FILE_
|
|
|
|
#define _SDB_SDB_HEADER_FILE_
|
|
|
|
|
|
|
|
#include <components.hpp>
|
|
|
|
#include <console.hpp>
|
|
|
|
#include <memory>
|
|
|
|
#include <stdexcept>
|
2024-04-11 03:44:54 +00:00
|
|
|
#include <string>
|
2024-04-10 12:12:41 +00:00
|
|
|
#include <trm_interface.hpp>
|
2024-04-09 09:03:21 +00:00
|
|
|
#include <types.h>
|
|
|
|
|
|
|
|
namespace cr = CppReadline;
|
|
|
|
using ret = cr::Console::ReturnCode;
|
|
|
|
|
|
|
|
namespace SDB {
|
|
|
|
|
2024-04-11 03:44:54 +00:00
|
|
|
enum SDBStatus {
|
|
|
|
SDB_SUCCESS,
|
|
|
|
SDB_EBREAK = 2,
|
|
|
|
SDB_WRONG_ARGUMENT,
|
|
|
|
SDB_DIFFTEST_FAILED
|
|
|
|
};
|
2024-04-10 12:12:41 +00:00
|
|
|
|
|
|
|
class SDBHandlers;
|
2024-04-09 09:03:21 +00:00
|
|
|
|
|
|
|
struct Handler {
|
|
|
|
const std::vector<const char *> names;
|
2024-04-10 12:12:41 +00:00
|
|
|
// cr::Console::CommandFunction f;
|
|
|
|
std::function<int(SDBHandlers *, const cr::Console::Arguments &)> f;
|
2024-04-09 09:03:21 +00:00
|
|
|
};
|
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
class SDBHandlers {
|
2024-04-09 09:03:21 +00:00
|
|
|
private:
|
2024-04-10 12:12:41 +00:00
|
|
|
const TrmInterface &funcs;
|
|
|
|
std::vector<Handler> all_handlers = {
|
|
|
|
Handler{{"c", "continue"}, &SDBHandlers::cmd_continue},
|
|
|
|
Handler{{"si", "step-instruction"}, &SDBHandlers::cmd_step},
|
|
|
|
Handler{{"info-r"}, &SDBHandlers::cmd_info_registers},
|
|
|
|
Handler{{"p", "print"}, &SDBHandlers::cmd_print},
|
2024-04-12 01:35:41 +00:00
|
|
|
Handler{{"disas", "disassemble"}, &SDBHandlers::cmd_disassemble},
|
2024-04-10 12:12:41 +00:00
|
|
|
};
|
|
|
|
int cmd_continue(const cr::Console::Arguments &input);
|
|
|
|
int cmd_step(const std::vector<std::string> &input);
|
|
|
|
int cmd_info_registers(const std::vector<std::string> &input);
|
|
|
|
int cmd_print(const std::vector<std::string> &input);
|
2024-04-12 01:35:41 +00:00
|
|
|
int cmd_disassemble(const std::vector<std::string> &input);
|
2024-04-11 03:44:54 +00:00
|
|
|
int exec_catch(uint64_t);
|
2024-04-09 09:03:21 +00:00
|
|
|
|
|
|
|
public:
|
2024-04-10 12:12:41 +00:00
|
|
|
SDBHandlers(const TrmInterface &funcs) : funcs(funcs){};
|
2024-04-11 03:44:54 +00:00
|
|
|
void register_handlers(cr::Console *c);
|
2024-04-09 09:03:21 +00:00
|
|
|
};
|
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
class SDB {
|
2024-04-09 09:03:21 +00:00
|
|
|
private:
|
|
|
|
std::unique_ptr<CppReadline::Console> c;
|
2024-04-10 12:12:41 +00:00
|
|
|
const TrmInterface &funcs;
|
|
|
|
SDBHandlers handlers;
|
2024-04-09 09:03:21 +00:00
|
|
|
|
|
|
|
public:
|
2024-04-10 12:12:41 +00:00
|
|
|
SDB(const TrmInterface &funcs,
|
|
|
|
std::string const &greeting = "\033[1;34m(npc)\033[0m ")
|
|
|
|
: handlers(SDBHandlers{funcs}), funcs(funcs) {
|
2024-04-09 09:03:21 +00:00
|
|
|
c = std::make_unique<CppReadline::Console>(greeting);
|
|
|
|
|
2024-04-11 03:44:54 +00:00
|
|
|
handlers.register_handlers(c.get());
|
2024-04-10 12:12:41 +00:00
|
|
|
};
|
2024-04-09 09:03:21 +00:00
|
|
|
|
|
|
|
int main_loop() {
|
2024-04-11 03:44:54 +00:00
|
|
|
int ret_code;
|
2024-04-10 12:12:41 +00:00
|
|
|
funcs.init(0);
|
2024-04-11 03:44:54 +00:00
|
|
|
std::vector<std::string> step_commands{"c", "continue", "si",
|
|
|
|
"step-instruction"};
|
2024-04-09 09:03:21 +00:00
|
|
|
do {
|
2024-04-11 03:44:54 +00:00
|
|
|
ret_code = c->readLine();
|
2024-04-09 09:03:21 +00:00
|
|
|
// We can also change the prompt based on last return value:
|
2024-04-11 03:44:54 +00:00
|
|
|
if (ret_code == SDB_SUCCESS || ret_code == SDB_EBREAK)
|
2024-04-09 09:03:21 +00:00
|
|
|
c->setGreeting("\033[1;34m(npc)\033[0m ");
|
|
|
|
else
|
|
|
|
c->setGreeting("\033[1;31m(npc)\033[0m ");
|
|
|
|
|
2024-04-11 03:44:54 +00:00
|
|
|
switch (ret_code) {
|
|
|
|
case SDB_EBREAK: {
|
|
|
|
std::cout << "\033[1;31m=== ebreak ===\033[0m" << std::endl;
|
|
|
|
c->removeCommands(step_commands);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDB_WRONG_ARGUMENT: {
|
|
|
|
std::cout << "Wrong argument(s) is given to command handler\n";
|
|
|
|
break;
|
|
|
|
}
|
2024-04-09 09:03:21 +00:00
|
|
|
}
|
2024-04-11 03:44:54 +00:00
|
|
|
} while (ret_code != ret::Quit);
|
2024-04-09 09:03:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace SDB
|
|
|
|
|
|
|
|
#endif
|