diff --git a/include/libkdb/bit.hpp b/include/libkdb/bit.hpp new file mode 100644 index 0000000..2c3e31c --- /dev/null +++ b/include/libkdb/bit.hpp @@ -0,0 +1,31 @@ +#pragma once +#include + +namespace kdb { + template T from_bytes(std::byte *bytes) { + T res; + std::memcpy(&res, bytes, sizeof(T)); + return res; + } + + template std::byte *as_bytes(T &t) { + return std::static_cast(static_cast(t)); + } + + template const std::byte *as_bytes(const T &t) { + return std::static_cast(std::static_cast(t)); + } + + template byte128 to_byte128(T src) { + byte128 res{}; + std::memcpy(&res, src, sizeof(T)); + return res; + } + + template byte64 to_byte64(T src) { + byte64 res{}; + std::memcpy(&res, src, sizeof(T)); + return res; + } + +} // namespace kdb \ No newline at end of file diff --git a/include/libkdb/error.hpp b/include/libkdb/error.hpp index c5c886a..0396406 100644 --- a/include/libkdb/error.hpp +++ b/include/libkdb/error.hpp @@ -15,7 +15,6 @@ namespace kdb { [[noexcept]] static void send_error_no(const std::string &prefix) { throw std::runtime_error(prefix + ": " + std::strerror(errno)); } - }; } // namespace kdb diff --git a/include/libkdb/ksyscall.cpp b/include/libkdb/ksyscall.cpp index 0e11521..8a6ed52 100644 --- a/include/libkdb/ksyscall.cpp +++ b/include/libkdb/ksyscall.cpp @@ -1,14 +1,17 @@ -#include -#include +#include "ksyscall.hpp" #include +#include #include -#include -#include "ksyscall.cpp" +namespace kdb { -namespace kdb{ - - - - -}//namespace kdb \ No newline at end of file + void ktrace(ptrace_args &args) { +#ifdef __linux__ + args.result = ptrace(args.req, args.pid, args.args.addr, args.data); + +#elif defined(__APPLE__) + args.result = ptrace(args.req, args.pid, args.addr, args.data); +#endif + } + +} // namespace kdb \ No newline at end of file diff --git a/include/libkdb/ksyscall.hpp b/include/libkdb/ksyscall.hpp index ca6010d..f422b7c 100644 --- a/include/libkdb/ksyscall.hpp +++ b/include/libkdb/ksyscall.hpp @@ -1,46 +1,45 @@ #pragma once #include -/* - * This library creates an abstraction for the syscalls. - * Usage should be simple, just call the abstracted syscall and it will figure - * out based on OS which one to call. - */ +namespace kdb { + /* + * This library creates an abstraction for the syscalls. + * Usage should be simple, just call the abstracted syscall and it will figure + * out based on OS which one to call. + */ #ifdef __linux__ -/* - * long ptrace(enum __ptrace_request op, pid_t pid, - void *addr, void *data); - */ -struct ptrace_args { - enum __ptrace__req; - pid_t pid; - void *addr; - void *data; - long result; -}; + /* + * long ptrace(enum __ptrace_request op, pid_t pid, + void *addr, void *data); + */ + struct ptrace_args { + enum __ptrace__req; + pid_t pid; + void *addr; + void *data; + long result; + }; #elif defined(__APPLE__) -/* - *int - ptrace(int request, pid_t pid, caddr_t addr, int data); - */ -struct ptrace_args { - int req; - pid_t pid; - caddr_t addr; - int data; - int result; -}; + /* + *int + ptrace(int request, pid_t pid, caddr_t addr, int data); + */ + struct ptrace_args { + int req; + pid_t pid; + caddr_t addr; + int data; + int result; + }; #endif -void ktrace(ptrace_args &args) { -#ifdef __linux__ - args.result = ptrace(args.req, args.pid, args.args.addr, args.data); - -#elif defined(__APPLE__) - args.result = ptrace(args.req, args.pid, args.addr, args.data); - #endif -} + struct pip_args { + inn + } + void + ptrace(ptrace_args &args); +} // namespace kdb diff --git a/include/libkdb/libkdb.hpp b/include/libkdb/libkdb.hpp index 7cf55ea..f98a39b 100644 --- a/include/libkdb/libkdb.hpp +++ b/include/libkdb/libkdb.hpp @@ -1,10 +1,10 @@ -#ifndef KDB_LIBKDB_HPP +#ifndef KDB_LIBKDB_HPP #define KDB_LIBKDB_HPP -namespace kdb{ - - void say_hello(); - -}// namespace kdb +namespace kdb { + + void say_hello(); + +} // namespace kdb #endif \ No newline at end of file diff --git a/include/libkdb/pipe.hpp b/include/libkdb/pipe.hpp index d2f0fb6..471a15c 100644 --- a/include/libkdb/pipe.hpp +++ b/include/libkdb/pipe.hpp @@ -18,10 +18,9 @@ namespace kdb { int release_write(); void close_read(); void close_write(); - + std::vector read(); void write(std::byte *from, std::size_t bytes); - }; } // namespace kdb \ No newline at end of file diff --git a/include/libkdb/process.hpp b/include/libkdb/process.hpp index b5ac068..a3d1e9a 100644 --- a/include/libkdb/process.hpp +++ b/include/libkdb/process.hpp @@ -24,7 +24,8 @@ namespace kdb { process_state state_ = process_state::stopped; bool is_attached = true; process(pid_t pid, bool terminate_on_end, bool is_attached) - : pid_(pid), terminate_on_end(terminate_on_end), is_attached(is_attached) {} + : pid_(pid), terminate_on_end(terminate_on_end), + is_attached(is_attached) {} public: process() = delete; @@ -32,7 +33,8 @@ namespace kdb { process &operator=(const process &) = delete; ~process(); - static std::unique_ptr launch(std::filesystem::path path, bool debug = true); + static std::unique_ptr launch(std::filesystem::path path, + bool debug = true); static std::unique_ptr attach(pid_t pid); pid_t pid() const { return pid_; } diff --git a/include/libkdb/register.inc b/include/libkdb/register.inc new file mode 100644 index 0000000..3b95c56 --- /dev/null +++ b/include/libkdb/register.inc @@ -0,0 +1,225 @@ +#ifndef DEFINE_REGISTER +#error "This file is intended for textual inclusion with the\ +DEFINE_REGISTER macro defined" +#endif + + +/* --------------------------------------------------------x86 Register----------------------------------------------------------------------- - */ +//--------------------------------// +// x86-64 bit macros // +//------------------------------// +#ifdef __linux__ +#define GPR_OFFSET(reg) (offsetof(user, reg) + offsetof(user_regs_struct,reg)) +#define DEFINE_GPR_64(name, dwarf_id) DEFINE_REGISTER(name,dwarf_id,8,GPR_OFFSET(name),register_type::name, register_format::uint) +#define DEFINE_GPR_32(name, super) DEFINE_REGISTER(name,-1, 4, GPR_OFFSET(super), regsiter_type::sub_gpr, register_format::uint) +#define DEFINE_GPR_16(name, super) DEFINE_REGSITER(name,-1, 2, GPR_OFFSET(super), register_type::sub_gpr, register_format::uint) +#define DEFINE_GPR_8H(name, super) DEFINE_REGISTER(name,-1, 1, GPR_OFFSET(super), register_type::sub_gpr, register_format::uint) +#define DEFINE_GPR_8L(name, super) DEFINE_REGSITER(name,-1, 1, GPR_OFFSET(name), register_type::sub_gpr, register_format::uint) +#define FPR_OFFSET(reg) (offsetof(user, i387) + offsetof(user_fpregs_struct, reg)) +#define FPR_OFFSET(reg) (sizeof(user_fpregs_struct::reg) +#define DEFINE_FPR(name,dwarf_id, user_name) DEFINE_REGISTER(name, dwarf_id, FPR_SIZE(user_name),FPR_OFFSET(user_name), register_type::fpr, register_format::uint) +#define DEFINE_FP_ST(number) DEFINE_REGISTER(st ##number, (33 + number), 16, (FPR_OFFSET(st_space) + number *16), regsiter_type::fpr, register_format::long_double) +#define DEFINE_FP_MM(number) DEFINE_REGISTER(mm ##number, (41 + number), 8, (FPR_OFFSET(st_space) + number * 16), register_type::fpr, register_format::vector) +#define DEFINE_FP_XMM(number) DEFINE_REGISTER(xmm ##number, (17 + number), 16, (FPR_OFFSET(st_space) + number *16, register_type::fpr, register_format::vector) +#define DR_OFFSET(number) (offsetof(user, u_debugging) + number * 8) +#define DEFINE_DR(number) DEFINE_REGISTER(dr ## number, -1, 8 , DR_OFFSET(number), register_type::dr, register_format::uint) + +DEFINE_GPR_64(rax,0), +DEFINE_GPR_64(rdx,1), +DEFINE_GPR_64(rcx,2), +DEFINE_GPR_64(rbx,3), +DEFINE_GPR_64(rsi,4), +DEFINE_GPR_64(rdi,5), +DEFINE_GPR_64(rbp,6), +DEFINE_GPR_64(rsp,7), +DEFINE_GPR_64(r8, 8), +DEFINE_GPR_64(r9, 9), +DEFINE_GPR_64(r10, 10), +DEFINE_GPR_64(r11, 11), +DEFINE_GPR_64(r12, 12), +DEFINE_GPR_64(r13, 13), +DEFINE_GPR_64(r14, 14), +DEFINE_GPR_64(r15, 15), +DEFINE_GPR_64(rip, 16), +DEFINE_GPR_64(eflags, 49), +DEFINE_GPR_64(cs, 51), +DEFINE_GPR_64(fs, 54), +DEFINE_GPR_64(gs, 55), +DEFINE_GPR_64(ss, 52), +DEFINE_GPR_64(ds, 53), +DEFINE_GPR_64(es, 50), +DEFINE_GPR_64(orig_rax,-1), + +DEFINE_GPR_32(eax, rax), DEFINE_GPR_32(edx, rdx), +DEFINE_GPR_32(ecx, rcx), DEFINE_GPR_32(ebi, rbx), +DEFINE_GPR_32(edi, rdi), DEFINE_GPR_32(ebp, rbp), +DEFINE_GPR_32(esi, rsi), DEFINE_GPR_32(esp, rsp), +DEFINE_GPR_32(r8d, r8), DEFINE_GPR_32(r9d, r9), +DEFINE_GPR_32(r9d, r9), DEFINE_GPR_32(r10d, r10), +DEFINE_GPR_32(r11d, r11), DEFINE_GPR_32(r12d, r12), +DEFINE_GPR_32(r13d, r13), DEFINE_GPR_32(r14d, r14), +DEFINE_GPR_32(r15d, r15), + +DEFINE_GPR_16(ax, rax), DEFINE_GPR_16(dx, rdx), +DEFINE_GPR_16(bx, rbx), DEFINE_GPR_16(cx, rcx), +DEFINE_GPR_16(si, rsi), DEFINE_GPR_16(di, rdi), +DEFINE_GPR_16(bp, rbp), DEFINE_GPR_16(sp, rsp), +DEFINE_GPR_16(r8w, r8), DEFINE_GPR_16(r9w, r9), +DEFINE_GPR_16(r10w, r10), DEFINE_GPR_16(r11w, r11), +DEFINE_GPR_16(r12w, r12), DEFINE_GPR_16(r13w, r13), +DEFINE_GPR_16(r14w, r14), DEFINE_GPR_16(r15w, r15), +DEFINE_GPR_16(r15w, r15), + +DEFINE_GPR_8H(ah, rax), DEFINE_GPR_8H(dh, rdx), +DEFINE_GPR_8H(ch, rcx), DEFINE_GPR_8H(bh, rbx), + +DEFINE_GPR_8L(al, rax), DEFINE_GPR_8L(dl, rdx), +DEFINE_GPR_8L(cl, rcx), DEFINE_GPR_8L(bl, rbx), +DEFINE_GPR_8L(sl, rsi), DEFINE_GPR_8L(dil, rdi), +DEFINE_GPR_8L(bpl, rbp), DEFINE_GPR_8L(spl, rsp), + +DEFINE_GPR_8L(r8b, r8), DEFINE_GPR_8L(r9b, r9), +DEFINE_GPR_8L(r10b, r10), DEFINE_GPR_8L(r11b, r11), +DEFINE_GPR_8L(r12b, r12), DEFINE_GPR_8L(r13b, r13), +DEFINE_GPR_8L(r14b, r14), DEFINE_GPR_8L(r15b, r15), + +DEFINE_FPR(fcw, 65, , cwd), +DEFINE_FPR(fsw, 66, swd), +DEFINE_FPR(ftw, -1, ftd), +DEFINE_FPR(fop, -1, fop), +DEFINE_FPR(frip, -1, rip), +DEFINE_FPR(frdp, -1, rdp), +DEFINE_FPR(mxcr, 64, mxcsr), +DEFINE_FPR(mxcsrmask, -1, mxcr_mask), + + +DEFINE_FPR_ST(0), DEFINE_FPR_ST(1), DEFINE_FPR_ST(2), DEFINE_FPR_ST(3), DEFINE_FPP_ST(4), +DEFINE_FPR_ST(5), DEFINE_FPR_ST(6), DEFINE_FPR_ST(7), + +DEFINE_FPR_MM(0), DEFINE_FPR_MM(1), DEFINE_FPR_MM(2), DEFINE_FPR_MM(3), DEFINE_FPP_MM(4), +DEFINE_FPR_MM(5), DEFINE_FPR_MM(6), DEFINE_FPR_MM(7), + + +DEFINE_FPR_XMM(0), DEFINE_FPR_XMM(1), DEFINE_FPR_XMM(2), DEFINE_FPR_XMM(3), DEFINE_FPP_XMM(4), +DEFINE_FPR_XMM(5), DEFINE_FPR_XMM(6), DEFINE_FPR_XMM(7),DEFINE_FPR_XMM(8), DEFINE_FPR_XMM(9), +DEFINE_FPR_XMM(10), DEFINE_FPR_XMM(11), DEFINE_FPP_XMM(12),DEFINE_FPR_XMM(13), DEFINE_FPR_XMM(14), DEFINE_FPR_XMM(157), + +DEFINE_DR(0), DEFINE_DR(1), DEFINE_DR(2), DEFINE_DR(3), DEFINE_DR(4), +DEFINE_DR(5), DEFINE_DR(6), DEFINE(7), + + +/* --------------------------------------------------------AArch 64 Register----------------------------------------------------------------------- - */ +//--------------------------------// +// AArch-64 bit macros // +//------------------------------// +#elif defined(__APPLE__) + +#define GPR_OFFSET(regs_idx) (offsetof(user_pt_regs, regs[regs_idx])) +#define DEFINE_GPR_64(name, dwarf_id) DEFINE_REGISTER(name,dwarf_id,8,GPR_OFFSET(name), register_type::name, register_format::uint64) +#define DEFINE_GPR_32(name, super) DEFINE_RESGITER(name, -1, 8, GPR_OFFSET(super), register_type::sub_gpr, register_format::uint32) + + +#define FPR_OFFSET(regs_idx) (offsetof(user_fpsimd_state,vregs[regs_idx]) +#define DEFINE_FPR(name, dwarf_id) DEFINE_REGISTER(name,dward_id,16, FPR_OFFSET(name), register_format::fpr, register_type::_uint128_t) + + +DEFINE_GPR_64(x0,0), +DEFINE_GPR_64(x1,1), +DEFINE_GPR_64(x2,2), +DEFINE_GPR_64(x3,3), +DEFINE_GPR_64(x4,4), +DEFINE_GPR_64(x5,5), +DEFINE_GPR_64(x6,6), +DEFINE_GPR_64(x7,7), +DEFINE_GPR_64(x8,8), +DEFINE_GPR_64(x9,9), +DEFINE_GPR_64(x10,10), +DEFINE_GPR_64(x11,11), +DEFINE_GPR_64(x12,12), +DEFINE_GPR_64(x13,13), +DEFINE_GPR_64(x14,14), +DEFINE_GPR_64(x15,15), +DEFINE_GPR_64(x16,16), +DEFINE_GPR_64(x17,17), +DEFINE_GPR_64(x18,18), +DEFINE_GPR_64(x19,19), +DEFINE_GPR_64(x20,20), +DEFINE_GPR_64(x21,21), +DEFINE_GPR_64(x22,22), +DEFINE_GPR_64(x23,23), +DEFINE_GPR_64(x24,24), +DEFINE_GPR_64(x25,25), +DEFINE_GPR_64(x26,26), +DEFINE_GPR_64(x27,27), +DEFINE_GPR_64(x28,28), +DEFINE_GPR_64(x29,29), +DEFINE_GPR_64(x30,30), +DEFINE_GPR_64(sp,31), +DEFINE_GPR_64(pc,32), +DEFINE_GPR_64(orig_rax,-1), + +DEFINE_GPR_32(w0, -1), +DEFINE_GPR_32(w1, -1), +DEFINE_GPR_32(w2, -1), +DEFINE_GPR_32(w3, -1), +DEFINE_GPR_32(w4, -1), +DEFINE_GPR_32(w5, -1), +DEFINE_GPR_32(w6, -1), +DEFINE_GPR_32(w7, -1), +DEFINE_GPR_32(w8, -1), +DEFINE_GPR_32(w9, -1), +DEFINE_GPR_32(w10, -1), +DEFINE_GPR_32(w11, -1), +DEFINE_GPR_32(w12, -1), +DEFINE_GPR_32(w13, -1), +DEFINE_GPR_32(w14, -1), +DEFINE_GPR_32(w15, -1), +DEFINE_GPR_32(w16, -1), +DEFINE_GPR_32(w17, -1), +DEFINE_GPR_32(w18, -1), +DEFINE_GPR_32(w19, -1), +DEFINE_GPR_32(w20, -1), +DEFINE_GPR_32(w21, -1), +DEFINE_GPR_32(w22, -1), +DEFINE_GPR_32(w23, -1), +DEFINE_GPR_32(w24, -1), +DEFINE_GPR_32(w25, -1), +DEFINE_GPR_32(w26, -1), +DEFINE_GPR_32(w27, -1), +DEFINE_GPR_32(w28, -1), +DEFINE_GPR_32(w29, -1), +DEFINE_GPR_32(w30, -1), + + +DEFINE_FPR_128(v0, 64), +DEFINE_FPR_128(v1, 65), +DEFINE_FPR_128(v2, 66), +DEFINE_FPR_128(v3, 67), +DEFINE_FPR_128(v4, 68), +DEFINE_FPR_128(v5, 69), +DEFINE_FPR_128(v6, 70), +DEFINE_FPR_128(v7, 71), +DEFINE_FPR_128(v8, 72), +DEFINE_FPR_128(v9, 73), +DEFINE_FPR_128(v10, 74), +DEFINE_FPR_128(v11, 75), +DEFINE_FPR_128(v12, 76), +DEFINE_FPR_128(v13, 77), +DEFINE_FPR_128(v14, 78), +DEFINE_FPR_128(v15, 79), +DEFINE_FPR_128(v16, 80), +DEFINE_FPR_128(v17, 81), +DEFINE_FPR_128(v17, 82), +DEFINE_FPR_128(v18, 83), +DEFINE_FPR_128(v19, 84), +DEFINE_FPR_128(v20, 85), +DEFINE_FPR_128(v21, 86), +DEFINE_FPR_128(v22, 87), +DEFINE_FPR_128(v23, 88), +DEFINE_FPR_128(v24, 89), +DEFINE_FPR_128(v25, 90), +DEFINE_FPR_128(v26, 91), +DEFINE_FPR_128(v27, 92), +DEFINE_FPR_128(v28, 93), +DEFINE_FPR_128(v29, 94), +DEFINE_FPR_128(v30, 95), diff --git a/include/libkdb/register_info.hpp b/include/libkdb/register_info.hpp new file mode 100644 index 0000000..0e9e4b2 --- /dev/null +++ b/include/libkdb/register_info.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +/* + * This file defines the enum we will use for the register. We will abstract in + * a way that enum fields will be populated according to operating system. + */ + +namespace kdb { + + /* + * This should contain the definition of both ARM(MACOS) and X86(linux) + * regsiters Note some of the registers work similar but other differ widely, + * for example the debug register in X86 is 8 (d*) whereas ARM64 has a pack of + * more than 15 + */ + enum class register_id { + +#define DEFINE_REGSITER(name, dwarf_id, size, offset, type, foramt) name +#include +#undef DEFINE_REGISTER + }; + + enum class register_type { + gpr, + sub_gpr, + ip, + flags, + fpr, + vector, + segment, + debug + }; + + enum class register_format { uint, long_double, double_float, vector; }; + + enum class register_info { + register_id id; std::string_view name; std::int32_t dwarf_id; + std::size_t size; + std::size_t offset; + register_type type; + register_format format; + }; + + inline constexpr const register_info g_ register_infos[] = + { +#define DEFINE_REGSITER(name, dwarf_if, size, offset, type, format) \ + {register_id::name, #name, dwarf_id, size, offset, format} +#include +#undef DEFINE_REGSITER + } + + template + const register_info & register_info_by(T t) { + auto it = std::find_if(std::begin(g_regsiter_info), + std::end(g_register_infos), t); + if (it == std::end(g_register_infos)) + error::send("Can't find register info"); + return *it; + } + + inline const register_info ®ister_info_by_id(register_id id) { + return register_info_by([id](auto &i)) { return i.id == id; } + } + + inline const register_info ®ister_info_by_name(std::string_view name) { + return register_info_by([name](auto &i)) { return i.name == name; } + } + + inline const register_info & + regsiter_info_by_dwarf_id(std::uint32_t dwarf_id) { + return regsiter_info([dwarf_id](auto &i)) { return i.dwarf_id == dwarf_id; } + } + +}; // namespace kdb diff --git a/include/libkdb/register_ref.inc b/include/libkdb/register_ref.inc new file mode 100644 index 0000000..d1982a8 --- /dev/null +++ b/include/libkdb/register_ref.inc @@ -0,0 +1,106 @@ + // --------------------------- + // X86-64 bit registers + // --------------------------- + + /* For more info on X86 registers, please visit + https://math.hws.edu/eck/cs220/f22/registers.html */ + + /* rax - rsp && r8 - r15 are all GPRs */ + + /* + rax, + rbx, + rcx, + rdx, + rsi, + rdi, + rbp, + rsp, + r8, + r9, + r10, + r11, + r12, + r13, + r14, + r15 + + /*Instruction pointer */ + rip, + + /*Flags */ + rflags, + + /* SIMD (SSE/ AVX ) */ + xmm0, + xmm1, + xmm2, + xmm3, + xmm4, + xmm5, + xmm6, + xmm7 + + /* debug register */ + dr0, + dr1, + dr2, + dr3, + dr6, + dr7 + // --------------------------- + // ARM-64 bit registers + // --------------------------- + + /*For more info on ARM64 registers, please visit + https://cybersandeep.gitbook.io/arm64basicguide/chapter-2-understanding-arm64-registers + */ + /*x0 - x7 (caller saved) used for paramter and result */ + x0, + x1, + x2, + x3, + x4, + x5, + x6, + x7, + /* used for syscall number. */ + x8, + + /* X9 - X15 are caller based register and are used for temp or intermediate + calcualtion. Use and throw away terminilogy */ + x9, + x10, + x11, + x12, + x13, + x14, + x15, + + /* x16 and x17 are special register (IPO/ IP1) and used during function + calls, jmp, goto stmts */ + x16, + x17, + x18, + /* x19 - x28 Callee saved register must be presereved across function calls. + Function using these regsiters should save the original state and then + overwrite. */ + x19, + x20, + x21, + x22, + x23, + x24, + x25, + x26, + x27, + x28, + /* X29 - Frame pointer points to starting of the stack frame of a function. + */ + x29, + /*x30 is useed to hold the return address when a func call is made */ + x30, + /*SP points to top of the stack */ + sp, + /*PC points to address of next instruction */ + pc \ No newline at end of file diff --git a/include/libkdb/registers.hpp b/include/libkdb/registers.hpp new file mode 100644 index 0000000..ce527de --- /dev/null +++ b/include/libkdb/registers.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +namespace kdb { + class process; + class registers { + private: + friend process; + registers(process &proc) : proc_(&proc) {} + user data_ : process *proc_; + + public: + registers() = delete; + registers(const registers &) = delete; + registers &operator=(const registers &) = delete; + using value = std::variant; + + using value = std::variant + value read(const register_info &info) const; + value write(const register_info &info, value val); + + template T read_by_id(register_id id) { + return std::get(read(register_info_by_id(id))); + } + + template T write_by_id(register_id, value val) { + write(register_info_by_id(id), val)); + } + }; + +} // namespace kdb \ No newline at end of file diff --git a/include/libkdb/types.hpp b/include/libkdb/types.hpp new file mode 100644 index 0000000..8a493f9 --- /dev/null +++ b/include/libkdb/types.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +namespace kdb { + using byte64 = std::array using byte128 = + std::array + +} // namespace kdb \ No newline at end of file diff --git a/src/libkdb.cpp b/src/libkdb.cpp index 77f6215..9066183 100644 --- a/src/libkdb.cpp +++ b/src/libkdb.cpp @@ -1,13 +1,8 @@ -#include +#include #include -namespace kdb{ - - void say_hello(){ - - std::cout << "hello kdb !" << std::endl; +namespace kdb { - } - - -}// namespace kdb \ No newline at end of file + void say_hello() { std::cout << "hello kdb !" << std::endl; } + +} // namespace kdb \ No newline at end of file diff --git a/src/pipe.cpp b/src/pipe.cpp index 8c69be0..eeda41f 100644 --- a/src/pipe.cpp +++ b/src/pipe.cpp @@ -44,7 +44,7 @@ namespace kdb { fds_[fd_write] = -1; } } - + std::vector pipe::read() { char buf[1024]; int chars_read{}; @@ -52,7 +52,7 @@ namespace kdb { error::send_error_no("Could not read from the buffer"); } // dont confuse yourself this is just reinterpret_cast(buf); - auto bytes = static_cast(static_cast(buf)); + auto bytes = static_cast(static_cast(buf)); return std::vector(bytes, bytes + chars_read); } void pipe::write(std::byte *from, std::size_t bytes) { diff --git a/test/target2.cpp b/test/target2.cpp index ccb2f2d..4b20851 100644 --- a/test/target2.cpp +++ b/test/target2.cpp @@ -1,11 +1,10 @@ -#include +#include -int main(){ - - while(100){ - std::cout << "yes" << std::endl; - } - - return 0; - +int main() { + + while (100) { + std::cout << "yes" << std::endl; + } + + return 0; } \ No newline at end of file diff --git a/test/targets/run_endlessly.cpp b/test/targets/run_endlessly.cpp index 109649f..8c511fd 100644 --- a/test/targets/run_endlessly.cpp +++ b/test/targets/run_endlessly.cpp @@ -1,9 +1,9 @@ -#include +#include +int main() { -int main(){ - - volatile int i = 0; - while(true)i = 42; - return 0; + volatile int i = 0; + while (true) + i = 42; + return 0; } \ No newline at end of file diff --git a/test/tests.cpp b/test/tests.cpp index 47d9a2c..6e8502c 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,59 +1,54 @@ #include -#include -#include -#include -#include -#include - - -namespace{ - bool process_exist(pid_t pid){ - auto sig = kill(pid,0); - return (sig != -1 && errno != ESRCH); - } - - char get_process_status(pid_t pid){ - std::ifstream stat("/proc/" + std::to_string(pid) + "/stat"); - std::string data; - std::getline(stat,data); - /*file structure for /proc/pid/stat/ is something like 1 (init) S 0 1 1 .. - * We generally care about the third value (S) which tells us the state of given process, here 1 means the pid, (init) means the process name - */ - - auto index_of_last_paranthesis = data.rfind(')'); - auto index_of_state = index_of_last_paranthesis + 2; - return data[index_of_state]; - - - } -}//namespace - -TEST_CASE("process::launch success", "[process]"){ - auto proc = kdb::process::launch("yes"); - REQUIRE(process_exist(proc->pid())); +#include +#include +#include +#include +#include + +namespace { + bool process_exist(pid_t pid) { + auto sig = kill(pid, 0); + return (sig != -1 && errno != ESRCH); + } + + char get_process_status(pid_t pid) { + std::ifstream stat("/proc/" + std::to_string(pid) + "/stat"); + std::string data; + std::getline(stat, data); + /*file structure for /proc/pid/stat/ is something like 1 (init) S 0 1 1 .. + * We generally care about the third value (S) which tells us the state of + * given process, here 1 means the pid, (init) means the process name + */ + + auto index_of_last_paranthesis = data.rfind(')'); + auto index_of_state = index_of_last_paranthesis + 2; + return data[index_of_state]; + } +} // namespace + +TEST_CASE("process::launch success", "[process]") { + auto proc = kdb::process::launch("yes"); + REQUIRE(process_exist(proc->pid())); } -TEST_CASE("process::launc no_such_program", "[process]"){ - REQUIRE_THROWS_AS(kdb::process::launch("this_should_not_work"), kdb::error); - } - -TEST_CASE("process::attach success", "[process]"){ - auto target = kdb::process::launch("targets/run_endlessely", false); - auto proc = kdb::process::attach(target->pid()); - REQUIRE(get_process_status(target->pid()) == 't'); +TEST_CASE("process::launc no_such_program", "[process]") { + REQUIRE_THROWS_AS(kdb::process::launch("this_should_not_work"), kdb::error); } -TEST_CASE("process::attach invalid PID", "[process]"){ - REQUIRE_THROWS_AS(kdb::process::attach(0), kdb::error); +TEST_CASE("process::attach success", "[process]") { + auto target = kdb::process::launch("targets/run_endlessely", false); + auto proc = kdb::process::attach(target->pid()); + REQUIRE(get_process_status(target->pid()) == 't'); } - -TEST_CASE("process::resume success", "[process]"){ - auto proc = kdb::process::launch("target/run_endlessly"); - proc->resume(); - auto status = get_process_status(proc->pid()); - auto success = (status == 'R') || (status == 'S'); - REQUIRE(success); +TEST_CASE("process::attach invalid PID", "[process]") { + REQUIRE_THROWS_AS(kdb::process::attach(0), kdb::error); } - +TEST_CASE("process::resume success", "[process]") { + auto proc = kdb::process::launch("target/run_endlessly"); + proc->resume(); + auto status = get_process_status(proc->pid()); + auto success = (status == 'R') || (status == 'S'); + REQUIRE(success); +} diff --git a/tools/kdb.cpp b/tools/kdb.cpp index 0b09eed..129449b 100644 --- a/tools/kdb.cpp +++ b/tools/kdb.cpp @@ -1,10 +1,10 @@ #include #include -#include #include +#include +#include #include #include -#include #include #include #include