blob: e92bc61092f51fde3d80d2558cdf8ffdfdd2dc35 [file] [log] [blame]
Yabin Cui5d991bc2016-11-15 17:47:09 -08001/*
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 Cui9e402bb2015-09-22 04:46:57 +000018#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 Cui5d991bc2016-11-15 17:47:09 -080029#include <android-base/file.h>
Yabin Cui4a26eaf2017-12-05 17:40:48 -080030#include <android-base/logging.h>
Christopher Ferris458cc662017-08-28 16:31:18 -070031#include <android-base/macros.h>
Yabin Cui5d991bc2016-11-15 17:47:09 -080032#include <android-base/stringprintf.h>
33#include <android-base/strings.h>
Yabin Cui9e402bb2015-09-22 04:46:57 +000034#include <backtrace/Backtrace.h>
35#include <backtrace/BacktraceMap.h>
36#include <cutils/threads.h>
37
38#include <gtest/gtest.h>
39
40extern "C" {
41// Prototypes for functions in the test library.
42int test_level_one(int, int, int, int, void (*)(void*), void*);
43int test_level_two(int, int, int, int, void (*)(void*), void*);
44int test_level_three(int, int, int, int, void (*)(void*), void*);
45int test_level_four(int, int, int, int, void (*)(void*), void*);
46int test_recursive_call(int, void (*)(void*), void*);
Yabin Cui5d991bc2016-11-15 17:47:09 -080047void test_get_context_and_wait(unw_context_t* unw_context, volatile int* exit_flag);
Yabin Cui9e402bb2015-09-22 04:46:57 +000048}
49
50static 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 Cui5d991bc2016-11-15 17:47:09 -080076struct FunctionSymbol {
77 std::string name;
78 uintptr_t start;
79 uintptr_t end;
80};
81
82static 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
102static 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
111static 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
121struct OfflineThreadArg {
122 unw_context_t unw_context;
123 pid_t tid;
124 volatile int exit_flag;
125};
126
127static 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 Ferris458cc662017-08-28 16:31:18 -0700134std::string GetTestPath(std::string path) {
135 return android::base::GetExecutableDirectory() + "/testdata/" + ABI_STRING + '/' + path;
136}
137
Yabin Cui5d991bc2016-11-15 17:47:09 -0800138// This test is disable because it is for generating test data.
139TEST(libbacktrace, DISABLED_generate_offline_testdata) {
Yabin Cui9e402bb2015-09-22 04:46:57 +0000140 // Create a thread to generate the needed stack and registers information.
Yabin Cui5d991bc2016-11-15 17:47:09 -0800141 const size_t stack_size = 16 * 1024;
Yabin Cui9e402bb2015-09-22 04:46:57 +0000142 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 Cui5d991bc2016-11-15 17:47:09 -0800150 arg.exit_flag = 0;
Yabin Cui9e402bb2015-09-22 04:46:57 +0000151 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 Cui5d991bc2016-11-15 17:47:09 -0800157 arg.exit_flag = 1;
Yabin Cui9e402bb2015-09-22 04:46:57 +0000158 ASSERT_EQ(0, pthread_join(thread, nullptr));
159 ASSERT_EQ(0, munmap(stack, stack_size));
160
Yabin Cui9e402bb2015-09-22 04:46:57 +0000161 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 Cui5d991bc2016-11-15 17:47:09 -0800169 // 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 Ferrisb7de5f52017-12-01 21:37:37 -0800175 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 Cui9e402bb2015-09-22 04:46:57 +0000181 }
Yabin Cui5d991bc2016-11-15 17:47:09 -0800182 // 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 Cui9e402bb2015-09-22 04:46:57 +0000203}
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 Cui5d991bc2016-11-15 17:47:09 -0800207static std::string FunctionNameForAddress(uintptr_t addr,
208 const std::vector<FunctionSymbol>& symbols) {
Yabin Cui9e402bb2015-09-22 04:46:57 +0000209 for (auto& symbol : symbols) {
210 if (addr >= symbol.start && addr < symbol.end) {
211 return symbol.name;
212 }
213 }
214 return "";
215}
216
Yabin Cuic4a480e2017-02-02 15:25:08 -0800217struct OfflineTestData {
Yabin Cui5d991bc2016-11-15 17:47:09 -0800218 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 Cuic4a480e2017-02-02 15:25:08 -0800225};
226
227bool 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 Cui5d991bc2016-11-15 17:47:09 -0800235 for (const auto& line : lines) {
236 if (android::base::StartsWith(line, "pid:")) {
Yabin Cuic4a480e2017-02-02 15:25:08 -0800237 sscanf(line.c_str(), "pid: %d tid: %d", &testdata->pid, &testdata->tid);
Yabin Cui5d991bc2016-11-15 17:47:09 -0800238 } else if (android::base::StartsWith(line, "map:")) {
Yabin Cuic4a480e2017-02-02 15:25:08 -0800239 testdata->maps.resize(testdata->maps.size() + 1);
240 backtrace_map_t& map = testdata->maps.back();
Yabin Cui5d991bc2016-11-15 17:47:09 -0800241 int pos;
242 sscanf(line.c_str(),
Christopher Ferris96722b02017-07-19 14:20:46 -0700243 "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 Cuic4a480e2017-02-02 15:25:08 -0800246 map.name = android::base::Trim(line.substr(pos));
Yabin Cui5d991bc2016-11-15 17:47:09 -0800247 } 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 Cuic4a480e2017-02-02 15:25:08 -0800251 if (sizeof(testdata->unw_context) != size) {
252 return false;
253 }
254 HexStringToRawData(&line[pos], &testdata->unw_context, size);
Yabin Cui4a26eaf2017-12-05 17:40:48 -0800255 } else if (android::base::StartsWith(line, "regs:")) {
Yabin Cui98791372017-12-18 14:37:54 -0800256 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 Cui4a26eaf2017-12-05 17:40:48 -0800290#endif
Yabin Cui5d991bc2016-11-15 17:47:09 -0800291 } 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 Cuic4a480e2017-02-02 15:25:08 -0800296 &testdata->stack_info.start, &testdata->stack_info.end, &size, &pos);
Yabin Cui4a26eaf2017-12-05 17:40:48 -0800297 CHECK_EQ(testdata->stack_info.end - testdata->stack_info.start, size);
Yabin Cuic4a480e2017-02-02 15:25:08 -0800298 testdata->stack.resize(size);
299 HexStringToRawData(&line[pos], &testdata->stack[0], size);
300 testdata->stack_info.data = testdata->stack.data();
Yabin Cui5d991bc2016-11-15 17:47:09 -0800301 } else if (android::base::StartsWith(line, "function:")) {
Yabin Cuic4a480e2017-02-02 15:25:08 -0800302 testdata->symbols.resize(testdata->symbols.size() + 1);
303 FunctionSymbol& symbol = testdata->symbols.back();
Yabin Cui5d991bc2016-11-15 17:47:09 -0800304 int pos;
305 sscanf(line.c_str(),
306 "function: start: %" SCNxPTR " end: %" SCNxPTR " name: %n",
Yabin Cuic4a480e2017-02-02 15:25:08 -0800307 &symbol.start, &symbol.end, &pos);
308 symbol.name = line.substr(pos);
Yabin Cui5d991bc2016-11-15 17:47:09 -0800309 }
310 }
Yabin Cuic4a480e2017-02-02 15:25:08 -0800311 return true;
312}
313
Christopher Ferris458cc662017-08-28 16:31:18 -0700314static 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 Cuic4a480e2017-02-02 15:25:08 -0800318 return;
319 }
320
Christopher Ferris458cc662017-08-28 16:31:18 -0700321 const std::string testlib_path(GetTestPath(testlib_name));
322 const std::string offline_testdata_path(GetTestPath("offline_testdata"));
Yabin Cuic4a480e2017-02-02 15:25:08 -0800323 OfflineTestData testdata;
324 ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata));
Yabin Cui5d991bc2016-11-15 17:47:09 -0800325
326 // Fix path of libbacktrace_testlib.so.
Yabin Cuic4a480e2017-02-02 15:25:08 -0800327 for (auto& map : testdata.maps) {
Yabin Cui5d991bc2016-11-15 17:47:09 -0800328 if (map.name.find("libbacktrace_test.so") != std::string::npos) {
329 map.name = testlib_path;
330 }
331 }
332
333 // Do offline backtrace.
Yabin Cuic4a480e2017-02-02 15:25:08 -0800334 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(testdata.pid, testdata.maps));
Yabin Cui5d991bc2016-11-15 17:47:09 -0800335 ASSERT_TRUE(map != nullptr);
336
337 std::unique_ptr<Backtrace> backtrace(
Yabin Cuic4a480e2017-02-02 15:25:08 -0800338 Backtrace::CreateOffline(testdata.pid, testdata.tid, map.get(), testdata.stack_info));
Yabin Cui5d991bc2016-11-15 17:47:09 -0800339 ASSERT_TRUE(backtrace != nullptr);
340
Yabin Cuic4a480e2017-02-02 15:25:08 -0800341 ucontext_t ucontext = GetUContextFromUnwContext(testdata.unw_context);
Yabin Cui5d991bc2016-11-15 17:47:09 -0800342 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
343
Yabin Cui5d991bc2016-11-15 17:47:09 -0800344 // Collect pc values of the call stack frames.
Yabin Cui9e402bb2015-09-22 04:46:57 +0000345 std::vector<uintptr_t> pc_values;
Yabin Cui5d991bc2016-11-15 17:47:09 -0800346 for (size_t i = 0; i < backtrace->NumFrames(); ++i) {
347 pc_values.push_back(backtrace->GetFrame(i)->pc);
348 }
Yabin Cui9e402bb2015-09-22 04:46:57 +0000349
350 size_t test_one_index = 0;
351 for (size_t i = 0; i < pc_values.size(); ++i) {
Yabin Cuic4a480e2017-02-02 15:25:08 -0800352 if (FunctionNameForAddress(pc_values[i], testdata.symbols) == "test_level_one") {
Yabin Cui9e402bb2015-09-22 04:46:57 +0000353 test_one_index = i;
354 break;
355 }
356 }
357
358 ASSERT_GE(test_one_index, 3u);
Yabin Cuic4a480e2017-02-02 15:25:08 -0800359 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 Cui9e402bb2015-09-22 04:46:57 +0000366}
367
Christopher Ferris458cc662017-08-28 16:31:18 -0700368// For now, these tests can only run on the given architectures.
Yabin Cui5d991bc2016-11-15 17:47:09 -0800369TEST(libbacktrace, offline_eh_frame) {
Christopher Ferris458cc662017-08-28 16:31:18 -0700370 BacktraceOfflineTest("arm64", "libbacktrace_test_eh_frame.so");
371 BacktraceOfflineTest("x86_64", "libbacktrace_test_eh_frame.so");
Yabin Cui5d991bc2016-11-15 17:47:09 -0800372}
373
374TEST(libbacktrace, offline_debug_frame) {
Christopher Ferris458cc662017-08-28 16:31:18 -0700375 BacktraceOfflineTest("arm", "libbacktrace_test_debug_frame.so");
376 BacktraceOfflineTest("x86", "libbacktrace_test_debug_frame.so");
Yabin Cui5d991bc2016-11-15 17:47:09 -0800377}
378
379TEST(libbacktrace, offline_gnu_debugdata) {
Christopher Ferris458cc662017-08-28 16:31:18 -0700380 BacktraceOfflineTest("arm", "libbacktrace_test_gnu_debugdata.so");
381 BacktraceOfflineTest("x86", "libbacktrace_test_gnu_debugdata.so");
Yabin Cui5d991bc2016-11-15 17:47:09 -0800382}
383
384TEST(libbacktrace, offline_arm_exidx) {
Christopher Ferris458cc662017-08-28 16:31:18 -0700385 BacktraceOfflineTest("arm", "libbacktrace_test_arm_exidx.so");
Yabin Cui9e402bb2015-09-22 04:46:57 +0000386}
Yabin Cuic4a480e2017-02-02 15:25:08 -0800387
Yabin Cui0ca49b02017-12-10 17:55:12 -0800388static 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 Ferris458cc662017-08-28 16:31:18 -0700391 GTEST_LOG_(INFO) << "Skipping test since offline for arm on " << ABI_STRING
392 << " isn't supported.";
Yabin Cuic4a480e2017-02-02 15:25:08 -0800393 return;
394 }
Yabin Cui0ca49b02017-12-10 17:55:12 -0800395 const std::string testlib_path(GetTestPath(testlib_name));
Yabin Cuic4a480e2017-02-02 15:25:08 -0800396 struct stat st;
397 ASSERT_EQ(0, stat(testlib_path.c_str(), &st)) << "can't find testlib " << testlib_path;
398
Yabin Cui0ca49b02017-12-10 17:55:12 -0800399 const std::string offline_testdata_path(GetTestPath(testdata_name));
Yabin Cuic4a480e2017-02-02 15:25:08 -0800400 OfflineTestData testdata;
401 ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata));
402
Yabin Cui0ca49b02017-12-10 17:55:12 -0800403 // Fix path of the testlib.
Yabin Cuic4a480e2017-02-02 15:25:08 -0800404 for (auto& map : testdata.maps) {
Yabin Cui0ca49b02017-12-10 17:55:12 -0800405 if (map.name.find(testlib_name) != std::string::npos) {
Yabin Cui4a26eaf2017-12-05 17:40:48 -0800406 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 Cui98791372017-12-18 14:37:54 -0800428 ASSERT_TRUE(backtrace->GetError().error_code == BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED ||
429 backtrace->GetError().error_code == BACKTRACE_UNWIND_ERROR_MAP_MISSING);
Yabin Cui4a26eaf2017-12-05 17:40:48 -0800430}
Yabin Cui0ca49b02017-12-10 17:55:12 -0800431
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.
434TEST(libbacktrace, offline_unwind_mix_eh_frame_and_arm_exidx) {
435 LibUnwindingTest("arm", "offline_testdata_for_libart", "libart.so");
436}
437
438TEST(libbacktrace, offline_debug_frame_with_load_bias) {
439 LibUnwindingTest("arm", "offline_testdata_for_libandroid_runtime", "libandroid_runtime.so");
440}
441
442TEST(libbacktrace, offline_try_armexidx_after_debug_frame) {
443 LibUnwindingTest("arm", "offline_testdata_for_libGLESv2_adreno", "libGLESv2_adreno.so");
444}
Yabin Cui98791372017-12-18 14:37:54 -0800445
446TEST(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
453TEST(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
459TEST(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}