forked from yavfast/dbg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbgSyncObjsProfiler.pas
254 lines (208 loc) · 7.74 KB
/
DbgSyncObjsProfiler.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
unit DbgSyncObjsProfiler;
interface
uses System.Classes, WinApi.Windows, Collections.Queues, DbgHookTypes,
System.SysUtils, System.SyncObjs, DebugerTypes;
type
TSyncObjsInfoQueue = TQueue<PDbgSyncObjsInfoListBuf>;
TDbgSyncObjsProfiler = class
private
FSyncObjsInfoQueue: TSyncObjsInfoQueue;
FSyncObjsTracking: LongBool;
procedure SetSyncObjsTracking(const Value: LongBool);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function ProcessSyncObjsInfoQueue: LongBool;
procedure LoadSyncObjsInfoPackEx(const SyncObjsInfoPack: Pointer; const Count: Cardinal);
procedure ProcessSyncObjsInfoBuf(const Buf: PDbgSyncObjsInfoListBuf);
property SyncObjsInfoQueue: TSyncObjsInfoQueue read FSyncObjsInfoQueue;
property SyncObjsTracking: LongBool read FSyncObjsTracking write SetSyncObjsTracking;
end;
implementation
uses Debuger, CollectList;
const
_MAX_SYNC_OBJS_INFO_BUF_COUNT = 512;
{ TDbgSyncObjsProfiler }
procedure TDbgSyncObjsProfiler.Clear;
begin
FSyncObjsInfoQueue.Clear;
end;
constructor TDbgSyncObjsProfiler.Create;
begin
inherited;
FSyncObjsInfoQueue := TSyncObjsInfoQueue.Create(True);
FSyncObjsInfoQueue.Capacity := _MAX_SYNC_OBJS_INFO_BUF_COUNT + 1;
end;
destructor TDbgSyncObjsProfiler.Destroy;
begin
FSyncObjsInfoQueue.Free;
inherited;
end;
procedure TDbgSyncObjsProfiler.LoadSyncObjsInfoPackEx(const SyncObjsInfoPack: Pointer; const Count: Cardinal);
var
Buf: PDbgSyncObjsInfoListBuf;
begin
if not SyncObjsTracking then
Exit;
while FSyncObjsInfoQueue.Count >= _MAX_SYNC_OBJS_INFO_BUF_COUNT do
SwitchToThread;
Buf := AllocMem(SizeOf(TDbgSyncObjsInfoListBuf));
Buf^.Count := Count;
Buf^.DbgSyncObjsInfoList := AllocMem(Count * SizeOf(TDbgSyncObjsInfo));
Buf^.DbgPointIdx := gvDebuger.ProcessData.CurDbgPointIdx;
if gvDebuger.ReadData(SyncObjsInfoPack, Buf^.DbgSyncObjsInfoList, Count * SizeOf(TDbgSyncObjsInfo)) then
FSyncObjsInfoQueue.Enqueue(Buf)
else
RaiseDebugCoreException();
end;
procedure TDbgSyncObjsProfiler.ProcessSyncObjsInfoBuf(const Buf: PDbgSyncObjsInfoListBuf);
var
ThData: PThreadData;
function FindCSLink(const CSData: PRTLCriticalSection): PSyncObjsInfo;
var
Idx: Integer;
begin
for Idx := ThData^.DbgSyncObjsInfo.Count - 1 downto 0 do
begin
Result := ThData^.DbgSyncObjsInfo[Idx];
if (Result^.SyncObjsInfo.SyncObjsType = soInCriticalSection) and
(Result^.Link = nil) and
(Result^.SyncObjsInfo.CS = CSData) and
(Result^.SyncObjsInfo.SyncObjsStateType = sosEnter)
then
Exit;
end;
Result := nil;
end;
var
Idx: Integer;
SyncObjsInfo: PDbgSyncObjsInfo;
ThSyncObjsInfo: PSyncObjsInfo;
SyncObjsLink: PSyncObjsInfo;
SyncObjsLinkExt: PSyncObjsInfo;
begin
ThData := Nil;
for Idx := 0 to Buf^.Count - 1 do
begin
SyncObjsInfo := @Buf^.DbgSyncObjsInfoList^[Idx];
if (ThData = Nil) or (ThData^.ThreadID <> SyncObjsInfo^.ThreadId) then
ThData := gvDebuger.GetThreadData(SyncObjsInfo^.ThreadId, True);
if ThData = Nil then
Continue; // TODO:  êàêèõ-òî ñëó÷àÿõ ñþäà çàõîäèò
//RaiseDebugCoreException();
case SyncObjsInfo^.SyncObjsType of
soSleep, soWaitForSingleObject, soWaitForMultipleObjects, soEnterCriticalSection, soInCriticalSection, soSendMessage:
begin
ThData^.DbgSyncObjsInfo.BeginRead;
try
SyncObjsLink := nil;
SyncObjsLinkExt := nil;
if SyncObjsInfo^.SyncObjsStateType = sosLeave then
begin
// Ïîèñê sosEnter âûçîâà
if SyncObjsInfo^.SyncObjsType = soInCriticalSection then
begin
// Òàê êàê Id ñîáûòèÿ âûõîäà íå ñîâïàäàåò ñ Id âõîäà, òî èùåì ïî óêàçàòåëþ CS
// Íåîáõîäèìî íàéòè ïîñëåäíåå ñîáûòèå ïî CS ñ SyncObjsStateType = sosEnter
SyncObjsLink := FindCSLink(SyncObjsInfo^.CS);
end
else
begin
// Ó îñòàëüíûõ òèïîâ Id ñîáûòèÿ âõîäà è âûõîäà áóäóò ñîâïàäàòü
if ThData^.DbgSyncObjsInfoByID.TryGetValue(SyncObjsInfo^.Id, SyncObjsLink) then
begin
// Óäàëÿåì îòðàáîòàííûé Id èç ñëîâàðÿ, êðîìå EnterCriticalSection,
// êîòîðûé åù¸ íóæåí äëÿ soInCriticalSection
if SyncObjsInfo^.SyncObjsType <> soEnterCriticalSection then
ThData^.DbgSyncObjsInfoByID.Remove(SyncObjsInfo^.Id);
end;
end;
end
else // sosEnter
begin
if SyncObjsInfo^.SyncObjsType = soInCriticalSection then
begin
// Èùåì ëèíê íà soEnterCriticalSection
if ThData^.DbgSyncObjsInfoByID.TryGetValue(SyncObjsInfo^.Id, SyncObjsLinkExt) then
ThData^.DbgSyncObjsInfoByID.Remove(SyncObjsInfo^.Id);
end;
end;
// Äîáàâëÿåì èíôó ïðî íîâûé ýëåìåíò
ThSyncObjsInfo := ThData^.DbgSyncObjsInfo.Add;
if ThData^.State = tsFinished then
ThSyncObjsInfo^.PerfIdx := PThreadPoint(ThData^.DbgPoints[ThData^.DbgPoints.Count - 1])^.PerfIdx
else
ThSyncObjsInfo^.PerfIdx := Buf^.DbgPointIdx;
// Ëèíê íà ïàðó
ThSyncObjsInfo^.Link := SyncObjsLink;
if SyncObjsLink <> nil then
SyncObjsLink^.Link := ThSyncObjsInfo;
// Âíåøíèé ëèíê
ThSyncObjsInfo^.LinkExt := SyncObjsLinkExt;
if SyncObjsLinkExt <> nil then
SyncObjsLinkExt^.LinkExt := ThSyncObjsInfo;
// Êîïèðóåì èíôó èç áóôåðà, òàê êàê îí ïîòîì áóäåò óíè÷òîæåí
ThSyncObjsInfo^.SyncObjsInfo.Init(SyncObjsInfo);
ThData^.DbgSyncObjsInfo.Commit;
// Äîáàâëÿåì èíôó ïðî sosEnter âûçîâû
if SyncObjsInfo^.SyncObjsStateType = sosEnter then
begin
if SyncObjsInfo^.SyncObjsType <> soInCriticalSection then
ThData^.DbgSyncObjsInfoByID.AddOrSetValue(SyncObjsInfo^.Id, ThSyncObjsInfo);
end;
// Ôîðìèðóåì ñòåê âûçîâà
case SyncObjsInfo^.SyncObjsType of
soEnterCriticalSection, soInCriticalSection,
soSendMessage,
soWaitForSingleObject, soWaitForMultipleObjects:
begin
ThData^.DbgSyncObjsUnitList.LoadStack(ThSyncObjsInfo);
end;
end;
finally
ThData^.DbgSyncObjsInfo.EndRead;
end;
end;
soLeaveCriticalSection:
begin
// TODO:
end;
end;
end;
end;
function TDbgSyncObjsProfiler.ProcessSyncObjsInfoQueue: LongBool;
var
Buf: PDbgSyncObjsInfoListBuf;
begin
Result := False;
if not SyncObjsTracking then
Exit;
if FSyncObjsInfoQueue.Count > 0 then
begin
try
// Ïðîïóñêàåì íåäàâíèå ñîáûòèÿ äëÿ êîððåêòíîé îáðàáîòêè êîðîòêèõ êðèòè÷åñêèõ ñåêöèé
if FSyncObjsInfoQueue.Count < _MAX_SYNC_OBJS_INFO_BUF_COUNT then
begin
Buf := FSyncObjsInfoQueue.First;
if (gvDebuger.ProcessData.CurDbgPointIdx - Buf^.DbgPointIdx) <= 2 then
Exit;
end;
Buf := FSyncObjsInfoQueue.Dequeue;
try
ProcessSyncObjsInfoBuf(Buf);
finally
FreeMemory(Buf^.DbgSyncObjsInfoList);
FreeMemory(Buf);
end;
Result := True;
except
on E: Exception do ; // TODO:
end;
end;
end;
procedure TDbgSyncObjsProfiler.SetSyncObjsTracking(const Value: LongBool);
begin
FSyncObjsTracking := Value;
end;
end.