Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 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 | |
| 10 | // Ensure that the unwinder can cope with the signal handler. |
Ulrich Weigand | f1108b6 | 2022-05-04 10:43:11 +0200 | [diff] [blame] | 11 | // REQUIRES: linux && (target={{aarch64-.+}} || target={{s390x-.+}} || target={{x86_64-.+}}) |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 12 | |
Louis Dionne | 31359e0 | 2022-03-02 17:49:13 -0500 | [diff] [blame] | 13 | // TODO: Figure out why this fails with Memory Sanitizer. |
| 14 | // XFAIL: msan |
| 15 | |
Florian Mayer | 47974e9 | 2022-08-04 15:02:52 -0700 | [diff] [blame] | 16 | #undef NDEBUG |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <dlfcn.h> |
| 19 | #include <signal.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | #include <unwind.h> |
| 26 | |
| 27 | _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) { |
| 28 | (void)arg; |
| 29 | Dl_info info = { 0, 0, 0, 0 }; |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 30 | |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 31 | // Unwind util the main is reached, above frames depend on the platform and |
| 32 | // architecture. |
| 33 | if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) && |
| 34 | info.dli_sname && !strcmp("main", info.dli_sname)) { |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 35 | _Exit(0); |
| 36 | } |
| 37 | return _URC_NO_REASON; |
| 38 | } |
| 39 | |
| 40 | void signal_handler(int signum) { |
| 41 | (void)signum; |
| 42 | _Unwind_Backtrace(frame_handler, NULL); |
| 43 | _Exit(-1); |
| 44 | } |
| 45 | |
Louis Dionne | cbfe017 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 46 | int main(int, char**) { |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 47 | signal(SIGUSR1, signal_handler); |
| 48 | kill(getpid(), SIGUSR1); |
| 49 | return -2; |
| 50 | } |