blob: 2ff50abbebb67e91b822abc43f466d17226c3b93 [file] [log] [blame]
Daniel Kiss163101b2020-09-16 23:03:19 +02001// -*- 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 Weigandf1108b62022-05-04 10:43:11 +020011// REQUIRES: linux && (target={{aarch64-.+}} || target={{s390x-.+}} || target={{x86_64-.+}})
Daniel Kiss163101b2020-09-16 23:03:19 +020012
Louis Dionne31359e02022-03-02 17:49:13 -050013// TODO: Figure out why this fails with Memory Sanitizer.
14// XFAIL: msan
15
Daniel Kiss163101b2020-09-16 23:03:19 +020016#include <assert.h>
17#include <dlfcn.h>
18#include <signal.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/types.h>
23#include <unistd.h>
24#include <unwind.h>
25
26_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
27 (void)arg;
28 Dl_info info = { 0, 0, 0, 0 };
Daniel Kiss163101b2020-09-16 23:03:19 +020029
Ryan Pricharda684bed2021-01-13 16:38:36 -080030 // Unwind util the main is reached, above frames depend on the platform and
31 // architecture.
32 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
33 info.dli_sname && !strcmp("main", info.dli_sname)) {
Daniel Kiss163101b2020-09-16 23:03:19 +020034 _Exit(0);
35 }
36 return _URC_NO_REASON;
37}
38
39void signal_handler(int signum) {
40 (void)signum;
41 _Unwind_Backtrace(frame_handler, NULL);
42 _Exit(-1);
43}
44
Louis Dionnecbfe0172020-10-08 13:36:33 -040045int main(int, char**) {
Daniel Kiss163101b2020-09-16 23:03:19 +020046 signal(SIGUSR1, signal_handler);
47 kill(getpid(), SIGUSR1);
48 return -2;
49}