Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 1 | //===---------------------- backtrace_test.cpp ----------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 8 | |
Louis Dionne | 8c61114 | 2020-04-17 10:29:15 -0400 | [diff] [blame] | 9 | // UNSUPPORTED: no-exceptions |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 10 | |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 11 | #include <assert.h> |
Dan Albert | e5f1521 | 2014-08-29 16:09:32 +0000 | [diff] [blame] | 12 | #include <stddef.h> |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 13 | #include <unwind.h> |
| 14 | |
| 15 | extern "C" _Unwind_Reason_Code |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 16 | trace_function(struct _Unwind_Context*, void* ntraced) { |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 17 | (*reinterpret_cast<size_t*>(ntraced))++; |
| 18 | // We should never have a call stack this deep... |
| 19 | assert(*reinterpret_cast<size_t*>(ntraced) < 20); |
| 20 | return _URC_NO_REASON; |
| 21 | } |
| 22 | |
Saleem Abdulrasool | 0b7b36a | 2016-08-28 18:16:00 +0000 | [diff] [blame] | 23 | __attribute__ ((__noinline__)) |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 24 | void call3_throw(size_t* ntraced) { |
| 25 | try { |
| 26 | _Unwind_Backtrace(trace_function, ntraced); |
| 27 | } catch (...) { |
| 28 | assert(false); |
| 29 | } |
| 30 | } |
| 31 | |
Saleem Abdulrasool | 0b7b36a | 2016-08-28 18:16:00 +0000 | [diff] [blame] | 32 | __attribute__ ((__noinline__, __disable_tail_calls__)) |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 33 | void call3_nothrow(size_t* ntraced) { |
| 34 | _Unwind_Backtrace(trace_function, ntraced); |
| 35 | } |
| 36 | |
Saleem Abdulrasool | 0b7b36a | 2016-08-28 18:16:00 +0000 | [diff] [blame] | 37 | __attribute__ ((__noinline__, __disable_tail_calls__)) |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 38 | void call2(size_t* ntraced, bool do_throw) { |
| 39 | if (do_throw) { |
| 40 | call3_throw(ntraced); |
| 41 | } else { |
| 42 | call3_nothrow(ntraced); |
| 43 | } |
| 44 | } |
| 45 | |
Saleem Abdulrasool | 0b7b36a | 2016-08-28 18:16:00 +0000 | [diff] [blame] | 46 | __attribute__ ((__noinline__, __disable_tail_calls__)) |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 47 | void call1(size_t* ntraced, bool do_throw) { |
| 48 | call2(ntraced, do_throw); |
| 49 | } |
| 50 | |
Louis Dionne | 504bc07 | 2020-10-08 13:36:33 -0400 | [diff] [blame^] | 51 | int main(int, char**) { |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 52 | size_t throw_ntraced = 0; |
| 53 | size_t nothrow_ntraced = 0; |
| 54 | |
| 55 | call1(¬hrow_ntraced, false); |
| 56 | |
| 57 | try { |
| 58 | call1(&throw_ntraced, true); |
| 59 | } catch (...) { |
| 60 | assert(false); |
| 61 | } |
| 62 | |
| 63 | // Different platforms (and different runtimes) will unwind a different number |
| 64 | // of times, so we can't make any better assumptions than this. |
| 65 | assert(nothrow_ntraced > 1); |
| 66 | assert(throw_ntraced == nothrow_ntraced); // Make sure we unwind through catch |
Dan Albert | 2c012d4 | 2014-08-29 15:26:06 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | } |