Unity provides several options for debugging on Windows for forensic or live debugging of game and editor processes.
Unity allows two types of debugging: the native C++ debugging and the C# managed debugging.
Native Debugging stores symbols in pdb files for the associated binary files, such as exe and dll.
On Windows, the standard .NET managed symbols are stored in pdb files.
Unity provides a symbol store at http://symbolserver.unity3d.com/. Much like the Microsoft’s symbol store, you can use the Unity server URL in Windows Debugger (WinDbg), or Visual Studio 2019 and later for automatic symbol resolution and downloading.
To add a symbol store on WinDbg, use the .sympath command:
.sympath+ SRV*c:\\symbols-cache*http://symbolserver.unity3d.com/
上のパスで
.sympath+
プラス (+) は既存のシンボルパスを保持し、シンボルストアを追加します。
SRV*c:\\symbols-cache
SRV はフェッチ元のリモートサーバーを示します。一方、c:\\symbols
はローカルパスで、ダウンロードされたシンボルをキャッシュし、再度ダウンロードする前に最初にそこを参照します。
*http://symbolserver.unity3d.com/
フェッチ元のシンボルストアのパスです。
Live Debugging is the scenario of attaching a debugger to a process that’s running normal, or to a process where an exception has been caught. For the debugger to know what’s going on, the symbols must be included in the build using the steps above. Additionally, the game executable is named according to your game name, so the debugger may have issues finding the correct pdb
if it doesn’t have access to the renamed executable.
On Windows, Microsoft sets up automatically on application crashes to go to Dr Watson/Error Reporting to Microsoft. However, if you have Visual Studio or WinDbg installed, Microsoft provides an option to instead opt to debug the crashes.
For ease of installing, follow this registry file contents to install:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug]
"Auto"="1"
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug]
"Auto"="1"
エディターデバッグのための追加コンテンツです。
Unity.exe -dbgbreak
Will launch Unity and immediately offer a debugger to connect if the automatic crash handling is set up.
Windows provides facilities to investigate crash dump files (.dmp or .mdmp). Depending on the type of crash dump, you might either see stack information or the entire process memory. The contents of the dump file determine the cause of the crash, which typically has at least a stack to investigate (as long as it’s a valid stack).
To investigate a dump file, you can load it up via Visual Studio or WinDbg. While Visual Studio is a more friendly tool to use, it’s slightly limited than WinDbg.
Visual Studio を実行する場合、Unity による VS Code 用の UnityMixedCallstack 拡張を使用すると、デバッグをさらに簡単に行うことができます。
NullReferenceException
は、通常は以下のようになります。
1b45558c()
> mono-2.0-bdwgc.dll!malloc(unsigned int size=12) Line 163 + 0x5f bytes C
mono-2.0-bdwgc.dll!g_hash_table_insert_replace(_GHashTable * hash=0x065c3960, void * key=0x0018cba4, void * value=0x0018cb8c, int replace=457528232) Line 204 + 0x7 bytes C
mono-2.0-bdwgc.dll!mono_jit_runtime_invoke(_MonoMethod * method=0x242bf8b0, void * obj=0x065c3960, void ** params=0x0018cba4, MonoObject * * exc=0x0018cb8c) Line 4889 + 0xc bytes C
前出の例を再度見てみましょう。
1b45558c()
> mono-2.0-bdwgc.dll!malloc(unsigned int size=12) Line 163 + 0x5f bytes C
mono-2.0-bdwgc.dll!g_hash_table_insert_replace(_GHashTable * hash=0x065c3960, void * key=0x0018cba4, void * value=0x0018cb8c, int replace=457528232) Line 204 + 0x7 bytes C
mono-2.0-bdwgc.dll!mono_jit_runtime_invoke(_MonoMethod * method=0x242bf8b0, void * obj=0x065c3960, void ** params=0x0018cba4, MonoObject * * exc=0x0018cb8c) Line 4889 + 0xc bytes C
The lines without any information are managed frames. There is, however, a way to get the managed stack information: mono has a builtin function called mono_pmip
, which accepts the address of a stack frame and returns a char* with information. You can invoke mono_pmip
in the Visual Studio immediate window:
?(char*){,,mono-2.0-bdwgc.dll}mono_pmip((void*)0x1b45558c)
0x26a296c0 “ Tiles:OnPostRender () + 0x1e4 (1B4553A8 1B4555DC) [065C6BD0 - Unity Child Domain]”`
ノート: mono-2.0-bdwgc.dll
シンボルが適切にロードされている場合にのみ作動します。
Ocassionally, the application doesn’t crash with the debugger attached, or an application crashes on a remote device where the debugger isn’t available. However, you can still get useful information if you can get the dump file - follow the below steps to do so.
ノート: これらの手順は、Windows スタンドアロンとユニバーサル Windows プラットフォームがデスクトップで実行されている場合に適用できます。
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting
へ移動します。LocalDumps
フォルダーがない場合は作成します。"DumpFolder"=<FolderPath goes here> , e.g., C:\\Temp
"DumpCount"=dword:00000010
"DumpType"=dword:00000002