Blame manual/debug.texi

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