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