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>
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
29 #include <android-base/file.h>
30 #include <android-base/stringprintf.h>
31 #include <android-base/strings.h>
32 #include <backtrace/Backtrace.h>
33 #include <backtrace/BacktraceMap.h>
34 #include <cutils/threads.h>
35
36 #include <gtest/gtest.h>
37
38 extern "C" {
39 // Prototypes for functions in the test library.
40 int test_level_one(int, int, int, int, void (*)(void*), void*);
41 int test_level_two(int, int, int, int, void (*)(void*), void*);
42 int test_level_three(int, int, int, int, void (*)(void*), void*);
43 int test_level_four(int, int, int, int, void (*)(void*), void*);
44 int test_recursive_call(int, void (*)(void*), void*);
45 void test_get_context_and_wait(unw_context_t* unw_context, volatile int* exit_flag);
46 }
47
GetUContextFromUnwContext(const unw_context_t & unw_context)48 static ucontext_t GetUContextFromUnwContext(const unw_context_t& unw_context) {
49 ucontext_t ucontext;
50 memset(&ucontext, 0, sizeof(ucontext));
51 #if defined(__arm__)
52 ucontext.uc_mcontext.arm_r0 = unw_context.regs[0];
53 ucontext.uc_mcontext.arm_r1 = unw_context.regs[1];
54 ucontext.uc_mcontext.arm_r2 = unw_context.regs[2];
55 ucontext.uc_mcontext.arm_r3 = unw_context.regs[3];
56 ucontext.uc_mcontext.arm_r4 = unw_context.regs[4];
57 ucontext.uc_mcontext.arm_r5 = unw_context.regs[5];
58 ucontext.uc_mcontext.arm_r6 = unw_context.regs[6];
59 ucontext.uc_mcontext.arm_r7 = unw_context.regs[7];
60 ucontext.uc_mcontext.arm_r8 = unw_context.regs[8];
61 ucontext.uc_mcontext.arm_r9 = unw_context.regs[9];
62 ucontext.uc_mcontext.arm_r10 = unw_context.regs[10];
63 ucontext.uc_mcontext.arm_fp = unw_context.regs[11];
64 ucontext.uc_mcontext.arm_ip = unw_context.regs[12];
65 ucontext.uc_mcontext.arm_sp = unw_context.regs[13];
66 ucontext.uc_mcontext.arm_lr = unw_context.regs[14];
67 ucontext.uc_mcontext.arm_pc = unw_context.regs[15];
68 #else
69 ucontext.uc_mcontext = unw_context.uc_mcontext;
70 #endif
71 return ucontext;
72 }
73
74 struct FunctionSymbol {
75 std::string name;
76 uintptr_t start;
77 uintptr_t end;
78 };
79
GetFunctionSymbols()80 static std::vector<FunctionSymbol> GetFunctionSymbols() {
81 std::vector<FunctionSymbol> symbols = {
82 {"unknown_start", 0, 0},
83 {"test_level_one", reinterpret_cast<uintptr_t>(&test_level_one), 0},
84 {"test_level_two", reinterpret_cast<uintptr_t>(&test_level_two), 0},
85 {"test_level_three", reinterpret_cast<uintptr_t>(&test_level_three), 0},
86 {"test_level_four", reinterpret_cast<uintptr_t>(&test_level_four), 0},
87 {"test_recursive_call", reinterpret_cast<uintptr_t>(&test_recursive_call), 0},
88 {"test_get_context_and_wait", reinterpret_cast<uintptr_t>(&test_get_context_and_wait), 0},
89 {"unknown_end", static_cast<uintptr_t>(-1), static_cast<uintptr_t>(-1)},
90 };
91 std::sort(
92 symbols.begin(), symbols.end(),
93 [](const FunctionSymbol& s1, const FunctionSymbol& s2) { return s1.start < s2.start; });
94 for (size_t i = 0; i + 1 < symbols.size(); ++i) {
95 symbols[i].end = symbols[i + 1].start;
96 }
97 return symbols;
98 }
99
RawDataToHexString(const void * data,size_t size)100 static std::string RawDataToHexString(const void* data, size_t size) {
101 const uint8_t* p = static_cast<const uint8_t*>(data);
102 std::string s;
103 for (size_t i = 0; i < size; ++i) {
104 s += android::base::StringPrintf("%02x", p[i]);
105 }
106 return s;
107 }
108
HexStringToRawData(const char * s,void * data,size_t size)109 static void HexStringToRawData(const char* s, void* data, size_t size) {
110 uint8_t* p = static_cast<uint8_t*>(data);
111 for (size_t i = 0; i < size; ++i) {
112 int value;
113 sscanf(s, "%02x", &value);
114 *p++ = static_cast<uint8_t>(value);
115 s += 2;
116 }
117 }
118
119 struct OfflineThreadArg {
120 unw_context_t unw_context;
121 pid_t tid;
122 volatile int exit_flag;
123 };
124
OfflineThreadFunc(void * arg)125 static void* OfflineThreadFunc(void* arg) {
126 OfflineThreadArg* fn_arg = reinterpret_cast<OfflineThreadArg*>(arg);
127 fn_arg->tid = gettid();
128 test_get_context_and_wait(&fn_arg->unw_context, &fn_arg->exit_flag);
129 return nullptr;
130 }
131
132 // This test is disable because it is for generating test data.
TEST(libbacktrace,DISABLED_generate_offline_testdata)133 TEST(libbacktrace, DISABLED_generate_offline_testdata) {
134 // Create a thread to generate the needed stack and registers information.
135 const size_t stack_size = 16 * 1024;
136 void* stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
137 ASSERT_NE(MAP_FAILED, stack);
138 uintptr_t stack_addr = reinterpret_cast<uintptr_t>(stack);
139 pthread_attr_t attr;
140 ASSERT_EQ(0, pthread_attr_init(&attr));
141 ASSERT_EQ(0, pthread_attr_setstack(&attr, reinterpret_cast<void*>(stack), stack_size));
142 pthread_t thread;
143 OfflineThreadArg arg;
144 arg.exit_flag = 0;
145 ASSERT_EQ(0, pthread_create(&thread, &attr, OfflineThreadFunc, &arg));
146 // Wait for the offline thread to generate the stack and unw_context information.
147 sleep(1);
148 // Copy the stack information.
149 std::vector<uint8_t> stack_data(reinterpret_cast<uint8_t*>(stack),
150 reinterpret_cast<uint8_t*>(stack) + stack_size);
151 arg.exit_flag = 1;
152 ASSERT_EQ(0, pthread_join(thread, nullptr));
153 ASSERT_EQ(0, munmap(stack, stack_size));
154
155 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid()));
156 ASSERT_TRUE(map != nullptr);
157
158 backtrace_stackinfo_t stack_info;
159 stack_info.start = stack_addr;
160 stack_info.end = stack_addr + stack_size;
161 stack_info.data = stack_data.data();
162
163 // Generate offline testdata.
164 std::string testdata;
165 // 1. Dump pid, tid
166 testdata += android::base::StringPrintf("pid: %d tid: %d\n", getpid(), arg.tid);
167 // 2. Dump maps
168 for (auto it = map->begin(); it != map->end(); ++it) {
169 testdata += android::base::StringPrintf(
170 "map: start: %" PRIxPTR " end: %" PRIxPTR " offset: %" PRIxPTR
171 " load_base: %" PRIxPTR " flags: %d name: %s\n",
172 it->start, it->end, it->offset, it->load_base, it->flags, it->name.c_str());
173 }
174 // 3. Dump registers
175 testdata += android::base::StringPrintf("registers: %zu ", sizeof(arg.unw_context));
176 testdata += RawDataToHexString(&arg.unw_context, sizeof(arg.unw_context));
177 testdata.push_back('\n');
178
179 // 4. Dump stack
180 testdata += android::base::StringPrintf(
181 "stack: start: %" PRIx64 " end: %" PRIx64 " size: %zu ",
182 stack_info.start, stack_info.end, stack_data.size());
183 testdata += RawDataToHexString(stack_data.data(), stack_data.size());
184 testdata.push_back('\n');
185
186 // 5. Dump function symbols
187 std::vector<FunctionSymbol> function_symbols = GetFunctionSymbols();
188 for (const auto& symbol : function_symbols) {
189 testdata += android::base::StringPrintf(
190 "function: start: %" PRIxPTR " end: %" PRIxPTR" name: %s\n",
191 symbol.start, symbol.end, symbol.name.c_str());
192 }
193
194 ASSERT_TRUE(android::base::WriteStringToFile(testdata, "offline_testdata"));
195 }
196
197 // Return the name of the function which matches the address. Although we don't know the
198 // exact end of each function, it is accurate enough for the tests.
FunctionNameForAddress(uintptr_t addr,const std::vector<FunctionSymbol> & symbols)199 static std::string FunctionNameForAddress(uintptr_t addr,
200 const std::vector<FunctionSymbol>& symbols) {
201 for (auto& symbol : symbols) {
202 if (addr >= symbol.start && addr < symbol.end) {
203 return symbol.name;
204 }
205 }
206 return "";
207 }
208
GetArch()209 static std::string GetArch() {
210 #if defined(__arm__)
211 return "arm";
212 #elif defined(__aarch64__)
213 return "aarch64";
214 #elif defined(__i386__)
215 return "x86";
216 #elif defined(__x86_64__)
217 return "x86_64";
218 #else
219 return "";
220 #endif
221 }
222
223 struct OfflineTestData {
224 int pid;
225 int tid;
226 std::vector<backtrace_map_t> maps;
227 unw_context_t unw_context;
228 backtrace_stackinfo_t stack_info;
229 std::vector<uint8_t> stack;
230 std::vector<FunctionSymbol> symbols;
231 };
232
ReadOfflineTestData(const std::string offline_testdata_path,OfflineTestData * testdata)233 bool ReadOfflineTestData(const std::string offline_testdata_path, OfflineTestData* testdata) {
234 std::string s;
235 if (!android::base::ReadFileToString(offline_testdata_path, &s)) {
236 return false;
237 }
238 // Parse offline_testdata.
239 std::vector<std::string> lines = android::base::Split(s, "\n");
240 memset(&testdata->unw_context, 0, sizeof(testdata->unw_context));
241 for (const auto& line : lines) {
242 if (android::base::StartsWith(line, "pid:")) {
243 sscanf(line.c_str(), "pid: %d tid: %d", &testdata->pid, &testdata->tid);
244 } else if (android::base::StartsWith(line, "map:")) {
245 testdata->maps.resize(testdata->maps.size() + 1);
246 backtrace_map_t& map = testdata->maps.back();
247 int pos;
248 sscanf(line.c_str(),
249 "map: start: %" SCNxPTR " end: %" SCNxPTR " offset: %" SCNxPTR
250 " load_base: %" SCNxPTR " flags: %d name: %n",
251 &map.start, &map.end, &map.offset, &map.load_base, &map.flags, &pos);
252 map.name = android::base::Trim(line.substr(pos));
253 } else if (android::base::StartsWith(line, "registers:")) {
254 size_t size;
255 int pos;
256 sscanf(line.c_str(), "registers: %zu %n", &size, &pos);
257 if (sizeof(testdata->unw_context) != size) {
258 return false;
259 }
260 HexStringToRawData(&line[pos], &testdata->unw_context, size);
261 } else if (android::base::StartsWith(line, "stack:")) {
262 size_t size;
263 int pos;
264 sscanf(line.c_str(),
265 "stack: start: %" SCNx64 " end: %" SCNx64 " size: %zu %n",
266 &testdata->stack_info.start, &testdata->stack_info.end, &size, &pos);
267 testdata->stack.resize(size);
268 HexStringToRawData(&line[pos], &testdata->stack[0], size);
269 testdata->stack_info.data = testdata->stack.data();
270 } else if (android::base::StartsWith(line, "function:")) {
271 testdata->symbols.resize(testdata->symbols.size() + 1);
272 FunctionSymbol& symbol = testdata->symbols.back();
273 int pos;
274 sscanf(line.c_str(),
275 "function: start: %" SCNxPTR " end: %" SCNxPTR " name: %n",
276 &symbol.start, &symbol.end, &pos);
277 symbol.name = line.substr(pos);
278 }
279 }
280 return true;
281 }
282
BacktraceOfflineTest(const std::string & testlib_name)283 static void BacktraceOfflineTest(const std::string& testlib_name) {
284 const std::string arch = GetArch();
285 if (arch.empty()) {
286 GTEST_LOG_(INFO) << "This test does nothing on current arch.";
287 return;
288 }
289 const std::string testlib_path = "testdata/" + arch + "/" + testlib_name;
290 struct stat st;
291 if (stat(testlib_path.c_str(), &st) == -1) {
292 GTEST_LOG_(INFO) << "This test is skipped as " << testlib_path << " doesn't exist.";
293 return;
294 }
295
296 const std::string offline_testdata_path = "testdata/" + arch + "/offline_testdata";
297 OfflineTestData testdata;
298 ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata));
299
300 // Fix path of libbacktrace_testlib.so.
301 for (auto& map : testdata.maps) {
302 if (map.name.find("libbacktrace_test.so") != std::string::npos) {
303 map.name = testlib_path;
304 }
305 }
306
307 // Do offline backtrace.
308 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(testdata.pid, testdata.maps));
309 ASSERT_TRUE(map != nullptr);
310
311 std::unique_ptr<Backtrace> backtrace(
312 Backtrace::CreateOffline(testdata.pid, testdata.tid, map.get(), testdata.stack_info));
313 ASSERT_TRUE(backtrace != nullptr);
314
315 ucontext_t ucontext = GetUContextFromUnwContext(testdata.unw_context);
316 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
317
318 // Collect pc values of the call stack frames.
319 std::vector<uintptr_t> pc_values;
320 for (size_t i = 0; i < backtrace->NumFrames(); ++i) {
321 pc_values.push_back(backtrace->GetFrame(i)->pc);
322 }
323
324 size_t test_one_index = 0;
325 for (size_t i = 0; i < pc_values.size(); ++i) {
326 if (FunctionNameForAddress(pc_values[i], testdata.symbols) == "test_level_one") {
327 test_one_index = i;
328 break;
329 }
330 }
331
332 ASSERT_GE(test_one_index, 3u);
333 ASSERT_EQ("test_level_one", FunctionNameForAddress(pc_values[test_one_index], testdata.symbols));
334 ASSERT_EQ("test_level_two", FunctionNameForAddress(pc_values[test_one_index - 1],
335 testdata.symbols));
336 ASSERT_EQ("test_level_three", FunctionNameForAddress(pc_values[test_one_index - 2],
337 testdata.symbols));
338 ASSERT_EQ("test_level_four", FunctionNameForAddress(pc_values[test_one_index - 3],
339 testdata.symbols));
340 }
341
TEST(libbacktrace,offline_eh_frame)342 TEST(libbacktrace, offline_eh_frame) {
343 BacktraceOfflineTest("libbacktrace_test_eh_frame.so");
344 }
345
TEST(libbacktrace,offline_debug_frame)346 TEST(libbacktrace, offline_debug_frame) {
347 BacktraceOfflineTest("libbacktrace_test_debug_frame.so");
348 }
349
TEST(libbacktrace,offline_gnu_debugdata)350 TEST(libbacktrace, offline_gnu_debugdata) {
351 BacktraceOfflineTest("libbacktrace_test_gnu_debugdata.so");
352 }
353
TEST(libbacktrace,offline_arm_exidx)354 TEST(libbacktrace, offline_arm_exidx) {
355 BacktraceOfflineTest("libbacktrace_test_arm_exidx.so");
356 }
357
358 // This test tests the situation that ranges of functions covered by .eh_frame and .ARM.exidx
359 // overlap with each other, which appears in /system/lib/libart.so.
TEST(libbacktrace,offline_unwind_mix_eh_frame_and_arm_exidx)360 TEST(libbacktrace, offline_unwind_mix_eh_frame_and_arm_exidx) {
361 const std::string arch = GetArch();
362 if (arch.empty() || arch != "arm") {
363 GTEST_LOG_(INFO) << "This test does nothing on current arch.";
364 return;
365 }
366 const std::string testlib_path = "testdata/" + arch + "/libart.so";
367 struct stat st;
368 ASSERT_EQ(0, stat(testlib_path.c_str(), &st)) << "can't find testlib " << testlib_path;
369
370 const std::string offline_testdata_path = "testdata/" + arch + "/offline_testdata_for_libart";
371 OfflineTestData testdata;
372 ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata));
373
374 // Fix path of /system/lib/libart.so.
375 for (auto& map : testdata.maps) {
376 if (map.name.find("libart.so") != std::string::npos) {
377 map.name = testlib_path;
378 }
379 }
380
381 // Do offline backtrace.
382 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(testdata.pid, testdata.maps));
383 ASSERT_TRUE(map != nullptr);
384
385 std::unique_ptr<Backtrace> backtrace(
386 Backtrace::CreateOffline(testdata.pid, testdata.tid, map.get(), testdata.stack_info));
387 ASSERT_TRUE(backtrace != nullptr);
388
389 ucontext_t ucontext = GetUContextFromUnwContext(testdata.unw_context);
390 ASSERT_TRUE(backtrace->Unwind(0, &ucontext));
391
392 // The last frame is outside of libart.so
393 ASSERT_EQ(testdata.symbols.size() + 1, backtrace->NumFrames());
394 for (size_t i = 0; i + 1 < backtrace->NumFrames(); ++i) {
395 uintptr_t vaddr_in_file = backtrace->GetFrame(i)->pc - testdata.maps[0].start +
396 testdata.maps[0].load_base;
397 std::string name = FunctionNameForAddress(vaddr_in_file, testdata.symbols);
398 ASSERT_EQ(name, testdata.symbols[i].name);
399 }
400 }
401