vkdoom_m/libraries/cppdap/include/dap/session.h
Nikita 45ac7c1d9e
ZScript DAP Debug server (#3009)
* Add STL-standard type traits and functions to TMap to enable for loop iteration

* add third-party range-map library

I have e-mailed the author for clarifcation on the license, will update this when they respond

* vcpkg: Add cppdap and eventpp libraries, update baseline

* DAP implementation

* Add `FileSystem::FileHash()` to get the CRC32 hash

* add `starting_offset` param to `VMDisasm()` for debugger disassembly view

Defaults to `0`, should not change output if it's not set

* Add `VMFrameStack::HasFrames()` to prevent assertions when inspecting in the debugger

* Add `PC` field to VMFrame, ensure that it is updated whenever vm increments/decrements the pc

Does not change alignment, the offsets used in VMFrame still work
We need this for the debugger because we otherwise have no way to get the pc; it was a local in `ExecScriptFunc()`

* `ZCCCompiler::CreateClassTypes()`: ensure SourceLumpName is set for classes derived from non-native classes

* start debug server in d_main, add `vm_debug` cvars and `-debug` CLI arg

* Add documentation for `vm_debug` cvars

* vm_exec: Add debugger hooks for instruction execution events

* c_console: Add debugger hooks for logging events in `PrintString`

* add `.cache/` to gitignore

* vendor cppdap on main @ 6464cd7

Patches:
removed .gitmodules (submodules were thirdparty/json, thirdparty/googletest)
removed thirdparty/googletest, not needed
removed thirdparty/json/docs, thirdparty/json/test, thirdparty/json/benchmarks to prevent massive bloat

* vendor eventpp on master @ 1224dd6

* build: use internal cppdap and eventpp by default

* dap: fix Binary::GetFunctionLineRange()

* fix bug in range_map::find_ranges

* make Binary dap::Source dynamic

* cache source code upon retrieval

* refactor Binary into a class, cleanup PexCache

* fix ending session gracefully

* d_main: Stop debug server in D_Cleanup()

* Fix connecting to debugger when session already started

* cleanup unused stuff in ZScriptDebugger

* always send TerminateEvents on disconnect if initialized

* tweak color display

* Don't cache disassembly lines when scanning scripts

* Cache nodes when getting runtime state

* WIP display locals

* Fix display of static arrays

* Ensure names display in proper order

* dap: Fix struct locals display, add args display

* Support `start` parameter

* Support `filter` parameter

* remove struct unbound native data display

Practically useless for debugging zscript and didn't work properly anyway

* d_main: fix vm_jit and vm_jit not being disabled soon enough

* support breaking on abort exceptions

* dap: refactor game event emitter functions into seperate header

* dap: show native functions on stack

* dap: Remove "Native" from exception handling, simplify exception event emit

* dap: add instruction breakpoints

* dap: fix display of locals not in scope yet

* dap: Make disassembly view display invalid instructions for non-code addresses

* dap: remove dot initializers

* dap: fix local structs in scope

* dap: don't parse the non-used options in the launch/attach requests

* dap: fix local struct view

* dap: Fix displaying objects that aren't their actual types

* dap: Fix action and state handling

* dap: stack display view

* dap: fix object display view

* d_dehacked: set qualified name in addition to the printable name

* dap: fix displaying breakpoint errors when script isn't loaded

* dap: remove debug print

* dap: Display parameter names

* dap: Turn down verbosity of logging

* dap: fix disassembly view

* dap: fix performance problems with arrays

* c_console: emit event only if not PRINT_NODAPEVENT

* dap: improve logging

* dap: update upstream cppdap library to fix deadlocks on no bind

* dap: Fix ending session on client socket closed

* dap: prevent DebugServer.h from pulling in `dap` and `ZScriptDebugger.h`

* dap: fix pause event not being emitted on pause

* dap: remove eventpp emitters, way too slow

* Remove eventpp dependency

* dap: Display correct register names

* dap: Show special inits in registers

* dap: Add stack offset to VMLocalVariable

* dap: fix display of static arrays and local variables on stack vs. registers

* dap: fix displaying function pointers

* dap: tweak color display

* dap: fix scalar display < 4

* dap: add Globals display to debugger

* dap: unify methods to get vmvalue

* dap: rename free method to freeValue to avoid running afoul of macro defs

* fix windows builds

* fix compile on linux

* cleanup

* Fix display of function breakpoints

* dap: Don't send back binary files

* dap: include sbarinfo in script types (no debug support for anything but zscript yet, this is just for returning source info)

* dap: don't show ending session message unless initialized

* fix erroneous commit

* dap: handle evaluate requests

* Fix getting bitfield values

* Add CVars Scope and evaluation

* add running console commands from repl

* dap: disable commands via repl for now

* fix loading functions DECORATE scripts

* Add source information to Dehacked VM functions, add debugging support

* dap: cleanup

* fix resolving archive paths

* don't send source back on native stack frames

* handle `modules` request

* cleanup

* allow evaluating cvars on hover

* fix oob bpinfos

* fix restarting the game blowing out the debug server

* dap: process input events while paused to prevent deadlocks

* fix getting local state

* dap: fix LocalState alignment

* dap: fix DumpStateHelper

* update cppdap protocol version to 1.68.0

* remove cppdap from vcpkg deps

We can't use the upstream version anyway because the maintainers are not merging our patches

* dap: make named variable nodes derive from the same class

* dap: make cvar scope available in native stack frames

* handle local variables with conflicting names

* add I_GetWindowEvent() to win32 to only process window events when debugging is paused

* dap: fix evaluate

* dap: fix display of `out` variables
2025-07-18 00:01:18 -03:00

460 lines
19 KiB
C++

// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef dap_session_h
#define dap_session_h
#include "future.h"
#include "io.h"
#include "traits.h"
#include "typeinfo.h"
#include "typeof.h"
#include <functional>
namespace dap {
// Forward declarations
struct Request;
struct Response;
struct Event;
////////////////////////////////////////////////////////////////////////////////
// Error
////////////////////////////////////////////////////////////////////////////////
// Error represents an error message in response to a DAP request.
struct Error {
Error() = default;
Error(const std::string& error);
Error(const char* msg, ...);
// operator bool() returns true if there is an error.
inline operator bool() const { return message.size() > 0; }
std::string message; // empty represents success.
};
////////////////////////////////////////////////////////////////////////////////
// ResponseOrError<T>
////////////////////////////////////////////////////////////////////////////////
// ResponseOrError holds either the response to a DAP request or an error
// message.
template <typename T>
struct ResponseOrError {
using Request = T;
inline ResponseOrError() = default;
inline ResponseOrError(const T& response);
inline ResponseOrError(T&& response);
inline ResponseOrError(const Error& error);
inline ResponseOrError(Error&& error);
inline ResponseOrError(const ResponseOrError& other);
inline ResponseOrError(ResponseOrError&& other);
inline ResponseOrError& operator=(const ResponseOrError& other);
inline ResponseOrError& operator=(ResponseOrError&& other);
T response;
Error error; // empty represents success.
};
template <typename T>
ResponseOrError<T>::ResponseOrError(const T& resp) : response(resp) {}
template <typename T>
ResponseOrError<T>::ResponseOrError(T&& resp) : response(std::move(resp)) {}
template <typename T>
ResponseOrError<T>::ResponseOrError(const Error& err) : error(err) {}
template <typename T>
ResponseOrError<T>::ResponseOrError(Error&& err) : error(std::move(err)) {}
template <typename T>
ResponseOrError<T>::ResponseOrError(const ResponseOrError& other)
: response(other.response), error(other.error) {}
template <typename T>
ResponseOrError<T>::ResponseOrError(ResponseOrError&& other)
: response(std::move(other.response)), error(std::move(other.error)) {}
template <typename T>
ResponseOrError<T>& ResponseOrError<T>::operator=(
const ResponseOrError& other) {
response = other.response;
error = other.error;
return *this;
}
template <typename T>
ResponseOrError<T>& ResponseOrError<T>::operator=(ResponseOrError&& other) {
response = std::move(other.response);
error = std::move(other.error);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
// Session
////////////////////////////////////////////////////////////////////////////////
// An enum flag that controls how the Session handles invalid data.
enum OnInvalidData {
// Ignore invalid data.
kIgnore,
// Close the underlying reader when invalid data is received.
kClose,
};
// Session implements a DAP client or server endpoint.
// The general usage is as follows:
// (1) Create a session with Session::create().
// (2) Register request and event handlers with registerHandler().
// (3) Optionally register a protocol error handler with onError().
// (3) Bind the session to the remote endpoint with bind().
// (4) Send requests or events with send().
class Session {
template <typename F, int N>
using ParamType = traits::ParameterType<F, N>;
template <typename T>
using IsRequest = traits::EnableIfIsType<dap::Request, T>;
template <typename T>
using IsEvent = traits::EnableIfIsType<dap::Event, T>;
template <typename F>
using IsRequestHandlerWithoutCallback = traits::EnableIf<
traits::CompatibleWith<F, std::function<void(dap::Request)>>::value>;
template <typename F, typename CallbackType>
using IsRequestHandlerWithCallback = traits::EnableIf<traits::CompatibleWith<
F,
std::function<void(dap::Request, std::function<void(CallbackType)>)>>::
value>;
public:
virtual ~Session();
// ErrorHandler is the type of callback function used for reporting protocol
// errors.
using ErrorHandler = std::function<void(const char*)>;
// ClosedHandler is the type of callback function used to signal that a
// connected endpoint has closed.
using ClosedHandler = std::function<void()>;
// create() constructs and returns a new Session.
static std::unique_ptr<Session> create();
// Sets how the Session handles invalid data.
virtual void setOnInvalidData(OnInvalidData) = 0;
// onError() registers a error handler that will be called whenever a protocol
// error is encountered.
// Only one error handler can be bound at any given time, and later calls
// will replace the existing error handler.
virtual void onError(const ErrorHandler&) = 0;
// registerHandler() registers a request handler for a specific request type.
// The function F must have one of the following signatures:
// ResponseOrError<ResponseType>(const RequestType&)
// ResponseType(const RequestType&)
// Error(const RequestType&)
template <typename F, typename RequestType = ParamType<F, 0>>
inline IsRequestHandlerWithoutCallback<F> registerHandler(F&& handler);
// registerHandler() registers a request handler for a specific request type.
// The handler has a response callback function for the second argument of the
// handler function. This callback may be called after the handler has
// returned.
// The function F must have the following signature:
// void(const RequestType& request,
// std::function<void(ResponseType)> response)
template <typename F,
typename RequestType = ParamType<F, 0>,
typename ResponseType = typename RequestType::Response>
inline IsRequestHandlerWithCallback<F, ResponseType> registerHandler(
F&& handler);
// registerHandler() registers a request handler for a specific request type.
// The handler has a response callback function for the second argument of the
// handler function. This callback may be called after the handler has
// returned.
// The function F must have the following signature:
// void(const RequestType& request,
// std::function<void(ResponseOrError<ResponseType>)> response)
template <typename F,
typename RequestType = ParamType<F, 0>,
typename ResponseType = typename RequestType::Response>
inline IsRequestHandlerWithCallback<F, ResponseOrError<ResponseType>>
registerHandler(F&& handler);
// registerHandler() registers a event handler for a specific event type.
// The function F must have the following signature:
// void(const EventType&)
template <typename F, typename EventType = ParamType<F, 0>>
inline IsEvent<EventType> registerHandler(F&& handler);
// registerSentHandler() registers the function F to be called when a response
// of the specific type has been sent.
// The function F must have the following signature:
// void(const ResponseOrError<ResponseType>&)
template <typename F,
typename ResponseType = typename ParamType<F, 0>::Request>
inline void registerSentHandler(F&& handler);
// send() sends the request to the connected endpoint and returns a
// future that is assigned the request response or error.
template <typename T, typename = IsRequest<T>>
future<ResponseOrError<typename T::Response>> send(const T& request);
// send() sends the event to the connected endpoint.
template <typename T, typename = IsEvent<T>>
void send(const T& event);
// bind() connects this Session to an endpoint using connect(), and then
// starts processing incoming messages with startProcessingMessages().
// onClose is the optional callback which will be called when the session
// endpoint has been closed.
inline void bind(const std::shared_ptr<Reader>& reader,
const std::shared_ptr<Writer>& writer,
const ClosedHandler& onClose);
inline void bind(const std::shared_ptr<ReaderWriter>& readerWriter,
const ClosedHandler& onClose);
//////////////////////////////////////////////////////////////////////////////
// Note:
// Methods and members below this point are for advanced usage, and are more
// likely to change signature than the methods above.
// The methods above this point should be sufficient for most use cases.
//////////////////////////////////////////////////////////////////////////////
// connect() connects this Session to an endpoint.
// connect() can only be called once. Repeated calls will raise an error, but
// otherwise will do nothing.
// Note: This method is used for explicit control over message handling.
// Most users will use bind() instead of calling this method directly.
virtual void connect(const std::shared_ptr<Reader>&,
const std::shared_ptr<Writer>&) = 0;
inline void connect(const std::shared_ptr<ReaderWriter>&);
// startProcessingMessages() starts a new thread to receive and dispatch
// incoming messages.
// onClose is the optional callback which will be called when the session
// endpoint has been closed.
// Note: This method is used for explicit control over message handling.
// Most users will use bind() instead of calling this method directly.
virtual void startProcessingMessages(const ClosedHandler& onClose = {}) = 0;
// getPayload() blocks until the next incoming message is received, returning
// the payload or an empty function if the connection was lost. The returned
// payload is function that can be called on any thread to dispatch the
// message to the Session handler.
// Note: This method is used for explicit control over message handling.
// Most users will use bind() instead of calling this method directly.
virtual std::function<void()> getPayload() = 0;
// The callback function type called when a request handler is invoked, and
// the request returns a successful result.
// 'responseTypeInfo' is the type information of the response data structure.
// 'responseData' is a pointer to response payload data.
using RequestHandlerSuccessCallback =
std::function<void(const TypeInfo* responseTypeInfo,
const void* responseData)>;
// The callback function type used to notify when a DAP request fails.
// 'responseTypeInfo' is the type information of the response data structure.
// 'message' is the error message
using RequestHandlerErrorCallback =
std::function<void(const TypeInfo* responseTypeInfo,
const Error& message)>;
// The callback function type used to invoke a request handler.
// 'request' is a pointer to the request data structure
// 'onSuccess' is the function to call if the request completed succesfully.
// 'onError' is the function to call if the request failed.
// For each call of the request handler, 'onSuccess' or 'onError' must be
// called exactly once.
using GenericRequestHandler =
std::function<void(const void* request,
const RequestHandlerSuccessCallback& onSuccess,
const RequestHandlerErrorCallback& onError)>;
// The callback function type used to handle a response to a request.
// 'response' is a pointer to the response data structure. May be nullptr.
// 'error' is a pointer to the reponse error message. May be nullptr.
// One of 'data' or 'error' will be nullptr.
using GenericResponseHandler =
std::function<void(const void* response, const Error* error)>;
// The callback function type used to handle an event.
// 'event' is a pointer to the event data structure.
using GenericEventHandler = std::function<void(const void* event)>;
// The callback function type used to notify when a response has been sent
// from this session endpoint.
// 'response' is a pointer to the response data structure.
// 'error' is a pointer to the reponse error message. May be nullptr.
using GenericResponseSentHandler =
std::function<void(const void* response, const Error* error)>;
// registerHandler() registers 'handler' as the request handler callback for
// requests of the type 'typeinfo'.
virtual void registerHandler(const TypeInfo* typeinfo,
const GenericRequestHandler& handler) = 0;
// registerHandler() registers 'handler' as the event handler callback for
// events of the type 'typeinfo'.
virtual void registerHandler(const TypeInfo* typeinfo,
const GenericEventHandler& handler) = 0;
// registerHandler() registers 'handler' as the response-sent handler function
// which is called whenever a response of the type 'typeinfo' is sent from
// this session endpoint.
virtual void registerHandler(const TypeInfo* typeinfo,
const GenericResponseSentHandler& handler) = 0;
// send() sends a request to the remote endpoint.
// 'requestTypeInfo' is the type info of the request data structure.
// 'requestTypeInfo' is the type info of the response data structure.
// 'request' is a pointer to the request data structure.
// 'responseHandler' is the handler function for the response.
virtual bool send(const dap::TypeInfo* requestTypeInfo,
const dap::TypeInfo* responseTypeInfo,
const void* request,
const GenericResponseHandler& responseHandler) = 0;
// send() sends an event to the remote endpoint.
// 'eventTypeInfo' is the type info for the event data structure.
// 'event' is a pointer to the event data structure.
virtual bool send(const TypeInfo* eventTypeInfo, const void* event) = 0;
};
template <typename F, typename RequestType>
Session::IsRequestHandlerWithoutCallback<F> Session::registerHandler(
F&& handler) {
using ResponseType = typename RequestType::Response;
const TypeInfo* typeinfo = TypeOf<RequestType>::type();
registerHandler(typeinfo,
[handler](const void* args,
const RequestHandlerSuccessCallback& onSuccess,
const RequestHandlerErrorCallback& onError) {
ResponseOrError<ResponseType> res =
handler(*reinterpret_cast<const RequestType*>(args));
if (res.error) {
onError(TypeOf<ResponseType>::type(), res.error);
} else {
onSuccess(TypeOf<ResponseType>::type(), &res.response);
}
});
}
template <typename F, typename RequestType, typename ResponseType>
Session::IsRequestHandlerWithCallback<F, ResponseType> Session::registerHandler(
F&& handler) {
using CallbackType = ParamType<F, 1>;
registerHandler(
TypeOf<RequestType>::type(),
[handler](const void* args,
const RequestHandlerSuccessCallback& onSuccess,
const RequestHandlerErrorCallback&) {
CallbackType responseCallback = [onSuccess](const ResponseType& res) {
onSuccess(TypeOf<ResponseType>::type(), &res);
};
handler(*reinterpret_cast<const RequestType*>(args), responseCallback);
});
}
template <typename F, typename RequestType, typename ResponseType>
Session::IsRequestHandlerWithCallback<F, ResponseOrError<ResponseType>>
Session::registerHandler(F&& handler) {
using CallbackType = ParamType<F, 1>;
registerHandler(
TypeOf<RequestType>::type(),
[handler](const void* args,
const RequestHandlerSuccessCallback& onSuccess,
const RequestHandlerErrorCallback& onError) {
CallbackType responseCallback =
[onError, onSuccess](const ResponseOrError<ResponseType>& res) {
if (res.error) {
onError(TypeOf<ResponseType>::type(), res.error);
} else {
onSuccess(TypeOf<ResponseType>::type(), &res.response);
}
};
handler(*reinterpret_cast<const RequestType*>(args), responseCallback);
});
}
template <typename F, typename T>
Session::IsEvent<T> Session::registerHandler(F&& handler) {
auto cb = [handler](const void* args) {
handler(*reinterpret_cast<const T*>(args));
};
const TypeInfo* typeinfo = TypeOf<T>::type();
registerHandler(typeinfo, cb);
}
template <typename F, typename T>
void Session::registerSentHandler(F&& handler) {
auto cb = [handler](const void* response, const Error* error) {
if (error != nullptr) {
handler(ResponseOrError<T>(*error));
} else {
handler(ResponseOrError<T>(*reinterpret_cast<const T*>(response)));
}
};
const TypeInfo* typeinfo = TypeOf<T>::type();
registerHandler(typeinfo, cb);
}
template <typename T, typename>
future<ResponseOrError<typename T::Response>> Session::send(const T& request) {
using Response = typename T::Response;
promise<ResponseOrError<Response>> promise;
auto sent = send(TypeOf<T>::type(), TypeOf<Response>::type(), &request,
[=](const void* result, const Error* error) {
if (error != nullptr) {
promise.set_value(ResponseOrError<Response>(*error));
} else {
promise.set_value(ResponseOrError<Response>(
*reinterpret_cast<const Response*>(result)));
}
});
if (!sent) {
promise.set_value(Error("Failed to send request"));
}
return promise.get_future();
}
template <typename T, typename>
void Session::send(const T& event) {
const TypeInfo* typeinfo = TypeOf<T>::type();
send(typeinfo, &event);
}
void Session::connect(const std::shared_ptr<ReaderWriter>& rw) {
connect(rw, rw);
}
void Session::bind(const std::shared_ptr<dap::Reader>& r,
const std::shared_ptr<dap::Writer>& w,
const ClosedHandler& onClose = {}) {
connect(r, w);
startProcessingMessages(onClose);
}
void Session::bind(const std::shared_ptr<ReaderWriter>& rw,
const ClosedHandler& onClose = {}) {
bind(rw, rw, onClose);
}
} // namespace dap
#endif // dap_session_h