Skip to content

Commit

Permalink
debugger: Add adbg_debugger_udata
Browse files Browse the repository at this point in the history
Because user data shouldn't only be set when waiting specifically, but on a per-process basis
  • Loading branch information
dd86k committed Oct 21, 2024
1 parent c22e2b9 commit 925d351
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions debugger/shell.d
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ int command_restart(int argc, const(char) **argv) {
int command_go(int argc, const(char) **argv) {
if (adbg_debugger_continue(process))
return ShellError.alicedbg;
if (adbg_debugger_wait(process, null))
if (adbg_debugger_wait(process))
return ShellError.alicedbg;
return 0;
}
Expand All @@ -807,7 +807,7 @@ int command_kill(int argc, const(char) **argv) {
int command_stepi(int argc, const(char) **argv) {
if (adbg_debugger_stepi(process))
return ShellError.alicedbg;
if (adbg_debugger_wait(process, null))
if (adbg_debugger_wait(process))
return ShellError.alicedbg;
return 0;
}
Expand Down
28 changes: 18 additions & 10 deletions src/adbg/debugger.d
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,20 @@ int adbg_debugger_on(adbg_process_t *proc, AdbgEvent on, void *handler) {
return 0;
}

/// Attach user data when an event occurs.
///
/// This is useful to identify requests, for example.
/// Params:
/// proc = Process instance.
/// udata = User data pointer. Passing null clears it.
/// Returns: Error code.
int adbg_debugger_udata(adbg_process_t *proc, void *udata) {
if (proc == null)
return adbg_oops(AdbgError.invalidArgument);
proc.udata = udata;
return 0;
}

/// Wait until a new debug event occurs. This call is blocking.
///
/// The lifetime of the event callback parameters are not guaranteed.
Expand All @@ -650,11 +664,9 @@ int adbg_debugger_on(adbg_process_t *proc, AdbgEvent on, void *handler) {
/// Windows: Uses WaitForDebugEvent.
/// POSIX: Uses waitpid(2) and ptrace(2).
///
/// Params:
/// proc = Tracee instance.
/// udata = User data passed to callback. Can be used to identify requests, for example.
/// Params: proc = Process instancied by the debugger.
/// Returns: Error code.
int adbg_debugger_wait(adbg_process_t *proc, void *udata) {
int adbg_debugger_wait(adbg_process_t *proc) {
version(Trace) trace("proc=%p udata=%p", proc, udata);

if (proc == null)
Expand All @@ -670,10 +682,6 @@ Lwait:
return adbg_oops(AdbgError.os);
}

// In case either this or caller process is going to be used
// to send the event, put it in both, just in case.
proc.udata = udata;

proc.pid = de.dwProcessId;
proc.tid = de.dwThreadId;

Expand All @@ -699,7 +707,7 @@ Lwait:

adbg_exception_t exception = void;
adbg_translate_exception(&exception, proc, &de);
proc.event_exception(proc, udata, &exception);
proc.event_exception(proc, proc.udata, &exception);
break;
case EXIT_PROCESS_DEBUG_EVENT:
version(Trace) trace("ProcExit pid=%d tid=%d code=%u",
Expand All @@ -710,7 +718,7 @@ Lwait:
if (proc.event_process_exited == null)
goto Lcontinue;

proc.event_process_exited(proc, udata, cast(int)de.ExitProcess.dwExitCode);
proc.event_process_exited(proc, proc.udata, cast(int)de.ExitProcess.dwExitCode);
break;
/*case CREATE_THREAD_DEBUG_EVENT:
case CREATE_PROCESS_DEBUG_EVENT:
Expand Down
2 changes: 1 addition & 1 deletion src/adbg/include/windows/winnt.d
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ version (AArch64) { // defined(_ARM64_)
alias PNEON128 = NEON128*;
}

// NOTE: It's the same layout
// NOTE: It's the same layout, for now
public alias WOW64_CONTEXT = X86_NT_CONTEXT;
alias PWOW64_CONTEXT = WOW64_CONTEXT*;

Expand Down
1 change: 1 addition & 0 deletions src/adbg/objects/pe.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import adbg.utils.math : min, MiB;
import adbg.objects.mz : mz_header_t;

// NOTE: Avoid the Windows base types as they are not defined outside "version (Windows)"
// NOTE: PE32 loader limits executable images to 4 GiB

extern (C):

Expand Down
11 changes: 7 additions & 4 deletions src/adbg/utils/list.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import core.stdc.stdlib : malloc, calloc, realloc, free;
import core.stdc.string : memcpy;
import adbg.error : adbg_oops, AdbgError;

// TODO: `find` function
// Using memcmp.
// TODO: `empty` function
// Clear *and* free memory (or realloc to initial capacity

extern (C):

struct list_t {
size_t capacity; /// Capacity in number of items
size_t capacity; /// Currently capacity in number of items
size_t itemsize; /// Size of one item
size_t count; /// Current item count
void *buffer; /// Item buffer
Expand Down Expand Up @@ -66,6 +71,7 @@ list_t* adbg_list_reserve(list_t *list, size_t newcapacity) {
assert(list.buffer);
assert(list.itemsize);

// The new capacity is already the current capacity, do nothing
if (newcapacity == list.capacity)
return list;

Expand Down Expand Up @@ -100,7 +106,6 @@ void* adbg_list_buffer(list_t *list) {
return null;
}
assert(list.buffer);
assert(list.itemsize);
return list.buffer;
}
/// Get the list's current capacity.
Expand All @@ -113,8 +118,6 @@ size_t adbg_list_capacity(list_t *list) {
adbg_oops(AdbgError.invalidArgument);
return 0;
}
assert(list.buffer);
assert(list.itemsize);
return list.capacity;
}

Expand Down

0 comments on commit 925d351

Please sign in to comment.