diff --git a/Applications/Debugger/Makefile b/Applications/Debugger/Makefile new file mode 100755 index 0000000000..2b9e6d7c50 --- /dev/null +++ b/Applications/Debugger/Makefile @@ -0,0 +1,8 @@ +OBJS = \ + main.o + +PROGRAM = Debugger + +LIB_DEPS = Core + +include ../../Makefile.common diff --git a/Applications/Debugger/main.cpp b/Applications/Debugger/main.cpp new file mode 100644 index 0000000000..fd76270957 --- /dev/null +++ b/Applications/Debugger/main.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int usage() +{ + printf("usage: sdb [command...]\n"); + return 1; +} + +static int g_pid = -1; + +static void handle_sigint(int) +{ + if (g_pid == -1) + return; + + if (ptrace(PT_DETACH, g_pid, 0, 0) == -1) { + perror("detach"); + } +} + +void run_child_and_attach(char** argv) +{ + int pid = fork(); + + if (!pid) { + if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) { + perror("traceme"); + return exit(1); + } + + int rc = execvp(argv[1], &argv[1]); + if (rc < 0) { + perror("execvp"); + exit(1); + } + ASSERT_NOT_REACHED(); + } + + if (waitpid(pid, nullptr, WSTOPPED) != pid) { + perror("waitpid"); + exit(1); + } + g_pid = pid; +} + +int main(int argc, char** argv) +{ + if (argc == 1) + return usage(); + + run_child_and_attach(argv); + + struct sigaction sa; + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = handle_sigint; + sigaction(SIGINT, &sa, nullptr); + + if (ptrace(PT_ATTACH, g_pid, 0, 0) == -1) { + perror("attach"); + return 1; + } + if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) { + perror("waitpid"); + return 1; + } + + if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) { + perror("continue"); + } + + // wait for breakpoint + if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) { + perror("waitpid"); + return 1; + } + + printf("hit breakpoint\n"); + + sleep(1); + + if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) { + perror("continue"); + } + + if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) { + perror("waitpid"); + return 1; + } +} diff --git a/Demos/Debugee/Makefile b/Demos/Debugee/Makefile new file mode 100644 index 0000000000..2c67a05707 --- /dev/null +++ b/Demos/Debugee/Makefile @@ -0,0 +1,8 @@ +OBJS = \ + main.o + +PROGRAM = Debugee + +# LIB_DEPS = Core + +include ../../Makefile.common diff --git a/Demos/Debugee/main.cpp b/Demos/Debugee/main.cpp new file mode 100644 index 0000000000..bfad0ef26d --- /dev/null +++ b/Demos/Debugee/main.cpp @@ -0,0 +1,8 @@ +#include +int main(int, char**) +{ + printf("before breakpoint\n"); + asm("int3"); + printf("after breakpoint\n"); + return 0; +} diff --git a/Kernel/Arch/i386/CPU.cpp b/Kernel/Arch/i386/CPU.cpp index fca0685c91..460cb47243 100644 --- a/Kernel/Arch/i386/CPU.cpp +++ b/Kernel/Arch/i386/CPU.cpp @@ -297,6 +297,21 @@ void page_fault_handler(RegisterState regs) } } +EH_ENTRY_NO_CODE(3, breakpoint); +void breakpoint_handler(RegisterState regs) +{ + clac(); + if (!Process::current || Process::current->is_ring0()) { + klog() << "Breakpoint Trap in Ring0"; + hang(); + return; + } + if (Thread::current->tracer()) { + Thread::current->tracer()->set_regs(regs); + } + Thread::current->send_urgent_signal_to_self(SIGTRAP); +} + #define EH(i, msg) \ static void _exception##i() \ { \ @@ -316,7 +331,6 @@ void page_fault_handler(RegisterState regs) EH(1, "Debug exception") EH(2, "Unknown error") -EH(3, "Breakpoint") EH(4, "Overflow") EH(5, "Bounds check") EH(8, "Double fault") @@ -486,7 +500,7 @@ void idt_init() register_interrupt_handler(0x00, divide_error_asm_entry); register_interrupt_handler(0x01, _exception1); register_interrupt_handler(0x02, _exception2); - register_interrupt_handler(0x03, _exception3); + register_user_callable_interrupt_handler(0x03, breakpoint_asm_entry); register_interrupt_handler(0x04, _exception4); register_interrupt_handler(0x05, _exception5); register_interrupt_handler(0x06, illegal_instruction_asm_entry); diff --git a/Kernel/Thread.h b/Kernel/Thread.h index fcc016f4cf..19914ddfe3 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include diff --git a/Kernel/ThreadTracer.cpp b/Kernel/ThreadTracer.cpp index f59bf84d21..d7cd685a50 100644 --- a/Kernel/ThreadTracer.cpp +++ b/Kernel/ThreadTracer.cpp @@ -25,6 +25,7 @@ */ #include +#include #include namespace Kernel { diff --git a/Kernel/ThreadTracer.h b/Kernel/ThreadTracer.h index 5938203f02..fae20afcae 100644 --- a/Kernel/ThreadTracer.h +++ b/Kernel/ThreadTracer.h @@ -26,11 +26,8 @@ #pragma once -#include #include #include -#include -#include #include #include diff --git a/Kernel/build-root-filesystem.sh b/Kernel/build-root-filesystem.sh index 19f6bb3a13..ddc2ee86e3 100755 --- a/Kernel/build-root-filesystem.sh +++ b/Kernel/build-root-filesystem.sh @@ -143,8 +143,10 @@ cp ../Applications/DisplayProperties/DisplayProperties mnt/bin/DisplayProperties cp ../Applications/Welcome/Welcome mnt/bin/Welcome cp ../Applications/Help/Help mnt/bin/Help cp ../Applications/Browser/Browser mnt/bin/Browser +cp ../Applications/Debugger/Debugger mnt/bin/sdb cp ../Games/Solitaire/Solitaire mnt/bin/Solitaire cp ../Demos/HelloWorld/HelloWorld mnt/bin/HelloWorld +cp ../Demos/Debugee/Debugee mnt/bin/Debugee cp ../Demos/WidgetGallery/WidgetGallery mnt/bin/WidgetGallery cp ../Demos/Fire/Fire mnt/bin/Fire cp ../Demos/DynamicLink/LinkDemo/LinkDemo mnt/bin/LinkDemo diff --git a/Libraries/LibC/sys/arch/i386/regs.h b/Libraries/LibC/sys/arch/i386/regs.h index 21cdf64116..f367d13f28 100644 --- a/Libraries/LibC/sys/arch/i386/regs.h +++ b/Libraries/LibC/sys/arch/i386/regs.h @@ -25,25 +25,24 @@ */ #pragma once -#include -#include +#include struct [[gnu::packed]] PtraceRegisters { - uint32_t eax; - uint32_t ecx; - uint32_t edx; - uint32_t ebx; - uint32_t esp; - uint32_t ebp; - uint32_t esi; - uint32_t edi; - uint32_t eip; - uint32_t eflags; - uint32_t cs; - uint32_t ss; - uint32_t ds; - uint32_t es; - uint32_t fs; - uint32_t gs; + u32 eax; + u32 ecx; + u32 edx; + u32 ebx; + u32 esp; + u32 ebp; + u32 esi; + u32 edi; + u32 eip; + u32 eflags; + u32 cs; + u32 ss; + u32 ds; + u32 es; + u32 fs; + u32 gs; };