Skip to content

Commit 07e9e02

Browse files
committed
C++26対応として<debugging>ライブラリを追加 (close #1232)
1 parent 806a8c8 commit 07e9e02

File tree

7 files changed

+302
-2
lines changed

7 files changed

+302
-2
lines changed

GLOBAL_QUALIFY_LIST.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
* <cstdio>[link /reference/cstdio.md]
9898
* <cstdlib>[link /reference/cstdlib.md]
9999
* <ctime>[link /reference/ctime.md]
100+
* <debugging>[link /reference/debugging.md]
100101
* <deque>[link /reference/deque/deque.md]
101102
* std::deque[link /reference/deque/deque.md]
102103
* <exception>[link /reference/exception.md]

lang/cpp26.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
8989
- 文字列エンコーディングを識別するライブラリとして、[`<text_encoding>`](/reference/text_encoding.md.nolink)を追加
9090
- 並行処理におけるデータの参照・更新を行うRCU (Read Copy Update) のライブラリとして、[`<rcu>`](/reference/rcu.md.nolink)を追加
9191
- 並行処理において参照中のデータが更新されないよう保護するハザードポインタのライブラリとして、[`<hazard_pointer>`](/reference/hazard_pointer.md.nolink)を追加
92-
- デバッグサポートのライブラリとして[`<debugging>`](/reference/debugging.md.nolink)を追加
92+
- デバッグサポートのライブラリとして[`<debugging>`](/reference/debugging.md)を追加
9393
- 線形代数ライブラリとして[`<linalg>`](/reference/linalg.md)を追加
9494
- コンパイル時に容量を固定する可変長配列クラスのライブラリとして[`<inplace_vector>`](/reference/inplace_vector.md.nolink)を追加
9595

reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157

158158
| ヘッダ | 説明 | 対応バージョン |
159159
|----------------------------------------|------------------|----------------|
160-
| [`<debugging>`](/reference/debugging.md.nolink) | デバッグサポート | C++26 |
160+
| [`<debugging>`](/reference/debugging.md) | デバッグサポート | C++26 |
161161

162162

163163
## <a id="io" href="#io">入出力ライブラリ</a>

reference/debugging.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# debugging
2+
* debugging[meta header]
3+
* cpp26[meta cpp]
4+
5+
`<debugging>`ヘッダでは、プログラムのデバッグ実行をサポートする機能を提供する。
6+
7+
| 名前 | 説明 | 対応バージョン |
8+
|------|------|----------------|
9+
| [`breakpoint`](debugging/breakpoint.md) | ブレークポイントを設置する (functional) | C++26 |
10+
| [`breakpoint_if_debugging`](debugging/breakpoint_if_debugging.md) | デバッガ実行のみブレークポイントを設置する (functional) | C++26 |
11+
| [`is_debugger_present`](debugging/is_debugger_present.md) | デバッガ実行中か判定する (functional) | C++26 |
12+
13+
14+
## この機能が必要になった背景・経緯
15+
開発プラットフォームによってデバッガやブレークポイントの機能は提供されているが、本ライブラリのようにプログラム中に明示的にブレークポイントを設置し、プログラムとデバッガを対話させることでデバッグ体験が向上することがある。
16+
17+
実装経験としては以下のようなものがあり、これらをこのライブラリで標準化した:
18+
19+
| 開発環境 | 機能 |
20+
|----------|------|
21+
| Microsoft C/C++ Optimizing Compiler | `__debugbreak()`関数 (無条件ブレークポイント) |
22+
| Win32 API | `IsDebuggerPresent()`関数 (デバッガ実行中か判定) |
23+
| LLVM Clang | `__builtin_debugtrap()`組み込み関数 (無条件ブレークポイント) |
24+
| arm Keil, ARM Compiler | `__breakpoint()`関数 (無条件ブレークポイント) |
25+
| Portable Snippetsライブラリ | `psnip_trap()`関数 (無条件ブレークポイント) |
26+
| Debug Breakライブラリ | `debug_break()`関数 (無条件ブレークポイント) |
27+
| Boost.Testライブラリ | `debugger_break()`関数 (無条件ブレークポイント)<br/> `under_debugger()`関数 (出張っg実行中か判定) |
28+
| EASTLライブラリ | `EASTL_DEBUG_BREAK()`マクロ (無条件ブレークポイント)、 |
29+
| Catch2ライブラリ | `CATCH_TRAP`マクロ (無条件ブレークポイント)<br/> `CATCH_BREAK_INTO_DEBUGGER`マクロ (条件付きブレークポイント)<br/> `isDebuggerActive()`関数 (デバッガ実行中か判定) |
30+
| JUCEライブラリ | `JUCE_BREAK_IN_DEBUGGER`マクロ (無条件ブレークポイント)<br/> `juce_isRunningUnderDebugger()`関数、`Process::isRunningUnderDebugger()`関数 (デバッガ実行中か判定) |
31+
| ImGuiライブラリ | `IM_DEBUG_BREAK()`マクロ (無条件ブレークポイント) |
32+
| AWS C SDK | `aws_debug_break()`関数 (条件付きブレークポイント)<br/> `aws_is_debugger_present()`関数 (デバッガ実行中か判定) |
33+
| UnrealEngine | `UE_DEBUG_BREAK`マクロ (条件付きブレークポイント)<br/> `IsDebuggerPresent()`関数 (デバッガ実行中か判定) |
34+
35+
36+
## バージョン
37+
### 言語
38+
- C++26
39+
40+
41+
## 参照
42+
- [P2546R5 Debugging Support](https://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2546r5.html)

reference/debugging/breakpoint.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# breakpoint
2+
* debugging[meta header]
3+
* std[meta namespace]
4+
* function[meta id-type]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
void breakpoint() noexcept; // (1) C++26
10+
}
11+
```
12+
13+
## 概要
14+
ブレークポイントを設置する。
15+
16+
この関数は無条件のブレークポイントであり、デバッガがプログラムを監視しているかに関わらずプログラムの一時停止 (ブレーク) を試みる。
17+
18+
19+
20+
## 効果
21+
標準規格としては、この関数の意味論は実装定義である。
22+
23+
実際の動作としては、この関数が呼び出されるとプログラムの実行が一時停止され、以下のいずれかの時点までデバッガに制御が渡される:
24+
25+
- デバッガによってプログラムが終了される、または
26+
- デバッガが、この関数が呼び出されなかったかのようにプログラムの実行を再開する
27+
28+
29+
## 例外
30+
投げない
31+
32+
33+
## 備考
34+
- 実装としては、生成されるコードがプラットフォームに対して可能な限り最小になるように最適化することが期待される。例として、x86ターゲット環境ではINT3命令をひとつだけ生成することが期待される
35+
36+
37+
## 例
38+
```cpp example
39+
#include <print>
40+
#include <debugging>
41+
#include <cmath>
42+
43+
// なんらかの処理
44+
double g(double a, double b) {
45+
return a / b;
46+
}
47+
48+
double f(double a, double b) {
49+
double ret = g(a, b);
50+
if (std::isnan(ret)) {
51+
// 演算結果でNaNが発生したらブレークし、
52+
// デバッガでパラメータ (ローカル変数) などを確認する
53+
std::breakpoint();
54+
}
55+
}
56+
57+
int main() {
58+
double ret = f(2.0, 0.0);
59+
std::println("{}", ret);
60+
}
61+
```
62+
* std::breakpoint[color ff0000]
63+
* std::isnan[link /reference/cmath/isnan.md]
64+
65+
### 出力
66+
```
67+
```
68+
69+
70+
## バージョン
71+
### 言語
72+
- C++26
73+
74+
### 処理系
75+
- [Clang](/implementation.md#clang): 19 [mark noimpl]
76+
- [GCC](/implementation.md#gcc): 14 [mark noimpl]
77+
- [Visual C++](/implementation.md#visual_cpp): 2022 Update 10
78+
79+
80+
## 参照
81+
- [P2546R5 Debugging Support](https://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2546r5.html)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# breakpoint_if_debugging
2+
* debugging[meta header]
3+
* std[meta namespace]
4+
* function[meta id-type]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
void breakpoint_if_debugging() noexcept; // (1) C++26
10+
}
11+
```
12+
13+
## 概要
14+
デバッガ実行のみブレークポイントを設置する。
15+
16+
この関数は条件付きブレークポイントであり、デバッガがプログラムを監視中であればプログラムを一時停止 (ブレーク) するが、そうでなければ何もしないよう動作する。
17+
18+
19+
## 効果
20+
以下と等価:
21+
22+
```cpp
23+
if (is_debugger_present()) {
24+
breakpoint();
25+
}
26+
```
27+
* is_debugger_present()[link is_debugger_present.md]
28+
* breakpoint()[link breakpoint.md]
29+
30+
31+
## 例外
32+
投げない
33+
34+
35+
## 備考
36+
- 実装としては、生成されるコードがプラットフォームに対して可能な限り最小になるように最適化することが期待される。例として、x86ターゲット環境ではINT3命令をひとつだけ生成することが期待される
37+
38+
39+
##
40+
```cpp example
41+
#include <print>
42+
#include <debugging>
43+
#include <cmath>
44+
45+
// なんらかの処理
46+
double g(double a, double b) {
47+
return a / b;
48+
}
49+
50+
double f(double a, double b) {
51+
double ret = g(a, b);
52+
if (std::isnan(ret)) {
53+
// 演算結果でNaNが発生したらブレークし、
54+
// デバッガでパラメータ (ローカル変数) などを確認する
55+
std::breakpoint_if_debugging();
56+
}
57+
}
58+
59+
int main() {
60+
double ret = f(2.0, 0.0);
61+
std::println("{}", ret);
62+
}
63+
```
64+
* std::breakpoint_if_debugging[color ff0000]
65+
* std::isnan[link /reference/cmath/isnan.md]
66+
67+
### 出力
68+
```
69+
```
70+
71+
72+
## バージョン
73+
### 言語
74+
- C++26
75+
76+
### 処理系
77+
- [Clang](/implementation.md#clang): 19 [mark noimpl]
78+
- [GCC](/implementation.md#gcc): 14 [mark noimpl]
79+
- [Visual C++](/implementation.md#visual_cpp): 2022 Update 10
80+
81+
82+
## 参照
83+
- [P2546R5 Debugging Support](https://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2546r5.html)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# is_debugger_present
2+
* debugging[meta header]
3+
* std[meta namespace]
4+
* function[meta id-type]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
bool is_debugger_present() noexcept; // (1) C++26
10+
}
11+
```
12+
13+
## 概要
14+
デバッガ実行中か判定する。
15+
16+
この関数は、デバッガがプログラムを監視中かどうかを判定する。プラットフォーム固有の結果を決定できない場合、ユーザーがこの関数を定義することで柔軟に動作を制御できる。
17+
18+
19+
## 置き換え可能
20+
ユーザーのプログラムでこの関数を定義することで、標準ライブラリで定義されるデフォルトの動作を置き換えることができる。
21+
22+
## 要求される振る舞い
23+
- この関数は事前条件をもたないこと
24+
25+
26+
## デフォルトの振る舞い
27+
- 実装定義
28+
29+
30+
## 効果
31+
デバッガがプログラムを監視中である場合、実装は`true`を返す。
32+
33+
34+
## 例外
35+
投げない
36+
37+
38+
## 備考
39+
- 実装は、プログラムがデバッガによって監視されているかどうかを判定するために、必要に応じて即時クエリを実行する
40+
- Windowsまたは同等のシステムでは、この関数の動作はWin32関数の`::IsDebuggerPresent()`を呼び出すことで実現できる
41+
- POSIX環境では、監視親プロセスをチェックすることで実現できる。その際、監視親プロセスがデバッガであるかどうかは最善の努力で判定する
42+
- この関数は、結果のキャッシュを規定しない。既存の実装もデバッガの存在クエリについての結果をキャッシュするようなことはしておらず、そのコストが問題にはなっていなかった。また、結果をキャッシュする場合、[`std::breakpoint_if_debugging()`](breakpoint_if_debugging.md)のインタフェースにも影響してしまう
43+
44+
45+
## 例
46+
```cpp example
47+
#include <print>
48+
#include <debugging>
49+
#include <cmath>
50+
51+
// なんらかの処理
52+
double g(double a, double b) {
53+
return a / b;
54+
}
55+
56+
double f(double a, double b) {
57+
double ret = g(a, b);
58+
if (std::isnan(ret)) {
59+
// 演算結果でNaNが発生したらブレークし、
60+
// デバッガでパラメータ (ローカル変数) などを確認する
61+
if (std::is_debugger_present()) {
62+
std::breakpoint();
63+
}
64+
}
65+
}
66+
67+
int main() {
68+
double ret = f(2.0, 0.0);
69+
std::println("{}", ret);
70+
}
71+
```
72+
* std::is_debugger_present[color ff0000]
73+
* std::breakpoint[link breakpoint.md]
74+
* std::isnan[link /reference/cmath/isnan.md]
75+
76+
### 出力
77+
```
78+
```
79+
80+
81+
## バージョン
82+
### 言語
83+
- C++26
84+
85+
### 処理系
86+
- [Clang](/implementation.md#clang): 19 [mark noimpl]
87+
- [GCC](/implementation.md#gcc): 14 [mark noimpl]
88+
- [Visual C++](/implementation.md#visual_cpp): 2022 Update 10
89+
90+
91+
## 参照
92+
- [P2546R5 Debugging Support](https://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2546r5.html)
93+
- [P2810R4 `is_debugger_present` `is_replaceable`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2810r4.html)

0 commit comments

Comments
 (0)