__BUILTIN_RETURN_ADDRESS(3)

πŸ“¦ version 11.0, in nixpkgs unstable

✨ __BUILTIN_RETURN_ADDRESS(3) ✨

okay bestie, so this one's a page from the Library Functions Manual and it's all about two little GNU compiler built-ins that let you peek at the call stack 🫢. Buckle up, it's giving "intro to stack frames but make it cute" πŸ’…


🌸 NAME 🌸

__builtin_return_address β€” literally just: the return address of a function πŸ’–


πŸŽ€ SYNOPSIS πŸŽ€

void *
__builtin_return_address(unsigned int level);

void *
__builtin_frame_address(unsigned int level);

πŸ¦‹ DESCRIPTION πŸ¦‹

So __builtin_return_address() is a GNU extension β€” aka not standard C, it's a GCC thing β€” and what it does is hand you the return address of the current function, or of one of the callers up the chain. Iconic, honestly 🌈

The parameter level is the vibe check for how many frames you wanna scan up the call stack:

  • 0 β†’ you get the address of the current function
  • 1 β†’ you get the address of the caller of the current function
  • 2 β†’ you get the address of the caller's caller
  • and so on, forever, until you run out of stack 🌸

If you've hit the top of the call stack, the function returns 0. Also important, bestie: on some architectures it's only possible to determine the address of the current function. In those cases it also returns 0. So like, it's usually safe to only use the value 0 for level β€” no cap, that's the move πŸ™ˆ

Then there's __builtin_frame_address(), which behaves similarly but returns the address of the function frame instead of the function's return address. Same energy, different address πŸ’«


⭐ SEE ALSO ⭐


πŸ’€ CAVEATS πŸ’€

These are non-standard, compiler-specific extensions. Periodt. Don't expect your other compilers to know what you're talking about 😭


fr fr read the caveats again before you ship anything with these in it πŸ‘‘

and that's the whole page, bestie!! written by the Debian girlies, dated December 19, 2010 β€” oldie but a goodie, and this manual page is forever __BUILTIN_RETURN_ADDRESS(3) coded πŸ’œπŸŽ€βœ¨

πŸ“¦ where this page comes from