From 764e5a1cde47fd128cc50f3c123d94d3e2f5e107 Mon Sep 17 00:00:00 2001 From: Egor00f Date: Wed, 9 Apr 2025 21:21:10 +0500 Subject: [PATCH] add debug funcs + created debug funcs + created `registers` class + add it to build build not passed( --- .vscode/settings.json | 4 +- Makefile | 7 +- src/debug.h | 1 + src/debug/debug.c | 175 ++++++++++++++++++++++++++++++++++++++++++ src/debug/debug.h | 26 +++++++ src/debug/registers.c | 146 +++++++++++++++++++++++++++++++++++ src/debug/registers.h | 25 ++++++ src/graphic.c | 6 +- src/syscalls.c | 34 +------- 9 files changed, 384 insertions(+), 40 deletions(-) create mode 100644 src/debug/debug.c create mode 100644 src/debug/debug.h create mode 100644 src/debug/registers.c create mode 100644 src/debug/registers.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 1613ee2..83001e8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,7 +5,9 @@ "stdlib.h": "c", "ksys.h": "c", "socket.h": "c", - "graphic.h": "c" + "graphic.h": "c", + "syscalls.h": "c", + "registers.h": "c" }, "cSpell.words": [ "syscalls", diff --git a/Makefile b/Makefile index 03ec53a..5ae86b5 100644 --- a/Makefile +++ b/Makefile @@ -31,8 +31,9 @@ MYOBJS = Socket_O = src/sockets/socket.o src/sockets/socket_lua.o src/sockets/sockaddr.o +Debug_O = src/debug/debug.o src/debug/registers.o -ALL_O = src/syscalls.o src/ARP_entry.o src/systemColors.o src/graphic.o $(Socket_O) +ALL_O = src/syscalls.o src/ARP_entry.o src/systemColors.o src/graphic.o $(Socket_O) $(Debug_O) syscalls.dll: $(ALL_O) $(CC) -shared -T dll.lds --entry _DllStartup -o $@ $(ALL_O) $(LIBS) @@ -51,10 +52,12 @@ src/sockets/socket.o: src/sockets/socket.c src/sockets/socket.h src/sockets/socket_lua.o: src/sockets/socket_lua.c src/sockets/socket_lua.h src/sockets/sockaddr.o: src/sockets/sockaddr.c src/sockets/sockaddr.h src/graphic.o: src/graphic.c src/graphic.h +src/debug/debug.o: src/debug/debug.c src/debug/debug.h src/debug/registers.h +src/debug/registers.o: src/debug/registers.c src/debug/registers.h src/syscalls.h ## headers src/graphic.h: src/syscalls.h src/sockets/socket_lua.h: src/syscalls.h src/sockets/socket.h src/sockets/sockaddr.h: src/sockets/socket.h src/syscalls.h - +src/debug/debug.h: src/syscalls.h diff --git a/src/debug.h b/src/debug.h index 2d4ce14..cb4335d 100644 --- a/src/debug.h +++ b/src/debug.h @@ -21,4 +21,5 @@ #define DEBUG_LINE(msg) DEBUG_PRINT(msg); DEBUG_PRINT("\n") + #endif // __DEBUG_H__ diff --git a/src/debug/debug.c b/src/debug/debug.c new file mode 100644 index 0000000..27cc447 --- /dev/null +++ b/src/debug/debug.c @@ -0,0 +1,175 @@ +#include +#include "debug.h" +#include "registers.h" + +int syscalls_DebugPuts(lua_State* L) +{ + _ksys_debug_puts(luaL_checkstring(L, 1)); + + return 0; +} + +int syscalls_DebugPutc(lua_State* L) +{ + _ksys_debug_putc(*luaL_checkstring(L, 1)); + + return 0; +} + +struct DebugMessageArea +{ + int Size; + int Used; + char data[]; +}; + +int syscalls_SetMessageArea(lua_State* L) +{ + struct DebugMessageArea* p = luaL_checkinteger(L, 1); + + asm_inline( + "int $0x40" + :: "a"(69), "b"(0), "c"(p) + ); + + return 0; +} + +int syscalls_GetRegisters(lua_State* L) +{ + struct registers* r = syscalls_pushRegisters(L); + + uint32_t pid = luaL_checkinteger(L, 1); + + asm_inline( + "int $0x40" + :: "a"(69), "b"(1), "c"(pid), "d"(sizeof(struct registers)), "S"(r) + ); + + return 1; +} + +int syscalls_SetRegisters(lua_State* L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + struct registers* r = luaL_checkudata(L, 1, syscalls_registers_metatable_name); + + asm_inline( + "int $0x40" + :: "a"(69), "b"(2), "c"(pid), "d"(sizeof(struct registers)), "S"(r) + ); + + return 1; +} + +int syscalls_Disconnect(lua_State* L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + + asm_inline( + "int $0x40" + :: "a"(69), "b"(3), "c"(pid) + ); + + return 1; +} + +int syscalls_Stop(lua_State* L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + + asm_inline( + "int $0x40" + :: "a"(69), "b"(4), "c"(pid) + ); + + return 1; +} + +int syscalls_Continue(lua_State* L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + + asm_inline( + "int $0x40" + :: "a"(69), "b"(5), "c"(pid) + ); + + return 1; +} + +int syscalls_ReadFromMem(lua_State *L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + uint32_t bytes = luaL_checkinteger(L, 2); + uint32_t pointer = luaL_checkinteger(L, 3); + uint32_t buffer = luaL_checkinteger(L, 4); + uint32_t ret; + + asm_inline( + "int $0x40" + : "=a"(ret) + : "a"(69), "b"(6), "c"(pid), "d"(bytes), "S"(pointer), "D"(buffer) + ); + + if (ret == -1) + lua_pushnil(L); + else + lua_pushinteger(L, ret); + + return 1; +} + +int syscalls_WriteToMem(lua_State* L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + uint32_t bytes = luaL_checkinteger(L, 2); + uint32_t pointer = luaL_checkinteger(L, 3); + uint32_t buffer = luaL_checkinteger(L, 4); + uint32_t ret; + + asm_inline( + "int $0x40" + : "=a"(ret) + : "a"(69), "b"(7), "c"(pid), "d"(bytes), "S"(pointer), "D"(buffer) + ); + + if (ret == -1) + lua_pushnil(L); + else + lua_pushinteger(L, ret); + + return 1; +} + +int syscalls_Done(lua_State* L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + + asm_inline( + "int $0x40" + :: "a"(69), "b"(8), "c"(pid) + ); + + return 1; +} + +int syscalls_DefineBreakpoint(lua_State* L) +{ + uint32_t pid = luaL_checkinteger(L, 1); + + + uint32_t flags = luaL_checkinteger(L, 2) & 0xFF | (luaL_checkinteger(L, 3) << 16) | (luaL_checkinteger(L, 4) << 18); + + uint32_t ret; + + asm_inline( + "int $0x40" + : "=a"(ret) + : "a"(69), "b"(9), "c"(pid), "d"(flags) + ); + + lua_pushinteger(L, ret); + + return 1; +} diff --git a/src/debug/debug.h b/src/debug/debug.h new file mode 100644 index 0000000..0cede26 --- /dev/null +++ b/src/debug/debug.h @@ -0,0 +1,26 @@ +#ifndef __DEBUG_LUA_H__ +#define __DEBUG_LUA_H__ + +#include "../syscalls.h" + +enum BreakpointCondition +{ + Execute = 0, + Write = 1, + ReadWrite = 0b11 +}; + +enum BreakpointLen +{ + Byte = 0, + Word = 1, + Dword = 0b11 +}; + +int syscalls_DebugPutc(lua_State* L); + +int syscalls_DebugPuts(lua_State* L); + +int syscalls_SetMessageArea(lua_State* L); + +#endif // __DEBUG_LUA_H__ \ No newline at end of file diff --git a/src/debug/registers.c b/src/debug/registers.c new file mode 100644 index 0000000..436f935 --- /dev/null +++ b/src/debug/registers.c @@ -0,0 +1,146 @@ +#include "registers.h" +#include "../syscalls.h" +#include + +static int syscalls_indexRegisters(lua_State* L) +{ + struct registers* r = luaL_checkudata(L, 1, syscalls_registers_metatable_name); + const char* index = luaL_checkstring(L, 2); + + if (strcmp(index, "eax") == 0) + { + lua_pushinteger(L, r->eax); + } + else if (strcmp(index, "ebx") == 0) + { + lua_pushinteger(L, r->ebx); + } + else if (strcmp(index, "esp") == 0) + { + lua_pushinteger(L, r->esp); + } + else if (strcmp(index, "esi") == 0) + { + lua_pushinteger(L, r->esi); + } + else if (strcmp(index, "edi") == 0) + { + lua_pushinteger(L, r->edi); + } + else if (strcmp(index, "eip") == 0) + { + lua_pushinteger(L, r->eip); + } + else if (strcmp(index, "eflags") == 0) + { + lua_pushinteger(L, r->eflags); + } + else + { + lua_pushnil(L); + } + + return 1; +} + +static int syscalls_newindexRegisters(lua_State* L) +{ + struct registers* r = luaL_checkudata(L, 1, syscalls_registers_metatable_name); + const char* index = luaL_checkstring(L, 2); + uint32_t val = luaL_checkinteger(L, 3); + + if (strcmp(index, "eax") == 0) + { + r->eax = val; + } + else if (strcmp(index, "ebx") == 0) + { + r->ebx = val; + } + else if (strcmp(index, "esp") == 0) + { + r->esp = val; + } + else if (strcmp(index, "esi") == 0) + { + r->esi = val; + } + else if (strcmp(index, "edi") == 0) + { + r->edi = val; + } + else if (strcmp(index, "eip") == 0) + { + r->eip = val; + } + else if (strcmp(index, "eflags") == 0) + { + r->eflags = val; + } + else + { + luaL_pushfail(L); + } + + return 1; +} + +static int syscalls_eqRegisters(lua_State* L) +{ + lua_pushboolean( + L, + memcmp( + luaL_checkudata(L, 1, syscalls_registers_metatable_name), + luaL_checkudata(L, 2, syscalls_registers_metatable_name), + sizeof(struct registers) + ) + ); + + return 1; +} + +static const luaL_Reg syscalls_registers_m[] = { + {"__index", syscalls_indexRegisters}, + {"__newindex", syscalls_newindexRegisters}, + {"__eq", syscalls_eqRegisters}, + {NULL, NULL} +}; + +struct ARP_entry* syscalls_pushRegisters(lua_State* L) +{ + DEBUG_LINE("push ARP entry"); + + struct ARP_entry* entry = lua_newuserdata(L, sizeof(struct registers)); + + luaL_setmetatable(L, syscalls_registers_metatable_name); + + return entry; +} + +static int syscalls_newRegisters(lua_State* L) +{ + struct registers* r = syscalls_pushRegisters(L); + memset(r, 0, sizeof(struct registers)); + + return 1; +} + +static const luaL_Reg syscalls_registers_lib[] = { + {"new", syscalls_newRegisters}, + {NULL, NULL} +}; + +void syscalls_register_registers(lua_State* L) +{ + DEBUG_LINE("register registers entry"); + + luaL_newlib(L, syscalls_registers_lib); + + lua_setfield(L, -2, syscalls_registers_name); + + + luaL_newmetatable(L, syscalls_registers_metatable_name); + luaL_setfuncs(L, syscalls_registers_m, 0); + + lua_pop(L, 1); +} diff --git a/src/debug/registers.h b/src/debug/registers.h new file mode 100644 index 0000000..da4ee47 --- /dev/null +++ b/src/debug/registers.h @@ -0,0 +1,25 @@ +#ifndef __REGISTERS_H__ +#define __REGISTERS_H__ + +#include + +struct registers +{ + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ebx; + uint32_t esp; + uint32_t esi; + uint32_t edi; + char zero[12]; +}; + +#define syscalls_registers_name "Registers table" +#define syscalls_registers_metatable_name "Registers metatable" + +struct registers* syscalls_pushRegisters(lua_State* L); + +inline void syscalls_register_registers(lua_State* L); + +#endif // __REGISTERS_H__ diff --git a/src/graphic.c b/src/graphic.c index 220c93c..4a1f0c3 100644 --- a/src/graphic.c +++ b/src/graphic.c @@ -28,7 +28,7 @@ int syscalls_drawLine(lua_State* L) return 0; } -static void drawText(char* text, uint32_t x, uint32_t y, ksys_color_t color, size_t len, uint64_t backgroundColor) +static inline void drawText(char* text, uint32_t x, uint32_t y, ksys_color_t color, size_t len, uint64_t backgroundColor) { bool fillBackground = !(backgroundColor << 32); @@ -50,16 +50,14 @@ static void drawText(char* text, uint32_t x, uint32_t y, ksys_color_t color, siz } -static void syscall_drawText(const char* text, uint32_t x, uint32_t y, ksys_color_t color, enum TextScale size, uint32_t len, uint64_t backgroundColor, enum DrawTextEncoding encoding) +static inline void syscall_drawText(const char* text, uint32_t x, uint32_t y, ksys_color_t color, enum TextScale size, uint32_t len, uint64_t backgroundColor, enum DrawTextEncoding encoding) { - enum DrawTextEncoding_ { cp866_6x9 = 0, cp866_8x16 = 1 }; - enum scale { scale_x1 = 0, diff --git a/src/syscalls.c b/src/syscalls.c index 717a367..e75c894 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -13,6 +13,7 @@ #include "systemColors.h" #include "sockets/socket_lua.h" +#include "debug/debug.h" #include "graphic.h" @@ -1487,39 +1488,6 @@ static int syscalls_ReadARPConflicts(lua_State* L) return 1; } -static int syscalls_DebugPuts(lua_State* L) -{ - _ksys_debug_puts(luaL_checkstring(L, 1)); - - return 0; -} - -static int syscalls_DebugPutc(lua_State* L) -{ - _ksys_debug_putc(*luaL_checkstring(L, 1)); - - return 0; -} - -struct DebugMessageArea -{ - int Size; - int Used; - char data[]; -}; - -static int syscalls_SetMessageArea(lua_State* L) -{ - struct DebugMessageArea* p = luaL_checkinteger(L, 1); - - asm_inline( - "int $0x40" - :: "a"(69), "b"(0) - ); - - return 0; -} - /* ** functions for 'syscalls' library */