[all-commits] [llvm/llvm-project] f56ea1: [libunwind] Unwind through Linux riscv sigreturn t...

Fangrui Song via All-commits all-commits at lists.llvm.org
Sat May 6 11:17:15 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: f56ea14c299490671c1c157a6fb08ce3e452be2d
      https://github.com/llvm/llvm-project/commit/f56ea14c299490671c1c157a6fb08ce3e452be2d
  Author: Feng Wang <wangfeng at eswincomputing.com>
  Date:   2023-05-06 (Sat, 06 May 2023)

  Changed paths:
    M libunwind/src/UnwindCursor.hpp
    M libunwind/test/signal_unwind.pass.cpp
    M libunwind/test/unwind_leaffunction.pass.cpp

  Log Message:
  -----------
  [libunwind] Unwind through Linux riscv sigreturn trampoline

Similar to D90898 (Linux AArch64) and D124765 (SystemZ).

On an Arch Linux RISC-V (riscv64gc), the following code

```
#define _GNU_SOURCE
#include <dlfcn.h>
#include <libunwind.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

static void handler(int signo) {
  unw_context_t context;
  unw_cursor_t cursor;
  unw_getcontext(&context);
  unw_init_local(&cursor, &context);
  unw_word_t pc, sp;
  do {
    unw_get_reg(&cursor, UNW_REG_IP, &pc);
    unw_get_reg(&cursor, UNW_REG_SP, &sp);
    printf("pc=0x%016zx sp=0x%016zx", (size_t)pc, (size_t)sp);
    Dl_info info = {};
    if (dladdr((void *)pc, &info))
      printf(" %s:%s", info.dli_fname, info.dli_sname ? info.dli_sname : "");
    puts("");
  } while (unw_step(&cursor) > 0);
  exit(0);
}

int main() {
  signal(SIGUSR1, handler);
  raise(SIGUSR1);
  return 1;
}
```

linked with `-Wl,--export-dynamic` gives an output like
```
pc=0x0000000000010a82 sp=0x00007fffd8a0b910 ./b:
pc=0x00007fffa7e77800 sp=0x00007fffd8a0c520 linux-vdso.so.1:__vdso_rt_sigreturn
pc=0x00007fffa7d73bee sp=0x00007fffd8a0c960 /usr/lib/libc.so.6:
pc=0x00007fffa7d3ed66 sp=0x00007fffd8a0c9b0 /usr/lib/libc.so.6:gsignal
pc=0x0000000000010a3c sp=0x00007fffd8a0c9c0 ./b:main
pc=0x00007fffa7d2f1d4 sp=0x00007fffd8a0c9e0 /usr/lib/libc.so.6:
pc=0x00007fffa7d2f27c sp=0x00007fffd8a0cb10 /usr/lib/libc.so.6:__libc_start_main
pc=0x00000000000109a0 sp=0x00007fffd8a0cb60 ./b:_start
```

Co-Authored-By: Fangrui Song <i at maskray.me>

Reviewed By: #libunwind, MaskRay

Differential Revision: https://reviews.llvm.org/D148499




More information about the All-commits mailing list