blob: fec7d985b6f6a227026f41bbc89822bdc3f764f3 [file] [log] [blame]
Christopher Ferris7fb22872013-09-27 12:43:15 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070017#include <signal.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070018#include <stdio.h>
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080019#include <unistd.h>
20
21#include <memory>
22#include <vector>
23
24#include <unwindstack/Regs.h>
25#include <unwindstack/RegsGetLocal.h>
Christopher Ferris7fb22872013-09-27 12:43:15 -070026
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070027#include "backtrace_testlib.h"
28
29void test_loop_forever() {
30 while (1)
31 ;
32}
33
34void test_signal_handler(int) { test_loop_forever(); }
35
36void test_signal_action(int, siginfo_t*, void*) { test_loop_forever(); }
37
38int test_level_four(int one, int two, int three, int four, void (*callback_func)(void*),
39 void* data) {
Christopher Ferris7fb22872013-09-27 12:43:15 -070040 if (callback_func != NULL) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070041 callback_func(data);
Christopher Ferris7fb22872013-09-27 12:43:15 -070042 } else {
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070043 while (1)
44 ;
Christopher Ferris7fb22872013-09-27 12:43:15 -070045 }
46 return one + two + three + four;
47}
48
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070049int test_level_three(int one, int two, int three, int four, void (*callback_func)(void*),
50 void* data) {
51 return test_level_four(one + 3, two + 6, three + 9, four + 12, callback_func, data) + 3;
Christopher Ferris7fb22872013-09-27 12:43:15 -070052}
53
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070054int test_level_two(int one, int two, int three, int four, void (*callback_func)(void*), void* data) {
55 return test_level_three(one + 2, two + 4, three + 6, four + 8, callback_func, data) + 2;
Christopher Ferris7fb22872013-09-27 12:43:15 -070056}
57
Christopher Ferris5ea2c1f2017-03-23 14:55:01 -070058int test_level_one(int one, int two, int three, int four, void (*callback_func)(void*), void* data) {
59 return test_level_two(one + 1, two + 2, three + 3, four + 4, callback_func, data) + 1;
Christopher Ferris7fb22872013-09-27 12:43:15 -070060}
61
Christopher Ferris17e91d42013-10-21 13:30:52 -070062int test_recursive_call(int level, void (*callback_func)(void*), void* data) {
Christopher Ferris7fb22872013-09-27 12:43:15 -070063 if (level > 0) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070064 return test_recursive_call(level - 1, callback_func, data) + level;
Christopher Ferris7fb22872013-09-27 12:43:15 -070065 } else if (callback_func != NULL) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070066 callback_func(data);
Christopher Ferris7fb22872013-09-27 12:43:15 -070067 } else {
68 while (1) {
69 }
70 }
71 return 0;
72}
Yabin Cui5d991bc2016-11-15 17:47:09 -080073
74typedef struct {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080075 std::vector<uint8_t>* ucontext;
Yabin Cui5d991bc2016-11-15 17:47:09 -080076 volatile int* exit_flag;
77} GetContextArg;
78
79static void GetContextAndExit(void* data) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080080 GetContextArg* arg = reinterpret_cast<GetContextArg*>(data);
81
82 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
83 unwindstack::RegsGetLocal(regs.get());
84
85 ucontext_t ucontext;
86 memset(&ucontext, 0, sizeof(ucontext));
87#if defined(__arm__)
88 memcpy(&ucontext.uc_mcontext, regs->RawData(), sizeof(uint32_t) * 16);
89#elif defined(__aarch64__)
90 memcpy(&ucontext.uc_mcontext, regs->RawData(), sizeof(uint64_t) * 33);
91#elif defined(__i386__)
92 uint32_t* reg_data = reinterpret_cast<uint32_t*>(regs->RawData());
93 ucontext.uc_mcontext.gregs[0] = reg_data[15];
94 ucontext.uc_mcontext.gregs[1] = reg_data[14];
95 ucontext.uc_mcontext.gregs[2] = reg_data[13];
96 ucontext.uc_mcontext.gregs[3] = reg_data[12];
97 ucontext.uc_mcontext.gregs[4] = reg_data[7];
98 ucontext.uc_mcontext.gregs[5] = reg_data[6];
99 ucontext.uc_mcontext.gregs[6] = reg_data[5];
100 ucontext.uc_mcontext.gregs[7] = reg_data[4];
101 ucontext.uc_mcontext.gregs[8] = reg_data[3];
102 ucontext.uc_mcontext.gregs[9] = reg_data[2];
103 ucontext.uc_mcontext.gregs[10] = reg_data[1];
104 ucontext.uc_mcontext.gregs[11] = reg_data[0];
105 ucontext.uc_mcontext.gregs[14] = reg_data[8];
106 ucontext.uc_mcontext.gregs[15] = reg_data[10];
107#elif defined(__x86_64__)
108 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
109 ucontext.uc_mcontext.gregs[0] = reg_data[8];
110 ucontext.uc_mcontext.gregs[1] = reg_data[9];
111 ucontext.uc_mcontext.gregs[2] = reg_data[10];
112 ucontext.uc_mcontext.gregs[3] = reg_data[11];
113 ucontext.uc_mcontext.gregs[4] = reg_data[12];
114 ucontext.uc_mcontext.gregs[5] = reg_data[13];
115 ucontext.uc_mcontext.gregs[6] = reg_data[14];
116 ucontext.uc_mcontext.gregs[7] = reg_data[15];
117 ucontext.uc_mcontext.gregs[8] = reg_data[5];
118 ucontext.uc_mcontext.gregs[9] = reg_data[4];
119 ucontext.uc_mcontext.gregs[10] = reg_data[6];
120 ucontext.uc_mcontext.gregs[11] = reg_data[3];
121 ucontext.uc_mcontext.gregs[12] = reg_data[1];
122 ucontext.uc_mcontext.gregs[13] = reg_data[0];
123 ucontext.uc_mcontext.gregs[14] = reg_data[2];
124 ucontext.uc_mcontext.gregs[15] = reg_data[7];
125 ucontext.uc_mcontext.gregs[16] = reg_data[16];
126#endif
127
128 arg->ucontext->resize(sizeof(ucontext));
129 memcpy(arg->ucontext->data(), &ucontext, sizeof(ucontext));
130
Yabin Cui5d991bc2016-11-15 17:47:09 -0800131 // Don't touch the stack anymore.
132 while (*arg->exit_flag == 0) {
133 }
134}
135
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800136void test_get_context_and_wait(void* ucontext, volatile int* exit_flag) {
Yabin Cui5d991bc2016-11-15 17:47:09 -0800137 GetContextArg arg;
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800138 arg.ucontext = reinterpret_cast<std::vector<uint8_t>*>(ucontext);
Yabin Cui5d991bc2016-11-15 17:47:09 -0800139 arg.exit_flag = exit_flag;
140 test_level_one(1, 2, 3, 4, GetContextAndExit, &arg);
141}