Libc-1439.100.3.tar.gz
[apple/libc.git] / gen / backtrace.3
1 .\" Copyright (c) 2007 Apple Inc.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of Apple Inc. ("Apple") nor the names of its
13 .\" contributors may be used to endorse or promote products derived from
14 .\" this software without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY APPLE AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"
29 .Dd March 1, 2018
30 .Dt backtrace 3
31 .Os "Darwin"
32 .Sh NAME
33 .Nm backtrace ,
34 .Nm backtrace_symbols ,
35 .Nm backtrace_symbols_fd ,
36 .Nm backtrace_image_offsets ,
37 .Nm backtrace_from_fp
38 .Nd call stack backtrace and display functions
39 .Sh SYNOPSIS
40 .In execinfo.h
41 .Ft int
42 .Fo backtrace
43 .Fa "void** array"
44 .Fa "int size"
45 .Fc
46 .Ft char**
47 .Fo backtrace_symbols
48 .Fa "void* const* array"
49 .Fa "int size"
50 .Fc
51 .Ft void
52 .Fo backtrace_symbols_fd
53 .Fa "void* const* array"
54 .Fa "int size"
55 .Fa "int fd"
56 .Fc
57 .Ft void
58 .Fo backtrace_image_offsets
59 .Fa "void* const* array"
60 .Fa "struct image_offset *image_offsets"
61 .Fa "int size"
62 .Fc
63 .Ft int
64 .Fo backtrace_from_fp
65 .Fa "void* startfp"
66 .Fa "void** array"
67 .Fa "int size"
68 .Fc
69 .Sh DESCRIPTION
70 These routines provide a mechanism to examine the current thread's call stack.
71 .Pp
72 .Fn backtrace
73 writes the function return addresses of the current call stack to the array of
74 pointers referenced by
75 .Fa array .
76 At most,
77 .Fa size
78 pointers are written. The number of pointers actually written to
79 .Fa array
80 is returned.
81 .Pp
82 .Fn backtrace_symbols
83 attempts to transform a call stack obtained by
84 .Fn backtrace
85 into an array of human-readable strings using
86 .Fn dladdr .
87 The array of strings returned has
88 .Fa size
89 elements. It is allocated using
90 .Fn malloc
91 and should be released using
92 .Fn free .
93 There is no need to free the individual strings in the array.
94 .Pp
95 .Fn backtrace_symbols_fd
96 performs the same operation as
97 .Fn backtrace_symbols ,
98 but the resulting strings are immediately written to the file descriptor
99 .Fa fd ,
100 and are not returned.
101 .Pp
102 .Fn backtrace_image_offsets
103 attempts to transform a call stack obtained by
104 .Fn backtrace
105 into an array of image offsets, for deferred symbolication. Each entry in the
106 array has an offset relative to the
107 .Li __TEXT
108 section of the image with the given UUID. The results are written to
109 .Fa image_offsets
110 which should be an array of
111 .Fa size
112 length.
113 .Pp
114 .Fn backtrace_from_fp
115 takes a backtrace of frames starting from the given frame pointer.
116 .Sh EXAMPLE
117 .Pp
118 #include <execinfo.h>
119 #include <stdio.h>
120 ...
121 void* callstack[128];
122 int i, frames = backtrace(callstack, 128);
123 char** strs = backtrace_symbols(callstack, frames);
124 for (i = 0; i < frames; ++i) {
125 printf("%s\\n", strs[i]);
126 }
127 free(strs);
128 ...
129 .Pp
130 .Sh HISTORY
131 .Fn backtrace ,
132 .Fn backtrace_symbols ,
133 and
134 .Fn backtrace_symbols_fd
135 first appeared in Mac OS X 10.5.
136 .Fn backtrace_image_offsets
137 and
138 .Fn backtrace_from_fp
139 first appeared macOS 10.14 and iOS 12.
140 .Sh SEE ALSO
141 .Xr dladdr 3 ,
142 .Xr malloc 3