<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58236>58236</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            DebuggerStats::ReportStatistics can cause lock inversion deadlocks if run while targets are processing dyld load notification
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            clayborg
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jimingham
      </td>
    </tr>
</table>

<pre>
    DebuggerStats::ReportStatistics takes the lock returned by:

Module::GetAllocationModuleCollectionMutex

and holds it while it iterates through all the extant modules - thus acquiring each of them in turn.  But if another module is right in the middle of creating itself, it looks like it can end up having already taken its Module mutex, and then trying to get the AllocationModuleCollectionMutex.  That shouldn't happen for simple Modules, but the two-level creation of modules that happens when using DWARFDebugMap symbol plugin allows the containing module to be visible to ReportStatistics even though it's still in the process of making new sub-modules for the .o files.  That stalls against ReportStatistics, and then ReportStatistics stalls getting the Module mutex for one of these in process of being created modules and everything grinds to a halt.

Here's some sample stacks from a session in which this deadlock was observed.  These will look a little odd because of the references to lldb-rpc-server, that's just a wrapper around the LLDB framework that Xcode uses.

  Thread 0x2c4a28    Thread name "<lldb.process.internal-state(pid=12720)>"    770 samples (1-770)    priority 31 (base 31)
  770  thread_start + 8 (libsystem_pthread.dylib + 7724) [0x19aeece2c]
    770  _pthread_start + 148 (pthread.c:893 in libsystem_pthread.dylib + 28780) [0x19aef206c]
      770  lldb_private::HostThreadMacOSX::ThreadCreateTrampoline(void*) + 32 (LLDB + 62542236) [0x1167c119c]
        770  lldb_private::HostNativeThreadBase::ThreadCreateTrampoline(void*) + 100 (LLDB + 2620756) [0x112e9bd54]
          770  std::1::function::__func<lldb_private::Process::StartPrivateStateThread(bool)::$_6, std::__1::allocator<lldb_private::Process::StartPrivateStateThread(bool)::$_6>, void* ()>::operator()() + 24 (LLDB + 3373800) [0x112f53ae8]
            770  lldb_private::Process::RunPrivateStateThread(bool) + 1180 (LLDB + 3346788) [0x112f4d164]
              770  lldb_private::Process::HandlePrivateEvent(std::__1::shared_ptr<lldb_private::Event>&) + 308 (LLDB + 3328216) [0x112f488d8]
                770  lldb_private::Process::ShouldBroadcastEvent(lldb_private::Event*) + 360 (LLDB + 3343432) [0x112f4c448]
                  770  lldb_private::ThreadList::ShouldStop(lldb_private::Event*) + 840 (LLDB + 3681968) [0x112f9eeb0]
                    770  lldb_private::Thread::ShouldStop(lldb_private::Event*) + 956 (LLDB + 3649332) [0x112f96f34]
                      770  lldb_private::StopInfoBreakpoint::ShouldStopSynchronous(lldb_private::Event*) + 400 (LLDB + 3502008) [0x112f72fb8]
                        770  lldb_private::BreakpointSite::ShouldStop(lldb_private::StoppointCallbackContext*) + 136 (LLDB + 1841680) [0x112ddda10]
                          770  lldb_private::BreakpointLocationCollection::ShouldStop(lldb_private::StoppointCallbackContext*) + 192 (LLDB + 1780960) [0x112dcece0]
                            770  lldb_private::BreakpointLocation::ShouldStop(lldb_private::StoppointCallbackContext*) + 180 (LLDB + 1777068) [0x112dcddac]
                              770  DynamicLoaderMacOS::NotifyBreakpointHit(void*, lldb_private::StoppointCallbackContext*, unsigned long long, unsigned long long) + 1192 (LLDB + 4185204) [0x113019c74]
                                770  DynamicLoaderMacOS::AddBinaries(std::1::vector<unsigned long long, std::1::allocator<unsigned long long> > const&) + 784 (LLDB + 4186680) [0x11301a238]
                                  770  lldb_private::DynamicLoaderDarwin::AddModulesUsingImageInfos(std::__1::vector<lldb_private::DynamicLoaderDarwin::ImageInfo, std::__1::allocator<lldb_private::DynamicLoaderDarwin::ImageInfo> >&) + 1684 (LLDB + 4208280) [0x11301f698]
                                    770  lldb_private::Target::ModulesDidLoad(lldb_private::ModuleList&) + 380 (LLDB + 3554444) [0x112f7fc8c]
                                      770  lldb_private::BreakpointList::UpdateBreakpoints(lldb_private::ModuleList&, bool, bool) + 72 (LLDB + 1770684) [0x112dcc4bc]
                                        770  lldb_private::Breakpoint::ModulesChanged(lldb_private::ModuleList&, bool, bool) + 904 (LLDB + 1743524) [0x112dc5aa4]
                                          770  lldb_private::Breakpoint::ResolveBreakpointInModules(lldb_private::ModuleList&, bool) + 224 (LLDB + 1742220) [0x112dc558c]
                                            770  lldb_private::SearchFilter::SearchInModuleList(lldb_private::Searcher&, lldb_private::ModuleList&) + 344 (LLDB + 2229540) [0x112e3c524]
                                              770  lldb_private::SearchFilter::DoModuleIteration(lldb_private::SymbolContext const&, lldb_private::Searcher&) + 144 (LLDB + 2228692) [0x112e3c1d4]
                                                770  lldb_private::BreakpointResolverFileLine::SearchCallback(lldb_private::SearchFilter&, lldb_private::SymbolContext&, lldb_private::Address*) + 248 (LLDB + 1824776) [0x112dd9808]
                                                  770  lldb_private::Module::GetCompileUnitAtIndex(unsigned long) + 104 (LLDB + 2136864) [0x112e25b20]
                                                    770  lldb_private::SymbolFile::GetCompileUnitAtIndex(unsigned int) + 200 (LLDB + 3085300) [0x112f0d3f4]
                                                      770  SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(unsigned int) + 80 (LLDB + 6835456) [0x1132a0d00]
                                                        770  SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo(SymbolFileDWARFDebugMap::CompileUnitInfo*) + 1248 (LLDB + 6830824) [0x11329fae8]
                                                          770  std::__1::shared_ptr<DebugMapModule> std::__1::allocate_shared<DebugMapModule, std::1::allocator<DebugMapModule>, std::1::shared_ptr<lldb_private::Module>, unsigned int, lldb_private::FileSpec&, lldb_private::ArchSpec&, lldb_private::ConstString*, int, std::__1::chrono::time_point<std::__1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000000l> > >, void>(std::1::allocator<DebugMapModule> const&, std::1::shared_ptr<lldb_private::Module>&&, unsigned int&&, lldb_private::FileSpec&, lldb_private::ArchSpec&, lldb_private::ConstString*&&, int&&, std::__1::chrono::time_point<std::__1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000000l> > >&&) + 140 (LLDB + 6852460) [0x1132a4f6c]
                                                            770  lldb_private::Module::Module(lldb_private::FileSpec const&, lldb_private::ArchSpec const&, lldb_private::ConstString const*, unsigned long long, std::__1::chrono::time_point<std::__1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000000l> > > const&) + 336 (LLDB + 2132876) [0x112e24b8c]
                                                              770  std::__1::recursive_mutex::lock() + 16 (__threading_support:283 in libc++.1.dylib + 78548) [0x19ae1e2d4]
                                                                770  _pthread_mutex_firstfit_lock_slow + 248 (pthread_mutex.c:1490 in libsystem_pthread.dylib + 6812) [0x19aeeca9c]
                                                                  770  __psynch_mutexwait + 8 (libsystem_kernel.dylib + 15004) [0x19aeb5a9c]
                                                                   *770  psynch_mtxcontinue + 0 (kern_synch.c:785 in com.apple.kec.pthread + 8060) [0xfffffe000b861b9c] (blocked by turnstile waiting for lldb-rpc-server [12692] [unique pid 112195] thread 0x2c4a6c)


And the deadlock is caused by DebuggerStats::ReportStatistics:

  Thread 0x2c4a6c    Thread name "RPC packet thread for client tid 002c4a6b (2902635)"    770 samples (1-770)    priority 31 (base 31)
  770  thread_start + 8 (libsystem_pthread.dylib + 7724) [0x19aeece2c]
    770  _pthread_start + 148 (pthread.c:893 in libsystem_pthread.dylib + 28780) [0x19aef206c]
      770  Packets::RunReadThread(void*) + 12 (RPCCommon.cpp:1401 in lldb-rpc-server + 401672) [0x10239a108]
        770  Packets::ReadThread() + 276 (RPCCommon.cpp:1426 in lldb-rpc-server + 401960) [0x10239a228]
          770  Packets::ProcessPackets() + 528 (RPCCommon.cpp:1568 in lldb-rpc-server + 402628) [0x10239a4c4]
            770  rpc_common::Connection::HandleRPCPacket(rpc_common::RPCPacket&) + 72 (RPCCommon.cpp:578 in lldb-rpc-server + 390928) [0x102397710]
              770  rpc_common::Connection::PrivateHandleRPCPacket(rpc_common::RPCPacket&, rpc_common::RPCPacket&, bool&) + 628 (RPCCommon.cpp:400 in lldb-rpc-server + 387012) [0x1023967c4]
                770  rpc_server::_ZN4lldb8SBTarget13GetStatisticsEv::HandleRPCCall(rpc_common::Connection&, rpc_common::RPCStream&, rpc_common::RPCStream&) + 56 (RPCUserServer_SBTarget.cpp:144 in lldb-rpc-server + 343236) [0x10238bcc4]
                  770  lldb::SBTarget::GetStatistics() + 252 (LLDB + 642604) [0x112cb8e2c]
                    770  lldb_private::DebuggerStats::ReportStatistics(lldb_private::Debugger&, lldb_private::Target*) + 968 (LLDB + 3493400) [0x112f70e18]
                      770  lldb_private::SymbolFile::GetSymtab() + 44 (LLDB + 3085784) [0x112f0d5d8]
                        770  std::__1::recursive_mutex::lock() + 16 (__threading_support:283 in libc++.1.dylib + 78548) [0x19ae1e2d4]
                          770  _pthread_mutex_firstfit_lock_slow + 248 (pthread_mutex.c:1490 in libsystem_pthread.dylib + 6812) [0x19aeeca9c]
                            770  __psynch_mutexwait + 8 (libsystem_kernel.dylib + 15004) [0x19aeb5a9c]
                             *770  psynch_mtxcontinue + 0 (kern_synch.c:785 in com.apple.kec.pthread + 8060) [0xfffffe000b861b9c] (blocked by turnstile waiting for lldb-rpc-server [12692] [unique pid 112195] thread 0x2c4a28)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWltv2zoS_jXOCxFDou4PeUicdlug7RbJKfZgXwxKom02sqQjUkn9788MKdmSLNtKWmDRg01Tx6LImW--IYfDS1yku5t7HtfrNa8eFVNy5tzC7wMvi0phgZBKJJIo9sThc8NJViRPpOKqrnKekniHDaz7mdV8fi7SOuNGyr-4us2gPkgpcvNiUWQZT_RzrfiPbkuWp2RTZKkkQpGXjcg4fhGKV0xp3VVRrzeEZZnGwX8oliuy1WIluYbCWhKW_FWLSuRrwlmyIcUK626JyAkCnhNyV4PMFWF5AS-qpjkRklRivVG6IgjfijSFYmieVBzggzyhJM9WM7pAVFlRPEmSiSeNMWE54YC-LsmGPWNllkGzdKdpy7EpMeaTrbYahKC1oAnUVTtsoQqy5korv8AZGPHHhik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