blob: 9954e8b7afaea39cdd8fe28f929314331e24572b [file] [log] [blame]
Dan Albert14690902014-08-29 15:26:06 +00001//===---------------------- backtrace_test.cpp ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +00009
10// UNSUPPORTED: libcxxabi-no-exceptions
11
Dan Albert14690902014-08-29 15:26:06 +000012#include <assert.h>
Dan Alberta3a836a2014-08-29 16:09:32 +000013#include <stddef.h>
Dan Albert14690902014-08-29 15:26:06 +000014#include <unwind.h>
15
16extern "C" _Unwind_Reason_Code
17trace_function(struct _Unwind_Context* context, void* ntraced) {
18 (*reinterpret_cast<size_t*>(ntraced))++;
19 // We should never have a call stack this deep...
20 assert(*reinterpret_cast<size_t*>(ntraced) < 20);
21 return _URC_NO_REASON;
22}
23
24void call3_throw(size_t* ntraced) {
25 try {
26 _Unwind_Backtrace(trace_function, ntraced);
27 } catch (...) {
28 assert(false);
29 }
30}
31
32void call3_nothrow(size_t* ntraced) {
33 _Unwind_Backtrace(trace_function, ntraced);
34}
35
36void call2(size_t* ntraced, bool do_throw) {
37 if (do_throw) {
38 call3_throw(ntraced);
39 } else {
40 call3_nothrow(ntraced);
41 }
42}
43
44void call1(size_t* ntraced, bool do_throw) {
45 call2(ntraced, do_throw);
46}
47
48int main() {
49 size_t throw_ntraced = 0;
50 size_t nothrow_ntraced = 0;
51
52 call1(&nothrow_ntraced, false);
53
54 try {
55 call1(&throw_ntraced, true);
56 } catch (...) {
57 assert(false);
58 }
59
60 // Different platforms (and different runtimes) will unwind a different number
61 // of times, so we can't make any better assumptions than this.
62 assert(nothrow_ntraced > 1);
63 assert(throw_ntraced == nothrow_ntraced); // Make sure we unwind through catch
Dan Albert14690902014-08-29 15:26:06 +000064 return 0;
65}