blob: 6f6b53508fc443a2e36521b668b5974041222a5f [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
Yabin Cui5d991bc2016-11-15 17:47:09 -080017#include <libunwind.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070018#include <stdio.h>
Christopher Ferris7fb22872013-09-27 12:43:15 -070019
20int test_level_four(int one, int two, int three, int four,
Christopher Ferris17e91d42013-10-21 13:30:52 -070021 void (*callback_func)(void*), void* data) {
Christopher Ferris7fb22872013-09-27 12:43:15 -070022 if (callback_func != NULL) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070023 callback_func(data);
Christopher Ferris7fb22872013-09-27 12:43:15 -070024 } else {
25 while (1) {
26 }
27 }
28 return one + two + three + four;
29}
30
31int test_level_three(int one, int two, int three, int four,
Christopher Ferris17e91d42013-10-21 13:30:52 -070032 void (*callback_func)(void*), void* data) {
33 return test_level_four(one+3, two+6, three+9, four+12, callback_func, data) + 3;
Christopher Ferris7fb22872013-09-27 12:43:15 -070034}
35
36int test_level_two(int one, int two, int three, int four,
Christopher Ferris17e91d42013-10-21 13:30:52 -070037 void (*callback_func)(void*), void* data) {
38 return test_level_three(one+2, two+4, three+6, four+8, callback_func, data) + 2;
Christopher Ferris7fb22872013-09-27 12:43:15 -070039}
40
41int test_level_one(int one, int two, int three, int four,
Christopher Ferris17e91d42013-10-21 13:30:52 -070042 void (*callback_func)(void*), void* data) {
43 return test_level_two(one+1, two+2, three+3, four+4, callback_func, data) + 1;
Christopher Ferris7fb22872013-09-27 12:43:15 -070044}
45
Christopher Ferris17e91d42013-10-21 13:30:52 -070046int test_recursive_call(int level, void (*callback_func)(void*), void* data) {
Christopher Ferris7fb22872013-09-27 12:43:15 -070047 if (level > 0) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070048 return test_recursive_call(level - 1, callback_func, data) + level;
Christopher Ferris7fb22872013-09-27 12:43:15 -070049 } else if (callback_func != NULL) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070050 callback_func(data);
Christopher Ferris7fb22872013-09-27 12:43:15 -070051 } else {
52 while (1) {
53 }
54 }
55 return 0;
56}
Yabin Cui5d991bc2016-11-15 17:47:09 -080057
58typedef struct {
59 unw_context_t* unw_context;
60 volatile int* exit_flag;
61} GetContextArg;
62
63static void GetContextAndExit(void* data) {
64 GetContextArg* arg = (GetContextArg*)data;
65 unw_getcontext(arg->unw_context);
66 // Don't touch the stack anymore.
67 while (*arg->exit_flag == 0) {
68 }
69}
70
71void test_get_context_and_wait(unw_context_t* unw_context, volatile int* exit_flag) {
72 GetContextArg arg;
73 arg.unw_context = unw_context;
74 arg.exit_flag = exit_flag;
75 test_level_one(1, 2, 3, 4, GetContextAndExit, &arg);
76}