Bug 112263 - [C++23] std::stacktrace does not identify symbols in shared library
Summary: [C++23] std::stacktrace does not identify symbols in shared library
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libbacktrace (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: patch, testsuite-fail
: 111315 (view as bug list)
Depends on:
Blocks:
 
Reported: 2023-10-28 15:35 UTC by vincenzo Innocente
Modified: 2023-11-06 23:30 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-11-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description vincenzo Innocente 2023-10-28 15:35:54 UTC
using 
gcc version 14.0.0 20231028 (experimental) [master r14-4988-g5d2a360f0a5] (GCC)
that contains the fix for #111936

This simple example  [1]
when run as a single executable prints all symbols in the stacktrace
when the nested functions are in a shared library their names are missing
c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINMAIN -DINLIB ; ./a.out
   0# nested_func2(int) at /afs/cern.ch/user/i/innocent/public/ctest/testStacktrace.cpp:13
   1# nested_func(int) at /afs/cern.ch/user/i/innocent/public/ctest/testStacktrace.cpp:18
   2# func(int) at /afs/cern.ch/user/i/innocent/public/ctest/testStacktrace.cpp:26
   3# main at /afs/cern.ch/user/i/innocent/public/ctest/testStacktrace.cpp:31
   4#      at :0
   5# _start at :0
   6#


c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINLIB -fpic -shared -o liba.so ; c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINMAIN -L. -la -Wl,-rpath=. ; ./a.out
   0#      at :0
   1#      at :0
   2# func(int) at /afs/cern.ch/user/i/innocent/public/ctest/testStacktrace.cpp:26
   3# main at /afs/cern.ch/user/i/innocent/public/ctest/testStacktrace.cpp:31
   4#      at :0
   5# _start at :0
   6#



[1]
cat testStacktrace.cpp
//compile and run with either
// c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINMAIN -DINLIB; ./a.out
// or
// c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINLIB -fpic -shared -o liba.so;c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINMAIN -L. -la -Wl,-rpath=.; ./a.out
//
#include <iostream>
#include <stacktrace>


#ifdef INLIB
int nested_func2(int c)
{
    std::cout << std::stacktrace::current() << '\n';
    return c + 1;
}
int nested_func(int c)
{
    return nested_func2(c + 1);
}
#else
int nested_func(int c);
#endif
#ifdef INMAIN
int func(int b)
{
    return nested_func(b + 1);
}

int main()
{
    std::cout << func(777);
   return 0;
}
#endif
Comment 1 Jonathan Wakely 2023-10-28 21:04:12 UTC
I'm not sure if this is a libstdc++ problem or should be component=libbacktrace
Comment 2 vincenzo Innocente 2023-10-30 13:43:24 UTC
I suspect libbacktrace even if I do not have ways to test it outside std::stacktrace
Comment 3 Ian Lance Taylor 2023-10-31 01:07:51 UTC
What OS and processor?  Is this x86 GNU/Linux?
Comment 4 vincenzo Innocente 2023-10-31 10:45:49 UTC
intel x86_64 
uname -a
Linux patatrack01 4.18.0-477.13.1.el8_8.x86_64 #1 SMP Thu May 18 10:27:05 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux

boost::backtrace works
can provide example
Comment 5 vincenzo Innocente 2023-11-01 11:16:38 UTC
so if I add to
    std::cout << std::stacktrace::current() << '\n';
I get what needed
   Dl_info dlinfo;
   for (auto & entry : std::stacktrace::current() ) {
     dladdr((const void*)(entry.native_handle()),&dlinfo);
     std::cout << dlinfo.dli_sname << ' ' << dlinfo.dli_fname <<'\n';
   }

 c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINLIB -fpic -shared -o liba.so -ldl ; c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINMAIN -L. -la -Wl,-rpath=. ; ./a.out
   0#      at :0
   1#      at :0
   2# func(int) at /data/user/innocent/MallocProfiler/tests/testStacktrace.cpp:44
   3# main at /data/user/innocent/MallocProfiler/tests/testStacktrace.cpp:49
   4#      at :0
   5# _start at :0
   6#

_Z12nested_func2i ./liba.so
_Z11nested_funci ./liba.so

of course not de-mangled....

so is it a feature or a defect?

I'm not sure how the implementation works (did not look to the code)
dladdr can be slow and may "hang" in some situations.
so it would be useful to have an option that the "name" is not immediately resolved
and have a function that returns the name from the native_handle "asynchronously"
Comment 6 vincenzo Innocente 2023-11-01 13:09:12 UTC
Sorry, made the (almost) full exercise:
read the doc in 
https://en.cppreference.com/w/cpp/utility/stacktrace_entry
and the code in stacktrace header file and in libstdc++-v3/src/c++23/stacktrace.cc
(have not read the specs in the C++23 standard)
indeed the entry implementation has just the handle as data member
and the details are retrieved when the "Query" methods are called.
This appears to happen in stacktrace_entry::_Info::_M_populate(native_handle_type pc)
which in turn calls
::__glibcxx_backtrace_pcinfo
if this fails it calls
::__glibcxx_backtrace_syminfo

so most probably the issue is in this last function unless there is a problem with the logic in _M_populate that I failed to identify.
Comment 8 vincenzo Innocente 2023-11-03 09:55:05 UTC
Thanks Ian for the patch.
For testing I will need the full git diff (including the makefile itself as my 
autoconf is not compatible with gcc14).

Backports down to gcc12 will be appreciated.
Could you please notify here when the patch enters the various main branches?
Comment 9 Jonathan Wakely 2023-11-03 16:32:28 UTC
*** Bug 111315 has been marked as a duplicate of this bug. ***
Comment 10 Andrew Pinski 2023-11-04 05:56:16 UTC
I think we can mark this as confirmed since there is a patch submitted and the duplicated bug confirmed that patch fixes the issue.
Comment 11 Ian Lance Taylor 2023-11-05 00:05:49 UTC
vincenzo: the patch in the linked e-mail is the complete diff.  There are no changes to generated Makefile.in files.
Comment 12 vincenzo Innocente 2023-11-05 10:49:43 UTC
confirm that the patch solves the issue

c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINLIB -fpic -shared -o liba.so -ldl;c++ -std=c++23 testStacktrace.cpp -lstdc++exp -g -DINMAIN -L. -la -Wl,-rpath=.; ./a.out
   0# nested_func2(int) at /data/user/innocent/MallocProfiler/tests/testStacktrace.cpp:63
   1# nested_func(int) at /data/user/innocent/MallocProfiler/tests/testStacktrace.cpp:93
   2# func(int) at /data/user/innocent/MallocProfiler/tests/testStacktrace.cpp:101
   3# main at /data/user/innocent/MallocProfiler/tests/testStacktrace.cpp:106
   4# __libc_start_main at :0
   5# _start at :0
   6#

what is the last empty entry is a different story I suppose (not an issue at the moment).

Thanks again for the fast action
Comment 13 GCC Commits 2023-11-06 23:11:59 UTC
The master branch has been updated by Ian Lance Taylor <ian@gcc.gnu.org>:

https://gcc.gnu.org/g:2b64e4a54042fb8f75f1c1429eb1c13afb9fa118

commit r14-5173-g2b64e4a54042fb8f75f1c1429eb1c13afb9fa118
Author: Ian Lance Taylor <iant@golang.org>
Date:   Mon Nov 6 15:09:18 2023 -0800

    libstdc++: use -D_GNU_SOURCE when building libbacktrace
    
            PR libbacktrace/111315
            PR libbacktrace/112263
            * acinclude.m4: Set -D_GNU_SOURCE in BACKTRACE_CPPFLAGS and when
            grepping link.h for dl_iterate_phdr.
            * configure: Regenerate.
Comment 14 GCC Commits 2023-11-06 23:12:34 UTC
The releases/gcc-13 branch has been updated by Ian Lance Taylor <ian@gcc.gnu.org>:

https://gcc.gnu.org/g:8484e3479fe80aebdc4a2406473aef906055b4f8

commit r13-8006-g8484e3479fe80aebdc4a2406473aef906055b4f8
Author: Ian Lance Taylor <iant@golang.org>
Date:   Mon Nov 6 15:12:05 2023 -0800

    libstdc++: use -D_GNU_SOURCE when building libbacktrace
    
            PR libbacktrace/111315
            PR libbacktrace/112263
            * acinclude.m4: Set -D_GNU_SOURCE in BACKTRACE_CPPFLAGS and when
            grepping link.h for dl_iterate_phdr.
            * configure: Regenerate.
Comment 15 GCC Commits 2023-11-06 23:13:03 UTC
The releases/gcc-12 branch has been updated by Ian Lance Taylor <ian@gcc.gnu.org>:

https://gcc.gnu.org/g:56909c9d4842925749cd9e061ef4afa0501f85e9

commit r12-9961-g56909c9d4842925749cd9e061ef4afa0501f85e9
Author: Ian Lance Taylor <iant@golang.org>
Date:   Mon Nov 6 15:12:41 2023 -0800

    libstdc++: use -D_GNU_SOURCE when building libbacktrace
    
            PR libbacktrace/111315
            PR libbacktrace/112263
            * acinclude.m4: Set -D_GNU_SOURCE in BACKTRACE_CPPFLAGS and when
            grepping link.h for dl_iterate_phdr.
            * configure: Regenerate.
Comment 16 Ian Lance Taylor 2023-11-06 23:13:37 UTC
Fix committed.
Comment 17 Jonathan Wakely 2023-11-06 23:30:34 UTC
Thanks, Ian!