| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 17 | #include <signal.h> | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 18 | #include <stdio.h> | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 19 | #include <unistd.h> | 
|  | 20 |  | 
|  | 21 | #include <memory> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <unwindstack/Regs.h> | 
|  | 25 | #include <unwindstack/RegsGetLocal.h> | 
| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 26 |  | 
| Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 27 | #include "backtrace_testlib.h" | 
|  | 28 |  | 
|  | 29 | void test_loop_forever() { | 
|  | 30 | while (1) | 
|  | 31 | ; | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | void test_signal_handler(int) { test_loop_forever(); } | 
|  | 35 |  | 
|  | 36 | void test_signal_action(int, siginfo_t*, void*) { test_loop_forever(); } | 
|  | 37 |  | 
|  | 38 | int test_level_four(int one, int two, int three, int four, void (*callback_func)(void*), | 
|  | 39 | void* data) { | 
| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 40 | if (callback_func != NULL) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 41 | callback_func(data); | 
| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 42 | } else { | 
| Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 43 | while (1) | 
|  | 44 | ; | 
| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 45 | } | 
|  | 46 | return one + two + three + four; | 
|  | 47 | } | 
|  | 48 |  | 
| Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 49 | int 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 Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 54 | int 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 Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 58 | int 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 Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 62 | int test_recursive_call(int level, void (*callback_func)(void*), void* data) { | 
| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 63 | if (level > 0) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 64 | return test_recursive_call(level - 1, callback_func, data) + level; | 
| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 65 | } else if (callback_func != NULL) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 66 | callback_func(data); | 
| Christopher Ferris | 7fb2287 | 2013-09-27 12:43:15 -0700 | [diff] [blame] | 67 | } else { | 
|  | 68 | while (1) { | 
|  | 69 | } | 
|  | 70 | } | 
|  | 71 | return 0; | 
|  | 72 | } | 
| Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 73 |  | 
|  | 74 | typedef struct { | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 75 | std::vector<uint8_t>* ucontext; | 
| Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 76 | volatile int* exit_flag; | 
|  | 77 | } GetContextArg; | 
|  | 78 |  | 
|  | 79 | static void GetContextAndExit(void* data) { | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 80 | 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 Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 131 | // Don't touch the stack anymore. | 
|  | 132 | while (*arg->exit_flag == 0) { | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 136 | void test_get_context_and_wait(void* ucontext, volatile int* exit_flag) { | 
| Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 137 | GetContextArg arg; | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 138 | arg.ucontext = reinterpret_cast<std::vector<uint8_t>*>(ucontext); | 
| Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 139 | arg.exit_flag = exit_flag; | 
|  | 140 | test_level_one(1, 2, 3, 4, GetContextAndExit, &arg); | 
|  | 141 | } |