如何在 Delphi 调试器可视化工具中拦截函数返回值?

Jan*_*sen 5 delphi debuggervisualizer

我有一个工作调试器可视化工具,可以帮助可视化 TIntTime 类型的变量。

TIntTime = 整数类型;

可视化工具用时间字符串 HH:MM:SS 替换自午夜以来的秒数。在调试会话期间,这对于 TIntTime 类型的变量效果很好,但对于函数则不然。例如,如果我将 GetiTime 放入手表中

function GetiTime: TIntTime;
begin
  Result:=30000;
end;
Run Code Online (Sandbox Code Playgroud)

手表将显示 30000。预期替换值为“08:20:00”。可视化工具不会拦截 TIntTime 类型的函数返回值,这就是问题所在。

我正在使用 Delphi 10 西雅图。我的可视化工具基于 Delphi 10\source\Visualizers 中的 DateTimeVisualizer.pas。DateTimeVisualizer 建议通过在 GetSupportedType 中使用类型名称字符串“function: TIntTime”来拦截函数返回值。我努力了

'function: TIntTime'
'function:TIntTime'
'function::TIntTime'
Run Code Online (Sandbox Code Playgroud)

没有运气。我怀疑这是一个使该类型名称字符串正确的问题,但无法在互联网上找到有关格式的信息。

如果我将 GetDateTime 放入手表中,它会按预期显示“14-02-2018 13:20:30”。如果我在选项中关闭 TDateTime/TDate/TTime 可视化工具,手表会显示 43145.5559... 这告诉我可以使用可视化工具拦截函数返回值。

  function GetDateTime: TDateTime;
  begin
    Result:=EncodeDateTime(2018,2,14,13,20,30,0);
  end;
Run Code Online (Sandbox Code Playgroud)

就我而言,不能选择使用 TDateTime 数据类型。所以我的问题是:如何让可视化工具拦截 TIntTime 类型的函数返回值?

以下是 TIntTime 可视化工具的来源

unit IntTimeVisualizer;

interface

procedure Register;

implementation

uses
  Classes, Forms, SysUtils, ToolsAPI;

resourcestring
  sIntTimeVisualizerName = 'TIntTime Visualizer for Delphi';
  sIntTimeVisualizerDescription = 'Displays TIntTime instances in a human-readable time format rather than as an integer value';

type
  TDebuggerIntTimeVisualizer = class(TInterfacedObject, IOTADebuggerVisualizer,
    IOTADebuggerVisualizerValueReplacer, IOTAThreadNotifier, IOTAThreadNotifier160)
  private
    FCompleted: Boolean;
    FDeferredResult: string;
  public
    { IOTADebuggerVisualizer }
    function GetSupportedTypeCount: Integer;
    procedure GetSupportedType(Index: Integer; var TypeName: string;
      var AllDescendants: Boolean);
    function GetVisualizerIdentifier: string;
    function GetVisualizerName: string;
    function GetVisualizerDescription: string;
    { IOTADebuggerVisualizerValueReplacer }
    function GetReplacementValue(const Expression, TypeName, EvalResult: string): string;
    { IOTAThreadNotifier }
    procedure EvaluteComplete(const ExprStr: string; const ResultStr: string;
      CanModify: Boolean; ResultAddress: Cardinal; ResultSize: Cardinal;
      ReturnCode: Integer);
    procedure ModifyComplete(const ExprStr: string; const ResultStr: string;
      ReturnCode: Integer);
    procedure ThreadNotify(Reason: TOTANotifyReason);
    procedure AfterSave;
    procedure BeforeSave;
    procedure Destroyed;
    procedure Modified;
    { IOTAThreadNotifier160 }
    procedure EvaluateComplete(const ExprStr: string; const ResultStr: string;
      CanModify: Boolean; ResultAddress: TOTAAddress; ResultSize: LongWord;
      ReturnCode: Integer);
  end;

  TIntTimeType = (dttIntTime);

  TIntTimeVisualizerType = record
    TypeName: string;
    TimeType: TIntTimeType;
  end;

const
  IntTimeVisualizerTypes: array[0..1] of TIntTimeVisualizerType =
  (
    (TypeName: 'TIntTime'; TimeType: dttIntTime;),    //<-- This type is working fine
    (TypeName: 'function: TIntTime'; TimeType: dttIntTime;)  //<-- This type is not working
  );

{ TDebuggerIntTimeVisualizer }

procedure TDebuggerIntTimeVisualizer.AfterSave;
begin
  // don't care about this notification
end;

procedure TDebuggerIntTimeVisualizer.BeforeSave;
begin
  // don't care about this notification
end;

procedure TDebuggerIntTimeVisualizer.Destroyed;
begin
  // don't care about this notification
end;

procedure TDebuggerIntTimeVisualizer.Modified;
begin
  // don't care about this notification
end;

procedure TDebuggerIntTimeVisualizer.ModifyComplete(const ExprStr,
  ResultStr: string; ReturnCode: Integer);
begin
  // don't care about this notification
end;

procedure TDebuggerIntTimeVisualizer.EvaluteComplete(const ExprStr,
  ResultStr: string; CanModify: Boolean; ResultAddress, ResultSize: Cardinal;
  ReturnCode: Integer);
begin
  EvaluateComplete(ExprStr, ResultStr, CanModify, TOTAAddress(ResultAddress),
    LongWord(ResultSize), ReturnCode);
end;

procedure TDebuggerIntTimeVisualizer.EvaluateComplete(const ExprStr,
  ResultStr: string; CanModify: Boolean; ResultAddress: TOTAAddress; ResultSize: LongWord;
  ReturnCode: Integer);
begin
  FCompleted := True;
  if ReturnCode = 0 then
    FDeferredResult := ResultStr;
end;

procedure TDebuggerIntTimeVisualizer.ThreadNotify(Reason: TOTANotifyReason);
begin
  // don't care about this notification
end;

function TDebuggerIntTimeVisualizer.GetReplacementValue(
  const Expression, TypeName, EvalResult: string): string;
var
  TimeType: TIntTimeType;
  I: Integer;

  function IntTimeToStr(s: Integer): string;
  var
    hh, mm, ss: integer;
  begin
    hh:=s div 3600;
    mm:=(s div 60)-hh*60;
    ss:=s mod 60;
    Result:=Format('%.2d:%.2d:%.2d',[hh,mm,ss]);
  end;

  function FormatResult(const LEvalResult: string; DTType: TIntTimeType; out ResStr: string): Boolean;
  var
    IntValue: integer;
  begin
    Result := True;
    try
      if not TryStrToInt(LEvalResult, IntValue) then
        Result:=false
      else
        case DTType of
          dttIntTime: ResStr:=IntTimeToStr(IntValue);
        end;
    except
      Result := False;
    end;
  end;

begin
  TimeType := TIntTimeType(-1);
  for I := Low(IntTimeVisualizerTypes) to High(IntTimeVisualizerTypes) do begin
    if TypeName = IntTimeVisualizerTypes[I].TypeName then begin
      TimeType:=IntTimeVisualizerTypes[I].TimeType;
      Break;
    end;
  end;

  if not FormatResult(EvalResult, TimeType, Result) then
    Result := EvalResult;
end;

function TDebuggerIntTimeVisualizer.GetSupportedTypeCount: Integer;
begin
  Result := Length(IntTimeVisualizerTypes);
end;

procedure TDebuggerIntTimeVisualizer.GetSupportedType(Index: Integer; var TypeName: string;
  var AllDescendants: Boolean);
begin
  AllDescendants := false;
  TypeName := IntTimeVisualizerTypes[Index].TypeName;
end;

function TDebuggerIntTimeVisualizer.GetVisualizerDescription: string;
begin
  Result := sIntTimeVisualizerDescription;
end;

function TDebuggerIntTimeVisualizer.GetVisualizerIdentifier: string;
begin
  Result := ClassName;
end;

function TDebuggerIntTimeVisualizer.GetVisualizerName: string;
begin
  Result := sIntTimeVisualizerName;
end;

var
  IntTimeVis: IOTADebuggerVisualizer;

procedure Register;
begin
  IntTimeVis:=TDebuggerIntTimeVisualizer.Create;
  (BorlandIDEServices as IOTADebuggerServices).RegisterDebugVisualizer(IntTimeVis);
end;

procedure RemoveVisualizer;
var
  DebuggerServices: IOTADebuggerServices;
begin
  if Supports(BorlandIDEServices, IOTADebuggerServices, DebuggerServices) then begin
    DebuggerServices.UnregisterDebugVisualizer(IntTimeVis);
    IntTimeVis:=nil;
  end;
end;

initialization

finalization
  RemoveVisualizer;
end.
Run Code Online (Sandbox Code Playgroud)