File: teststepping.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (1219 lines) | stat: -rw-r--r-- 48,911 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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
unit TestStepping;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, math, TestDbgControl, TestDbgTestSuites, TestBase,
  TTestWatchUtilities, TestCommonSources, TestDbgConfig, TestOutputLogger,
  FpDebugDebugger, FpDebugDebuggerUtils, LazDebuggerIntf,
  LazDebuggerIntfBaseTypes, DbgIntfDebuggerBase, DbgIntfBaseTypes,
  {$ifdef FORCE_LAZLOGGER_DUMMY} LazLoggerDummy {$else} LazLoggerBase {$endif}, Forms;

type

  { TTestStepping }

  TTestStepping = class(TDBGTestCase)
  protected
    Src: TCommonSource;
    Dbg: TDebuggerIntf;

    FGotExceptCount: Integer;
    FGotExceptClass: String;
    FGotExceptMsg: String;
    FGotExceptType: TDBGExceptionType;
    FGotExceptionLocation: TDBGLocationRec;
    FContinue: Boolean;

    function TestLocation(ATestName, ABrkName: String; ABreakHitCount: Integer = 1; AnAcceptLinesBefore: integer = 0): Boolean; // only line-number
    procedure TestLoopCount(ATestName: String);
    function IsAtLocation(ABrkName: String; ATrueOnNoLine: Boolean = False): Boolean; // only line-number
    procedure DoDebuggerException(Sender: TObject;
                                  const AExceptionType: TDBGExceptionType;
                                  const AExceptionClass: String;
                                  const AExceptionLocation: TDBGLocationRec;
                                  const AExceptionText: String;
                                  out AContinue: Boolean);
    function StepOverToLine(ATestName, ABrkName: String; AnExitIfNoLineInfo: Boolean = False;
      ACmd: TDBGCommand = dcStepOver; AMaxSteps: Integer = 100; AForbiddenLine: String = ''): Boolean;
    function StepIfAtLine(ATestName, ABrkName: String; ACmd: TDBGCommand = dcStepOver; AnExpAtBrkName: String = ''): Boolean;
    procedure DoTestStepOver(ANextOnlyStopOnStartLine: Boolean);
    procedure DoTestStepOverInstr(ANextOnlyStopOnStartLine: Boolean);
    procedure DoTestExceptionStepOutEx(ANextOnlyStopOnStartLine: Boolean);
    procedure DoTestExceptionStepOverEx(ANextOnlyStopOnStartLine: Boolean);
    procedure DoTestStepTryBlocks(ANextOnlyStopOnStartLine: Boolean);
  public
    function RunToNextPauseNoLoopBreak(AName: String; ACmd: TDBGCommand; ATimeOut: Integer;
      AnExpAtBrkName: String = ''; AnExpAcceptLinesBefore: integer = 0): Boolean;
    function RunToNextPauseNoLoopBreak(AName: String; ACmd: TDBGCommand;
      AnExpAtBrkName: String = ''; AnExpAcceptLinesBefore: integer = 0): Boolean;
    function RunToNextPauseEx(AName: String; ACmd: TDBGCommand;
      AnExpAtBrkName: String = ''; AnExpAcceptLinesBefore: integer = 0): Boolean;
  published
    (* Step over to work with various events happening during the step
       - creation/exit of threads
       - ignored breakpoints (same thread / other thread)
       - TODO: ignored exception: caught inside the step
       - TODO: ignored exception: caught caught outside the step (step to except/finally)
    *)
    procedure TestStepOver;
    procedure TestStepOverInstr;
    procedure TestExceptionStepOutEx;
    procedure TestExceptionStepOverEx;
    procedure TestStepTryBlocks;

    procedure TestStepOver_NextOnlyFalse;
    procedure TestStepOverInstr_NextOnlyFalse;
    procedure TestExceptionStepOutEx_NextOnlyFalse;
    procedure TestExceptionStepOverEx_NextOnlyFalse;
    procedure TestStepTryBlocks_NextOnlyFalse;
  end;

implementation

var
  ControlTest,
  ControlTest_NextOnlyTrue, ControlTestStepOver, ControlTestStepOverInstr,
  ControlTestExceptionStepOutEx, ControlTestExceptionStepOverEx, ControlTestStepTryBlocks: Pointer;

  ControlTest_NextOnly, ControlTestStepOver_NextOnly, ControlTestStepOverInstr_NextOnly,
  ControlTestExceptionStepOutEx_NextOnly, ControlTestExceptionStepOverEx_NextOnly, ControlTestStepTryBlocks_NextOnly: Pointer;

function TTestStepping.TestLocation(ATestName, ABrkName: String;
  ABreakHitCount: Integer; AnAcceptLinesBefore: integer): Boolean;
var
  lc: TDBGLocationRec;
  brk: LongInt;
begin
  Result := True;
  AssertDebuggerState(dsPause);
  lc := Debugger.LazDebugger.GetLocation;
  brk := Src.BreakPoints[ABrkName];
  if (AnAcceptLinesBefore > 0) and (lc.SrcLine < brk) and (lc.SrcLine >= brk - AnAcceptLinesBefore)
  then
    Result := TestEquals(ATestName+' '+ABrkName+' Loc', brk, lc.SrcLine, 0, 'Ignored - In AcceptLinesBefore range')
  else
    Result := TestEquals(ATestName+' '+ABrkName+' Loc', brk, lc.SrcLine);
  if ABreakHitCount >= 0 then
    if not TestEquals(ATestName+' '+ABrkName+' HitCnt', Debugger.BreakPointByName(ABrkName).HitCount, ABreakHitCount)
    then
      Result := False;
end;

procedure TTestStepping.TestLoopCount(ATestName: String);
begin
  TestEquals(ATestName+' - No unexpected breaks of debugloop', 1, THookedFpDebugDebugger(dbg).LockRelCount);
  THookedFpDebugDebugger(dbg).LockRelCount := 0;
end;

function TTestStepping.IsAtLocation(ABrkName: String; ATrueOnNoLine: Boolean
  ): Boolean;
var
  lc: TDBGLocationRec;
begin
  lc := Debugger.LazDebugger.GetLocation;
  if (lc.SrcFile = '') then
    exit(ATrueOnNoLine);
  Result := Src.BreakPoints[ABrkName] = lc.SrcLine;
end;

procedure TTestStepping.DoDebuggerException(Sender: TObject;
  const AExceptionType: TDBGExceptionType; const AExceptionClass: String;
  const AExceptionLocation: TDBGLocationRec; const AExceptionText: String; out
  AContinue: Boolean);
begin
  inc(FGotExceptCount);
  FGotExceptClass := AExceptionClass;
  FGotExceptMsg   := AExceptionText;
  FGotExceptType  := AExceptionType;
  FGotExceptionLocation := AExceptionLocation;
  AContinue := FContinue;
end;

function TTestStepping.StepOverToLine(ATestName, ABrkName: String;
  AnExitIfNoLineInfo: Boolean; ACmd: TDBGCommand; AMaxSteps: Integer;
  AForbiddenLine: String): Boolean;
var
  i: Integer;
begin
  Result := False;
  ATestName := ATestName + dbgs(ACmd) + 'TO: ' + ABrkName + ' (StepOverToLine) ';
  i := 1;
  while (i <= AMaxSteps) and (not IsAtLocation(ABrkName, AnExitIfNoLineInfo)) do begin
    RunToNextPauseNoLoopBreak(ATestName+' '+IntToStr(i), ACmd);
    AssertDebuggerState(dsPause, ATestName);
    if (AForbiddenLine <> '') and IsAtLocation(AForbiddenLine, False) then
      TestFalse(ATestName+' FORBIDDEN LINE '+AForbiddenLine, True);
    inc(i);
  end;
  Result := IsAtLocation(ABrkName, AnExitIfNoLineInfo);
  if not AnExitIfNoLineInfo then
    TestLocation(ATestName, ABrkName, -1);
  debugln(['XXXXXXXXXXXXXXXXXXXXXXXX to ', ABrkName, '  ',ATestName]);
end;

function TTestStepping.StepIfAtLine(ATestName, ABrkName: String;
  ACmd: TDBGCommand; AnExpAtBrkName: String): Boolean;
begin
  Result := True;
  ATestName := ATestName + dbgs(ACmd) + ' (StepIfAtLine) ';
  if not IsAtLocation(ABrkName) then
    exit;
  RunToNextPauseNoLoopBreak(ATestName, ACmd);
  TestTrue(ATestName+' finally entered at begin (not end) / '+ ABrkName, False, 0, 'ignore');
  AssertDebuggerState(dsPause, ATestName);
  debugln(['XXXXXXXXXXXXXXXXXXXXXXXX STEPPED from END LINE to begin??? ', ABrkName, '  ',ATestName]);

  if AnExpAtBrkName <> '' then
    TestLocation(ATestName+': Finished at Line ', AnExpAtBrkName, -1);
end;

procedure TTestStepping.DoTestStepOver(ANextOnlyStopOnStartLine: Boolean);
var
  ExeName: String;
  MainBrk, BrkDis, BrkHitCnt: TDBGBreakPoint;
  ThreadIdMain: Integer;
begin
  Src := GetCommonSourceFor(AppDir + 'StepOverPrg.pas');
  TestCompile(Src, ExeName);

  TestTrue('Start debugger', Debugger.StartDebugger(AppDir, ExeName));
  dbg := Debugger.LazDebugger;
  TFpDebugDebuggerProperties(dbg.GetProperties).NextOnlyStopOnStartLine := ANextOnlyStopOnStartLine;

  try
    MainBrk := Debugger.SetBreakPoint(Src, 'BrkStart');
    Debugger.SetBreakPoint(Src, 'BrkThreadCreateInStep');
    Debugger.SetBreakPoint(Src, 'BrkInterfereByThread');
    Debugger.SetBreakPoint(Src, 'BrkNested');
    Debugger.SetBreakPoint(Src, 'CallStepOverEnd');

    BrkDis    := Debugger.SetBreakPoint(Src, 'BrkDisabled');
    BrkHitCnt := Debugger.SetBreakPoint(Src, 'BrkHitCnt');
    BrkDis.Enabled := False;
    BrkHitCnt.BreakHitCount := 999;
    AssertDebuggerNotInErrorState;

    RunToNextPauseNoInternal('', dcRun);
    AssertDebuggerState(dsPause);
    TestLocation('Init', 'BrkStart');
    ThreadIdMain := dbg.Threads.CurrentThreads.CurrentThreadId;

    // Step over a line
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStep', 'AfterStep', -1);

    // Step over a longer line
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepLongLine', 'AfterStepLongLine', -1);

    // Step over a subroutine call
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepProc', 'AfterStepProc', -1);

    // Step over a several subroutine calls
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepProcLong', 'AfterStepProcLong', -1);

    // Step over a subroutine call, with sleep
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepSleepProc', 'AfterStepSleepProc', -1);

    // Step over a call to sleep
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepSleep', 'AfterStepSleep', -1);

    // Step over a subroutine call, with a disabled breakpoint
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepBrkDis', 'AfterStepBrkDis', -1);

    // Step over a subroutine call, with a breakpoint that continues
    // No internal pause should happen
    RunToNextPauseTestInternal('', 0, dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepBrkHitCnt', 'AfterStepBrkHitCnt', -1);

    TestEquals('No Hit for disabled break', 0, BrkDis.HitCount);
    TestEquals('No Hit for skipped break',  1, BrkHitCnt.HitCount);

    BrkDis.Enabled := True;
    // Step over a subroutine call, BUT STOP at the breakpoint within it
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At BrkDisabled', 'BrkDisabled', -1);
    // And do another step
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);

    TestEquals('No Hit for disabled break', 1, BrkDis.HitCount);
    TestEquals('No Hit for skipped break',  1, BrkHitCnt.HitCount);


    // Step over a RECURSIVE subroutine call
    RunToNextPauseNoLoopBreak('', dcRun);
    AssertDebuggerState(dsPause);
    TestLocation('At BrkNested', 'BrkNested', -1);

    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterNested', 'AfterNested', -1);


    (* The debugger will encounter a thread create event, during the stepping
       This will mean the main-loop's FCurrentThread is the new thread
    *)
    RunToNextPauseNoLoopBreak('', dcRun, 45000);
    AssertDebuggerState(dsPause);
    TestLocation('At BrkThreadCreateInStep', 'BrkThreadCreateInStep', -1);

    // This test can take longer, as the new thread gets very little scheduler time
    // during the single stepping of the main thread.
    RunToNextPauseNoLoopBreak('', dcStepOver, 55000);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterThreadCreateInStep', 'AfterThreadCreateInStep', -1);
    TestEquals('ThreadId AfterThreadCreateInStep', ThreadIdMain, dbg.Threads.CurrentThreads.CurrentThreadId);

    (* The debugger will step over a call.
       Other threads will hit the FHiddenBreakpoint
    *)
    RunToNextPauseNoLoopBreak('', dcRun);
    AssertDebuggerState(dsPause);
    TestLocation('At BrkInterfereByThread', 'BrkInterfereByThread', -1);

    RunToNextPauseNoLoopBreak('', dcStepOver);
    RunToNextPauseNoLoopBreak('', dcStepOver);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterInterfereByThread', 'AfterInterfereByThread', -1);
    TestEquals('ThreadId AfterInterfereByThread', ThreadIdMain, dbg.Threads.CurrentThreads.CurrentThreadId);




    (* go to CallMyNested
       Step into, and step to endline
       => ensure "end" takes ONE step to leave
    *)
    RunToNextPauseNoLoopBreak('', dcRun);
    TestLocation('At CallStepOverEnd', 'CallStepOverEnd', -1);
    RunToNextPauseNoLoopBreak('', dcStepInto);
    RunToNextPauseNoLoopBreak('', dcStepOver);
    if not IsAtLocation('StepOverEnd') then  // depends on "begin" was code or not
      RunToNextPauseNoLoopBreak('', dcStepOver);

    RunToNextPauseNoLoopBreak('', dcStepInto);
    RunToNextPauseNoLoopBreak('', dcStepOver);
    if not IsAtLocation('StepOverEnd') then  // depends on "begin" was code or not
      RunToNextPauseNoLoopBreak('', dcStepOver);

    TestLocation('At StepOverEnd', 'StepOverEnd', -1);
    RunToNextPauseNoLoopBreak('', dcStepOver);
    RunToNextPauseNoLoopBreak('', dcStepOver);
    TestLocation('At AfterCallStepOverEnd', 'AfterCallStepOverEnd', -1);


    dbg.Stop;
  finally
    Debugger.FreeDebugger;

    AssertTestErrors;
  end;
end;

procedure TTestStepping.TestStepOver;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestStepOver) then exit;
  DoTestStepOver(True);
end;

procedure TTestStepping.TestStepOverInstr;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestStepOverInstr) then exit;
  DoTestStepOverInstr(True);
end;

procedure TTestStepping.TestExceptionStepOutEx;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestExceptionStepOutEx) then exit;
  DoTestExceptionStepOutEx(True);
end;

procedure TTestStepping.TestExceptionStepOverEx;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestExceptionStepOverEx) then exit;
  DoTestExceptionStepOverEx(True);
end;

procedure TTestStepping.TestStepTryBlocks;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestStepTryBlocks) then exit;
  DoTestStepTryBlocks(True);
end;

procedure TTestStepping.TestStepOver_NextOnlyFalse;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestStepOver_NextOnly) then exit;
  DoTestStepOver(False);
end;

procedure TTestStepping.TestStepOverInstr_NextOnlyFalse;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestStepOverInstr_NextOnly) then exit;
  DoTestStepOverInstr(False);
end;

procedure TTestStepping.TestExceptionStepOutEx_NextOnlyFalse;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestExceptionStepOutEx_NextOnly) then exit;
  DoTestExceptionStepOutEx(False);
end;

procedure TTestStepping.TestExceptionStepOverEx_NextOnlyFalse;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestExceptionStepOverEx_NextOnly) then exit;
  DoTestExceptionStepOverEx(False);
end;

procedure TTestStepping.TestStepTryBlocks_NextOnlyFalse;
begin
  if SkipTest then exit;
  if not TestControlCanTest(ControlTestStepTryBlocks_NextOnly) then exit;
  DoTestStepTryBlocks(False);
end;

procedure TTestStepping.DoTestStepOverInstr(ANextOnlyStopOnStartLine: Boolean);
  procedure StepInstrToNextLine(AName: String; AnExpIntPauseCnt: integer = 0);
  var
    lc: TDBGLocationRec;
  begin
    lc := Debugger.LazDebugger.GetLocation;
    repeat
      RunToNextPauseTestInternal('', AnExpIntPauseCnt, dcStepOverInstr);
      AssertDebuggerState(dsPause, 'step instr '+AName);
    until (lc.SrcLine <> Debugger.LazDebugger.GetLocation.SrcLine);
  end;

var
  ExeName: String;
  MainBrk, BrkDis, BrkHitCnt: TDBGBreakPoint;
  ThreadIdMain: Integer;
begin
  Src := GetCommonSourceFor(AppDir + 'StepOverPrg.pas');
  TestCompile(Src, ExeName);

  TestTrue('Start debugger', Debugger.StartDebugger(AppDir, ExeName));
  dbg := Debugger.LazDebugger;
  TFpDebugDebuggerProperties(dbg.GetProperties).NextOnlyStopOnStartLine := ANextOnlyStopOnStartLine;

  try
    MainBrk := Debugger.SetBreakPoint(Src, 'BrkStart');
    Debugger.SetBreakPoint(Src, 'BrkThreadCreateInStep');
    Debugger.SetBreakPoint(Src, 'BrkInterfereByThread');
    Debugger.SetBreakPoint(Src, 'BrkNested');

    BrkDis    := Debugger.SetBreakPoint(Src, 'BrkDisabled');
    BrkHitCnt := Debugger.SetBreakPoint(Src, 'BrkHitCnt');
    BrkDis.Enabled := False;
    BrkHitCnt.BreakHitCount := 999;
    AssertDebuggerNotInErrorState;

    RunToNextPauseNoInternal('', dcRun);
    AssertDebuggerState(dsPause);
    TestLocation('Init', 'BrkStart');
    ThreadIdMain := dbg.Threads.CurrentThreads.CurrentThreadId;

    StepInstrToNextLine('Go to AfterStep');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStep', 'AfterStep', -1);

    StepInstrToNextLine('Go to AfterStepLongLine');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepLongLine', 'AfterStepLongLine', -1);

    StepInstrToNextLine('Go to AfterStepProc');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepProc', 'AfterStepProc', -1);

    StepInstrToNextLine('Go to AfterStepProcLong');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepProcLong', 'AfterStepProcLong', -1);

    StepInstrToNextLine('Go to AfterStepSleepProc');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepSleepProc', 'AfterStepSleepProc', -1);

    StepInstrToNextLine('Go to AfterStepSleep');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepSleep', 'AfterStepSleep', -1);

    StepInstrToNextLine('Go to AfterStepBrkDis');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepBrkDis', 'AfterStepBrkDis', -1);

    StepInstrToNextLine('Go to AfterStepBrkHitCnt', 0);
    AssertDebuggerState(dsPause);
    TestLocation('At AfterStepBrkHitCnt', 'AfterStepBrkHitCnt', -1);

    TestEquals('No Hit for disabled break', 0, BrkDis.HitCount);
    TestEquals('No Hit for skipped break',  1, BrkHitCnt.HitCount);


    RunToNextPauseNoLoopBreak('', dcRun);
    AssertDebuggerState(dsPause);
    TestLocation('At BrkNested', 'BrkNested', -1);

    StepInstrToNextLine('Go to AfterNested 1');
    AssertDebuggerState(dsPause);
    StepInstrToNextLine('Go to AfterNested 2');
    AssertDebuggerState(dsPause);
    TestLocation('At AfterNested', 'AfterNested', -1);



    dbg.Stop;
  finally
    Debugger.FreeDebugger;

    AssertTestErrors;
  end;
end;

procedure TTestStepping.DoTestExceptionStepOutEx(
  ANextOnlyStopOnStartLine: Boolean);
var
  ExeName, TstName: String;
begin
  ClearTestErrors;
  FGotExceptCount := 0;

  Src := GetCommonSourceFor(AppDir + 'ExceptTestPrg.pas');
  TestCompile(Src, ExeName);

  TestTrue('Start debugger', Debugger.StartDebugger(AppDir, ExeName));
  dbg := Debugger.LazDebugger;
  TFpDebugDebuggerProperties(dbg.GetProperties).NextOnlyStopOnStartLine := ANextOnlyStopOnStartLine;
  try
    dbg.Exceptions.Add('MyExceptionIgnore').Enabled := False;
    dbg.OnException      := @DoDebuggerException;

    TstName := ' Run to Except';
    FContinue := False;
    RunToNextPauseNoInternal(TestName, dcRun);
    TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
//    TestLocation(TstName+': CurLine ', 'BrkMyRaise', -1);
    FContinue := True;

    THookedFpDebugDebugger(dbg).LockRelCount := 0;
    TstName := ' Step';
    RunToNextPauseNoLoopBreak(TestName, dcStepOver);
    TestLocation(TstName+': CurLine ', 'BrkStep3Fin_A', -1);
    //TestLoopCount(TstName+': BrkStep3Fin_A');

    RunToNextPauseNoLoopBreak(TestName, dcStepOut);
    TestLocation(TstName+': CurLine ', 'BrkStep3FinOuter_A', -1);
    //TestLoopCount(TstName+': BrkStep3FinOuter_A');

    RunToNextPauseNoLoopBreak(TestName, dcStepOut);
    TestLocation(TstName+': CurLine ', 'BrkStepMainExcept1', -1, 1);
    //TestLoopCount(TstName+': BrkStepMainExcept1');


    dbg.Stop;
  finally
    Debugger.FreeDebugger;
  end;

  AssertTestErrors;
end;

procedure TTestStepping.DoTestExceptionStepOverEx(
  ANextOnlyStopOnStartLine: Boolean);
  procedure ExpectEnterFinally(AName: String; ATestAppRecStep: Integer;
    ATestIgnoreRaise, ATestRaiseSkipped, ATestStepOverNested: Boolean;
    ATestIgnoreRaise_2, ATestRaiseSkipped_2, ATestStepOverNested_2: Boolean);
  var
    TstName: String;
    MyRaiseBrk: TDBGBreakPoint;
  begin
    TstName := AName + ' Run to raise';
    FGotExceptCount := 0;
    FContinue := ATestIgnoreRaise or ATestStepOverNested;

    if ATestStepOverNested then begin
      // step to call line, starting on:  Nop => TestVal => If ... then => call
      RunToNextPauseNoInternal(TstName, dcStepOver);
      RunToNextPauseNoInternal(TstName, dcStepOver);
      RunToNextPauseNoInternal(TstName, dcStepOver);
// step is done by caller
//      RunToNextPauseNoInternal(TstName, dcStepOver);  // Step over recurse
      //TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
      exit;
    end
    else
    if ATestIgnoreRaise then begin
      MyRaiseBrk := Debugger.SetBreakPoint(Src, 'BrkMyRaise');
      RunToNextPauseNoInternal(TstName, dcRun);
      MyRaiseBrk.ReleaseReference;
      RunToNextPauseNoInternal(TstName, dcStepOver);  // exception will be ignored => step to finally
      TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
    end
    else begin
      RunToNextPauseNoInternal(TstName, dcRun);
      TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
// TODO: currently reports in except.inc
//      TestLocation(TstName+': CurLine ', 'BrkMyRaise', -1);
      RunToNextPauseNoInternal(TstName, dcStepOver);
    end;

    TstName := AName + ' Run to Finally A';
    TestLocation(TstName+': CurLine ', 'BrkStep3Fin_A', -1);

    if (ATestAppRecStep = 1) and (not ATestRaiseSkipped) then
      ExpectEnterFinally(TstName+' INNER ', 0, ATestIgnoreRaise_2, ATestRaiseSkipped_2, ATestStepOverNested_2, False, False, False)
    else
      StepOverToLine(TstName, 'BrkStep3Fin_A_END', True);

    TstName := AName + ' Run to Finally B';
    RunToNextPauseNoInternal(TstName, dcStepOver);  // Step to next finally
    TestLocation(TstName+': CurLine ', 'BrkStep3Fin_B', -1);

    //if (ATestAppRecStep = 2) and (not ATestRaiseSkipped) then
    //  ExpectEnterFinally(TstName+' INNER ', 0, ATestIgnoreRaise_2, ATestRaiseSkipped_2, ATestStepOverNested_2, False, False, False)
    //else
      StepOverToLine(TstName, 'BrkStep3Fin_B_END', True);

    TstName := AName + ' Run to Finally C';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to next finally
    TestLocation(TstName+': CurLine ', 'BrkStep3Fin_C', -1);

    if (ATestAppRecStep = 2) and (not ATestRaiseSkipped) then
      ExpectEnterFinally(TstName+' INNER ', 0, ATestIgnoreRaise_2, ATestRaiseSkipped_2, ATestStepOverNested_2, False, False, False)
    else
      StepOverToLine(TstName, 'BrkStep3Fin_C_END', True);

    TstName := AName + ' Run to Finally A(outer)';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to next finally
    //StepIfAtLine(TstName, 'BrkStep3Fin_IMPLICIT'); // 32 bit
    //StepIfAtLine(TstName, 'BrkStep3Fin_IMPLICIT_1');
    TestLocation(TstName+': CurLine ', 'BrkStep3FinOuter_A', -1);

    if (ATestAppRecStep = 3) and (not ATestRaiseSkipped) then
      ExpectEnterFinally(TstName+' INNER ', 0, ATestIgnoreRaise_2, ATestRaiseSkipped_2, ATestStepOverNested_2, False, False, False)
    else
      StepOverToLine(TstName, 'BrkStep3FinOuter_A_END', True);

    TstName := AName + ' Run to Finally B(outer)';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to next finally
    TestLocation(TstName+': CurLine ', 'BrkStep3FinOuter_B', -1);

    //if (ATestAppRecStep = 5) and (not ATestRaiseSkipped) then
    //  ExpectEnterFinally(TstName+' INNER ', 0, ATestIgnoreRaise_2, ATestRaiseSkipped_2, ATestStepOverNested_2, False, False, False)
    //else
      StepOverToLine(TstName, 'BrkStep3FinOuter_B_END', True);

    TstName := AName + ' Run to Finally C(outer)';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to next finally
    TestLocation(TstName+': CurLine ', 'BrkStep3FinOuter_C', -1);

    if (ATestAppRecStep = 4) and (not ATestRaiseSkipped) then
      ExpectEnterFinally(TstName+' INNER ', 0, ATestIgnoreRaise_2, ATestRaiseSkipped_2, ATestStepOverNested_2, False, False, False)
    else
      StepOverToLine(TstName,'BrkStep3FinOuter_C_END', True);
  end;

  procedure ExpectNestedExcept(AName: String);
  var
    TstName: String;
  begin
    TstName := AName + ' Run to raise';
    FGotExceptCount := 0;
    FContinue := False;
    RunToNextPauseNoLoopBreak(TstName, dcRun);
    TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
//    TestLocation(TstName+': CurLine ', 'BrkMyRaise', -1);

    TstName := AName + ' Run to except fin';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to fin
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept_Finally', -1);

    // NESTED
    TstName := AName + ' Run to raise nested';
    FGotExceptCount := 0;
    FContinue := False;
    RunToNextPauseNoLoopBreak(TstName, dcRun);
    TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
//    TestLocation(TstName+': CurLine ', 'BrkMyRaise', -1);

    TstName := AName + ' Run to except fin nested';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to fin
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept_Finally', -1);

    StepOverToLine(TstName,'BrkStepNestedExcept_Finally_END', True);

    TstName := AName + ' Run to except nested';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to next finally
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept', -1, 1);

    StepOverToLine(TstName,'BrkStepNestedExcept_END', True);
    // END NESTED

    TstName := AName + ' Run back except fin';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step back to end
    StepIfAtLine(TstName, 'BrkStepNestedExcept_TRY'); // may step to "try"
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step back to finaly
    //if not ANextOnlyStopOnStartLine then
    //  StepIfAtLine(TstName, 'BrkStepNestedExcept_Finally_BEFORE'); // TODO: XXXXX StepOver may stop at the step out line.
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept_Finally_AFTER', -1);

    StepOverToLine(TstName,'BrkStepNestedExcept_Finally_END', True);

    TstName := AName + ' Run to except';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to next finally
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept', -1, 1);

    RunToNextPauseNoLoopBreak(TstName, dcStepOut);  // Step out
  end;

  procedure ExpectNestedExcept_Ignore(AName: String);
  var
    TstName: String;
  begin
    TstName := AName + ' Run to raise';
    FGotExceptCount := 0;
    FContinue := False;
    RunToNextPauseNoLoopBreak(TstName, dcRun);
    TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
//    TestLocation(TstName+': CurLine ', 'BrkMyRaise', -1);

    TstName := AName + ' Run to except fin';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to fin
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept_Finally', -1);

    // NESTED
    TstName := AName + ' Step over raise nested';
    FGotExceptCount := 0;
    FContinue := True;
    StepOverToLine(TstName,'BrkStepNestedExcept_Finally_BEFORE', True);
    RunToNextPauseNoInternal(TstName, dcStepOver);
    TestEquals(TstName+': Got 1 exceptions: ', 1, FGotExceptCount);
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept_Finally_AFTER', -1);

    StepOverToLine(TstName,'BrkStepNestedExcept_Finally_END', True);

    TstName := AName + ' Run to except';
    RunToNextPauseNoLoopBreak(TstName, dcStepOver);  // Step to next finally
    TestLocation(TstName+': CurLine ', 'BrkStepNestedExcept', -1, 1);

    //StepOverToLine(TstName,'BrkStepNestedExcept_END', True);
    RunToNextPauseNoLoopBreak(TstName, dcStepOut);  // Step out

  end;

var
  ExeName, TstName, LName: String;
  TestAppRecRaise, TestAppRecStep: Integer;
begin
  ClearTestErrors;

  Src := GetCommonSourceFor(AppDir + 'ExceptTestPrg.pas');
  TestCompile(Src, ExeName);

  TestTrue('Start debugger', Debugger.StartDebugger(AppDir, ExeName));
  dbg := Debugger.LazDebugger;
  TFpDebugDebuggerProperties(dbg.GetProperties).NextOnlyStopOnStartLine := ANextOnlyStopOnStartLine;
  try
    dbg.Exceptions.Add('MyExceptionIgnore').Enabled := False;
    dbg.OnException      := @DoDebuggerException;

    (* RecRaise
       0 : Step in => Raise nested except and debugger breaks at except, step to finally
       1 : Step in => Raise nested except but debugger continues at except, step ends in finally
       2 : Step in => do NOT raise nested except. Step through all the finally
       3 : Step over => Raise nested except but debugger continues at except => nested finally NOT paused by debugger
    *)
    for TestAppRecRaise := 0 to 3 do
    for TestAppRecStep := 0 to 4 do
    if (TestAppRecRaise = 0) or (TestAppRecStep in [0,1,4]) then
    begin
      LName := Format('[RecRaise=%d / RecStep=%d] ', [TestAppRecRaise, TestAppRecStep]);
      ExpectEnterFinally(LName, TestAppRecStep,
         False, False, False,
         TestAppRecRaise = 1, TestAppRecRaise = 2, TestAppRecRaise = 3);

      TstName := LName + ' Run to Except (Main)';
      RunToNextPauseNoInternal(TstName, dcStepOver);  // Step to next finally
      //StepIfAtLine(TstName, 'BrkStep3FinOuter_IMPLICIT'); // 32 bit
      //StepIfAtLine(TstName, 'BrkStep3FinOuter_IMPLICIT_1');
      TestLocation(TstName+': CurLine ', 'BrkStepMainExcept1', -1, 1);

      TstName := LName + ' Step to After Except (Main)';
      RunToNextPauseNoInternal(TstName, dcStepOver);
      StepOverToLine(TstName,'BrkStepMainAfterExcept1', True);
      TestLocation(TstName+': CurLine ', 'BrkStepMainAfterExcept1', -1);
    end;

    ExpectNestedExcept('Nested Except 1');

    ExpectNestedExcept_Ignore('Nested Except Ignore');

    dbg.Stop;
  finally
    Debugger.FreeDebugger;
  end;

  AssertTestErrors;
end;

procedure TTestStepping.DoTestStepTryBlocks(ANextOnlyStopOnStartLine: Boolean);
  var NextCallAtName, NextTestAtName: String;
      BrkAtCall, BrkInTest: TDBGBreakPoint;
  procedure SetNextCallTo(ABrkName: String);
  begin
    NextCallAtName := ABrkName;
    if BrkAtCall <> nil then begin
      BrkAtCall.Enabled := False;
      BrkAtCall.ReleaseReference;
    end;
    BrkAtCall := Debugger.SetBreakPoint(Src, ABrkName);
  end;
  function IsAtNextCallTo: Boolean;
  begin
    Result := IsAtLocation(NextCallAtName);
  end;
  procedure RunToNextCallTo(ATestName: String);
  begin
    if not IsAtNextCallTo then
      RunToNextPauseEx(ATestName + ' (RunToNextCallTo) ', dcRun, NextCallAtName);
  end;

  procedure SetNextBreak(ABrkName: String);
  begin
    NextTestAtName := ABrkName;
    if BrkInTest <> nil then begin
      BrkInTest.Enabled := False;
      BrkInTest.ReleaseReference;
    end;
    BrkInTest := Debugger.SetBreakPoint(Src, ABrkName);
  end;
  function IsAtNextBreak: Boolean;
  begin
    Result := IsAtLocation(NextTestAtName);
  end;
  function RunToNextBreak(ATestName: String): boolean;
  begin
    Result := IsAtNextBreak;
    if not Result then
      Result := RunToNextPauseEx(ATestName + ' (RunToNextBreakTo) ', dcRun, NextTestAtName);
  end;


  function MaybeStepMissingLine(TstName, AFirstBrkName, AFinalBrkName, IgnReason: String; AnUnwantedBrk: String = ''; ACmd1: TDBGCommand = dcStepOver; ACmd2: TDBGCommand = dcStepOver): boolean;
  begin
    FIgnoreReason := IgnReason + ' (Should go first to '+AFinalBrkName+' - But may directly go to '+AFinalBrkName+')';
    Result := RunToNextPauseNoLoopBreak(TstName+'', ACmd1, AFirstBrkName);
    FIgnoreReason := '';
    if Result then begin
      Result := RunToNextPauseNoLoopBreak(TstName+'', ACmd2, AFinalBrkName);
    end
    else begin
      if AnUnwantedBrk <> '' then
        FIgnoreReason := IgnReason + ' (Skipped desired first step to '+AFinalBrkName+' - May have hit undesired '+AnUnwantedBrk+')';
      Result := TestLocation(TstName, AFinalBrkName, -1); // went directly to brk-2
      FIgnoreReason := '';

      if (AnUnwantedBrk <> '') and (not Result) and IsAtLocation(AnUnwantedBrk) then begin
        FIgnoreReason := IgnReason + ' (Went to unwanted '+AnUnwantedBrk+' - May have skipped first hit at '+AFirstBrkName+')';
        Result := RunToNextPauseNoLoopBreak(TstName+'', ACmd2, AFirstBrkName);
        FIgnoreReason := '';
        if (result) then
          Result := RunToNextPauseNoLoopBreak(TstName+'', ACmd2, AFinalBrkName)
        else
          Result := TestLocation(TstName, AFinalBrkName, -1); // went directly to brk-2
      end;
    end;
  end;

  function MaybeStepUnWantedLine(TstName, AUnwantedBrkName, AFinalBrkName, IgnReason: String; ACmd: TDBGCommand = dcStepOver): boolean;
  begin
    FIgnoreReason := IgnReason + ' (Should go to '+AFinalBrkName+' - But may first hit '+AUnwantedBrkName+')';
    Result := RunToNextPauseNoLoopBreak(TstName+'', ACmd, AFinalBrkName);
    FIgnoreReason := '';
    if Result then
      exit;

    TestLocation(TstName, AUnwantedBrkName, -1);
    Result := RunToNextPauseNoLoopBreak(TstName+'', ACmd, AFinalBrkName); // try again
  end;


  function StepEnterTry_F(TstName, ATryBrkName, ANextBrk: String; ACmd1: TDBGCommand = dcStepOver; ACmd2: TDBGCommand = dcStepOver): boolean;
  begin
    Result := MaybeStepMissingLine(TstName, ATryBrkName, ANextBrk, '[Step-enter-try (try-finally)]', '', ACmd1, ACmd2);
// 3.2.0 and above may be fine
    //Result := RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, ATryBrkName);
    //if Result then
    //Result := RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, ANextBrk);
  end;

  function StepEnterFinally(TstName, AFinallyBrkName, ANextBrk, ATryBrkName: String; ACmd1: TDBGCommand = dcStepOver; ACmd2: TDBGCommand = dcStepOver): boolean;
  begin
    Result := MaybeStepMissingLine(TstName, AFinallyBrkName, ANextBrk, '[Step-enter-finally]', {Unwanted:} ATryBrkName, ACmd1, ACmd2);
  end;

  function StepLeaveFinally(TstName, ANextBrk, ATryBrkName: String; ACmd: TDBGCommand = dcStepOver): boolean;
  begin
    Result := MaybeStepUnWantedLine(TstName, ATryBrkName, ANextBrk, '[Step-leave-finally]', ACmd);
    //Result := RunToNextPauseNoLoopBreak(TstName, ACmd, ANextBrk);
  end;

  function StepEnterTry_E(TstName, ATryBrkName, ANextBrk: String; ACmd1: TDBGCommand = dcStepOver; ACmd2: TDBGCommand = dcStepOver): boolean;
  begin
    Result := MaybeStepMissingLine(TstName, ATryBrkName, ANextBrk, '[Step-enter-try (try-except)]', '', ACmd1, ACmd2);
  end;

  function StepLeaveTryNoExcept(TstName, ANextBrk, ATryBrkName: String; ACmd: TDBGCommand = dcStepOver): boolean;
  begin
    Result := MaybeStepUnWantedLine(TstName, ATryBrkName, ANextBrk, '[Step-leave-try (try-except)]', ACmd);
    //Result := RunToNextPauseNoLoopBreak(TstName, ACmd, ANextBrk);
  end;


  procedure TestSimpleStepOver(TstName: String; ACmd: TDBGCommand = dcStepOver);
  var
    IsDone: Boolean;
  begin
    IsDone := False;
    try
      FIgnoreReason := '';
      SetNextBreak('BEGIN');
      if not RunToNextBreak(TstName) then exit;

      // Enter TRY_A
      if not RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, 'TRY_A_1')             then exit;
      if not StepOverToLine(TstName+'', 'TRY_A_1_BEFORE_TRY_B', False, dcStepOver, 3)  then exit;

      // Enter TRY_B
      if not StepEnterTry_F(TstName+'', 'TRY_B_1_EARLY', 'TRY_B_1', dcStepOver, ACmd) then exit;
      if not RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, 'TRY_B_1_BEFORE_TRY_C')     then exit;

      // Enter TRY_C
      if not StepEnterTry_E(TstName+'', 'TRY_C_EARLY', 'TRY_C_1', dcStepOver, ACmd) then exit;
      if not StepOverToLine(TstName+'', 'TRY_C_AFTER_RAISE_1', False, dcStepOver, 4)  then exit;
      if not RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, 'TRY_C_BEFORE_EXCEPT_1')     then exit;

      // Leave TRY_C => back to TRY_B
      if not StepLeaveTryNoExcept(TstName, 'TRY_B_AFTER_EXCEPT_1', 'TRY_C_EARLY') then exit;

      if not StepOverToLine(TstName+'', 'TRY_B_BEFORE_FINALLY', False, dcStepOver, 5)  then exit;
      // Enter TRY_B_FINALLY
      if not StepEnterFinally(TstName+'', 'TRY_B_FINALLY', 'TRY_B_FINALLY_1', 'TRY_B_1_EARLY', dcStepOver, ACmd) then exit;
      if not StepOverToLine(TstName+'', 'TRY_B_FINALLY_END', False, dcStepOver, 2)  then exit;

      // Leave TRY_B => back to TRY_A
      if not StepLeaveFinally(TstName, 'TRY_A_AFTER_FIN_B', 'TRY_B_1_EARLY') then exit;
      if not RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, 'TRY_A_BEFORE_FINALLY')      then exit;
      // Enter finally A
      if not StepEnterFinally(TstName+'', 'TRY_A_FINALLY_EARLY', 'TRY_A_FINALLY_1', 'TRY_A_1', dcStepOver, ACmd) then exit;

      // Enter TRY_D
      if not StepOverToLine(TstName+'', 'TRY_D_1', False, dcStepOver, 3)  then exit;
      // Enter TRY_E
      if not StepOverToLine(TstName+'', 'TRY_E_1', False, dcStepOver, 4)  then exit;
      if not StepOverToLine(TstName+'', 'TRY_E_BEFORE_FINALLY', False, dcStepOver, 3)  then exit;
      if not MaybeStepUnWantedLine(TstName, 'TRY_E_1', 'TRY_E_FINALLY_1', '[step-enter-finally-nop]') then exit;

      // Enter TRY_F
      if not StepOverToLine(TstName+'', 'TRY_F_1', False, dcStepOver, 4)  then exit;
      if not StepOverToLine(TstName+'', 'TRY_F_BEFORE_FINALLY', False, dcStepOver, 8)  then exit;
      // Enter finally_F
      if not StepEnterFinally(TstName+'', 'TRY_F_FINALLY', 'TRY_F_FINALLY_1', 'TRY_F_1', dcStepOver, ACmd) then exit;
      if not StepOverToLine(TstName+'', 'TRY_F_BEFORE_TRY_G', False, dcStepOver, 6)  then exit;

      // Enter TRY_G
      if not RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, 'TRY_G_1')      then exit;
      if not StepOverToLine(TstName+'', 'TRY_G_2', False, dcStepOver, 6)  then exit;
      if not StepEnterFinally(TstName+'', 'TRY_G_FINALLY', 'TRY_G_FINALLY_1', 'TRY_G_1', dcStepOver, ACmd) then exit;
      if not StepOverToLine(TstName+'', 'TRY_G_FINALLY_END', False, dcStepOver, 4)  then exit;

      // LEAVE TRY_G => back to TRY F
      if not StepLeaveFinally(TstName, 'TRY_F_FINALLY_AFTER_FIN_G', 'TRY_G_1') then exit;
      if not StepOverToLine(TstName+'', 'TRY_F_FINALLY_END', False, dcStepOver, 3)  then exit;

      // LEAVE TRY_F => back to TRY E
      if not StepLeaveFinally(TstName, 'TRY_E_FINALLY_AFTER_FIN_F', 'TRY_F_1') then exit;
      if not StepOverToLine(TstName+'', 'TRY_E_FINALLY_END', False, dcStepOver, 3)  then exit;

      // LEAVE TRY_E => back to TRY A (skip except _D)
      //if not StepLeaveFinally(TstName, 'TRY_A_FINALLY_AFTER_EXCEPT_D', 'TRY_E_1') then exit; // we can also hit the try for the EXCEPT block....
      if not StepOverToLine(TstName, 'TRY_A_FINALLY_AFTER_EXCEPT_D', False, dcStepOver, 4) then exit;
      if not StepOverToLine(TstName+'', 'TRY_A_FINALLY_END', False, dcStepOver, 3)  then exit;

      if not StepOverToLine(TstName+'', 'TRY_X_1', False, dcStepOver, 2)  then exit;
      if not StepEnterFinally(TstName+'', 'TRY_X_FINALLY', 'TRY_X_FINALLY_1', 'TRY_X_EARLY', dcStepOver, ACmd) then exit;
      if not StepOverToLine(TstName+'', 'BEFORE_END', False, dcStepOver, 4)  then exit;
      if not RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, 'END')      then exit;


      IsDone := True;
    finally
      TestTrue(TstName+' - DID NOT FINISH ALL TESTS', IsDone);
    end;
  end;

  procedure TestSimpleRunAndStepOver(TstName: String; ACmd: TDBGCommand = dcStepOver);
  (* Run into finally blocks, and see that step over behaves
  *)
  var
    IsDone: Boolean;
  begin
    IsDone := False;
    try
      SetNextBreak('TRY_C_BEFORE_EXCEPT_1');
      if not RunToNextBreak(TstName) then exit;
      // Leave TRY_C => back to TRY_B
      if not StepLeaveTryNoExcept(TstName, 'TRY_B_AFTER_EXCEPT_1', 'TRY_C_EARLY') then exit;

      if not StepOverToLine(TstName+'', 'TRY_B_BEFORE_FINALLY', False, dcStepOver, 5)  then exit;
      // Enter TRY_B_FINALLY
      if not StepEnterFinally(TstName+'', 'TRY_B_FINALLY', 'TRY_B_FINALLY_1', 'TRY_B_1_EARLY', dcStepOver, ACmd) then exit;
      if not StepOverToLine(TstName+'', 'TRY_B_FINALLY_END', False, dcStepOver, 2)  then exit;


      SetNextBreak('TRY_F_BEFORE_TRY_G');
      RunToNextBreak(TstName);
      // Enter TRY_G
      if not RunToNextPauseNoLoopBreak(TstName+'', dcStepOver, 'TRY_G_1')      then exit;
      if not StepOverToLine(TstName+'', 'TRY_G_2', False, dcStepOver, 6)  then exit;
      if not StepEnterFinally(TstName+'', 'TRY_G_FINALLY', 'TRY_G_FINALLY_1', 'TRY_G_1', dcStepOver, ACmd) then exit;
      if not StepOverToLine(TstName+'', 'TRY_G_FINALLY_END', False, dcStepOver, 4)  then exit;

      // LEAVE TRY_G => back to TRY F
      if not StepLeaveFinally(TstName, 'TRY_F_FINALLY_AFTER_FIN_G', 'TRY_G_1') then exit;
      if not StepOverToLine(TstName+'', 'TRY_F_FINALLY_END', False, dcStepOver, 3)  then exit;

      // LEAVE TRY_F => back to TRY E
      if not StepLeaveFinally(TstName, 'TRY_E_FINALLY_AFTER_FIN_F', 'TRY_F_1') then exit;
      if not StepOverToLine(TstName+'', 'TRY_E_FINALLY_END', False, dcStepOver, 3)  then exit;

      // LEAVE TRY_E => back to TRY A (skip except _D)
      //if not StepLeaveFinally(TstName, 'TRY_A_FINALLY_AFTER_EXCEPT_D', 'TRY_E_1') then exit; // we can also hit the try for the EXCEPT block....
      if not StepOverToLine(TstName, 'TRY_A_FINALLY_AFTER_EXCEPT_D', False, dcStepOver, 4) then exit;
      if not StepOverToLine(TstName+'', 'TRY_A_FINALLY_END', False, dcStepOver, 3)  then exit;



      IsDone := True;
    finally
      TestTrue(TstName+' - DID NOT FINISH ALL TESTS', IsDone);
    end;
  end;

  procedure TestSimpleRunAndStepOverExit(TstName: String);
  (* Run into finally blocks, and see that step over behaves
  *)
  var
    IsDone: Boolean;
    CurTstName: String;
  begin
    if (Compiler.Version < 030000) or
       ( (Compiler.CpuBitType = cpu32) and (Compiler.Version < 030200) )
    then
      exit;

    IsDone := False;
    try
      CurTstName := TstName + ' exit 1 ';
      SetNextBreak('TRY_F_3');
      if not RunToNextBreak(CurTstName) then exit;
      if not StepOverToLine(CurTstName+'', 'TRY_F_FINALLY', False, dcStepOver, 4, 'TRY_F_BEFORE_FINALLY')  then exit;
      if not RunToNextPauseNoLoopBreak(CurTstName+'', dcStepOver, 'TRY_F_FINALLY_1')      then exit;
      if not StepOverToLine(CurTstName+'', 'TRY_F_BEFORE_TRY_G', False, dcStepOver, 5)  then exit;
      if not StepOverToLine(CurTstName+'', 'TRY_G_1', False, dcStepOver, 2)  then exit;
      if not StepOverToLine(CurTstName+'', 'TRY_G_FINALLY', False, dcStepOver, 4)  then exit;
      if not StepOverToLine(CurTstName+'', 'TRY_F_FINALLY_END', False, dcStepOver, 8)  then exit;
      if not StepOverToLine(CurTstName+'', 'BAR1_RET', False, dcStepOver, 25, 'TRY_E_FINALLY_AFTER_FIN_F_2')  then exit;


(*
      CurTstName := TstName + ' exit 2 ';
      SetNextBreak('TRY_F_FINALLY_1');
      if not RunToNextBreak(CurTstName) then exit;
      if not StepOverToLine(CurTstName+'', 'BAR1_RET', False, dcStepOver, 35, 'TRY_E_FINALLY_AFTER_FIN_F_2')  then exit;
*)

      IsDone := True;
    finally
      TestTrue(CurTstName+' - DID NOT FINISH ALL TESTS', IsDone);
    end;
  end;

  procedure TestSimpleRunAndStepOut(TstName: String);
  (* Run into finally blocks, and see that step over behaves
  *)
  var
    IsDone: Boolean;
  begin
    IsDone := False;
    try
      SetNextBreak('TRY_C_BEFORE_EXCEPT_1');
      if not RunToNextBreak(TstName) then exit;
      RunToNextPauseNoLoopBreak(TstName+'', dcStepOut); // may end at BEGIN OR END
      if not StepOverToLine(TstName+'', 'BAR2_RET', False, dcStepOver, 4, 'BEFORE_END')  then exit;
      if not StepOverToLine(TstName+'', 'BAR1_RET_FIN', False, dcStepOver, 4)  then exit;
      RunToNextPauseNoLoopBreak(TstName+'', dcStepOut); // may end at BEGIN OR END
      if not StepOverToLine(TstName+'', 'FOO_RET', False, dcStepOver, 4)  then exit;


      SetNextBreak('TRY_F_BEFORE_TRY_G');
      RunToNextBreak(TstName);
      RunToNextPauseNoLoopBreak(TstName+'', dcStepOut); // may end at BEGIN OR END
      if not StepOverToLine(TstName+'', 'BAR2_RET', False, dcStepOver, 4, 'BEFORE_END')  then exit;
      RunToNextPauseNoLoopBreak(TstName+'', dcStepOut); // may end at BEGIN OR END
      if not StepOverToLine(TstName+'', 'FOO_RET', False, dcStepOver, 4)  then exit;

      IsDone := True;
    finally
      TestTrue(TstName+' - DID NOT FINISH ALL TESTS', IsDone);
    end;
  end;


var
  ExeName: String;
begin
  ClearTestErrors;
  FGotExceptCount := 0;
  FContinue := False;
  BrkAtCall := nil;
  BrkInTest := nil;

  Src := GetCommonSourceFor(AppDir + 'StepTryBlocksPrg.pas');
  TestCompile(Src, ExeName);

  TestTrue('Start debugger', Debugger.StartDebugger(AppDir, ExeName));
  dbg := Debugger.LazDebugger;
  TFpDebugDebuggerProperties(dbg.GetProperties).NextOnlyStopOnStartLine := ANextOnlyStopOnStartLine;
  try
    dbg.OnException      := @DoDebuggerException;

    SetNextCallTo('CALL_TestSimpleStepOver');
    RunToNextCallTo('');

    SetNextCallTo('CALL_TestSimpleRunAndStepOver'); // catch any run-away - stop here
    TestSimpleStepOver('Simple Step-Over: ');
    if BrkInTest <> nil then BrkInTest.Enabled := False;
    RunToNextCallTo('');

(* ONLY works if the compiler/RTL has no debug info / otherwise we correctly ?? step into the rtl *)
    //SetNextCallTo('CALL_a');
    //TestSimpleStepOver('Simple Step-Into: ', dcStepInto);
    //RunToNextCallTo('');

    SetNextCallTo('CALL_TestSimpleRunAndStepOverExit'); // catch any run-away - stop here
    TestSimpleRunAndStepOver('TestSimpleRunAndStepOver: ');
    if BrkInTest <> nil then BrkInTest.Enabled := False;
    RunToNextCallTo('');

    SetNextCallTo('CALL_TestSimpleRunAndStepOut'); // catch any run-away - stop here
    TestSimpleRunAndStepOverExit('TestSimpleRunAndStepOverExit');
    if BrkInTest <> nil then BrkInTest.Enabled := False;
    RunToNextCallTo('');

    SetNextCallTo('CALL_xx'); // catch any run-away - stop here
    TestSimpleRunAndStepOut('TestSimpleRunAndStepOut: ');
    if BrkInTest <> nil then BrkInTest.Enabled := False;
    RunToNextCallTo('');




    dbg.Stop;
  finally
    FIgnoreReason := '';
    Debugger.FreeDebugger;
  end;

  AssertTestErrors;
end;

function TTestStepping.RunToNextPauseNoLoopBreak(AName: String;
  ACmd: TDBGCommand; ATimeOut: Integer; AnExpAtBrkName: String;
  AnExpAcceptLinesBefore: integer): Boolean;
begin
  if AnExpAtBrkName <> '' then
    AName := AName + ' (TO: '+AnExpAtBrkName+')';
  THookedFpDebugDebugger(dbg).LockRelCount := 0;
  Result := RunToNextPauseNoInternal(AName, ACmd, ATimeOut);

  // LockRelease called in 2 * DoState / 1 * DebugLoopFinished
  if not TestEquals(AName + ' ' + dbgs(ACmd)+' - lock cnt', 3, THookedFpDebugDebugger(dbg).LockRelCount)
  then
    Result := False;

  if AnExpAtBrkName <> '' then
    if not TestLocation(aName + ' ' + dbgs(ACmd)+': Finished at Line ', AnExpAtBrkName, -1, AnExpAcceptLinesBefore)
    then
      Result := False;
end;

function TTestStepping.RunToNextPauseNoLoopBreak(AName: String;
  ACmd: TDBGCommand; AnExpAtBrkName: String; AnExpAcceptLinesBefore: integer
  ): Boolean;
begin
  Result := RunToNextPauseNoLoopBreak(AName, ACmd, 5000, AnExpAtBrkName, AnExpAcceptLinesBefore);
end;

function TTestStepping.RunToNextPauseEx(AName: String; ACmd: TDBGCommand;
  AnExpAtBrkName: String; AnExpAcceptLinesBefore: integer): Boolean;
begin
  if AnExpAtBrkName <> '' then
    AName := AName + ' (TO: '+AnExpAtBrkName+')';
  Result := Debugger.RunToNextPause(ACmd);

  if AnExpAtBrkName <> '' then
    if not TestLocation(aName + ' ' + dbgs(ACmd)+': Finished at Line ', AnExpAtBrkName, -1, AnExpAcceptLinesBefore)
    then
      Result := False;
end;


initialization

  RegisterDbgTest(TTestStepping);

  ControlTest              := TestControlRegisterTest('TTestStepping');
  ControlTest_NextOnlyTrue := TestControlRegisterTest('TTestStepping_NextOnly=True', ControlTest);
  ControlTestStepOver      := TestControlRegisterTest('TTestStepOver', ControlTest_NextOnlyTrue);
  ControlTestStepOverInstr := TestControlRegisterTest('TTestStepOverInstr', ControlTest_NextOnlyTrue);
  ControlTestExceptionStepOutEx  := TestControlRegisterTest('TTestExceptionStepOutEx', ControlTest_NextOnlyTrue);
  ControlTestExceptionStepOverEx := TestControlRegisterTest('TTestExceptionStepOverEx', ControlTest_NextOnlyTrue);
  ControlTestStepTryBlocks := TestControlRegisterTest('TTestStepTryBlocks', ControlTest_NextOnlyTrue);

  ControlTest_NextOnly              := TestControlRegisterTest('TTestStepping_NextOnly=False', ControlTest);
  ControlTestStepOver_NextOnly      := TestControlRegisterTest('TTestStepOver_NextOnly=False', ControlTest_NextOnly);
  ControlTestStepOverInstr_NextOnly := TestControlRegisterTest('TTestStepOverInstr_NextOnly=False', ControlTest_NextOnly);
  ControlTestExceptionStepOutEx_NextOnly  := TestControlRegisterTest('TTestExceptionStepOutEx_NextOnly=False', ControlTest_NextOnly);
  ControlTestExceptionStepOverEx_NextOnly := TestControlRegisterTest('TTestExceptionStepOverEx_NextOnly=False', ControlTest_NextOnly);
  ControlTestStepTryBlocks_NextOnly := TestControlRegisterTest('TTestStepTryBlocks_NextOnly=False', ControlTest_NextOnly);

end.