File: fpdebugconvdebugforjson.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 (354 lines) | stat: -rw-r--r-- 10,960 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
unit FpDebugConvDebugForJson;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls, ActnList,
  FpDebugStringConstants, FpDebugValueConvertors, FpDebugDebuggerBase,
  LazDebuggerValueConverter, LazDebuggerIntfBaseTypes, LazDebuggerIntf,
  FpDbgInfo, FpDbgClasses, FpdMemoryTools, FpDbgCallContextInfo,
  FpErrorMessages, DbgIntfBaseTypes;

type

  { TJsonForDebugSettingsFrame }

  TJsonForDebugSettingsFrame = class(TFrame, ILazDbgValueConverterSettingsFrameIntf)
    ConverterWithFuncCallSettingsFrame1: TConverterWithFuncCallSettingsFrame;
    edFuncName: TEdit;
    edJsonAddress: TEdit;
    edJsonTypename: TEdit;
    Label1: TLabel;
    lblFuncName: TLabel;
    lblJsonAddress: TLabel;
    lblJsonTypename: TLabel;
  private

  protected
    procedure ReadFrom(AConvertor: ILazDbgValueConverterIntf);
    function WriteTo(AConvertor: ILazDbgValueConverterIntf): Boolean;
    function GetFrame: TObject;
  public
    constructor Create(TheOwner: TComponent); override;
  end;

  { TFpDbgValueConverterJsonForDebug }

  TFpDbgValueConverterJsonForDebug = class(TFpDbgValueConverterWithFuncCall)
  private
    FFunctionName: String;
    FJsonAddressKey: String;
    FJsonTypenameKey: String;
    function FunctionNameIsStored: Boolean;
    function GetProcAddr(AnFpDebugger: TFpDebugDebuggerBase; AnExpressionScope: TFpDbgSymbolScope): TDBGPtr;
    function JsonAddressKeyIsStored: Boolean;
  protected
    function GetSettingsFrame: ILazDbgValueConverterSettingsFrameIntf; override;
    procedure Init; override;
  public
    class function GetName: String; override;
    function GetRegistryEntry: TLazDbgValueConvertRegistryEntryClass; override;
    procedure Assign(ASource: TFpDbgValueConverter); override;
    function ConvertValue(ASourceValue: TFpValue;
                          AnFpDebugger: TFpDebugDebuggerBase;
                          AnExpressionScope: TFpDbgSymbolScope;
                          var AnResData: IDbgWatchDataIntf
                         ): TFpValue; override;

  published
    property FunctionName: String read FFunctionName write FFunctionName stored FunctionNameIsStored;
    property JsonAddressKey: String read FJsonAddressKey write FJsonAddressKey stored JsonAddressKeyIsStored;
    property JsonTypenameKey: String read FJsonTypenameKey write FJsonTypenameKey;
  end;

  { TFpDbgValueConverterJsonForDebugRegistryEntry }

  TFpDbgValueConverterJsonForDebugRegistryEntry = class(TFpDbgValueConverterRegistryEntry)
  public
    class function GetConvertorClass: TClass; override;
  end;

implementation

{$R *.lfm}

{ TJsonForDebugSettingsFrame }

procedure TJsonForDebugSettingsFrame.ReadFrom(
  AConvertor: ILazDbgValueConverterIntf);
var
  c: TFpDbgValueConverterJsonForDebug;
begin
  ConverterWithFuncCallSettingsFrame1.ReadFrom(AConvertor);

  if not (AConvertor.GetObject is TFpDbgValueConverterJsonForDebug) then
    exit;

  c := TFpDbgValueConverterJsonForDebug(AConvertor.GetObject);

  edFuncName.Text := c.FFunctionName;
  edJsonAddress.Text := c.FJsonAddressKey;
  edJsonTypename.Text := c.FJsonTypenameKey;
end;

function TJsonForDebugSettingsFrame.WriteTo(
  AConvertor: ILazDbgValueConverterIntf): Boolean;
var
  c: TFpDbgValueConverterJsonForDebug;
begin
  Result := ConverterWithFuncCallSettingsFrame1.WriteTo(AConvertor);

  if not (AConvertor.GetObject is TFpDbgValueConverterJsonForDebug) then
    exit;

  c := TFpDbgValueConverterJsonForDebug(AConvertor.GetObject);

  Result :=
    Result or
    (c.FFunctionName    <> trim(edFuncName.Text)) or
    (c.FJsonAddressKey  <> trim(edJsonAddress.Text)) or
    (c.FJsonTypenameKey <> trim(edJsonTypename.Text));

  c.FFunctionName    := trim(edFuncName.Text);
  c.FJsonAddressKey  := trim(edJsonAddress.Text);
  c.FJsonTypenameKey := trim(edJsonTypename.Text);
end;

function TJsonForDebugSettingsFrame.GetFrame: TObject;
begin
  Result := Self;
end;

constructor TJsonForDebugSettingsFrame.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  lblFuncName.Caption := drsFunctionName;
  lblJsonAddress.Caption := drsKeyForAddress;
  lblJsonTypename.Caption := drsKeyForTypename;
end;

{ TFpDbgValueConverterJsonForDebug }

function TFpDbgValueConverterJsonForDebug.GetProcAddr(
  AnFpDebugger: TFpDebugDebuggerBase; AnExpressionScope: TFpDbgSymbolScope
  ): TDBGPtr;
var
  CurProc: TDbgProcess;
  ProcSymVal: TFpValue;
  ProcSym: TFpSymbol;
begin
  Result := AnFpDebugger.GetCachedData(pointer(Self));
  if Result <> 0 then
    exit;

  CurProc := AnFpDebugger.DbgController.CurrentProcess;
  if CurProc = nil then
    exit;

  ProcSymVal := AnExpressionScope.FindSymbol(FFunctionName);
  if ProcSymVal <> nil then begin
    if (ProcSymVal.Kind = skProcedure) and IsTargetAddr(ProcSymVal.DataAddress)
    //and
    //   (ProcSymVal.NestedSymbolCount = 3)
    then begin
      Result := ProcSymVal.DataAddress.Address;
      AnFpDebugger.SetCachedData(pointer(TFpDbgValueConverterJsonForDebug), Result);
      ProcSymVal.ReleaseReference;
      exit;
    end;
    Result := 0;
    Result := ProcSymVal.DataAddress.Address;
  end;

  ProcSym := CurProc.FindProcSymbol(FFunctionName);
  if (ProcSym <> nil) and (ProcSym.Kind = skProcedure) and
     (IsTargetAddr(ProcSym.Address))
  then begin
    Result := ProcSym.Address.Address;
    AnFpDebugger.SetCachedData(pointer(TFpDbgValueConverterJsonForDebug), Result);
  end;
  ProcSym.ReleaseReference;
end;

function TFpDbgValueConverterJsonForDebug.JsonAddressKeyIsStored: Boolean;
begin
  Result := FJsonAddressKey <> 'Address';
end;

function TFpDbgValueConverterJsonForDebug.FunctionNameIsStored: Boolean;
begin
  Result := FFunctionName <> 'JsonForDebug';
end;

function TFpDbgValueConverterJsonForDebug.GetSettingsFrame: ILazDbgValueConverterSettingsFrameIntf;
begin
  Result := TJsonForDebugSettingsFrame.Create(nil);
end;

procedure TFpDbgValueConverterJsonForDebug.Init;
begin
  inherited Init;
  FFunctionName := 'JsonForDebug';
  FJsonAddressKey := 'Address'
end;

class function TFpDbgValueConverterJsonForDebug.GetName: String;
begin
  Result := drsCallJsonForDebug;
end;

function TFpDbgValueConverterJsonForDebug.GetRegistryEntry: TLazDbgValueConvertRegistryEntryClass;
begin
  Result := TFpDbgValueConverterJsonForDebugRegistryEntry;
end;

procedure TFpDbgValueConverterJsonForDebug.Assign(ASource: TFpDbgValueConverter);
begin
  inherited Assign(ASource);
  if ASource is TFpDbgValueConverterJsonForDebug then begin
    FFunctionName    := TFpDbgValueConverterJsonForDebug(ASource).FFunctionName;
    FJsonAddressKey  := TFpDbgValueConverterJsonForDebug(ASource).FJsonAddressKey;
    FJsonTypenameKey := TFpDbgValueConverterJsonForDebug(ASource).FJsonTypenameKey;
  end;
end;

function TFpDbgValueConverterJsonForDebug.ConvertValue(ASourceValue: TFpValue;
  AnFpDebugger: TFpDebugDebuggerBase; AnExpressionScope: TFpDbgSymbolScope;
  var AnResData: IDbgWatchDataIntf): TFpValue;
var
  CurProccess: TDbgProcess;
  TpName, JsonText: String;
  ProcAddr, SetLenProc, DecRefProc,
  TpNameAddr, TpNewNameAddr, TpNameRefAddr, TextAddr, TextRefAddr: TDbgPtr;
  CallContext: TFpDbgInfoCallContext;
  r: Boolean;
begin
  Result := nil;

  if (not (svfAddress in ASourceValue.FieldFlags)) or
     (not IsTargetAddr(ASourceValue.Address))
  then begin
    SetError(CreateError(fpErrAnyError, ['Value not in memory']));
    exit;
  end;

  TpName := '';
  if ASourceValue.TypeInfo <> nil then
    TpName := ASourceValue.TypeInfo.Name;

  if TpName = '' then begin
    SetError(CreateError(fpErrAnyError, ['no typename']));
    exit;
  end;

  ProcAddr := GetProcAddr(AnFpDebugger, AnExpressionScope);
  if ProcAddr = 0 then begin
    SetError(CreateError(fpErrAnyError, ['JsonForDebug not found']));
    exit;
  end;

  CurProccess := AnFpDebugger.DbgController.CurrentProcess;
  SetLenProc := AnFpDebugger.GetCached_FPC_ANSISTR_SETLENGTH;
  DecRefProc := AnFpDebugger.GetCached_FPC_ANSISTR_DECR_REF;
  if (SetLenProc = 0) or (DecRefProc = 0) or (CurProccess = nil)
  then begin
    SetError(CreateError(fpErrAnyError, ['internal error']));
    exit;
  end;


  TpNameAddr := 0;
  TpNewNameAddr := 0;
  TpNameRefAddr := 0;
  TextAddr := 0;
  TextRefAddr := 0;
  try
    if (not AnFpDebugger.CreateAnsiStringInTarget(SetLenProc, TpNameAddr, TpName, AnExpressionScope.LocationContext)) or
       (TpNameAddr = 0)
    then begin
      TpNameAddr := 0;
      SetError(CreateError(fpErrAnyError, ['failed to set param']));
      exit;
    end;


    CallContext := AnFpDebugger.DbgController.Call(TargetLoc(ProcAddr), AnExpressionScope.LocationContext,
      AnFpDebugger.MemReader, AnFpDebugger.MemConverter);
    if CallContext = nil then begin
      SetError(CreateError(fpErrAnyError, ['function call not possible']));
      exit;
    end;

    if (not CallContext.AddOrdinalParam(ASourceValue.Address.Address)) or
       (not CallContext.AddOrdinalViaRefAsParam(TpNameAddr, TpNameRefAddr)) or
       (not CallContext.AddOrdinalViaRefAsParam(0, TextRefAddr))
    then begin
      SetError(CreateError(fpErrAnyError, ['failed to set param']));
      exit;
    end;

    CallContext.FinalizeParams; // force the string as first param (32bit) // TODO

    AnFpDebugger.BeforeWatchEval(CallContext);
    AnFpDebugger.RunProcessLoop(not FuncCallRunAllThreads);

    if not CallContext.IsValid then begin
      if (IsError(CallContext.LastError)) then
        SetError(CallContext.LastError)
      else
      if (CallContext.Message <> '') then
        SetError(CreateError(fpErrAnyError, [CallContext.Message]));
      exit;
    end;

    r := True;
    if not CurProccess.ReadAddress(TpNameRefAddr, TpNewNameAddr) then begin
      r := False;
      TpNewNameAddr := 0;
    end;
    if not CurProccess.ReadAddress(TextRefAddr, TextAddr) then begin
      r := False;
      TextAddr:= 0;
    end;

    if not AnFpDebugger.ReadAnsiStringFromTarget(TpNewNameAddr, TpName) then
      r := False;
    if not AnFpDebugger.ReadAnsiStringFromTarget(TextAddr, JsonText) then
      r := False;

    if not r then begin
      SetError(CreateError(fpErrAnyError, ['failed to get result']));
      exit;
    end;


    AnFpDebugger.DbgController.AbortCurrentCommand(True);
    CallContext.ReleaseReference;

    Result := TFpValueConstString.Create(JsonText);
    TFpValueConstString(Result).SetTypeName(TpName);

  finally
    if TpNewNameAddr <> 0 then
      TpNameAddr := TpNewNameAddr;
    if TpNameAddr <> 0 then
      AnFpDebugger.CallTargetFuncStringDecRef(DecRefProc, TpNameAddr, AnExpressionScope.LocationContext);
    if TextAddr <> 0 then
      AnFpDebugger.CallTargetFuncStringDecRef(DecRefProc, TextAddr, AnExpressionScope.LocationContext);
  end;
end;

{ TFpDbgValueConverterJsonForDebugRegistryEntry }

class function TFpDbgValueConverterJsonForDebugRegistryEntry.GetConvertorClass: TClass;
begin
  Result := TFpDbgValueConverterJsonForDebug;
end;

initialization
  ValueConverterRegistry.Add(TFpDbgValueConverterJsonForDebugRegistryEntry);

end.