Skip to content

Commit

Permalink
debugger: Make proper event setters
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Dec 25, 2024
1 parent 6fd8e2d commit 4b9da8a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
8 changes: 4 additions & 4 deletions debugger/shell.d
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ immutable(command2_t)* shell_findcommand(const(char) *ucommand) {

// After
int shell_setup() {
if (adbg_debugger_on(process, AdbgEvent.exception, &shell_event_exception) ||
adbg_debugger_on(process, AdbgEvent.processExit, &shell_event_process_exit) ||
adbg_debugger_on(process, AdbgEvent.processContinue, &shell_event_process_continue))
if (adbg_debugger_on_exception(process, &shell_event_exception) ||
adbg_debugger_on_process_exit(process, &shell_event_process_exit) ||
adbg_debugger_on_process_continue(process, &shell_event_process_continue))
return ShellError.alicedbg;

// Open disassembler for process machine type
Expand Down Expand Up @@ -663,7 +663,7 @@ void shell_event_exception(adbg_process_t *proc, void *udata, adbg_exception_t *
void shell_event_process_exit(adbg_process_t *proc, void *udata, int code) {
printf("* Process %d exited with code %d\n", adbg_process_id(proc), code);
}
void shell_event_process_continue(adbg_process_t *proc, void *udata, int tid) {
void shell_event_process_continue(adbg_process_t *proc, void *udata, long tid) {
printf("* Process %d continued\n", adbg_process_id(proc));
}

Expand Down
62 changes: 61 additions & 1 deletion src/adbg/debugger.d
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ private alias cbproccontinued = void function(adbg_process_t*, void*, long);
/// proc = Process instance. It must be spawned or attached by the debugger.
/// handler = Event handler. Setting it to `null` disables it, skipping it.
/// Returns: Error code.
deprecated("Use adbg_debugger_on_* functions")
int adbg_debugger_on(adbg_process_t *proc, AdbgEvent on, void *handler) {
if (proc == null)
return adbg_oops(AdbgError.invalidArgument);
Expand Down Expand Up @@ -634,9 +635,68 @@ int adbg_debugger_on(adbg_process_t *proc, AdbgEvent on, void *handler) {
return 0;
}

// NOTE: adbg_debugger_on_* advantages vs. adbg_debugger_on(enum)
// - callback type checking (when source compiling)
// - access to attributes (like `deprecated`)
// - no need to map/update enums
// - Better documentation solely for the event

/// Set an event handler for handling exceptions when they occur for
/// an attached process.
///
/// The callback will receive the active process (`adbg_process_t*`),
/// user data (`void*`), and the exception (`adbg_exception_t*`).
/// It must not return (`void`).
/// Params:
/// proc = Active process instance.
/// callback = Callback function. Set to null to disable.
/// Returns: Error code.
int adbg_debugger_on_exception(adbg_process_t *proc,
void function(adbg_process_t*, void*, adbg_exception_t*) callback) {
if (proc == null)
return adbg_oops(AdbgError.invalidArgument);
proc.event_exception = callback;
return 0;
}
/// Set an event handler to know when the attached process exits.
///
/// The callback will receive the active process (`adbg_process_t*`),
/// user data (`void*`), and the exit code (`int`).
/// It must not return (`void`).
/// Params:
/// proc = Active process instance.
/// callback = Callback function. Set to null to disable.
/// Returns: Error code.
int adbg_debugger_on_process_exit(adbg_process_t *proc,
void function(adbg_process_t*, void*, int) callback) {
if (proc == null)
return adbg_oops(AdbgError.invalidArgument);
proc.event_process_exited = callback;
return 0;
}
/// Set an event handler to know when the attached process continues.
///
/// This includes continue and step events.
///
/// The callback will receive the active process (`adbg_process_t*`),
/// user data (`void*`), and thread/job ID (`long`).
/// It must not return (`void`).
/// Params:
/// proc = Active process instance.
/// callback = Callback function. Set to null to disable.
/// Returns: Error code.
int adbg_debugger_on_process_continue(adbg_process_t *proc,
void function(adbg_process_t*, void*, long) callback) {
if (proc == null)
return adbg_oops(AdbgError.invalidArgument);
proc.event_process_continued = callback;
return 0;
}

/// Attach user data when an event occurs.
/// Useful to identify requests for example.
///
/// This is useful to identify requests, for example.
/// User data is sent to event callback functions.
/// Params:
/// proc = Process instance.
/// udata = User data pointer. Passing null clears it.
Expand Down

0 comments on commit 4b9da8a

Please sign in to comment.