File: dbgintfmiscclasses.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (568 lines) | stat: -rw-r--r-- 14,938 bytes parent folder | download | duplicates (3)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
unit DbgIntfMiscClasses;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LazClasses;

type

  (* TDbgEntityValue are created with a refcount of 0 (zero)
  *)

  TDbgEntityValuesList = class;
  TDbgEntitiesThreadStackList = class;

  { TDbgEntityValue
    Locals, Watches, Registers
  }

  TDbgEntityValue = class(TRefCountedObject)
  private
    FOwner: TDbgEntityValuesList;
    FFlags: set of (devImmutable);
    function GetImmutable: Boolean;
    function GetStackFrame: Integer;
    function GetThreadId: Integer;
    procedure SetImmutable(AValue: Boolean);
  protected
    procedure DoAssign({%H-}AnOther: TDbgEntityValue); virtual;
    property Owner: TDbgEntityValuesList read FOwner;
  public
    procedure Assign({%H-}AnOther: TDbgEntityValue);
    property ThreadId: Integer read GetThreadId;
    property StackFrame: Integer read GetStackFrame;
    property Immutable: Boolean read GetImmutable write SetImmutable; // mainly used by assert
  end;

  { TDbgEntityValuesList
    All Values for a specifer Thread/StackFrame
  }

  TDbgEntityValuesList = class(TRefCountedObject)
  private
    FStackFrame: Integer;
    FThreadId: Integer;
    FFlags: set of (devlImmutable);
    FList: TRefCntObjList;
    FOwner: TDbgEntitiesThreadStackList;
    function GetEntry(AnIndex: Integer): TDbgEntityValue;
    function GetImmutable: Boolean;
    procedure SetImmutable(AValue: Boolean);
  protected
    function  CreateEntry: TDbgEntityValue; virtual; abstract;
    procedure DoAssign(AnOther: TDbgEntityValuesList); virtual;      // assert other has same thread/stack
    procedure DoAssignListContent(AnOther: TDbgEntityValuesList); virtual;      // assert other has same thread/stack
    procedure DoCleared; virtual;
    procedure DoAdded({%H-}AnEntry: TDbgEntityValue); virtual;
    procedure Init; virtual;
    property Owner: TDbgEntitiesThreadStackList read FOwner;
  public
    constructor Create(AThreadId, AStackFrame: Integer);
    destructor Destroy; override;
    procedure Assign(AnOther: TDbgEntityValuesList);       // assert other has same thread/stack
    procedure Add(AnEntry: TDbgEntityValue);
    procedure Clear;
    function Count: Integer;
    property Entries[AnIndex: Integer]: TDbgEntityValue read GetEntry;

    property ThreadId: Integer read FThreadId;
    property StackFrame: Integer read FStackFrame;
    property Immutable: Boolean read GetImmutable write SetImmutable; // mainly used by assert
  end;

  TDbgValuesThreadList = record
    ThreadId: Integer;
    List: TRefCntObjList;
  end;

  { TDbgEntitiesThreadStackList }

  TDbgEntitiesThreadStackList = class(TRefCountedObject)
  private
    FList: array of TDbgValuesThreadList;
    FFlags: set of (devtsImmutable);
    function GetEntry(AThreadId, AStackFrame: Integer): TDbgEntityValuesList;
    function GetEntryByIdx(AnIndex: Integer): TDbgEntityValuesList;
    function GetHasEntry(AThreadId, AStackFrame: Integer): Boolean;
    function GetImmutable: Boolean;
    function IndexOfThread(AThreadId: Integer; ACreateSubList: Boolean = False): Integer;
    procedure SetImmutable(AValue: Boolean);
  protected
    function  CreateEntry(AThreadId, AStackFrame: Integer): TDbgEntityValuesList; virtual; abstract;
    procedure DoAssign(AnOther: TDbgEntitiesThreadStackList); virtual;
    procedure DoAssignListContent(AnOther: TDbgEntitiesThreadStackList); virtual;
    procedure DoCleared; virtual;
    procedure DoAdded({%H-}AnEntry: TDbgEntityValuesList); virtual;
  public
    destructor Destroy; override;
    procedure Assign(AnOther: TDbgEntitiesThreadStackList);
    procedure Add(AnEntry: TDbgEntityValuesList);
    procedure Clear;
    function Count: Integer;
    property EntriesByIdx[AnIndex: Integer]: TDbgEntityValuesList read GetEntryByIdx;
    // Entries will automatically be created
    property Entries[AThreadId, AStackFrame: Integer]: TDbgEntityValuesList read GetEntry; default;
    property HasEntry[AThreadId, AStackFrame: Integer]: Boolean read GetHasEntry;
    property Immutable: Boolean read GetImmutable write SetImmutable; // used by assert
  end;

  { TDelayedUdateItem }

  TDelayedUdateItem = class(TCollectionItem)
  private
    FUpdateCount: Integer;
    FDoChanged: Boolean;
  protected
    procedure Changed;
    procedure DoChanged; virtual;
    procedure DoEndUpdate; virtual; // even if not changed
  public
    procedure Assign(ASource: TPersistent); override;
    procedure BeginUpdate; virtual;
    constructor Create(ACollection: TCollection); override;
    procedure EndUpdate;
    function IsUpdating: Boolean;
  end;

  { TRefCountedColectionItem }

  TRefCountedColectionItem = class(TDelayedUdateItem)
  public
    constructor Create(ACollection: TCollection); override;
    destructor  Destroy; override;
    procedure AddReference;
    procedure ReleaseReference;
  private
    FRefCount: Integer;
  protected
    procedure DoFree; virtual;
    property  RefCount: Integer read FRefCount;
  end;

procedure ReleaseRefAndNil(var ARefCountedObject);

implementation

procedure ReleaseRefAndNil(var ARefCountedObject);
begin
  Assert( (Pointer(ARefCountedObject) = nil) or
          (TObject(ARefCountedObject) is TRefCountedObject) or
          (TObject(ARefCountedObject) is TRefCountedColectionItem),
         'ReleaseRefAndNil requires TRefCountedObject');

  if Pointer(ARefCountedObject) = nil then
    exit;

  if (TObject(ARefCountedObject) is TRefCountedObject) then
    TRefCountedObject(ARefCountedObject).ReleaseReference
  else
  if (TObject(ARefCountedObject) is TRefCountedColectionItem) then
    TRefCountedColectionItem(ARefCountedObject).ReleaseReference;

  Pointer(ARefCountedObject) := nil;
end;

{ TDbgEntityValue }

function TDbgEntityValue.GetImmutable: Boolean;
begin
  Result := (devImmutable in FFlags) or ((FOwner <> nil) and FOwner.Immutable);
end;

function TDbgEntityValue.GetStackFrame: Integer;
begin
  Result := FOwner.StackFrame;
end;

function TDbgEntityValue.GetThreadId: Integer;
begin
  Result := FOwner.ThreadId;
end;

procedure TDbgEntityValue.SetImmutable(AValue: Boolean);
begin
  assert((AValue = True) or not(Immutable), 'TDbgEntityValue.SetImmutable Not allowed to set to false');
  if AValue then Include(FFlags, devImmutable);
end;

procedure TDbgEntityValue.DoAssign(AnOther: TDbgEntityValue);
begin
  //
end;

procedure TDbgEntityValue.Assign(AnOther: TDbgEntityValue);
begin
  Assert(not Immutable, 'TDbgEntityValue.Assign Immutable');
  DoAssign(AnOther);
end;

{ TDbgEntityValuesList }

function TDbgEntityValuesList.GetImmutable: Boolean;
begin
  Result := devlImmutable in FFlags;
end;

function TDbgEntityValuesList.GetEntry(AnIndex: Integer): TDbgEntityValue;
begin
  Result := TDbgEntityValue(FList[AnIndex]);
end;

procedure TDbgEntityValuesList.SetImmutable(AValue: Boolean);
begin
  assert((AValue = True) or not(devlImmutable in FFlags), 'TDbgEntityValuesList.SetImmutable Not allowed to set to false');
  if AValue then Include(FFlags, devlImmutable);
end;

procedure TDbgEntityValuesList.DoCleared;
begin
  //
end;

procedure TDbgEntityValuesList.DoAdded(AnEntry: TDbgEntityValue);
begin

end;

procedure TDbgEntityValuesList.Init;
begin
  //
end;

constructor TDbgEntityValuesList.Create(AThreadId, AStackFrame: Integer);
begin
  inherited Create;
  FFlags := [];
  FThreadId   := AThreadId;
  FStackFrame := AStackFrame;
  FList := TRefCntObjList.Create;
  Init;
end;

destructor TDbgEntityValuesList.Destroy;
begin
  Exclude(FFlags, devlImmutable);
  Clear;
  FList.Free;
  inherited Destroy;
end;

procedure TDbgEntityValuesList.DoAssign(AnOther: TDbgEntityValuesList);
begin
  DoAssignListContent(AnOther);
end;

procedure TDbgEntityValuesList.DoAssignListContent(AnOther: TDbgEntityValuesList);
var
  e: TDbgEntityValue;
  i: Integer;
begin
  for i := 0 to AnOther.FList.Count - 1 do begin
    e := CreateEntry;
    e.FOwner := Self;
    e.Assign(TDbgEntityValue(AnOther.FList[i]));
    FList.Add(e);
  end;
end;

procedure TDbgEntityValuesList.Assign(AnOther: TDbgEntityValuesList);
begin
  Assert(not Immutable, 'TDbgEntityValuesList.Assign Immutable');
  Assert((FThreadId = AnOther.FThreadId) and (FStackFrame = AnOther.FStackFrame), 'TDbgEntityValuesList.Assign same thread and stack');

  Clear;
  DoAssign(AnOther);
end;

procedure TDbgEntityValuesList.Add(AnEntry: TDbgEntityValue);
begin
  Assert(not Immutable, 'TDbgEntityValuesList.Add  Immutable');
  AnEntry.FOwner := Self;
  FList.Add(AnEntry);
  DoAdded(AnEntry);
end;

procedure TDbgEntityValuesList.Clear;
var
  i: Integer;
begin
  Assert(not Immutable, 'TDbgEntityValuesList.Clear Immutable');
  if FList.Count = 0 then
    exit;
  for i := 0 to FList.Count - 1 do
    TDbgEntityValue(FList[i]).FOwner := nil;
  FList.Clear;
  DoCleared;
end;

function TDbgEntityValuesList.Count: Integer;
begin
  Result := FList.Count;
end;

{ TDbgEntitiesThreadStackList }

function TDbgEntitiesThreadStackList.GetEntry(AThreadId, AStackFrame: Integer): TDbgEntityValuesList;
var
  i, j: Integer;
begin
  i := IndexOfThread(AThreadId);
  if i >= 0 then begin
    // TODO: binary search / need sorted list
    for j := 0 to FList[i].List.Count - 1 do begin
      Result := TDbgEntityValuesList(FList[i].List[j]);
      if Result.StackFrame = AStackFrame then
        exit;
    end;
  end;

  if Immutable then begin
    Result := nil;
    exit;
  end;

  Result := CreateEntry(AThreadId, AStackFrame);
  Add(Result);
end;

function TDbgEntitiesThreadStackList.GetEntryByIdx(AnIndex: Integer): TDbgEntityValuesList;
var
  i: Integer;
begin
  Result := nil;
  i := 0;
  while AnIndex >= FList[i].List.Count do begin
    dec(AnIndex, FList[i].List.Count);
    inc(i);
    if i >= Length(FList) then
      exit;
  end;
  Result := TDbgEntityValuesList(FList[i].List[AnIndex]);
end;

function TDbgEntitiesThreadStackList.GetHasEntry(AThreadId, AStackFrame: Integer): Boolean;
var
  i, j: Integer;
begin
  Result := False;
  i := IndexOfThread(AThreadId);
  if i < 0 then exit;
  // TODO: binary search / need sorted list
  for j := 0 to FList[i].List.Count - 1 do begin
    if TDbgEntityValuesList(FList[i].List[j]).StackFrame = AStackFrame then begin
      Result := True;
      exit;
    end;
  end;
end;

function TDbgEntitiesThreadStackList.GetImmutable: Boolean;
begin
  Result := devtsImmutable in FFlags;
end;

function TDbgEntitiesThreadStackList.IndexOfThread(AThreadId: Integer;
  ACreateSubList: Boolean): Integer;
begin
  Result := length(FList) - 1;
  while (Result >= 0) and (FList[Result].ThreadId <> AThreadId) do
    dec(Result);
  if (Result >= 0) or (not ACreateSubList) then
    exit;

  Result := length(FList);
  SetLength(FList, Result + 1);
  FList[Result].ThreadId := AThreadId;
  FList[Result].List := TRefCntObjList.Create;
end;

procedure TDbgEntitiesThreadStackList.SetImmutable(AValue: Boolean);
begin
  assert((AValue = True) or not(devtsImmutable in FFlags), 'TDbgEntityValuesList.SetImmutable Not allowed to set to false');
  if AValue then Include(FFlags, devtsImmutable);
end;

procedure TDbgEntitiesThreadStackList.DoCleared;
begin
  //
end;

procedure TDbgEntitiesThreadStackList.DoAdded(AnEntry: TDbgEntityValuesList);
begin
  //
end;

destructor TDbgEntitiesThreadStackList.Destroy;
begin
  Exclude(FFlags, devtsImmutable);
  Clear;
  inherited Destroy;
end;

procedure TDbgEntitiesThreadStackList.DoAssign(AnOther: TDbgEntitiesThreadStackList);
begin
  DoAssignListContent(AnOther);
end;

procedure TDbgEntitiesThreadStackList.DoAssignListContent(AnOther: TDbgEntitiesThreadStackList);
var
  i, j: Integer;
  t: Integer;
  e, o: TDbgEntityValuesList;
begin
  SetLength(FList, length(AnOther.FList));
  for i := 0 to Length(FList) - 1 do begin
    t := AnOther.FList[i].ThreadId;
    FList[i].ThreadId := t;
    FList[i].List := TRefCntObjList.Create;
    for j := 0 to AnOther.FList[i].List.Count - 1 do begin
      o := TDbgEntityValuesList(AnOther.FList[i].List[j]);
      e := CreateEntry(t, o.StackFrame);
      e.FOwner := Self;
      e.Assign(o);
      FList[i].List.Add(e);
    end;
  end;
end;

procedure TDbgEntitiesThreadStackList.Assign(AnOther: TDbgEntitiesThreadStackList);
begin
  Assert(not Immutable, 'TDbgEntitiesThreadStackList.Assign Immutable');
  Clear;
  DoAssign(AnOther);
end;

procedure TDbgEntitiesThreadStackList.Add(AnEntry: TDbgEntityValuesList);
var
  i: Integer;
begin
  Assert(not Immutable, 'TDbgEntitiesThreadStackList.Add Immutable');
  Assert((AnEntry.FOwner = nil) or (AnEntry.FOwner = Self), 'TDbgEntitiesThreadStackList.Add Entry.FThreadStackList');
  AnEntry.FOwner := Self;
  i := IndexOfThread(AnEntry.ThreadId, True);
  FList[i].List.Add(AnEntry);
  DoAdded(AnEntry);
end;

procedure TDbgEntitiesThreadStackList.Clear;
var
  i, j: Integer;
begin
  Assert(not Immutable, 'TDbgEntitiesThreadStackList.Clear Immutable');
  if Length(FList) = 0 then
    exit;
  for i := 0 to Length(FList) - 1 do begin
    for j := 0 to FList[i].List.Count - 1 do
      TDbgEntityValuesList(FList[i].List[j]).FOwner := nil;
    FList[i].List.Free;
  end;
  SetLength(FList, 0);
  DoCleared;
end;

function TDbgEntitiesThreadStackList.Count: Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := 0 to Length(FList) - 1 do
    Result := Result + FList[i].List.Count;
end;


{ TDelayedUdateItem }

procedure TDelayedUdateItem.Assign(ASource: TPersistent);
begin
  BeginUpdate;
  try
    inherited Assign(ASource);
  finally
    EndUpdate;
  end;
end;

procedure TDelayedUdateItem.BeginUpdate;
begin
  Inc(FUpdateCount);
  if FUpdateCount = 1 then FDoChanged := False;
end;

procedure TDelayedUdateItem.Changed;
begin
  if FUpdateCount > 0
  then FDoChanged := True
  else DoChanged;
end;

constructor TDelayedUdateItem.Create(ACollection: TCollection);
begin
  inherited Create(ACollection);
  FUpdateCount := 0;
end;

procedure TDelayedUdateItem.DoChanged;
begin
  inherited Changed(False);
end;

procedure TDelayedUdateItem.DoEndUpdate;
begin
  //
end;

procedure TDelayedUdateItem.EndUpdate;
begin
  Dec(FUpdateCount);
  if FUpdateCount < 0 then raise EInvalidOperation.Create('TDelayedUdateItem.EndUpdate');
  if (FUpdateCount = 0)
  then DoEndUpdate;
  if (FUpdateCount = 0) and FDoChanged
  then begin
    DoChanged;
    FDoChanged := False;
  end;
end;

function TDelayedUdateItem.IsUpdating: Boolean;
begin
  Result := FUpdateCount > 0;
end;


{ TRefCountedColectionItem }

constructor TRefCountedColectionItem.Create(ACollection: TCollection);
begin
  FRefCount := 0;
  inherited Create(ACollection);
end;

destructor TRefCountedColectionItem.Destroy;
begin
  Assert(FRefcount = 0, 'Destroying referenced object');
  inherited Destroy;
end;

procedure TRefCountedColectionItem.AddReference;
begin
  Inc(FRefcount);
end;

procedure TRefCountedColectionItem.ReleaseReference;
begin
  Assert(FRefCount > 0, 'TRefCountedObject.ReleaseReference  RefCount > 0');
  Dec(FRefCount);
  if FRefCount = 0 then DoFree;
end;

procedure TRefCountedColectionItem.DoFree;
begin
  Self.Free;
end;

end.