简体   繁体   中英

Get variable name by address

I have a dialog that displays an array of bytes, for the purpose of hex editing them. It is possible to switch displayed memory regions. But while some regions are internally arrays, there are a few regions that are structs, and I'm displaying certain elements from them (consecutively).

I want to add a kind of symbolic name feature, where selecting an address gives you its name. For actual arrays it's done by the user, who calls them whatever he wants, and it's saved in file. But those internal structs are not supposed to have user-defined names, instead I want to give them hard-coded names.

The question: is it possible to get their names by knowing their address? Like, byte X of struct Y is referring to the variable Z, when selecting cell X, display it's name as "Z". Or I also have to create name lists and use them regularly?

EDIT:

Simplifying the question: Is there a backward analog of offsetof() ? Normally you pass it a pointer and a variable, and it gives you the offset to that variable. So I need to pass a pointer and an offset, and get the variable (structure member) name (and then stringify it with # ).

The best way of doing this as I know it, is to use a map between addresses region and the names.

The code should be something like this :

class address_region{
    intptr_t start;
    intptr_t end;

    template<class T>
    address_region(const T& t){
       start = (intptr_t)(&t);
       end   = start + sizeof(t); 
    }

    address_region(intptr_t start_adr=0, intptr_t end_adr=0){
         start = start_adr;
         end   = end_adr;
    }

    bool operator<(const address_region& adr){
        if( adr.start >  start ) return true;
        else return false;
    }
};

map< address_region , std::string > addresses_map;

assume you have this struct;

struct A
{
  int X;
  int Y;
};

A a;

to store addresses it should be something like :
addresses_map[ address_region(a) ] = "struct A";
addresses_map[ addresses_map(a.X) ] = "struct A : int X";
addresses_map[ addresses_map(a.X) ] = "struct A : int Y";

to retrieve result:

  intptr_t adrs = ....;

  std::string symbole_name = addresses_map[ address_region(adrs) ];

Your compiler should be able to print a map file which is a reference of symbols and their addresses (or offsets).

Looks like you need to create a std::map at initialization. The map would have the symbol's address and the name, something like this:

  std::map<uint64_t address, std::string> symbol_dictionary;

You will have to perform this at run-time because some operating systems will place your code at different addresses on each execution, depending memory usage by other applications (and memory layout).

I believe you need to change your design, or rethink the requirements.
For example, can you change variables by name rather than by address? You would display the variable name in the window and allow the user to change the value. Your program takes this value and sets the variable to this value. No need to know the addresses.

For security purposes, you do not want users changing memory by address. They could change the wrong address in your program (such as the code in a function), then call you up because your application is not working anymore.

Sorry, but I've got better things to do than support people who type in addresses incorrectly and then wonder why the program doesn't work any more.

最后制作了一个符号名称结构(数组指针,开始,大小,名称)的向量,并手动向其中添加了一堆名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM