Printf the current address in C program

后端 未结 8 1329
时光说笑
时光说笑 2021-02-04 12:27

Imagine I have the following simple C program:

int main() {

int a=5, b= 6, c;
c = a +b; 
return 0;
}

Now, I would like to know the address of

相关标签:
8条回答
  • 2021-02-04 12:30

    For x86:

    int test()
    {
        __asm {
            mov eax, [esp]
        }
    }
    
    
    __declspec(noinline) int main() // or whatever noinline feature your compiler has
    {
        int a = 5;
        int aftertest;
    
        aftertest = test()+3; // aftertest = disasms to 89 45 F8 mov dword ptr [a],eax.
    
        printf("%i", a+9);
        printf("%x", test());
        return 0;
    }
    
    0 讨论(0)
  • 2021-02-04 12:42

    Visual C++ has the _ReturnAddress intrinsic, which can be used to get some info here.

    For instance:

    __declspec(noinline) void PrintCurrentAddress()
    {
        printf("%p", __ReturnAddress);
    }
    

    Which will give you an address close to the expression you're looking at. In the event of some optimizations, like tail folding, this will not be reliable.

    0 讨论(0)
  • 2021-02-04 12:42

    I don't know the details, but there should be a way to make a call to a function that can then crawl the return stack for the address of the caller, and then copy and print that out.

    0 讨论(0)
  • 2021-02-04 12:42

    If the compiler is any good this addition happens in registers and is never stored in memory, at least not in the way you are thinking. Actually a good compiler will see that your program does nothing, manipulating values within a function but never sending those values anywhere outside the function can result in no code.

    If you were to:

    c = a+b; printf("%u\n",c);

    Then a good compiler will also never store that value C in memory it will stay in registers, although it depends on the processor as well. If for example compilers for that processor use the stack to pass variables to functions then the value for c will be computed using registers (a good compiler will see that C is always 11 and just assign it) and the value will be put on the stack while being sent to the printf function. Naturally the printf function may well need temporary storage in memory due to its complexity (cant fit everything it needs to do in registers).

    Where I am heading is that there is no answer to your question. It is heavily dependent on the processor, compiler, etc. There is no generic answer. I have to wonder what the root of the question is, if you were hoping to probe with a debugger, then this is not the question to ask.

    Bottom line, disassemble your program and look at it, for that compile on that day with those settings, you will be able to see where the compiler has placed intermediate values. Even if the compiler assigns a memory location for the variable that doesnt mean the program will ever store the variable in that location. It depends on optimizations.

    0 讨论(0)
  • 2021-02-04 12:44

    Tested in Visual Studio 2008:

    int addr;
    __asm
    {
        call _here
        _here: pop eax
        ; eax now holds the PC.
        mov [addr], eax
    }
    
    printf("%x\n", addr);
    

    Credit to this question.

    0 讨论(0)
  • 2021-02-04 12:45

    Here's a sketch of an alternative approach:

    Assume that you haven't stripped debug symbols, and in particular you have the line number to address table that a source-level symbolic debugger needs in order to implement things like single step by source line, set a break point at a source line, and so forth.

    Most tool chains use reasonably well documented debug data formats, and there are often helper libraries that implement most of the details.

    Given that and some help from the preprocessor macro __LINE__ which evaluates to the current line number, it should be possible to write a function which looks up the address of any source line.

    Advantages are that no assembly is required, portability can be achieved by calling on platform-specific debug information libraries, and it isn't necessary to directly manipulate the stack or use tricks that break the CPU pipeline.

    A big disadvantage is that it will be slower than any approach based on directly reading the program counter.

    0 讨论(0)
提交回复
热议问题