]> git.saurik.com Git - apple/libc.git/blame_incremental - gen/backtrace.3
Libc-1244.50.9.tar.gz
[apple/libc.git] / gen / backtrace.3
... / ...
CommitLineData
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 February 15, 2007
30.Dt backtrace 3
31.Os "Mac OS X"
32.Sh NAME
33.Nm backtrace ,
34.Nm backtrace_symbols ,
35.Nm backtrace_symbols_fd
36.Nd call stack backtrace and display functions
37.Sh SYNOPSIS
38.In execinfo.h
39.Ft int
40.Fo backtrace
41.Fa "void** array"
42.Fa "int size"
43.Fc
44.Ft char**
45.Fo backtrace_symbols
46.Fa "void* const* array"
47.Fa "int size"
48.Fc
49.Ft void
50.Fo backtrace_symbols_fd
51.Fa "void* const* array"
52.Fa "int size"
53.Fa "int fd"
54.Fc
55.Sh DESCRIPTION
56These routines provide a mechanism to examine the current thread's call stack.
57.Pp
58.Fn backtrace
59writes the function return addresses of the current call stack to the array of
60pointers referenced by
61.Fa array .
62At most,
63.Fa size
64pointers are written. The number of pointers actually written to
65.Fa array
66is returned.
67.Pp
68.Fn backtrace_symbols
69attempts to transform a call stack obtained by
70.Fn backtrace
71into an array of human-readable strings using
72.Fn dladdr .
73The array of strings returned has
74.Fa size
75elements. It is allocated using
76.Fn malloc
77and should be released using
78.Fn free .
79There is no need to free the individual strings in the array.
80.Pp
81.Fn backtrace_symbols_fd
82performs the same operation as
83.Fn backtrace_symbols ,
84but the resulting strings are immediately written to the file descriptor
85.Fa fd ,
86and are not returned.
87.Sh EXAMPLE
88.Pp
89 #include <execinfo.h>
90 #include <stdio.h>
91 ...
92 void* callstack[128];
93 int i, frames = backtrace(callstack, 128);
94 char** strs = backtrace_symbols(callstack, frames);
95 for (i = 0; i < frames; ++i) {
96 printf("%s\\n", strs[i]);
97 }
98 free(strs);
99 ...
100.Pp
101.Sh HISTORY
102These functions first appeared in
103Mac OS X 10.5.
104.Sh SEE ALSO
105.Xr dladdr 3 ,
106.Xr malloc 3