Skip to content

Commit

Permalink
Make closing server clean again
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Aug 8, 2024
1 parent e98f9a7 commit f5e852f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
3 changes: 0 additions & 3 deletions source/adapters/dap.d
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,6 @@ class DAPAdapter : Adapter
case "disconnect":
// If launched, close debuggee.
// If attached, detach. Unless terminateDebuggee:true specified.
//
// Server should only understand closing, so send appropriate
// request type.
request.type = RequestType.close;
switch (processCreation) {
case RequestType.attach:
Expand Down
7 changes: 6 additions & 1 deletion source/adapters/mi.d
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,18 @@ class MIAdapter : Adapter
send(gdbString);
goto Lread;
}
AdapterRequest request;

// Recognized requests
string requestCommand = args[0];
RequestType *req = requestCommand in requests;

// Filter by recognized requests
AdapterRequest request;
if (req) switch (*req) {
/*
case RequestType.launch:
return request;
*/
case RequestType.attach:
if (args.length < 2)
{
Expand All @@ -187,6 +191,7 @@ class MIAdapter : Adapter
reply(AdapterError(format("Illegal process-id: '%s'.", args[1])));
goto Lread;
}

request.type = RequestType.attach;
return request;
case RequestType.currentWorkingDirectory:
Expand Down
8 changes: 3 additions & 5 deletions source/debuggers/alicedbg.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Alicedbg : IDebugger
{
process = adbg_debugger_spawn(exec.toStringz(), 0);
if (process == null)
throw new Exception("TODO");
throw new Exception(errorMessage());
}

void attach(int pid)
{
process = adbg_debugger_attach(pid, 0);
if (process == null)
throw new Exception("TODO");
throw new Exception(errorMessage());
}

void go()
Expand All @@ -39,10 +39,8 @@ class Alicedbg : IDebugger
private:
adbg_process_t *process;

/*
string errorMessage()
{
return fromStringz(adbg_error_message());
return cast(string)fromStringz(adbg_error_message());
}
*/
}
60 changes: 32 additions & 28 deletions source/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import std.concurrency;
import std.conv;
import std.string;
import core.thread;
import config;
import logging;
import adapters;
import transports;
import debuggers;
import adapters, debuggers;

// NOTE: Structure
//
Expand All @@ -26,6 +23,10 @@ import debuggers;
//
// Child thread handle their own debugger instance.
// (TODO) Attach debugger ID to requests.
//
// In general, the server understands close requests, but
// debuggers do not (their UI do, though). Debuggers only understand
// detach and terminate requests.

debug enum LogLevel DEFAULT_LOGLEVEL = LogLevel.trace;
else enum LogLevel DEFAULT_LOGLEVEL = LogLevel.info;
Expand Down Expand Up @@ -55,7 +56,7 @@ void startServer(Adapter adapter) // Handles adapter

// Get requests
logTrace("Listening...");
bool debuggerActive;
RequestType debuggerType;
Tid debuggerTid;
AdapterRequest request = void;
Lrequest:
Expand All @@ -72,7 +73,7 @@ Lrequest:
// Launch process with debugger
case RequestType.launch:
// TODO: Accept multi-session
if (debuggerActive)
if (debuggerType)
{
adapter.reply(AdapterError(messageDebuggerActive));
goto Lrequest;
Expand All @@ -89,12 +90,12 @@ Lrequest:
}

adapter.reply(AdapterReply());
debuggerActive = true;
debuggerType = RequestType.launch;
break;
// Attach debugger to process
case RequestType.attach:
// TODO: Accept multi-session
if (debuggerActive)
if (debuggerType)
{
adapter.reply(AdapterError(messageDebuggerActive));
goto Lrequest;
Expand All @@ -111,11 +112,11 @@ Lrequest:
}

adapter.reply(AdapterReply());
debuggerActive = true;
debuggerType = RequestType.attach;
break;
// Detach debugger from process
case RequestType.detach:
if (debuggerActive == false) // Nothing to detach from
if (debuggerType == RequestType.unknown) // Nothing to detach from
{
adapter.reply(AdapterError(messageDebuggerUnactive));
goto Lrequest;
Expand All @@ -131,11 +132,11 @@ Lrequest:
}

adapter.reply(AdapterReply());
debuggerActive = false;
debuggerType = RequestType.unknown;
break;
// Terminate process
case RequestType.terminate:
if (debuggerActive == false) // Nothing to terminate
if (debuggerType == RequestType.unknown) // Nothing to terminate
{
adapter.reply(AdapterError(messageDebuggerUnactive));
goto Lrequest;
Expand All @@ -151,22 +152,30 @@ Lrequest:
}

adapter.reply(AdapterReply());
debuggerActive = false;
debuggerType = RequestType.unknown;
break;
// Either detaches or terminates process depending how the debugger is attached
case RequestType.close:
if (debuggerActive && request.closeOptions.action != CloseAction.nothing)
static immutable Duration quitTimeout = 10.seconds;
switch (debuggerType) {
case RequestType.launch: // if was launched
send(debuggerTid, RequestTerminate());
break;
case RequestType.attach: // if was attached
send(debuggerTid, RequestDetach());
break;
default:
}

if (debuggerType)
{
logTrace("Sending debugger termination signal...");
send(debuggerTid, RequestQuit());

static immutable Duration quitTimeout = 5.seconds;
logTrace("Waiting for debugger to quit...");
logTrace("Waiting for debugger to quit (timeout: %s)...", quitTimeout);
if (receiveTimeout(quitTimeout, (MsgReply reply) {}) == false)
logWarn("Debugger did not reply in %s, quitting anyway", quitTimeout);
logWarn("Debugger timeout, quitting anyway");
}

// TODO: Multi-session: Return to listen to requests if adapterCount > 0
// And reset debuggerType.
adapter.close();
return;
default:
Expand All @@ -193,7 +202,6 @@ struct MsgReply

struct RequestDetach {}
struct RequestTerminate {}
struct RequestQuit {}

//TODO: Add configuration settings (mainly breakpoints) before starting
struct DebuggerStartOptions
Expand Down Expand Up @@ -279,18 +287,14 @@ void startDebugger(Tid parent, DebuggerStartOptions start) // Handles debugger
}
send(parent, MsgReply());
*/
send(parent, MsgReply());
active = false;
},
(RequestTerminate req) {
logTrace("Debugger: Terminating process...");
send(parent, MsgReply());
active = false;
},
(RequestQuit req) {
logTrace("Debugger: Closing debugger...");
send(parent, MsgReply());
active = false;
}
);

// Send terminating message
send(parent, MsgReply());
}

0 comments on commit f5e852f

Please sign in to comment.