backtrace_testlib.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include <signal.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <memory>
  20. #include <vector>
  21. #include <unwindstack/Regs.h>
  22. #include <unwindstack/RegsGetLocal.h>
  23. #include "backtrace_testlib.h"
  24. void test_loop_forever() {
  25. while (1)
  26. ;
  27. }
  28. void test_signal_handler(int) { test_loop_forever(); }
  29. void test_signal_action(int, siginfo_t*, void*) { test_loop_forever(); }
  30. int test_level_four(int one, int two, int three, int four, void (*callback_func)(void*),
  31. void* data) {
  32. if (callback_func != NULL) {
  33. callback_func(data);
  34. } else {
  35. while (1)
  36. ;
  37. }
  38. return one + two + three + four;
  39. }
  40. int test_level_three(int one, int two, int three, int four, void (*callback_func)(void*),
  41. void* data) {
  42. return test_level_four(one + 3, two + 6, three + 9, four + 12, callback_func, data) + 3;
  43. }
  44. int test_level_two(int one, int two, int three, int four, void (*callback_func)(void*), void* data) {
  45. return test_level_three(one + 2, two + 4, three + 6, four + 8, callback_func, data) + 2;
  46. }
  47. int test_level_one(int one, int two, int three, int four, void (*callback_func)(void*), void* data) {
  48. return test_level_two(one + 1, two + 2, three + 3, four + 4, callback_func, data) + 1;
  49. }
  50. int test_recursive_call(int level, void (*callback_func)(void*), void* data) {
  51. if (level > 0) {
  52. return test_recursive_call(level - 1, callback_func, data) + level;
  53. } else if (callback_func != NULL) {
  54. callback_func(data);
  55. } else {
  56. while (1) {
  57. }
  58. }
  59. return 0;
  60. }
  61. typedef struct {
  62. std::vector<uint8_t>* ucontext;
  63. volatile int* exit_flag;
  64. } GetContextArg;
  65. static void GetContextAndExit(void* data) {
  66. GetContextArg* arg = reinterpret_cast<GetContextArg*>(data);
  67. std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
  68. unwindstack::RegsGetLocal(regs.get());
  69. ucontext_t ucontext;
  70. memset(&ucontext, 0, sizeof(ucontext));
  71. #if defined(__arm__)
  72. memcpy(&ucontext.uc_mcontext, regs->RawData(), sizeof(uint32_t) * 16);
  73. #elif defined(__aarch64__)
  74. memcpy(&ucontext.uc_mcontext, regs->RawData(), sizeof(uint64_t) * 33);
  75. #elif defined(__i386__)
  76. uint32_t* reg_data = reinterpret_cast<uint32_t*>(regs->RawData());
  77. ucontext.uc_mcontext.gregs[0] = reg_data[15];
  78. ucontext.uc_mcontext.gregs[1] = reg_data[14];
  79. ucontext.uc_mcontext.gregs[2] = reg_data[13];
  80. ucontext.uc_mcontext.gregs[3] = reg_data[12];
  81. ucontext.uc_mcontext.gregs[4] = reg_data[7];
  82. ucontext.uc_mcontext.gregs[5] = reg_data[6];
  83. ucontext.uc_mcontext.gregs[6] = reg_data[5];
  84. ucontext.uc_mcontext.gregs[7] = reg_data[4];
  85. ucontext.uc_mcontext.gregs[8] = reg_data[3];
  86. ucontext.uc_mcontext.gregs[9] = reg_data[2];
  87. ucontext.uc_mcontext.gregs[10] = reg_data[1];
  88. ucontext.uc_mcontext.gregs[11] = reg_data[0];
  89. ucontext.uc_mcontext.gregs[14] = reg_data[8];
  90. ucontext.uc_mcontext.gregs[15] = reg_data[10];
  91. #elif defined(__x86_64__)
  92. uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
  93. ucontext.uc_mcontext.gregs[0] = reg_data[8];
  94. ucontext.uc_mcontext.gregs[1] = reg_data[9];
  95. ucontext.uc_mcontext.gregs[2] = reg_data[10];
  96. ucontext.uc_mcontext.gregs[3] = reg_data[11];
  97. ucontext.uc_mcontext.gregs[4] = reg_data[12];
  98. ucontext.uc_mcontext.gregs[5] = reg_data[13];
  99. ucontext.uc_mcontext.gregs[6] = reg_data[14];
  100. ucontext.uc_mcontext.gregs[7] = reg_data[15];
  101. ucontext.uc_mcontext.gregs[8] = reg_data[5];
  102. ucontext.uc_mcontext.gregs[9] = reg_data[4];
  103. ucontext.uc_mcontext.gregs[10] = reg_data[6];
  104. ucontext.uc_mcontext.gregs[11] = reg_data[3];
  105. ucontext.uc_mcontext.gregs[12] = reg_data[1];
  106. ucontext.uc_mcontext.gregs[13] = reg_data[0];
  107. ucontext.uc_mcontext.gregs[14] = reg_data[2];
  108. ucontext.uc_mcontext.gregs[15] = reg_data[7];
  109. ucontext.uc_mcontext.gregs[16] = reg_data[16];
  110. #endif
  111. arg->ucontext->resize(sizeof(ucontext));
  112. memcpy(arg->ucontext->data(), &ucontext, sizeof(ucontext));
  113. // Don't touch the stack anymore.
  114. while (*arg->exit_flag == 0) {
  115. }
  116. }
  117. void test_get_context_and_wait(void* ucontext, volatile int* exit_flag) {
  118. GetContextArg arg;
  119. arg.ucontext = reinterpret_cast<std::vector<uint8_t>*>(ucontext);
  120. arg.exit_flag = exit_flag;
  121. test_level_one(1, 2, 3, 4, GetContextAndExit, &arg);
  122. }