Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:03:53+00:00 2026-06-04T20:03:53+00:00

Consider this typical code for method tracing (simplified for illustration): type IMethodTracer = interface

  • 0

Consider this typical code for method tracing (simplified for illustration):

type
  IMethodTracer = interface
  end;

  TMethodTracer = class(TInterfacedObject, IMethodTracer)
  private
    FName: String;
    FResultAddr: Pointer;
    FResultType: PTypeInfo;
  public
    constructor Create(
      const AName: String;
      const AResultAddr: Pointer = nil;
      const AResultType: PTypeInfo = nil);
    destructor Destroy; override;
  end;

constructor TMethodTracer.Create(
  const AName: String;
  const AResultAddr: Pointer;
  const AResultType: PTypeInfo);
begin
  inherited Create();
  FName := AName;
  FResultAddr := AResultAddr;
  FResultType := AResultType;
  Writeln('Entering ' + FName);
end;

destructor TMethodTracer.Destroy;
var
  lSuffix: String;
  lResVal: TValue;
begin
  lSuffix := '';
  if FResultAddr <> nil then
    begin
      //there's probably a more straight-forward to doing this, without involving TValue:
      TValue.Make(FResultAddr, FResultType, lResVal);
      lSuffix := ' - Result = ' + lResVal.AsString;
    end;
  Writeln('Leaving ' + FName + lSuffix);

  inherited Destroy;
end;

function TraceMethod(
  const AName: String;
  const AResultAddr: Pointer;
  const AResultType: PTypeInfo): IMethodTracer;
begin
  Result := TMethodTracer.Create(AName, AResultAddr, AResultType);
end;

//////

function F1: String;
begin
  TraceMethod('F1', @Result, TypeInfo(String));
  Writeln('Doing some stuff...');
  Result := 'Booyah!';
end;

F1();

This is working as intended. The output is:

Entering F1
Doing some stuff…
Leaving F1 – Result = Booyah!

I am now looking for a way to minimize the number of required parameters for the call to TraceMethod(), ideally allowing me to skip the Result-related arguments altogether. I have no experience with assembler or stack layout myself but If I am not mistaken, judging by the “magic” I have seen other people do, at least the memory address of the implied magic Result-variable should be obtainable somehow, shouldn’t it? And possibly one can work from there to get at its type info, too?

Of course, if it were possible to even determine the name of the “surrounding” function itself that would eliminate the need for passing arguments to TraceMethod entirely…

I am using Delphi XE2, so all recently introduced language/framework features can be used.

And before anyone mentions it: My actual code already uses CodeSite.EnterMethod/ExitMethod instead of Writeln-calls. I am also aware that this simplified example cannot handle complex types and performs no error handling whatsoever.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:03:55+00:00Added an answer on June 4, 2026 at 8:03 pm

    Your best bet is truly to just pass in @Result. If you don’t, then there’s no guarantee that Result even has an address. Functions returning simple types like Integer and Boolean put the result in the EAX register. If there’s no reason for the result to have an address, then the compiler won’t allocate any memory for it. Using the expression @Result forces the compiler to give it an address.

    Merely knowing the address won’t get you the return type, though. There might be a way to discover it with RTTI. It would involve three steps:

    1. Extract the class name from the method name. Then you can get the RTTI for that type. This would require the method name to include an unambiguous name for the class (including unit name).

    2. Using the list of methods from that type, find the RTTI for the method. This will be complicated by the fact that the name doesn’t necessarily uniquely identify a method. Overloads will all show the same name. (Rruz showed how to deal with RTTI of overloaded methods in the context of the Invoke method.) Also, the method name you get from the debug info won’t necessarily match the RTTI name.

      Instead of trying to match the name, you could instead loop over all the class’s methods, searching for the one whose CodeAddress property matches the address of the caller. Determining how to get the address of the start of the caller (instead of the return address) is proving more difficult to find than I expected, though.

    3. Get the method’s return type and use the Handle property to finally get the PTypeInfo value you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code snippet public class ConstantFolding { static final int number1 = 5;
Consider this simplified model in Django: class Item(models.Model): title = models.CharField(max_length=200) pub_date = models.DateTimeField()
Consider this example (typical in OOP books): I have an Animal class, where each
Consider some typical CF code involving error handling, say something like this: ABRecordRef aRecord
Consider the following typical Scala 'pimp' code: class PimpedA(a:A){ def pimp() = hi }
Consider this JavaScript Code: <script type=text/javascript> function loop(Message){ document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + Message +
Consider this sample html <div class=A> <div class=B> <span class=C>Sample text</span> <div class=D> <div
Consider this code _bindEvents: -> @input.bind 'keyup', => @filter($(this).val()) if $this .val() is ''
Consider this code: #define N_ 0 #define N_X 1 #define M(a) N_ M(arg)X; //
Consider this: class Vec3 { private: float n[3]; public: float& x; float& y; float&

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.