[ovs-dev] [PATCH 02/18] deal with platforms where backtrace() is in a different library than libc.

YAMAMOTO Takashi yamamoto at valinux.co.jp
Fri Feb 1 02:25:35 UTC 2013


> On Thu, Jan 31, 2013 at 07:49:36PM +0900, YAMAMOTO Takashi wrote:
>> From: YAMAMOTO Takashi <yamt at mwd.biglobe.ne.jp>
>> execinfo for NetBSD and ubacktrace for uClibc.
>> i don't know if the latter is relevant to Open vSwitch, though.
>> Signed-off-by: YAMAMOTO Takashi <yamamoto at valinux.co.jp>
> I didn't know that backtrace() was more widely available than glibc.
> Does this patch mean that NetBSD makes a backtrace function matching the
> following description available via libbacktrace?  (I can see from the
> web that this is true for uclibc via libubacktrace.)

NetBSD-current has the function in libexecinfo and it seems reasonably
compatible.  (see the end of this mail)


>  -- Function: int backtrace (void **BUFFER, int SIZE)
>      The `backtrace' function obtains a backtrace for the current
>      thread, as a list of pointers, and places the information into
>      BUFFER.  The argument SIZE should be the number of `void *'
>      elements that will fit into BUFFER.  The return value is the
>      actual number of entries of BUFFER that are obtained, and is at
>      most SIZE.
>      The pointers placed in BUFFER are actually return addresses
>      obtained by inspecting the stack, one return address per stack
>      frame.
>      Note that certain compiler optimizations may interfere with
>      obtaining a valid backtrace.  Function inlining causes the inlined
>      function to not have a stack frame; tail call optimization
>      replaces one stack frame with another; frame pointer elimination
>      will stop `backtrace' from interpreting the stack contents
>      correctly.
> Thanks,
> Ben.

BACKTRACE(3)		   Library Functions Manual		  BACKTRACE(3)

     backtrace -- fill in the backtrace of the currently executing thread

     Backtrace Information Library (libexecinfo, -lexecinfo)

     #include <execinfo.h>

     backtrace(void **addrlist, size_t len);

     char **
     backtrace_symbols(void * const *addrlist, size_t len);

     backtrace_symbols_fd(void * const *addrlist, size_t len, int fd);

     char **
     backtrace_symbols_fmt(void * const *addrlist, size_t len, const char

     backtrace_symbols_fmt_fd(void * const *addrlist, size_t len, const char
     *fmt, int fd);

     The backtrace() function places into the array pointed by addrlist the
     array of the values of the program counter for each frame called up to
     len frames.  The number of frames found (which can be fewer than len) is

     The backtrace_symbols_fmt() function takes an array of previously filled
     addresses from backtrace() in addrlist of len elements, and uses fmt to
     format them.  The formatting characters available are:

	   a  The numeric address of each element as would be printed using

	   n  The name of the nearest function symbol (smaller than the
	      address element) as determined by dladdr(3) if the symbol was
	      dynamic, or looked up in the executable if static and the /proc
	      filesystem is available to determine the executable path.

	   d  The difference of the symbol address and the address element
	      printed using 0x%tx.

	   D  The difference of the symbol addresss and the address element
	      printed using +0x%tx if non-zero, or nothing if zero.

	   f  The filename of the symbol as determined by dladdr(3).

     The array of formatted strings is returned as a contiguous memory address
     which can be freed by a single free(3).

     The backtrace_symbols() function is equivalent of calling
     backtrace_symbols_fmt() with a format argument of %a <%n%D> at %f

     The backtrace_symbols_fd() and backtrace_symbols_fmt_fd() are similar to
     the non _fd named functions, only instead of returning an array or
     strings, they print a new-line separated array of strings in fd, and
     return 0 on success and -1 on failure.

     The backtrace() function returns the number of elements tht were filled
     in the backtrace.	The backtrace_symbols() and backtrace_symbols_fmt()
     return a string array on success, and NULL on failure, setting errno.
     Diagnostic output may also be produced by the ELF symbol lookup

     dladdr(3), elf(3)

     The backtrace() library of functions first appeared in NetBSD 7.0.

     1.	  Errors should not be printed but communicated to the caller

     2.	  Because these functions use elf(3) this is a separate library
	  instead of being part of libc/libutil so that no library
	  dependencies are introduced.

     3.	  The Linux versions of the functions (there are no _fmt variants) use
	  int instead of size_t arguments.

     4.	  Since dladdr(3) only deals with dynamic symbols, we need to find the
	  symbols from the main portion of the program.	 For that we need to
	  locate the executable, and we use procfs for finding it, which is
	  not portable.

NetBSD 6.99.10			 May 26, 2012			NetBSD 6.99.10

More information about the dev mailing list