File: fpdbgrsp.pas

package info (click to toggle)
lazarus 3.0%2Bdfsg1-8~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 253,428 kB
  • sloc: pascal: 2,075,465; xml: 455,989; makefile: 323,023; cpp: 93,069; sh: 3,305; java: 609; perl: 297; sql: 222; ansic: 137
file content (1135 lines) | stat: -rw-r--r-- 33,030 bytes parent folder | download | duplicates (2)
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
unit FpDbgRsp;

interface

uses
  Classes, SysUtils, ssockets, DbgIntfDebuggerBase, DbgIntfBaseTypes,
  FpDbgClasses;

const
  // Possible signal numbers that can be expected over rsp
  // for now only cater for posix like signals
  SIGHUP     = 1;
  SIGINT     = 2;
  SIGQUIT    = 3;
  SIGILL     = 4;
  SIGTRAP    = 5;
  SIGABRT    = 6;
  SIGIOT     = 6;
  SIGBUS     = 7;
  SIGFPE     = 8;
  SIGKILL    = 9;
  SIGUSR1    = 10;
  SIGSEGV    = 11;
  SIGUSR2    = 12;
  SIGPIPE    = 13;
  SIGALRM    = 14;
  SIGTERM    = 15;
  SIGSTKFLT  = 16;
  SIGCHLD    = 17;
  SIGCONT    = 18;
  SIGSTOP    = 19;
  SIGTSTP    = 20;
  SIGTTIN    = 21;
  SIGTTOU    = 22;
  SIGURG     = 23;
  SIGXCPU    = 24;
  SIGXFSZ    = 25;
  SIGVTALRM  = 26;
  SIGPROF    = 27;
  SIGWINCH   = 28;
  SIGIO      = 29;
  SIGPOLL    = SIGIO;
  SIGPWR     = 30;
  SIGUNUSED  = 31;

type
  { TRemoteConfig }

  TRemoteConfig = class(TDbgProcessConfig)
  private
    FHost: string;
    FPort: integer;
    FUploadBinary: boolean;
    FAfterConnectMonitorCmds: TStringList;
    FSkipSectionsList: TStringList;
    FAfterUploadBreakZero: boolean;
    FAfterUploadMonitorCmds: TStringList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;

    property Host: string read FHost write FHost;
    property Port: integer read FPort write FPort;
    property UploadBinary: boolean read FUploadBinary write FUploadBinary;
    property AfterConnectMonitorCmds: TStringList read FAfterConnectMonitorCmds write FAfterConnectMonitorCmds;
    property SkipSectionsList: TStringList read FSkipSectionsList write FSkipSectionsList;
    property AfterUploadBreakZero: boolean read FAfterUploadBreakZero write FAfterUploadBreakZero;
    property AfterUploadMonitorCmds: TStringList read FAfterUploadMonitorCmds write FAfterUploadMonitorCmds;
  end;

  TInitializedRegister = record
    Initialized: boolean;
    Value: qword; // sized to handle largest register, should truncate as required to smaller registers
  end;
  TInitializedRegisters = array of TInitializedRegister;

  TStopReason = (srNone, srSWBreakPoint, srHWBreakPoint, srWriteWatchPoint, srReadWatchPoint, srAnyWatchPoint);

  TStatusEvent = record
    signal: integer;
    coreID: integer;
    processID: integer;
    threadID: integer;
    stopReason: TStopReason;
    watchPointAddress: qword;  // contains address which triggered watch point
    registers: TInitializedRegisters;
  end;

  { TRspConnection }

  TRspConnection = class(TInetSocket)
  private
    FState: integer;
    FStatusEvent: TStatusEvent;
    fCS: TRTLCriticalSection;
    FFileName: string;
    FOwner: TDbgProcess;
    // Catch exceptions and store as socket errors
    FSockErr: boolean;
    FConfig: TRemoteConfig;
    procedure SetRegisterCacheSize(sz: cardinal);
    function WaitForData(timeout_ms: integer): integer; overload;

    // Wrappers to catch exceptions and set SockErr
    function SafeReadByte: byte;
    function SafeWrite(const buffer; count : Longint): Longint;
    procedure SafeWriteByte(b: Byte);

    function ReadReply(out retval: string): boolean;
    function SendCommand(const cmd: string): boolean;
    // Send command and wait for acknowledge
    function SendCommandAck(const cmd: string): boolean;
    // Return reply to cmd
    function SendCmdWaitForReply(const cmd: string; out reply: string): boolean;

    // Note that numbers are transmitted as hex characters in target endian sequence
    // For little endian targets this creates an endian swap if the string is parsed by Val
    // because a hex representation of a number is interpreted as big endian
    function HexToIntLittleEndian(constref hextext: string; out value: qword): boolean;
    function IntToHexLittleEndian(value: qword): string;

    function HexEncodeStr(s: string): string;
    function HexDecodeStr(hexcode: string): string;
  public
    constructor Create(AFileName: string; AOwner: TDbgProcess; AConfig: TRemoteConfig); Overload;
    destructor Destroy; override;
    // Wait for async signal - blocking
    function WaitForSignal(out msg: string; out registers: TInitializedRegisters): integer;
    procedure ResetStatusEvent;

    procedure Break();
    function Kill(): boolean;
    function Detach(): boolean;
    function MustReplyEmpty: boolean;
    function SetBreakWatchPoint(addr: PtrUInt; BreakWatchKind: TDBGWatchPointKind; watchsize: integer = 1; HWbreak: boolean = true): boolean;
    function DeleteBreakWatchPoint(addr: PtrUInt; BreakWatchKind: TDBGWatchPointKind; watchsize: integer = 1; HWBreak: boolean = true): boolean;
    // TODO: no support for thread ID or different address
    function Continue(): boolean;
    function SingleStep(): boolean;

    // Data exchange
    function ReadDebugReg(ind: byte; out AVal: TDbgPtr): boolean;
    function WriteDebugReg(ind: byte; AVal: TDbgPtr): boolean;
    function ReadRegisters(out regs; const sz: integer): boolean;  // size is not required by protocol, but is used to preallocate memory for the response
    function WriteRegisters(constref regs; const sz: integer): boolean;
    function ReadData(const AAddress: TDbgPtr; const ASize: cardinal; out AData
      ): boolean;
    function WriteData(const AAdress: TDbgPtr;
      const ASize: Cardinal; const AData): Boolean;

    function SendMonitorCmd(const s: string): boolean;
    // check state of target - ?
    function Init: integer;

    property State: integer read FState;
    property RegisterCacheSize: cardinal write SetRegisterCacheSize;
    property lastStatusEvent: TStatusEvent read FStatusEvent;
    property SockErr: boolean read FSockErr;
  end;

implementation

uses
  {$ifdef FORCE_LAZLOGGER_DUMMY} LazLoggerDummy {$else} LazLoggerBase {$endif}, StrUtils,
  FpImgReaderBase,
  {$IFNDEF WINDOWS}BaseUnix, termio;
  {$ELSE}winsock2, windows;
  {$ENDIF}

var
  DBG_VERBOSE, DBG_WARNINGS, DBG_RSP: PLazLoggerLogGroup;

{ TRemoteConfig }

constructor TRemoteConfig.Create;
begin
  FHost := 'localhost';
  FPort := 1234;        // default port for qemu
  FUploadBinary := false;
  FAfterConnectMonitorCmds := TStringList.Create;
  FSkipSectionsList := TStringList.Create;
  FAfterUploadBreakZero := false;
  FAfterUploadMonitorCmds := TStringList.Create;
end;

destructor TRemoteConfig.Destroy;
begin
  FreeAndNil(FAfterConnectMonitorCmds);
  FreeAndNil(FSkipSectionsList);
  FreeAndNil(FAfterUploadMonitorCmds);
end;

procedure TRemoteConfig.Assign(Source: TPersistent);
var
  ASource: TRemoteConfig;
begin
  if Assigned(Source) and (Source is TRemoteConfig) then
  begin
    ASource := TRemoteConfig(Source);
    FHost := ASource.Host;
    FPort := ASource.Port;
    FUploadBinary := ASource.UploadBinary;
    FAfterUploadBreakZero := ASource.AfterUploadBreakZero;
    FAfterConnectMonitorCmds.Assign(ASource.AfterConnectMonitorCmds);
    FSkipSectionsList.Assign(ASource.SkipSectionsList);
    FAfterUploadMonitorCmds.Assign(ASource.AfterUploadMonitorCmds);
  end;
end;

procedure TRspConnection.SetRegisterCacheSize(sz: cardinal);
begin
  SetLength(FStatusEvent.registers, sz);
end;

procedure TRspConnection.ResetStatusEvent;
var
  i: integer;
begin
  with FStatusEvent do
  begin
    signal := 0;
    coreID := 0;
    processID := 0;
    threadID := 0;
    stopReason := srNone;
    watchPointAddress := 0;
    for i := low(registers) to high(registers) do
    begin
      registers[i].Initialized := false;
      registers[i].Value := 0;
    end;
  end;
end;

function TRspConnection.WaitForData(timeout_ms: integer): integer;
{$if defined(unix) or defined(windows)}
var
  FDS: TFDSet;
  TimeV: TTimeVal;
{$endif}
begin
  if SockErr then
  begin
    Result := -1;
    exit;
  end;
  TimeV.tv_usec := timeout_ms * 1000;  // 1 msec
  TimeV.tv_sec := 0;
  FDS := Default(TFDSet);
{$ifdef unix}
  fpFD_Zero(FDS);
  fpFD_Set(self.Handle, FDS);
  Result := fpSelect(self.Handle + 1, @FDS, nil, nil, @TimeV);
{$else}
{$ifdef windows}
  FD_Zero(FDS);
  FD_Set(self.Handle, FDS);
  Result := winsock2.Select(self.Handle + 1, @FDS, nil, nil, @TimeV);
{$endif}
{$endif}
end;

function TRspConnection.SafeReadByte: byte;
begin
  try
    Result := ReadByte;
  except
    FSockErr := true;
    Result := 0;
  end;
end;

function TRspConnection.SafeWrite(const buffer; count: Longint): Longint;
begin
  try
    Result := Write(buffer, count);
  except
    FSockErr := true;
    Result := 0;
  end;
end;

procedure TRspConnection.SafeWriteByte(b: Byte);
begin
  try
    WriteByte(b);
  except
    FSockErr := true;
  end;
end;

function TRspConnection.SendCommand(const cmd: string): boolean;
var
  checksum: byte;
  i, totalSent: integer;
  s: string;
begin
  Result := false;
  if SockErr then exit;
  checksum := 0;
  for i := 1 to length(cmd) do
    checksum := byte(checksum + ord(cmd[i]));

  s := '$'+cmd+'#'+IntToHex(checksum, 2);
  totalSent := SafeWrite(s[1], length(s));

  result := (totalSent = length(s));
  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: TRspConnection.FSendRspCommand error.'])
  else
    DebugLn(DBG_RSP, ['RSP -> ', cmd]);
end;

function TRspConnection.SendCommandAck(const cmd: string): boolean;
var
  c: char;
  retryCount: integer;
begin
  result := false;
  if SockErr then exit;
  retryCount := 0;

  repeat
    if SendCommand(cmd) then
    begin
      // now check if target returned error, resend ('-') or ACK ('+')
      // No support for ‘QStartNoAckMode’, i.e. always expect a -/+
      c := char(SafeReadByte);
      result := c = '+';
      if not result then
        inc(retryCount);
    end
    else
      inc(retryCount);
  // Abort this command if no ACK after 5 attempts
  until result or (retryCount > 5) or SockErr;
end;

function TRspConnection.ReadReply(out retval: string): boolean;
const failcountmax = 1000;
var
  c: char;
  i: integer;
  cksum, calcSum: byte;
  outputPacket: boolean;
begin
  Result := false;
  if SockErr then exit;

  repeat  // Outer loop runs until no more "O" packets received
    i := 0;
    retval := '';
    repeat
      c := chr(SafeReadByte);
      inc(i);
      retval := retval + c;
    until (c = '$') or (i = failcountmax) or SockErr;  // exit loop after start or count expired

    if c <> '$' then
    begin
      DebugLn(DBG_WARNINGS, ['Warning: Timeout waiting for RSP reply']);
      result := false;
      retval := '';
      exit;
    end
    else if i > 1 then
      DebugLn(DBG_WARNINGS, ['Warning: Discarding unexpected data before start of new message', retval])
    else if SockErr then
      DebugLn(DBG_WARNINGS, ['Warning: socket error.']);

    c := chr(SafeReadByte);
    retval := '';
    calcSum := 0;
    while c <> '#' do
    begin
      calcSum := byte(calcSum+byte(c));

      if c=#$7D then // escape marker, unescape data
      begin
        c := char(SafeReadByte);

        // Something weird happened
        if c = '#' then
        begin
          DebugLn(DBG_WARNINGS, ['Warning: Received end of packet marker in escaped sequence: ', c]);
          break;
        end;

        calcSum := byte(calcSum + byte(c));

        c := char(byte(c) xor $20);
      end;

      retval := retval + c;
      c := char(SafeReadByte);
    end;

    cksum := StrToInt('$' + char(SafeReadByte) + char(SafeReadByte));
    if not (calcSum = cksum) then
      DebugLn(DBG_WARNINGS, ['Warning: Reply packet with invalid checksum: ', retval]);

    // Check if this packet is a console output packet, which isn't acknowledged
    // Todo: display output somewhere
    if (length(retval) > 2) and (retval[1] = 'O') and (retval[2] <> 'K') then
    begin
      outputPacket := True;
      // Output should be hex encoded, length should be odd
      if Odd(length(retval)) then
      begin
        delete(retval, 1, 1);
        DebugLn(DBG_RSP, ['RSP <- <Console output> ', HexDecodeStr(retval)]);
      end
      else
        DebugLn(DBG_WARNINGS, ['RSP <- <Possible unencoded output>: ', retval]);
    end
    else
    begin
      outputPacket := False;
    end;
  until not outputPacket;

  // Do not acknowledge OK
  if retval <> 'OK' then
    SafeWriteByte(byte('+'));
  result := not SockErr;
  DebugLn(DBG_RSP, ['RSP <- ', retval]);
end;

function TRspConnection.SendCmdWaitForReply(const cmd: string; out reply: string
  ): boolean;
var
  retryCount: integer;
begin
  reply := '';
  if SockErr then exit;
  retryCount := 0;

  if SendCommandAck(cmd) then
  begin
    // Read reply, with retry if no success
    repeat
      result := ReadReply(reply);
      if not result then
      begin
        inc(retryCount);
        SafeWriteByte(ord('-'));
      end;
    until result or (retryCount > 5) or SockErr;
  end;

  if retryCount > 5 then
    DebugLn(DBG_WARNINGS, ['Warning: Retries exceeded in TRspConnection.FSendCmdWaitForReply for cmd: ', cmd]);
end;

function TRspConnection.HexToIntLittleEndian(constref
  hextext: string; out value: qword): boolean;
var
  err: integer;
begin
  Val('$'+hextext, value, err);
  if (err = 0) then
  begin
    result := true;
    case length(hextext) of
      1,2: ; // no conversion required
      3,4:  value := SwapEndian(word(value));
      5..8:  value := SwapEndian(dword(value));
      9..16: value := SwapEndian(value);
      else
      begin
        result := false;
        DebugLn(DBG_WARNINGS, ['Warning: Unexpected hex length: ', IntToStr(length(hextext))]);
      end;
    end;
  end
  else
    result := false;
end;

function TRspConnection.IntToHexLittleEndian(value: qword): string;
var
  b, accumulator: byte;
begin
  Result := '';
  accumulator := 0;
  while value > 0 do
  begin
    inc(accumulator, 2);
    b := byte(value);
    value := value shr 8;
    Result := Result + IntToHex(b, 2);
  end;
end;

function TRspConnection.HexEncodeStr(s: string): string;
var
  i: integer;
  tmp: string;
begin
  setlength(Result, length(s)*2);
  for i := 1 to length(s) do
  begin
    tmp := HexStr(ord(s[i]), 2);
    Result[2*i - 1] := tmp[1];
    Result[2*i] := tmp[2];
  end;
end;

function TRspConnection.HexDecodeStr(hexcode: string): string;
var
  i: integer;
  s: string;
begin
  SetLength(Result, length(hexCode) div 2);
  for i := 1 to length(Result) do
  begin
    s := '$' + hexCode[2*i-1] + hexCode[2*i];
    result[i] := char(StrToInt(s));
  end;
end;

procedure TRspConnection.Break();
begin
  EnterCriticalSection(fCS);
  try
    SafeWriteByte(3);  // Ctrl-C
    DebugLn(DBG_RSP, ['RSP -> <Ctrl+C>']);
  finally
    LeaveCriticalSection(fCS);
  end;
end;

function TRspConnection.Kill(): boolean;
var
  c: char;
begin
  EnterCriticalSection(fCS);
  try
    result := SendCommand('k');
    // Swallow the last ack if send
    if Result and not SockErr then
      result := WaitForData(1000) > 0;
  finally
    LeaveCriticalSection(fCS);
  end;

  if result then
  begin
    c := char(SafeReadByte);
    Result := c = '+';
  end;
end;

function TRspConnection.Detach(): boolean;
var
  reply: string;
begin
  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply('D', reply);
  finally
    LeaveCriticalSection(fCS);
  end;
  result := not(SockErr) and (pos('OK', reply) = 1);
end;

constructor TRspConnection.Create(AFileName: string; AOwner: TDbgProcess;
  AConfig: TRemoteConfig);
var
  FSocketHandler: TSocketHandler;
begin
  // Just copy reference to AConfig
  FConfig := AConfig;
  { Create a socket handler, so that TInetSocket.Create call doesn't automatically connect.
    This can raise an exception when connection fails.
    The FSocketHandler instance will be managed by TInetSocket. }
  FSocketHandler := TSocketHandler.Create;
  inherited Create(FConfig.Host, FConfig.Port, FSocketHandler);
  InitCriticalSection(fCS);
  FFileName := AFileName;
  FOwner := AOwner;
  FSockErr := false;
end;

destructor TRspConnection.Destroy;
begin
  inherited;
  DoneCriticalSection(fCS);
end;

function TRspConnection.WaitForSignal(out msg: string; out
  registers: TInitializedRegisters): integer;
var
  res: boolean;
  startIndex, colonIndex, semicolonIndex, i: integer;
  tmp, tmp2: qword;
  part1, part2, s: string;
begin
  result := 0;
  res := false;
  SetLength(registers, 0);

  EnterCriticalSection(fCS);
  try
    // -1 if no data could be read, e.g. socket is closed
    // 0 if timeout.  Use timeout so that asynchronous evens such as break can also be processed
    i := WaitForData(500);
    if i <= 0 then
    begin
      msg := '';
      if i < 0 then
        result := SIGHUP;
      exit;
    end;

    try
      res := ReadReply(msg);
    except
      on E: Exception do
        DebugLn(DBG_WARNINGS, ['Warning: WaitForSignal exception: ', E.Message]);
    end;
  finally
    LeaveCriticalSection(fCS);
  end;

  if res then
  begin
    if (length(msg) > 2) and (msg[1] in ['S', 'T']) then
    begin
      try
        ResetStatusEvent;
        result := StrToInt('$' + copy(msg, 2, 2));
        FState := result;
        FStatusEvent.signal := result;
        if (msg[1] = 'T') and (length(msg) > 6) then // not much meaning can be returned in less than 2 bytes
        begin
          startIndex := 4; // first part of message TAA... is already parsed, rest should be nn:rr; pairs
          repeat
            colonIndex := posex(':', msg, startIndex);
            semicolonIndex := posex(';', msg, startIndex);
            // Check if there is a first part
            if colonIndex > startIndex then
              part1 := copy(msg, startIndex, colonIndex-startIndex)
            else
              part1 := '';

            if (part1 <> '') and (semicolonIndex > colonIndex + 1) then
              part2 := copy(msg, colonIndex+1, semicolonIndex - colonIndex - 1)
            else
              part2 := '';

            // Check for stop reason
            case part1 of
              'watch','rwatch','awatch':
              begin
                case part1 of
                  'watch': FStatusEvent.stopReason := srWriteWatchPoint;
                  'rwatch': FStatusEvent.stopReason := srReadWatchPoint;
                  'awatch': FStatusEvent.stopReason := srAnyWatchPoint;
                end;
                Val('$'+part2, tmp, i);
                if i = 0 then
                  FStatusEvent.watchPointAddress := tmp
                else
                  DebugLn(DBG_WARNINGS, format('Invalid value received for %s: %s ', [part1, part2]));
              end;
              'swbreak':
              begin
                FStatusEvent.stopReason := srSWBreakPoint;
              end;
              'hwbreak':
              begin
                FStatusEvent.stopReason := srHWBreakPoint;
              end;
              'thread':
              begin
                if length(part2) > 0 then
                begin
                  // ... optionally include both process and thread ID fields, as ‘ppid.tid’.
                  if (part2[1] = 'p') then
                  begin
                    i := pos('.', part2);
                    if i > 2 then
                    begin
                      s := copy(part2, 2, i-1);
                      if HexToIntLittleEndian(s, tmp) then
                        FStatusEvent.processID := tmp
                      else
                      begin
                        FStatusEvent.processID := 0;
                        DebugLn(DBG_WARNINGS, format('Invalid process ID prefix: [%s]', [s]));
                      end;

                      s := copy(part2, i+1, 255);
                      if HexToIntLittleEndian(s, tmp) then
                        FStatusEvent.threadID := tmp
                      else
                      begin
                        FStatusEvent.threadID := 0;
                        DebugLn(DBG_WARNINGS, format('Invalid thread ID postfix: [%s]', [s]));
                      end;
                    end
                    else
                      DebugLn(DBG_WARNINGS, format('Not enough text for a process ID: [%s]', [part2]));
                  end
                  else
                  // Expect only thread ID
                  begin
                    if HexToIntLittleEndian(part2, tmp) then
                      FStatusEvent.threadID := tmp
                    else
                    begin
                      FStatusEvent.threadID := 0;
                      DebugLn(DBG_WARNINGS, format('Invalid thread ID postfix: [%s]', [part2]));
                    end;
                  end;
                end
                else
                  DebugLn(DBG_WARNINGS, 'Stop reason "thread" with no thread data');
              end;
            else // catch valid hex numbers - will be register info
              begin
                // check if part1 is a number, this should then be a register index
                if HexToIntLittleEndian(part1, tmp) and HexToIntLittleEndian(part2, tmp2) then
                begin
                  if tmp < length(FStatusEvent.registers) then
                  begin
                    FStatusEvent.registers[tmp].Value := tmp2;
                    FStatusEvent.registers[tmp].Initialized := true;
                  end
                  else
                    DebugLn(DBG_WARNINGS, format('Register index exceeds total number of registers (%d > %d)',
                            [tmp, length(FStatusEvent.registers)]));
                end
                else
                  DebugLn(DBG_WARNINGS, format('Ignoring stop reply pair [%s:%s] ', [part1, part2]));
              end;
            end;
            startIndex := semicolonIndex + 1;
          until (semicolonIndex = 0) or (semicolonIndex = length(msg));
        end;
      except
        DebugLn(DBG_WARNINGS, ['Error converting signal number from reply: ', msg]);
      end;
    end
    else
      DebugLn(DBG_WARNINGS, ['Unexpected WaitForSignal reply: ', msg]);
  end;
end;

function TRspConnection.MustReplyEmpty: boolean;
var
  reply: string;
begin
  EnterCriticalSection(fCS);
  try
    SendCmdWaitForReply('vMustReplyEmpty', reply);
  finally
    LeaveCriticalSection(fCS);
  end;
  result := not(SockErr) and (reply = '');
  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: vMustReplyEmpty command returned unexpected result: ', reply]);
end;

function TRspConnection.SetBreakWatchPoint(addr: PtrUInt;
  BreakWatchKind: TDBGWatchPointKind; watchsize: integer; HWbreak: boolean
  ): boolean;
var
  cmd, reply: string;
begin
  cmd := 'Z';
  case BreakWatchKind of
    wpkWrite: cmd := cmd + '2,' + IntToHex(addr, 4) + ',' + IntToHex(watchsize, 4);
    wpkRead:  cmd := cmd + '3,' + IntToHex(addr, 4) + ',' + IntToHex(watchsize, 4);
    wpkReadWrite: cmd := cmd + '4,' + IntToHex(addr, 4) + ',' + IntToHex(watchsize, 4);
    // NOTE: Not sure whether hardware break is better than software break, depends on gdbserver implementation...
    wkpExec:
      if HWbreak then
        cmd := cmd + '1,' + IntToHex(addr, 4) + ',00'
      else
        cmd := cmd + '0,' + IntToHex(addr, 4) + ',00';
  end;

  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply(cmd, reply) and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if result then
    result := pos('OK', reply) > 0;
end;

function TRspConnection.DeleteBreakWatchPoint(addr: PtrUInt;
  BreakWatchKind: TDBGWatchPointKind; watchsize: integer; HWBreak: boolean
  ): boolean;
var
  cmd, reply: string;
begin
  cmd := 'z';
  case BreakWatchKind of
    wpkWrite: cmd := cmd + '2,' + IntToHex(addr, 4) + ',' + IntToHex(watchsize, 4);
    wpkRead:  cmd := cmd + '3,' + IntToHex(addr, 4) + ',' + IntToHex(watchsize, 4);
    wpkReadWrite: cmd := cmd + '4,' + IntToHex(addr, 4) + ',' + IntToHex(watchsize, 4);
    // NOTE: Not sure whether hardware break is better than software break, depends on gdbserver implementation...
    wkpExec:
      if HWBreak then
        cmd := cmd + '1,' + IntToHex(addr, 4) + ',00'
      else
        cmd := cmd + '0,' + IntToHex(addr, 4) + ',00';
  end;

  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply(cmd, reply) and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if result then
    result := pos('OK', reply) > 0;
end;

function TRspConnection.Continue(): boolean;
begin
  DebugLn(DBG_VERBOSE, ['TRspConnection.Continue() called']);
  EnterCriticalSection(fCS);
  try
    result := SendCommandAck('c') and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: Continue command failure in TRspConnection.Continue()']);
end;

function TRspConnection.SingleStep(): boolean;
begin
  EnterCriticalSection(fCS);
  try
    result := SendCommandAck('s') and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: SingleStep command failure in TRspConnection.SingleStep()']);
end;

function TRspConnection.ReadDebugReg(ind: byte; out AVal: TDbgPtr): boolean;
var
  cmd, reply: string;
  tmp: qword;
begin
  cmd := 'p'+IntToHex(ind, 2);
  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply(cmd, reply) and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if result then
  begin
    result := HexToIntLittleEndian(reply, tmp);
    AVal := PtrUInt(tmp);
  end;

  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: "p" command returned unexpected result: ', reply]);
end;

function TRspConnection.WriteDebugReg(ind: byte; AVal: TDbgPtr): boolean;
var
  cmd, reply: string;
begin
  cmd := 'P'+IntToHex(ind, 2)+'='+IntToHexLittleEndian(AVal);
  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply(cmd, reply) and (reply = 'OK');
  finally
    LeaveCriticalSection(fCS);
  end;
  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: "P" command returned unexpected result: ', reply]);
end;

function TRspConnection.ReadRegisters(out regs; const sz: integer): boolean;
var
  reply: string;
  b: array of byte;
  i: integer;
begin
  reply := '';
  setlength(b, sz);
  // Normal receive error, or an error response of the form Exx
  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply('g', reply) and ((length(reply) > 4) and (reply[1] <> 'E'))
      and (length(reply) = 2*sz);
  finally
    LeaveCriticalSection(fCS);
  end;
  Result := Result and not(SockErr);
  if Result then
  begin
    for i := 0 to sz-1 do
      b[i] := StrToInt('$'+reply[2*i+1]+reply[2*i+2]);
    result := true;
  end
  else
  begin
    DebugLn(DBG_WARNINGS, ['Warning: "g" command returned unexpected result: ', reply]);
    FillByte(b[0], sz, 0);
  end;
  Move(b[0], regs, sz);
end;

function TRspConnection.WriteRegisters(constref regs; const sz: integer
  ): boolean;
var
  cmd, reply, s: string;
  i, offset: integer;
  pb: PByte;
begin
  pb := @regs;
  result := false;
  reply := '';
  cmd := format('G', []);
  offset := length(cmd);
  setlength(cmd, offset+sz*2);
  for i := 0 to sz-1 do
  begin
    s := IntToHex(pb^, 2);
    cmd[offset + 2*i + 1] := s[1];
    cmd[offset + 2*i + 2] := s[2];
  end;

  // Normal receive error, or an error number of the form Exx
  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply(cmd, reply) and (reply = 'OK') and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: "G" command returned unexpected result: ', reply]);
end;

function TRspConnection.ReadData(const AAddress: TDbgPtr;
  const ASize: cardinal; out AData): boolean;
var
  buf: pbyte;
  cmd, reply: string;
  i: integer;
begin
  result := false;
  getmem(buf, ASize);
  cmd := 'm'+IntToHex(AAddress, 2)+',' + IntToHex(ASize, 2);
  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply(cmd, reply) and (length(reply) = ASize*2) and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if result then
  begin
    for i := 0 to ASize-1 do
      buf[i] := StrToInt('$'+reply[2*i + 1]+reply[2*i + 2])
  end
  else
  begin
    DebugLn(DBG_WARNINGS, ['Warning: "m" command returned unexpected result: ', reply]);
    FillByte(buf[0], ASize, 0);
  end;

  System.Move(buf^, AData, ASize);
  Freemem(buf);
end;

function TRspConnection.WriteData(const AAdress: TDbgPtr;
  const ASize: Cardinal; const AData): Boolean;
var
  cmd, reply, s: string;
  i, offset: integer;
  pb: PByte;
begin
  result := false;
  cmd := format('M%X,%X:', [AAdress, ASize]);
  offset := length(cmd);
  setlength(cmd, offset + 2*ASize);
  pb := @AData;
  for i := 0 to ASize-1 do
  begin
    s := IntToHex(pb^, 2);
    cmd[offset + 2*i+1] := s[1];
    cmd[offset + 2*i+2] := s[2];
    inc(pb);
  end;

  EnterCriticalSection(fCS);
  try
    result := SendCmdWaitForReply(cmd, reply) and (reply = 'OK') and not(SockErr);
  finally
    LeaveCriticalSection(fCS);
  end;
  if not result then
    DebugLn(DBG_WARNINGS, ['Warning: "M" command returned unexpected result: ', reply]);
end;

function TRspConnection.SendMonitorCmd(const s: string): boolean;
var
  cmdstr, reply: string;
begin
  cmdstr := 'qRcmd,' + HexEncodeStr(s);
  result := SendCmdWaitForReply(cmdstr, reply) and not(SockErr);

  if reply = '' then
    DebugLn(DBG_RSP, ['[Monitor '+s+'] : "qRcmd" not recognized by gdbserver.'])
  else
  begin
    // Check if reply is not hex encoded, else decode reply
    if Result and not((reply = 'OK') or ((length(reply) = 3) and (reply[1] = 'E'))) then
      reply := HexDecodeStr(reply);

    DebugLn(DBG_RSP, ['[Monitor '+s+'] reply: ', reply]);
  end;
end;

function TRspConnection.Init: integer;
var
  reply: string;
  intRegs: TInitializedRegisters;
  res: boolean;
  pSection: PDbgImageSection;
  dataStart: int64;
  reloadData: boolean = false;
  i: integer;
begin
  result := 0;
  reply := '';
  EnterCriticalSection(fCS);
  try
    if not SendCmdWaitForReply('vMustReplyEmpty', reply) or (reply <> '') or SockErr then
    begin
      DebugLn(DBG_WARNINGS, ['Warning: vMustReplyEmpty command returned unexpected result: ', reply]);
      exit;
    end;

    // Fancy stuff - load exe & sections, run monitor cmds etc
    if assigned(FConfig.AfterConnectMonitorCmds) and (FConfig.AfterConnectMonitorCmds.Count > 0) then
    begin
      for i := 0 to FConfig.AfterConnectMonitorCmds.Count-1 do
        SendMonitorCmd(FConfig.AfterConnectMonitorCmds[i]);
    end;

    // Start with AVR logic
    // If more targets are supported, move this to target specific debugger class
    if FConfig.UploadBinary and (FFileName <> '') then
    begin
      // Ensure loader is initialized
      if not Assigned(FOwner.DbgInfo) then
        FOwner.LoadInfo;
      datastart := -1;
      i := -1;
      repeat
        inc(i);
        pSection := FOwner.LoaderList[0].SectionByID[i];

        if (pSection <> nil) and (pSection^.Size > 0) and (pSection^.IsLoadable) then
        begin
          if Assigned(FConfig.SkipSectionsList) and
             (FConfig.SkipSectionsList.IndexOf(pSection^.Name) < 0) then
          begin
            // .data section should be programmed straight after .text for AVR
            // Require tracking because sections are sorted alphabetically,
            // so .data is encountered before .text
            if (pSection^.Name = '.data') then
            begin
              // Data can only be loaded after text, since the end address of text+1 is the start of data in flash
              if (dataStart < 0) then
              begin
                reloadData := true;
                system.Continue;
              end
              else
                WriteData(dataStart, pSection^.Size, pSection^.RawData^);
            end
            else
            begin
              WriteData(pSection^.VirtualAddress, pSection^.Size, pSection^.RawData^);
              if pSection^.Name = '.text' then
                dataStart := pSection^.Size;
            end;
          end;
        end;
      until (pSection = nil);

      // reloadData will only be set if it is not in the skipped sections list
      if reloadData and (dataStart >= 0) then
      begin
        pSection := FOwner.LoaderList[0].Section['.data'];
        WriteData(dataStart, pSection^.Size, pSection^.RawData^);
      end;
    end;

    // Hack to finish initializing atbackend agent
    if FConfig.AfterUploadBreakZero then
      SetBreakWatchPoint(0, wkpExec);  // Todo: check if different address is required

    if assigned(FConfig.AfterUploadMonitorCmds) and (FConfig.AfterUploadMonitorCmds.Count > 0) then
    begin
      for i := 0 to FConfig.AfterUploadMonitorCmds.Count-1 do
        SendMonitorCmd(FConfig.AfterUploadMonitorCmds[i]);
    end;

    // Must be last init command, after init the debug loop waits for the response in WaitForSignal
    res := SendCommandAck('?');
  finally
    LeaveCriticalSection(fCS);
  end;

  if res then
  begin
    // Already wrapped in critical section
    result := WaitForSignal(reply, intRegs);
  end;
end;

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

end.