static basic_stacktrace
current(const allocator_type& alloc = allocator_type()) noexcept; // (1) C++23
static basic_stacktrace
current(size_type skip,
const allocator_type& alloc = allocator_type()) noexcept; // (2) C++23
static basic_stacktrace
current(size_type skip,
size_type max_depth,
const allocator_type& alloc = allocator_type()) noexcept; // (3) C++23
概要
現在位置からのスタックトレースを取得する。
- (1) : 現在位置から現在の実行スレッドの初期関数までの全体のスタックトレースを取得する
- (2) : 現在位置 +
skipから実行スレッドの初期関数までのスタックトレースを取得する - (3) : 現在位置 +
skipから最大でmax_depth個のスタックトレースを取得する
事前条件
- (3) :
skip <= skip + max_depthであること
戻り値
- (1) :
- 現在の実行スレッドでの、現在の評価のスタックトレースを保持する
basic_stacktraceオブジェクトを構築する allocはstacktrace_entryオブジェクトの配列を保持するstd::vector型メンバ変数に渡される
- 現在の実行スレッドでの、現在の評価のスタックトレースを保持する
- (2) :
basic_stacktrace::current(alloc)で構築されたオブジェクトstのst.size()をnとして、- イテレータ範囲
[st.begin() + min(n, skip), st.end())とallocを、stacktrace_entryオブジェクトの配列を保持するstd::vector型メンバ変数として保持する。ただし、その初期化に失敗した場合、basic_stacktraceオブジェクトは空になる
- (3) :
basic_stacktrace::current(alloc)で構築されたオブジェクトstのst.size()をnとして、- イテレータ範囲
[st.begin() + min(n, skip), st.begin() + min(n, skip + max_depth))とallocを、stacktrace_entryオブジェクトの配列を保持するstd::vector型メンバ変数として保持する。ただし、その初期化に失敗した場合、basic_stacktraceオブジェクトは空になる
備考
- (1) : 正常にスタックトレースを取得できた場合、構築された
basic_stacktrace型のオブジェクトstの先頭要素st[0]は、現在の評価を表すstacktrace_entryオブジェクトであり、末尾要素st[st.size() - 1]は現在の実行スレッドの初期関数を表すstacktrace_entryオブジェクトである
例
全体のスタックトレースを取得する
#include <iostream>
#include <stacktrace>
void g() {
std::cout << std::stacktrace::current() << std::endl;
}
void f() {
g();
}
int main() {
f();
}
出力例 (GCC)
0# g() at /app/example.cpp:5
1# f() at /app/example.cpp:9
2# main at /app/example.cpp:13
3# at :0
4# __libc_start_main at :0
5# _start at :0
6#
現在位置からN個を除いたスタックトレースを取得する
#include <iostream>
#include <stacktrace>
void g() {
std::cout << std::stacktrace::current(1) << std::endl;
}
void f() {
g();
}
int main() {
f();
}
出力例 (GCC)
0# f() at /app/example.cpp:9
1# main at /app/example.cpp:13
2# at :0
3# __libc_start_main at :0
4# _start at :0
5#
指定範囲のスタックトレースを取得する
#include <iostream>
#include <stacktrace>
void g() {
std::cout << std::stacktrace::current(1, 2) << std::endl;
}
void f() {
g();
}
int main() {
f();
}
出力例 (GCC)
0# f() at /app/example.cpp:9
1# main at /app/example.cpp:13
バージョン
言語
- C++23
処理系
- Clang: ??
- GCC: 12 ✅
- Visual C++: ??