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. |
Louis Dionne | ed4a85e | 2021-06-18 13:33:14 -0400 | [diff] [blame^] | 11 | // REQUIRES: linux && (target={{aarch64-.+}} || target={{x86_64-.+}}) |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <dlfcn.h> |
| 15 | #include <signal.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | #include <unwind.h> |
| 22 | |
| 23 | _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) { |
| 24 | (void)arg; |
| 25 | Dl_info info = { 0, 0, 0, 0 }; |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 26 | |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 27 | // Unwind util the main is reached, above frames depend on the platform and |
| 28 | // architecture. |
| 29 | if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) && |
| 30 | info.dli_sname && !strcmp("main", info.dli_sname)) { |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 31 | _Exit(0); |
| 32 | } |
| 33 | return _URC_NO_REASON; |
| 34 | } |
| 35 | |
| 36 | void signal_handler(int signum) { |
| 37 | (void)signum; |
| 38 | _Unwind_Backtrace(frame_handler, NULL); |
| 39 | _Exit(-1); |
| 40 | } |
| 41 | |
Louis Dionne | cbfe017 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 42 | int main(int, char**) { |
Daniel Kiss | 163101b | 2020-09-16 23:03:19 +0200 | [diff] [blame] | 43 | signal(SIGUSR1, signal_handler); |
| 44 | kill(getpid(), SIGUSR1); |
| 45 | return -2; |
| 46 | } |