xref: /xnu-8796.121.2/bsd/man/man9/backtrace.9 (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions.\" Copyright (c) 2021, Apple Inc.  All rights reserved.
2*c54f35caSApple OSS Distributions.
3*c54f35caSApple OSS Distributions.Dd June 30, 2021
4*c54f35caSApple OSS Distributions.Dt BACKTRACE 9
5*c54f35caSApple OSS Distributions.Os Darwin
6*c54f35caSApple OSS Distributions.
7*c54f35caSApple OSS Distributions.Sh NAME
8*c54f35caSApple OSS Distributions.Nm backtrace ,
9*c54f35caSApple OSS Distributions.Nm backtrace_packed ,
10*c54f35caSApple OSS Distributions.Nm backtrace_user ,
11*c54f35caSApple OSS Distributions.Nd gather the PC and return addresses of a thread's kernel or user call stack
12*c54f35caSApple OSS Distributions.
13*c54f35caSApple OSS Distributions.Sh SYNOPSIS
14*c54f35caSApple OSS Distributions.In kern/backtrace.h
15*c54f35caSApple OSS Distributions.Ft unsigned int
16*c54f35caSApple OSS Distributions.Fo backtrace
17*c54f35caSApple OSS Distributions.Fa "uintptr_t *bt"
18*c54f35caSApple OSS Distributions.Fa "unsigned int btlen"
19*c54f35caSApple OSS Distributions.Fa "struct backtrace_control *ctl"
20*c54f35caSApple OSS Distributions.Fa "backtrace_info_t *info_out"
21*c54f35caSApple OSS Distributions.Fc
22*c54f35caSApple OSS Distributions.
23*c54f35caSApple OSS Distributions.Ft size_t
24*c54f35caSApple OSS Distributions.Fo backtrace_packed
25*c54f35caSApple OSS Distributions.Fa "backtrace_pack_t packing"
26*c54f35caSApple OSS Distributions.Fa "uintptr_t *bt"
27*c54f35caSApple OSS Distributions.Fa "size_t btlen"
28*c54f35caSApple OSS Distributions.Fa "struct backtrace_control *ctl"
29*c54f35caSApple OSS Distributions.Fa "backtrace_info_t *info_out"
30*c54f35caSApple OSS Distributions.Fc
31*c54f35caSApple OSS Distributions.
32*c54f35caSApple OSS Distributions.Ft unsigned int
33*c54f35caSApple OSS Distributions.Fo backtrace_user
34*c54f35caSApple OSS Distributions.Fa "uintptr_t *bt"
35*c54f35caSApple OSS Distributions.Fa "unsigned int btlen"
36*c54f35caSApple OSS Distributions.Fa "struct backtrace_control *ctl"
37*c54f35caSApple OSS Distributions.Fa "struct backtrace_user_info *info_out"
38*c54f35caSApple OSS Distributions.Fc
39*c54f35caSApple OSS Distributions.
40*c54f35caSApple OSS Distributions.Sh DESCRIPTION
41*c54f35caSApple OSS DistributionsThe
42*c54f35caSApple OSS Distributions.Nm backtrace ,
43*c54f35caSApple OSS Distributions.Nm backtrace_packed ,
44*c54f35caSApple OSS Distributionsand
45*c54f35caSApple OSS Distributions.Nm backtrace_user
46*c54f35caSApple OSS Distributionsfunctions fill a buffer with the current PC and return addresses of a thread's
47*c54f35caSApple OSS Distributionskernel and user call stack, respectively.
48*c54f35caSApple OSS DistributionsThis is only possible when frame pointers are pushed to the stack, alongside the
49*c54f35caSApple OSS Distributionsreturn addresses.
50*c54f35caSApple OSS Distributions.Xr clang 1 ,
51*c54f35caSApple OSS Distributionsdisables this behavior with the
52*c54f35caSApple OSS Distributions.Fl fomit-frame-pointer
53*c54f35caSApple OSS Distributionsflag, so it will prevent these functions from working.
54*c54f35caSApple OSS DistributionsFurthermore, leaf functions and inlined function calls can also prevent
55*c54f35caSApple OSS Distributionsbacktracing from reporting the source-level function control flow.
56*c54f35caSApple OSS Distributions.Fn backtrace_user
57*c54f35caSApple OSS Distributionsoperates on user call stacks, while
58*c54f35caSApple OSS Distributions.Fn backtrace
59*c54f35caSApple OSS Distributionscaptures the current kernel call stack.
60*c54f35caSApple OSS Distributions.Fn backtrace_packed
61*c54f35caSApple OSS Distributionswrites a more compact representation of the return addresses to a buffer, which
62*c54f35caSApple OSS Distributionscan be unpacked with
63*c54f35caSApple OSS Distributions.Xr backtrace_unpack 9 .
64*c54f35caSApple OSS DistributionsCalling
65*c54f35caSApple OSS Distributions.Fn backtrace_user
66*c54f35caSApple OSS Distributionson a kernel thread
67*c54f35caSApple OSS Distributions.Pq which lacks a user context
68*c54f35caSApple OSS Distributionsis undefined.
69*c54f35caSApple OSS Distributions.Pp
70*c54f35caSApple OSS DistributionsUp to
71*c54f35caSApple OSS Distributions.Fa btlen
72*c54f35caSApple OSS Distributionsinstruction addresses
73*c54f35caSApple OSS Distributions.Po
74*c54f35caSApple OSS Distributionsor
75*c54f35caSApple OSS Distributions.Fa btsize
76*c54f35caSApple OSS Distributionsbytes for
77*c54f35caSApple OSS Distributions.Fn backtrace_packed
78*c54f35caSApple OSS Distributions.Pc
79*c54f35caSApple OSS Distributionsare written to the buffer at
80*c54f35caSApple OSS Distributions.Fa bt .
81*c54f35caSApple OSS DistributionsThese functions also accept
82*c54f35caSApple OSS Distributions.Fa ctl
83*c54f35caSApple OSS Distributionsand
84*c54f35caSApple OSS Distributions.Fa info_out
85*c54f35caSApple OSS Distributionsarguments, described in
86*c54f35caSApple OSS Distributions.Sx BACKTRACE_CONTROL
87*c54f35caSApple OSS Distributionsand
88*c54f35caSApple OSS Distributions.Sx BACKTRACE_INFO ,
89*c54f35caSApple OSS Distributionsrespectively.
90*c54f35caSApple OSS Distributions.Fn backtrace_packed
91*c54f35caSApple OSS Distributionstakes a
92*c54f35caSApple OSS Distributions.Ft backtrace_pack_t
93*c54f35caSApple OSS Distributionsto control which packing scheme to use.
94*c54f35caSApple OSS Distributions.Pp
95*c54f35caSApple OSS Distributions.Fn backtrace
96*c54f35caSApple OSS Distributionsrecords the kernel PC and call stack of the current thread.
97*c54f35caSApple OSS Distributions.Pp
98*c54f35caSApple OSS Distributions.Fn backtrace_packed
99*c54f35caSApple OSS Distributionsrecords the kernel PC and call stack of the current thread in a buffer in a
100*c54f35caSApple OSS Distributionscompact representation.
101*c54f35caSApple OSS DistributionsSee
102*c54f35caSApple OSS Distributions.Xr backtrace_pack 9
103*c54f35caSApple OSS Distributionsfor a description of the supported formats.
104*c54f35caSApple OSS Distributions.Pp
105*c54f35caSApple OSS Distributions.Fn backtrace_user
106*c54f35caSApple OSS Distributionsrecords the user PC and call stack of the current thread, which must be
107*c54f35caSApple OSS Distributionsassociated with a user space task.
108*c54f35caSApple OSS Distributions.
109*c54f35caSApple OSS Distributions.Sh RETURN VALUES
110*c54f35caSApple OSS DistributionsThe
111*c54f35caSApple OSS Distributions.Nm
112*c54f35caSApple OSS Distributionsfunctions return the number of PC and return address elements
113*c54f35caSApple OSS Distributions.Po
114*c54f35caSApple OSS Distributionsor bytes for
115*c54f35caSApple OSS Distributions.Fn backtrace_packed
116*c54f35caSApple OSS Distributions.Pc
117*c54f35caSApple OSS Distributionswritten to the provided buffer.
118*c54f35caSApple OSS DistributionsIf there is space, the buffer is terminated with a NULL entry
119*c54f35caSApple OSS Distributions.Po
120*c54f35caSApple OSS Distributionsexcept for
121*c54f35caSApple OSS Distributions.Fn backtrace_packed
122*c54f35caSApple OSS Distributions.Pc .
123*c54f35caSApple OSS DistributionsThe
124*c54f35caSApple OSS Distributions.Fa info_out
125*c54f35caSApple OSS Distributionsargument will be set with information about the provided call stack.
126*c54f35caSApple OSS Distributions.Fn backtrace_user
127*c54f35caSApple OSS Distributionswill set
128*c54f35caSApple OSS Distributions.Ft btui_error
129*c54f35caSApple OSS Distributionsto an error of the
130*c54f35caSApple OSS Distributions.Xr copyin 9
131*c54f35caSApple OSS Distributionsroutine if an error occurred during call stack traversal.
132*c54f35caSApple OSS Distributions.
133*c54f35caSApple OSS Distributions.Sh BACKTRACE_CONTROL
134*c54f35caSApple OSS DistributionsThe
135*c54f35caSApple OSS Distributions.Nm
136*c54f35caSApple OSS Distributionsfunctions accept a
137*c54f35caSApple OSS Distributions.Ft struct backtrace_control
138*c54f35caSApple OSS Distributionscontrol argument to alter their behavior,
139*c54f35caSApple OSS Distributionswith the following fields:
140*c54f35caSApple OSS Distributions.Bl -tag -width btc_user_thread
141*c54f35caSApple OSS Distributions.It Ft btc_flags
142*c54f35caSApple OSS DistributionsThese flags control the backtracer's behavior:
143*c54f35caSApple OSS Distributions.Bl -tag -width BTF_KERN_INTERRUPTED
144*c54f35caSApple OSS Distributions.It Dv BTF_KERN_INTERRUPTED
145*c54f35caSApple OSS DistributionsFor
146*c54f35caSApple OSS Distributions.Fn backtrace
147*c54f35caSApple OSS Distributionsonly, record the PC and return addresses of the interrupted call stack.
148*c54f35caSApple OSS Distributions.El
149*c54f35caSApple OSS Distributions.It Ft btc_frame_addr
150*c54f35caSApple OSS DistributionsStart backtracing from the provided frame address.
151*c54f35caSApple OSS Distributions.It Ft btc_user_thread
152*c54f35caSApple OSS DistributionsCapture the backtrace of the provided thread pointer.
153*c54f35caSApple OSS DistributionsThis must be either the current thread or a different thread that is suspended
154*c54f35caSApple OSS Distributionsand unable to run in user space.
155*c54f35caSApple OSS Distributions.It Ft btc_user_copy
156*c54f35caSApple OSS DistributionsFor
157*c54f35caSApple OSS Distributions.Fn backtrace_user
158*c54f35caSApple OSS Distributionsonly, the function to use instead of
159*c54f35caSApple OSS Distributions.Xr copyin 9
160*c54f35caSApple OSS Distributionsto copy data from the thread's user space virtual address space into the kernel.
161*c54f35caSApple OSS Distributions.It Ft btc_user_copy_context
162*c54f35caSApple OSS DistributionsAdditional data that's passed to the custom copy routine to act as private
163*c54f35caSApple OSS Distributionscontext.
164*c54f35caSApple OSS Distributions.El
165*c54f35caSApple OSS Distributions.
166*c54f35caSApple OSS Distributions.Sh BACKTRACE_INFO
167*c54f35caSApple OSS DistributionsThe
168*c54f35caSApple OSS Distributions.Nm
169*c54f35caSApple OSS Distributionsfunctions report additional information through a
170*c54f35caSApple OSS Distributions.Ft backtrace_info_t
171*c54f35caSApple OSS Distributionsflags out-parameter,
172*c54f35caSApple OSS Distributionswith the following options:
173*c54f35caSApple OSS Distributions.Bl -tag -width BTI_TRUNCATED
174*c54f35caSApple OSS Distributions.It Dv BTI_64_BIT
175*c54f35caSApple OSS DistributionsThe PC and call stack return addresses are 64-bit quantities.
176*c54f35caSApple OSS Distributions.It Dv BTI_TRUNCATED
177*c54f35caSApple OSS DistributionsThe backtrace has been truncated and does not terminate with the base frame.
178*c54f35caSApple OSS Distributions.El
179*c54f35caSApple OSS Distributions.Pp
180*c54f35caSApple OSS DistributionsThe
181*c54f35caSApple OSS Distributions.Fn backtrace_user
182*c54f35caSApple OSS Distributionsvariant uses an out-parameter structure
183*c54f35caSApple OSS Distributions.Ft struct backtrace_user_info
184*c54f35caSApple OSS Distributionsto return additional context:
185*c54f35caSApple OSS Distributions.Bl -tag -width btui_
186*c54f35caSApple OSS Distributions.It Ft btui_info
187*c54f35caSApple OSS DistributionsThe
188*c54f35caSApple OSS Distributions.Ft backtrace_info_t
189*c54f35caSApple OSS Distributionsflags, described above.
190*c54f35caSApple OSS Distributions.It Ft btui_error
191*c54f35caSApple OSS DistributionsAny error encountered while copying data.
192*c54f35caSApple OSS Distributions.It Ft btui_async_start_index
193*c54f35caSApple OSS DistributionsFor Swift continuations
194*c54f35caSApple OSS Distributions.Pq async stacks ,
195*c54f35caSApple OSS Distributionsthe location where the continuation hint was found and where it logically
196*c54f35caSApple OSS Distributionsbranches from the standard call stack.
197*c54f35caSApple OSS Distributions.It Ft btui_async_frame_addr
198*c54f35caSApple OSS DistributionsThe frame address of the Swift continuation to pass in to a subsequent call
199*c54f35caSApple OSS Distributionsto
200*c54f35caSApple OSS Distributions.Fn backtrace_user
201*c54f35caSApple OSS Distributions.Pq as the control structure's frame address field
202*c54f35caSApple OSS Distributionsto follow the corresponding async stack.
203*c54f35caSApple OSS Distributions.It Ft btui_next_frame_addr
204*c54f35caSApple OSS DistributionsIn the case of a truncated backtrace due to lack of space in the destination
205*c54f35caSApple OSS Distributionsbuffer, the next frame address to resume the backtrace operation.
206*c54f35caSApple OSS Distributions.El
207*c54f35caSApple OSS Distributions.
208*c54f35caSApple OSS Distributions.Sh EXAMPLE
209*c54f35caSApple OSS Distributions.Bd -literal
210*c54f35caSApple OSS Distributionsuintptr_t bt[8] = {};
211*c54f35caSApple OSS Distributionsenum backtrace_info bti = BTI_NONE;
212*c54f35caSApple OSS Distributionsunsigned int len = backtrace(bt, sizeof(bt) / sizeof(bt[0]), NULL, &bti);
213*c54f35caSApple OSS Distributionsfor (unsigned int i = 0; i < len; i++)  {
214*c54f35caSApple OSS Distributions	printf("%d: 0x%lx\\n", i, bt[i]);
215*c54f35caSApple OSS Distributions}
216*c54f35caSApple OSS Distributionsif (bti & BTI_TRUNCATED) {
217*c54f35caSApple OSS Distributions	printf("[... TRUNCATED ...]\\n");
218*c54f35caSApple OSS Distributions}
219*c54f35caSApple OSS Distributions.Ed
220*c54f35caSApple OSS Distributions.
221*c54f35caSApple OSS Distributions.Sh SEE ALSO
222*c54f35caSApple OSS Distributions.Xr backtrace 3 ,
223*c54f35caSApple OSS Distributions.Xr backtrace_pack 9 ,
224*c54f35caSApple OSS Distributionsand
225*c54f35caSApple OSS Distributions.Xr copyin 9
226