summaryrefslogtreecommitdiff
path: root/Applications/Debugger/main.cpp
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2020-04-16 12:41:13 +0300
committerAndreas Kling <kling@serenityos.org>2020-04-16 12:22:59 +0200
commit1642fdf82d986dcca09756290aa8cdb65cad373b (patch)
treecbf798c10d5f09ad6cf79dedd76a06dd0e9bf08a /Applications/Debugger/main.cpp
parentef6dc6129135bd1548dedb0a35db12f27baf79ba (diff)
downloadserenity-1642fdf82d986dcca09756290aa8cdb65cad373b.zip
Debugger: Use LibLine
Diffstat (limited to 'Applications/Debugger/main.cpp')
-rw-r--r--Applications/Debugger/main.cpp41
1 files changed, 12 insertions, 29 deletions
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 <LibC/sys/arch/i386/regs.h>
#include <LibCore/File.h>
#include <LibDebug/DebugSession.h>
+#include <LibLine/Editor.h>
#include <LibX86/Disassembler.h>
#include <LibX86/Instruction.h>
#include <signal.h>
@@ -41,6 +42,8 @@
#include <string.h>
#include <unistd.h>
+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<PtraceRegisters> 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<DebugSession::DebugDecision> 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();