Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #include <inttypes.h> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 18 | #include <libunwind.h> |
| 19 | #include <pthread.h> |
| 20 | #include <stdint.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <functional> |
| 24 | #include <memory> |
| 25 | #include <string> |
| 26 | #include <utility> |
| 27 | #include <vector> |
| 28 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 29 | #include <android-base/file.h> |
Yabin Cui | 4a26eaf | 2017-12-05 17:40:48 -0800 | [diff] [blame] | 30 | #include <android-base/logging.h> |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 31 | #include <android-base/macros.h> |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
| 33 | #include <android-base/strings.h> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 34 | #include <backtrace/Backtrace.h> |
| 35 | #include <backtrace/BacktraceMap.h> |
| 36 | #include <cutils/threads.h> |
| 37 | |
| 38 | #include <gtest/gtest.h> |
| 39 | |
| 40 | extern "C" { |
| 41 | // Prototypes for functions in the test library. |
| 42 | int test_level_one(int, int, int, int, void (*)(void*), void*); |
| 43 | int test_level_two(int, int, int, int, void (*)(void*), void*); |
| 44 | int test_level_three(int, int, int, int, void (*)(void*), void*); |
| 45 | int test_level_four(int, int, int, int, void (*)(void*), void*); |
| 46 | int test_recursive_call(int, void (*)(void*), void*); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 47 | void test_get_context_and_wait(unw_context_t* unw_context, volatile int* exit_flag); |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static ucontext_t GetUContextFromUnwContext(const unw_context_t& unw_context) { |
| 51 | ucontext_t ucontext; |
| 52 | memset(&ucontext, 0, sizeof(ucontext)); |
| 53 | #if defined(__arm__) |
| 54 | ucontext.uc_mcontext.arm_r0 = unw_context.regs[0]; |
| 55 | ucontext.uc_mcontext.arm_r1 = unw_context.regs[1]; |
| 56 | ucontext.uc_mcontext.arm_r2 = unw_context.regs[2]; |
| 57 | ucontext.uc_mcontext.arm_r3 = unw_context.regs[3]; |
| 58 | ucontext.uc_mcontext.arm_r4 = unw_context.regs[4]; |
| 59 | ucontext.uc_mcontext.arm_r5 = unw_context.regs[5]; |
| 60 | ucontext.uc_mcontext.arm_r6 = unw_context.regs[6]; |
| 61 | ucontext.uc_mcontext.arm_r7 = unw_context.regs[7]; |
| 62 | ucontext.uc_mcontext.arm_r8 = unw_context.regs[8]; |
| 63 | ucontext.uc_mcontext.arm_r9 = unw_context.regs[9]; |
| 64 | ucontext.uc_mcontext.arm_r10 = unw_context.regs[10]; |
| 65 | ucontext.uc_mcontext.arm_fp = unw_context.regs[11]; |
| 66 | ucontext.uc_mcontext.arm_ip = unw_context.regs[12]; |
| 67 | ucontext.uc_mcontext.arm_sp = unw_context.regs[13]; |
| 68 | ucontext.uc_mcontext.arm_lr = unw_context.regs[14]; |
| 69 | ucontext.uc_mcontext.arm_pc = unw_context.regs[15]; |
| 70 | #else |
| 71 | ucontext.uc_mcontext = unw_context.uc_mcontext; |
| 72 | #endif |
| 73 | return ucontext; |
| 74 | } |
| 75 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 76 | struct FunctionSymbol { |
| 77 | std::string name; |
| 78 | uintptr_t start; |
| 79 | uintptr_t end; |
| 80 | }; |
| 81 | |
| 82 | static std::vector<FunctionSymbol> GetFunctionSymbols() { |
| 83 | std::vector<FunctionSymbol> symbols = { |
| 84 | {"unknown_start", 0, 0}, |
| 85 | {"test_level_one", reinterpret_cast<uintptr_t>(&test_level_one), 0}, |
| 86 | {"test_level_two", reinterpret_cast<uintptr_t>(&test_level_two), 0}, |
| 87 | {"test_level_three", reinterpret_cast<uintptr_t>(&test_level_three), 0}, |
| 88 | {"test_level_four", reinterpret_cast<uintptr_t>(&test_level_four), 0}, |
| 89 | {"test_recursive_call", reinterpret_cast<uintptr_t>(&test_recursive_call), 0}, |
| 90 | {"test_get_context_and_wait", reinterpret_cast<uintptr_t>(&test_get_context_and_wait), 0}, |
| 91 | {"unknown_end", static_cast<uintptr_t>(-1), static_cast<uintptr_t>(-1)}, |
| 92 | }; |
| 93 | std::sort( |
| 94 | symbols.begin(), symbols.end(), |
| 95 | [](const FunctionSymbol& s1, const FunctionSymbol& s2) { return s1.start < s2.start; }); |
| 96 | for (size_t i = 0; i + 1 < symbols.size(); ++i) { |
| 97 | symbols[i].end = symbols[i + 1].start; |
| 98 | } |
| 99 | return symbols; |
| 100 | } |
| 101 | |
| 102 | static std::string RawDataToHexString(const void* data, size_t size) { |
| 103 | const uint8_t* p = static_cast<const uint8_t*>(data); |
| 104 | std::string s; |
| 105 | for (size_t i = 0; i < size; ++i) { |
| 106 | s += android::base::StringPrintf("%02x", p[i]); |
| 107 | } |
| 108 | return s; |
| 109 | } |
| 110 | |
| 111 | static void HexStringToRawData(const char* s, void* data, size_t size) { |
| 112 | uint8_t* p = static_cast<uint8_t*>(data); |
| 113 | for (size_t i = 0; i < size; ++i) { |
| 114 | int value; |
| 115 | sscanf(s, "%02x", &value); |
| 116 | *p++ = static_cast<uint8_t>(value); |
| 117 | s += 2; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | struct OfflineThreadArg { |
| 122 | unw_context_t unw_context; |
| 123 | pid_t tid; |
| 124 | volatile int exit_flag; |
| 125 | }; |
| 126 | |
| 127 | static void* OfflineThreadFunc(void* arg) { |
| 128 | OfflineThreadArg* fn_arg = reinterpret_cast<OfflineThreadArg*>(arg); |
| 129 | fn_arg->tid = gettid(); |
| 130 | test_get_context_and_wait(&fn_arg->unw_context, &fn_arg->exit_flag); |
| 131 | return nullptr; |
| 132 | } |
| 133 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 134 | std::string GetTestPath(std::string path) { |
| 135 | return android::base::GetExecutableDirectory() + "/testdata/" + ABI_STRING + '/' + path; |
| 136 | } |
| 137 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 138 | // This test is disable because it is for generating test data. |
| 139 | TEST(libbacktrace, DISABLED_generate_offline_testdata) { |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 140 | // Create a thread to generate the needed stack and registers information. |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 141 | const size_t stack_size = 16 * 1024; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 142 | void* stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 143 | ASSERT_NE(MAP_FAILED, stack); |
| 144 | uintptr_t stack_addr = reinterpret_cast<uintptr_t>(stack); |
| 145 | pthread_attr_t attr; |
| 146 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 147 | ASSERT_EQ(0, pthread_attr_setstack(&attr, reinterpret_cast<void*>(stack), stack_size)); |
| 148 | pthread_t thread; |
| 149 | OfflineThreadArg arg; |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 150 | arg.exit_flag = 0; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 151 | ASSERT_EQ(0, pthread_create(&thread, &attr, OfflineThreadFunc, &arg)); |
| 152 | // Wait for the offline thread to generate the stack and unw_context information. |
| 153 | sleep(1); |
| 154 | // Copy the stack information. |
| 155 | std::vector<uint8_t> stack_data(reinterpret_cast<uint8_t*>(stack), |
| 156 | reinterpret_cast<uint8_t*>(stack) + stack_size); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 157 | arg.exit_flag = 1; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 158 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 159 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 160 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 161 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid())); |
| 162 | ASSERT_TRUE(map != nullptr); |
| 163 | |
| 164 | backtrace_stackinfo_t stack_info; |
| 165 | stack_info.start = stack_addr; |
| 166 | stack_info.end = stack_addr + stack_size; |
| 167 | stack_info.data = stack_data.data(); |
| 168 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 169 | // Generate offline testdata. |
| 170 | std::string testdata; |
| 171 | // 1. Dump pid, tid |
| 172 | testdata += android::base::StringPrintf("pid: %d tid: %d\n", getpid(), arg.tid); |
| 173 | // 2. Dump maps |
| 174 | for (auto it = map->begin(); it != map->end(); ++it) { |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 175 | const backtrace_map_t* entry = *it; |
| 176 | testdata += |
| 177 | android::base::StringPrintf("map: start: %" PRIxPTR " end: %" PRIxPTR " offset: %" PRIxPTR |
| 178 | " load_bias: %" PRIxPTR " flags: %d name: %s\n", |
| 179 | entry->start, entry->end, entry->offset, entry->load_bias, |
| 180 | entry->flags, entry->name.c_str()); |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 181 | } |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 182 | // 3. Dump registers |
| 183 | testdata += android::base::StringPrintf("registers: %zu ", sizeof(arg.unw_context)); |
| 184 | testdata += RawDataToHexString(&arg.unw_context, sizeof(arg.unw_context)); |
| 185 | testdata.push_back('\n'); |
| 186 | |
| 187 | // 4. Dump stack |
| 188 | testdata += android::base::StringPrintf( |
| 189 | "stack: start: %" PRIx64 " end: %" PRIx64 " size: %zu ", |
| 190 | stack_info.start, stack_info.end, stack_data.size()); |
| 191 | testdata += RawDataToHexString(stack_data.data(), stack_data.size()); |
| 192 | testdata.push_back('\n'); |
| 193 | |
| 194 | // 5. Dump function symbols |
| 195 | std::vector<FunctionSymbol> function_symbols = GetFunctionSymbols(); |
| 196 | for (const auto& symbol : function_symbols) { |
| 197 | testdata += android::base::StringPrintf( |
| 198 | "function: start: %" PRIxPTR " end: %" PRIxPTR" name: %s\n", |
| 199 | symbol.start, symbol.end, symbol.name.c_str()); |
| 200 | } |
| 201 | |
| 202 | ASSERT_TRUE(android::base::WriteStringToFile(testdata, "offline_testdata")); |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Return the name of the function which matches the address. Although we don't know the |
| 206 | // exact end of each function, it is accurate enough for the tests. |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 207 | static std::string FunctionNameForAddress(uintptr_t addr, |
| 208 | const std::vector<FunctionSymbol>& symbols) { |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 209 | for (auto& symbol : symbols) { |
| 210 | if (addr >= symbol.start && addr < symbol.end) { |
| 211 | return symbol.name; |
| 212 | } |
| 213 | } |
| 214 | return ""; |
| 215 | } |
| 216 | |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 217 | struct OfflineTestData { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 218 | int pid; |
| 219 | int tid; |
| 220 | std::vector<backtrace_map_t> maps; |
| 221 | unw_context_t unw_context; |
| 222 | backtrace_stackinfo_t stack_info; |
| 223 | std::vector<uint8_t> stack; |
| 224 | std::vector<FunctionSymbol> symbols; |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | bool ReadOfflineTestData(const std::string offline_testdata_path, OfflineTestData* testdata) { |
| 228 | std::string s; |
| 229 | if (!android::base::ReadFileToString(offline_testdata_path, &s)) { |
| 230 | return false; |
| 231 | } |
| 232 | // Parse offline_testdata. |
| 233 | std::vector<std::string> lines = android::base::Split(s, "\n"); |
| 234 | memset(&testdata->unw_context, 0, sizeof(testdata->unw_context)); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 235 | for (const auto& line : lines) { |
| 236 | if (android::base::StartsWith(line, "pid:")) { |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 237 | sscanf(line.c_str(), "pid: %d tid: %d", &testdata->pid, &testdata->tid); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 238 | } else if (android::base::StartsWith(line, "map:")) { |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 239 | testdata->maps.resize(testdata->maps.size() + 1); |
| 240 | backtrace_map_t& map = testdata->maps.back(); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 241 | int pos; |
| 242 | sscanf(line.c_str(), |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 243 | "map: start: %" SCNxPTR " end: %" SCNxPTR " offset: %" SCNxPTR " load_bias: %" SCNxPTR |
| 244 | " flags: %d name: %n", |
| 245 | &map.start, &map.end, &map.offset, &map.load_bias, &map.flags, &pos); |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 246 | map.name = android::base::Trim(line.substr(pos)); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 247 | } else if (android::base::StartsWith(line, "registers:")) { |
| 248 | size_t size; |
| 249 | int pos; |
| 250 | sscanf(line.c_str(), "registers: %zu %n", &size, &pos); |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 251 | if (sizeof(testdata->unw_context) != size) { |
| 252 | return false; |
| 253 | } |
| 254 | HexStringToRawData(&line[pos], &testdata->unw_context, size); |
Yabin Cui | 4a26eaf | 2017-12-05 17:40:48 -0800 | [diff] [blame] | 255 | } else if (android::base::StartsWith(line, "regs:")) { |
Yabin Cui | 9879137 | 2017-12-18 14:37:54 -0800 | [diff] [blame^] | 256 | std::vector<std::string> strs = android::base::Split(line.substr(6), " "); |
| 257 | if (strs.size() % 2 != 0) { |
| 258 | return false; |
| 259 | } |
| 260 | std::vector<std::pair<std::string, uint64_t>> items; |
| 261 | for (size_t i = 0; i + 1 < strs.size(); i += 2) { |
| 262 | if (!android::base::EndsWith(strs[i], ":")) { |
| 263 | return false; |
| 264 | } |
| 265 | uint64_t value = std::stoul(strs[i + 1], nullptr, 16); |
| 266 | items.push_back(std::make_pair(strs[i].substr(0, strs[i].size() - 1), value)); |
| 267 | } |
| 268 | #if defined(__arm__) |
| 269 | for (auto& item : items) { |
| 270 | if (item.first == "sp") { |
| 271 | testdata->unw_context.regs[13] = item.second; |
| 272 | } else if (item.first == "pc") { |
| 273 | testdata->unw_context.regs[15] = item.second; |
| 274 | } else { |
| 275 | return false; |
| 276 | } |
| 277 | } |
| 278 | #elif defined(__aarch64__) |
| 279 | for (auto& item : items) { |
| 280 | if (item.first == "pc") { |
| 281 | testdata->unw_context.uc_mcontext.pc = item.second; |
| 282 | } else if (item.first == "sp") { |
| 283 | testdata->unw_context.uc_mcontext.sp = item.second; |
| 284 | } else if (item.first == "x29") { |
| 285 | testdata->unw_context.uc_mcontext.regs[UNW_AARCH64_X29] = item.second; |
| 286 | } else { |
| 287 | return false; |
| 288 | } |
| 289 | } |
Yabin Cui | 4a26eaf | 2017-12-05 17:40:48 -0800 | [diff] [blame] | 290 | #endif |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 291 | } else if (android::base::StartsWith(line, "stack:")) { |
| 292 | size_t size; |
| 293 | int pos; |
| 294 | sscanf(line.c_str(), |
| 295 | "stack: start: %" SCNx64 " end: %" SCNx64 " size: %zu %n", |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 296 | &testdata->stack_info.start, &testdata->stack_info.end, &size, &pos); |
Yabin Cui | 4a26eaf | 2017-12-05 17:40:48 -0800 | [diff] [blame] | 297 | CHECK_EQ(testdata->stack_info.end - testdata->stack_info.start, size); |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 298 | testdata->stack.resize(size); |
| 299 | HexStringToRawData(&line[pos], &testdata->stack[0], size); |
| 300 | testdata->stack_info.data = testdata->stack.data(); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 301 | } else if (android::base::StartsWith(line, "function:")) { |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 302 | testdata->symbols.resize(testdata->symbols.size() + 1); |
| 303 | FunctionSymbol& symbol = testdata->symbols.back(); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 304 | int pos; |
| 305 | sscanf(line.c_str(), |
| 306 | "function: start: %" SCNxPTR " end: %" SCNxPTR " name: %n", |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 307 | &symbol.start, &symbol.end, &pos); |
| 308 | symbol.name = line.substr(pos); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 314 | static void BacktraceOfflineTest(const char* arch, const std::string& testlib_name) { |
| 315 | // TODO: For now, we can only run this on the same arch as the library arch. |
| 316 | if (std::string(ABI_STRING) != arch) { |
| 317 | GTEST_LOG_(INFO) << "Ignoring arch " << arch << " for lib " << testlib_name; |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 321 | const std::string testlib_path(GetTestPath(testlib_name)); |
| 322 | const std::string offline_testdata_path(GetTestPath("offline_testdata")); |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 323 | OfflineTestData testdata; |
| 324 | ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata)); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 325 | |
| 326 | // Fix path of libbacktrace_testlib.so. |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 327 | for (auto& map : testdata.maps) { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 328 | if (map.name.find("libbacktrace_test.so") != std::string::npos) { |
| 329 | map.name = testlib_path; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // Do offline backtrace. |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 334 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(testdata.pid, testdata.maps)); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 335 | ASSERT_TRUE(map != nullptr); |
| 336 | |
| 337 | std::unique_ptr<Backtrace> backtrace( |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 338 | Backtrace::CreateOffline(testdata.pid, testdata.tid, map.get(), testdata.stack_info)); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 339 | ASSERT_TRUE(backtrace != nullptr); |
| 340 | |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 341 | ucontext_t ucontext = GetUContextFromUnwContext(testdata.unw_context); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 342 | ASSERT_TRUE(backtrace->Unwind(0, &ucontext)); |
| 343 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 344 | // Collect pc values of the call stack frames. |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 345 | std::vector<uintptr_t> pc_values; |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 346 | for (size_t i = 0; i < backtrace->NumFrames(); ++i) { |
| 347 | pc_values.push_back(backtrace->GetFrame(i)->pc); |
| 348 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 349 | |
| 350 | size_t test_one_index = 0; |
| 351 | for (size_t i = 0; i < pc_values.size(); ++i) { |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 352 | if (FunctionNameForAddress(pc_values[i], testdata.symbols) == "test_level_one") { |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 353 | test_one_index = i; |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | ASSERT_GE(test_one_index, 3u); |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 359 | ASSERT_EQ("test_level_one", FunctionNameForAddress(pc_values[test_one_index], testdata.symbols)); |
| 360 | ASSERT_EQ("test_level_two", FunctionNameForAddress(pc_values[test_one_index - 1], |
| 361 | testdata.symbols)); |
| 362 | ASSERT_EQ("test_level_three", FunctionNameForAddress(pc_values[test_one_index - 2], |
| 363 | testdata.symbols)); |
| 364 | ASSERT_EQ("test_level_four", FunctionNameForAddress(pc_values[test_one_index - 3], |
| 365 | testdata.symbols)); |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 368 | // For now, these tests can only run on the given architectures. |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 369 | TEST(libbacktrace, offline_eh_frame) { |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 370 | BacktraceOfflineTest("arm64", "libbacktrace_test_eh_frame.so"); |
| 371 | BacktraceOfflineTest("x86_64", "libbacktrace_test_eh_frame.so"); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | TEST(libbacktrace, offline_debug_frame) { |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 375 | BacktraceOfflineTest("arm", "libbacktrace_test_debug_frame.so"); |
| 376 | BacktraceOfflineTest("x86", "libbacktrace_test_debug_frame.so"); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | TEST(libbacktrace, offline_gnu_debugdata) { |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 380 | BacktraceOfflineTest("arm", "libbacktrace_test_gnu_debugdata.so"); |
| 381 | BacktraceOfflineTest("x86", "libbacktrace_test_gnu_debugdata.so"); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | TEST(libbacktrace, offline_arm_exidx) { |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 385 | BacktraceOfflineTest("arm", "libbacktrace_test_arm_exidx.so"); |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 386 | } |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 387 | |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 388 | static void LibUnwindingTest(const std::string& arch, const std::string& testdata_name, |
| 389 | const std::string& testlib_name) { |
| 390 | if (std::string(ABI_STRING) != arch) { |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 391 | GTEST_LOG_(INFO) << "Skipping test since offline for arm on " << ABI_STRING |
| 392 | << " isn't supported."; |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 393 | return; |
| 394 | } |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 395 | const std::string testlib_path(GetTestPath(testlib_name)); |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 396 | struct stat st; |
| 397 | ASSERT_EQ(0, stat(testlib_path.c_str(), &st)) << "can't find testlib " << testlib_path; |
| 398 | |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 399 | const std::string offline_testdata_path(GetTestPath(testdata_name)); |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 400 | OfflineTestData testdata; |
| 401 | ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata)); |
| 402 | |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 403 | // Fix path of the testlib. |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame] | 404 | for (auto& map : testdata.maps) { |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 405 | if (map.name.find(testlib_name) != std::string::npos) { |
Yabin Cui | 4a26eaf | 2017-12-05 17:40:48 -0800 | [diff] [blame] | 406 | map.name = testlib_path; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Do offline backtrace. |
| 411 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(testdata.pid, testdata.maps)); |
| 412 | ASSERT_TRUE(map != nullptr); |
| 413 | |
| 414 | std::unique_ptr<Backtrace> backtrace( |
| 415 | Backtrace::CreateOffline(testdata.pid, testdata.tid, map.get(), testdata.stack_info)); |
| 416 | ASSERT_TRUE(backtrace != nullptr); |
| 417 | |
| 418 | ucontext_t ucontext = GetUContextFromUnwContext(testdata.unw_context); |
| 419 | ASSERT_TRUE(backtrace->Unwind(0, &ucontext)); |
| 420 | |
| 421 | ASSERT_EQ(testdata.symbols.size(), backtrace->NumFrames()); |
| 422 | for (size_t i = 0; i < backtrace->NumFrames(); ++i) { |
| 423 | uintptr_t vaddr_in_file = |
| 424 | backtrace->GetFrame(i)->pc - testdata.maps[0].start + testdata.maps[0].load_bias; |
| 425 | std::string name = FunctionNameForAddress(vaddr_in_file, testdata.symbols); |
| 426 | ASSERT_EQ(name, testdata.symbols[i].name); |
| 427 | } |
Yabin Cui | 9879137 | 2017-12-18 14:37:54 -0800 | [diff] [blame^] | 428 | ASSERT_TRUE(backtrace->GetError().error_code == BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED || |
| 429 | backtrace->GetError().error_code == BACKTRACE_UNWIND_ERROR_MAP_MISSING); |
Yabin Cui | 4a26eaf | 2017-12-05 17:40:48 -0800 | [diff] [blame] | 430 | } |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 431 | |
| 432 | // This test tests the situation that ranges of functions covered by .eh_frame and .ARM.exidx |
| 433 | // overlap with each other, which appears in /system/lib/libart.so. |
| 434 | TEST(libbacktrace, offline_unwind_mix_eh_frame_and_arm_exidx) { |
| 435 | LibUnwindingTest("arm", "offline_testdata_for_libart", "libart.so"); |
| 436 | } |
| 437 | |
| 438 | TEST(libbacktrace, offline_debug_frame_with_load_bias) { |
| 439 | LibUnwindingTest("arm", "offline_testdata_for_libandroid_runtime", "libandroid_runtime.so"); |
| 440 | } |
| 441 | |
| 442 | TEST(libbacktrace, offline_try_armexidx_after_debug_frame) { |
| 443 | LibUnwindingTest("arm", "offline_testdata_for_libGLESv2_adreno", "libGLESv2_adreno.so"); |
| 444 | } |
Yabin Cui | 9879137 | 2017-12-18 14:37:54 -0800 | [diff] [blame^] | 445 | |
| 446 | TEST(libbacktrace, offline_cie_with_P_augmentation) { |
| 447 | // Make sure we can unwind through functions with CIE entry containing P augmentation, which |
| 448 | // makes unwinding library reading personality handler from memory. One example is |
| 449 | // /system/lib64/libskia.so. |
| 450 | LibUnwindingTest("arm64", "offline_testdata_for_libskia", "libskia.so"); |
| 451 | } |
| 452 | |
| 453 | TEST(libbacktrace, offline_empty_eh_frame_hdr) { |
| 454 | // Make sure we can unwind through libraries with empty .eh_frame_hdr section. One example is |
| 455 | // /vendor/lib64/egl/eglSubDriverAndroid.so. |
| 456 | LibUnwindingTest("arm64", "offline_testdata_for_eglSubDriverAndroid", "eglSubDriverAndroid.so"); |
| 457 | } |
| 458 | |
| 459 | TEST(libbacktrace, offline_max_frames_limit) { |
| 460 | // The length of callchain can reach 256 when recording an application. |
| 461 | ASSERT_GE(MAX_BACKTRACE_FRAMES, 256); |
| 462 | } |