libunwind cannot resolve symbol names

26 Views Asked by At

I have this code:

#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <stdio.h>

void backtrace() {
  unw_cursor_t cursor;
  unw_context_t context;

  unw_getcontext(&context);
  unw_init_local(&cursor, &context);

  while (unw_step(&cursor) > 0) {
    unw_word_t offset, pc;
    unw_get_reg(&cursor, UNW_REG_IP, &pc);
    if (pc == 0) {
      break;
    }
    printf("0x%lx:", pc);

    char sym[256];
    if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
      printf(" (%s+0x%lx)\n", sym, offset);
    } else {
      printf(" -- No symbol :(\n");
    }
  }
}

void foo() { backtrace(); }

void bar() { foo(); }

int main(int argc, char **argv) {
  bar();

  return 0;
}

I compile with:

gcc -g3 in.c -lunwind

But if I run the executable, I just get a bunch of addresses and no symbol names, despite being compiled with debuginfo. I tried playing around with DWARF versions using -gdwarf-2|3|4|5, but had no success.

I use gcc 13.2.0 (Or clang 18.1.2) with mingw64-w64-x86_64-libunwind 18.1.2-1 from pacman in a MINGW64 environment provided by the MSYS2 project. If I run the same code on linux, compiled with the same parameters, it works.

0

There are 0 best solutions below