xref: /freebsd/share/man/man9/stack.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
112cef7aaSRobert Watson.\"
236fecbf3SRobert Watson.\" Copyright (c) 2007-2009 Robert N. M. Watson
312cef7aaSRobert Watson.\" All rights reserved.
412cef7aaSRobert Watson.\"
512cef7aaSRobert Watson.\" Redistribution and use in source and binary forms, with or without
612cef7aaSRobert Watson.\" modification, are permitted provided that the following conditions
712cef7aaSRobert Watson.\" are met:
812cef7aaSRobert Watson.\" 1. Redistributions of source code must retain the above copyright
912cef7aaSRobert Watson.\"    notice(s), this list of conditions and the following disclaimer as
1012cef7aaSRobert Watson.\"    the first lines of this file unmodified other than the possible
1112cef7aaSRobert Watson.\"    addition of one or more copyright notices.
1212cef7aaSRobert Watson.\" 2. Redistributions in binary form must reproduce the above copyright
1312cef7aaSRobert Watson.\"    notice(s), this list of conditions and the following disclaimer in the
1412cef7aaSRobert Watson.\"    documentation and/or other materials provided with the distribution.
1512cef7aaSRobert Watson.\"
1612cef7aaSRobert Watson.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1712cef7aaSRobert Watson.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1812cef7aaSRobert Watson.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1912cef7aaSRobert Watson.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2012cef7aaSRobert Watson.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2112cef7aaSRobert Watson.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2212cef7aaSRobert Watson.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2312cef7aaSRobert Watson.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2412cef7aaSRobert Watson.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2512cef7aaSRobert Watson.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2612cef7aaSRobert Watson.\" DAMAGE.
2712cef7aaSRobert Watson.\"
28*a65bee67SEric van Gyzen.Dd March 6, 2022
2912cef7aaSRobert Watson.Dt STACK 9
3012cef7aaSRobert Watson.Os
3112cef7aaSRobert Watson.Sh NAME
3212cef7aaSRobert Watson.Nm stack
3312cef7aaSRobert Watson.Nd kernel thread stack tracing routines
3412cef7aaSRobert Watson.Sh SYNOPSIS
35a69a5114SEd Maste.In sys/param.h
3612cef7aaSRobert Watson.In sys/stack.h
3799fdade2SMark Johnston.Pp
383c90d1eaSRobert WatsonIn the kernel configuration file:
3912cef7aaSRobert Watson.Cd "options DDB"
403c90d1eaSRobert Watson.Cd "options STACK"
4199fdade2SMark Johnston.Pp
4212cef7aaSRobert Watson.Ft struct stack *
43f38c0c46SMark Johnston.Fn stack_create "int flags"
4412cef7aaSRobert Watson.Ft void
4512cef7aaSRobert Watson.Fn stack_destroy "struct stack *st"
4612cef7aaSRobert Watson.Ft int
4712cef7aaSRobert Watson.Fn stack_put "struct stack *st" "vm_offset_t pc"
4812cef7aaSRobert Watson.Ft void
49*a65bee67SEric van Gyzen.Fn stack_copy "const struct stack *src" "struct stack *dst"
5012cef7aaSRobert Watson.Ft void
5112cef7aaSRobert Watson.Fn stack_zero "struct stack *st"
5212cef7aaSRobert Watson.Ft void
53a2035830SPawel Jakub Dawidek.Fn stack_print "const struct stack *st"
547340f1a1SRobert Watson.Ft void
55a2035830SPawel Jakub Dawidek.Fn stack_print_ddb "const struct stack *st"
56b9a98037SRobert Watson.Ft void
57a2035830SPawel Jakub Dawidek.Fn stack_print_short "const struct stack *st"
5836fecbf3SRobert Watson.Ft void
59a2035830SPawel Jakub Dawidek.Fn stack_print_short_ddb "const struct stack *st"
6036fecbf3SRobert Watson.Ft void
61*a65bee67SEric van Gyzen.Fn stack_sbuf_print "struct sbuf *sb" "const struct stack *st"
6212cef7aaSRobert Watson.Ft void
63*a65bee67SEric van Gyzen.Fn stack_sbuf_print_ddb "struct sbuf *sb" "const struct stack *st"
649ccca7d1SRobert Watson.Ft void
6512cef7aaSRobert Watson.Fn stack_save "struct stack *st"
6699fdade2SMark Johnston.Ft int
671c29da02SMark Johnston.Fn stack_save_td "struct stack *st" "struct thread *td"
6812cef7aaSRobert Watson.Sh DESCRIPTION
6912cef7aaSRobert WatsonThe
7012cef7aaSRobert Watson.Nm
7112cef7aaSRobert WatsonKPI allows querying of kernel stack trace information and the automated
7212cef7aaSRobert Watsongeneration of kernel stack trace strings for the purposes of debugging and
7312cef7aaSRobert Watsontracing.
743c90d1eaSRobert WatsonTo use the KPI, at least one of
753c90d1eaSRobert Watson.Cd "options DDB"
763c90d1eaSRobert Watsonand
773c90d1eaSRobert Watson.Cd "options STACK"
783c90d1eaSRobert Watsonmust be compiled into the kernel.
7912cef7aaSRobert Watson.Pp
8012cef7aaSRobert WatsonEach stack trace is described by a
8112cef7aaSRobert Watson.Vt "struct stack" .
82*a65bee67SEric van GyzenIt can be declared in the usual ways, including on the stack, and optionally
83*a65bee67SEric van Gyzeninitialized with
84*a65bee67SEric van Gyzen.Fn stack_zero ,
85*a65bee67SEric van Gyzenthough this is not necessary before saving a trace.
86*a65bee67SEric van GyzenIt can also be dynamically allocated with
87f38c0c46SMark Johnston.Fn stack_create .
88f38c0c46SMark JohnstonThe
89f38c0c46SMark Johnston.Ar flags
90f38c0c46SMark Johnstonargument is passed to
91f38c0c46SMark Johnston.Xr malloc 9 .
92*a65bee67SEric van GyzenThis dynamic allocation must be freed with
9312cef7aaSRobert Watson.Fn stack_destroy .
9412cef7aaSRobert Watson.Pp
951c29da02SMark JohnstonA trace of the current thread's kernel call stack may be captured using
966a2ecc38SRobert Watson.Fn stack_save .
9799fdade2SMark Johnston.Fn stack_save_td
981c29da02SMark Johnstoncan be used to capture the kernel stack of a caller-specified thread.
99*a65bee67SEric van GyzenCallers of
100*a65bee67SEric van Gyzen.Fn stack_save_td
101*a65bee67SEric van Gyzenmust own the thread lock of the specified thread,
1021c29da02SMark Johnstonand the thread's stack must not be swapped out.
10399fdade2SMark Johnston.Fn stack_save_td
1041c29da02SMark Johnstoncan capture the kernel stack of a running thread, though note that this is
1051c29da02SMark Johnstonnot implemented on all platforms.
1061c29da02SMark JohnstonIf the thread is running, the caller must also hold the process lock for the
1071c29da02SMark Johnstontarget thread.
10812cef7aaSRobert Watson.Pp
1097340f1a1SRobert Watson.Fn stack_print
11036fecbf3SRobert Watsonand
11136fecbf3SRobert Watson.Fn stack_print_short
1127340f1a1SRobert Watsonmay be used to print a stack trace using the kernel
1133c90d1eaSRobert Watson.Xr printf 9 ,
1143c90d1eaSRobert Watsonand may sleep as a result of acquiring
1153c90d1eaSRobert Watson.Xr sx 9
1163c90d1eaSRobert Watsonlocks in the kernel linker while looking up symbol names.
1173c90d1eaSRobert WatsonIn locking-sensitive environments, the unsynchronized
1183c90d1eaSRobert Watson.Fn stack_print_ddb
11936fecbf3SRobert Watsonand
12036fecbf3SRobert Watson.Fn stack_print_short_ddb
12136fecbf3SRobert Watsonvariants may be invoked.
1223c90d1eaSRobert WatsonThis function bypasses kernel linker locking, making it usable in
1233c90d1eaSRobert Watson.Xr ddb 4 ,
1243c90d1eaSRobert Watsonbut not in a live system where linker data structures may change.
1257340f1a1SRobert Watson.Pp
12612cef7aaSRobert Watson.Fn stack_sbuf_print
12712cef7aaSRobert Watsonmay be used to construct a human-readable string, including conversion (where
12812cef7aaSRobert Watsonpossible) from a simple kernel instruction pointer to a named symbol and
12912cef7aaSRobert Watsonoffset.
13012cef7aaSRobert WatsonThe argument
13112cef7aaSRobert Watson.Ar sb
13212cef7aaSRobert Watsonmust be an initialized
13312cef7aaSRobert Watson.Dv struct sbuf
13412cef7aaSRobert Watsonas described in
13512cef7aaSRobert Watson.Xr sbuf 9 .
13612cef7aaSRobert WatsonThis function may sleep if an auto-extending
13712cef7aaSRobert Watson.Dv struct sbuf
1383c90d1eaSRobert Watsonis used, or due to kernel linker locking.
1399ccca7d1SRobert WatsonIn locking-sensitive environments, such as
1403c90d1eaSRobert Watson.Xr ddb 4 ,
1419ccca7d1SRobert Watsonthe unsynchronized
1429ccca7d1SRobert Watson.Fn stack_sbuf_print_ddb
1433c90d1eaSRobert Watsonvariant may be invoked to avoid kernel linker locking; it should be used with
1443c90d1eaSRobert Watsona fixed-length sbuf.
14512cef7aaSRobert Watson.Pp
14612cef7aaSRobert WatsonThe utility functions
14712cef7aaSRobert Watson.Nm stack_zero ,
14812cef7aaSRobert Watson.Nm stack_copy ,
14912cef7aaSRobert Watsonand
15012cef7aaSRobert Watson.Nm stack_put
15112cef7aaSRobert Watsonmay be used to manipulate stack data structures directly.
15299fdade2SMark Johnston.Sh RETURN VALUES
15399fdade2SMark Johnston.Fn stack_put
15499fdade2SMark Johnstonreturns 0 on success.
15599fdade2SMark JohnstonOtherwise the
15699fdade2SMark Johnston.Dv struct stack
15799fdade2SMark Johnstondoes not contain space to record additional frames, and a non-zero value is
15899fdade2SMark Johnstonreturned.
15999fdade2SMark Johnston.Pp
1601c29da02SMark Johnston.Fn stack_save_td
16199fdade2SMark Johnstonreturns 0 when the stack capture was successful and a non-zero error number
16299fdade2SMark Johnstonotherwise.
16399fdade2SMark JohnstonIn particular,
1641c29da02SMark Johnston.Er EBUSY
16599fdade2SMark Johnstonis returned if the thread was running in user mode at the time that the
16699fdade2SMark Johnstoncapture was attempted, and
16799fdade2SMark Johnston.Er EOPNOTSUPP
16899fdade2SMark Johnstonis returned if the operation is not implemented.
16912cef7aaSRobert Watson.Sh SEE ALSO
1703c90d1eaSRobert Watson.Xr ddb 4 ,
1717340f1a1SRobert Watson.Xr printf 9 ,
1729ccca7d1SRobert Watson.Xr sbuf 9 ,
1739ccca7d1SRobert Watson.Xr sx 9
17412cef7aaSRobert Watson.Sh AUTHORS
17512cef7aaSRobert Watson.An -nosplit
17612cef7aaSRobert WatsonThe
17716e36754SEdward Tomasz Napierala.Nm
17812cef7aaSRobert Watsonfunction suite was created by
17912cef7aaSRobert Watson.An Antoine Brodin .
18016e36754SEdward Tomasz Napierala.Nm
1813c90d1eaSRobert Watsonwas extended by
1823c90d1eaSRobert Watson.An Robert Watson
1833c90d1eaSRobert Watsonfor general-purpose use outside of
1843c90d1eaSRobert Watson.Xr ddb 4 .
185