|
| 1 | +/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 | +/* Copyright © 2016-2024 Byteduck */ |
| 3 | + |
| 4 | +#include "LiveDebugger.h" |
| 5 | +#include <sys/ptrace.h> |
| 6 | +#include <fcntl.h> |
| 7 | +#include <sys/mman.h> |
| 8 | +#include <libduck/MappedBuffer.h> |
| 9 | + |
| 10 | +using namespace Debug; |
| 11 | +using Duck::Result, Duck::ResultRet; |
| 12 | + |
| 13 | +Result LiveDebugger::attach(pid_t pid, tid_t tid) { |
| 14 | + if (ptrace(PTRACE_ATTACH, tid, nullptr, nullptr) < 0) |
| 15 | + return Result(errno); |
| 16 | + m_pid = pid; |
| 17 | + m_tid = tid; |
| 18 | + TRYRES(reload_process_info()); |
| 19 | + return Result::SUCCESS; |
| 20 | +} |
| 21 | + |
| 22 | +ResultRet<uintptr_t> Debug::LiveDebugger::peek(size_t addr) { |
| 23 | + if (!m_pid || !m_tid) |
| 24 | + return Result("Not attached"); |
| 25 | + uintptr_t data; |
| 26 | + if (ptrace(PTRACE_PEEK, m_tid, (void*) addr, &data) < 0) |
| 27 | + return Result(errno); |
| 28 | + return data; |
| 29 | +} |
| 30 | + |
| 31 | +Result LiveDebugger::poke(size_t addr, uintptr_t val) { |
| 32 | + if (!m_pid || !m_tid) |
| 33 | + return Result("Not attached"); |
| 34 | + if (ptrace(PTRACE_POKE, m_tid, (void*) addr, (void*) val) < 0) |
| 35 | + return Result(errno); |
| 36 | + return Result::SUCCESS; |
| 37 | +} |
| 38 | + |
| 39 | +ResultRet<Sys::Process::MemoryRegion> LiveDebugger::region_at(size_t addr) { |
| 40 | + for (auto& region : m_memory_regions) { |
| 41 | + if (region.start + region.size > addr) { |
| 42 | + // Assume they're in order |
| 43 | + if (region.start > addr) |
| 44 | + return Result("No such memory region"); |
| 45 | + return region; |
| 46 | + } |
| 47 | + } |
| 48 | + return Result("No such memory region"); |
| 49 | +} |
| 50 | + |
| 51 | +ResultRet<Exec::Object*> LiveDebugger::object_at(size_t addr) { |
| 52 | + auto reg = TRY(region_at(addr)); |
| 53 | + if (reg.type != Sys::Process::MemoryRegion::Inode) |
| 54 | + return Result("Region not an object"); |
| 55 | + |
| 56 | + auto loadedobj = m_objects.find(reg.name); |
| 57 | + if (loadedobj != m_objects.end()) { |
| 58 | + auto obj = loadedobj->second.get(); |
| 59 | + if (!obj) |
| 60 | + return Result("No object at region"); // Previously failed to load |
| 61 | + return obj; |
| 62 | + } |
| 63 | + |
| 64 | + auto do_load = [&] () -> ResultRet<Duck::Ptr<Exec::Object>> { |
| 65 | + auto objfile = TRY(Duck::File::open(reg.name, "r")); |
| 66 | + auto mappedfile = TRY(Duck::MappedBuffer::make_file(objfile, Duck::MappedBuffer::R, Duck::MappedBuffer::SharedFile)); |
| 67 | + auto obj = std::make_shared<Exec::Object>(); |
| 68 | + obj->fd = objfile.fd(); |
| 69 | + obj->mapped_file = mappedfile->data<uint8_t>(); |
| 70 | + obj->mapped_size = mappedfile->size(); |
| 71 | + obj->memloc = reg.start; |
| 72 | + obj->fd = objfile.fd(); |
| 73 | + obj->name = Duck::Path(reg.name).basename(); |
| 74 | + TRYRES(obj->load_for_debugger()); |
| 75 | + objfile.set_close_on_destroy(false); |
| 76 | + mappedfile->set_unmap_on_destroy(false); |
| 77 | + return obj; |
| 78 | + }; |
| 79 | + |
| 80 | + auto load_res = do_load(); |
| 81 | + if (load_res.is_error()) { |
| 82 | + m_objects[reg.name] = nullptr; |
| 83 | + return load_res.result(); |
| 84 | + } |
| 85 | + m_objects[reg.name] = load_res.value(); |
| 86 | + return load_res.value().get(); |
| 87 | +} |
| 88 | + |
| 89 | +Duck::ResultRet<PTraceRegisters> LiveDebugger::get_registers() { |
| 90 | + if (!m_pid || !m_tid) |
| 91 | + return Result("Not attached"); |
| 92 | + PTraceRegisters ret; |
| 93 | + if (ptrace(PTRACE_GETREGS, m_tid, nullptr, &ret) < 0) |
| 94 | + return Result(errno); |
| 95 | + return ret; |
| 96 | +} |
| 97 | + |
| 98 | +Result LiveDebugger::reload_process_info() { |
| 99 | + m_process = TRY(Sys::Process::get(m_pid)); |
| 100 | + m_memory_regions = TRY(m_process.memory_regions()); |
| 101 | + return Result::SUCCESS; |
| 102 | +} |
0 commit comments