1eb8650a7SLouis Dionne //===----------------------------------------------------------------------===//
2e434b34fSJonathan Roelofs //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
857e446daSAsiri Rathnayake
98c61114cSLouis Dionne // UNSUPPORTED: no-exceptions
1057e446daSAsiri Rathnayake
11*9a220dc6SKazushi (Jam) Marukawa // VE only supports SjLj and doesn't provide _Unwind_Backtrace.
12*9a220dc6SKazushi (Jam) Marukawa // UNSUPPORTED: target={{ve-.*}}
13*9a220dc6SKazushi (Jam) Marukawa
14e434b34fSJonathan Roelofs #include <assert.h>
15e434b34fSJonathan Roelofs #include <stddef.h>
16e434b34fSJonathan Roelofs #include <unwind.h>
17e434b34fSJonathan Roelofs
18e434b34fSJonathan Roelofs extern "C" _Unwind_Reason_Code
trace_function(struct _Unwind_Context *,void * ntraced)19a140cba7SEric Fiselier trace_function(struct _Unwind_Context*, void* ntraced) {
20e434b34fSJonathan Roelofs (*reinterpret_cast<size_t*>(ntraced))++;
21e434b34fSJonathan Roelofs // We should never have a call stack this deep...
22e434b34fSJonathan Roelofs assert(*reinterpret_cast<size_t*>(ntraced) < 20);
23e434b34fSJonathan Roelofs return _URC_NO_REASON;
24e434b34fSJonathan Roelofs }
25e434b34fSJonathan Roelofs
260b7b36a9SSaleem Abdulrasool __attribute__ ((__noinline__))
call3_throw(size_t * ntraced)27e434b34fSJonathan Roelofs void call3_throw(size_t* ntraced) {
28e434b34fSJonathan Roelofs try {
29e434b34fSJonathan Roelofs _Unwind_Backtrace(trace_function, ntraced);
30e434b34fSJonathan Roelofs } catch (...) {
31e434b34fSJonathan Roelofs assert(false);
32e434b34fSJonathan Roelofs }
33e434b34fSJonathan Roelofs }
34e434b34fSJonathan Roelofs
350b7b36a9SSaleem Abdulrasool __attribute__ ((__noinline__, __disable_tail_calls__))
call3_nothrow(size_t * ntraced)36e434b34fSJonathan Roelofs void call3_nothrow(size_t* ntraced) {
37e434b34fSJonathan Roelofs _Unwind_Backtrace(trace_function, ntraced);
38e434b34fSJonathan Roelofs }
39e434b34fSJonathan Roelofs
400b7b36a9SSaleem Abdulrasool __attribute__ ((__noinline__, __disable_tail_calls__))
call2(size_t * ntraced,bool do_throw)41e434b34fSJonathan Roelofs void call2(size_t* ntraced, bool do_throw) {
42e434b34fSJonathan Roelofs if (do_throw) {
43e434b34fSJonathan Roelofs call3_throw(ntraced);
44e434b34fSJonathan Roelofs } else {
45e434b34fSJonathan Roelofs call3_nothrow(ntraced);
46e434b34fSJonathan Roelofs }
47e434b34fSJonathan Roelofs }
48e434b34fSJonathan Roelofs
490b7b36a9SSaleem Abdulrasool __attribute__ ((__noinline__, __disable_tail_calls__))
call1(size_t * ntraced,bool do_throw)50e434b34fSJonathan Roelofs void call1(size_t* ntraced, bool do_throw) {
51e434b34fSJonathan Roelofs call2(ntraced, do_throw);
52e434b34fSJonathan Roelofs }
53e434b34fSJonathan Roelofs
main(int,char **)54504bc07dSLouis Dionne int main(int, char**) {
55e434b34fSJonathan Roelofs size_t throw_ntraced = 0;
56e434b34fSJonathan Roelofs size_t nothrow_ntraced = 0;
57e434b34fSJonathan Roelofs
58e434b34fSJonathan Roelofs call1(¬hrow_ntraced, false);
59e434b34fSJonathan Roelofs
60e434b34fSJonathan Roelofs try {
61e434b34fSJonathan Roelofs call1(&throw_ntraced, true);
62e434b34fSJonathan Roelofs } catch (...) {
63e434b34fSJonathan Roelofs assert(false);
64e434b34fSJonathan Roelofs }
65e434b34fSJonathan Roelofs
66e434b34fSJonathan Roelofs // Different platforms (and different runtimes) will unwind a different number
67e434b34fSJonathan Roelofs // of times, so we can't make any better assumptions than this.
68e434b34fSJonathan Roelofs assert(nothrow_ntraced > 1);
69e434b34fSJonathan Roelofs assert(throw_ntraced == nothrow_ntraced); // Make sure we unwind through catch
70e434b34fSJonathan Roelofs return 0;
71e434b34fSJonathan Roelofs }
72