forked from fpc/Lazarus
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfppascalbuilder.pas
More file actions
1524 lines (1379 loc) · 45.9 KB
/
Copy pathfppascalbuilder.pas
File metadata and controls
1524 lines (1379 loc) · 45.9 KB
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
unit FpPascalBuilder;
{$mode objfpc}{$H+}
{$IFDEF INLINE_OFF}{$INLINE OFF}{$ENDIF}
{$TYPEDADDRESS on}
interface
uses
Classes, SysUtils, Math, DbgIntfBaseTypes, DbgIntfDebuggerBase, FpDbgInfo,
FpdMemoryTools, FpErrorMessages, FpDbgDwarfDataClasses, FpDbgDwarf,
FpDbgClasses,
{$ifdef FORCE_LAZLOGGER_DUMMY} LazLoggerDummy {$else} LazLoggerBase {$endif},
LazUTF8, LazClasses, LazDebuggerIntfFloatTypes;
type
TDataDisplayFormat =
(ddfDefault,
ddfStructure,
ddfChar, ddfString,
ddfDecimal, ddfUnsigned, ddfHex, ddfBinary,
ddfPointer,
ddfMemDump
);
TTypeNameFlag = (
tnfOnlyDeclared, // do not return a substitute with ^ symbol
tnfNoSubstitute // do not return "{record}" if debug info has no type name
);
TTypeNameFlags = set of TTypeNameFlag;
TTypeDeclarationFlag = (
tdfNoFirstLineIndent,
tdfIncludeVarName, // like i: Integer
tdfSkipClassBody, // shorten class
tdfSkipRecordBody, // shorten class
tdfDynArrayWithPointer, // TODO, temp, act like gdb
tdfStopAfterPointer
);
TTypeDeclarationFlags = set of TTypeDeclarationFlag;
TFpPrettyPrintValueFlag = (
ppvCreateDbgType,
ppvSkipClassBody, ppvSkipRecordBody
);
TFpPrettyPrintValueFlags = set of TFpPrettyPrintValueFlag;
TFpPrettyPrintOption = (
ppoStackParam
);
TFpPrettyPrintOptions = set of TFpPrettyPrintOption;
const
PV_FORWARD_FLAGS = [ppvSkipClassBody, ppvSkipRecordBody];
type
PDBGType = ^TDBGType;
{ TFpPascalPrettyPrinter }
TFpPascalPrettyPrinter = class
private
FAddressSize: Integer;
FContext: TFpDbgLocationContext;
function InternalPrintValue(out APrintedValue: String;
AValue: TFpValue;
AnAddressSize: Integer;
AFlags: TFpPrettyPrintValueFlags;
ANestLevel: Integer; AnIndent: String;
ADisplayFormat: TDataDisplayFormat;
ARepeatCount: Integer = -1;
ADBGTypeInfo: PDBGType = nil;
AOptions: TFpPrettyPrintOptions = []
): Boolean;
public
constructor Create(AnAddressSize: Integer);
function PrintValue(out APrintedValue: String;
AValue: TFpValue;
ADisplayFormat: TDataDisplayFormat = ddfDefault;
ARepeatCount: Integer = -1;
AOptions: TFpPrettyPrintOptions = [];
AFlags: TFpPrettyPrintValueFlags = []
): Boolean;
function PrintValue(out APrintedValue: String;
out ADBGTypeInfo: TDBGType;
AValue: TFpValue;
ADisplayFormat: TDataDisplayFormat = ddfDefault;
ARepeatCount: Integer = -1
): Boolean;
property AddressSize: Integer read FAddressSize write FAddressSize;
property Context: TFpDbgLocationContext read FContext write FContext;
end;
function GetTypeName(out ATypeName: String; ADbgSymbol: TFpSymbol; AFlags: TTypeNameFlags = []): Boolean;
function GetTypeAsDeclaration(out ATypeDeclaration: String; ADbgSymbol: TFpSymbol;
AFlags: TTypeDeclarationFlags = []; AnIndent: Integer = 0): Boolean;
function GetParamsAsString(
AThread: TDbgThread;
ADbgCallStack: TDbgCallstackEntry;
AMemManager: TFpDbgMemManager;
ATargetWidth: Byte;
APrettyPrinter: TFpPascalPrettyPrinter): string;
function QuoteText(AText: Utf8String): UTf8String;
implementation
function GetParamsAsString(
AThread: TDbgThread;
ADbgCallStack: TDbgCallstackEntry;
AMemManager: TFpDbgMemManager;
ATargetWidth: Byte;
APrettyPrinter: TFpPascalPrettyPrinter): string;
var
ProcVal: TFpValue;
ProcSymbol: TFpSymbol;
AContext: TFpDbgSimpleLocationContext;
m: TFpValue;
v: String;
i: Integer;
begin
result := '';
ProcSymbol := ADbgCallStack.ProcSymbol;
if assigned(ProcSymbol) then begin
ProcVal := ProcSymbol.Value;
if (ProcVal <> nil) then begin
AContext := TFpDbgSimpleLocationContext.Create(AMemManager,
LocToAddrOrNil(TargetLoc(ADbgCallStack.AnAddress)), ATargetWidth div 8, AThread.ID, ADbgCallStack.Index);
if AContext <> nil then begin
TFpValueDwarf(ProcVal).Context := AContext;
APrettyPrinter.Context := AContext;
APrettyPrinter.AddressSize := AContext.SizeOfAddress;
for i := 0 to ProcVal.MemberCount - 1 do begin
m := ProcVal.Member[i];
if (m <> nil) and (sfParameter in m.DbgSymbol.Flags) then begin
APrettyPrinter.PrintValue(v, m, ddfDefault, -1, [ppoStackParam]);
if result <> '' then result := result + ', ';
result := result + v;
end;
m.ReleaseReference;
end;
TFpValueDwarf(ProcVal).Context := nil;
AContext.ReleaseReference;
end;
ProcVal.ReleaseReference;
end;
if result <> '' then
result := '(' + result + ')';
end;
end;
function GetTypeName(out ATypeName: String; ADbgSymbol: TFpSymbol;
AFlags: TTypeNameFlags): Boolean;
var
s: String;
sym: TFpSymbol;
begin
ATypeName := '';
Result := ADbgSymbol <> nil;
if not Result then
exit;
if (ADbgSymbol.SymbolType = stValue) then begin
ADbgSymbol := ADbgSymbol.TypeInfo;
Result := ADbgSymbol <> nil;
if not Result then
exit;
end;
ATypeName := ADbgSymbol.Name;
Result := ATypeName <> '';
if Result then
exit;
ATypeName := '';
sym := ADbgSymbol;
while (sym.Kind = skPointer) and (sym.TypeInfo <> nil) do begin
ATypeName := ATypeName + '^';
sym := sym.TypeInfo;
s := sym.Name;
if s <> '' then begin
ATypeName := ATypeName + s;
Result := True;
exit;
end;
end;
if tnfNoSubstitute in AFlags then
exit;
Result := True;
case ADbgSymbol.Kind of
skInstance: ATypeName := '{class}';
skProcedure: ATypeName := '{procedure}';
skFunction: ATypeName := '{function}';
skProcedureRef: ATypeName := '{procedure}';
skFunctionRef: ATypeName := '{function}';
skPointer: ATypeName := '{pointer}';
skInteger: ATypeName := '{signed int}';
skCardinal: ATypeName := '{unsigned int}';
skBoolean: ATypeName := '{boolean}';
skChar: ATypeName := '{char}';
skFloat: ATypeName := '{float}';
skString: ATypeName := '{string}';
skAnsiString: ATypeName := '{string}';
skCurrency: ATypeName := '{currency}';
skVariant: ATypeName := '{variant}';
skWideString: ATypeName := '{widestring}';
skEnum: ATypeName := '{enum}';
skSet: ATypeName := '{set}';
skRecord: ATypeName := '{record}';
skObject: ATypeName := '{object}';
skClass: ATypeName := '{class}';
skInterface: ATypeName := '{interface}';
else begin
ATypeName := '';
Result := False;
end;
end;
end;
function GetTypeAsDeclaration(out ATypeDeclaration: String; ADbgSymbol: TFpSymbol;
AFlags: TTypeDeclarationFlags; AnIndent: Integer): Boolean;
var
IndentString: String;
function GetIndent: String;
begin
if (IndentString = '') and (AnIndent > 0) then
IndentString := StringOfChar(' ', AnIndent);
Result := IndentString;
end;
function NeedBracket(S: String): Boolean;
var
i, l: Integer;
begin
l := 0;
i := length(s);
while (i > 0) do begin
case s[i] of
'a'..'z', 'A'..'Z', '0'..'9', '_', '$', '^': ;
'(': dec(l);
')': inc(l);
else
if l = 0 then break;
end;
dec(i);
end;
Result := i > 0;
end;
Function MembersAsGdbText(out AText: String; WithVisibilty: Boolean; ANewFlags: TTypeDeclarationFlags = []): Boolean;
var
CurVis: TDbgSymbolMemberVisibility;
procedure AddVisibility(AVis: TDbgSymbolMemberVisibility; AFirst: Boolean);
begin
if not (WithVisibilty and ((CurVis <> AVis) or AFirst)) then
exit;
CurVis := AVis;
case AVis of
svPrivate: AText := AText + GetIndent + ' private' + LineEnding;
svProtected: AText := AText + GetIndent + ' protected' + LineEnding;
svPublic: AText := AText + GetIndent + ' public' + LineEnding;
end;
end;
var
c, i: Integer;
m: TFpSymbol;
s: String;
r: Boolean;
begin
Result := True;
AText := '';
ANewFlags := ANewFlags + AFlags;
c := ADbgSymbol.NestedSymbolCount;
i := 0;
while (i < c) and Result do begin
m := ADbgSymbol.NestedSymbol[i];
AddVisibility(m.MemberVisibility, i= 0);
if tdfStopAfterPointer in ANewFlags then begin
r := GetTypeName(s, m);
s := m.Name + ': ' + s;
end
else
r := GetTypeAsDeclaration(s, m, [tdfIncludeVarName, tdfStopAfterPointer] + ANewFlags, AnIndent + 4);
if r then
AText := AText + GetIndent + s + ';' + LineEnding
else
AText := AText + m.Name + ': <unknown>;' + LineEnding;
inc(i);
end;
end;
function GetPointerType(out ADeclaration: String): Boolean;
var
s: String;
begin
s := '';
while (ADbgSymbol.Kind = skPointer) and (ADbgSymbol.TypeInfo <> nil) do begin
ADbgSymbol := ADbgSymbol.TypeInfo;
s := s + '^';
end;
if (tdfStopAfterPointer in AFlags) then begin
Result := GetTypeName(ADeclaration, ADbgSymbol, []);
end
else begin
Result := GetTypeAsDeclaration(ADeclaration, ADbgSymbol, AFlags + [tdfStopAfterPointer]);
if not Result then
Result := GetTypeName(ADeclaration, ADbgSymbol, []);
end;
if NeedBracket(ADeclaration)
then ADeclaration := s + '(' + ADeclaration + ')'
else ADeclaration := s + ADeclaration;
end;
function GetBaseType(out ADeclaration: String): Boolean;
var
s1, s2: String;
hb, lb: Int64;
begin
if sfSubRange in ADbgSymbol.Flags then begin
case ADbgSymbol.Kind of
// TODO: check bound are in size
skInteger: begin
Result := ADbgSymbol.GetValueBounds(nil, lb, hb);
if Result then ADeclaration := Format('%d..%d', [lb, hb]);
end;
skCardinal: begin
Result := ADbgSymbol.GetValueBounds(nil, lb, hb);
if Result then ADeclaration := Format('%u..%u', [QWord(lb), QWord(hb)]);
end;
skChar: begin
Result := ADbgSymbol.GetValueBounds(nil, lb, hb);
if (lb >= 32) and (hb <= 126)
then s1 := '''' + chr(lb) + ''''
else s1 := '#'+IntToStr(lb);
if (hb >= 32) and (hb <= 126)
then s2 := '''' + chr(hb) + ''''
else s2 := '#'+IntToStr(hb);
if Result then ADeclaration := Format('%s..%s', [s1, s2]);
end;
else
Result := False; // not sure how to show a subrange of skFloat, skBoolean, :
end;
end
else
Result := GetTypeName(ADeclaration, ADbgSymbol, []);
end;
function GetParameterList(out ADeclaration: String): Boolean;
var
i: Integer;
m: TFpSymbol;
name, lname: String;
begin
ADeclaration := '';
lname := '';
for i := 0 to ADbgSymbol.NestedSymbolCount - 1 do begin
m := ADbgSymbol.NestedSymbol[i];
if (m <> nil) and (sfParameter in m.Flags) then begin
GetTypeName(name, m, [tnfOnlyDeclared]);
if (lname <> '') then begin
if (lname = name) then
ADeclaration := ADeclaration + ', '
else
ADeclaration := ADeclaration + ': ' + lname + '; ';
end
else
if ADeclaration <> '' then
ADeclaration := ADeclaration + '; ';
ADeclaration := ADeclaration + m.Name;
lname := name;
end;
end;
if (lname <> '') then
ADeclaration := ADeclaration + ': ' + lname;
Result := True;
end;
function GetFunctionType(out ADeclaration: String): Boolean;
var
s, p: String;
begin
GetTypeAsDeclaration(s, ADbgSymbol.TypeInfo, AFlags);
GetParameterList(p);
ADeclaration := 'function ' + '(' + p + '): ' + s + '';
if sfVirtual in ADbgSymbol.Flags then ADeclaration := ADeclaration + '; virtual';
Result := true;
end;
function GetProcedureType(out ADeclaration: String): Boolean;
var
p: String;
begin
GetParameterList(p);
ADeclaration := 'procedure ' + '(' + p + ')';
if sfVirtual in ADbgSymbol.Flags then ADeclaration := ADeclaration + '; virtual';
Result := true;
end;
function GetClassType(out ADeclaration: String): Boolean;
var
s, s2: String;
begin
Result := tdfSkipClassBody in AFlags;
if Result then begin
GetTypeName(s, ADbgSymbol);
ADeclaration := s + ' {=class}';
exit;
end;
Result := MembersAsGdbText(s, True, [tdfSkipClassBody]);
if not GetTypeName(s2, ADbgSymbol.TypeInfo) then
s2 := '';
if Result then
ADeclaration := Format('class(%s)%s%s%send',
[s2, LineEnding, s, GetIndent]);
end;
function GetRecordType(out ADeclaration: String): Boolean;
var
s: String;
begin
if tdfSkipRecordBody in AFlags then begin
Result := True;
if GetTypeName(s, ADbgSymbol) then
ADeclaration := s + ' {=record}'
else
ADeclaration := Format('record {...};%s%send', [LineEnding, GetIndent]);
exit;
end;
Result := MembersAsGdbText(s, False);
if Result then
ADeclaration := Format('record%s%s%send', [LineEnding, s, GetIndent]);
end;
function GetObjectType(out ADeclaration: String): Boolean;
var
s: String;
begin
if tdfSkipRecordBody in AFlags then begin
Result := True;
if GetTypeName(s, ADbgSymbol) then
ADeclaration := s + ' {=object}'
else
ADeclaration := Format('object {...};%s%send', [LineEnding, GetIndent]);
exit;
end;
Result := MembersAsGdbText(s, False);
if Result then
ADeclaration := Format('object%s%s%send', [LineEnding, s, GetIndent]);
end;
function GetEnumType(out ADeclaration: String): Boolean;
var
i, j, val: Integer;
m: TFpSymbol;
begin
// TODO assigned value (a,b:=3,...)
Result := True;
ADeclaration := '(';
j := 0;
for i := 0 to ADbgSymbol.NestedSymbolCount - 1 do begin
m := ADbgSymbol.NestedSymbol[i];
if i > 0 then ADeclaration := ADeclaration + ', ';
ADeclaration := ADeclaration + m.Name;
if m.HasOrdinalValue then begin
val := m.OrdinalValue;
if j <> val then begin
ADeclaration := ADeclaration + ' := ' + IntToStr(val);
j := val;
continue;
end;
end;
inc(j);
end;
ADeclaration := ADeclaration + ')'
end;
function GetSetType(out ADeclaration: String): Boolean;
var
t: TFpSymbol;
s: String;
lb, hb: Int64;
begin
// TODO assigned value (a,b:=3,...)
t := ADbgSymbol.TypeInfo;
Result := t <> nil;
if not Result then exit;
case t.Kind of
skInteger: begin
Result := t.GetValueBounds(nil, lb, hb);
ADeclaration := format('set of %d..%d', [lb, hb]);
end;
skCardinal: begin
Result := t.GetValueBounds(nil, lb, hb);
ADeclaration := format('set of %u..%u', [QWord(lb), QWord(hb)]);
end;
skEnum: begin
if t.Name <> '' then begin
Result := True;
s := t.Name;
end
else
Result := GetTypeAsDeclaration(s, t, AFlags);
ADeclaration := 'set of ' + s;
end;
else
Result := False;
end;
end;
function GetArrayType(out ADeclaration: String): Boolean;
var
t: TFpSymbol;
s: String;
i: Integer;
lb, hb: Int64;
begin
// TODO assigned value (a,b:=3,...)
t := ADbgSymbol.TypeInfo;
Result := (t <> nil);
if not Result then exit;
s := t.Name;
if s = '' then begin
if tdfStopAfterPointer in AFlags then
Result := GetTypeName(s, t)
else
Result := GetTypeAsDeclaration(s, t, [tdfNoFirstLineIndent, tdfStopAfterPointer] + AFlags, AnIndent + 4); // no class ?
if not Result then exit;
end;
if sfDynArray in ADbgSymbol.Flags then begin //supprts only one level
ADeclaration := 'array of ' + s;
if tdfDynArrayWithPointer in AFlags then
ADeclaration := '^(' + ADeclaration + ')';
end
else begin
ADeclaration := 'array [';
for i := 0 to ADbgSymbol.NestedSymbolCount - 1 do begin
if i > 0 then
ADeclaration := ADeclaration + ', ';
t := ADbgSymbol.NestedSymbol[i];
t.GetValueBounds(nil, lb, hb);
if t.Kind = skCardinal
then ADeclaration := ADeclaration + Format('%u..%u', [QWord(lb), QWord(hb)])
else ADeclaration := ADeclaration + Format('%d..%d', [lb, hb]);
end;
ADeclaration := ADeclaration + '] of ' + s;
end;
end;
var
VarName: String;
begin
Result := ADbgSymbol <> nil;
if not Result then
exit;
VarName := '';
if (ADbgSymbol.SymbolType = stValue) then begin
if tdfIncludeVarName in AFlags then
VarName := ADbgSymbol.Name;
ADbgSymbol := ADbgSymbol.TypeInfo;
Result := ADbgSymbol <> nil;
if not Result then
exit;
end;
case ADbgSymbol.Kind of
skPointer: Result := GetPointerType(ATypeDeclaration);
skInteger, skCardinal, skBoolean, skChar, skFloat:
Result := GetBaseType(ATypeDeclaration);
skFunction, skFunctionRef: Result := GetFunctionType(ATypeDeclaration);
skProcedure, skProcedureRef: Result := GetProcedureType(ATypeDeclaration);
skClass: Result := GetClassType(ATypeDeclaration);
skRecord: Result := GetRecordType(ATypeDeclaration);
skObject: Result := GetObjectType(ATypeDeclaration);
//skInterface: Result := GetInterfaceType(ATypeDeclaration);
skEnum: Result := GetEnumType(ATypeDeclaration);
skset: Result := GetSetType(ATypeDeclaration);
skArray: Result := GetArrayType(ATypeDeclaration);
skNone: ATypeDeclaration := '<unknown>';
else Result := GetBaseType(ATypeDeclaration);
end;
if VarName <> '' then
ATypeDeclaration := VarName + ': ' + ATypeDeclaration;
if (AnIndent <> 0) and not(tdfNoFirstLineIndent in AFlags) then
ATypeDeclaration := GetIndent + ATypeDeclaration;
end;
function QuoteWideText(AText: WideString): WideString;
const
HEXCHR: array [0..15] of char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
var
Len: Integer;
c: WideChar;
RPos, SPos, SEnd, QPos: PWideChar;
begin
if AText = '' then
exit('''''');
Len := Length(AText);
SetLength(Result, Len * 4); // This is the maximal length result can get
RPos := @Result[1];
SPos := @AText[1];
SEnd := @AText[Len] + 1;
repeat
RPos^ := ''''; inc(RPos);
QPos := RPos;
repeat
c := SPos^;
case c of
#0..#31, #127, #$80..#$9F:
break;
'''': begin
RPos^ := c; inc(RPos);
RPos^ := c; inc(RPos);
inc(SPos);
end;
else begin
RPos^ := c; inc(RPos);
inc(SPos);
end;
end;
c := SPos^;
until False;;
if RPos = QPos then
dec(RPos)
else begin
RPos^ := ''''; inc(RPos);
end;
repeat
c := SPos^;
if (c = #0) and (SPos >= SEnd) then begin
// END OF TEXT
Assert(RPos-1 <= @Result[Length(Result)], 'RPos-1 <= @Result[Length(Result)]');
SetLength(Result, RPos - PWideChar(@Result[1]));
exit;
end;
RPos^ := '#'; inc(RPos);
RPos^ := '$'; inc(RPos);
RPos^ := HEXCHR[Byte(c) >> 4]; inc(RPos);
RPos^ := HEXCHR[Byte(c) and 15]; inc(RPos);
inc(SPos);
c := SPos^;
until not(c in [#0..#31, #127, #$80..#$9F]);
until False;
end;
function QuoteText(AText: Utf8String): UTf8String;
// TODO: process large text in chunks to avoid allocating huge memory
const
HEXCHR: array [0..15] of char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
var
Len: Integer;
c: Char;
RPos, SPos, SEnd, QPos: PChar;
begin
if AText = '' then
exit('''''');
Len := Length(AText);
SetLength(Result, Len * 4); // This is the maximal length result can get
RPos := @Result[1];
SPos := @AText[1];
SEnd := @AText[Len] + 1;
repeat
RPos^ := ''''; inc(RPos);
QPos := RPos;
repeat
c := SPos^;
case c of
'''': begin
RPos^ := c; inc(RPos);
RPos^ := c; inc(RPos);
inc(SPos);
end;
#32..Pred(''''), Succ('''')..#126: begin
RPos^ := c; inc(RPos);
inc(SPos);
end;
#192..#223: begin
if ((Byte(SPos[1]) and $C0) <> $80)
then
break; // invalid utf8 -> escape
RPos^ := c; inc(RPos);
RPos^ := SPos[1]; inc(RPos);
inc(SPos, 2);
end;
#224..#239: begin
if ((Byte(SPos[1]) and $C0) <> $80) or ((Byte(SPos[2]) and $C0) <> $80)
then
break; // invalid utf8 -> escape
RPos^ := c; inc(RPos);
RPos^ := SPos[1]; inc(RPos);
RPos^ := SPos[2]; inc(RPos);
inc(SPos, 3);
end;
#240..#247: begin
if ((Byte(SPos[1]) and $C0) <> $80) or ((Byte(SPos[2]) and $C0) <> $80) or
((Byte(SPos[3]) and $C0) <> $80)
then
break; // invalid utf8 -> escape
RPos^ := c; inc(RPos);
RPos^ := SPos[1]; inc(RPos);
RPos^ := SPos[2]; inc(RPos);
RPos^ := SPos[3]; inc(RPos);
inc(SPos, 4);
end;
#0: begin
if (SPos < SEnd) then
break; // need escaping
// END OF TEXT
RPos^ := ''''; inc(RPos);
Assert(RPos-1 <= @Result[Length(Result)], 'RPos-1 <= @Result[Length(Result)]');
SetLength(Result, RPos - @Result[1]);
exit;
end;
else
break; // need escaping
end;
c := SPos^;
until False;
if RPos = QPos then
dec(RPos)
else begin
RPos^ := ''''; inc(RPos);
end;
repeat
c := SPos^;
if (c = #0) and (SPos >= SEnd) then begin
// END OF TEXT
Assert(RPos-1 <= @Result[Length(Result)], 'RPos-1 <= @Result[Length(Result)]');
SetLength(Result, RPos - @Result[1]);
exit;
end;
RPos^ := '#'; inc(RPos);
RPos^ := '$'; inc(RPos);
RPos^ := HEXCHR[Byte(c) >> 4]; inc(RPos);
RPos^ := HEXCHR[Byte(c) and 15]; inc(RPos);
inc(SPos);
c := SPos^;
until not(c in [#0..#31, #127, #$80..#$BF]);
until False;
end;
{ TFpPascalPrettyPrinter }
function TFpPascalPrettyPrinter.InternalPrintValue(out APrintedValue: String;
AValue: TFpValue; AnAddressSize: Integer; AFlags: TFpPrettyPrintValueFlags;
ANestLevel: Integer; AnIndent: String; ADisplayFormat: TDataDisplayFormat;
ARepeatCount: Integer; ADBGTypeInfo: PDBGType; AOptions: TFpPrettyPrintOptions): Boolean;
function ResTypeName: String;
begin
if not((AValue.TypeInfo<> nil) and
GetTypeName(Result, AValue.TypeInfo, []))
then
Result := '';
end;
function ResTypeName(AVal : TFpValue): String;
begin
if not((AVal.TypeInfo<> nil) and
GetTypeName(Result, AVal.TypeInfo, []))
then
Result := '';
end;
procedure DoPointer(AnAddress: boolean);
var
s: String;
v: QWord;
m: TFpValue;
begin
if ((ADisplayFormat = ddfDefault) and (ANestLevel=0)) or // default for unested: with typename
(ADisplayFormat = ddfStructure)
then
s := ResTypeName
else
s := '';
if AnAddress then
v := AValue.Address.Address
else
v := AValue.AsCardinal;
if (ppvCreateDbgType in AFlags) then begin
ADBGTypeInfo^ := TDBGType.Create(skPointer, s);
ADBGTypeInfo^.Value.AsPointer := Pointer(v); // TODO: no cut off
end;
case ADisplayFormat of
ddfDecimal, ddfUnsigned: APrintedValue := IntToStr(v);
ddfHex: APrintedValue := '$'+IntToHex(v, AnAddressSize*2);
else begin //ddfPointer/Default ;
if v = 0 then
APrintedValue := 'nil'
else
APrintedValue := '$'+IntToHex(v, AnAddressSize*2);
end;
end;
if ADisplayFormat = ddfPointer then begin
if s <> '' then
APrintedValue := s + '(' + APrintedValue + ')';
exit; // no data
end;
(* In Dwarf 2 Strings are Pchar => do not add the typename.
TODO: In Dwarf 3 this should be true pchar, maybe add typename?
*)
if svfString in AValue.FieldFlags then begin
if v <> 0 then
APrintedValue := APrintedValue + '^: ' + QuoteText(AValue.AsString);
end
else
if svfWideString in AValue.FieldFlags then begin
if v <> 0 then
APrintedValue := APrintedValue + '^: ' + QuoteWideText(AValue.AsWideString)
end
else begin
m := AValue.Member[0];
if (m<>nil) and (svfString in m.FieldFlags) then
APrintedValue := APrintedValue + '^: ' + QuoteText(m.AsString)
else
if (m<>nil) and (svfWideString in m.FieldFlags) then
APrintedValue := APrintedValue + '^: ' + QuoteWideText(m.AsWideString)
else
if s <> '' then
APrintedValue := s + '(' + APrintedValue + ')'; // no typeinfo for strings/pchar
m.ReleaseReference;
end;
Result := True;
end;
procedure DoUnknown;
begin
APrintedValue := 'Unknown type';
if svfAddress in AValue.FieldFlags then
APrintedValue := APrintedValue + '($'+IntToHex(AValue.Address.Address, AnAddressSize*2) + ')'
else
if svfDataAddress in AValue.FieldFlags then
APrintedValue := APrintedValue + '($'+IntToHex(AValue.DataAddress.Address, AnAddressSize*2) + ')';
Result := True;
end;
procedure DoType;
begin
// maybe include tdfIncludeVarName for structure members "TFooClass.SomeField"
if GetTypeAsDeclaration(APrintedValue, AValue.DbgSymbol) then
APrintedValue := 'type ' + APrintedValue
else
DoUnknown;
Result := True;
end;
procedure DoFunction;
var
s: String;
proc, procref: TFpSymbolDwarf;
t, sym: TFpSymbol;
par: TFpValueDwarf;
v: TFpDbgMemLocation;
va: TDBGPtr;
begin
proc := nil;
procref := nil;
v := AValue.DataAddress;
va := v.Address;
if (ppvCreateDbgType in AFlags) then begin
ADBGTypeInfo^ := TDBGType.Create(AValue.Kind, '');
if AValue.Kind in [skFunctionRef, skProcedureRef] then
ADBGTypeInfo^.Value.AsPointer := Pointer(va); // TODO: no cut off
end;
// TODO: depending on verbosity: TypeName($0123456)
if AValue.Kind in [skFunctionRef, skProcedureRef] then begin
if va = 0 then
APrintedValue := 'nil'
else
APrintedValue := '$'+IntToHex(va, AnAddressSize*2);
t := AValue.TypeInfo;
sym := AValue.DbgSymbol;
if (sym is TFpSymbolDwarfDataProc) then begin
proc := TFpSymbolDwarf(sym);
end
else begin
proc := TFpSymbolDwarf(TDbgDwarfSymbolBase(t).CompilationUnit.Owner.FindProcSymbol(va));
procref := proc;
end;
if proc <> nil then begin
s := proc.Name;
if (proc is TFpSymbolDwarfDataProc) then begin
par := TFpSymbolDwarfDataProc(proc).GetSelfParameter; // Has no Context set, but we only need TypeInfo.Name
if (par <> nil) and (par.TypeInfo <> nil) then
s := par.TypeInfo.Name + '.' + s;
par.ReleaseReference;
end;
APrintedValue := APrintedValue + ' = ' + s; // TODO: offset to startaddress
end;
APrintedValue := APrintedValue + ': ';
end
else
t := AValue.DbgSymbol;
if (t is TFpSymbolDwarfDataProperty) and
(AValue is TFpValueDwarfProperty) and
(TFpValueDwarfProperty(AValue).GetterValue <> nil)
then
t := TFpValueDwarfProperty(AValue).GetterValue.DbgSymbol;
if AFlags * PV_FORWARD_FLAGS <> [] then
GetTypeName(s, t)
else
GetTypeAsDeclaration(s, t);
APrintedValue := APrintedValue + s;
if (AValue.Kind in [skFunction, skProcedure]) and Context.MemModel.IsReadableLocation(v) then begin
APrintedValue := APrintedValue + ' AT ' + '$'+IntToHex(va, AnAddressSize*2);
end;
ReleaseRefAndNil(procref);
Result := True;
end;
procedure DoInt;
var
n: Integer;
ValSize: TFpDbgValueSize;
begin
case ADisplayFormat of
ddfUnsigned: APrintedValue := IntToStr(QWord(AValue.AsInteger));
ddfHex: begin
if (svfSize in AValue.FieldFlags) and AValue.GetSize(ValSize) then
n := SizeToFullBytes(ValSize)* 2
else begin
n := 16;
if QWord(AValue.AsInteger) <= high(Cardinal) then n := 8;
if QWord(AValue.AsInteger) <= high(Word) then n := 3;
if QWord(AValue.AsInteger) <= high(Byte) then n := 2;
end;
APrintedValue := '$'+IntToHex(QWord(AValue.AsInteger), n);
end;
// TODO ddfChar:
else
// 128 bit values only fit in the string representation
if AValue.GetSize(ValSize) and (SizeToFullBytes(ValSize) > SizeOf(QWord)) then
APrintedValue := AValue.AsString
else
APrintedValue := IntToStr(AValue.AsInteger);
end;
if (ppvCreateDbgType in AFlags) then begin
ADBGTypeInfo^ := TDBGType.Create(skSimple, ResTypeName);
//ADBGTypeInfo^.Value.As64Bits := QWord(AValue.AsInteger); // TODO: no cut off
end;
Result := True;
end;
procedure DoCardinal;
var
n: Integer;
ValSize: TFpDbgValueSize;
begin
case ADisplayFormat of