summaryrefslogtreecommitdiffstats
path: root/Source/cmDebuggerWindowsPipeConnection.cxx
blob: 5f6f8484fe93563f9367211c25b82ba2e0558a75 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmDebuggerWindowsPipeConnection.h"

#include <algorithm>
#include <cassert>
#include <cstring>
#include <stdexcept>
#include <utility>

namespace cmDebugger {

#ifdef _WIN32

DuplexPipe_WIN32::DuplexPipe_WIN32(HANDLE pipe)
  : hPipe(pipe)
{
  readOp.Offset = readOp.OffsetHigh = 0;
  readOp.hEvent = CreateEvent(NULL, true, false, NULL);
  writeOp.Offset = readOp.OffsetHigh = 0;
  writeOp.hEvent = CreateEvent(NULL, true, false, NULL);
}

DuplexPipe_WIN32::~DuplexPipe_WIN32()
{
  close();
}

size_t DuplexPipe_WIN32::read(void* buffer, size_t n)
{
  if (hPipe != INVALID_HANDLE_VALUE) {
    readOp.Offset = readOp.OffsetHigh = 0;
    ResetEvent(readOp.hEvent);
    auto r = ReadFile(hPipe, buffer, n, NULL, &readOp);
    auto err = GetLastError();
    if (r || err == ERROR_IO_PENDING) {
      DWORD nRead = 0;
      if (GetOverlappedResult(hPipe, &readOp, &nRead, true)) {
        return nRead;
      }
    }
  }

  return 0;
}

bool DuplexPipe_WIN32::write(void const* buffer, size_t n)
{
  if (hPipe != INVALID_HANDLE_VALUE) {
    writeOp.Offset = writeOp.OffsetHigh = 0;
    ResetEvent(writeOp.hEvent);
    auto w = WriteFile(hPipe, buffer, n, NULL, &writeOp);
    auto err = GetLastError();
    if (w || err == ERROR_IO_PENDING) {
      DWORD nWrite = 0;
      if (GetOverlappedResult(hPipe, &writeOp, &nWrite, true)) {
        return n == nWrite;
      }
    }
  }

  return false;
}

void DuplexPipe_WIN32::close()
{
  CloseHandle(hPipe);
  hPipe = INVALID_HANDLE_VALUE;
  CloseHandle(readOp.hEvent);
  CloseHandle(writeOp.hEvent);
  readOp.hEvent = writeOp.hEvent = INVALID_HANDLE_VALUE;
}

bool DuplexPipe_WIN32::WaitForConnection()
{
  auto connect = ConnectNamedPipe(hPipe, &readOp);
  auto err = GetLastError();
  if (!connect && err == ERROR_IO_PENDING) {
    DWORD ignored;
    if (GetOverlappedResult(hPipe, &readOp, &ignored, true)) {
      return true;
    }
  }

  return connect || err == ERROR_PIPE_CONNECTED;
}

cmDebuggerPipeConnection_WIN32::cmDebuggerPipeConnection_WIN32(
  std::string name)
  : PipeName(std::move(name))
  , pipes(nullptr)
{
}

cmDebuggerPipeConnection_WIN32::~cmDebuggerPipeConnection_WIN32()
{
  if (isOpen()) {
    pipes = nullptr;
  }
}

bool cmDebuggerPipeConnection_WIN32::StartListening(std::string& errorMessage)
{
  bool result = true;

  auto hPipe = CreateNamedPipeA(
    PipeName.c_str(),
    PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
    PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_REJECT_REMOTE_CLIENTS, 1,
    1024 * 16, 1024 * 16, NMPWAIT_USE_DEFAULT_WAIT, NULL);

  if (hPipe == INVALID_HANDLE_VALUE) {
    auto err = GetLastError();
    errorMessage = GetErrorMessage(err);
    result = false;
  }

  if (result) {
    pipes = std::make_unique<DuplexPipe_WIN32>(hPipe);
  }

  StartedListening.set_value();
  return result;
}

std::string cmDebuggerPipeConnection_WIN32::GetErrorMessage(DWORD errorCode)
{
  LPSTR message = nullptr;
  DWORD size = FormatMessageA(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS,
    nullptr, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPSTR)&message, 0, nullptr);
  std::string errorMessage = "Internal Error with " + this->PipeName + ": " +
    std::string(message, size);
  LocalFree(message);
  return errorMessage;
}

std::shared_ptr<dap::Reader> cmDebuggerPipeConnection_WIN32::GetReader()
{
  return std::static_pointer_cast<dap::Reader>(shared_from_this());
}

std::shared_ptr<dap::Writer> cmDebuggerPipeConnection_WIN32::GetWriter()
{
  return std::static_pointer_cast<dap::Writer>(shared_from_this());
}

bool cmDebuggerPipeConnection_WIN32::isOpen()
{
  return pipes != nullptr;
}

void cmDebuggerPipeConnection_WIN32::close()
{
  CloseConnection();
}

void cmDebuggerPipeConnection_WIN32::CloseConnection()
{
  if (isOpen()) {
    pipes->close();
    pipes = nullptr;
  }
}

void cmDebuggerPipeConnection_WIN32::WaitForConnection()
{
  if (!isOpen()) {
    return;
  }

  if (pipes->WaitForConnection()) {
    return;
  }

  CloseConnection();
}

size_t cmDebuggerPipeConnection_WIN32::read(void* buffer, size_t n)
{
  size_t result = 0;
  if (isOpen()) {
    result = pipes->read(buffer, n);
    if (result == 0) {
      CloseConnection();
    }
  }

  return result;
}

bool cmDebuggerPipeConnection_WIN32::write(void const* buffer, size_t n)
{
  bool result = false;
  if (isOpen()) {
    result = pipes->write(buffer, n);
    if (!result) {
      CloseConnection();
    }
  }

  return result;
}

cmDebuggerPipeClient_WIN32::cmDebuggerPipeClient_WIN32(std::string name)
  : PipeName(std::move(name))
{
}

cmDebuggerPipeClient_WIN32::~cmDebuggerPipeClient_WIN32()
{
  close();
}

void cmDebuggerPipeClient_WIN32::WaitForConnection()
{
  if (!isOpen()) {
    auto hPipe = CreateFileA(PipeName.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
                             NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
    if (hPipe == INVALID_HANDLE_VALUE) {
      auto err = GetLastError();
      throw std::runtime_error(std::string("CreateFile failed for pipe ") +
                               GetErrorMessage(err));
    }

    pipes = std::make_unique<DuplexPipe_WIN32>(hPipe);
  }
}

std::string cmDebuggerPipeClient_WIN32::GetErrorMessage(DWORD errorCode)
{
  LPSTR message = nullptr;
  DWORD size = FormatMessageA(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS,
    nullptr, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPSTR)&message, 0, nullptr);
  std::string errorMessage =
    this->PipeName + ": " + std::string(message, size);
  LocalFree(message);
  return errorMessage;
}

bool cmDebuggerPipeClient_WIN32::isOpen()
{
  return pipes != nullptr;
}

void cmDebuggerPipeClient_WIN32::close()
{
  if (isOpen()) {
    pipes->close();
    pipes = nullptr;
  }
}

size_t cmDebuggerPipeClient_WIN32::read(void* buffer, size_t n)
{
  size_t result = 0;
  if (isOpen()) {
    result = pipes->read(buffer, n);
    if (result == 0) {
      close();
    }
  }

  return result;
}

bool cmDebuggerPipeClient_WIN32::write(void const* buffer, size_t n)
{
  bool result = false;
  if (isOpen()) {
    result = pipes->write(buffer, n);
    if (!result) {
      close();
    }
  }

  return result;
}

#endif // _WIN32

} // namespace cmDebugger