c++ - What's wrong with this code that's attempting to print a backtrace on Linux - Stack Overflow

I'm trying to print a backtrace after an exception occurs in my code.I acquire the backtrace lik

I'm trying to print a backtrace after an exception occurs in my code. I acquire the backtrace like:

constexpr size_t MAX_STACK_FRAMES{ 64 };
static void* stack_traces[MAX_STACK_FRAMES];
void posix_print_stack_trace()
{
    int i, trace_size = 0;
    char** messages = (char**)NULL;
    char buffer[1024]{};    // buffer for error message


    trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
    messages = backtrace_symbols(stack_traces, trace_size);

    /* skip the first couple stack frames (as they are this function and
       our handler) and also skip the last frame as it's (always?) junk. */
       // for (i = 3; i < (trace_size - 1); ++i)
       // we'll use this for now so you can see what's going on
    for (i = 0; i < trace_size; ++i)
    {
        if (addr2line(global_program_name, stack_traces[i]) != 0)

And use a function called addr2line to attempt to format the backtrace by invoking the addr2line command:

    /* have addr2line map the address to the relevant line in the code */
#ifdef __APPLE__
  /* apple does things differently... */
    sprintf(addr2line_cmd, "atos -o %.256s %p", program_name, addr);
#else
    sprintf(addr2line_cmd, "addr2line -f -p -e %.256s %p", program_name, addr);
#endif

The problem I have is that output I get looks like:

?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0

I'm sure it's how I'm invoking the addr2line command. If I need to adjust the address I pass to it, what do I need to do please?

I'm trying to print a backtrace after an exception occurs in my code. I acquire the backtrace like:

constexpr size_t MAX_STACK_FRAMES{ 64 };
static void* stack_traces[MAX_STACK_FRAMES];
void posix_print_stack_trace()
{
    int i, trace_size = 0;
    char** messages = (char**)NULL;
    char buffer[1024]{};    // buffer for error message


    trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
    messages = backtrace_symbols(stack_traces, trace_size);

    /* skip the first couple stack frames (as they are this function and
       our handler) and also skip the last frame as it's (always?) junk. */
       // for (i = 3; i < (trace_size - 1); ++i)
       // we'll use this for now so you can see what's going on
    for (i = 0; i < trace_size; ++i)
    {
        if (addr2line(global_program_name, stack_traces[i]) != 0)

And use a function called addr2line to attempt to format the backtrace by invoking the addr2line command:

    /* have addr2line map the address to the relevant line in the code */
#ifdef __APPLE__
  /* apple does things differently... */
    sprintf(addr2line_cmd, "atos -o %.256s %p", program_name, addr);
#else
    sprintf(addr2line_cmd, "addr2line -f -p -e %.256s %p", program_name, addr);
#endif

The problem I have is that output I get looks like:

?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0
?? ??:0

I'm sure it's how I'm invoking the addr2line command. If I need to adjust the address I pass to it, what do I need to do please?

Share Improve this question edited Nov 18, 2024 at 16:55 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Nov 16, 2024 at 14:47 David PartridgeDavid Partridge 3914 silver badges12 bronze badges 1
  • 1 Are you sure you compile your program with debug symbols? – dumbass Commented Nov 18, 2024 at 16:57
Add a comment  | 

1 Answer 1

Reset to default 0

Needed to convert absolute address to offset using:

std::uint64_t convertToVMA(void* addr)
{
    Dl_info info;
    struct link_map* link_map;
    dladdr1((void*)addr, &info, (void**)&link_map, RTLD_DL_LINKMAP);
    return reinterpret_cast<std::uint64_t>(addr) - link_map->l_addr;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745656162a4638554.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信