Skip to content

Commit

Permalink
Deprecate older aliases for bswap
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Feb 16, 2024
1 parent d238f51 commit 0d47afc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 58 deletions.
35 changes: 17 additions & 18 deletions src/adbg/debugger/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
26 changes: 12 additions & 14 deletions src/adbg/legacy/server/macho.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
38 changes: 19 additions & 19 deletions src/adbg/object/format/macho.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/adbg/object/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/adbg/utils/bit.d
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/adbg/utils/uid.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0d47afc

Please sign in to comment.