This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Is there a (somewhat) portable way to get the return address of a function?
- To: george at moberg dot com
- Subject: Re: Is there a (somewhat) portable way to get the return address of a function?
- From: Nick Ing-Simmons <nik at tiuk dot ti dot com>
- Date: Wed, 7 Jun 2000 12:42:46 +0100 (BST)
- Cc: gcc at gcc dot gnu dot org
- Organization: via, but not speaking for : Texas Instruments Ltd.
- References: <393D8039.26DE4D8@moberg.com>
- Reply-To: Nick Ing-Simmons <nik at tiuk dot ti dot com>
George T . Talbot <george@moberg.com> writes:
>I'm working on some library code in Linux, and, for debugging purposes, I'd
>like to be able to call dlinfo() on the return address of a function in my
>library so that I can track usage of the function:
>
>#define magic() ...woudln't I like to know...
>
>void some_library_function(void)
>{
> void* return_address = magic();
> Dl_info info;
>
> if (dlinfo(return_address, &info))
> fprintf(some_log_file, "some_library_function() called from %s in executable
>%s.\n",
> info.dli_sname, info.dli_fname);
> else
> fprintf(some_log_file, "some_library_function() called from unknown
>caller.\n");
>}
>
>-----
>void some_code(void)
>{
> ...
> some_library_function();
> ...
>}
>
>
>In other words, I want magic() above to return the address in some_code()
>where some_library_function() was called.
Sure:
void some_library_function(void)
{
void* return_address = __builtin_return_address(1);
DL_info info;
if (dladdr(return_address,&info))
{
}
...
}
Note that is dladdr() not dlinfo().
Works at least on Linux and Solaris.
--
Nick Ing-Simmons <nik@tiuk.ti.com>
Via, but not speaking for: Texas Instruments Ltd.