1f357c00bSEd Maste.\" $NetBSD: backtrace.3,v 1.5 2013/08/22 17:08:43 christos Exp $ 2b32840d0SEd Maste.\" $FreeBSD$ 3287472b3SEd Maste.\" 4287472b3SEd Maste.\" Copyright (c) 2012 The NetBSD Foundation, Inc. 5287472b3SEd Maste.\" All rights reserved. 6287472b3SEd Maste.\" 7287472b3SEd Maste.\" This code is derived from software contributed to The NetBSD Foundation 8287472b3SEd Maste.\" by Christos Zoulas 9287472b3SEd Maste.\" 10287472b3SEd Maste.\" Redistribution and use in source and binary forms, with or without 11287472b3SEd Maste.\" modification, are permitted provided that the following conditions 12287472b3SEd Maste.\" are met: 13287472b3SEd Maste.\" 1. Redistributions of source code must retain the above copyright 14287472b3SEd Maste.\" notice, this list of conditions and the following disclaimer. 15287472b3SEd Maste.\" 2. Redistributions in binary form must reproduce the above copyright 16287472b3SEd Maste.\" notice, this list of conditions and the following disclaimer in the 17287472b3SEd Maste.\" documentation and/or other materials provided with the distribution. 18287472b3SEd Maste.\" 19287472b3SEd Maste.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20287472b3SEd Maste.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21287472b3SEd Maste.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22287472b3SEd Maste.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23287472b3SEd Maste.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24287472b3SEd Maste.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25287472b3SEd Maste.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26287472b3SEd Maste.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27287472b3SEd Maste.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28287472b3SEd Maste.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29287472b3SEd Maste.\" POSSIBILITY OF SUCH DAMAGE. 30287472b3SEd Maste.\" 31*d8eb75b4SBaptiste Daroussin.Dd December 12, 2015 32287472b3SEd Maste.Dt BACKTRACE 3 33287472b3SEd Maste.Os 34287472b3SEd Maste.Sh NAME 35287472b3SEd Maste.Nm backtrace 36287472b3SEd Maste.Nd fill in the backtrace of the currently executing thread 37287472b3SEd Maste.Sh LIBRARY 38287472b3SEd Maste.Lb libexecinfo 39287472b3SEd Maste.Sh SYNOPSIS 40287472b3SEd Maste.In execinfo.h 41287472b3SEd Maste.Ft size_t 42287472b3SEd Maste.Fn backtrace "void **addrlist" "size_t len" 43287472b3SEd Maste.Ft "char **" 44287472b3SEd Maste.Fn backtrace_symbols "void * const *addrlist" "size_t len" 45287472b3SEd Maste.Ft int 46287472b3SEd Maste.Fn backtrace_symbols_fd "void * const *addrlist" "size_t len" "int fd" 47287472b3SEd Maste.Ft "char **" 48287472b3SEd Maste.Fn backtrace_symbols_fmt "void * const *addrlist" "size_t len" "const char *fmt" 49287472b3SEd Maste.Ft int 50*d8eb75b4SBaptiste Daroussin.Fn backtrace_symbols_fd_fmt "void * const *addrlist" "size_t len" "int fd" "const char *fmt" 51287472b3SEd Maste.Sh DESCRIPTION 52287472b3SEd MasteThe 53287472b3SEd Maste.Fn backtrace 54287472b3SEd Mastefunction places into the array pointed by 55287472b3SEd Maste.Fa addrlist 56287472b3SEd Mastethe array of the values of the program counter for each frame called up to 57287472b3SEd Maste.Fa len 58287472b3SEd Masteframes. 59287472b3SEd MasteThe number of frames found (which can be fewer than 60287472b3SEd Maste.Fa len ) 61287472b3SEd Masteis returned. 62287472b3SEd Maste.Pp 63287472b3SEd MasteThe 64287472b3SEd Maste.Fn backtrace_symbols_fmt 65287472b3SEd Mastefunction takes an array of previously filled addresses from 66287472b3SEd Maste.Fn backtrace 67287472b3SEd Mastein 68287472b3SEd Maste.Fa addrlist 69287472b3SEd Masteof 70287472b3SEd Maste.Fa len 71287472b3SEd Masteelements, and uses 72287472b3SEd Maste.Fa fmt 73287472b3SEd Masteto format them. 74287472b3SEd MasteThe formatting characters available are: 75287472b3SEd Maste.Bl -tag -width a -offset indent 76287472b3SEd Maste.It Dv a 77287472b3SEd MasteThe numeric address of each element as would be printed using %p. 78287472b3SEd Maste.It Dv n 79287472b3SEd MasteThe name of the nearest function symbol (smaller than the address element) 80287472b3SEd Masteas determined by 81287472b3SEd Maste.Xr dladdr 3 82287472b3SEd Masteif the symbol was dynamic, or looked up in the executable if static and 83287472b3SEd Mastethe /proc filesystem is available to determine the executable path. 84287472b3SEd Maste.It Dv d 85287472b3SEd MasteThe difference of the symbol address and the address element printed 86287472b3SEd Masteusing 0x%tx. 87287472b3SEd Maste.It Dv D 88*d8eb75b4SBaptiste DaroussinThe difference of the symbol address and the address element printed using 89287472b3SEd Maste+0x%tx if non-zero, or nothing if zero. 90287472b3SEd Maste.It Dv f 91287472b3SEd MasteThe filename of the symbol as determined by 92287472b3SEd Maste.Xr dladdr 3 . 93287472b3SEd Maste.El 94287472b3SEd Maste.Pp 95287472b3SEd MasteThe array of formatted strings is returned as a contiguous memory address which 96287472b3SEd Mastecan be freed by a single 97287472b3SEd Maste.Xr free 3 . 98287472b3SEd Maste.Pp 99287472b3SEd MasteThe 100287472b3SEd Maste.Fn backtrace_symbols 101287472b3SEd Mastefunction is equivalent of calling 102287472b3SEd Maste.Fn backtrace_symbols_fmt 103287472b3SEd Mastewith a format argument of 104287472b3SEd Maste.Dv "%a <%n%D> at %f" 105287472b3SEd Maste.Pp 106287472b3SEd MasteThe 107287472b3SEd Maste.Fn backtrace_symbols_fd 108287472b3SEd Masteand 109458b591fSBaptiste Daroussin.Fn backtrace_symbols_fd_fmt 110287472b3SEd Masteare similar to the non _fd named functions, only instead of returning 111287472b3SEd Mastean array or strings, they print a new-line separated array of strings in 112287472b3SEd Mastefd, and return 113287472b3SEd Maste.Dv 0 114287472b3SEd Masteon success and 115287472b3SEd Maste.Dv \-1 116287472b3SEd Masteon failure. 117287472b3SEd Maste.Sh RETURN VALUES 118287472b3SEd MasteThe 119287472b3SEd Maste.Fn backtrace 120b32840d0SEd Mastefunction returns the number of elements that were filled in the backtrace. 121287472b3SEd MasteThe 122287472b3SEd Maste.Fn backtrace_symbols 123287472b3SEd Masteand 124287472b3SEd Maste.Fn backtrace_symbols_fmt 125287472b3SEd Mastereturn a string array on success, and 126287472b3SEd Maste.Dv NULL 127287472b3SEd Masteon failure, setting 128287472b3SEd Maste.Va errno . 129287472b3SEd MasteDiagnostic output may also be produced by the ELF symbol lookup functions. 130287472b3SEd Maste.Sh SEE ALSO 131287472b3SEd Maste.Xr dladdr 3 , 132287472b3SEd Maste.Xr elf 3 133287472b3SEd Maste.Sh HISTORY 134287472b3SEd MasteThe 135287472b3SEd Maste.Fn backtrace 136287472b3SEd Mastelibrary of functions first appeared in 137b32840d0SEd Maste.Nx 7.0 138b32840d0SEd Masteand 139b32840d0SEd Maste.Fx 10.0 . 140287472b3SEd Maste.Sh BUGS 141287472b3SEd Maste.Bl -enum 142287472b3SEd Maste.It 143287472b3SEd MasteErrors should not be printed but communicated to the caller differently. 144287472b3SEd Maste.It 145287472b3SEd MasteBecause these functions use 146287472b3SEd Maste.Xr elf 3 147287472b3SEd Mastethis is a separate library instead of being part of libc/libutil 148287472b3SEd Masteso that no library dependencies are introduced. 149287472b3SEd Maste.It 150287472b3SEd MasteThe Linux versions of the functions (there are no _fmt variants) use 151287472b3SEd Maste.Ft int 152287472b3SEd Masteinstead of 153287472b3SEd Maste.Ft size_t 154287472b3SEd Mastearguments. 155287472b3SEd Maste.El 156