|
| 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