Recent

Author Topic: [Solved] Get variable name of the address pointed to by pointer possible?  (Read 888 times)

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 97
Hello All,

I'm just playing around. I'm NOT trying to incorporate this into an actual program.
It is elementary to find the "VALUE" of a variable using a pointer

My question is:  Can Laz/FPC, or inline ASM be made to reveal that the variable "NAME" not the "VALUE" pointed to by the pointer; in the case below
can I somehow return the string "My_var" from a query of some type?

  SCENARIO
  1. Suppose you have declared                            My_var: Integer;
  2. Then created a pointer "My_Ptr" pointing to      My_var

Salamat
DBM

« Last Edit: June 06, 2024, 01:12:28 am by Ten_Mile_Hike »
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

jamie

  • Hero Member
  • *****
  • Posts: 6811
Re: Get variable name of the address pointed to by pointer possible?
« Reply #1 on: June 06, 2024, 12:13:22 am »
Hello All,

I'm just playing around. I'm NOT trying to incorporate this into an actual program.
It is elementary to find the "VALUE" of a variable using a pointer

My question is:  Can Laz/FPC, or inline ASM be made to reveal that the variable "NAME" not the "VALUE" pointed to by the pointer; in the case below
can I somehow return the string "My_var" from a query of some type?

  SCENARIO
  1. Suppose you have declared                            My_var: Integer;
  2. Then created a pointer "My_Ptr" pointing to      My_var

Salamat
DBM

No. This is a real compile language..

You can, however, compare the address value with known variables of their address values.


Also, for Class and such, there is runtime info to some degree.


The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 5066
Re: Get variable name of the address pointed to by pointer possible?
« Reply #2 on: June 06, 2024, 12:25:48 am »
There is one case where it is possible to obtain the variable name the pointer points to and that is when the executable is compiled with debugging information.

In that case, by inspecting the debugging symbols it is possible to find the address and the identifier (name) associated with that address.

The important thing to always keep in mind is that the ability depends on debugging information being present otherwise the executable does not contain the necessary information to map an address to an identifier/name.

Just FYI, debuggers as a matter of course, do the opposite of that.  The name/identifier is mapped to an address and whatever is at that address is the value of that name/identifier.    A debugger can and, probably does whenever necessary, do the opposite, i.e, go from address to name.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10897
  • Debugger - SynEdit - and more
    • wiki
Re: Get variable name of the address pointed to by pointer possible?
« Reply #3 on: June 06, 2024, 12:58:15 am »
If it is a variable, then the name (other than in the debug info) will not be in the exe. The compiler will have replaced it by the address it had gotten for it, and so once everything used that address the name was irrelevant.

However, if it is not a variable, but a published field/property of an object (class) then the compiler will create RTTI for that property.
If you have the address of such a field, and if you then also have the class, then you can iterate over the published fields, and find the one with the matching address. And you can then get the name of it. (That is how a form is loaded from lfm, only that here the lookup is by name, and the address (or setter if it is a property) is then used to write the loaded value to.

 

TinyPortal © 2005-2018