dladdr - Linux


Overview

dladdr provides information about the subroutine that contains a given address in the executing program. It is used for debugging or profiling purposes, and for getting information about function addresses.

Syntax

dladdr(addr, info)

Where:

  • addr: Address to get information about (integer)
  • info: A Dl_info structure to fill with the address information (pointer)

Options/Flags

None

Examples

Get the subroutine name and address for the current program location:

#include <dlfcn.h>
...
Dl_info info;
int addr = __builtin_return_address(0);
int status = dladdr(addr, &info);
printf("Function name: %s, Address: %p\n", info.dli_sname, info.dli_saddr);

Common Issues

  • Null pointer exception: If the info argument is null, dladdr will return -1 and set errno to EINVAL.
  • Invalid address: If the addr argument is invalid, dladdr will return -1 and set errno to EFAULT.

Integration

dladdr can be used with other Linux commands and tools for advanced tasks. For example, it can be used to get the address of a symbol in a shared library:

nm -A library.so | grep symbol_name | dladdr

Related Commands

  • nm: Displays symbols in object files
  • ldd: Displays shared library dependencies
  • gprof: Generates profile data from a program