-
Notifications
You must be signed in to change notification settings - Fork 20
/
LogViewer.Receivers.Winods.pas
440 lines (377 loc) · 11.4 KB
/
LogViewer.Receivers.Winods.pas
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
{
Copyright (C) 2013-2022 Tim Sinaeve [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}
unit LogViewer.Receivers.Winods;
{ Winods channel receiver. }
interface
uses
Winapi.Windows,
System.Classes,
Spring, Spring.Collections,
LogViewer.Interfaces, LogViewer.Receivers.Base,
LogViewer.Receivers.Winods.Settings;
{$REGION 'documentation'}
{
https://stackoverflow.com/questions/
509498/in-delphi-is-outputdebugstring-thread-safe?rq=1
When OutputDebugString() is called by an application, it takes these steps.
Note that a failure at any point abandons the whole thing and treats the
debugging request as a no-op (the string isn't sent anywhere).
1. Open DBWinMutex and wait until we have exclusive access to it.
2. Map the DBWIN_BUFFER segment into memory: if it's not found, there is no
debugger running so the entire request is ignored.
3. Open the DBWIN_BUFFER_READY and DBWIN_DATA_READY events. As with the shared
memory segment, missing objects mean that no debugger is available.
4. Wait for the DBWIN_BUFFER_READY event to be signaled: this says that the
memory buffer is no longer in use. Most of the time, this event will be
signaled immediately when it's examined, but it won't wait longer than 10
seconds for the buffer to become ready (a timeout abandons the request).
5. Copy up to about 4kbytes of data to the memory buffer, and store the
current process ID there as well. Always put a NUL byte at the end of the
string.
6. Tell the debugger that the buffer is ready by setting the DBWIN_DATA_READY
event. The debugger takes it from there.
7. Release the mutex
8. Close the Event and Section objects, though we keep the handle to the mutex
around for later.
}
{$ENDREGION}
type
PDBWinBuffer = ^TDBWinBuffer;
TDBWinBuffer = record
ProcessId : DWORD;
Data : array[0..(4096 - SizeOf(DWORD)) - 1] of AnsiChar;
end;
TOdsMessageReceivedEvent = procedure(
const AString : AnsiString;
AProcessId : UInt32
) of object;
TWinDebugMonitor = class
private
FOnMessageReceived : TOdsMessageReceivedEvent;
FHDBWinMutex : THandle;
FHDBMonBuffer : THandle;
FHEventBufferReady : THandle;
FHEventDataReady : THandle;
FHWinDebugMonitorThread : THandle;
FWinDebugMonStopped : Boolean;
FPDBBuffer : PDBWinBuffer;
function Initialize: DWORD;
procedure Uninitialize;
function WinDebugMonitorProcess: DWORD;
protected
procedure DoMessageReceived(
const AString : AnsiString;
AProcessId : UInt32
);
public
constructor Create;
destructor Destroy; override;
property OnMessageReceived : TOdsMessageReceivedEvent
read FOnMessageReceived write FOnMessageReceived;
end;
type
TWinodsChannelReceiver = class(TChannelReceiver, IChannelReceiver, IWinods)
private
FDebugMonitor : TWinDebugMonitor;
FBuffer : TMemoryStream;
function GetSettings: TWinodsSettings;
procedure SettingsChanged(Sender: TObject);
procedure FDebugMonitorMessageReceived(
const AString : AnsiString;
AProcessId : UInt32
);
public
procedure AfterConstruction; override;
destructor Destroy; override;
property Settings: TWinodsSettings
read GetSettings;
end;
implementation
uses
System.SysUtils,
Spring.Helpers,
DDuce.Logger, DDuce.Logger.Interfaces, DDuce.Utils.Winapi,
LogViewer.Subscribers.Winods;
{$REGION 'TWinDebugMonitor'}
// ----------------------------------------------------------------------------
// PROPERTIES OF OBJECTS
// ----------------------------------------------------------------------------
// NAME | DBWinMutex DBWIN_BUFFER_READY DBWIN_DATA_READY
// ----------------------------------------------------------------------------
// TYPE | Mutex Event Event
// ACCESS | All All Sync
// INIT STATE | ? Signaled Nonsignaled
// PROPERTY | ? Auto-Reset Auto-Reset
// ----------------------------------------------------------------------------
constructor TWinDebugMonitor.Create;
begin
Logger.Track(Self, 'Create');
inherited Create;
if Initialize <> 0 then
begin
//OutputDebugString('TWinDebugMonitor.Initialize failed.'#10);
end;
end;
destructor TWinDebugMonitor.Destroy;
begin
Logger.Track(Self, 'Destroy');
Uninitialize;
inherited Destroy;
end;
procedure TWinDebugMonitor.DoMessageReceived(const AString: AnsiString;
AProcessId: UInt32);
begin
if Assigned(FOnMessageReceived) then
FOnMessageReceived(AString, AProcessId);
end;
function WinDebugMonitorThread(pData: Pointer): DWORD; stdcall;
var
WDB : TWinDebugMonitor;
begin
WDB := TWinDebugMonitor(pData);
if WDB <> nil then
begin
while not WDB.FWinDebugMonStopped do
begin
WDB.WinDebugMonitorProcess;
end;
end;
Result := 0;
end;
function TWinDebugMonitor.Initialize: DWORD;
var
LThreadId : DWORD;
begin
Result := 0;
SetLastError(0);
// Mutex: DBWin
// ---------------------------------------------------------
FHDBWinMutex := OpenMutex(MUTEX_ALL_ACCESS, False, 'DBWin');
if FHDBWinMutex = 0 then
begin
FHDBWinMutex := CreateMutex(nil, LongBool(1), 'DBWin');
if FHDBWinMutex = 0 then
begin
Result := GetLastError;
Exit;
end;
end;
// Event: buffer ready
// ---------------------------------------------------------
FHEventBufferReady := OpenEvent(EVENT_ALL_ACCESS, False, 'DBWIN_BUFFER_READY');
if FHEventBufferReady = 0 then
begin
FHEventBufferReady := CreateEvent(nil, False, TRUE, 'DBWIN_BUFFER_READY');
if FHEventBufferReady = 0 then
begin
Result := GetLastError;
Exit;
end;
end;
// Event: data ready
FHEventDataReady := OpenEvent(SYNCHRONIZE, False, 'DBWIN_DATA_READY');
if FHEventDataReady = 0 then
begin
FHEventDataReady := CreateEvent(nil, False, False, 'DBWIN_DATA_READY');
if FHEventDataReady = 0 then
begin
Result := GetLastError;
end;
end;
// Shared memory
FHDBMonBuffer := OpenFileMapping(FILE_MAP_READ, False, 'DBWIN_BUFFER');
if FHDBMonBuffer = 0 then
begin
begin
FHDBMonBuffer := CreateFileMapping(
INVALID_HANDLE_VALUE,
nil,
PAGE_READWRITE,
0,
SizeOf(TDBWinBuffer),
'DBWIN_BUFFER'
);
if FHDBMonBuffer = 0 then
begin
Result := GetLastError;
Exit;
end;
end;
FPDBBuffer := PDBWinBuffer(
MapViewOfFile(FHDBMonBuffer, SECTION_MAP_READ, 0, 0, 0)
);
if FPDBBuffer = nil then
begin
Result := GetLastError;
Exit;
end;
// Monitoring thread
FWinDebugMonStopped := False;
FHWinDebugMonitorThread := CreateThread(
nil,
0,
@WinDebugMonitorThread,
Pointer(Self),
0,
LThreadId
);
if FHWinDebugMonitorThread = 0 then
begin
FWinDebugMonStopped := True;
Result := GetLastError;
Exit;
end;
// set monitor thread priority to highest
SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(FHWinDebugMonitorThread, THREAD_PRIORITY_TIME_CRITICAL);
Result := 0;
end;
end;
procedure TWinDebugMonitor.Uninitialize;
begin
Logger.Track(Self, 'Uninitialize');
if FHWinDebugMonitorThread <> 0 then
begin
FWinDebugMonStopped := True;
WaitForSingleObject(FHWinDebugMonitorThread, INFINITE);
CloseHandle(FHWinDebugMonitorThread);
FHWinDebugMonitorThread := 0;
end;
if FHDBWinMutex <> 0 then
begin
CloseHandle(FHDBWinMutex);
FHDBWinMutex := 0;
end;
if FPDBBuffer <> nil then
begin
UnmapViewOfFile(FPDBBuffer);
FPDBBuffer := nil;
end;
if FHDBMonBuffer <> 0 then
begin
CloseHandle(FHDBMonBuffer);
FHDBMonBuffer := 0;
end;
if FHEventBufferReady <> 0 then
begin
CloseHandle(FHEventBufferReady);
FHEventBufferReady := 0;
end;
if FHEventDataReady <> 0 then
begin
CloseHandle(FHEventDataReady);
FHEventDataReady := 0;
end;
end;
function TWinDebugMonitor.WinDebugMonitorProcess: DWORD;
const
TIMEOUT_WIN_DEBUG = 100;
begin
// wait for data ready
Result := WaitForSingleObject(FHEventDataReady, TIMEOUT_WIN_DEBUG);
if Result = WAIT_OBJECT_0 then
begin
//TThread.CurrentThread.Queue(
TThread.CurrentThread.Synchronize(
TThread.CurrentThread,
procedure
begin
DoMessageReceived(FPDBBuffer^.Data, FPDBBuffer.ProcessId);
end
);
// signal buffer ready
SetEvent(FHEventBufferReady);
end;
end;
{$ENDREGION}
{$REGION 'TWinODSChannelReceiver'}
{$REGION 'construction and destruction'}
procedure TWinodsChannelReceiver.AfterConstruction;
begin
Logger.Track(Self, 'AfterConstruction');
inherited AfterConstruction;
FBuffer := TMemoryStream.Create;
FDebugMonitor := TWinDebugMonitor.Create;
FDebugMonitor.OnMessageReceived := FDebugMonitorMessageReceived;
Settings.OnChanged.Add(SettingsChanged);
end;
destructor TWinodsChannelReceiver.Destroy;
begin
Logger.Track(Self, 'Destroy');
PollTimer.Enabled := False;
FDebugMonitor.OnMessageReceived := nil;
FDebugMonitor.Free;
FBuffer.Free;
inherited Destroy;
end;
{$ENDREGION}
{$REGION 'property access methods'}
function TWinodsChannelReceiver.GetSettings: TWinodsSettings;
begin
Result := Manager.Settings.WinodsSettings;
end;
{$ENDREGION}
{$REGION 'event handlers'}
procedure TWinodsChannelReceiver.FDebugMonitorMessageReceived(
const AString: AnsiString; AProcessId: UInt32);
const
ZERO_BUF : Integer = 0;
var
LTextSize : Integer;
LText : AnsiString;
LMsgType : Byte;
LDummy : Byte;
LProcessName : string;
LSubscriber : ISubscriber;
begin
Logger.Track(Self, 'FDebugMonitorMessageReceived');
if Enabled then
begin
LDummy := 0;
begin
FBuffer.Clear;
LText := #13#10 + AString;
LMsgType := Integer(lmtText);
LTextSize := Length(LText);
FBuffer.Seek(0, soFromBeginning);
FBuffer.WriteBuffer(LMsgType);
FBuffer.WriteBuffer(LDummy);
FBuffer.WriteBuffer(LDummy);
FBuffer.WriteBuffer(LDummy);
FBuffer.WriteBuffer(Now);
FBuffer.WriteBuffer(LTextSize);
FBuffer.WriteBuffer(LText[1], LTextSize);
FBuffer.WriteBuffer(ZERO_BUF);
if not Processes.TryGetValue(AProcessId, LProcessName) then
begin
LProcessName := GetExenameForProcess(AProcessId);
Processes.AddOrSetValue(AProcessId, LProcessName);
end;
if not SubscriberList.TryGetValue(AProcessId, LSubscriber) then
begin
LSubscriber := TWinodsSubscriber.Create(
Self, AProcessId, '', LProcessName, True
);
SubscriberList.AddOrSetValue(AProcessId, LSubscriber);
end;
LSubscriber.DoReceiveMessage(FBuffer);
end;
end;
end;
procedure TWinodsChannelReceiver.SettingsChanged(Sender: TObject);
begin
Enabled := Settings.Enabled;
end;
{$ENDREGION}
{$ENDREGION}
end.