From 1642fdf82d986dcca09756290aa8cdb65cad373b Mon Sep 17 00:00:00 2001 From: Itamar Date: Thu, 16 Apr 2020 12:41:13 +0300 Subject: Debugger: Use LibLine --- Applications/Debugger/main.cpp | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'Applications/Debugger/main.cpp') diff --git a/Applications/Debugger/main.cpp b/Applications/Debugger/main.cpp index afe40166af..82f6d98bb7 100644 --- a/Applications/Debugger/main.cpp +++ b/Applications/Debugger/main.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,8 @@ #include #include +static Line::Editor editor {}; + static int usage() { printf("usage: sdb [command...]\n"); @@ -57,28 +60,6 @@ static void handle_sigint(int) g_debug_session = nullptr; } -String get_command() -{ - printf("(sdb) "); - fflush(stdout); - char* line = nullptr; - size_t allocated_size = 0; - ssize_t nread = getline(&line, &allocated_size, stdin); - if (nread < 0) { - if (errno == 0) { - fprintf(stderr, "\n"); - } else { - perror("getline"); - exit(1); - } - } - String command(line); - free(line); - if (command.ends_with('\n')) - command = command.substring(0, command.length() - 1); - return command; -} - void handle_print_registers(const PtraceRegisters& regs) { printf("eax: 0x%x\n", regs.eax); @@ -176,7 +157,7 @@ void print_help() int main(int argc, char** argv) { - if (pledge("stdio proc exec rpath", nullptr) < 0) { + if (pledge("stdio proc exec rpath tty", nullptr) < 0) { perror("pledge"); return 1; } @@ -184,6 +165,8 @@ int main(int argc, char** argv) if (argc == 1) return usage(); + editor.initialize(); + StringBuilder command; command.append(argv[1]); for (int i = 2; i < argc; ++i) { @@ -205,8 +188,6 @@ int main(int argc, char** argv) bool rc = g_debug_session->insert_breakpoint(g_debug_session->elf().entry().as_ptr()); ASSERT(rc); - String previous_command; - g_debug_session->run([&](DebugSession::DebugBreakReason reason, Optional optional_regs) { if (reason == DebugSession::DebugBreakReason::Exited) { printf("Program exited.\n"); @@ -219,12 +200,12 @@ int main(int argc, char** argv) auto symbol_at_ip = g_debug_session->elf().symbolicate(regs.eip); printf("Program is stopped at: 0x%x (%s)\n", regs.eip, symbol_at_ip.characters()); for (;;) { - auto command = get_command(); + auto command = editor.get_line("(sdb) "); bool success = false; Optional decision; - if (command.is_empty() && !previous_command.is_empty()) { - command = previous_command; + if (command.is_empty() && !editor.history().is_empty()) { + command = editor.history().last(); } if (command == "cont") { decision = DebugSession::DebugDecision::Continue; @@ -247,7 +228,9 @@ int main(int argc, char** argv) } if (success && !command.is_empty()) { - previous_command = command; + // Don't add repeated commands to history + if (editor.history().is_empty() || editor.history().last() != command) + editor.add_to_history(command); } if (!success) { print_help(); -- cgit v1.2.3