1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" drawing on material by Justin Pryzby <pryzbyj@justinpryzby.com>
4 .\" Permission is hereby granted, free of charge, to any person obtaining
5 .\" a copy of this software and associated documentation files (the
6 .\" "Software"), to deal in the Software without restriction, including
7 .\" without limitation the rights to use, copy, modify, merge, publish,
8 .\" distribute, sublicense, and/or sell copies of the Software, and to
9 .\" permit persons to whom the Software is furnished to do so, subject to
10 .\" the following conditions:
12 .\" The above copyright notice and this permission notice shall be
13 .\" included in all copies or substantial portions of the Software.
15 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 .\" glibc manual and source
26 .\" Japanese Version Copyright (c) 2007 Akihiro MOTOKI
27 .\" all rights reserved.
28 .\" Translated 2007-10-16, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.66
30 .TH BACKTRACE 3 2008-06-14 GNU "Linux Programmer's Manual"
33 .\"O backtrace, backtrace_symbols, backtrace_symbols_fd \- support
34 .\"O for application self-debugging
35 backtrace, backtrace_symbols, backtrace_symbols_fd \-
39 .B #include <execinfo.h>
46 .B char **backtrace_symbols(void *const
51 .B void backtrace_symbols_fd(void *const
60 .\"O returns a backtrace for the calling program,
61 .\"O in the array pointed to by
63 .\"O A backtrace is the series of currently active function calls for
65 .\"O Each item in the array pointed to by
67 .\"O is of type \fIvoid *\fP, and is the return address from
68 .\"O the corresponding stack frame.
72 が指す配列に入れて返す。バックトレースは、プログラムで
75 が指す配列の個々の要素は \fIvoid *\fP 型で、
76 対応するスタックフレームからのリターンアドレスである。
79 .\"O argument specifies the maximum number of addresses
80 .\"O that can be stored in
82 .\"O If the backtrace is larger than
84 .\"O then the addresses corresponding to the
86 .\"O most recent function calls are returned;
87 .\"O to obtain the complete backtrace, make sure that
91 .\"O are large enough.
100 個の直近の関数呼び出しに対応するアドレスが返される。
101 完全なバックトレースを取得するためには、確実に
107 .\"O Given the set of addresses returned by
108 .\"O .BR backtrace ()
111 .\"O .BR backtrace_symbols ()
112 .\"O translates the addresses into an array of strings that describe
113 .\"O the addresses symbolically.
118 .BR backtrace_symbols ()
119 によって、アドレス集合を、そのアドレスをシンボルで表した文字列の配列
123 .\"O argument specifies the number of addresses in
129 .\"O The symbolic representation of each address consists of the function name
130 .\"O (if this can be determined), a hexadecimal offset into the function,
131 .\"O and the actual return address (in hexadecimal).
132 個々のアドレスのシンボル表現は、関数名 (特定できた場合)、
133 関数へのオフセット (16進表記)、実際のリターンアドレス (16進表記)
135 .\"O The address of the array of string pointers is returned
136 .\"O as the function result of
137 .\"O .BR backtrace_symbols ().
139 .\"O .BR malloc (3)ed
141 .\"O .BR backtrace_symbols (),
142 .\"O and must be freed by the caller.
143 .\"O (The strings pointed to by the array of pointers
144 .\"O need not and should not be freed.)
145 .BR backtrace_symbols ()
147 文字列ポインタの配列のアドレスが返される。
149 .BR backtrace_symbols ()
152 され、呼び出し側で free しなければならない
153 (ポインタの配列が指す個々の文字列は free する必要はないし、
156 .\"O .BR backtrace_symbols_fd ()
162 .\"O .BR backtrace_symbols (),
163 .\"O but instead of returning an array of strings to the caller,
164 .\"O it writes the strings, one per line, to the file descriptor
166 .\"O .BR backtrace_symbols_fd ()
169 .\"O and so can be employed in situations where the latter function might fail.
170 .BR backtrace_symbols_fd ()
172 .BR backtrace_symbols ()
177 をとるが、呼び出し側に文字列の配列を返す代わりに、
180 に 1 行に 1 エントリの形で書き込む。
181 .BR backtrace_symbols_fd ()
185 そのため、これに続く関数が失敗する可能性がある状況でも利用できる。
186 .\"O .SH "RETURN VALUE"
188 .\"O .BR backtrace ()
189 .\"O returns the number of addresses returned in
191 .\"O which is not greater than
193 .\"O If the return value is less than
195 .\"O then the full backtrace was stored; if it is equal to
197 .\"O then it may have been truncated, in which case the addresses of the
198 .\"O oldest stack frames are not returned.
202 に格納したアドレスの個数を返す。その個数は
207 より小さい場合、バックトレース全体が格納されている。返り値が
209 と等しい場合、バックトレースは切り詰められているかもしれない。
210 切り詰められた場合、最も古いスタックフレームのアドレスは
214 .\"O .BR backtrace_symbols ()
215 .\"O returns a pointer to the array
216 .\"O .BR malloc (3)ed
218 .\"O on error, NULL is returned.
219 .BR backtrace_symbols ()
226 .\"O .BR backtrace (),
227 .\"O .BR backtrace_symbols (),
229 .\"O .BR backtrace_symbols_fd ()
230 .\"O are provided in glibc since version 2.1.
232 .BR backtrace_symbols (),
233 .BR backtrace_symbols_fd ()
234 はバージョン 2.1 以降の glibc で提供されている。
235 .\"O .SH CONFORMING TO
237 .\"O These functions are GNU extensions.
238 これらの関数は GNU による拡張である。
241 .\"O These functions make some assumptions about how a function's return
242 .\"O address is stored on the stack.
243 .\"O Note the following:
244 これらの関数は、関数のリターンアドレスがスタック上でどのように格納されるか
248 .\"O Omission of the frame pointers (as
249 .\"O implied by any of
251 .\"O nonzero optimization levels) may cause these assumptions to be
254 の 0 以外の最適化レベルで暗黙のうちに行われる)
255 フレームポインタの省略を行うと、これらの前提が崩れる可能性がある。
257 .\"O Inlined functions do not have stack frames.
258 インライン関数はスタックフレームを持たない。
260 .\"O Tail-call optimization causes one stack frame to replace another.
261 末尾呼び出しの最適化 (tail-call optimization) を行うと、
262 あるスタックフレームが別のスタックフレームを置き換える可能性がある。
264 .\"O The symbol names may be unavailable without the use of special linker
266 .\"O For systems using the GNU linker, it is necessary to use the
269 .\"O Note that names of "static" functions are not exposed,
270 .\"O and won't be available in the backtrace.
271 シンボル名は特別なリンカ・オプションを使用しないと利用できない場合がある。
275 "static" な関数のシンボル名は公開されず、
276 バックトレースでは利用できない点に注意すること。
279 .\"O The program below demonstrates the use of
280 .\"O .BR backtrace ()
282 .\"O .BR backtrace_symbols ().
283 .\"O The following shell session shows what we might see when running the
288 .BR backtrace_symbols ()
291 このプログラムを動かした際の実行例である。
295 .RB "$" " cc \-rdynamic prog.c \-o prog"
297 backtrace() returned 8 addresses
298 \&./prog(myfunc3+0x5c) [0x80487f0]
300 \&./prog(myfunc+0x21) [0x8048894]
301 \&./prog(myfunc+0x1a) [0x804888d]
302 \&./prog(myfunc+0x1a) [0x804888d]
303 \&./prog(main+0x65) [0x80488fb]
304 \&/lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c]
308 .\"O .SS Program source
312 #include <execinfo.h>
325 nptrs = backtrace(buffer, SIZE);
326 printf("backtrace() returned %d addresses\\n", nptrs);
328 .\"O /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
329 .\"O would produce similar output to the following: */
330 /* backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) を
331 呼び出しても、以下と同様の出力が得られる。 */
333 strings = backtrace_symbols(buffer, nptrs);
334 if (strings == NULL) {
335 perror("backtrace_symbols");
339 for (j = 0; j < nptrs; j++)
340 printf("%s\\n", strings[j]);
345 .\"O static void /* "static" means don\(aqt export the symbol... */
346 static void /* "static" はシンボルを公開しないことを意味する */
362 main(int argc, char *argv[])
365 fprintf(stderr, "%s num\-calls\\n", argv[0]);
369 myfunc(atoi(argv[1]));