# 生成された C++ コードのデバッグ

> Understand how to use Visual Studio to debug your generated C++ code.

Visual Studio を使用して、ユニバーサル Windows プラットフォーム (UWP) アプリケーション用に生成された C++ コードをデバッグできます。

## 生成された C++ コードのクラスとメソッドの名前##understand-class-and-method-naming-in-generated-c++-code

### IL2CPP クラス##il2cpp-classes

IL2CPP クラスは、`<ClassName>_t#number` の形式に従います。

* `<ClassName>` はクラス名
* 任意の `#number` は一意のタイプ番号です。

IL2CPP クラスの例:

* `String_t` - [String クラス](https://learn.microsoft.com/en-us/dotnet/api/system.string)
* `Object_t` - [Object クラス](https://learn.microsoft.com/en-us/dotnet/api/system.object)
* `Type_t` - [Type クラス](https://learn.microsoft.com/en-us/dotnet/api/system.type)
* `StringBuilder_t26` - [StringBuilder クラス](https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder)

### IL2CPP メソッド##il2cpp-methods

IL2CPP メソッドは、`<ClassName>_<MethodName>_m#number` の形式に従います。

* `<ClassName>` はメソッドの宣言型のクラス名
* `<MethodName>` はメソッド名
* `#number` は一意のメソッド番号

IL2CPP メソッドの例:

* `ConfigurationSection_DoDeserializeSection_m1275` - [ConfigurationSection クラスの DeserializeSection メソッド](https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationsection.deserializesection)
* `String_Format_m4102` - [String クラスの Format メソッド](https://learn.microsoft.com/en-us/dotnet/api/system.string.format)
* `Mathf_Sqrt_m289` - [Mathf クラスの Sqrt メソッド](https://learn.microsoft.com/en-us/dotnet/api/system.mathf.sqrt)

### IL2CPP 静的フィールド構造体##il2cpp-static-field-structures

静的フィールド構造体は、`<ClassName>_t#number_StaticFields` の形式に従います。構造体名の最初の部分は宣言型と同じです。

静的フィールド構造体の例:

* `StringBuilder_t26_StaticFields`
* `Thing_t24_StaticFields`

### C++ コメント##c++-comments

各クラスまたはメソッド定義の前に、C++ は完全なクラスまたはメソッド名を示すコメントを自動的に生成します。

C++ コメントの例:

```
    // System.Text.StringBuilder
    struct StringBuilder_t26  : public Object_t
    {
        // System.Int32 System.Text.StringBuilder::_length
        int32_t length_1;
        // System.Int32 System.Text.StringBuilder::_maxCapacity
        int32_t maxCapacity_2;
    };
```

## 変数の値の確認##observe-variable-values

生成された C++ コードは、[Visual Studio デバッガーを使用](https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/get-started/using-visual-studio-tools-for-unity?pivots=windows#unity-debugging)して変数の値を確認することでデバッグできます。

デバッガーを停止したい場所に [Visual Studio でブレークポイントを設定](/engine/6000.0/manual/scripting/debugging-and-diagnostics/managed-code-debugging.md#BreakpointsVS)して変数を確認できます。Visual Studio では、変数にマウスオーバーするか、[Watch ウィンドウを使用](https://learn.microsoft.com/en-us/visualstudio/debugger/watch-and-quickwatch-windows?)して変数の値を確認できます。

## 静的フィールドの確認##observe-a-static-field

IL2CPP では、Unity は Il2CppClass インスタンスに静的フィールドを格納します。静的フィールドの値を確認するには、以下を行う必要があります。

1. コード内でその型の Il2CppClass 構造体への[ポインター](https://learn.microsoft.com/en-us/cpp/cpp/pointers-cpp)を探します。

   **注意**これらのポインターは、使用するメソッドのスコープ内にありますが、1 回確認した後は、アプリケーション実行中、同じメモリアドレスにとどまります。
2. そのポインタから `static_fields` フィールドの値を取得します。これは、その特定の型の静的フィールドを含むメモリブロックへのポインターです。
3. 対応する静的フィールド構造体に値をキャストします。
4. [Visual Studio デバッガー](https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/get-started/using-visual-studio-tools-for-unity?pivots=windows#unity-debugging)で値を確認します。

## 例外の検証##investigate-exceptions

IL2CPP はネイティブ C++ 例外を使用して .NET 例外を実装します。

コードの例外を調べるには、以下を行います。

* [ウォッチウィンドウ](https://learn.microsoft.com/en-us/visualstudio/debugger/watch-and-quickwatch-windows?)での例外オブジェクトの検査
* C++ 例外で[デバッガーのブレークを有効化](https://learn.microsoft.com/en-us/visualstudio/debugger/managing-exceptions-with-the-debugger)

## 追加リソース##additional-resources

* [Visual Studio を使用した C++ コードのデバッグ方法に関する Microsoft ドキュメント](https://learn.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger-cpp?)
* [Visual Studio での Unity デバッグに関する Microsoft ドキュメント](https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/get-started/using-visual-studio-tools-for-unity?pivots=windows#unity-debugging)
* [IL2CPP の概要](/engine/6000.0/manual/scripting/compilation-and-code-reload/backends/il2cpp.md)
* [UWP の IL2CPP スクリプティングバックエンド](/engine/6000.0/manual/platform-specific/windows-store/uwp-developing/uwp-il2cpp-scripting.md)
