Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
6.3 KiB

  1. // Spewview: remote debug spew monitor
  2. //
  3. // Copyright (c) 2000 Microsoft Corp.
  4. //
  5. // Thread to read remote debug spew
  6. //
  7. // 20 Mar 2000 sburns
  8. #include "headers.hxx"
  9. #include "resource.h"
  10. #include "ReaderThread.hpp"
  11. #include "SpewDialog.hpp"
  12. int readMessageCount = 0;
  13. HRESULT
  14. CreateAndConnectSpewPipe(
  15. const String& appName,
  16. HANDLE& result)
  17. {
  18. LOG_FUNCTION(CreateAndConnectSpewPipe);
  19. ASSERT(!appName.empty());
  20. result = INVALID_HANDLE_VALUE;
  21. HRESULT hr = S_OK;
  22. do
  23. {
  24. // build a null dacl for the pipe to allow everyone access
  25. SECURITY_ATTRIBUTES sa;
  26. memset(&sa, 0, sizeof(sa));
  27. SECURITY_DESCRIPTOR sd;
  28. hr = Win::InitializeSecurityDescriptor(sd);
  29. BREAK_ON_FAILED_HRESULT(hr);
  30. hr = Win::SetSecurityDescriptorDacl(sd, true, 0, false);
  31. BREAK_ON_FAILED_HRESULT(hr);
  32. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  33. sa.lpSecurityDescriptor = &sd;
  34. sa.bInheritHandle = TRUE;
  35. String pipename = L"\\\\.\\pipe\\spewview\\" + appName;
  36. hr =
  37. Win::CreateNamedPipe(
  38. pipename,
  39. PIPE_ACCESS_INBOUND | WRITE_DAC,
  40. PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE /* | PIPE_NOWAIT */ ,
  41. 1,
  42. MAX_PATH,
  43. MAX_PATH,
  44. 500,
  45. &sa,
  46. result);
  47. BREAK_ON_FAILED_HRESULT(hr);
  48. // block until the spewing app connects to us.
  49. hr = Win::ConnectNamedPipe(result, 0);
  50. BREAK_ON_FAILED_HRESULT(hr);
  51. }
  52. while (0);
  53. LOG_HRESULT(hr);
  54. return hr;
  55. }
  56. void
  57. AddSpewMessage(
  58. HWND spewWindow,
  59. const String& message,
  60. DWORD readMessageCount = -1)
  61. {
  62. ASSERT(!message.empty());
  63. // post the received text to the message window.
  64. // deleted in the UI thread when the WM_UPDATE_SPEWAGE message
  65. // is receieved.
  66. String* spew = new String(message);
  67. Win::PostMessage(
  68. spewWindow,
  69. SpewDialog::WM_UPDATE_SPEWAGE,
  70. readMessageCount,
  71. reinterpret_cast<LPARAM>(spew));
  72. }
  73. HRESULT
  74. ReadSpew(HANDLE pipe, HWND spewWindow)
  75. {
  76. LOG_FUNCTION(ReadSpew);
  77. ASSERT(pipe != INVALID_HANDLE_VALUE);
  78. ASSERT(Win::IsWindow(spewWindow));
  79. HRESULT hr = S_OK;
  80. String messageBuffer;
  81. messageBuffer.reserve(2048);
  82. static const size_t SPEW_BUF_MAX_CHARACTERS = 1023;
  83. static const size_t SPEW_BUF_MAX_BYTES =
  84. SPEW_BUF_MAX_CHARACTERS * sizeof(wchar_t);
  85. wchar_t buf[SPEW_BUF_MAX_CHARACTERS + 1];
  86. BYTE* byteBuf = reinterpret_cast<BYTE*>(buf);
  87. for (;;)
  88. {
  89. DWORD bytesRead = 0;
  90. hr =
  91. Win::ReadFile(
  92. pipe,
  93. byteBuf,
  94. SPEW_BUF_MAX_BYTES,
  95. bytesRead,
  96. 0);
  97. if (hr == Win32ToHresult(ERROR_MORE_DATA))
  98. {
  99. // don't break: we will pick up the rest of the message in the
  100. // next pass
  101. }
  102. else
  103. {
  104. BREAK_ON_FAILED_HRESULT(hr);
  105. }
  106. if (!bytesRead)
  107. {
  108. // it's possbile that the client wrote zero bytes.
  109. continue;
  110. }
  111. // force null termination
  112. byteBuf[bytesRead] = 0;
  113. byteBuf[bytesRead + 1] = 0;
  114. ++readMessageCount;
  115. String message(buf);
  116. if (messageBuffer.length() + message.length() >= 2048)
  117. {
  118. // flush the buffer
  119. AddSpewMessage(
  120. spewWindow,
  121. messageBuffer,
  122. readMessageCount);
  123. messageBuffer.erase();
  124. messageBuffer.reserve(2048);
  125. }
  126. messageBuffer.append(message);
  127. }
  128. // flush the buffer
  129. AddSpewMessage(
  130. spewWindow,
  131. messageBuffer,
  132. readMessageCount);
  133. return hr;
  134. }
  135. void _cdecl
  136. ReaderThreadProc(void* p)
  137. {
  138. LOG_FUNCTION(ReaderThreadProc);
  139. ASSERT(p);
  140. if (!p)
  141. {
  142. return;
  143. }
  144. // copy what we need from p, then delete it.
  145. ReaderThreadParams* params = reinterpret_cast<ReaderThreadParams*>(p);
  146. HWND spewWindow = params->hwnd;
  147. int* endReaderThread = params->endFlag;
  148. String appName = params->appName;
  149. delete params;
  150. params = 0;
  151. p = 0;
  152. HRESULT hr = S_OK;
  153. do
  154. {
  155. // create and connect to the pipe
  156. HANDLE pipe = INVALID_HANDLE_VALUE;
  157. hr = CreateAndConnectSpewPipe(appName, pipe);
  158. if (FAILED(hr))
  159. {
  160. AddSpewMessage(
  161. spewWindow,
  162. String::format(L"Connect failed: %1!08X!", hr));
  163. popup.Error(
  164. spewWindow,
  165. hr,
  166. IDS_ERROR_CONNECTING);
  167. break;
  168. }
  169. // read the contents of the slot until the thread is flagged to die,
  170. // or a failure occurs.
  171. while (!*endReaderThread)
  172. {
  173. hr = ReadSpew(pipe, spewWindow);
  174. if (FAILED(hr))
  175. {
  176. LOG_HRESULT(hr);
  177. *endReaderThread = 1;
  178. popup.Error(
  179. spewWindow,
  180. hr,
  181. IDS_ERROR_READING);
  182. }
  183. }
  184. // we're supposed to stop.
  185. // this will terminate the client's connection too.
  186. hr = Win::DisconnectNamedPipe(pipe);
  187. BREAK_ON_FAILED_HRESULT(hr);
  188. // this will delete the pipe instance
  189. Win::CloseHandle(pipe);
  190. // Tell the viewer to enable the start button
  191. Win::PostMessage(spewWindow, SpewDialog::WM_ENABLE_START, 0, 0);
  192. }
  193. while (0);
  194. }
  195. // CODEWORK:
  196. // stop button does not work, as ReadFile is blocking, and reader thread
  197. // never gets around to evaluating end flag.
  198. // therefore, restart and cancel buttons do not work either
  199. // to allow non-blocking reads of the pipe, need to use async read so
  200. // reader thread is not blocked waiting for client write
  201. // replace end flag with event triggered when cancel or stop pressed.
  202. // static alloc overlapped struct
  203. // put ptr to spew window in overlapped hEvent member
  204. // call ReadFileEx
  205. //
  206. // while WaitForMultpleObjects(cancel event, read, timeout)
  207. // if timeout
  208. // flush read buffer
  209. // if io complete, do nothing
  210. // if cancel signalled,
  211. // flush read buffer,
  212. // cancel io
  213. // exit loop
  214. // if other error
  215. // flush read buffer,
  216. // cancel io
  217. // exit loop
  218. //
  219. // completion callback
  220. // if error != ERROR_OPERATION_ABORTED
  221. // append to read buffer
  222. // if buffer full, flush to spew window
  223. // call ReadFileEx
  224. // will this eat stack forever?