cmDebuggerPipeConnection.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmDebuggerPipeConnection.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstring>
  7. #include <stdexcept>
  8. #include <utility>
  9. namespace cmDebugger {
  10. struct write_req_t
  11. {
  12. uv_write_t req;
  13. uv_buf_t buf;
  14. };
  15. cmDebuggerPipeBase::cmDebuggerPipeBase(std::string name)
  16. : PipeName(std::move(name))
  17. {
  18. Loop.init();
  19. LoopExit.init(
  20. *Loop, [](uv_async_t* handle) { uv_stop((uv_loop_t*)handle->data); },
  21. Loop);
  22. WriteEvent.init(
  23. *Loop,
  24. [](uv_async_t* handle) {
  25. auto* conn = static_cast<cmDebuggerPipeBase*>(handle->data);
  26. conn->WriteInternal();
  27. },
  28. this);
  29. PipeClose.init(
  30. *Loop,
  31. [](uv_async_t* handle) {
  32. auto* conn = static_cast<cmDebuggerPipeBase*>(handle->data);
  33. if (conn->Pipe.get()) {
  34. conn->Pipe->data = nullptr;
  35. conn->Pipe.reset();
  36. }
  37. },
  38. this);
  39. }
  40. void cmDebuggerPipeBase::WaitForConnection()
  41. {
  42. std::unique_lock<std::mutex> lock(Mutex);
  43. Connected.wait(lock, [this] { return isOpen() || FailedToOpen; });
  44. if (FailedToOpen) {
  45. throw std::runtime_error("Failed to open debugger connection.");
  46. }
  47. }
  48. void cmDebuggerPipeBase::close()
  49. {
  50. std::unique_lock<std::mutex> lock(Mutex);
  51. CloseConnection();
  52. PipeClose.send();
  53. lock.unlock();
  54. ReadReady.notify_all();
  55. }
  56. size_t cmDebuggerPipeBase::read(void* buffer, size_t n)
  57. {
  58. std::unique_lock<std::mutex> lock(Mutex);
  59. ReadReady.wait(lock, [this] { return !isOpen() || !ReadBuffer.empty(); });
  60. if (!isOpen() && ReadBuffer.empty()) {
  61. return 0;
  62. }
  63. auto size = std::min(n, ReadBuffer.size());
  64. memcpy(buffer, ReadBuffer.data(), size);
  65. ReadBuffer.erase(0, size);
  66. return size;
  67. }
  68. bool cmDebuggerPipeBase::write(const void* buffer, size_t n)
  69. {
  70. std::unique_lock<std::mutex> lock(Mutex);
  71. WriteBuffer.append(static_cast<const char*>(buffer), n);
  72. lock.unlock();
  73. WriteEvent.send();
  74. lock.lock();
  75. WriteComplete.wait(lock, [this] { return WriteBuffer.empty(); });
  76. return true;
  77. }
  78. void cmDebuggerPipeBase::StopLoop()
  79. {
  80. LoopExit.send();
  81. if (LoopThread.joinable()) {
  82. LoopThread.join();
  83. }
  84. }
  85. void cmDebuggerPipeBase::BufferData(const std::string& data)
  86. {
  87. std::unique_lock<std::mutex> lock(Mutex);
  88. ReadBuffer += data;
  89. lock.unlock();
  90. ReadReady.notify_all();
  91. }
  92. void cmDebuggerPipeBase::WriteInternal()
  93. {
  94. std::unique_lock<std::mutex> lock(Mutex);
  95. auto n = WriteBuffer.length();
  96. assert(this->Pipe.get());
  97. write_req_t* req = new write_req_t;
  98. req->req.data = &WriteComplete;
  99. char* rawBuffer = new char[n];
  100. req->buf = uv_buf_init(rawBuffer, static_cast<unsigned int>(n));
  101. memcpy(req->buf.base, WriteBuffer.data(), n);
  102. WriteBuffer.clear();
  103. lock.unlock();
  104. uv_write(
  105. reinterpret_cast<uv_write_t*>(req), this->Pipe, &req->buf, 1,
  106. [](uv_write_t* cb_req, int status) {
  107. (void)status; // We need to free memory even if the write failed.
  108. write_req_t* wr = reinterpret_cast<write_req_t*>(cb_req);
  109. reinterpret_cast<std::condition_variable*>(wr->req.data)->notify_all();
  110. delete[] (wr->buf.base);
  111. delete wr;
  112. });
  113. #ifdef __clang_analyzer__
  114. // Tell clang-analyzer that 'rawBuffer' does not leak.
  115. // We pass ownership to the closure.
  116. delete[] rawBuffer;
  117. #endif
  118. }
  119. cmDebuggerPipeConnection::cmDebuggerPipeConnection(std::string name)
  120. : cmDebuggerPipeBase(std::move(name))
  121. {
  122. ServerPipeClose.init(
  123. *Loop,
  124. [](uv_async_t* handle) {
  125. auto* conn = static_cast<cmDebuggerPipeConnection*>(handle->data);
  126. if (conn->ServerPipe.get()) {
  127. conn->ServerPipe->data = nullptr;
  128. conn->ServerPipe.reset();
  129. }
  130. },
  131. this);
  132. }
  133. cmDebuggerPipeConnection::~cmDebuggerPipeConnection()
  134. {
  135. StopLoop();
  136. }
  137. bool cmDebuggerPipeConnection::StartListening(std::string& errorMessage)
  138. {
  139. this->ServerPipe.init(*Loop, 0,
  140. static_cast<cmDebuggerPipeConnection*>(this));
  141. int r;
  142. if ((r = uv_pipe_bind(this->ServerPipe, this->PipeName.c_str())) != 0) {
  143. errorMessage =
  144. "Internal Error with " + this->PipeName + ": " + uv_err_name(r);
  145. return false;
  146. }
  147. r = uv_listen(this->ServerPipe, 1, [](uv_stream_t* stream, int status) {
  148. if (status >= 0) {
  149. auto* conn = static_cast<cmDebuggerPipeConnection*>(stream->data);
  150. if (conn) {
  151. conn->Connect(stream);
  152. }
  153. }
  154. });
  155. if (r != 0) {
  156. errorMessage =
  157. "Internal Error listening on " + this->PipeName + ": " + uv_err_name(r);
  158. return false;
  159. }
  160. // Start the libuv event loop thread so that a client can connect.
  161. LoopThread = std::thread([this] { uv_run(Loop, UV_RUN_DEFAULT); });
  162. StartedListening.set_value();
  163. return true;
  164. }
  165. std::shared_ptr<dap::Reader> cmDebuggerPipeConnection::GetReader()
  166. {
  167. return std::static_pointer_cast<dap::Reader>(shared_from_this());
  168. }
  169. std::shared_ptr<dap::Writer> cmDebuggerPipeConnection::GetWriter()
  170. {
  171. return std::static_pointer_cast<dap::Writer>(shared_from_this());
  172. }
  173. bool cmDebuggerPipeConnection::isOpen()
  174. {
  175. return this->Pipe.get() != nullptr;
  176. }
  177. void cmDebuggerPipeConnection::CloseConnection()
  178. {
  179. ServerPipeClose.send();
  180. }
  181. void cmDebuggerPipeConnection::Connect(uv_stream_t* server)
  182. {
  183. if (this->Pipe.get()) {
  184. // Accept and close all pipes but the first:
  185. cm::uv_pipe_ptr rejectPipe;
  186. rejectPipe.init(*Loop, 0);
  187. uv_accept(server, rejectPipe);
  188. return;
  189. }
  190. cm::uv_pipe_ptr ClientPipe;
  191. ClientPipe.init(*Loop, 0, static_cast<cmDebuggerPipeConnection*>(this));
  192. if (uv_accept(server, ClientPipe) != 0) {
  193. return;
  194. }
  195. StartReading<cmDebuggerPipeConnection>(ClientPipe);
  196. std::unique_lock<std::mutex> lock(Mutex);
  197. Pipe = std::move(ClientPipe);
  198. lock.unlock();
  199. Connected.notify_all();
  200. }
  201. cmDebuggerPipeClient::~cmDebuggerPipeClient()
  202. {
  203. StopLoop();
  204. }
  205. void cmDebuggerPipeClient::Start()
  206. {
  207. this->Pipe.init(*Loop, 0, static_cast<cmDebuggerPipeClient*>(this));
  208. uv_connect_t* connect = new uv_connect_t;
  209. connect->data = this;
  210. uv_pipe_connect(
  211. connect, Pipe, PipeName.c_str(), [](uv_connect_t* cb_connect, int status) {
  212. auto* conn = static_cast<cmDebuggerPipeClient*>(cb_connect->data);
  213. if (status >= 0) {
  214. conn->Connect();
  215. } else {
  216. conn->FailConnection();
  217. }
  218. delete cb_connect;
  219. });
  220. // Start the libuv event loop so that the pipe can connect.
  221. LoopThread = std::thread([this] { uv_run(Loop, UV_RUN_DEFAULT); });
  222. }
  223. bool cmDebuggerPipeClient::isOpen()
  224. {
  225. return IsConnected;
  226. }
  227. void cmDebuggerPipeClient::CloseConnection()
  228. {
  229. IsConnected = false;
  230. }
  231. void cmDebuggerPipeClient::Connect()
  232. {
  233. StartReading<cmDebuggerPipeClient>(Pipe);
  234. std::unique_lock<std::mutex> lock(Mutex);
  235. IsConnected = true;
  236. lock.unlock();
  237. Connected.notify_all();
  238. }
  239. void cmDebuggerPipeClient::FailConnection()
  240. {
  241. std::unique_lock<std::mutex> lock(Mutex);
  242. FailedToOpen = true;
  243. lock.unlock();
  244. Connected.notify_all();
  245. }
  246. } // namespace cmDebugger