signal_unwind.pass.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Ensure that the unwinder can cope with the signal handler.
  10. // REQUIRES: linux && (target-aarch64 || target-x86_64)
  11. #include <assert.h>
  12. #include <dlfcn.h>
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <unwind.h>
  20. _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
  21. (void)arg;
  22. Dl_info info = { 0, 0, 0, 0 };
  23. // Unwind util the main is reached, above frames depend on the platform and
  24. // architecture.
  25. if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
  26. info.dli_sname && !strcmp("main", info.dli_sname)) {
  27. _Exit(0);
  28. }
  29. return _URC_NO_REASON;
  30. }
  31. void signal_handler(int signum) {
  32. (void)signum;
  33. _Unwind_Backtrace(frame_handler, NULL);
  34. _Exit(-1);
  35. }
  36. int main(int, char**) {
  37. signal(SIGUSR1, signal_handler);
  38. kill(getpid(), SIGUSR1);
  39. return -2;
  40. }