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