More WIP, debugger starting correctly. Tested several commands and all working.

This commit is contained in:
Martín Lucas Golini
2024-12-28 20:31:33 -03:00
parent d07bb2572c
commit bbfcc74977
16 changed files with 349 additions and 161 deletions

View File

@@ -1,5 +1,5 @@
#include "messages.hpp"
#include "protocol.hpp"
#include "messages.hpp"
#include <eepp/core/core.hpp>
#include <eepp/system/fileinfo.hpp>
#include <eepp/system/filesystem.hpp>
@@ -7,40 +7,46 @@
using namespace EE;
using namespace EE::System;
std::optional<int> parseOptionalInt( const json& value ) {
if ( value.is_null() || value.empty() || !value.is_number() ) {
std::optional<int> parseOptionalInt( const json& value, const std::string_view& name ) {
if ( !value.contains( name ) || value[name].is_null() || value[name].empty() ||
!value[name].is_number() ) {
return std::nullopt;
}
return value.get<int>();
return value[name].get<int>();
}
std::optional<bool> parseOptionalBool( const json& value ) {
if ( value.is_null() || value.empty() || !value.is_boolean() ) {
std::optional<bool> parseOptionalBool( const json& value, const std::string_view& name ) {
if ( !value.contains( name ) || value[name].is_null() || value[name].empty() ||
!value[name].is_boolean() ) {
return std::nullopt;
}
return value.get<bool>();
return value[name].get<bool>();
}
std::optional<std::string> parseOptionalString( const json& value ) {
if ( value.is_null() || value.empty() || !value.is_string() ) {
std::optional<std::string> parseOptionalString( const json& value, const std::string_view& name ) {
if ( !value.contains( name ) || value[name].is_null() || value[name].empty() ||
!value[name].is_string() ) {
return std::nullopt;
}
return value.get<std::string>();
return value[name].get<std::string>();
}
template <typename T> std::optional<T> parseOptionalObject( const json& value ) {
if ( value.is_null() || value.empty() || !value.is_object() ) {
template <typename T>
std::optional<T> parseOptionalObject( const json& value, const std::string_view& name ) {
if ( !value.contains( name ) || value[name].is_null() || value[name].empty() ||
!value[name].is_object() ) {
return std::nullopt;
}
return T( value );
return T( value[name] );
}
std::optional<std::unordered_map<std::string, std::string>>
parseOptionalStringMap( const json& value ) {
if ( value.is_null() || value.empty() || !value.is_object() ) {
parseOptionalStringMap( const json& value, const std::string_view& name ) {
if ( !value.contains( name ) || value[name].is_null() || value[name].empty() ||
!value[name].is_object() ) {
return std::nullopt;
}
const auto& dict = value;
const auto& dict = value[name];
std::unordered_map<std::string, std::string> map;
for ( auto it = dict.begin(); it != dict.end(); ++it ) {
map[it.key()] = it.value().get<std::string>();
@@ -56,12 +62,14 @@ template <typename T> std::vector<T> parseObjectList( const json& array ) {
return out;
}
std::optional<std::vector<int>> parseOptionalIntList( const json& value ) {
if ( value.is_null() || value.empty() || !value.is_array() ) {
std::optional<std::vector<int>> parseOptionalIntList( const json& value,
const std::string_view& name ) {
if ( !value.contains( name ) || value[name].is_null() || value[name].empty() ||
!value[name].is_array() ) {
return std::nullopt;
}
std::vector<int> values;
for ( const auto& item : value ) {
for ( const auto& item : value[name] ) {
values.push_back( item.get<int>() );
}
return values;
@@ -80,19 +88,19 @@ namespace ecode::dap {
Message::Message( const json& body ) :
id( body[DAP_ID].get<int>() ),
format( body["format"].get<std::string>() ),
variables( parseOptionalStringMap( body["variables"] ) ),
sendTelemetry( parseOptionalBool( body["sendTelemetry"] ) ),
showUser( parseOptionalBool( body["showUser"] ) ),
url( parseOptionalString( body["url"] ) ),
urlLabel( parseOptionalString( body["urlLabel"] ) ) {}
variables( parseOptionalStringMap( body, "variables" ) ),
sendTelemetry( parseOptionalBool( body, "sendTelemetry" ) ),
showUser( parseOptionalBool( body, "showUser" ) ),
url( parseOptionalString( body, "url" ) ),
urlLabel( parseOptionalString( body, "urlLabel" ) ) {}
Response::Response( const json& msg ) :
request_seq( msg.value( "request_seq", -1 ) ),
success( msg.value( "success", false ) ),
command( msg.value( DAP_COMMAND, "" ) ),
message( msg.value( "message", "" ) ),
body( msg[DAP_BODY] ),
errorBody( success ? std::nullopt : parseOptionalObject<Message>( body["error"] ) ) {}
body( msg.contains( DAP_BODY ) ? msg[DAP_BODY] : nlohmann::json{} ),
errorBody( success ? std::nullopt : parseOptionalObject<Message>( body, "error" ) ) {}
bool Response::isCancelled() const {
return message == "cancelled";
@@ -100,20 +108,20 @@ bool Response::isCancelled() const {
ProcessInfo::ProcessInfo( const json& body ) :
name( body.value( DAP_NAME, "" ) ),
systemProcessId( parseOptionalInt( body[DAP_SYSTEM_PROCESS_ID] ) ),
isLocalProcess( parseOptionalBool( body[DAP_IS_LOCAL_PROCESS] ) ),
startMethod( parseOptionalString( body[DAP_START_METHOD] ) ),
pointerSize( parseOptionalInt( body[DAP_POINTER_SIZE] ) ) {}
systemProcessId( parseOptionalInt( body, DAP_SYSTEM_PROCESS_ID ) ),
isLocalProcess( parseOptionalBool( body, DAP_IS_LOCAL_PROCESS ) ),
startMethod( parseOptionalString( body, DAP_START_METHOD ) ),
pointerSize( parseOptionalInt( body, DAP_POINTER_SIZE ) ) {}
Output::Output( const json& body ) :
category( Category::Unknown ),
output( body.value( DAP_OUTPUT, "" ) ),
group( std::nullopt ),
variablesReference( parseOptionalInt( body[DAP_VARIABLES_REFERENCE] ) ),
source( parseOptionalObject<Source>( DAP_SOURCE ) ),
line( parseOptionalInt( body[DAP_LINE] ) ),
column( parseOptionalInt( body[DAP_COLUMN] ) ),
data( body[DAP_DATA] ) {
variablesReference( parseOptionalInt( body, DAP_VARIABLES_REFERENCE ) ),
source( parseOptionalObject<Source>( body, DAP_SOURCE ) ),
line( parseOptionalInt( body, DAP_LINE ) ),
column( parseOptionalInt( body, DAP_COLUMN ) ),
data( body.contains( DAP_DATA ) ? body[DAP_DATA] : nlohmann::json{} ) {
if ( body.contains( DAP_GROUP ) ) {
const auto value = body[DAP_GROUP].get<std::string>();
if ( DAP_START == value ) {
@@ -159,12 +167,12 @@ std::string Source::getUnifiedId( const std::string& path, std::optional<int> so
}
Source::Source( const json& body ) :
name( body[DAP_NAME].get<std::string>() ),
path( body[DAP_PATH].get<std::string>() ),
sourceReference( parseOptionalInt( body[DAP_SOURCE_REFERENCE] ) ),
presentationHint( parseOptionalString( body[DAP_PRESENTATION_HINT] ) ),
origin( body[DAP_ORIGIN].get<std::string>() ),
adapterData( body[DAP_ADAPTER_DATA] ) {
name( body.value( DAP_NAME, "" ) ),
path( body.value( DAP_PATH, "" ) ),
sourceReference( parseOptionalInt( body, DAP_SOURCE_REFERENCE ) ),
presentationHint( parseOptionalString( body, DAP_PRESENTATION_HINT ) ),
origin( body.value( DAP_ORIGIN, "" ) ),
adapterData( body.contains( DAP_ADAPTER_DATA ) ? body[DAP_ADAPTER_DATA] : nlohmann::json{} ) {
// sources
if ( body.contains( DAP_SOURCES ) ) {
const auto values = body[DAP_SOURCES];
@@ -225,27 +233,27 @@ json Checksum::toJson() const {
}
Capabilities::Capabilities( const json& body ) :
supportsConfigurationDoneRequest( body["supportsConfigurationDoneRequest"].get<bool>() ),
supportsFunctionBreakpoints( body["supportsFunctionBreakpoints"].get<bool>() ),
supportsConditionalBreakpoints( body["supportsConditionalBreakpoints"].get<bool>() ),
supportsHitConditionalBreakpoints( body["supportsHitConditionalBreakpoints"].get<bool>() ),
supportsLogPoints( body["supportsLogPoints"].get<bool>() ),
supportsModulesRequest( body["supportsModulesRequest"].get<bool>() ),
supportsTerminateRequest( body["supportsTerminateRequest"].get<bool>() ),
supportTerminateDebuggee( body["supportTerminateDebuggee"].get<bool>() ),
supportsGotoTargetsRequest( body["supportsGotoTargetsRequest"].get<bool>() ) {}
supportsConfigurationDoneRequest( body.value( "supportsConfigurationDoneRequest", false ) ),
supportsFunctionBreakpoints( body.value( "supportsFunctionBreakpoints", false ) ),
supportsConditionalBreakpoints( body.value( "supportsConditionalBreakpoints", false ) ),
supportsHitConditionalBreakpoints( body.value( "supportsHitConditionalBreakpoints", false ) ),
supportsLogPoints( body.value( "supportsLogPoints", false ) ),
supportsModulesRequest( body.value( "supportsModulesRequest", false ) ),
supportsTerminateRequest( body.value( "supportsTerminateRequest", false ) ),
supportTerminateDebuggee( body.value( "supportTerminateDebuggee", false ) ),
supportsGotoTargetsRequest( body.value( "supportsGotoTargetsRequest", false ) ) {}
ThreadEvent::ThreadEvent( const json& body ) :
reason( body[DAP_REASON].get<std::string>() ), threadId( body[DAP_THREAD_ID].get<int>() ) {}
StoppedEvent::StoppedEvent( const json& body ) :
reason( body[DAP_REASON].get<std::string>() ),
description( parseOptionalString( body["description"] ) ),
description( parseOptionalString( body, "description" ) ),
threadId( body[DAP_THREAD_ID].get<int>() ),
preserveFocusHint( parseOptionalBool( body["preserveFocusHint"] ) ),
text( parseOptionalString( body["text"] ) ),
allThreadsStopped( parseOptionalBool( body["allThreadsStopped"] ) ),
hitBreakpointsIds( parseOptionalIntList( body["hitBreakpointsIds"] ) ) {}
preserveFocusHint( parseOptionalBool( body, "preserveFocusHint" ) ),
text( parseOptionalString( body, "text" ) ),
allThreadsStopped( parseOptionalBool( body, "allThreadsStopped" ) ),
hitBreakpointsIds( parseOptionalIntList( body, "hitBreakpointsIds" ) ) {}
Thread::Thread( const json& body ) :
id( body[DAP_ID].get<int>() ), name( body[DAP_NAME].get<std::string>() ) {}
@@ -259,48 +267,48 @@ std::vector<Thread> Thread::parseList( const json& threads ) {
StackFrame::StackFrame( const json& body ) :
id( body[DAP_ID].get<int>() ),
name( body[DAP_NAME].get<std::string>() ),
source( parseOptionalObject<Source>( body[DAP_SOURCE] ) ),
source( parseOptionalObject<Source>( body, DAP_SOURCE ) ),
line( body[DAP_LINE].get<int>() ),
column( body[DAP_COLUMN].get<int>() ),
endLine( parseOptionalInt( body["endLine"] ) ),
canRestart( parseOptionalBool( ( body["canRestart"] ) ) ),
instructionPointerReference( parseOptionalString( body["instructionPointerReference"] ) ),
moduleId_int( parseOptionalInt( body[DAP_MODULE_ID] ) ),
moduleId_str( parseOptionalString( body[DAP_MODULE_ID] ) ),
presentationHint( parseOptionalString( body[DAP_PRESENTATION_HINT] ) ) {}
endLine( parseOptionalInt( body, "endLine" ) ),
canRestart( parseOptionalBool( body, "canRestart" ) ),
instructionPointerReference( parseOptionalString( body, "instructionPointerReference" ) ),
moduleId_int( parseOptionalInt( body, DAP_MODULE_ID ) ),
moduleId_str( parseOptionalString( body, DAP_MODULE_ID ) ),
presentationHint( parseOptionalString( body, DAP_PRESENTATION_HINT ) ) {}
StackTraceInfo::StackTraceInfo( const json& body ) :
stackFrames( parseObjectList<StackFrame>( body["stackFrames"] ) ),
totalFrames( parseOptionalInt( body["totalFrames"] ) ) {}
totalFrames( parseOptionalInt( body, "totalFrames" ) ) {}
Module::Module( const json& body ) :
id_int( parseOptionalInt( body[DAP_ID] ) ),
id_str( parseOptionalString( body[DAP_ID] ) ),
id_int( parseOptionalInt( body, DAP_ID ) ),
id_str( parseOptionalString( body, DAP_ID ) ),
name( body[DAP_NAME].get<std::string>() ),
path( parseOptionalString( body[DAP_PATH] ) ),
isOptimized( parseOptionalBool( body["isOptimized"] ) ),
isUserCode( parseOptionalBool( body["isUserCode"] ) ),
version( parseOptionalString( body["version"] ) ),
symbolStatus( parseOptionalString( body["symbolStatus"] ) ),
symbolFilePath( parseOptionalString( body["symbolFilePath"] ) ),
dateTimeStamp( parseOptionalString( body["dateTimeStamp"] ) ),
addressRange( parseOptionalString( body["addressRange"] ) ) {}
path( parseOptionalString( body, DAP_PATH ) ),
isOptimized( parseOptionalBool( body, "isOptimized" ) ),
isUserCode( parseOptionalBool( body, "isUserCode" ) ),
version( parseOptionalString( body, "version" ) ),
symbolStatus( parseOptionalString( body, "symbolStatus" ) ),
symbolFilePath( parseOptionalString( body, "symbolFilePath" ) ),
dateTimeStamp( parseOptionalString( body, "dateTimeStamp" ) ),
addressRange( parseOptionalString( body, "addressRange" ) ) {}
ModuleEvent::ModuleEvent( const json& body ) :
reason( body[DAP_REASON].get<std::string>() ), module( Module( body["module"] ) ) {}
Scope::Scope( const json& body ) :
name( body[DAP_NAME].get<std::string>() ),
presentationHint( parseOptionalString( body[DAP_PRESENTATION_HINT] ) ),
presentationHint( parseOptionalString( body, DAP_PRESENTATION_HINT ) ),
variablesReference( body[DAP_VARIABLES_REFERENCE].get<int>() ),
namedVariables( parseOptionalInt( body["namedVariables"] ) ),
indexedVariables( parseOptionalInt( body["indexedVariables"] ) ),
expensive( parseOptionalBool( body["expensive"] ) ),
source( parseOptionalObject<Source>( body["source"] ) ),
line( parseOptionalInt( body["line"] ) ),
column( parseOptionalInt( body["column"] ) ),
endLine( parseOptionalInt( body["endLine"] ) ),
endColumn( parseOptionalInt( body["endColumn"] ) ) {}
namedVariables( parseOptionalInt( body, "namedVariables" ) ),
indexedVariables( parseOptionalInt( body, "indexedVariables" ) ),
expensive( parseOptionalBool( body, "expensive" ) ),
source( parseOptionalObject<Source>( body, "source" ) ),
line( parseOptionalInt( body, "line" ) ),
column( parseOptionalInt( body, "column" ) ),
endLine( parseOptionalInt( body, "endLine" ) ),
endColumn( parseOptionalInt( body, "endColumn" ) ) {}
Scope::Scope( int variablesReference, std::string name ) :
name( name ), variablesReference( variablesReference ) {}
@@ -312,12 +320,12 @@ std::vector<Scope> Scope::parseList( const json& scopes ) {
Variable::Variable( const json& body ) :
name( body[DAP_NAME].get<std::string>() ),
value( body["value"].get<std::string>() ),
type( parseOptionalString( body[DAP_TYPE].get<std::string>() ) ),
evaluateName( parseOptionalString( body["evaluateName"].get<std::string>() ) ),
type( parseOptionalString( body, DAP_TYPE ) ),
evaluateName( parseOptionalString( body, "evaluateName" ) ),
variablesReference( body[DAP_VARIABLES_REFERENCE].get<int>() ),
namedVariables( parseOptionalInt( body["namedVariables"] ) ),
indexedVariables( parseOptionalInt( body["indexedVariables"] ) ),
memoryReference( parseOptionalString( body["memoryReference"] ) ) {}
namedVariables( parseOptionalInt( body, "namedVariables" ) ),
indexedVariables( parseOptionalInt( body, "indexedVariables" ) ),
memoryReference( parseOptionalString( body, "memoryReference" ) ) {}
Variable::Variable( const std::string& name, const std::string& value, const int reference ) :
name( name ), value( value ), variablesReference( reference ) {}
@@ -328,18 +336,18 @@ std::vector<Variable> Variable::parseList( const json& variables ) {
ModulesInfo::ModulesInfo( const json& body ) :
modules( parseObjectList<Module>( body[DAP_MODULES] ) ),
totalModules( parseOptionalInt( body["totalModules"] ) ) {}
totalModules( parseOptionalInt( body, "totalModules" ) ) {}
ContinuedEvent::ContinuedEvent( const json& body ) :
threadId( body[DAP_THREAD_ID].get<int>() ),
allThreadsContinued( parseOptionalBool( body[DAP_ALL_THREADS_CONTINUED] ) ) {}
allThreadsContinued( parseOptionalBool( body, DAP_ALL_THREADS_CONTINUED ) ) {}
ContinuedEvent::ContinuedEvent( int threadId, bool allThreadsContinued ) :
threadId( threadId ), allThreadsContinued( allThreadsContinued ) {}
SourceContent::SourceContent( const json& body ) :
content( body["content"].get<std::string>() ),
mimeType( parseOptionalString( body["mimeType"] ) ) {}
mimeType( parseOptionalString( body, "mimeType" ) ) {}
SourceContent::SourceContent( const std::string& path ) {
const FileInfo file( path );
@@ -351,10 +359,10 @@ SourceContent::SourceContent( const std::string& path ) {
SourceBreakpoint::SourceBreakpoint( const json& body ) :
line( body[DAP_LINE].get<int>() ),
column( parseOptionalInt( body[DAP_COLUMN] ) ),
condition( parseOptionalString( body[DAP_CONDITION] ) ),
hitCondition( parseOptionalString( body[DAP_HIT_CONDITION] ) ),
logMessage( parseOptionalString( body["logMessage"] ) ) {}
column( parseOptionalInt( body, DAP_COLUMN ) ),
condition( parseOptionalString( body, DAP_CONDITION ) ),
hitCondition( parseOptionalString( body, DAP_HIT_CONDITION ) ),
logMessage( parseOptionalString( body, "logMessage" ) ) {}
SourceBreakpoint::SourceBreakpoint( const int line ) : line( line ) {}
@@ -377,16 +385,16 @@ json SourceBreakpoint::toJson() const {
}
Breakpoint::Breakpoint( const json& body ) :
id( parseOptionalInt( body[DAP_ID] ) ),
verified( body["verified"].get<bool>() ),
message( parseOptionalString( body["message"] ) ),
source( parseOptionalObject<Source>( body[DAP_SOURCE] ) ),
line( parseOptionalInt( body[DAP_LINE] ) ),
column( parseOptionalInt( body[DAP_COLUMN] ) ),
endLine( parseOptionalInt( body[DAP_END_LINE] ) ),
endColumn( parseOptionalInt( body[DAP_END_COLUMN] ) ),
instructionReference( parseOptionalString( body["instructionReference"] ) ),
offset( parseOptionalInt( body["offset"] ) ) {}
id( parseOptionalInt( body, DAP_ID ) ),
verified( body.value( "verified", false ) ),
message( parseOptionalString( body, "message" ) ),
source( parseOptionalObject<Source>( body, DAP_SOURCE ) ),
line( parseOptionalInt( body, DAP_LINE ) ),
column( parseOptionalInt( body, DAP_COLUMN ) ),
endLine( parseOptionalInt( body, DAP_END_LINE ) ),
endColumn( parseOptionalInt( body, DAP_END_COLUMN ) ),
instructionReference( parseOptionalString( body, "instructionReference" ) ),
offset( parseOptionalInt( body, "offset" ) ) {}
Breakpoint::Breakpoint( const int line ) : line( line ) {}
@@ -396,20 +404,20 @@ BreakpointEvent::BreakpointEvent( const json& body ) :
EvaluateInfo::EvaluateInfo( const json& body ) :
result( body[DAP_RESULT].get<std::string>() ),
type( parseOptionalString( body[DAP_TYPE] ) ),
type( parseOptionalString( body, DAP_TYPE ) ),
variablesReference( body[DAP_VARIABLES_REFERENCE].get<int>() ),
namedVariables( parseOptionalInt( body["namedVariables"] ) ),
indexedVariables( parseOptionalInt( body["indexedVariables"] ) ),
memoryReference( parseOptionalString( body["memoryReference"] ) ) {}
namedVariables( parseOptionalInt( body, "namedVariables" ) ),
indexedVariables( parseOptionalInt( body, "indexedVariables" ) ),
memoryReference( parseOptionalString( body, "memoryReference" ) ) {}
GotoTarget::GotoTarget( const json& body ) :
id( body[DAP_ID].get<int>() ),
label( body["label"].get<std::string>() ),
line( body[DAP_LINE].get<int>() ),
column( parseOptionalInt( body[DAP_COLUMN] ) ),
endLine( parseOptionalInt( body[DAP_END_LINE] ) ),
endColumn( parseOptionalInt( body[DAP_END_COLUMN] ) ),
instructionPointerReference( parseOptionalString( body["instructionPointerReference"] ) ) {}
column( parseOptionalInt( body, DAP_COLUMN ) ),
endLine( parseOptionalInt( body, DAP_END_LINE ) ),
endColumn( parseOptionalInt( body, DAP_END_COLUMN ) ),
instructionPointerReference( parseOptionalString( body, "instructionPointerReference" ) ) {}
std::vector<GotoTarget> GotoTarget::parseList( const json& variables ) {
return parseObjectList<GotoTarget>( variables );