2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 // TODO: Investigate these failures on x86_64 macOS back deployment
11 // UNSUPPORTED: target=x86_64-apple-darwin{{.+}}
13 // TODO: Figure out why this fails with Memory Sanitizer.
16 #include <libunwind.h>
20 void backtrace(int lower_bound
) {
21 unw_context_t context
;
22 unw_getcontext(&context
);
25 unw_init_local(&cursor
, &context
);
33 } while (unw_step(&cursor
) > 0);
35 if (n
< lower_bound
) {
44 void test2(int i
, int j
) {
49 void test3(int i
, int j
, int k
) {
55 unw_context_t context
;
56 unw_getcontext(&context
);
59 unw_init_local(&cursor
, &context
);
62 int ret
= unw_get_proc_info(&cursor
, &info
);
63 if (ret
!= UNW_ESUCCESS
)
66 // Set the IP to an address clearly outside any function.
67 unw_set_reg(&cursor
, UNW_REG_IP
, (unw_word_t
)0);
69 ret
= unw_get_proc_info(&cursor
, &info
);
70 if (ret
!= UNW_ENOINFO
)
74 void test_reg_names() {
75 unw_context_t context
;
76 unw_getcontext(&context
);
79 unw_init_local(&cursor
, &context
);
81 int max_reg_num
= -100;
84 #elif defined(__x86_64__)
88 const char prefix
[] = "unknown";
89 for (int i
= -2; i
< max_reg_num
; ++i
) {
90 if (strncmp(prefix
, unw_regname(&cursor
, i
), sizeof(prefix
) - 1) == 0)
94 if (strncmp(prefix
, unw_regname(&cursor
, max_reg_num
+ 1),
95 sizeof(prefix
) - 1) != 0)
99 #if defined(__x86_64__)
100 void test_reg_get_set() {
101 unw_context_t context
;
102 unw_getcontext(&context
);
105 unw_init_local(&cursor
, &context
);
107 for (int i
= 0; i
< 17; ++i
) {
108 const unw_word_t set_value
= 7;
109 if (unw_set_reg(&cursor
, i
, set_value
) != UNW_ESUCCESS
)
112 unw_word_t get_value
= 0;
113 if (unw_get_reg(&cursor
, i
, &get_value
) != UNW_ESUCCESS
)
116 if (set_value
!= get_value
)
121 void test_fpreg_get_set() {
122 unw_context_t context
;
123 unw_getcontext(&context
);
126 unw_init_local(&cursor
, &context
);
128 // get/set is not implemented for x86_64 fpregs.
129 for (int i
= 17; i
< 33; ++i
) {
130 const unw_fpreg_t set_value
= 7;
131 if (unw_set_fpreg(&cursor
, i
, set_value
) != UNW_EBADREG
)
134 unw_fpreg_t get_value
= 0;
135 if (unw_get_fpreg(&cursor
, i
, &get_value
) != UNW_EBADREG
)
140 void test_reg_get_set() {}
141 void test_fpreg_get_set() {}
144 int main(int, char**) {
151 test_fpreg_get_set();