Blame manual/debug.texi

Packit 6c4009
@node Debugging Support
Packit 6c4009
@c @node Debugging Support, Threads, Cryptographic Functions, Top
Packit 6c4009
@c %MENU% Functions to help debugging applications
Packit 6c4009
@chapter Debugging support
Packit 6c4009
Packit 6c4009
Applications are usually debugged using dedicated debugger programs.
Packit 6c4009
But sometimes this is not possible and, in any case, it is useful to
Packit 6c4009
provide the developer with as much information as possible at the time
Packit 6c4009
the problems are experienced.  For this reason a few functions are
Packit 6c4009
provided which a program can use to help the developer more easily
Packit 6c4009
locate the problem.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Backtraces::                Obtaining and printing a back trace of the
Packit 6c4009
                               current stack.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Backtraces, , , Debugging Support
Packit 6c4009
@section Backtraces
Packit 6c4009
Packit 6c4009
@cindex backtrace
Packit 6c4009
@cindex backtrace_symbols
Packit 6c4009
@cindex backtrace_fd
Packit 6c4009
A @dfn{backtrace} is a list of the function calls that are currently
Packit 6c4009
active in a thread.  The usual way to inspect a backtrace of a program
Packit 6c4009
is to use an external debugger such as gdb.  However, sometimes it is
Packit 6c4009
useful to obtain a backtrace programmatically from within a program,
Packit 6c4009
e.g., for the purposes of logging or diagnostics.
Packit 6c4009
Packit 6c4009
The header file @file{execinfo.h} declares three functions that obtain
Packit 6c4009
and manipulate backtraces of the current thread.
Packit 6c4009
@pindex execinfo.h
Packit 6c4009
Packit 6c4009
@deftypefun int backtrace (void **@var{buffer}, int @var{size})
Packit 6c4009
@standards{GNU, execinfo.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asuinit{} @ascuheap{} @ascudlopen{} @ascuplugin{} @asulock{}}@acunsafe{@acuinit{} @acsmem{} @aculock{} @acsfd{}}}
Packit 6c4009
@c The generic implementation just does pointer chasing within the local
Packit 6c4009
@c stack, without any guarantees that this will handle signal frames
Packit 6c4009
@c correctly, so it's AS-Unsafe to begin with.  However, most (all?)
Packit 6c4009
@c arches defer to libgcc_s's _Unwind_* implementation, dlopening
Packit 6c4009
@c libgcc_s.so to that end except in a static version of libc.
Packit 6c4009
@c libgcc_s's implementation may in turn defer to libunwind.  We can't
Packit 6c4009
@c assume those implementations are AS- or AC-safe, but even if we
Packit 6c4009
@c could, our own initialization path isn't, and libgcc's implementation
Packit 6c4009
@c calls malloc and performs internal locking, so...
Packit 6c4009
The @code{backtrace} function obtains a backtrace for the current
Packit 6c4009
thread, as a list of pointers, and places the information into
Packit 6c4009
@var{buffer}.  The argument @var{size} should be the number of
Packit 6c4009
@w{@code{void *}} elements that will fit into @var{buffer}.  The return
Packit 6c4009
value is the actual number of entries of @var{buffer} that are obtained,
Packit 6c4009
and is at most @var{size}.
Packit 6c4009
Packit 6c4009
The pointers placed in @var{buffer} are actually return addresses
Packit 6c4009
obtained by inspecting the stack, one return address per stack frame.
Packit 6c4009
Packit 6c4009
Note that certain compiler optimizations may interfere with obtaining a
Packit 6c4009
valid backtrace.  Function inlining causes the inlined function to not
Packit 6c4009
have a stack frame; tail call optimization replaces one stack frame with
Packit 6c4009
another; frame pointer elimination will stop @code{backtrace} from
Packit 6c4009
interpreting the stack contents correctly.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char **} backtrace_symbols (void *const *@var{buffer}, int @var{size})
Packit 6c4009
@standards{GNU, execinfo.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @aculock{}}}
Packit 6c4009
@c Collects info returned by _dl_addr in an auto array, allocates memory
Packit 6c4009
@c for the whole return buffer with malloc then sprintfs into it storing
Packit 6c4009
@c pointers to the strings into the array entries in the buffer.
Packit 6c4009
@c _dl_addr takes the recursive dl_load_lock then calls
Packit 6c4009
@c _dl_find_dso_for_object and determine_info.
Packit 6c4009
@c _dl_find_dso_for_object calls _dl-addr_inside_object.
Packit 6c4009
@c All of them are safe as long as the lock is held.
Packit 6c4009
@c @asucorrupt?  It doesn't look like the dynamic loader's data
Packit 6c4009
@c structures could be in an inconsistent state that would cause
Packit 6c4009
@c malfunction here.
Packit 6c4009
The @code{backtrace_symbols} function translates the information
Packit 6c4009
obtained from the @code{backtrace} function into an array of strings.
Packit 6c4009
The argument @var{buffer} should be a pointer to an array of addresses
Packit 6c4009
obtained via the @code{backtrace} function, and @var{size} is the number
Packit 6c4009
of entries in that array (the return value of @code{backtrace}).
Packit 6c4009
Packit 6c4009
The return value is a pointer to an array of strings, which has
Packit 6c4009
@var{size} entries just like the array @var{buffer}.  Each string
Packit 6c4009
contains a printable representation of the corresponding element of
Packit 6c4009
@var{buffer}.  It includes the function name (if this can be
Packit 6c4009
determined), an offset into the function, and the actual return address
Packit 6c4009
(in hexadecimal).
Packit 6c4009
Packit 6c4009
Currently, the function name and offset can only be obtained on systems that
Packit 6c4009
use the ELF binary format for programs and libraries.  On other systems,
Packit 6c4009
only the hexadecimal return address will be present.  Also, you may need
Packit 6c4009
to pass additional flags to the linker to make the function names
Packit 6c4009
available to the program.  (For example, on systems using GNU ld, you
Packit 6c4009
must pass @code{-rdynamic}.)
Packit 6c4009
Packit 6c4009
The return value of @code{backtrace_symbols} is a pointer obtained via
Packit 6c4009
the @code{malloc} function, and it is the responsibility of the caller
Packit 6c4009
to @code{free} that pointer.  Note that only the return value need be
Packit 6c4009
freed, not the individual strings.
Packit 6c4009
Packit 6c4009
The return value is @code{NULL} if sufficient memory for the strings
Packit 6c4009
cannot be obtained.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void backtrace_symbols_fd (void *const *@var{buffer}, int @var{size}, int @var{fd})
Packit 6c4009
@standards{GNU, execinfo.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
Packit 6c4009
@c Single loop of _dl_addr over addresses, collecting info into an iovec
Packit 6c4009
@c written out with a writev call per iteration.  Addresses and offsets
Packit 6c4009
@c are converted to hex in auto buffers, so the only potential issue
Packit 6c4009
@c here is leaking the dl lock in case of cancellation.
Packit 6c4009
The @code{backtrace_symbols_fd} function performs the same translation
Packit 6c4009
as the function @code{backtrace_symbols} function.  Instead of returning
Packit 6c4009
the strings to the caller, it writes the strings to the file descriptor
Packit 6c4009
@var{fd}, one per line.  It does not use the @code{malloc} function, and
Packit 6c4009
can therefore be used in situations where that function might fail.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The following program illustrates the use of these functions.  Note that
Packit 6c4009
the array to contain the return addresses returned by @code{backtrace}
Packit 6c4009
is allocated on the stack.  Therefore code like this can be used in
Packit 6c4009
situations where the memory handling via @code{malloc} does not work
Packit 6c4009
anymore (in which case the @code{backtrace_symbols} has to be replaced
Packit 6c4009
by a @code{backtrace_symbols_fd} call as well).  The number of return
Packit 6c4009
addresses is normally not very large.  Even complicated programs rather
Packit 6c4009
seldom have a nesting level of more than, say, 50 and with 200 possible
Packit 6c4009
entries probably all programs should be covered.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include execinfo.c.texi
Packit 6c4009
@end smallexample