Repository: wolfpld/tracy
File: public/client/TracyCallstack.cpp (C++)

Code before:
            image->m_name = info->dlpi_name;
        }
        else
        {
            if( !cache->m_imageName )
            {
                Dl_info dlInfo;
                if( dladdr( (void *)info->dlpi_addr, &dlInfo ) )
                {
                    cache->m_imageName = dlInfo.dli_fname;
                }
            }
            image->m_name = cache->m_imageName;
        }

        return 0;
    }

    void Refresh()
    {
        m_images->clear();

        dl_iterate_phdr( Callback, this );
        
        std::sort( m_images->begin(), m_images->end(), 
            []( const ImageEntry& lhs, const ImageEntry& rhs ) { return lhs.m_startAddress > rhs.m_startAddress; } );
    }

    const ImageEntry* GetImageEntryForAddress( void* address ) const 
    {
        auto it = std::lower_bound( m_images->begin(), m_images->end(), address, 
            []( const ImageEntry& lhs, const void* rhs ) { return lhs.m_startAddress > rhs; } );

        if( it != m_images->end() && address < it->m_endAddress )
        {
            return it;
        }
        return nullptr;
    }
};
#endif //#ifdef TRACY_USE_IMAGE_CACHE

// when "TRACY_SYMBOL_OFFLINE_RESOLVE" is set, instead of fully resolving symbols at runtime,
// simply resolve the offset and image name (which will be enough the resolving to be done offline)
#ifdef TRACY_SYMBOL_OFFLINE_RESOLVE
constexpr bool s_shouldResolveSymbolsOffline = true;
#else
static bool s_shouldResolveSymbolsOffline = false;
bool ShouldResolveSymbolsOffline()
{
    const char* symbolOfflineResolve = GetEnvVar( "TRACY_SYMBOL_OFFLINE_RESOLVE" );

Review comment (suggestion):
Is `image->m_name` valid after this function returns? My guess would be that the `info` argument is only valid inside this function, and the `dlInfo` provided filename will be invalidated after the next call to `dladdr`.

Code after:

        const char* imageName = nullptr;
        // the base executable name isn't provided when iterating with dl_iterate_phdr, get it with dladdr()
        if( info->dlpi_name && info->dlpi_name[0] != '\0' )
        {
            imageName = info->dlpi_name;
        }
        else
        {
            Dl_info dlInfo;
            if( dladdr( (void *)info->dlpi_addr, &dlInfo ) )
            {
                imageName = dlInfo.dli_fname;
            }
        }

        if(imageName != nullptr)
        {
            size_t sz = strlen( imageName ) + 1;
            image->m_name = (char*)tracy_malloc( sz );
            memcpy( (void*)image->m_name, imageName, sz );
        }
        else
        {
            image->m_name = nullptr;
        }

        return 0;
    }

    void Refresh()
    {
        Clear();

        dl_iterate_phdr( Callback, this );
        
        std::sort( m_images->begin(), m_images->end(), 
            []( const ImageEntry& lhs, const ImageEntry& rhs ) { return lhs.m_startAddress > rhs.m_startAddress; } );
    }

    const ImageEntry* GetImageForAddressImpl( void* address ) const 
    {
        auto it = std::lower_bound( m_images->begin(), m_images->end(), address, 
            []( const ImageEntry& lhs, const void* rhs ) { return lhs.m_startAddress > rhs; } );

        if( it != m_images->end() && address < it->m_endAddress )
        {
            return it;
        }
        return nullptr;
    }