Skip to content

Commit

Permalink
Rework Context API (+)
Browse files Browse the repository at this point in the history
- shell: Remove "regs" command
- shell: Add "registers" subcommand for "thread"
- Remove register module and merge it into thread module
- utils: Add adbg_list_clear
  • Loading branch information
dd86k committed Sep 14, 2024
1 parent 505ea03 commit 2d52f71
Show file tree
Hide file tree
Showing 6 changed files with 546 additions and 539 deletions.
136 changes: 52 additions & 84 deletions debugger/shell.d
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ __gshared:

adbg_process_t *process;
adbg_disassembler_t *dis;
adbg_registers_t *registers;

const(char)* last_spawn_exec;
const(char)** last_spawn_argv;
Expand Down Expand Up @@ -378,22 +377,6 @@ immutable command2_t[] shell_commands = [
&command_stepi,
},
//
// Context
//
{
[ "regs" ],
"Lists register values.",
[ "[NAME]" ],
MODULE_DEBUGGER, CATEGORY_CONTEXT,
[
{
SECTION_DESCRIPTION,
[ "Get list of registers and values from process." ]
}
],
&command_regs,
},
//
// Memory
//
{
Expand Down Expand Up @@ -476,7 +459,7 @@ immutable command2_t[] shell_commands = [
{
[ "t", "thread" ],
"Manage process threads.",
[ "ACTION" ],
[ "list", "TID registers" ],
MODULE_DEBUGGER, CATEGORY_PROCESS,
[
{ SECTION_DESCRIPTION,
Expand Down Expand Up @@ -797,8 +780,6 @@ int command_detach(int argc, const(char) **argv) {
return ShellError.alicedbg;
}
adbg_dis_close(dis);
free(registers);

return 0;
}

Expand Down Expand Up @@ -843,7 +824,6 @@ int command_kill(int argc, const(char) **argv) {
return ShellError.alicedbg;
puts("Process killed");
adbg_dis_close(dis);
free(registers);
return 0;
}

Expand All @@ -856,42 +836,6 @@ int command_stepi(int argc, const(char) **argv) {
return 0;
}

int command_regs(int argc, const(char) **argv) {
if (process == null)
return ShellError.pauseRequired;

if (registers == null) {
registers = adbg_registers_new(adbg_process_get_machine(process));
if (registers == null)
return ShellError.alicedbg;
}

adbg_registers_fill(registers, process);

if (registers.count == 0) {
logerror("No registers available");
return ShellError.unavailable;
}

adbg_register_t *reg = registers.items.ptr;
const(char) *rselect = argc >= 2 ? argv[1] : null;
bool found;
for (size_t i; i < registers.count; ++i, ++reg) {
bool show = rselect == null || strcmp(rselect, reg.info.name) == 0;
if (show == false) continue;
char[20] normal = void, hexdec = void;
adbg_register_format(normal.ptr, 20, reg, AdbgRegFormat.dec);
adbg_register_format(hexdec.ptr, 20, reg, AdbgRegFormat.hexPadded);
printf("%-8s 0x%8s %s\n", reg.info.name, hexdec.ptr, normal.ptr);
found = true;
}
if (rselect && found == false) {
logerror("Register not found");
return ShellError.invalidParameter;
}
return 0;
}

int command_memory(int argc, const(char) **argv) {
if (argc < 2) {
return ShellError.missingOption;
Expand Down Expand Up @@ -991,27 +935,15 @@ int command_disassemble(int argc, const(char) **argv) {
if (dis == null)
return ShellError.unavailable;

// Need address
if (argc < 2)
return ShellError.missingArgument;

long uaddress = void;
if (argc > 1) { // Address given
if (unformat64(&uaddress, argv[1]))
return ShellError.unformat;
} else { // Address not given, get PC/eIP value
//TODO: dedicated function to get PC/IP/EIP/RIP value
// HACK: register list should be allocated on-demand in global space
AdbgMachine mach = adbg_process_get_machine(process);
adbg_registers_t regs = void;
//if (adbg_registers_config(&regs, ))
// return ShellError.alicedbg;
if (adbg_registers_fill(&regs, process))
return ShellError.alicedbg;
// Assume EIP/RIP is in regs[0]
switch (mach) with (AdbgMachine) {
case i386: uaddress = regs.items[0].u32; break;
case amd64: uaddress = regs.items[0].u64; break;
default: return ShellError.unavailable;
}
}
if (unformat64(&uaddress, argv[1]))
return ShellError.unformat;

// Number of instruction, default to 10
int ucount = 10;
if (argc >= 3) {
if (unformat(&ucount, argv[2]))
Expand Down Expand Up @@ -1226,21 +1158,57 @@ int command_thread(int argc, const(char) **argv) {
if (argc < 2)
return ShellError.missingArgument;

adbg_thread_t *thread = void;

const(char) *action = argv[1];
// thread list - get a list of threads
if (strcmp(action, "list") == 0) {
void *list = adbg_thread_list(process);
if (list == null)
if (adbg_thread_list_update(process))
return ShellError.alicedbg;

puts("Threads:");
int tid = void;
for (size_t i; (tid = adbg_thread_list_get(list, i)) != 0; ++i)
printf("%d\n", tid);
size_t i;
while ((thread = adbg_thread_list_get(process, i++)) != null) {
printf("%*d\n", -10, adbg_thread_id(thread));
}
return 0;
}

// Else: thread TID subcommand
if (argc < 3) // thread id
return ShellError.missingArgument;

// Select thread
int tid = atoi(argv[1]);
size_t i;
adbg_thread_t *selected;
while ((thread = adbg_thread_list_get(process, i++)) != null) {
if (adbg_thread_id(thread) == tid) {
selected = thread;
break;
}
}
if (selected == null)
return ShellError.alicedbg;

action = argv[2];
if (strcmp(action, "registers") == 0) {
// Update its context
adbg_thread_context_update(process, selected);

adbg_thread_list_free(list);
} else {
i = 0;
adbg_register_t *register = void;
while ((register = adbg_register_get(thread, i++)) != null) {
char[20] dec = void, hex = void;
adbg_register_format(dec.ptr, 20, register, AdbgRegisterFormat.dec);
adbg_register_format(hex.ptr, 20, register, AdbgRegisterFormat.hexPadded);
printf("%*s 0x%*s %s\n",
-8, adbg_register_name(register),
8, hex.ptr,
dec.ptr);
}
} else
return ShellError.invalidParameter;
}

return 0;
}
Expand Down
23 changes: 12 additions & 11 deletions src/adbg/process/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
/// License: BSD-3-Clause-Clear
module adbg.process.base;

//TODO: Process Pause/Resume
// Windows: NtSuspendProcess/NtResumeProcess or SuspendThread/ResumeThread
// Linux: Send SIGSTOP/SIGCONT signals via kill(2)
//TODO: List threads of process (maybe in a module called threading.d)
// TODO: Process Pause/Resume
// Windows: NtSuspendProcess/NtResumeProcess or SuspendThread/ResumeThread
// Linux: Send SIGSTOP/SIGCONT signals via kill(2)
// TODO: List threads of process (maybe in a module called threading.d)

import adbg.include.c.stdlib; // malloc, calloc, free, exit;
import adbg.include.c.stdarg;
import adbg.error;
import adbg.utils.strings;
import adbg.process.exception : adbg_exception_t, adbg_exception_translate;
import adbg.machines;
import adbg.utils.strings;
import adbg.utils.list : list_t, adbg_list_free;
import core.stdc.string;

version (Windows) {
Expand Down Expand Up @@ -68,13 +69,13 @@ enum ADBG_PROCESS_NAME_LENGTH = 256;
struct adbg_process_t {
version (Windows) { // Original identifiers; Otherwise informal
int pid; /// Process identificiation number
int tid; /// Thread identification number
deprecated int tid; /// Thread identification number
HANDLE hpid; /// Process handle
HANDLE htid; /// Thread handle
deprecated HANDLE htid; /// Thread handle
char *args; /// Saved arguments when process was launched
// TODO: Deprecate and remove wow64 field
// Make function to query process machine type
version (Win64) int wow64;
version (Win64) deprecated int wow64;
}
version (Posix) {
pid_t pid; /// Process ID
Expand All @@ -88,6 +89,8 @@ struct adbg_process_t {
AdbgProcStatus status;
/// Process' creation source.
AdbgCreation creation;
/// List of threads
list_t *thread_list;
//TODO: Deprecate and remove static buffer in process struct
/// Process base module name.
char[ADBG_PROCESS_NAME_LENGTH] name;
Expand All @@ -102,6 +105,7 @@ void adbg_process_free(adbg_process_t *proc) {
version (Posix) {
if (proc.argv) free(proc.argv);
}
adbg_list_free(proc.thread_list);
free(proc);
}

Expand Down Expand Up @@ -283,9 +287,6 @@ version (Windows) {
return 0;
}
}
unittest {
//TODO: Test one character buffers
}

/// Get the current runtime machine platform.
///
Expand Down
1 change: 0 additions & 1 deletion src/adbg/process/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module adbg.process;

public import
adbg.process.base,
adbg.process.register,
adbg.process.exception,
adbg.process.breakpoint,
adbg.process.memory,
Expand Down
Loading

0 comments on commit 2d52f71

Please sign in to comment.