File: fpdbgcallcontextinfo.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 (725 lines) | stat: -rw-r--r-- 22,276 bytes parent folder | download
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
unit FpDbgCallContextInfo;

{$mode objfpc}{$H+}
{$IFDEF INLINE_OFF}{$INLINE OFF}{$ENDIF}
interface

uses
  DbgIntfBaseTypes, math,
  FpDbgInfo,
  FpdMemoryTools,
  FpDbgDwarfDataClasses,
  FpDbgDwarf,
  FpDbgClasses, FpErrorMessages, FpDbgUtil, {$ifdef FORCE_LAZLOGGER_DUMMY} LazLoggerDummy {$else} LazLoggerBase {$endif}, LazClasses;

type

  { TFpSymbolCallParamOrdinalOrPointer }

  TFpSymbolCallParamOrdinalOrPointer = class(TFpSymbolDwarfTypeBasic) // act as pointer
  private type
    { TFpValueCallParamStringByRef }

    TFpValueCallParamStringByRef = class(TFpValueDwarfPointer)
      function GetDwarfDataAddress(out AnAddress: TFpDbgMemLocation;
        ATargetType: TFpSymbolDwarfType = nil): Boolean; override;
    end;

  protected
    procedure KindNeeded; override;
    procedure Init; override;
  public
    constructor Create(AName: String; AStringVarAddress: TDBGPtr);
    function GetTypedValueObject(ATypeCast: Boolean; AnOuterType: TFpSymbolDwarfType = nil): TFpValueDwarf; override;
  end;

  { TFpSymbolDwarfFunctionResult }

  TFpSymbolDwarfFunctionResult = class(TFpSymbolDwarfDataWithLocation)
  protected
    function GetValueAddress(AValueObj: TFpValueDwarf; out AnAddress: TFpDbgMemLocation): Boolean; override;
    procedure Init; override;
  public
    constructor Create(const AName: String;
                       const AAddress: TFpDbgMemLocation;
                       ATypeSymbol: TFpSymbol); overload;
    property NewAddress: TFpDbgMemLocation write SetAddress;
  end;

  { TFpDbgInfoCallContext }

  TOnCallRoutineHitBreapoint = procedure(AnAddress: TDBGPtr; out ACanContinue: Boolean) of object;

  TFpDbgInfoCallContext = class(TFpDbgAbstractCallContext)
  private
    FDbgProcess: TDbgProcess;
    FDbgThread: TDbgThread;
    FLastError: TFpError;
    FNextParamRegister: Integer;
    FOnCallRoutineHitBreapoint: TOnCallRoutineHitBreapoint;
    FOrigStackPtr: TDBGPtr;
    FPreparedStack: Array of Byte;
    FNeedStringResInFinalize: Boolean;
    FStringResultMem: TDBGPtr;

    function AllocStack(ASize: Integer): TDbgPtr;
    function CreatePreparedStackLocation(ASize: Integer): TFpDbgMemLocation;
    function InternalGetLocation(AParamIndex: Integer; ASize: Integer = 0): TFpDbgMemLocation;
    function InternalCreateParamSymbol(ParameterMemLocation: TFpDbgMemLocation; ASymbolType: TFpSymbol; AName: String): TFpValue;
    function InternalCreateParamSymbol(AParamIndex: Integer; ASymbolType: TFpSymbol; AName: String): TFpValue; inline;
    function AddRecordParam(AParamSymbolType: TFpSymbol; AValue: TFpValue): Boolean;
    function InternalAddStringResult: Boolean;
  public
    constructor Create(const ABaseContext: TFpDbgLocationContext;
      AMemReader: TFpDbgMemReaderBase;
      AMemModel: TFpDbgMemModel;
      AMemConverter: TFpDbgMemConvertor;
      ADbgProcess: TDbgProcess;
      ADbgThread: TDbgThread);
    destructor Destroy; override;
    function WriteStack: Boolean; // called by controller-command

    function CreateParamSymbol(AParamIndex: Integer; ASymbolType: TFpSymbol; AName: String = ''): TFpValue; virtual;

    function AddParam(AParamSymbolType: TFpSymbol; AValue: TFpValue): Boolean;
    function AddOrdinalParam(AParamSymbolType: TFpSymbol; AValue: QWord): Boolean;
    function AddOrdinalParam(AValue: QWord): Boolean; inline;
    (* AddOrdinalViaRefAsParam
       TODO: need size of the ordinal -- currently using SizeOfAddr in target
    *)
    function AddOrdinalViaRefAsParam(AValue: QWord): Boolean; inline;  // For string dec-ref
    function AddOrdinalViaRefAsParam(AValue: QWord; out ATargetParamAddr: TDBGPtr): Boolean;
    (* AddStringResult:
       Must be called before any AddParam.
       Except for "Self": In case of a method, AddParm(self) must be set before the StringResult
     *)
    function AddStringResult: Boolean;
    function FinalizeParams: Boolean;

    // The caller must take care to call DecRef for the result
    function GetStringResultAsPointer(out AStringAsPtr: TDbgPtr): Boolean;
    function GetStringResult(out AVal: TFpValue; AStringSymbolType: TFpSymbol = nil): Boolean;
    function GetWideStringResult(out AVal: TFpValue; AStringSymbolType: TFpSymbol = nil): Boolean;

    property LastError: TFpError read FLastError;
    property OnCallRoutineHitBreapoint: TOnCallRoutineHitBreapoint read FOnCallRoutineHitBreapoint write FOnCallRoutineHitBreapoint;
  end;

implementation

var
  FPDBG_FUNCCALL: PLazLoggerLogGroup;

{ TFpSymbolCallParamOrdinalOrPointer.TFpValueCallParamStringByRef }

function TFpSymbolCallParamOrdinalOrPointer.TFpValueCallParamStringByRef.GetDwarfDataAddress
  (out AnAddress: TFpDbgMemLocation; ATargetType: TFpSymbolDwarfType): Boolean;
begin
  AnAddress := Address;
  Result := MemManager.MemModel.IsReadableLocation(AnAddress);
end;

{ TFpSymbolCallParamOrdinalOrPointer }

procedure TFpSymbolCallParamOrdinalOrPointer.KindNeeded;
begin
  SetKind(skPointer);
end;

procedure TFpSymbolCallParamOrdinalOrPointer.Init;
begin
  inherited Init;
  EvaluatedFields := EvaluatedFields + [sfiAddress];
end;

function TFpSymbolCallParamOrdinalOrPointer.GetTypedValueObject(ATypeCast: Boolean;
  AnOuterType: TFpSymbolDwarfType): TFpValueDwarf;
begin
  Result := TFpValueCallParamStringByRef.Create(AnOuterType);
end;

constructor TFpSymbolCallParamOrdinalOrPointer.Create(AName: String;
  AStringVarAddress: TDBGPtr);
begin
  inherited Create(AName, skPointer, TargetLoc(AStringVarAddress));
  SetTypeInfo(TFpSymbolDwarfTypePointer.Create(AName));
  TypeInfo.ReleaseReference;
  Init;
end;

{ TFpSymbolDwarfFunctionResult }

function TFpSymbolDwarfFunctionResult.GetValueAddress(AValueObj: TFpValueDwarf; out AnAddress: TFpDbgMemLocation): Boolean;
begin
  AnAddress := Address;
  Result := IsInitializedLoc(AnAddress);
end;

procedure TFpSymbolDwarfFunctionResult.Init;
begin
  inherited Init;
  EvaluatedFields := EvaluatedFields + [sfiAddress];
end;

constructor TFpSymbolDwarfFunctionResult.Create(const AName: String;
  const AAddress: TFpDbgMemLocation; ATypeSymbol: TFpSymbol);
begin
  inherited Create(AName, ATypeSymbol.Kind, AAddress); // TODO: intercept at TDbgDwarfSymbolBase and call Init?
  SetTypeInfo(ATypeSymbol);
  Init;
end;

{ TFpDbgInfoCallContext }

function TFpDbgInfoCallContext.AllocStack(ASize: Integer): TDbgPtr;
begin
  Result := FDbgThread.GetStackPointerRegisterValue;
  if FOrigStackPtr = 0 then
    FOrigStackPtr := Result;

  {$IF defined(WINDOWS)}
  if FDbgProcess.Mode = dm64 then begin
    ASize := (ASize + 32) and not(31); // keep aligned to 32 bytes
  end
  else
  {$ENDIF}
  ASize := (ASize + 8) and not(7); // keep aligned to 8 bytes

  dec(Result, ASize);
  FDbgThread.SetStackPointerRegisterValue(Result);
end;

function TFpDbgInfoCallContext.CreatePreparedStackLocation(ASize: Integer
  ): TFpDbgMemLocation;
var
  l: SizeInt;
begin
  l := Length(FPreparedStack);
  SetLength(FPreparedStack, l + ASize);

  if FDbgProcess.Mode = dm32 then begin
    if l > 0 then
      move(FPreparedStack[0], FPreparedStack[ASize], SizeOf(FPreparedStack[0]) * l);
    l := 0;
  end;

  Result := SelfLoc(@FPreparedStack[l]);
end;

function TFpDbgInfoCallContext.InternalGetLocation(AParamIndex: Integer;
  ASize: Integer): TFpDbgMemLocation;
begin
  if (FDbgProcess.Mode = dm32) and (ASize = 8) then begin
    Result := CreatePreparedStackLocation(ASize);
    exit;
  end;

  Result := FDbgProcess.CallParamDefaultLocation(AParamIndex);
  if IsValidLoc(Result) then
    exit;

  if ASize = 0 then
    ASize := SizeOfAddress;
  Result := CreatePreparedStackLocation(ASize);
end;

function TFpDbgInfoCallContext.InternalCreateParamSymbol(
  ParameterMemLocation: TFpDbgMemLocation; ASymbolType: TFpSymbol; AName: String
  ): TFpValue;
var
  ParamSymbol: TFpSymbol;// TFpSymbolDwarfFunctionResult;
begin
  Result := nil;
  if not IsValidLoc(ParameterMemLocation) then begin
    FLastError := CreateError(fpErrAnyError, ['Too many params']);
    exit;
  end;
  ParamSymbol := TFpSymbolDwarfFunctionResult.Create(AName, ParameterMemLocation, ASymbolType);
  try
    Result := ParamSymbol.Value;
  finally
    ParamSymbol.ReleaseReference;
  end;
  TFpValueDwarf(Result).Context := Self;
end;

function TFpDbgInfoCallContext.InternalCreateParamSymbol(AParamIndex: Integer;
  ASymbolType: TFpSymbol; AName: String): TFpValue;
begin
  Result := InternalCreateParamSymbol(InternalGetLocation(AParamIndex), ASymbolType, AName);
end;

function TFpDbgInfoCallContext.AddRecordParam(AParamSymbolType: TFpSymbol;
  AValue: TFpValue): Boolean;
// Only intel/amd
  function ReadRecFromMem(Addr: TDbgPtr; sz: Integer; out Data: QWord): Boolean;
  var
    d: QWord;
  begin
    Result := FDbgProcess.ReadData(addr, sz, d);
    if Result then begin
      Data := 0;
      Move(d, Data, sz);
    end
    else begin
      FLastError := CreateError(fpErrAnyError, ['failed to read record data from memory']);
    end;
  end;

  function HasUnalignedFields: Boolean;
  var
    i: Integer;
    FldLoc: TFpDbgMemLocation;
    FldSize: TFpDbgValueSize;
    RecAddr, FldOffs: TDBGPtr;
    m: TFpValue;
  begin
    Result := False;
    RecAddr := AValue.Address.Address;
    m := nil;
    for i := 0 to AValue.MemberCount - 1 do begin
      m.ReleaseReference;
      m := AValue.Member[i];
      FldLoc := m.Address;
      Result := FldLoc.BitOffset <> 0;
      if Result then
        break;
      FldOffs := FldLoc.Address - RecAddr;
      FldSize := m.DataSize;
      Result := FldSize.BitSize <> 0;
      if Result then
        break;
      Result := FldOffs mod FldSize.Size <> 0;
      if Result then
        break;
    end;
    m.ReleaseReference;
  end;

var
  ParamSymbol: TFpValue;

  procedure InitParamSymbol;
  begin
    ParamSymbol := InternalCreateParamSymbol(FNextParamRegister, AParamSymbolType, '');
    inc(FNextParamRegister);
    if Length(FPreparedStack) > 0 then
      MemManager.SetWritableSeflMem(TDBGPtr(@FPreparedStack[0]), Length(FPreparedStack));
  end;

var
  {$If defined(UNIX)}
  i: Integer;
  {$ENDIF}
  RecSize: Integer;
  RecLoc: TFpDbgMemLocation;
  RecAddr, RecData: TDBGPtr;
  l: SizeInt;
begin
  ParamSymbol := nil;
  try
    RecSize := SizeToFullBytes(AValue.DataSize);
    Result := not IsError(AValue.LastError);
    if Result then begin
      RecLoc := AValue.Address;
      Result := IsValidLoc(RecLoc);
      RecAddr := RecLoc.Address;
    end;
    if not Result then begin
      FLastError := AValue.LastError;
      exit;
    end;

    {$IF defined(WINDOWS)}
    if FDbgProcess.Mode = dm32 then begin
      if (RecSize <= FDbgProcess.PointerSize) then begin
        Result := ReadRecFromMem(RecAddr, RecSize, RecData);
        if not Result then
          exit;

        l := Length(FPreparedStack);
        SetLength(FPreparedStack, l + 4);
        if l > 0 then
          move(FPreparedStack[0], FPreparedStack[4], SizeOf(FPreparedStack[0]) * l);
        PDWord(@FPreparedStack[0])^ := RecData;

        exit;
      end;

      InitParamSymbol;
      ParamSymbol.AsCardinal := RecAddr;
      Result := not IsError(ParamSymbol.LastError);
      FLastError := ParamSymbol.LastError;
    end
    else begin
      InitParamSymbol;
      if (RecSize <= FDbgProcess.PointerSize) and (RecSize in [2,4,8]) then begin
        Result := ReadRecFromMem(RecAddr, RecSize, RecData);
        RecAddr := RecData;
      end;

      if Result then begin
        ParamSymbol.AsCardinal := RecAddr;
        Result := not IsError(ParamSymbol.LastError);
        FLastError := ParamSymbol.LastError;
      end;
    end;
    {$ElseIf defined(UNIX)}
    if FDbgProcess.Mode = dm64 then begin
      if (RecSize <= 16) and not HasUnalignedFields then begin
        // Use 1 or 2 registers
        While RecSize > 0 do begin
          ParamSymbol.ReleaseReference;
          InitParamSymbol;
          Result := ReadRecFromMem(RecAddr, Min(8, RecSize), RecData);
          if not Result then
            exit;

          ParamSymbol.AsCardinal := RecData;
          Result := not IsError(ParamSymbol.LastError);
          if not result then begin
            FLastError := ParamSymbol.LastError;
            exit;
          end;
          dec(RecSize, 8);
          inc(RecAddr, 8);
        end;
      end
      else begin
        // on the stack
        i := (RecSize+7) and $FFFFFFF8;
        l := Length(FPreparedStack);
        SetLength(FPreparedStack, l + i);
        Result := FDbgProcess.ReadData(RecAddr, RecSize, FPreparedStack[l]);
        if not Result then begin
          FLastError := CreateError(fpErrAnyError, ['failed to read mem']);
          exit;
        end;
      end;
    end
    else begin
      // 32bit linux
        if (RecSize <= 4) then begin
          Result := ReadRecFromMem(RecAddr, 4, RecData);
          if not Result then
            exit;

          l := Length(FPreparedStack);
          SetLength(FPreparedStack, l + 4);
          if l > 0 then
            move(FPreparedStack[0], FPreparedStack[4], SizeOf(FPreparedStack[0]) * l);
          PDWord(@FPreparedStack[0])^ := RecData;
        end
        else begin
          InitParamSymbol;
          ParamSymbol.AsCardinal := RecAddr;
          Result := not IsError(ParamSymbol.LastError);
          FLastError := ParamSymbol.LastError;
        end;
    end;
    {$Else}
      Result := False;
      FLastError := CreateError(fpErrAnyError, ['record as param are not supported']);
    {$ENDIF}
  finally
    ParamSymbol.ReleaseReference;
    MemManager.ClearWritableSeflMem;
  end;
end;

function TFpDbgInfoCallContext.InternalAddStringResult: Boolean;
var
  ParamSymbol: TFpValue;
  RefSym: TFpSymbolCallParamOrdinalOrPointer;
begin
  RefSym := TFpSymbolCallParamOrdinalOrPointer.Create('', 0);
  ParamSymbol := InternalCreateParamSymbol(FNextParamRegister, RefSym, '');
  try
    Result := ParamSymbol <> nil;
    if not Result then begin
      exit;
    end;
    ParamSymbol.AsCardinal := FStringResultMem;
    Result := not IsError(ParamSymbol.LastError);
    FLastError := ParamSymbol.LastError;
  finally
    ParamSymbol.ReleaseReference;
    RefSym.ReleaseReference;
  end;
  inc(FNextParamRegister);
end;

constructor TFpDbgInfoCallContext.Create(
  const ABaseContext: TFpDbgLocationContext; AMemReader: TFpDbgMemReaderBase;
  AMemModel: TFpDbgMemModel; AMemConverter: TFpDbgMemConvertor; ADbgProcess: TDbgProcess;
  ADbgThread: TDbgThread);
begin
  inherited Create(ABaseContext, AMemReader, AMemModel, AMemConverter);
  FDbgProcess := ADbgProcess;
  FDbgThread := ADbgThread;
  FNextParamRegister := 0;
end;

destructor TFpDbgInfoCallContext.Destroy;
begin
  inherited Destroy;

  if FOrigStackPtr <> 0 then
    FDbgThread.SetStackPointerRegisterValue(FOrigStackPtr);
  debugln(FPDBG_FUNCCALL, ['CallRoutine END (CTX DESTROY)']);
end;

function TFpDbgInfoCallContext.WriteStack: Boolean;
var
  m: TDBGPtr;
begin
  Result := True;
  if Length(FPreparedStack) = 0 then
    exit;

  {$IF defined(WINDOWS)}
  if FDbgProcess.Mode = dm64 then begin
    m := AllocStack(Length(FPreparedStack)+32) + 32;
  end

  else
  {$ENDIF}
  m := AllocStack(Length(FPreparedStack));

  Result := FDbgProcess.WriteData(m, Length(FPreparedStack), FPreparedStack[0]);
  if not Result then
    FLastError := CreateError(fpErrAnyError, ['failed to write call info to stack memory']);
end;

function TFpDbgInfoCallContext.CreateParamSymbol(AParamIndex: Integer;
  ASymbolType: TFpSymbol; AName: String): TFpValue;
begin
  if AName = '' then
    AName := ASymbolType.Name;
  Result := InternalCreateParamSymbol(AParamIndex, ASymbolType.TypeInfo, AName);
end;

function TFpDbgInfoCallContext.AddParam(AParamSymbolType: TFpSymbol; AValue: TFpValue): Boolean;
var
  ParamSymbol: TFpSymbolDwarfFunctionResult;
  ParamValue: TFpValue;
  s: TFpDbgValueSize;
begin
  if AValue.Kind = skRecord then begin
    Result := AddRecordParam(AParamSymbolType, AValue);
    exit;
  end;

  Result := False;

//  ParamValue := InternalCreateParamSymbol(FNextParamRegister, AParamSymbolType, '');
  ParamValue := nil;
  ParamSymbol := TFpSymbolDwarfFunctionResult.Create('', InvalidLoc, AParamSymbolType);
  try
    ParamValue := ParamSymbol.Value;
    TFpValueDwarf(ParamValue).Context := Self;
    if (FDbgProcess.Mode = dm32) and (ParamValue.GetSize(s)) and (SizeToFullBytes(s) = 8) then
      ParamSymbol.NewAddress := InternalGetLocation(FNextParamRegister, SizeToFullBytes(s))
    else
      ParamSymbol.NewAddress := InternalGetLocation(FNextParamRegister);

    Result := ParamValue <> nil;
    if not Result then
      exit;
    if Length(FPreparedStack) > 0 then
      MemManager.SetWritableSeflMem(TDBGPtr(@FPreparedStack[0]), Length(FPreparedStack));
    ParamValue.AsCardinal := AValue.AsCardinal;
    Result := not IsError(ParamValue.LastError);
    FLastError := ParamValue.LastError;
  finally
    ParamSymbol.ReleaseReference;
    ParamValue.ReleaseReference;
    MemManager.ClearWritableSeflMem;
  end;
  inc(FNextParamRegister);
end;

function TFpDbgInfoCallContext.AddOrdinalParam(AParamSymbolType: TFpSymbol; AValue: QWord): Boolean;
var
  ParamSymbol: TFpValue;
begin
  debugln(FPDBG_FUNCCALL, ['TFpDbgInfoCallContext.AddOrdinalParam ',FNextParamRegister]);
  Result := False;
  if AParamSymbolType = nil then
    AParamSymbolType := TFpSymbolCallParamOrdinalOrPointer.Create('', 0)
  else
    AParamSymbolType.AddReference;
  ParamSymbol := InternalCreateParamSymbol(FNextParamRegister, AParamSymbolType, '');
  AParamSymbolType.ReleaseReference;
  Result := ParamSymbol <> nil;
  if not Result then
    exit;
  try
    ParamSymbol.AsCardinal := AValue;
    Result := not IsError(ParamSymbol.LastError);
    FLastError := ParamSymbol.LastError;
  finally
    ParamSymbol.ReleaseReference;
  end;
  inc(FNextParamRegister);
end;

function TFpDbgInfoCallContext.AddOrdinalParam(AValue: QWord): Boolean;
begin
  Result := AddOrdinalParam(nil, AValue);
end;

function TFpDbgInfoCallContext.AddStringResult: Boolean;
var
  ANil: QWord;
begin
  Result := True;
  ANil := 0;
  FStringResultMem := AllocStack(32); // TODO: only Win64 needs 32 alignemnt
  Result := FDbgProcess.WriteData(FStringResultMem, FDbgProcess.PointerSize, ANil);
  if not Result then begin
    FLastError := CreateError(fpErrAnyError, ['Error writing result param to stack memory']);
    exit;
  end;

  FNeedStringResInFinalize := FDbgProcess.Mode = dm32;
  if not FNeedStringResInFinalize then
    Result := InternalAddStringResult;
end;

function TFpDbgInfoCallContext.AddOrdinalViaRefAsParam(AValue: QWord): Boolean;
var
  m: TDBGPtr;
begin
  Result := AddOrdinalViaRefAsParam(AValue, m);
end;

function TFpDbgInfoCallContext.AddOrdinalViaRefAsParam(AValue: QWord; out
  ATargetParamAddr: TDBGPtr): Boolean;
var
  ParamSymbol: TFpValue;
  RefSym: TFpSymbolCallParamOrdinalOrPointer;
begin
  ATargetParamAddr := AllocStack(32);
  // TODO: need size of the ordinal
  Result := FDbgProcess.WriteData(ATargetParamAddr, FDbgProcess.PointerSize, AValue);
  if not Result then begin
    FLastError := CreateError(fpErrAnyError, ['Error writing param param to stack memory']);
    exit;
  end;

  RefSym := TFpSymbolCallParamOrdinalOrPointer.Create('', ATargetParamAddr);
  ParamSymbol := InternalCreateParamSymbol(FNextParamRegister, RefSym, '');
  try
    Result := ParamSymbol <> nil;
    if not Result then
      exit;
    ParamSymbol.AsCardinal := ATargetParamAddr;
    Result := not IsError(ParamSymbol.LastError);
    FLastError := ParamSymbol.LastError;
  finally
    ParamSymbol.ReleaseReference;
    RefSym.ReleaseReference;
  end;
  inc(FNextParamRegister);
end;

function TFpDbgInfoCallContext.FinalizeParams: Boolean;
begin
  Result := True;
  if FNeedStringResInFinalize then
    Result := InternalAddStringResult;
end;

function TFpDbgInfoCallContext.GetStringResultAsPointer(out
  AStringAsPtr: TDbgPtr): Boolean;
begin
  Result := FDbgProcess.ReadAddress(FStringResultMem, AStringAsPtr);
  if not Result then
    FLastError := CreateError(fpErrAnyError, ['failed to read result from stack mem']);
end;

function TFpDbgInfoCallContext.GetStringResult(out AVal: TFpValue;
  AStringSymbolType: TFpSymbol): Boolean;
var
  Addr, l: TDbgPtr;
  ResSymbol: TFpValue;
  s: String;
  r: Cardinal;
begin
  AVal := nil;
  Result := GetStringResultAsPointer(Addr);
  if not Result then
    exit;

  if AStringSymbolType <> nil then begin
    ResSymbol := InternalCreateParamSymbol(ConstDerefLoc(Addr), AStringSymbolType, 'result');
    AVal := TFpValueConstString.Create(ResSymbol.AsString);
    Result := IsError(ResSymbol.LastError);
    if not Result then
      FLastError := ResSymbol.LastError;
    ReleaseRefAndNil(ResSymbol);
    exit;
  end;

  s := '';
  if Addr <> 0 then begin
    Result := FDbgProcess.ReadAddress(Addr - FDbgProcess.PointerSize, l);
    if Result then begin
      l := min(l, 1000*1024);
      SetLength(s, l);
      Result := FDbgProcess.ReadData(Addr, l, s[1], r);
      SetLength(s,r);
    end;
    if not Result then begin
      FLastError := CreateError(fpErrAnyError, ['failed to read result from mem']);
      exit;
    end;
  end;
  AVal := TFpValueConstString.Create(s);
end;

function TFpDbgInfoCallContext.GetWideStringResult(out AVal: TFpValue;
  AStringSymbolType: TFpSymbol): Boolean;
var
  Addr, l: TDbgPtr;
  ResSymbol: TFpValue;
  s: WideString;
  r: Cardinal;
begin
  AVal := nil;
  Result := GetStringResultAsPointer(Addr);
  if not Result then
    exit;

  if AStringSymbolType <> nil then begin
    ResSymbol := InternalCreateParamSymbol(ConstDerefLoc(Addr), AStringSymbolType, 'result');
    AVal := TFpValueConstString.Create(ResSymbol.AsString);
    Result := IsError(ResSymbol.LastError);
    if not Result then
      FLastError := ResSymbol.LastError;
    ReleaseRefAndNil(ResSymbol);
    exit;
  end;

  s := '';
  if Addr <> 0 then begin
    Result := FDbgProcess.ReadAddress(Addr - FDbgProcess.PointerSize, l);
    if Result then begin
      l := min(l, 1000*1024);
      SetLength(s, l);
      Result := FDbgProcess.ReadData(Addr, l*2, s[1], r);
      SetLength(s,r);
    end;
    if not Result then begin
      FLastError := CreateError(fpErrAnyError, ['failed to read result from mem']);
      exit;
    end;
  end;
  AVal := TFpValueConstString.Create(s);
end;

initialization
  FPDBG_FUNCCALL := DebugLogger.FindOrRegisterLogGroup('FPDBG_FUNCCALL' {$IFDEF FPDBG_FUNCCALL} , True {$ENDIF} );
end.