1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include <chrono>
#include <cstdio>
#include <future>
#include <memory>
#include <string>
#include <cm3p/cppdap/future.h>
#include <cm3p/cppdap/io.h>
#include <cm3p/cppdap/optional.h>
#include <cm3p/cppdap/protocol.h>
#include <cm3p/cppdap/session.h>
#include <cm3p/cppdap/types.h>
#include "cmDebuggerAdapter.h"
#include "cmDebuggerProtocol.h"
#include "cmVersionConfig.h"
#include "testCommon.h"
#include "testDebugger.h"
class DebuggerLocalConnection : public cmDebugger::cmDebuggerConnection
{
public:
DebuggerLocalConnection()
: ClientToDebugger(dap::pipe())
, DebuggerToClient(dap::pipe())
{
}
bool StartListening(std::string& errorMessage) override
{
errorMessage = "";
return true;
}
void WaitForConnection() override {}
std::shared_ptr<dap::Reader> GetReader() override
{
return ClientToDebugger;
};
std::shared_ptr<dap::Writer> GetWriter() override
{
return DebuggerToClient;
}
std::shared_ptr<dap::ReaderWriter> ClientToDebugger;
std::shared_ptr<dap::ReaderWriter> DebuggerToClient;
};
bool runTest(std::function<bool(dap::Session&)> onThreadExitedEvent)
{
std::promise<bool> debuggerAdapterInitializedPromise;
std::future<bool> debuggerAdapterInitializedFuture =
debuggerAdapterInitializedPromise.get_future();
std::promise<bool> initializedEventReceivedPromise;
std::future<bool> initializedEventReceivedFuture =
initializedEventReceivedPromise.get_future();
std::promise<bool> exitedEventReceivedPromise;
std::future<bool> exitedEventReceivedFuture =
exitedEventReceivedPromise.get_future();
std::promise<bool> terminatedEventReceivedPromise;
std::future<bool> terminatedEventReceivedFuture =
terminatedEventReceivedPromise.get_future();
std::promise<bool> threadStartedPromise;
std::future<bool> threadStartedFuture = threadStartedPromise.get_future();
std::promise<bool> threadExitedPromise;
std::future<bool> threadExitedFuture = threadExitedPromise.get_future();
std::promise<bool> disconnectResponseReceivedPromise;
std::future<bool> disconnectResponseReceivedFuture =
disconnectResponseReceivedPromise.get_future();
auto futureTimeout = std::chrono::seconds(60);
auto connection = std::make_shared<DebuggerLocalConnection>();
std::unique_ptr<dap::Session> client = dap::Session::create();
client->registerHandler([&](dap::InitializedEvent /*unused*/) {
initializedEventReceivedPromise.set_value(true);
});
client->registerHandler([&](dap::ExitedEvent /*unused*/) {
exitedEventReceivedPromise.set_value(true);
});
client->registerHandler([&](dap::TerminatedEvent const& /*unused*/) {
terminatedEventReceivedPromise.set_value(true);
});
client->registerHandler([&](dap::ThreadEvent const& e) {
if (e.reason == "started") {
threadStartedPromise.set_value(true);
} else if (e.reason == "exited") {
threadExitedPromise.set_value(true);
}
});
client->bind(connection->DebuggerToClient, connection->ClientToDebugger);
ScopedThread debuggerThread([&]() -> int {
std::shared_ptr<cmDebugger::cmDebuggerAdapter> debuggerAdapter =
std::make_shared<cmDebugger::cmDebuggerAdapter>(
connection, dap::file(stdout, false));
debuggerAdapterInitializedPromise.set_value(true);
debuggerAdapter->ReportExitCode(0);
// Ensure the disconnectResponse has been received before
// destructing debuggerAdapter.
ASSERT_TRUE(disconnectResponseReceivedFuture.wait_for(futureTimeout) ==
std::future_status::ready);
return 0;
});
dap::CMakeInitializeRequest initializeRequest;
auto initializeResponse = client->send(initializeRequest).get();
ASSERT_TRUE(initializeResponse.response.cmakeVersion.full == CMake_VERSION);
ASSERT_TRUE(initializeResponse.response.cmakeVersion.major ==
CMake_VERSION_MAJOR);
ASSERT_TRUE(initializeResponse.response.cmakeVersion.minor ==
CMake_VERSION_MINOR);
ASSERT_TRUE(initializeResponse.response.cmakeVersion.patch ==
CMake_VERSION_PATCH);
ASSERT_TRUE(initializeResponse.response.supportsExceptionInfoRequest);
ASSERT_TRUE(
initializeResponse.response.exceptionBreakpointFilters.has_value());
ASSERT_TRUE(initializeResponse.response.supportsValueFormattingOptions);
dap::LaunchRequest launchRequest;
auto launchResponse = client->send(launchRequest).get();
ASSERT_TRUE(!launchResponse.error);
dap::ConfigurationDoneRequest configurationDoneRequest;
auto configurationDoneResponse =
client->send(configurationDoneRequest).get();
ASSERT_TRUE(!configurationDoneResponse.error);
ASSERT_TRUE(debuggerAdapterInitializedFuture.wait_for(futureTimeout) ==
std::future_status::ready);
ASSERT_TRUE(initializedEventReceivedFuture.wait_for(futureTimeout) ==
std::future_status::ready);
ASSERT_TRUE(threadStartedFuture.wait_for(futureTimeout) ==
std::future_status::ready);
ASSERT_TRUE(threadExitedFuture.wait_for(futureTimeout) ==
std::future_status::ready);
if (onThreadExitedEvent) {
ASSERT_TRUE(onThreadExitedEvent(*client));
}
ASSERT_TRUE(exitedEventReceivedFuture.wait_for(futureTimeout) ==
std::future_status::ready);
ASSERT_TRUE(terminatedEventReceivedFuture.wait_for(futureTimeout) ==
std::future_status::ready);
dap::DisconnectRequest disconnectRequest;
auto disconnectResponse = client->send(disconnectRequest).get();
disconnectResponseReceivedPromise.set_value(true);
ASSERT_TRUE(!disconnectResponse.error);
return true;
}
bool testBasicProtocol()
{
return runTest(nullptr);
}
bool testThreadsRequestAfterThreadExitedEvent()
{
return runTest([](dap::Session& session) -> bool {
// Try requesting threads again after receiving the thread exited event.
// Some clients do this to ensure that their thread list is up-to-date.
dap::ThreadsRequest threadsRequest;
auto threadsResponse = session.send(threadsRequest).get();
ASSERT_TRUE(!threadsResponse.error);
// CMake only has one DAP thread. Once that thread exits, there should be
// no threads left.
ASSERT_TRUE(threadsResponse.response.threads.empty());
return true;
});
}
int testDebuggerAdapter(int, char*[])
{
return runTests(
{ testBasicProtocol, testThreadsRequestAfterThreadExitedEvent });
}
|