]> git.saurik.com Git - apple/libc.git/blame - gen/backtrace.3
Libc-1272.200.26.tar.gz
[apple/libc.git] / gen / backtrace.3
CommitLineData
224c7076
A
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.\"
70ad1dc8 29.Dd March 1, 2018
224c7076 30.Dt backtrace 3
70ad1dc8 31.Os "Darwin"
224c7076
A
32.Sh NAME
33.Nm backtrace ,
34.Nm backtrace_symbols ,
70ad1dc8
A
35.Nm backtrace_symbols_fd ,
36.Nm backtrace_image_offsets ,
37.Nm backtrace_from_fp
224c7076
A
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
70ad1dc8
A
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
224c7076
A
69.Sh DESCRIPTION
70These routines provide a mechanism to examine the current thread's call stack.
71.Pp
72.Fn backtrace
73writes the function return addresses of the current call stack to the array of
74pointers referenced by
75.Fa array .
76At most,
77.Fa size
78pointers are written. The number of pointers actually written to
79.Fa array
80is returned.
81.Pp
82.Fn backtrace_symbols
83attempts to transform a call stack obtained by
84.Fn backtrace
85into an array of human-readable strings using
86.Fn dladdr .
87The array of strings returned has
88.Fa size
89elements. It is allocated using
90.Fn malloc
91and should be released using
92.Fn free .
93There is no need to free the individual strings in the array.
94.Pp
95.Fn backtrace_symbols_fd
96performs the same operation as
97.Fn backtrace_symbols ,
98but the resulting strings are immediately written to the file descriptor
99.Fa fd ,
100and are not returned.
70ad1dc8
A
101.Pp
102.Fn backtrace_image_offsets
103attempts to transform a call stack obtained by
104.Fn backtrace
105into an array of image offsets, for deferred symbolication. Each entry in the
106array has an offset relative to the
107.Li __TEXT
108section of the image with the given UUID. The results are written to
109.Fa image_offsets
110which should be an array of
111.Fa size
112length.
113.Pp
114.Fn backtrace_from_fp
115takes a backtrace of frames starting from the given frame pointer.
224c7076
A
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
70ad1dc8
A
131.Fn backtrace ,
132.Fn backtrace_symbols ,
133and
134.Fn backtrace_symbols_fd
135first appeared in Mac OS X 10.5.
136.Fn backtrace_image_offsets
137and
138.Fn backtrace_from_fp
139first appeared macOS 10.14 and iOS 12.
224c7076
A
140.Sh SEE ALSO
141.Xr dladdr 3 ,
142.Xr malloc 3