1*84d9c625SLionel Sambuc.\" $NetBSD: backtrace.3,v 1.5 2013/08/22 17:08:43 christos Exp $ 2*84d9c625SLionel Sambuc.\" 3*84d9c625SLionel Sambuc.\" Copyright (c) 2012 The NetBSD Foundation, Inc. 4*84d9c625SLionel Sambuc.\" All rights reserved. 5*84d9c625SLionel Sambuc.\" 6*84d9c625SLionel Sambuc.\" This code is derived from software contributed to The NetBSD Foundation 7*84d9c625SLionel Sambuc.\" by Christos Zoulas 8*84d9c625SLionel Sambuc.\" 9*84d9c625SLionel Sambuc.\" Redistribution and use in source and binary forms, with or without 10*84d9c625SLionel Sambuc.\" modification, are permitted provided that the following conditions 11*84d9c625SLionel Sambuc.\" are met: 12*84d9c625SLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright 13*84d9c625SLionel Sambuc.\" notice, this list of conditions and the following disclaimer. 14*84d9c625SLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright 15*84d9c625SLionel Sambuc.\" notice, this list of conditions and the following disclaimer in the 16*84d9c625SLionel Sambuc.\" documentation and/or other materials provided with the distribution. 17*84d9c625SLionel Sambuc.\" 18*84d9c625SLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19*84d9c625SLionel Sambuc.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20*84d9c625SLionel Sambuc.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21*84d9c625SLionel Sambuc.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22*84d9c625SLionel Sambuc.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*84d9c625SLionel Sambuc.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*84d9c625SLionel Sambuc.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*84d9c625SLionel Sambuc.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*84d9c625SLionel Sambuc.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*84d9c625SLionel Sambuc.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*84d9c625SLionel Sambuc.\" POSSIBILITY OF SUCH DAMAGE. 29*84d9c625SLionel Sambuc.\" 30*84d9c625SLionel Sambuc.Dd May 26, 2012 31*84d9c625SLionel Sambuc.Dt BACKTRACE 3 32*84d9c625SLionel Sambuc.Os 33*84d9c625SLionel Sambuc.Sh NAME 34*84d9c625SLionel Sambuc.Nm backtrace 35*84d9c625SLionel Sambuc.Nd fill in the backtrace of the currently executing thread 36*84d9c625SLionel Sambuc.Sh LIBRARY 37*84d9c625SLionel Sambuc.Lb libexecinfo 38*84d9c625SLionel Sambuc.Sh SYNOPSIS 39*84d9c625SLionel Sambuc.In execinfo.h 40*84d9c625SLionel Sambuc.Ft size_t 41*84d9c625SLionel Sambuc.Fn backtrace "void **addrlist" "size_t len" 42*84d9c625SLionel Sambuc.Ft "char **" 43*84d9c625SLionel Sambuc.Fn backtrace_symbols "void * const *addrlist" "size_t len" 44*84d9c625SLionel Sambuc.Ft int 45*84d9c625SLionel Sambuc.Fn backtrace_symbols_fd "void * const *addrlist" "size_t len" "int fd" 46*84d9c625SLionel Sambuc.Ft "char **" 47*84d9c625SLionel Sambuc.Fn backtrace_symbols_fmt "void * const *addrlist" "size_t len" "const char *fmt" 48*84d9c625SLionel Sambuc.Ft int 49*84d9c625SLionel Sambuc.Fn backtrace_symbols_fmt_fd "void * const *addrlist" "size_t len" "const char *fmt" "int fd" 50*84d9c625SLionel Sambuc.Sh DESCRIPTION 51*84d9c625SLionel SambucThe 52*84d9c625SLionel Sambuc.Fn backtrace 53*84d9c625SLionel Sambucfunction places into the array pointed by 54*84d9c625SLionel Sambuc.Fa addrlist 55*84d9c625SLionel Sambucthe array of the values of the program counter for each frame called up to 56*84d9c625SLionel Sambuc.Fa len 57*84d9c625SLionel Sambucframes. 58*84d9c625SLionel SambucThe number of frames found (which can be fewer than 59*84d9c625SLionel Sambuc.Fa len ) 60*84d9c625SLionel Sambucis returned. 61*84d9c625SLionel Sambuc.Pp 62*84d9c625SLionel SambucThe 63*84d9c625SLionel Sambuc.Fn backtrace_symbols_fmt 64*84d9c625SLionel Sambucfunction takes an array of previously filled addresses from 65*84d9c625SLionel Sambuc.Fn backtrace 66*84d9c625SLionel Sambucin 67*84d9c625SLionel Sambuc.Fa addrlist 68*84d9c625SLionel Sambucof 69*84d9c625SLionel Sambuc.Fa len 70*84d9c625SLionel Sambucelements, and uses 71*84d9c625SLionel Sambuc.Fa fmt 72*84d9c625SLionel Sambucto format them. 73*84d9c625SLionel SambucThe formatting characters available are: 74*84d9c625SLionel Sambuc.Bl -tag -width a -offset indent 75*84d9c625SLionel Sambuc.It Dv a 76*84d9c625SLionel SambucThe numeric address of each element as would be printed using %p. 77*84d9c625SLionel Sambuc.It Dv n 78*84d9c625SLionel SambucThe name of the nearest function symbol (smaller than the address element) 79*84d9c625SLionel Sambucas determined by 80*84d9c625SLionel Sambuc.Xr dladdr 3 81*84d9c625SLionel Sambucif the symbol was dynamic, or looked up in the executable if static and 82*84d9c625SLionel Sambucthe /proc filesystem is available to determine the executable path. 83*84d9c625SLionel Sambuc.It Dv d 84*84d9c625SLionel SambucThe difference of the symbol address and the address element printed 85*84d9c625SLionel Sambucusing 0x%tx. 86*84d9c625SLionel Sambuc.It Dv D 87*84d9c625SLionel SambucThe difference of the symbol addresss and the address element printed using 88*84d9c625SLionel Sambuc+0x%tx if non-zero, or nothing if zero. 89*84d9c625SLionel Sambuc.It Dv f 90*84d9c625SLionel SambucThe filename of the symbol as determined by 91*84d9c625SLionel Sambuc.Xr dladdr 3 . 92*84d9c625SLionel Sambuc.El 93*84d9c625SLionel Sambuc.Pp 94*84d9c625SLionel SambucThe array of formatted strings is returned as a contiguous memory address which 95*84d9c625SLionel Sambuccan be freed by a single 96*84d9c625SLionel Sambuc.Xr free 3 . 97*84d9c625SLionel Sambuc.Pp 98*84d9c625SLionel SambucThe 99*84d9c625SLionel Sambuc.Fn backtrace_symbols 100*84d9c625SLionel Sambucfunction is equivalent of calling 101*84d9c625SLionel Sambuc.Fn backtrace_symbols_fmt 102*84d9c625SLionel Sambucwith a format argument of 103*84d9c625SLionel Sambuc.Dv "%a <%n%D> at %f" 104*84d9c625SLionel Sambuc.Pp 105*84d9c625SLionel SambucThe 106*84d9c625SLionel Sambuc.Fn backtrace_symbols_fd 107*84d9c625SLionel Sambucand 108*84d9c625SLionel Sambuc.Fn backtrace_symbols_fmt_fd 109*84d9c625SLionel Sambucare similar to the non _fd named functions, only instead of returning 110*84d9c625SLionel Sambucan array or strings, they print a new-line separated array of strings in 111*84d9c625SLionel Sambucfd, and return 112*84d9c625SLionel Sambuc.Dv 0 113*84d9c625SLionel Sambucon success and 114*84d9c625SLionel Sambuc.Dv \-1 115*84d9c625SLionel Sambucon failure. 116*84d9c625SLionel Sambuc.Sh RETURN VALUES 117*84d9c625SLionel SambucThe 118*84d9c625SLionel Sambuc.Fn backtrace 119*84d9c625SLionel Sambucfunction returns the number of elements that were filled in the backtrace. 120*84d9c625SLionel SambucThe 121*84d9c625SLionel Sambuc.Fn backtrace_symbols 122*84d9c625SLionel Sambucand 123*84d9c625SLionel Sambuc.Fn backtrace_symbols_fmt 124*84d9c625SLionel Sambucreturn a string array on success, and 125*84d9c625SLionel Sambuc.Dv NULL 126*84d9c625SLionel Sambucon failure, setting 127*84d9c625SLionel Sambuc.Va errno . 128*84d9c625SLionel SambucDiagnostic output may also be produced by the ELF symbol lookup functions. 129*84d9c625SLionel Sambuc.Sh SEE ALSO 130*84d9c625SLionel Sambuc.Xr dladdr 3 , 131*84d9c625SLionel Sambuc.Xr elf 3 132*84d9c625SLionel Sambuc.Sh HISTORY 133*84d9c625SLionel SambucThe 134*84d9c625SLionel Sambuc.Fn backtrace 135*84d9c625SLionel Sambuclibrary of functions first appeared in 136*84d9c625SLionel Sambuc.Nx 7.0 . 137*84d9c625SLionel Sambuc.Sh BUGS 138*84d9c625SLionel Sambuc.Bl -enum 139*84d9c625SLionel Sambuc.It 140*84d9c625SLionel SambucErrors should not be printed but communicated to the caller differently. 141*84d9c625SLionel Sambuc.It 142*84d9c625SLionel SambucBecause these functions use 143*84d9c625SLionel Sambuc.Xr elf 3 144*84d9c625SLionel Sambucthis is a separate library instead of being part of libc/libutil 145*84d9c625SLionel Sambucso that no library dependencies are introduced. 146*84d9c625SLionel Sambuc.It 147*84d9c625SLionel SambucThe Linux versions of the functions (there are no _fmt variants) use 148*84d9c625SLionel Sambuc.Ft int 149*84d9c625SLionel Sambucinstead of 150*84d9c625SLionel Sambuc.Ft size_t 151*84d9c625SLionel Sambucarguments. 152*84d9c625SLionel Sambuc.It 153*84d9c625SLionel SambucSince 154*84d9c625SLionel Sambuc.Xr dladdr 3 155*84d9c625SLionel Sambuconly deals with dynamic symbols, we need to find the symbols from the main 156*84d9c625SLionel Sambucportion of the program. 157*84d9c625SLionel SambucFor that we need to locate the executable, and we use procfs for 158*84d9c625SLionel Sambucfinding it, which is not portable. 159*84d9c625SLionel Sambuc.El 160