Skip to content

Commit

Permalink
Update Alicedbg
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Nov 3, 2024
1 parent 53909c3 commit 0b81a6f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors "dd86k <dd@dax.moe>"
copyright "Copyright © 2024, dd86k <dd@dax.moe>"
license "BSD-3-Clause-Clear"

dependency "alicedbg" repository="git+https://github.com/dd86k/alicedbg.git" version="3098e250232e248f6376d444ec656c7639694466"
dependency "alicedbg" repository="git+https://github.com/dd86k/alicedbg.git" version="c6ad0bbefbeeb4ca17d3deedf7324bb53feb0235"
dependency "ddlogger" repository="git+https://github.com/dd86k/ddlogger.git" version="99a077a599c6d56e4fdeefb8b5d5002620cd228f"

# NOTE: By default, docs are built with dependencies.
Expand Down
80 changes: 52 additions & 28 deletions source/debugger/alicedbg.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,39 @@ class AliceDebugger : IDebugger
process = adbg_debugger_spawn(exec.toStringz(), 0);
if (process == null)
throw new AlicedbgException();
_configure();
}

void attach(int pid)
{
process = adbg_debugger_attach(pid, 0);
if (process == null)
throw new AlicedbgException();
_configure();
}

private
void _configure()
{
adbg_debugger_on(process, AdbgEvent.exception, &adbgEventException);
adbg_debugger_on(process, AdbgEvent.processExit, &adbgEventExited);
adbg_debugger_on(process, AdbgEvent.processContinue, &adbgEventContinued);
adbg_debugger_udata(process, &event);
}

void continue_()
{
enforceActiveProcess();
if (adbg_debugger_continue(process))
throw new AlicedbgException();
// HACK: To allow continuing from a previous event
switch (event.type) {
case AdapterEventType.stopped:
if (adbg_debugger_continue(process, event.stopped.threadId))
throw new AlicedbgException();
break;
default:
throw new Exception("Not in a stopped state");
}
_configure();
}

void terminate()
Expand All @@ -67,14 +86,16 @@ class AliceDebugger : IDebugger
{
enforceActiveProcess();
AdapterEvent event = void;
if (adbg_debugger_wait(process, &handleAdbgEvent, &event))
if (adbg_debugger_wait(process))
throw new AlicedbgException();
return event;
}

private:
/// Current process.
adbg_process_t *process;
/// Last adapter event.
AdapterEvent event;

// Actively check if we have an active process.
// Otherwise, Alicedbg would complain about an invalid handle, which
Expand All @@ -92,10 +113,9 @@ private:
string adbgExceptionName(adbg_exception_t *ex)
{
switch (ex.type) with (AdbgException) {
case Exit: return "Exit";
case Breakpoint: return "Breakpoint";
case Step: return "Step";
case Fault: return "Fault";
case AccessViolation: return "Access Violation";
case BoundExceeded: return "Bound Exceeded";
case Misalignment: return "Misalignment";
case IllegalInstruction:return "Illegal Instruction";
Expand All @@ -111,8 +131,6 @@ string adbgExceptionName(adbg_exception_t *ex)
case FPUOverflow: return "FPU Overflow";
case FPUUnderflow: return "FPU Underflow";
case FPUStackOverflow: return "FPU StackOverflow";
case Disposition: return "Disposition";
case NoContinue: return "No Continue";
default: return null;
}
}
Expand All @@ -126,27 +144,33 @@ AdapterEventStoppedReason adbgExceptionReason(adbg_exception_t *ex) {
}
}

// Handle Alicedbg events
// Handle exceptions
extern (C)
void handleAdbgEvent(adbg_process_t *proc, int type, void *edata, void *udata)
void adbgEventException(adbg_process_t *proc, void *udata, adbg_exception_t *exception)
{
AdapterEvent *event = cast(AdapterEvent*)udata;
switch (type) {
case AdbgEvent.exception:
event.type = AdapterEventType.stopped;
adbg_exception_t *ex = cast(adbg_exception_t*)edata;
event.stopped.reason = adbgExceptionReason(ex);
event.stopped.text = adbgExceptionName(ex);
event.stopped.description = "Exception";
// TODO: Assign Thread ID once Alicedbg gets TID association
event.stopped.threadId = 0;
return;
case AdbgEvent.processExit:
event.type = AdapterEventType.exited;
int *code = cast(int*)edata;
event.exited.code = *code;
return;
default:
// ...
}
}
event.type = AdapterEventType.stopped;
event.stopped.reason = adbgExceptionReason(exception);
event.stopped.text = adbgExceptionName(exception);
event.stopped.description = "Exception";
event.stopped.threadId = cast(int)adbg_exception_tid(exception);
}

// Handle continuations
extern (C)
void adbgEventContinued(adbg_process_t *proc, void *udata)
{
AdapterEvent *event = cast(AdapterEvent*)udata;
event.type = AdapterEventType.continued;
// TODO: Assign Thread ID once Alicedbg gets better TID association
event.continued.threadId = adbg_process_id(proc);
}

// Handle exits
extern (C)
void adbgEventExited(adbg_process_t *proc, void *udata, int code)
{
AdapterEvent *event = cast(AdapterEvent*)udata;
event.type = AdapterEventType.exited;
event.exited.code = code;
}

0 comments on commit 0b81a6f

Please sign in to comment.