OSDN Git Service

(split) LDP: Update releases based on LDP 3.52 release
[linuxjm/LDP_man-pages.git] / release / man3 / backtrace.3
1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" drawing on material by Justin Pryzby <pryzbyj@justinpryzby.com>
3 .\"
4 .\" %%%LICENSE_START(PERMISSIVE_MISC)
5 .\" Permission is hereby granted, free of charge, to any person obtaining
6 .\" a copy of this software and associated documentation files (the
7 .\" "Software"), to deal in the Software without restriction, including
8 .\" without limitation the rights to use, copy, modify, merge, publish,
9 .\" distribute, sublicense, and/or sell copies of the Software, and to
10 .\" permit persons to whom the Software is furnished to do so, subject to
11 .\" the following conditions:
12 .\"
13 .\" The above copyright notice and this permission notice shall be
14 .\" included in all copies or substantial portions of the Software.
15 .\"
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References:
26 .\"   glibc manual and source
27 .\"*******************************************************************
28 .\"
29 .\" This file was generated with po4a. Translate the source file.
30 .\"
31 .\"*******************************************************************
32 .TH BACKTRACE 3 2008\-06\-14 GNU "Linux Programmer's Manual"
33 .SH 名前
34 backtrace, backtrace_symbols, backtrace_symbols_fd \- アプリケーション自身でのデバッグのサポート
35 .SH 書式
36 \fB#include <execinfo.h>\fP
37
38 \fBint backtrace(void\fP \fB**\fP\fIbuffer\fP\fB,\fP \fBint\fP \fIsize\fP\fB);\fP
39
40 \fBchar **backtrace_symbols(void *const\fP \fB*\fP\fIbuffer\fP\fB,\fP \fBint\fP
41 \fIsize\fP\fB);\fP
42
43 \fBvoid backtrace_symbols_fd(void *const\fP \fB*\fP\fIbuffer\fP\fB,\fP \fBint\fP
44 \fIsize\fP\fB,\fP \fBint\fP \fIfd\fP\fB);\fP
45 .SH 説明
46 \fBbacktrace\fP()  returns a backtrace for the calling program, in the array
47 pointed to by \fIbuffer\fP.  A backtrace is the series of currently active
48 function calls for the program.  Each item in the array pointed to by
49 \fIbuffer\fP is of type \fIvoid\ *\fP, and is the return address from the
50 corresponding stack frame.  The \fIsize\fP argument specifies the maximum
51 number of addresses that can be stored in \fIbuffer\fP.  If the backtrace is
52 larger than \fIsize\fP, then the addresses corresponding to the \fIsize\fP most
53 recent function calls are returned; to obtain the complete backtrace, make
54 sure that \fIbuffer\fP and \fIsize\fP are large enough.
55
56 \fBbacktrace\fP()  によって \fIbuffer\fP にアドレスの集合が得られたら、 \fBbacktrace_symbols\fP()
57 によって、アドレス集合を、そのアドレスをシンボルで表した文字列の配列 に翻訳できる。 \fIsize\fP 引き数は \fIbuffer\fP
58 に格納されたアドレスの数を指定する。 個々のアドレスのシンボル表現は、関数名 (特定できた場合)、 関数へのオフセット
59 (16進表記)、実際のリターンアドレス (16進表記)  から構成される。 \fBbacktrace_symbols\fP()  の実行結果としては、
60 文字列ポインタの配列のアドレスが返される。 この配列は \fBbacktrace_symbols\fP()  によって \fBmalloc\fP(3)
61 され、呼び出し側で free しなければならない (ポインタの配列が指す個々の文字列は free する必要はないし、 free すべきでもない)。
62
63 \fBbacktrace_symbols_fd\fP()  は、 \fBbacktrace_symbols\fP()  と同じ引き数 \fIbuffer\fP と
64 \fIsize\fP をとるが、呼び出し側に文字列の配列を返す代わりに、 文字列をファイルディスクリプタ \fIfd\fP に 1 行に 1
65 エントリの形で書き込む。 \fBbacktrace_symbols_fd\fP()  は \fBmalloc\fP(3)  を呼び出さない。
66 そのため、これに続く関数が失敗する可能性がある状況でも利用できる。
67 .SH 返り値
68 \fBbacktrace\fP()  は \fIbuffer\fP に格納したアドレスの個数を返す。その個数は \fIsize\fP より大きくなることはない。 返り値が
69 \fIsize\fP より小さい場合、バックトレース全体が格納されている。返り値が \fIsize\fP
70 と等しい場合、バックトレースは切り詰められているかもしれない。 切り詰められた場合、最も古いスタックフレームのアドレスは 返されないことになる。
71
72 \fBbacktrace_symbols\fP()  は、成功すると、この呼び出しで \fBmalloc\fP(3)  された配列へのポインタを返す。
73 エラーの場合、 NULL を返す。
74 .SH バージョン
75 \fBbacktrace\fP(), \fBbacktrace_symbols\fP(), \fBbacktrace_symbols_fd\fP()  はバージョン
76 2.1 以降の glibc で提供されている。
77 .SH 準拠
78 これらの関数は GNU による拡張である。
79 .SH 注意
80 これらの関数は、関数のリターンアドレスがスタック上でどのように格納されるか に関してある仮定を置いている。 以下の点に注意。
81 .IP * 3
82 (\fBgcc\fP(1)  の 0 以外の最適化レベルで暗黙のうちに行われる)  フレームポインタの省略を行うと、これらの前提が崩れる可能性がある。
83 .IP *
84 インライン関数はスタックフレームを持たない。
85 .IP *
86 末尾呼び出しの最適化 (tail\-call optimization) を行うと、 あるスタックフレームが別のスタックフレームを置き換える可能性がある。
87 .PP
88 シンボル名は特別なリンカ・オプションを使用しないと利用できない場合がある。 GNU リンカを使用するシステムでは、 \fI\-rdynamic\fP
89 リンカ・オプションを使う必要がある。 "static" な関数のシンボル名は公開されず、 バックトレースでは利用できない点に注意すること。
90 .SH 例
91 以下のプログラムは、 \fBbacktrace\fP()  と \fBbacktrace_symbols\fP()  の使用例を示したものである。
92 以下に示すシェルのセッションは、 このプログラムを動かした際の実行例である。
93 .nf
94 .in +4n
95
96 $\fB cc \-rdynamic prog.c \-o prog\fP
97 $\fB ./prog 3\fP
98 backtrace() returned 8 addresses
99 \&./prog(myfunc3+0x5c) [0x80487f0]
100 \&./prog [0x8048871]
101 \&./prog(myfunc+0x21) [0x8048894]
102 \&./prog(myfunc+0x1a) [0x804888d]
103 \&./prog(myfunc+0x1a) [0x804888d]
104 \&./prog(main+0x65) [0x80488fb]
105 \&/lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c]
106 \&./prog [0x8048711]
107 .in
108 .fi
109 .SS プログラムのソース
110 \&
111 .nf
112 #include <execinfo.h>
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <unistd.h>
116
117 void
118 myfunc3(void)
119 {
120     int j, nptrs;
121 #define SIZE 100
122     void *buffer[100];
123     char **strings;
124
125     nptrs = backtrace(buffer, SIZE);
126     printf("backtrace() returned %d addresses\en", nptrs);
127
128     /* backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) を
129        呼び出しても、以下と同様の出力が得られる。 */
130
131     strings = backtrace_symbols(buffer, nptrs);
132     if (strings == NULL) {
133         perror("backtrace_symbols");
134         exit(EXIT_FAILURE);
135     }
136
137     for (j = 0; j < nptrs; j++)
138         printf("%s\en", strings[j]);
139
140     free(strings);
141 }
142
143 static void   /* "static" はシンボルを公開しないことを意味する */
144 myfunc2(void)
145 {
146     myfunc3();
147 }
148
149 void
150 myfunc(int ncalls)
151 {
152     if (ncalls > 1)
153         myfunc(ncalls \- 1);
154     else
155         myfunc2();
156 }
157
158 int
159 main(int argc, char *argv[])
160 {
161     if (argc != 2) {
162         fprintf(stderr, "%s num\-calls\en", argv[0]);
163         exit(EXIT_FAILURE);
164     }
165
166     myfunc(atoi(argv[1]));
167     exit(EXIT_SUCCESS);
168 }
169 .fi
170 .SH 関連項目
171 \fBgcc\fP(1), \fBld\fP(1), \fBdlopen\fP(3), \fBmalloc\fP(3)
172 .SH この文書について
173 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
174 である。プロジェクトの説明とバグ報告に関する情報は
175 http://www.kernel.org/doc/man\-pages/ に書かれている。