jak-project/common/cross_os_debug/xdbg.h
water111 0451a06d76
Set up the compiler to ptrace the runtime (#107)
* set up the compiler to ptrace the runtime

* clang format

* move debugger state to a separate Debugger class

* support registers and break and continue

* documentation and fix windows

* make listener part of compiler, not a separate library

* implement memory read and write

* fix for windows
2020-10-31 14:07:43 -04:00

97 lines
2.2 KiB
C++

/*!
* @file xdbg.h
* Debugging utility library. This hides the platform specific details of the debugger.
* Nothing in here should hold state, that should all be managed in Debugger.
*/
#pragma once
#include <string>
#include <cstdint>
#include "common/common_types.h"
#ifdef __linux
#include <sys/types.h>
#elif _WIN32
// todo - windows includes
#endif
namespace xdbg {
#ifdef __linux
struct ThreadID {
pid_t id = 0;
std::string to_string() const;
explicit ThreadID(const std::string& str);
explicit ThreadID(pid_t _id);
ThreadID() = default;
};
struct MemoryHandle {
int fd;
};
#elif _WIN32
struct ThreadID {
// todo - whatever windows uses to identify a thread
std::string to_string() const;
ThreadID(const std::string& str);
ThreadID(); // todo - add id type here, like in linux version
};
struct MemoryHandle {
int a;
};
#endif
struct DebugContext {
ThreadID tid;
uintptr_t base;
uint32_t s7;
};
struct Regs {
u64 gprs[16];
u128 xmms[16];
u64 rip;
std::string print_gprs() const;
std::string print_xmms_as_flt() const;
std::string print_xmms_as_int() const;
std::string print_xmms_as_flt_vec() const;
};
// Functions
ThreadID get_current_thread_id();
bool attach_and_break(const ThreadID& tid);
void allow_debugging();
bool detach_and_resume(const ThreadID& tid);
bool get_regs_now(const ThreadID& tid, Regs* out);
bool break_now(const ThreadID& tid);
bool cont_now(const ThreadID& tid);
bool open_memory(const ThreadID& tid, MemoryHandle* out);
bool close_memory(const ThreadID& tid, MemoryHandle* handle);
bool read_goal_memory(u8* dest_buffer,
int size,
u32 goal_addr,
const DebugContext& context,
const MemoryHandle& mem);
bool write_goal_memory(const u8* src_buffer,
int size,
u32 goal_addr,
const DebugContext& context,
const MemoryHandle& mem);
template <typename T>
bool write_goal_value(T& value,
u32 goal_addr,
const DebugContext& context,
const MemoryHandle& handle) {
return write_goal_memory(&value, sizeof(value), goal_addr, context, handle);
}
} // namespace xdbg