-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
265 lines (213 loc) · 6.28 KB
/
Copy pathmain.cpp
File metadata and controls
265 lines (213 loc) · 6.28 KB
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
#define _UNICODE
#define UNICODE
#include <stlsoft/stlsoft.h>
#if _STLSOFT_VER < 0x010b01c3
# error requires STLSoft v1.11.1-rc3 or later
#endif
#if __cplusplus < 201702L
# error requires C++17 or later
#endif
#include <platformstl/filesystem/path_functions.h>
#include <stlsoft/smartptr/scoped_handle.hpp>
#include <stlsoft/util/string/snprintf.h>
#include <winstl/exception/winstl_exception.hpp>
#include <winstl/exception/access_exception.hpp>
#include <winstl/filesystem/memory_mapped_file.hpp>
#include <winstl/synch/event.hpp>
#include <psapi.h>
#include <Windows.h>
#include <cstdlib>
#include <iostream>
#include <thread>
#define PROGRAM_VER_MAJOR 0
#define PROGRAM_VER_MINOR 0
#define PROGRAM_VER_PATCH 1
union Payload
{
#if defined(__GNUC__) && !defined(__clang__)
__extension__
#endif
struct
{
DWORD pid;
CHAR content[1];
};
UCHAR bytes[4096];
};
// [[noreturn]]
__declspec(noreturn)
void throw_(
DWORD le
, char const* msg
)
{
using namespace winstl;
if (ERROR_ACCESS_DENIED == le)
{
STLSOFT_THROW_X(access_exception(msg, le));
}
else
{
STLSOFT_THROW_X(winstl_exception(msg, le));
}
}
std::string GetProcessNameFromPid(DWORD pid) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
return "Error: Could not open process";
}
char buffer[MAX_PATH];
if (GetProcessImageFileNameA(hProcess, buffer, MAX_PATH) == 0) {
CloseHandle(hProcess);
return "Error: Could not get image file name";
}
CloseHandle(hProcess);
std::string fullPath(buffer);
size_t lastSlash = fullPath.find_last_of("\\");
std::string processName = (lastSlash == std::string::npos) ? fullPath : fullPath.substr(lastSlash + 1);
return processName;
}
// [[noreturn]]
__declspec(noreturn)
void run()
{
winstl::event ev_buffer_ready(L"DBWIN_BUFFER_READY", false, false);
winstl::event ev_data_ready(L"DBWIN_DATA_READY", false, false);
HANDLE const hFileMap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(Payload), "DBWIN_BUFFER");
if (NULL == hFileMap)
{
DWORD const le = ::GetLastError();
throw_(le, "failed to create file-mapping");
}
stlsoft::scoped_handle scoper_fm(hFileMap, ::CloseHandle);
Payload* const payload = (Payload*)::MapViewOfFile(hFileMap, SECTION_MAP_READ, 0, 0, 0);
if (NULL == payload)
{
DWORD const le = ::GetLastError();
throw_(le, "failed to map view");
}
HANDLE hStderr = GetStdHandle(STD_ERROR_HANDLE);
for (;;)
{
ev_buffer_ready.set();
DWORD const wait = ::WaitForSingleObject(ev_data_ready.handle(), INFINITE);
if (WAIT_OBJECT_0 != wait)
{
DWORD const le = ::GetLastError();
throw_(le, "failed to wait for buffer-ready");
}
size_t len = strlen(payload->content);
for ( ; 0 != len; --len)
{
if ('\r' != payload->content[len - 1] &&
'\n' != payload->content[len - 1])
{
break;
}
}
if (len != 0)
{
using stlsoft::stlsoft_C_snprintf;
char msg[4096];
int r = stlsoft_C_snprintf(msg, "%lu - %s: %.*s", payload->pid, GetProcessNameFromPid(payload->pid).c_str(), (int)len, payload->content);
DWORD numWritten;
if (r < 5)
{
::WriteFile(hStderr, "could not prepare output line\r\n", 31, &numWritten, NULL);
}
else
{
if (r > int(STLSOFT_NUM_ELEMENTS(msg) - 3))
{
msg[STLSOFT_NUM_ELEMENTS(msg) - 3] = '\r';
msg[STLSOFT_NUM_ELEMENTS(msg) - 2] = '\n';
msg[STLSOFT_NUM_ELEMENTS(msg) - 1] = '\0';
}
else
{
msg[r++] = '\r';
msg[r++] = '\n';
msg[r] = '\0';
}
::WriteFile(hStderr, msg, (DWORD)r, &numWritten, NULL);
}
}
}
}
int wmain(int argc, wchar_t* argv[])
{
stlsoft::string_slice_w_t const program_name = platformstl::get_executable_name_from_path(argv[0]);
try
{
switch (argc)
{
case 2:
if (0 == std::wcscmp(L"--help", argv[1]))
{
std::wcout
<< L"USAGE: "
<< program_name
<< L" [ { --help | --version } ]"
<< std::endl;
return EXIT_SUCCESS;
}
else
if (0 == std::wcscmp(L"--version", argv[1]))
{
std::wcout
<< program_name
<< L" v"
<< PROGRAM_VER_MAJOR
<< L'.'
<< PROGRAM_VER_MINOR
<< L'.'
<< PROGRAM_VER_PATCH
<< std::endl;
return EXIT_SUCCESS;
}
else
{
std::wcerr
<< program_name
<< L": unrecognised argument '"
<< argv[1]
<< L"'; use --help for usage"
<< std::endl;
return EXIT_FAILURE;
}
break;
case 1:
run();
#ifndef NDEBUG
fwprintf(
stderr
, L"%.*s: %s:%d: UNEXPECTED\n"
, int(program_name.len), program_name.ptr
, STLSOFT_STRINGIZE_w(__FILE__), __LINE__
);
::DebugBreak();
return EXIT_FAILURE;
#endif
default:
std::wcerr
<< program_name
<< L": too many arguments; use --help for usage"
<< std::endl;
return EXIT_FAILURE;
}
}
catch (std::bad_alloc&)
{
fputws(L"out of memory\n", stderr);
}
catch (std::exception& x)
{
fwprintf(
stderr
, L"%.*s: process failed: %S\n"
, int(program_name.len), program_name.ptr
, x.what()
);
}
return EXIT_SUCCESS;
}