File: debuginstructions.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 (678 lines) | stat: -rw-r--r-- 22,179 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
unit DebugInstructions;
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LazClasses, LazLoggerBase, debugprocess;

type

  { TDBGInstruction }

  TDBGInstructionFlag = (
    ifRequiresThread,
    ifRequiresStackFrame
  );
  TDBGInstructionFlags = set of TDBGInstructionFlag;

  TDBGInstructionState = (
    disNew,
    disQueued,
    disDataSent,
    disContentReceived, //may still wait for closing token
    disComleted,
    disFailed
  );

  TDBGInstructionErrorFlag = (
    ifeContentError,  // the imput from debugger was not in the expected format
    ifeWriteError,    // writing to debugger (pipe) failed
    ifeReadError,
    ifeDbgNotRunning,
    ifeTimedOut,
    ifeRecoveredTimedOut, // not an error
    ifeInvalidStackFrame,
    ifeInvalidThreadId,
    ifeQueueContextError,  // The thread or stack command went ok, but something else interfered with setting th econtext
    ifeCancelled
  );
  TDBGInstructionErrorFlags = set of TDBGInstructionErrorFlag;

  TDBGInstructionQueue = class;

  { TDBGInstruction }

  TDBGInstruction = class(TRefCountedObject)
  private
    FCommand: String;
    FOnFailure: TNotifyEvent;
    FOnFinish: TNotifyEvent;
    FOnSuccess: TNotifyEvent;
    FStackFrame: Integer;
    FThreadId: Integer;
    FFlags: TDBGInstructionFlags;
    FState: TDBGInstructionState;
    FErrorFlags: TDBGInstructionErrorFlags;
    FTimeOut: Integer;
    FQueue: TDBGInstructionQueue;
  protected
    FNextQueuedInstruction, FPrevQueuedInstruction: TDBGInstruction;
  protected
    function GetCommandAsString(): String; virtual;
    procedure SendCommandDataToDbg(); virtual;
    procedure SetStateRunning;
    function  ProcessInputFromDbg(const AData: String): Boolean; virtual; abstract; // True if data was handled

  //  function  GetTimeOutVerifier: TDBGInstruction; virtual;
    procedure Init; virtual;
    procedure SetQueued(AQueue: TDBGInstructionQueue);
    procedure SetContentReceieved;
    procedure InternalCreate(ACommand: String;
                             AThread, AFrame: Integer; // ifRequiresThread, ifRequiresStackFrame will always be included
                             AFlags: TDBGInstructionFlags;
                             ATimeOut: Integer
                            );
    property Queue: TDBGInstructionQueue read FQueue;
    property NextInstruction: TDBGInstruction read FNextQueuedInstruction;
  public
    constructor Create(ACommand: String;
                       AFlags: TDBGInstructionFlags = [];
                       ATimeOut: Integer = 0
                      );
    constructor Create(ACommand: String;
                       AThread: Integer;         // ifRequiresThread will always be included
                       AOtherFlags: TDBGInstructionFlags = [];
                       ATimeOut: Integer = 0
                      );
    constructor Create(ACommand: String;
                       AThread, AFrame: Integer; // ifRequiresThread, ifRequiresStackFrame will always be included
                       AOtherFlags: TDBGInstructionFlags = [];
                       ATimeOut: Integer = 0
                      );
    procedure Cancel;
    function  IsSuccess: Boolean;
    function  IsCompleted: boolean;
    function  IsRunning: boolean;
    procedure MarkAsSuccess; // calls DoInstructionFinished // releases the instruction
    procedure MarkAsFailed;  // calls DoInstructionFinished // releases the instruction

    procedure HandleWriteError; virtual;
    procedure HandleReadError; virtual;
    procedure HandleTimeOut; virtual;
    procedure HandleRecoveredTimeOut; virtual;
    procedure HandleNoDbgRunning; virtual;
    procedure HandleContentError; virtual;
    procedure HandleError(AnError: TDBGInstructionErrorFlag; AMarkAsFailed: Boolean = True); virtual;
    function  DebugText: String;

    property Command: String read GetCommandAsString;
    property ThreadId: Integer read FThreadId;
    property StackFrame: Integer read FStackFrame;
    property Flags: TDBGInstructionFlags read FFlags;
    property State: TDBGInstructionState read FState;
    property ErrorFlags: TDBGInstructionErrorFlags read FErrorFlags;
    property TimeOut: Integer read FTimeOut;

    property OnFinish: TNotifyEvent read FOnFinish write FOnFinish;
    property OnSuccess: TNotifyEvent read FOnSuccess write FOnSuccess;
    property OnFailure: TNotifyEvent read FOnFailure write FOnFailure;
  end;

  { TDBGInstructionQueue }

  TDBGInstructionQueueFlag = (
    iqfValidThread,
    iqfValidStackFrame
  );
  TDBGInstructionQueueFlags = set of TDBGInstructionQueueFlag;

  TLineReceivedNotification = procedure(var ALine: String) of object;


  TDBGInstructionQueue = class
  private
    FDebugProcess: TDebugProcess;
    FFirstQueuedInstruction, FLastQueuedInstruction: TDBGInstruction;
    FCurrentInstruction, FRunningInstruction: TDBGInstruction;
    FOnBeginLinesReceived: TNotifyEvent;
    FOnEndLinesReceived: TNotifyEvent;
    FOnDebuggerTerminated: TNotifyEvent;
    FOnAfterHandleLineReceived: TLineReceivedNotification;
    FOnBeforeHandleLineReceived: TLineReceivedNotification;

    FFlags: TDBGInstructionQueueFlags;
    FLockQueueRun: Integer;
    FCurrentStackFrame: Integer;
    FCurrentThreadId: Integer;

    procedure DoDbgLineReceived(Sender: TObject; ALine: String);
    procedure DoBeginLinesReceived(Sender: TObject);
    procedure DoEndLinesReceived(Sender: TObject);
    procedure DoDbgSendError(Sender: TObject; ALine: String);
    procedure DoDbgTerminated(Sender: TObject);

    procedure RunInstruction(AnInstruction: TDBGInstruction);
  protected
    procedure RunQueue;
    function  GetNextInstructionToRun: TDBGInstruction; virtual;
    function  GetChangeContextInstruction(AnInstruction: TDBGInstruction): TDBGInstruction; virtual;

    function  GetSelectThreadInstruction(AThreadId: Integer): TDBGInstruction; virtual; abstract;
    function  GetSelectFrameInstruction(AFrame: Integer): TDBGInstruction; virtual; abstract;

    procedure DoBeforeHandleLineReceived(var ALine: String); virtual;
    procedure DoAfterHandleLineReceived(var ALine: String); virtual;
    procedure DoDebuggerTerminated; virtual;
    procedure DoInstructionFinished(Sender: TDBGInstruction); virtual;

    procedure SendDataToDBG(ASender: TDBGInstruction; AData: String);
    procedure SendDataToDBG(ASender: TDBGInstruction; AData: String; const AValues: array of const);

    procedure RemoveInstruction(AnInstruction: TDBGInstruction);
    property  FirstInstruction: TDBGInstruction read FFirstQueuedInstruction;
  public
    constructor Create(ADebugProcess: TDebugProcess);
    destructor Destroy; override;
    procedure LockQueueRun; // prevent next instruction from running, until unLockQueueRun
    procedure UnLockQueueRun;

    procedure InvalidateThredAndFrame(AStackFrameOnly: Boolean = False);
    procedure SetKnownThread(AThread: Integer);
    procedure SetKnownThreadAndFrame(AThread, AFrame: Integer);
    procedure QueueInstruction(AnInstruction: TDBGInstruction); // Wait for instruction to be finished, not queuing
    property CurrentThreadId: Integer read FCurrentThreadId;
    property CurrentStackFrame: Integer read FCurrentStackFrame;
    property Debugger: TDebugProcess read FDebugProcess;
    property CurrentInstruction: TDBGInstruction read FCurrentInstruction;
    property RunningInstruction: TDBGInstruction read FRunningInstruction;
  public
    property OnBeforeHandleLineReceived: TLineReceivedNotification
      read FOnBeforeHandleLineReceived write FOnBeforeHandleLineReceived;
    property OnAfterHandleLineReceived: TLineReceivedNotification
      read FOnAfterHandleLineReceived write FOnAfterHandleLineReceived;
    property OnDebuggerTerminated: TNotifyEvent read FOnDebuggerTerminated write FOnDebuggerTerminated;
    property OnBeginLinesReceived: TNotifyEvent read FOnBeginLinesReceived write FOnBeginLinesReceived;
    property OnEndLinesReceived: TNotifyEvent read FOnEndLinesReceived write FOnEndLinesReceived;
  end;

function dbgs(AState: TDBGInstructionState): String; overload;
function dbgs(AFlag: TDBGInstructionQueueFlag): String; overload;
function dbgs(AFlags: TDBGInstructionQueueFlags): String; overload;
function dbgs(AFlag: TDBGInstructionFlag): String; overload;
function dbgs(AFlags: TDBGInstructionFlags): String; overload;
function dbgsInstr(AnInstr: TDBGInstruction): String;

implementation

var
  DBGMI_TIMEOUT_DEBUG, DBG_THREAD_AND_FRAME, DBG_VERBOSE: PLazLoggerLogGroup;

function dbgs(AState: TDBGInstructionState): String;
begin
  writestr(Result{%H-}, AState);
end;

function dbgs(AFlag: TDBGInstructionQueueFlag): String;
begin
  writestr(Result{%H-}, AFlag);
end;

function dbgs(AFlags: TDBGInstructionQueueFlags): String;
var
  i: TDBGInstructionQueueFlag;
begin
  Result := '';
  for i := low(TDBGInstructionQueueFlags) to high(TDBGInstructionQueueFlags) do
    if i in AFlags then
      if Result = '' then
        Result := Result + dbgs(i)
      else
        Result := Result + ', ' +dbgs(i);
  if Result <> '' then
    Result := '[' + Result + ']';
end;

function dbgs(AFlag: TDBGInstructionFlag): String;
begin
  writestr(Result{%H-}, AFlag);
end;

function dbgs(AFlags: TDBGInstructionFlags): String;
var
  i: TDBGInstructionFlag;
begin
  Result := '';
  for i := low(TDBGInstructionFlags) to high(TDBGInstructionFlags) do
    if i in AFlags then
      if Result = '' then
        Result := Result + dbgs(i)
      else
        Result := Result + ', ' +dbgs(i);
  if Result <> '' then
    Result := '[' + Result + ']';
end;

function dbgsInstr(AnInstr: TDBGInstruction): String;
begin
  if AnInstr = nil then
    Result := 'nil'
  else
    Result := AnInstr.DebugText;
end;

{ TDBGInstruction }

function TDBGInstruction.GetCommandAsString(): String;
begin
  Result := FCommand;
end;

procedure TDBGInstruction.SendCommandDataToDbg();
begin
  FQueue.SendDataToDBG(Self, GetCommandAsString);
  FState := disDataSent;
end;

procedure TDBGInstruction.SetStateRunning;
begin
  Assert(FState=disQueued, 'FState=disQueued');
  FState := disDataSent;
end;

procedure TDBGInstruction.Init;
begin
  //
end;

procedure TDBGInstruction.SetQueued(AQueue: TDBGInstructionQueue);
begin
  FState := disQueued;
  FQueue := AQueue;
end;

procedure TDBGInstruction.SetContentReceieved;
begin
  FState := disContentReceived;
debugln('disContentReceived');
end;

procedure TDBGInstruction.InternalCreate(ACommand: String; AThread,
  AFrame: Integer; AFlags: TDBGInstructionFlags; ATimeOut: Integer);
begin
  inherited Create;
  AddReference;
  FState := disNew;
  FCommand := ACommand;
  FThreadId   := AThread;
  FStackFrame := AFrame;
  FFlags := AFlags;
  FTimeOut := ATimeOut;
  Init;
end;

constructor TDBGInstruction.Create(ACommand: String;
  AFlags: TDBGInstructionFlags; ATimeOut: Integer);
begin
  InternalCreate(ACommand, -1, -1, AFlags, ATimeOut);
end;

constructor TDBGInstruction.Create(ACommand: String; AThread: Integer;
  AOtherFlags: TDBGInstructionFlags; ATimeOut: Integer);
begin
  InternalCreate(ACommand, AThread, -1,
                 AOtherFlags + [ifRequiresThread], ATimeOut);
end;

constructor TDBGInstruction.Create(ACommand: String; AThread, AFrame: Integer;
  AOtherFlags: TDBGInstructionFlags; ATimeOut: Integer);
begin
  InternalCreate(ACommand, AThread, AFrame,
                 AOtherFlags + [ifRequiresThread, ifRequiresStackFrame], ATimeOut);
end;

procedure TDBGInstruction.Cancel;
begin
debugln(['TDBGInstruction.Cancel ', Command]);
  if FState = disQueued then
    FQueue.RemoveInstruction(Self)
  else
    HandleError(ifeCancelled);
end;

function TDBGInstruction.IsSuccess: Boolean;
begin
  Result := FState = disComleted;
end;

function TDBGInstruction.IsCompleted: boolean;
begin
  Result := (FState = disComleted) or (FState = disFailed);
end;

function TDBGInstruction.IsRunning: boolean;
begin
  Result := (FState = disDataSent) or (FState = disContentReceived);
end;

procedure TDBGInstruction.MarkAsSuccess;
begin
debugln(['TDBGInstruction.MarkAsSuccess SUCCESS ', Command]);
  FState := disComleted;
  if FOnSuccess <> nil then FOnSuccess(Self);
  if FOnFinish <> nil then FOnFinish(Self);

  If FQueue <> nil then
    FQueue.DoInstructionFinished(Self);
end;

procedure TDBGInstruction.MarkAsFailed;
begin
debugln(['TDBGInstruction.MarkAsFailed FAILED ',Command]);
  FState := disFailed;
  if FOnFailure <> nil then FOnFailure(Self);
  if FOnFinish <> nil then FOnFinish(Self);

  If FQueue <> nil then
    FQueue.DoInstructionFinished(Self);
end;

procedure TDBGInstruction.HandleWriteError;
begin
  HandleError(ifeWriteError, False);
//  if (FTimeOut = 0) or (FTimeOut > TIMEOUT_AFTER_WRITE_ERROR) then
//    FTimeOut := TIMEOUT_AFTER_WRITE_ERROR;
end;

procedure TDBGInstruction.HandleReadError;
begin
  HandleError(ifeReadError);
end;

procedure TDBGInstruction.HandleTimeOut;
begin
  HandleError(ifeTimedOut);
end;

procedure TDBGInstruction.HandleRecoveredTimeOut;
begin
  Include(FErrorFlags, ifeRecoveredTimedOut);
end;

procedure TDBGInstruction.HandleNoDbgRunning;
begin
  HandleError(ifeDbgNotRunning);
end;

procedure TDBGInstruction.HandleContentError;
begin
  HandleError(ifeContentError);
end;

procedure TDBGInstruction.HandleError(AnError: TDBGInstructionErrorFlag;
  AMarkAsFailed: Boolean);
begin
  Include(FErrorFlags,  AnError);
  if AMarkAsFailed then
    MarkAsFailed;
end;

function TDBGInstruction.DebugText: String;
begin
  Result := ClassName + ': "' + FCommand + '", ' + dbgs(FFlags) + ', ' + dbgs(FState)+' # refcnt '+dbgs(RefCount);
  if ifRequiresThread in FFlags then
    Result := Result + ' Thr=' + IntToStr(FThreadId);
  if ifRequiresStackFrame in FFlags then
    Result := Result + ' Frm=' + IntToStr(FStackFrame);
end;

{ TDBGInstructionQueue }

procedure TDBGInstructionQueue.RunQueue;
var
  ContextInstr: TDBGInstruction;
begin
  if FLockQueueRun > 0 then
    exit;
  if FRunningInstruction <> nil then
    exit;
debugln(['TDBGInstructionQueue.RunQueue ', dbgsInstr(FCurrentInstruction), ' / ', dbgsInstr(FRunningInstruction)]);

  if FCurrentInstruction = nil then begin
    FCurrentInstruction := GetNextInstructionToRun;
    if FCurrentInstruction = nil then
      exit;
    FCurrentInstruction.AddReference;
    RemoveInstruction(FCurrentInstruction);
DebugLnEnter(['>> Current Instruction: ', FCurrentInstruction.Command]);
  end;

  // set FCurrentInstruction to a pre running state, while changing stack....

  ContextInstr := GetChangeContextInstruction(FCurrentInstruction);
  if ContextInstr <> nil then begin
    ContextInstr.SetQueued(Self);
    RunInstruction(ContextInstr);
    ContextInstr.ReleaseReference;
    exit; // run will be called again
  end;

  RunInstruction(FCurrentInstruction);
  ReleaseRefAndNil(FCurrentInstruction);
end;

procedure TDBGInstructionQueue.DoDbgLineReceived(Sender: TObject; ALine: String
  );
begin
  // Lock DoInstructionFinished
  DoBeforeHandleLineReceived(ALine);
  if (FRunningInstruction <> nil) and (ALine <> '') then begin
    if FRunningInstruction.ProcessInputFromDbg(ALine) then
      ALine := '';
  end;
  if ALine <> '' then
    DoAfterHandleLineReceived(ALine);

  if (FRunningInstruction = nil) and (ALine <> '') then
    DebugLn(DBG_VERBOSE, ['TDBGInstructionQueue: Got Data, but no command running: ', ALine]);
  // unlock DoInstructionFinished
end;

procedure TDBGInstructionQueue.DoBeginLinesReceived(Sender: TObject);
begin
  if FOnBeginLinesReceived <> nil then
    FOnBeginLinesReceived(Self);
end;

procedure TDBGInstructionQueue.DoEndLinesReceived(Sender: TObject);
begin
  if FOnEndLinesReceived <> nil then
    FOnEndLinesReceived(Self);
end;

procedure TDBGInstructionQueue.DoDbgSendError(Sender: TObject; ALine: String);
begin
  if FRunningInstruction <> nil then
    FRunningInstruction.HandleWriteError;
end;

procedure TDBGInstructionQueue.DoDbgTerminated(Sender: TObject);
begin
  if FRunningInstruction <> nil then
    FRunningInstruction.HandleNoDbgRunning;
  DoDebuggerTerminated;
end;

function TDBGInstructionQueue.GetNextInstructionToRun: TDBGInstruction;
begin
  Result := FFirstQueuedInstruction;
end;

function TDBGInstructionQueue.GetChangeContextInstruction(
  AnInstruction: TDBGInstruction): TDBGInstruction;
begin
  Result := nil;
  if (ifRequiresThread in AnInstruction.Flags) and
  ( (CurrentThreadId <> AnInstruction.ThreadId) or not (iqfValidThread in FFlags) )
  then begin
    Result := GetSelectThreadInstruction(AnInstruction.ThreadId);
    exit;
  end;

  if (ifRequiresStackFrame in AnInstruction.Flags) and
  ( (CurrentStackFrame <> AnInstruction.StackFrame) or not (iqfValidStackFrame in FFlags) )
  then begin
    Result := GetSelectFrameInstruction(AnInstruction.StackFrame);
    exit;
  end;
end;

procedure TDBGInstructionQueue.DoBeforeHandleLineReceived(var ALine: String);
begin
  if FOnBeforeHandleLineReceived <> nil then
    FOnBeforeHandleLineReceived(ALine);
end;

procedure TDBGInstructionQueue.DoAfterHandleLineReceived(var ALine: String);
begin
  if FOnAfterHandleLineReceived <> nil then
    FOnAfterHandleLineReceived(ALine);
end;

procedure TDBGInstructionQueue.DoDebuggerTerminated;
begin
  if FOnDebuggerTerminated <> nil then
    FOnDebuggerTerminated(self);
end;

procedure TDBGInstructionQueue.DoInstructionFinished(Sender: TDBGInstruction);
begin
DebugLnExit(['<< Finished Instruction: ', FRunningInstruction.Command, ' // ', Sender=FRunningInstruction]);
if nil = FCurrentInstruction then DebugLnExit(['<< Current Instruction: ']);
  ReleaseRefAndNil(FRunningInstruction);
  RunQueue;
end;

procedure TDBGInstructionQueue.RunInstruction(AnInstruction: TDBGInstruction);
begin
DebugLnEnter(['>> Running Instruction: ', AnInstruction.Command]);
  FRunningInstruction := AnInstruction;
  FRunningInstruction.AddReference;
  FRunningInstruction.SendCommandDataToDBG;
end;

procedure TDBGInstructionQueue.RemoveInstruction(AnInstruction: TDBGInstruction
  );
begin
  If AnInstruction.FPrevQueuedInstruction <> nil then
    AnInstruction.FPrevQueuedInstruction.FNextQueuedInstruction := AnInstruction.FNextQueuedInstruction
  else begin
  assert(FFirstQueuedInstruction = AnInstruction, 'not on queue');
    FFirstQueuedInstruction := AnInstruction.FNextQueuedInstruction;
  end;

  If AnInstruction.FNextQueuedInstruction <> nil then
    AnInstruction.FNextQueuedInstruction.FPrevQueuedInstruction := AnInstruction.FPrevQueuedInstruction
  else begin
  assert(FLastQueuedInstruction = AnInstruction, 'not on queue');
    FLastQueuedInstruction := AnInstruction.FPrevQueuedInstruction;
  end;

  AnInstruction.FPrevQueuedInstruction := nil;
  AnInstruction.FNextQueuedInstruction := nil;
  AnInstruction.ReleaseReference;
end;

procedure TDBGInstructionQueue.SendDataToDBG(ASender: TDBGInstruction;
  AData: String);
begin
  FDebugProcess.SendCmdLn(AData);
end;

procedure TDBGInstructionQueue.SendDataToDBG(ASender: TDBGInstruction;
  AData: String; const AValues: array of const);
begin
  SendDataToDBG(ASender, Format(AData, AValues));
end;

constructor TDBGInstructionQueue.Create(ADebugProcess: TDebugProcess);
begin
  FDebugProcess := ADebugProcess;
  FDebugProcess.OnLineReceived := @DoDbgLineReceived;
  FDebugProcess.OnBeginLinesReceived := @DoBeginLinesReceived;
  FDebugProcess.OnEndLinesReceived := @DoEndLinesReceived;
  FDebugProcess.OnSendError := @DoDbgSendError;
  FDebugProcess.OnTerminate := @DoDbgTerminated;
end;

destructor TDBGInstructionQueue.Destroy;
begin
  while FFirstQueuedInstruction <> nil do
    RemoveInstruction(FFirstQueuedInstruction);
  if FRunningInstruction <> nil then
    DoInstructionFinished(FRunningInstruction); // TODO: maybe cancel?
  inherited Destroy;
end;

procedure TDBGInstructionQueue.LockQueueRun;
begin
  inc(FLockQueueRun);
end;

procedure TDBGInstructionQueue.UnLockQueueRun;
begin
  dec(FLockQueueRun);
  if FLockQueueRun = 0 then
    RunQueue;
end;

procedure TDBGInstructionQueue.InvalidateThredAndFrame(AStackFrameOnly: Boolean);
begin
  if AStackFrameOnly then begin
    DebugLn(DBG_THREAD_AND_FRAME, ['TGDB_IQ: Invalidating queue''s stack only. Was: ', dbgs(FFlags), ' Thr=', FCurrentThreadId, ' Frm=', FCurrentStackFrame]);
    FFlags := FFlags - [iqfValidStackFrame];
  end
  else begin
    DebugLn(DBG_THREAD_AND_FRAME, ['TGDB_IQ: Invalidating queue''s current thread and stack. Was: ', dbgs(FFlags), ' Thr=', FCurrentThreadId, ' Frm=', FCurrentStackFrame]);
    FFlags := FFlags - [iqfValidThread, iqfValidStackFrame];
  end;
end;

procedure TDBGInstructionQueue.SetKnownThread(AThread: Integer);
begin
  DebugLn(DBG_THREAD_AND_FRAME, ['TGDB_IQ: Setting queue''s current thread and stack. New: Thr=', AThread, ' Was: ', dbgs(FFlags), ' Thr=', FCurrentThreadId, ' Frm=', FCurrentStackFrame]);
  FCurrentThreadId := AThread;
  FFlags := FFlags + [iqfValidThread] - [iqfValidStackFrame];
end;

procedure TDBGInstructionQueue.SetKnownThreadAndFrame(AThread, AFrame: Integer);
begin
  DebugLn(DBG_THREAD_AND_FRAME, ['TGDB_IQ: Setting queue''s current thread and stack. New: Thr=', AThread, ' Frm=', AFrame,' Was: ', dbgs(FFlags), ' Thr=', FCurrentThreadId, ' Frm=', FCurrentStackFrame]);
  FCurrentThreadId := AThread;
  FCurrentStackFrame := AFrame;
  FFlags := FFlags + [iqfValidThread, iqfValidStackFrame];
end;

procedure TDBGInstructionQueue.QueueInstruction(AnInstruction: TDBGInstruction);
begin
debugln(['TDBGInstructionQueue.QueueInstruction ', AnInstruction.DebugText]);
  Assert(AnInstruction.State = disNew, 'queue only new instr');
  AnInstruction.AddReference;
  AnInstruction.FNextQueuedInstruction := nil;
  AnInstruction.FPrevQueuedInstruction := FLastQueuedInstruction;
  if FLastQueuedInstruction <> nil then
    FLastQueuedInstruction.FNextQueuedInstruction := AnInstruction
  else
    FFirstQueuedInstruction := AnInstruction;
  FLastQueuedInstruction := AnInstruction;
  AnInstruction.SetQueued(Self);
  RunQueue;
end;

initialization
  DBGMI_TIMEOUT_DEBUG := DebugLogger.FindOrRegisterLogGroup('DBGMI_TIMEOUT_DEBUG' {$IFDEF DBGMI_TIMEOUT_DEBUG} , True {$ENDIF} );
  DBG_THREAD_AND_FRAME := DebugLogger.FindOrRegisterLogGroup('DBG_THREAD_AND_FRAME' {$IFDEF DBG_THREAD_AND_FRAME} , True {$ENDIF} );
  DBG_VERBOSE := DebugLogger.FindOrRegisterLogGroup('DBG_VERBOSE' {$IFDEF DBG_VERBOSE} , True {$ENDIF} );

end.