Search code examples
c++windowsdebuggingwindbg

How do I get CDB to show source lines for Microsoft’s C++ Standard Library?


I’m working on contributing to Descent 3. The pull request that I’m currently working on has a Windows-only bug. I would like to use a debugger in order to help me track down the bug.

Specifically, the abort() function gets called when you try to open the Multiplayer menu. I was able to use CDB to get a backtrace for the crash:

0:000:x86> kp
ChildEBP RetAddr
0075e080 71b11701     ucrtbased!issue_debug_notification(wchar_t * message = 0x71a7384c "abort() has been called")+0x30 [minkernel\crts\ucrt\src\appcrt\internal\report_runtime_error.cpp @ 28]
0075e098 71b23eba     ucrtbased!__acrt_report_runtime_error(wchar_t * message = 0x71a7384c "abort() has been called")+0x11 [minkernel\crts\ucrt\src\appcrt\internal\report_runtime_error.cpp @ 154]
0075e0a8 00da135d     ucrtbased!abort(void)+0x1a [minkernel\crts\ucrt\src\appcrt\startup\abort.cpp @ 51]
0075e0b0 00b89e89     Descent3!__std_fs_directory_iterator_close(__std_fs_dir_handle _Handle = 0n34772936 (No matching enumerant))+0x1d [D:\a\_work\1\s\src\vctools\crt\github\stl\src\filesystem.cpp @ 255]
0075e0c0 00b89e49     Descent3!std::filesystem::_Find_file_handle::~_Find_file_handle(void)+0x19 [C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\filesystem @ 611]
0075e0cc 00b8a3e6     Descent3!std::filesystem::_Dir_enum_impl::~_Dir_enum_impl(void)+0x19
0075e0d8 00b87d5d     Descent3!std::filesystem::_Dir_enum_impl::`scalar deleting destructor'(void)+0x16
0075e0e4 00b8a96a     Descent3!std::_Destroy_in_place<std::filesystem::_Dir_enum_impl>(struct std::filesystem::_Dir_enum_impl * _Obj = 0x136a6110)+0xd [C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\xmemory @ 293]
0075e0f4 00b8a83d     Descent3!std::_Ref_count_obj2<std::filesystem::_Dir_enum_impl>::_Destroy(void)+0x1a [C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\memory @ 2101]
0075e104 00b8a7f2     Descent3!std::_Ref_count_base::_Decref(void)+0x2d [C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\memory @ 1163]
0075e110 00b89da6     Descent3!std::_Ptr_base<std::filesystem::_Dir_enum_impl>::_Decref(void)+0x22 [C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\memory @ 1380]
0075e11c 00b89f16     Descent3!std::shared_ptr<std::filesystem::_Dir_enum_impl>::~shared_ptr<std::filesystem::_Dir_enum_impl>(void)+0x16 [C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\memory @ 1671]
0075e128 00b7ed01     Descent3!std::filesystem::directory_iterator::~directory_iterator(void)+0x16 [C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\filesystem @ 2700]
0075eab8 00b20216     Descent3!MainMultiplayerMenu(void)+0x7c1 [D:\VC\Partially mine\Descent3\repo\Descent3\multi_ui.cpp @ 457]
0075f578 00a7d8e6     Descent3!MainMenu(void)+0x626 [D:\VC\Partially mine\Descent3\repo\Descent3\menu.cpp @ 876]
0075f598 00a7d754     Descent3!MainLoop(void)+0x86 [D:\VC\Partially mine\Descent3\repo\Descent3\descent.cpp @ 543]
0075f654 00c82f13     Descent3!Descent3(void)+0x214 [D:\VC\Partially mine\Descent3\repo\Descent3\descent.cpp @ 507]
0075f660 00c825d1     Descent3!oeD3Win32App::run(void)+0x13 [D:\VC\Partially mine\Descent3\repo\Descent3\winmain.cpp @ 145]
0075f7cc 00c81a7b     Descent3!HandledWinMain(struct HINSTANCE__ * hInst = 0x009e0000, struct HINSTANCE__ * hPrevInst = 0x00000000, char * szCmdLine = 0x0203136e "-WINDOWED -NOINTRO -PILOT JAYMAN2000", int nCmdShow = 0n10)+0x271 [D:\VC\Partially mine\Descent3\repo\Descent3\winmain.cpp @ 525]
0075f818 00dbaf7d     Descent3!WinMain(struct HINSTANCE__ * hInst = 0x009e0000, struct HINSTANCE__ * hPrevInst = 0x00000000, char * szCmdLine = 0x0203136e "-WINDOWED -NOINTRO -PILOT JAYMAN2000", int nCmdShow = 0n10)+0x6b [D:\VC\Partially mine\Descent3\repo\Descent3\winmain.cpp @ 536]
0075f838 00dbadfa     Descent3!invoke_main(void)+0x2d [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 107]
0075f894 00dbac8d     Descent3!__scrt_common_main_seh(void)+0x15a [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288]
0075f89c 00dbaff8     Descent3!__scrt_common_main(void)+0xd [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 331]
0075f8a4 760d7ba9     Descent3!WinMainCRTStartup(void * __formal = 0x004e0000)+0x8 [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_winmain.cpp @ 17]
0075f8b4 779dc10b     KERNEL32!BaseThreadInitThunk+0x19
0075f90c 779dc08f     ntdll32!__RtlUserThreadStart+0x2b
0075f91c 00000000     ntdll32!_RtlUserThreadStart+0x1b
0:000:x86> 

I wanted to know why __std_fs_directory_iterator_close() was calling abort(), so I tried setting a breakpoint on __std_fs_directory_iterator_close():

0:011> bp __std_fs_directory_iterator_close
0:011> g
[…]
Entering MainMultiplayerMenu()
Breakpoint 0 hit
Descent3!__std_fs_directory_iterator_close:
00da1340 55              push    ebp
0:000:x86> 

I always use CDB in source debugging mode (I run CDB with these command-line arguments: -lines -c "ld Descent3; l+s; l+t" -o "%cd%\Descent3.exe".), but it’s not showing the source lines for __std_fs_directory_iterator_close(). CDB shows source lines for functions that are in Descent 3’s source code, but it doesn’t show source lines for functions that are in the C++ standard library.

How do I get CDB to show source lines for STL (Microsoft’s C++ Standard Library)?


Solution

  • You can get CDB to show source lines for STL (Microsoft’s C++ Standard Library) by downloading a copy of the STL repository and setting CDB’s source path:

    1. Make sure that Git is installed:

      winget install Git.Git
      
    2. Restart your Command Prompt in order to make sure that Git is on your Path.

    3. Clone the STL repository:

      git clone https://github.com/microsoft/STL
      
    4. (Optional) Make sure that the Git repo and your code are using the same version of STL:

      1. Change directory into the STL repo:

        CD STL
        
      2. Determine the appropriate Git tag. I’m using Visual Studio 2022 version 17.10.2, so the appropriate tag for me is vs-2022-17.10. You can find a list of Git tags for the STL repo here.

      3. Switch to the tag:

        git switch --detach <tag>
        
    5. Start CDB:

      <path-to-cdb> -lines -c "l+t; l+s; .srcfix; .srcpath+ <path-to-STL-repo>" -o <path-to-debugee>
      

      Note: Don’t escape <path-to-STL-repo>, even if it contains spaces.