Search code examples
c++thread-sanitizer

How to find line number in ThreadSanitizer stack trace


I compile using Clang with -g3 and -O1 flags, but TSan complains that it found a data-race and it outputs a totally obscure stack trace with no clear line numbers.

How to find line numbers in this case?

Output on Pastebin since Stack Overflow doesn't support more than 30k chars.

https://pastebin.com/raw/6izxznym


Solution

  • Look for "/home" for finding your code.

    The stacks of the threads look nice with well shown line numbers. Your MediaServer::initialize() created the thread T1.

    Thread T1 (tid=2667937, running) created by main thread at:
      #2 MediaServer::initialize /home/MediaServer/MediaServerMethods.cpp:1808
      #3 main /home/MediaServer/MediaServer.cpp:33
    

    T1 continued MediaServer initialization and created the thread 7.

    Thread T7 (tid=2667963, running) created by thread T1 at:
      #31 SimpleWeb::SocketServerBase<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor> >::start() /usr/local/include/simple-websocket-server/server_ws.hpp:437
      #32 MediaServer::initialize()::$_7::operator()() const /home/MediaServer/MediaServerMethods.cpp:1812
    

    T7 received some message and called your code, that was creating std::shared_ptr that was creating some std::string from a C string perhaps.

    Previous write of size 8 at 0x7b1800006060 by thread T7:
      #1 void std::__cxx11::basic_string<char>::_M_construct<char*>(char*, char*, std::forward_iterator_tag) /usr/include/c++/11/bits/basic_string.tcc:219
      #2 rtc::impl::Certificate::Certificate()
      ...
      #8 std::make_shared<rtc::PeerConnection>() /usr/include/c++/11/bits/shared_ptr.h:876
      #9 RTCTransport::createPeerConnection() /home/MediaServer/MediaServerMethods.cpp:458
      #10 MediaServer::initialize()::$_5::operator()(...) const /home/MediaServer/MediaServerMethods.cpp:1761
    

    Finally thread T1 was attempting to construct the same std::string without using a lock guard.

    WARNING: ThreadSanitizer: data race (pid=2667935)
      Read of size 8 at 0x7b1800006060 by thread T1:
        #1 void std::__cxx11::basic_string<char>::_M_construct<char*>(char*, char*, std::forward_iterator_tag) /usr/include/c++/11/bits/basic_string.tcc:225
        #2 rtc::impl::Certificate::fingerprint[abi:cxx11]() const
        ...
        #24 MediaServer::initialize()::$_7::operator()() const /home/MediaServer/MediaServerMethods.cpp:1812