Skip to content

Commit

Permalink
More WIP, debugger starting correctly. Tested several commands and al…
Browse files Browse the repository at this point in the history
…l working.
  • Loading branch information
SpartanJ committed Dec 28, 2024
1 parent d07bb25 commit bbfcc74
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 160 deletions.
3 changes: 2 additions & 1 deletion bin/assets/plugins/debugger.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"args": "${args}",
"cwd": "${cwd}",
"env": "${env}",
"stopOnEntry": "${stopOnEntry}"
"stopOnEntry": "${stopOnEntry}",
"stopAtBeginningOfMainSubprogram": false
}
}
]
Expand Down
2 changes: 2 additions & 0 deletions include/eepp/system/process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class EE_API Process {

typedef std::function<void( const char* bytes, size_t n )> ReadFn;

static std::vector<std::string> parseArgs( const std::string& str );

Process();

/** @brief Create a process.
Expand Down
2 changes: 1 addition & 1 deletion src/eepp/system/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static bool isFlatpakEnv() {

#define PROCESS_PTR ( static_cast<struct subprocess_s*>( mProcess ) )

static std::vector<std::string> parseArgs( const std::string& str ) {
std::vector<std::string> Process::parseArgs( const std::string& str ) {
bool inquote = false;
char quoteChar = 0;
std::vector<std::string> res;
Expand Down
4 changes: 4 additions & 0 deletions src/eepp/ui/uiconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ void UIConsole::setMaxLogLines( const Uint32& maxLogLines ) {

void UIConsole::privPushText( String&& str ) {
Lock l( mMutex );
if ( str.find_first_of( '\r' ) != String::InvalidPos )
String::replaceAll( str, "\r", "" );
if ( str.empty() )
return;
mCmdLog.push_back( { std::move( str ), String::hash( str ) } );
if ( mVisible )
invalidateDraw();
Expand Down
2 changes: 2 additions & 0 deletions src/tools/ecode/plugins/debugger/bus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Bus {

virtual size_t write( const char* buffer, const size_t& size ) = 0;

virtual ~Bus() {}

protected:
void setState( State state );

Expand Down
56 changes: 47 additions & 9 deletions src/tools/ecode/plugins/debugger/dap/debuggerclientdap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ DebuggerClientDap::DebuggerClientDap( const ProtocolSettings& protocolSettings,
std::unique_ptr<Bus>&& bus ) :
mBus( std::move( bus ) ), mProtocol( protocolSettings ) {}

DebuggerClientDap::~DebuggerClientDap() {
mBus.reset();
}

void DebuggerClientDap::makeRequest( const std::string_view& command,
const nlohmann::json& arguments, ResponseHandler onFinish ) {
nlohmann::json jsonCmd = {
Expand All @@ -31,10 +35,12 @@ void DebuggerClientDap::makeRequest( const std::string_view& command,
{ "arguments", arguments.empty() ? nlohmann::json::object() : arguments } };

std::string cmd = jsonCmd.dump();
std::string msg( String::format( "Content-Length: %zu\r\n\r\n%s", cmd.size(), cmd ) );

Log::instance()->writel( mDebug ? LogLevel::Info : LogLevel::Debug, cmd );
Log::instance()->writel( mDebug ? LogLevel::Info : LogLevel::Debug,
"DebuggerClientDap::makeRequest:" );
Log::instance()->writel( mDebug ? LogLevel::Info : LogLevel::Debug, msg );

std::string msg( String::format( "Content-Length: %zu\r\n\r\n%s", cmd.size(), cmd ) );
mBus->write( msg.data(), msg.size() );

mRequests[mIdx] = { std::string{ command }, arguments, onFinish };
Expand Down Expand Up @@ -113,9 +119,9 @@ void DebuggerClientDap::requestLaunchCommand() {
[this]( const Response& response, const auto& ) {
if ( response.success ) {
mLaunched = true;
checkRunning();
for ( auto listener : mListeners )
listener->launched();
checkRunning();
} else {
if ( response.errorBody ) {
Log::warning( "DebuggerClientDap::requestLaunchCommand: error %ld %s",
Expand All @@ -131,7 +137,7 @@ void DebuggerClientDap::requestInitialize() {
{ DAP_CLIENT_ID, "ecode-dap" },
{ DAP_CLIENT_NAME, "ecode dap" },
{ "locale", mProtocol.locale },
{ DAP_ADAPTER_ID, "qdap" },
{ DAP_ADAPTER_ID, "ecode-dap" },
{ DAP_LINES_START_AT1, mProtocol.linesStartAt1 },
{ DAP_COLUMNS_START_AT2, mProtocol.columnsStartAt1 },
{ DAP_PATH, ( mProtocol.pathFormatURI ? DAP_URI : DAP_PATH ) },
Expand Down Expand Up @@ -166,8 +172,10 @@ void DebuggerClientDap::asyncRead( const char* bytes, size_t n ) {
#endif
auto message = json::parse( data );

if ( mDebug )
if ( mDebug ) {
Log::debug( "DebuggerClientDap::asyncRead:" );
Log::debug( message.dump() );
}

processProtocolMessage( message );
#ifndef EE_DEBUG
Expand Down Expand Up @@ -229,7 +237,7 @@ void DebuggerClientDap::errorResponse( const std::string& summary,

void DebuggerClientDap::processEvent( const nlohmann::json& msg ) {
const std::string event = msg.value( DAP_EVENT, "" );
const auto body = msg[DAP_BODY];
const auto body = msg.contains( DAP_BODY ) ? msg[DAP_BODY] : nlohmann::json{};

if ( "initialized"sv == event ) {
processEventInitialized();
Expand Down Expand Up @@ -262,6 +270,8 @@ void DebuggerClientDap::processEventInitialized() {
return;
}
setState( State::Initialized );

configurationDone();
}

void DebuggerClientDap::processEventTerminated() {
Expand Down Expand Up @@ -337,7 +347,8 @@ std::optional<DebuggerClientDap::HeaderInfo> DebuggerClientDap::readHeader() {
}

const auto header = mBuffer.substr( start, end - start );
end += DAP_SEP_SIZE;
while ( std::string_view{ mBuffer }.substr( end, 2 ) == DAP_SEP )
end += DAP_SEP_SIZE;

// header block separator
if ( header.size() == 0 ) {
Expand All @@ -362,18 +373,19 @@ std::optional<DebuggerClientDap::HeaderInfo> DebuggerClientDap::readHeader() {
if ( String::startsWith( header, DAP_CONTENT_LENGTH ) ) {
std::string lengthStr( header.substr( sep + 1, header.size() - sep ) );
String::trimInPlace( lengthStr );
Uint64 length;
if ( !String::fromString( length, lengthStr ) ) {
Log::error( "DebuggerClientDap::readHeader invalid value: ", header );
discardExploredBuffer();
continue; // CONTINUE HEADER
} else {
break;
}
}

start = end;
}

if ( length < 0 )
if ( length < 0 || length == std::string::npos )
return std::nullopt;

return HeaderInfo{ end, length };
Expand Down Expand Up @@ -683,4 +695,30 @@ bool DebuggerClientDap::watch( const std::string& expression, std::optional<int>
return evaluate( expression, "watch", frameId );
}

bool DebuggerClientDap::configurationDone() {
if ( mState != State::Initialized ) {
Log::warning( "DebuggerClientDap::requestConfigurationDone: trying to configure in an "
"unexpected status" );
return false;
}

if ( !mAdapterCapabilities.supportsConfigurationDoneRequest ) {
for ( auto listener : mListeners )
listener->configured();
return true;
}

makeRequest( "configurationDone", nlohmann::json{},
[this]( const auto& response, const auto& ) {
if ( response.success ) {
mConfigured = true;
checkRunning();
for ( auto listener : mListeners )
listener->configured();
}
} );

return true;
}

} // namespace ecode::dap
10 changes: 8 additions & 2 deletions src/tools/ecode/plugins/debugger/dap/debuggerclientdap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class DebuggerClientDap : public DebuggerClient {

DebuggerClientDap( const ProtocolSettings& protocolSettings, std::unique_ptr<Bus>&& bus );

virtual ~DebuggerClientDap();

bool start() override;

bool resume( int threadId, bool singleThread = false ) override;
Expand All @@ -38,11 +40,12 @@ class DebuggerClientDap : public DebuggerClient {

bool threads() override;

bool stackTrace( int threadId, int startFrame, int levels ) override;
bool stackTrace( int threadId, int startFrame = 0, int levels = 0 ) override;

bool scopes( int frameId ) override;

bool variables( int variablesReference, Variable::Type filter, int start, int count ) override;
bool variables( int variablesReference, Variable::Type filter = Variable::Type::Both,
int start = 0, int count = 0 ) override;

bool modules( int start, int count ) override;

Expand All @@ -69,6 +72,8 @@ class DebuggerClientDap : public DebuggerClient {

bool watch( const std::string& expression, std::optional<int> frameId ) override;

bool configurationDone() override;

protected:
std::unique_ptr<Bus> mBus;
UnorderedMap<std::string, UnorderedSet<int>> mBreakpoints;
Expand Down Expand Up @@ -131,6 +136,7 @@ class DebuggerClientDap : public DebuggerClient {
void requestLaunchCommand();

void processResponseInitialize( const Response& response, const nlohmann::json& );

void processResponseNext( const Response& response, const nlohmann::json& request );
};

Expand Down
Loading

0 comments on commit bbfcc74

Please sign in to comment.