https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110196

--- Comment #15 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to [email protected] from comment #5)
> Indeed, changing _S_init to the following improved it:
> 
> ```
>   static backtrace_state *_S_init() {
> 
>     static backtrace_state *__state = []() {
>       auto getpath = []() -> std::string {
>         char buf[PATH_MAX + 1];
>         if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) == -1) {
>           return "";
>         }
>         std::string str(buf);
>         return str.substr(0, str.rfind('/'));
>       };
> 
>       auto exec_filename = getpath();
>       return backtrace_create_state(exec_filename.c_str(), 1, _S_err_handler,
>                                     nullptr);
>     }();
>     return __state;
>   }
> ```

This is wrong, because exec_filename.c_str() is a temporary that goes out of
scope, and the documentation for backtrace_create_state in backtrace.h says "If
not NULL, FILENAME must point to a permanent buffer." Making exec_filename a
local static would be correct.

I also don't understand how this change had any effect on the output:

(In reply to [email protected] from comment #6)
> output is now 
> ```
>    0# main at /mnt/c/Users/Febbe/workspace/test/test/main.cpp:7
>    1# __libc_start_call_main at ../sysdeps/nptl/libc_start_call_main.h:58
>    2# __libc_start_main_impl at ../csu/libc-start.c:392
>    3# _start at :0
>    4# 
> ```

How does passing a filename to backtrace_create_state cause the filenames and
line numbers to be shown in frames #2 and #3?  When I pass a filename to
backtrace_create_state, that doesn't happen for me.

Reply via email to