From 0d47afc02cec1677e189c9212eb832e096900a5a Mon Sep 17 00:00:00 2001 From: dd86k Date: Fri, 16 Feb 2024 11:59:37 -0500 Subject: [PATCH] Deprecate older aliases for bswap --- src/adbg/debugger/memory.d | 35 +++++++++++++++---------------- src/adbg/legacy/server/macho.d | 26 +++++++++++------------ src/adbg/object/format/macho.d | 38 +++++++++++++++++----------------- src/adbg/object/server.d | 4 ++-- src/adbg/utils/bit.d | 2 +- src/adbg/utils/uid.d | 8 +++---- 6 files changed, 55 insertions(+), 58 deletions(-) diff --git a/src/adbg/debugger/memory.d b/src/adbg/debugger/memory.d index 1521011a..d663a170 100644 --- a/src/adbg/debugger/memory.d +++ b/src/adbg/debugger/memory.d @@ -22,29 +22,30 @@ version (Windows) { import adbg.include.windows.ntdll; } else version (Posix) { import core.sys.posix.sys.stat; - import core.sys.posix.sys.wait : waitpid, SIGCONT, WUNTRACED; import core.sys.posix.signal : kill, SIGKILL, siginfo_t, raise; import core.sys.posix.sys.uio; import core.sys.posix.fcntl : open; import adbg.include.posix.mann; import adbg.include.posix.ptrace; import adbg.include.posix.unistd; - import adbg.include.linux.user; - private enum __WALL = 0x40000000; + + version (linux) { + import adbg.include.linux.user; + import core.stdc.errno : errno; + } } extern (C): -/// Get the system configured size of a page. -/// -/// This typically is the lowest supported page size on the platform. -/// Returns: Page size in Bytes, or 0 on error. +/// Get the system configured size of a page, typically target's smallest size. +/// Returns: Page size in bytes; Or 0 on error. size_t adbg_memory_pagesize() { version (Windows) { SYSTEM_INFO sysinfo = void; GetSystemInfo(&sysinfo); return sysinfo.dwPageSize; } else version (Posix) { + // NOTE: sysconf, on error, returns -1 c_long r = sysconf(_SC_PAGE_SIZE); return r < 0 ? 0 : r; } @@ -64,19 +65,17 @@ size_t adbg_memory_pagesize() { /// size = Size of data. /// Returns: Error code. int adbg_memory_read(adbg_process_t *tracee, size_t addr, void *data, uint size) { - if (tracee == null || data == null) { - return adbg_oops(AdbgError.nullArgument); - } + if (tracee == null || data == null) + return adbg_oops(AdbgError.invalidArgument); //TODO: FreeBSD/NetBSD/OpenBSD: PT_IO // Linux 6.2 (include/uapi/linux/ptrace.h) still has no PT_IO + //TODO: Under Linux, /proc/pid/mem could be interesting version (Windows) { if (ReadProcessMemory(tracee.hpid, cast(void*)addr, data, size, null) == 0) return adbg_oops(AdbgError.os); return 0; } else version (linux) { // Based on https://www.linuxjournal.com/article/6100 - import core.stdc.errno : errno; - c_long *dest = cast(c_long*)data; /// target int r = size / c_long.sizeof; /// number of "long"s to read @@ -109,7 +108,7 @@ int adbg_memory_read(adbg_process_t *tracee, size_t addr, void *data, uint size) /// Returns: Error code. int adbg_memory_write(adbg_process_t *tracee, size_t addr, void *data, uint size) { if (tracee == null || data == null) - return adbg_oops(AdbgError.nullArgument); + return adbg_oops(AdbgError.invalidArgument); //TODO: FreeBSD/NetBSD/OpenBSD: PT_IO version (Windows) { @@ -213,7 +212,7 @@ struct adbg_memory_map_t { /// Returns: Error code. int adbg_memory_maps(adbg_process_t *tracee, adbg_memory_map_t **mmaps, size_t *mcount, ...) { if (tracee == null || mmaps == null || mcount == null) - return adbg_oops(AdbgError.nullArgument); + return adbg_oops(AdbgError.invalidArgument); // Get options va_list list = void; @@ -232,7 +231,7 @@ L_OPTION: *mcount = 0; if (tracee.pid == 0) - return adbg_oops(AdbgError.notAttached); + return adbg_oops(AdbgError.debuggerUnattached); version (Windows) { if (__dynlib_psapi_load()) // EnumProcessModules, QueryWorkingSet @@ -736,7 +735,7 @@ adbg_scan_t* adbg_memory_scan(adbg_process_t *tracee, void* data, size_t datasiz // Initial check and setup if (tracee == null || data == null) { - adbg_oops(AdbgError.nullArgument); + adbg_oops(AdbgError.invalidArgument); return null; } if (datasize == 0) { @@ -752,7 +751,7 @@ adbg_scan_t* adbg_memory_scan(adbg_process_t *tracee, void* data, size_t datasiz switch (tracee.status) with (AdbgStatus) { case standby, paused, running: break; default: - adbg_oops(AdbgError.notPaused); + adbg_oops(AdbgError.debuggerUnpaused); return null; } @@ -873,7 +872,7 @@ LENTRY: for (size_t mi; mi < modcount; ++mi) { int adbg_memory_rescan(adbg_scan_t *scanner, void* data, size_t size) { // Initial check and setup if (scanner == null || data == null) - return adbg_oops(AdbgError.nullArgument); + return adbg_oops(AdbgError.invalidArgument); if (size == 0) return adbg_oops(AdbgError.scannerDataEmpty); if (size > 8) diff --git a/src/adbg/legacy/server/macho.d b/src/adbg/legacy/server/macho.d index 74747e65..8a86dddd 100644 --- a/src/adbg/legacy/server/macho.d +++ b/src/adbg/legacy/server/macho.d @@ -313,22 +313,20 @@ int adbg_obj_macho_load(adbg_object_t *obj, int sig) { } if (obj.macho.reversed) { - //obj.macho.hdr.magic = adbg_util_bswap32(obj.macho.hdr.magic); - if (obj.macho.fat) { - obj.macho.fathdr.nfat_arch = adbg_util_bswap32(obj.macho.fathdr.nfat_arch); - obj.macho.fatarch.cputype = adbg_util_bswap32(obj.macho.fatarch.cputype); - obj.macho.fatarch.subtype = adbg_util_bswap32(obj.macho.fatarch.subtype); - obj.macho.fatarch.offset = adbg_util_bswap32(obj.macho.fatarch.offset); - obj.macho.fatarch.size = adbg_util_bswap32(obj.macho.fatarch.size); - obj.macho.fatarch.alignment = adbg_util_bswap32(obj.macho.fatarch.alignment); + obj.macho.fathdr.nfat_arch = adbg_bswap32(obj.macho.fathdr.nfat_arch); + obj.macho.fatarch.cputype = adbg_bswap32(obj.macho.fatarch.cputype); + obj.macho.fatarch.subtype = adbg_bswap32(obj.macho.fatarch.subtype); + obj.macho.fatarch.offset = adbg_bswap32(obj.macho.fatarch.offset); + obj.macho.fatarch.size = adbg_bswap32(obj.macho.fatarch.size); + obj.macho.fatarch.alignment = adbg_bswap32(obj.macho.fatarch.alignment); } else { - obj.macho.hdr.cputype = adbg_util_bswap32(obj.macho.hdr.cputype); - obj.macho.hdr.subtype = adbg_util_bswap32(obj.macho.hdr.subtype); - obj.macho.hdr.filetype = adbg_util_bswap32(obj.macho.hdr.filetype); - obj.macho.hdr.ncmds = adbg_util_bswap32(obj.macho.hdr.ncmds); - obj.macho.hdr.sizeofcmds = adbg_util_bswap32(obj.macho.hdr.sizeofcmds); - obj.macho.hdr.flags = adbg_util_bswap32(obj.macho.hdr.flags); + obj.macho.hdr.cputype = adbg_bswap32(obj.macho.hdr.cputype); + obj.macho.hdr.subtype = adbg_bswap32(obj.macho.hdr.subtype); + obj.macho.hdr.filetype = adbg_bswap32(obj.macho.hdr.filetype); + obj.macho.hdr.ncmds = adbg_bswap32(obj.macho.hdr.ncmds); + obj.macho.hdr.sizeofcmds = adbg_bswap32(obj.macho.hdr.sizeofcmds); + obj.macho.hdr.flags = adbg_bswap32(obj.macho.hdr.flags); } } diff --git a/src/adbg/object/format/macho.d b/src/adbg/object/format/macho.d index e25d68f9..34ce4c25 100644 --- a/src/adbg/object/format/macho.d +++ b/src/adbg/object/format/macho.d @@ -427,19 +427,19 @@ int adbg_object_macho_load(adbg_object_t *o) { with (o.i) if (o.p.reversed) { if (macho.fat) { - macho.fat_header.nfat_arch = adbg_util_bswap32(macho.fat_header.nfat_arch); - macho.fat_arch.cputype = adbg_util_bswap32(macho.fat_arch.cputype); - macho.fat_arch.subtype = adbg_util_bswap32(macho.fat_arch.subtype); - macho.fat_arch.offset = adbg_util_bswap32(macho.fat_arch.offset); - macho.fat_arch.size = adbg_util_bswap32(macho.fat_arch.size); - macho.fat_arch.alignment = adbg_util_bswap32(macho.fat_arch.alignment); + macho.fat_header.nfat_arch = adbg_bswap32(macho.fat_header.nfat_arch); + macho.fat_arch.cputype = adbg_bswap32(macho.fat_arch.cputype); + macho.fat_arch.subtype = adbg_bswap32(macho.fat_arch.subtype); + macho.fat_arch.offset = adbg_bswap32(macho.fat_arch.offset); + macho.fat_arch.size = adbg_bswap32(macho.fat_arch.size); + macho.fat_arch.alignment = adbg_bswap32(macho.fat_arch.alignment); } else { - macho.header.cputype = adbg_util_bswap32(macho.header.cputype); - macho.header.subtype = adbg_util_bswap32(macho.header.subtype); - macho.header.filetype = adbg_util_bswap32(macho.header.filetype); - macho.header.ncmds = adbg_util_bswap32(macho.header.ncmds); - macho.header.sizeofcmds = adbg_util_bswap32(macho.header.sizeofcmds); - macho.header.flags = adbg_util_bswap32(macho.header.flags); + macho.header.cputype = adbg_bswap32(macho.header.cputype); + macho.header.subtype = adbg_bswap32(macho.header.subtype); + macho.header.filetype = adbg_bswap32(macho.header.filetype); + macho.header.ncmds = adbg_bswap32(macho.header.ncmds); + macho.header.sizeofcmds = adbg_bswap32(macho.header.sizeofcmds); + macho.header.flags = adbg_bswap32(macho.header.flags); } } @@ -483,11 +483,11 @@ macho_fat_arch* adbg_object_macho_fat_arch(adbg_object_t *o, size_t index) { macho_fat_arch* fat_arch = &o.i.macho.fat_arch[index]; if (o.p.reversed && o.i.macho.reversed_fat_arch[index] == false) { - fat_arch.cputype = adbg_util_bswap32(fat_arch.cputype); - fat_arch.subtype = adbg_util_bswap32(fat_arch.subtype); - fat_arch.offset = adbg_util_bswap32(fat_arch.offset); - fat_arch.size = adbg_util_bswap32(fat_arch.size); - fat_arch.alignment = adbg_util_bswap32(fat_arch.alignment); + fat_arch.cputype = adbg_bswap32(fat_arch.cputype); + fat_arch.subtype = adbg_bswap32(fat_arch.subtype); + fat_arch.offset = adbg_bswap32(fat_arch.offset); + fat_arch.size = adbg_bswap32(fat_arch.size); + fat_arch.alignment = adbg_bswap32(fat_arch.alignment); o.i.macho.reversed_fat_arch[index] = true; } return fat_arch; @@ -508,8 +508,8 @@ macho_load_command* adbg_object_macho_load_command(adbg_object_t *o, size_t inde command = cast(macho_load_command*)(cast(void*)o.i.macho.commands + next); if (o.p.reversed && o.i.macho.reversed_commands[i] == false) { version (Trace) trace("Reversing %u", cast(uint)i); - command.cmd = adbg_util_bswap32(command.cmd); - command.cmdsize = adbg_util_bswap32(command.cmdsize); + command.cmd = adbg_bswap32(command.cmd); + command.cmdsize = adbg_bswap32(command.cmdsize); o.i.macho.reversed_commands[i] = true; } version (Trace) trace("command cmd=%x size=0x%x", command.cmd, command.cmdsize); diff --git a/src/adbg/object/server.d b/src/adbg/object/server.d index 654ca33c..fd4da915 100644 --- a/src/adbg/object/server.d +++ b/src/adbg/object/server.d @@ -36,13 +36,13 @@ extern (C): // Either: // - extent formats to include everyting else, have functions to say type (_is_dump) // - keep format and add a "type/purpose" enum (exec/dump/symbols/etc.) -// - do type as first-class and format second //TODO: Consider loading first 4 KiB for detection before loading rest //TODO: const(ubyte)* adbg_obj_section(obj, ".abc"); //TODO: const(ubyte)* adbg_obj_section_i(obj, index); //TODO: const(ubyte)* adbg_object_get_section_by_type(obj, type); //TODO: const(char)* adbg_object_get_debug_path(obj); -//TODO: int adbg_object_load_debug(obj, path); +//TODO: Function to attach debug or coredump object to executable +// int adbg_object_load_debug(obj, path); //TODO: Consider structure definition, using a template // Uses: // - For swapping, uses less code than inlining it diff --git a/src/adbg/utils/bit.d b/src/adbg/utils/bit.d index 2caf8203..7bb69c83 100644 --- a/src/adbg/utils/bit.d +++ b/src/adbg/utils/bit.d @@ -99,7 +99,7 @@ ushort adbg_util_ensure16(ushort v, bool little) pure { /// Returns: Potentially swapped value. deprecated uint adbg_util_ensure32(uint v, bool little) pure { - return little == PLATFORM_LSB ? v : adbg_util_bswap32(v); + return little == PLATFORM_LSB ? v : adbg_bswap32(v); } /// Ensure endianness on 64-bit number. /// Params: diff --git a/src/adbg/utils/uid.d b/src/adbg/utils/uid.d index c04c4eef..d555ffff 100644 --- a/src/adbg/utils/uid.d +++ b/src/adbg/utils/uid.d @@ -91,10 +91,10 @@ int uid_string(ref UID uid, char *buf, size_t buflen, int target) { /// Swap endianness of a UID. GUID (LSB) becomes UUID (MSB) and vice-versa. /// Params: uid = UID structure void uid_swap(ref UID uid) { - uid.time_low = adbg_util_bswap32(uid.time_low); - uid.time_mid = adbg_util_bswap16(uid.time_mid); - uid.time_ver = adbg_util_bswap16(uid.time_ver); - uid.clock = adbg_util_bswap16(uid.clock); + uid.time_low = adbg_bswap32(uid.time_low); + uid.time_mid = adbg_bswap16(uid.time_mid); + uid.time_ver = adbg_bswap16(uid.time_ver); + uid.clock = adbg_bswap16(uid.clock); } @system unittest { UID uid;