LoginSignup
6

More than 1 year has passed since last update.

簡易版 .NETのランタイムデバッグをする方法

Last updated at Posted at 2021-11-11

先日.NET6が正式リリースされました。

ということで、良い機会なのでランタイムのコードを読んでみているのですが、やっぱり動かしながらどんな値が通っているのかを見ながらの方が捗ります。

以下の公式ドキュメントを参考にしています。

ランタイムのソースコードをClone

git clone git@github.com:dotnet/runtime.git
cd runtime
git checkout v6.0.0

ランタイムのビルド

.\build.cmd -vs coreclr.sln -a x64 -c Debug

コマンド完了すると、VisualStudioが立ち上がります。
また、.dotnetというディレクトリが出来上がっており、中にランタイムのバイナリが入っています。

.NET実行可能アプリケーションの作成

C#のConsoleAppプロジェクトのことです。

cd artifacts
mkdir MyApp
cd MyApp
..\..\.dotnet\dotnet.exe new console

error IDE0073回避のために、Program.csにヘッダを追加します。
また、使用されているランタイムを確認するために、標準ライブラリのアセンブリのパスをダンプしておきます。

Program.cs
// <auto-generated />
Console.WriteLine(typeof(Console).Assembly.Location);
Console.WriteLine("Hello, World!");

ビルドします。

..\..\.dotnet\dotnet.exe build -p:Configuration=Debug

artifacts\MyApp\bin\Debug\net6.0\MyApp.exeが生成されます。

VisualStudioのデバッグの設定

build.cmdによってartifacts\obj\coreclr\windows.x64.Debug\ide\CoreCLR.slnがVisaualStudioで開かれている状態のはずです。(閉じてしまったらslnを再度開いてください)

適当なデバッグ用のプロジェクトを追加します。
image.pngimage.png
image.png

先ほど作成した.NETアプリでデバッグ設定をします。
image.png

スタートアップに設定
image.png

実行

Hello, World!
C:\Users\huser\Desktop\runtime\.dotnet\shared\Microsoft.NETCore.App\6.0.0-rc.1.21430.1\System.Console.dll

C:\Users\huser\Desktop\runtime\artifacts\MyApp\bin\Debug\net6.0\MyApp.exe (プロセス 24616) は、コード 0 で終了しました。
デバッグが停止したときに自動的にコンソールを閉じるには、[ツール] -> [オプション] -> [デバッグ] -> [デバッグの停止時に自 動的にコンソールを閉じる] を有効にします。
このウィンドウを閉じるには、任意のキーを押してください...

VisualStuido内でのMyAppのビルドを避けているのは、システムインストールのSDKではなく、リポジトリ内のSDKを使ってC#プロジェクトのビルドを行いたいためです。

エントリポイント

corehost.hwmainがエントリポイントになりますので、ソースコード上の好きなところにブレークポイントを置いてデバッグをすることができます。

#if defined(_WIN32)
int __cdecl wmain(const int argc, const pal::char_t* argv[])
#else
int main(const int argc, const pal::char_t* argv[])
#endif
{
    ...
    int exit_code = exe_start(argc, argv);
    ...
    return exit_code;
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6