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