-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDbgOut.cpp
201 lines (171 loc) · 6.42 KB
/
DbgOut.cpp
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
// DbgOut.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "WinUtils.h"
#include <string>
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
WORD wAttributes = 0;
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
if (wAttributes != 0)
SetConsoleTextAttribute(hStdOut, wAttributes);
return FALSE;
}
struct DbgData
{
DWORD dwProcessId;
char data[4096 - sizeof(DWORD)];
};
struct CompareNoCase
{
bool operator() (const std::wstring& a, const std::wstring& b) const
{
return _tcsicmp(a.c_str(), b.c_str()) < 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
bool bDisplayABout = false;
std::set<DWORD> filterPid;
std::set<std::wstring, CompareNoCase> filterExe;
bool ret = 0;
// TODO Allow filter Pid and Exe together
// TODO Filter Exe using wildcards
for (int arg = 1; arg < argc; ++arg)
{
if (_tcscmp(argv[arg], L"/?") == 0)
bDisplayABout = true;
else if (_istdigit(argv[arg][0]))
filterPid.insert(_tstoi(argv[arg]));
else if (_istalpha(argv[arg][0]))
filterExe.insert(argv[arg]);
else
{
_ftprintf(stderr, L"Unknown argument: %s\n", argv[arg]);
ret = 1;
}
}
if (bDisplayABout)
{
DisplayAboutMessage(NULL);
return 0;
}
if (ret != 0)
{
return ret;
}
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi = {};
GetConsoleScreenBufferInfo(hStdOut, &csbi);
wAttributes = csbi.wAttributes;
SetConsoleCtrlHandler(CtrlHandler, TRUE);
#if 0
LPCTSTR DBWinMutex = L"DBWinMutex";
HandlePtr hDBWinMutex(::OpenMutex(MUTEX_ALL_ACCESS, FALSE, DBWinMutex));
if (!hDBWinMutex)
{
DWORD errorCode = GetLastError();
_ftprintf(stderr, L"Error opening %s: %d\n", DBWinMutex, errorCode);
return errorCode;
}
#endif
LPCTSTR DBWIN_BUFFER_READY = L"DBWIN_BUFFER_READY";
HandlePtr hEventBufferReady(::OpenEvent(EVENT_ALL_ACCESS, FALSE, DBWIN_BUFFER_READY));
if (!hEventBufferReady)
{
hEventBufferReady.reset(CreateEvent(NULL, FALSE, TRUE, DBWIN_BUFFER_READY)); // auto-reset, initial state: signaled
if (!hEventBufferReady)
{
DWORD errorCode = GetLastError();
_ftprintf(stderr, L"Error opening %s: %d\n", DBWIN_BUFFER_READY, errorCode);
return errorCode;
}
}
LPCTSTR DBWIN_DATA_READY = L"DBWIN_DATA_READY";
HandlePtr hEventDataReady(::OpenEvent(SYNCHRONIZE, FALSE, DBWIN_DATA_READY));
if (!hEventDataReady)
{
hEventDataReady.reset(::CreateEvent(NULL, FALSE, FALSE, DBWIN_DATA_READY)); // auto-reset, initial state: nonsignaled
if (!hEventDataReady)
{
DWORD errorCode = GetLastError();
_ftprintf(stderr, L"Error opening %s: %d\n", DBWIN_DATA_READY, errorCode);
return errorCode;
}
}
LPCTSTR DBWIN_BUFFER = L"DBWIN_BUFFER";
HandlePtr hDBMonBuffer(::OpenFileMapping(FILE_MAP_READ, FALSE, DBWIN_BUFFER));
if (!hDBMonBuffer)
{
hDBMonBuffer.reset(CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(DbgData), DBWIN_BUFFER));
if (!hDBMonBuffer)
{
DWORD errorCode = GetLastError();
_ftprintf(stderr, L"Error opening %s: %d\n", DBWIN_BUFFER, errorCode);
return errorCode;
}
}
DbgData* pDBBuffer = (DbgData*) ::MapViewOfFile(hDBMonBuffer.get(), SECTION_MAP_READ, 0, 0, 0);
if (!pDBBuffer)
{
DWORD errorCode = GetLastError();
_ftprintf(stderr, L"Error MapViewOfFile: %d\n", errorCode);
return errorCode;
}
WORD Colors[] = {
FOREGROUND_BLUE | FOREGROUND_INTENSITY,
FOREGROUND_GREEN | FOREGROUND_INTENSITY,
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
FOREGROUND_RED | FOREGROUND_INTENSITY,
FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY,
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY,
};
WORD bgColor = csbi.wAttributes & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
for (int i = 0; i < ARRAYSIZE(Colors); ++i)
{
if (Colors[i] == (csbi.wAttributes & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY)))
Colors[i] = (bgColor >> 4) | FOREGROUND_INTENSITY;
}
while (true)
{
DWORD ret = ::WaitForSingleObject(hEventDataReady.get(), INFINITE);
if (ret == WAIT_OBJECT_0)
{
if (filterPid.empty() || filterPid.find(pDBBuffer->dwProcessId) != filterPid.end())
{
SYSTEMTIME rTimeDate;
GetLocalTime(&rTimeDate);
wchar_t strDate[256] = L"";
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &rTimeDate, NULL, strDate, ARRAYSIZE(strDate));
wchar_t strTime[256] = L"";
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &rTimeDate, NULL, strTime, ARRAYSIZE(strTime));
wchar_t strProcessName[MAX_PATH] = L"";
HandlePtr hProcess(OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pDBBuffer->dwProcessId));
if (hProcess)
{
GetProcessImageFileName(hProcess.get(), strProcessName, ARRAYSIZE(strProcessName));
}
LPCTSTR strExeName = _tcsrchr(strProcessName, '\\');
strExeName = strExeName ? strExeName + 1 : strProcessName;
if (filterExe.empty() || filterExe.find(strExeName) != filterExe.end())
{
SetConsoleTextAttribute(hStdOut, bgColor | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
wprintf(L"%s %s ", strDate, strTime);
SetConsoleTextAttribute(hStdOut, bgColor | Colors[pDBBuffer->dwProcessId % ARRAYSIZE(Colors)]);
wprintf(L"%d %s ", pDBBuffer->dwProcessId, strExeName);
SetConsoleTextAttribute(hStdOut, csbi.wAttributes);
printf(pDBBuffer->data);
}
}
SetEvent(hEventBufferReady.get());
}
}
SetConsoleTextAttribute(hStdOut, csbi.wAttributes);
if (pDBBuffer != NULL)
{
::UnmapViewOfFile(pDBBuffer);
pDBBuffer = NULL;
}
return ret;
}