How do I get the address of the current function?

Tim Writer writer at me.utoronto.ca
Fri Jun 29 15:34:15 AEST 1990


In article <1823 at tkou02.enet.dec.com> diamond at tkou02.enet.dec.com (diamond at tkovoa) writes:

>In article <90Jun28.195353edt.19442 at me.utoronto.ca> writer at me.utoronto.ca (Tim Writer) writes:

>If you know the name of the function, if it is foo, you just say &foo

I don't want to need to know the name of the function.

>void (*fcn)();
>void foo() {
>	fcn = &foo;
>}

Actually, it would just be `fcn=foo'.

As I feared, you misunderstood me.  I want to *compute* the address of the
currently executing function.  In other words, I want the computer to tell
me what is the address of the function it is executing.

Consider the following pseudocode.

     1	jmp_buf	buf1,
     2		buf2,
     3		buf3;
     4	
     5	main()
     6	{
     7		if (setjmp(buf1)) {
     8			... stuff ...
     9			longjmp(buf3,1);
    10		}
    11		... stuff ...
    12		fcn()
    13		... stuff ...
    14	}
    15	
    16	fcn()
    17	{
    18		if (setjmp(buf2)) {
    19			... stuff ...
    20			longjmp(buf3,1);
    21		}
    22		... stuff ...
    23		if (... condition ...) {
    24			void setjmp(buf3);
    25			longjmp(... buf1 or buf2, I don't know ...,1)
    26		}
    27		... stuff ...
    28	}

Longjmp() on line 25 may jump to line 19, or it may jump to line 8; I don't
know which.  If it jumps to line 19, longjmp() on line 19 is fine.  However,
if it jumps to 8, the longjmp() on line 9 would be jumping into fcn() which
has returned.  The problem is how to detect this.

I have a written a fairly general exception handling facility which includes
several macros which generate code similar to the above.  That is why
I don't know where the longjmp() of line 25 is going to jump to.  When I use
these macros I want them to be able to detect a bad longjmp() without needing
to pass them extra arguments which differ from function to function.

I figured if there was a way to compute the address of the currently
executing function, I could have my macros set and examine a global function
pointer.  So, before line 25, I would set it, and I would check it against
the address of the currently executing function before lines 9 and 20.
That way, I could tell where the longjmp() was going to go.

I hope this makes things a bit more clear.

Any suggestions?

Tim




More information about the Comp.lang.c mailing list