Skip to content

Unable to get function name using name() with mingw-w64+libbacktrace #212

@prbegd

Description

@prbegd

Hi,

I'm building with mingw-w64 on Windows and using libbacktrace as implementation. I try to get function names using name() on a frame, but I got register_frame_ctor. But other functions like address(),, source_file() and source_line() are working correctly.

Reproducing

Here is an example:

#define BOOST_STACKTRACE_USE_BACKTRACE
#include <backtrace.h>
#include <boost/stacktrace.hpp>
#include <iostream>

int main()
{
    boost::stacktrace::stacktrace trace;
    for (unsigned i = 0; i < trace.size(); ++i) {
        std::cout << '#' << i << ' ' << trace[i].name() << " (" << trace[i].address() << ") at " << trace[i].source_file() << ':' << trace[i].source_line() << '\n';
    }
    return 0;
}

Result: (I have already installed boost headers and libbacktrace at E:/libraries/xxx and built libbacktrace)

PS C:\Desktop\demo> g++ main.cpp -o main.exe -IE:/libraries/boost/include -IE:/libraries/libbacktrace -lbacktrace -std=c++20 -g
PS C:\Desktop\demo> ./main
#0 register_frame_ctor (0x7ff7ac312dcc) at E:/libraries/boost/include/boost/stacktrace/stacktrace.hpp:109
#1 register_frame_ctor (0x7ff7ac3014a2) at C:/Desktop/demo/main.cpp:8
#2 register_frame_ctor (0x7ff7ac3012ef) at :0
#3 register_frame_ctor (0x7ff7ac301406) at :0
#4 register_frame_ctor (0x7ffdada7e8d7) at :0
#5 register_frame_ctor (0x7ffdae90c53c) at :0

Expected Behavior

trace[i].name() should return correct function names (such as main).

Environment

  • OS: Windows 11 24H2
  • Compiler: MinGW-w64 GCC 14.2.0 x86_64 posix-seh-msvcrt
  • Boost: 1.88.0

Investigation

I found the possible reason using debugger: function calling backtrace_syminfo. I found that this function is being called on my machine but it shouldn't being called, because this function is using symbol table, which programs on Windows don't export it by default. (I think we still need -rdynamic to make it work on Linux) (What I said here might be wrong)

I change the code in include/boost/stacktrace/detail/libbacktrace_impls.hpp to as following, so it will use debug info and it turns to works on my machine.

    if (state) { // line 173
        ::backtrace_pcinfo(
            state,
            reinterpret_cast<uintptr_t>(addr),
            boost::stacktrace::detail::libbacktrace_full_callback,
            boost::stacktrace::detail::libbacktrace_error_callback,
            &data
        );
        // removed backtrace_syminfo
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions