2020-04-12 18:45:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-12 18:45:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
2022-05-01 20:57:24 +00:00
|
|
|
#include <Kernel/API/SyscallString.h>
|
2020-05-23 19:10:45 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-12-13 19:50:51 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-04-12 18:45:04 +00:00
|
|
|
#include <LibDebug/DebugSession.h>
|
|
|
|
#include <LibELF/Image.h>
|
2021-12-13 19:50:51 +00:00
|
|
|
#include <LibMain/Main.h>
|
2020-04-12 18:45:04 +00:00
|
|
|
#include <LibX86/Disassembler.h>
|
|
|
|
#include <LibX86/Instruction.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-01-03 14:30:44 +00:00
|
|
|
#include <sys/arch/regs.h>
|
2021-02-05 11:16:30 +00:00
|
|
|
#include <syscall.h>
|
2020-04-12 18:45:04 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-08-25 03:33:07 +00:00
|
|
|
static OwnPtr<Debug::DebugSession> g_debug_session;
|
2020-06-08 07:19:30 +00:00
|
|
|
static bool g_should_output_color = false;
|
2020-04-12 18:45:04 +00:00
|
|
|
|
|
|
|
static void handle_sigint(int)
|
|
|
|
{
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("Debugger: SIGINT");
|
2020-04-12 18:45:04 +00:00
|
|
|
|
|
|
|
// The destructor of DebugSession takes care of detaching
|
|
|
|
g_debug_session = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
static void print_function_call(ByteString function_name, size_t depth)
|
2020-04-12 18:45:04 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < depth; ++i) {
|
2020-11-09 09:47:19 +00:00
|
|
|
out(" ");
|
2020-04-12 18:45:04 +00:00
|
|
|
}
|
2020-10-20 15:28:17 +00:00
|
|
|
outln("=> {}", function_name);
|
2020-04-12 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static void print_syscall(PtraceRegisters& regs, size_t depth)
|
2020-04-15 18:10:51 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < depth; ++i) {
|
2021-05-31 14:43:25 +00:00
|
|
|
out(" ");
|
2020-04-15 18:10:51 +00:00
|
|
|
}
|
2021-12-13 19:50:51 +00:00
|
|
|
StringView begin_color = g_should_output_color ? "\033[34;1m"sv : ""sv;
|
|
|
|
StringView end_color = g_should_output_color ? "\033[0m"sv : ""sv;
|
2022-10-03 23:51:58 +00:00
|
|
|
#if ARCH(X86_64)
|
2021-07-21 23:30:24 +00:00
|
|
|
outln("=> {}SC_{}({:#x}, {:#x}, {:#x}){}",
|
2021-06-30 13:29:13 +00:00
|
|
|
begin_color,
|
|
|
|
Syscall::to_string((Syscall::Function)regs.rax),
|
|
|
|
regs.rdx,
|
|
|
|
regs.rcx,
|
|
|
|
regs.rbx,
|
|
|
|
end_color);
|
2022-10-12 19:55:05 +00:00
|
|
|
#elif ARCH(AARCH64)
|
|
|
|
(void)regs;
|
|
|
|
(void)begin_color;
|
|
|
|
(void)end_color;
|
|
|
|
TODO_AARCH64();
|
2023-09-18 10:56:39 +00:00
|
|
|
#elif ARCH(RISCV64)
|
|
|
|
(void)regs;
|
|
|
|
(void)begin_color;
|
|
|
|
(void)end_color;
|
|
|
|
TODO_RISCV64();
|
2022-07-22 18:48:24 +00:00
|
|
|
#else
|
|
|
|
# error Unknown architecture
|
2021-06-30 13:29:13 +00:00
|
|
|
#endif
|
2020-04-15 18:10:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 01:16:27 +00:00
|
|
|
static NonnullOwnPtr<HashMap<FlatPtr, X86::Instruction>> instrument_code()
|
2020-04-12 18:45:04 +00:00
|
|
|
{
|
2022-01-27 01:16:27 +00:00
|
|
|
auto instrumented = make<HashMap<FlatPtr, X86::Instruction>>();
|
2022-04-01 17:58:27 +00:00
|
|
|
g_debug_session->for_each_loaded_library([&](Debug::LoadedLibrary const& lib) {
|
2021-01-06 19:49:25 +00:00
|
|
|
lib.debug_info->elf().for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
|
|
|
|
if (section.name() != ".text")
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
2021-05-01 22:00:52 +00:00
|
|
|
X86::SimpleInstructionStream stream((const u8*)((uintptr_t)lib.file->data() + section.offset()), section.size());
|
2021-01-06 19:49:25 +00:00
|
|
|
X86::Disassembler disassembler(stream);
|
|
|
|
for (;;) {
|
|
|
|
auto offset = stream.offset();
|
2022-01-27 01:16:27 +00:00
|
|
|
auto instruction_address = section.address() + offset + lib.base_address;
|
2021-01-06 19:49:25 +00:00
|
|
|
auto insn = disassembler.next();
|
|
|
|
if (!insn.has_value())
|
|
|
|
break;
|
|
|
|
if (insn.value().mnemonic() == "RET" || insn.value().mnemonic() == "CALL") {
|
|
|
|
g_debug_session->insert_breakpoint(instruction_address);
|
|
|
|
instrumented->set(instruction_address, insn.value());
|
|
|
|
}
|
2020-04-12 18:45:04 +00:00
|
|
|
}
|
2021-01-06 19:49:25 +00:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2020-04-12 18:45:04 +00:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return instrumented;
|
|
|
|
}
|
|
|
|
|
2021-12-13 19:50:51 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-04-12 18:45:04 +00:00
|
|
|
{
|
2021-12-13 19:50:51 +00:00
|
|
|
TRY(Core::System::pledge("stdio proc exec rpath sigaction ptrace"));
|
2020-04-12 18:45:04 +00:00
|
|
|
|
2020-06-08 07:19:30 +00:00
|
|
|
if (isatty(STDOUT_FILENO))
|
|
|
|
g_should_output_color = true;
|
|
|
|
|
2021-12-13 19:50:51 +00:00
|
|
|
StringView command;
|
2020-05-23 19:10:45 +00:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(command,
|
|
|
|
"The program to be traced, along with its arguments",
|
|
|
|
"program", Core::ArgsParser::Required::Yes);
|
2021-12-13 19:50:51 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-04-12 18:45:04 +00:00
|
|
|
|
2020-08-25 03:33:07 +00:00
|
|
|
auto result = Debug::DebugSession::exec_and_attach(command);
|
2020-04-12 18:45:04 +00:00
|
|
|
if (!result) {
|
2020-10-20 15:28:17 +00:00
|
|
|
warnln("Failed to start debugging session for: \"{}\"", command);
|
2020-04-12 18:45:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
g_debug_session = result.release_nonnull();
|
|
|
|
|
|
|
|
auto instrumented = instrument_code();
|
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(struct sigaction));
|
|
|
|
sa.sa_handler = handle_sigint;
|
2021-12-13 19:50:51 +00:00
|
|
|
TRY(Core::System::sigaction(SIGINT, &sa, nullptr));
|
2020-04-12 18:45:04 +00:00
|
|
|
|
|
|
|
size_t depth = 0;
|
|
|
|
bool new_function = true;
|
|
|
|
|
2021-01-06 19:49:25 +00:00
|
|
|
g_debug_session->run(Debug::DebugSession::DesiredInitialDebugeeState::Running, [&](Debug::DebugSession::DebugBreakReason reason, Optional<PtraceRegisters> regs) {
|
2020-08-25 03:33:07 +00:00
|
|
|
if (reason == Debug::DebugSession::DebugBreakReason::Exited) {
|
2020-10-20 15:28:17 +00:00
|
|
|
outln("Program exited.");
|
2020-08-25 03:33:07 +00:00
|
|
|
return Debug::DebugSession::DebugDecision::Detach;
|
2020-04-12 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:33:07 +00:00
|
|
|
if (reason == Debug::DebugSession::DebugBreakReason::Syscall) {
|
2020-04-15 18:10:51 +00:00
|
|
|
print_syscall(regs.value(), depth + 1);
|
2020-08-25 03:33:07 +00:00
|
|
|
return Debug::DebugSession::DebugDecision::ContinueBreakAtSyscall;
|
2020-04-15 18:10:51 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 23:51:58 +00:00
|
|
|
#if ARCH(X86_64)
|
2021-06-30 13:29:13 +00:00
|
|
|
const FlatPtr ip = regs.value().rip;
|
2022-10-12 19:55:05 +00:00
|
|
|
#elif ARCH(AARCH64)
|
|
|
|
const FlatPtr ip = 0; // FIXME
|
|
|
|
TODO_AARCH64();
|
2023-09-18 10:56:39 +00:00
|
|
|
#elif ARCH(RISCV64)
|
|
|
|
const FlatPtr ip = 0; // FIXME
|
|
|
|
TODO_RISCV64();
|
2022-07-22 18:48:24 +00:00
|
|
|
#else
|
|
|
|
# error Unknown architecture
|
2021-06-30 13:29:13 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-12 18:45:04 +00:00
|
|
|
if (new_function) {
|
2021-06-30 13:29:13 +00:00
|
|
|
auto function_name = g_debug_session->symbolicate(ip);
|
2021-01-06 19:49:25 +00:00
|
|
|
print_function_call(function_name.value().symbol, depth);
|
2020-04-12 18:45:04 +00:00
|
|
|
new_function = false;
|
2020-08-25 03:33:07 +00:00
|
|
|
return Debug::DebugSession::ContinueBreakAtSyscall;
|
2020-04-12 18:45:04 +00:00
|
|
|
}
|
2022-01-27 01:16:27 +00:00
|
|
|
auto instruction = instrumented->get(ip).value();
|
2020-04-12 18:45:04 +00:00
|
|
|
|
|
|
|
if (instruction.mnemonic() == "RET") {
|
|
|
|
if (depth != 0)
|
|
|
|
--depth;
|
2020-08-25 03:33:07 +00:00
|
|
|
return Debug::DebugSession::ContinueBreakAtSyscall;
|
2020-04-12 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: we could miss some leaf functions that were called with a jump
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(instruction.mnemonic() == "CALL");
|
2020-04-12 18:45:04 +00:00
|
|
|
|
|
|
|
++depth;
|
|
|
|
new_function = true;
|
|
|
|
|
2020-08-25 03:33:07 +00:00
|
|
|
return Debug::DebugSession::DebugDecision::SingleStep;
|
2020-04-12 18:45:04 +00:00
|
|
|
});
|
2021-12-13 19:50:51 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-04-12 18:45:04 +00:00
|
|
|
}
|