blob: 9ca373a7ef8a50d16877a1d7d4c299d73eef47a4 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -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
Christopher Ferrisca09ce92015-03-31 17:28:22 -070017#define _GNU_SOURCE 1
Christopher Ferris17e91d42013-10-21 13:30:52 -070018#include <dirent.h>
Christopher Ferris67aba682015-05-08 15:44:46 -070019#include <dlfcn.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070020#include <errno.h>
Christopher Ferris67aba682015-05-08 15:44:46 -070021#include <fcntl.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080022#include <inttypes.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070023#include <pthread.h>
24#include <signal.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080025#include <stdint.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ptrace.h>
Christopher Ferris67aba682015-05-08 15:44:46 -070030#include <sys/stat.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070031#include <sys/types.h>
32#include <sys/wait.h>
33#include <time.h>
34#include <unistd.h>
35
Christopher Ferrise2960912014-03-07 19:42:19 -080036#include <algorithm>
Christopher Ferris67aba682015-05-08 15:44:46 -070037#include <list>
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070038#include <memory>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070039#include <string>
Christopher Ferris17e91d42013-10-21 13:30:52 -070040#include <vector>
41
Dan Albert23f750b2015-04-30 12:52:21 -070042#include <backtrace/Backtrace.h>
43#include <backtrace/BacktraceMap.h>
44
Elliott Hughes4f713192015-12-04 22:00:26 -080045#include <android-base/stringprintf.h>
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070046#include <android-base/unique_fd.h>
Dan Albert23f750b2015-04-30 12:52:21 -070047#include <cutils/atomic.h>
48#include <cutils/threads.h>
49
50#include <gtest/gtest.h>
51
52// For the THREAD_SIGNAL definition.
53#include "BacktraceCurrent.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070054#include "thread_utils.h"
55
56// Number of microseconds per milliseconds.
57#define US_PER_MSEC 1000
58
59// Number of nanoseconds in a second.
60#define NS_PER_SEC 1000000000ULL
61
62// Number of simultaneous dumping operations to perform.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080063#define NUM_THREADS 40
Christopher Ferris17e91d42013-10-21 13:30:52 -070064
65// Number of simultaneous threads running in our forked process.
66#define NUM_PTRACE_THREADS 5
67
Christopher Ferris46756822014-01-14 20:16:30 -080068struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070069 pid_t tid;
70 int32_t state;
71 pthread_t threadId;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070072 void* data;
Christopher Ferris46756822014-01-14 20:16:30 -080073};
Christopher Ferris17e91d42013-10-21 13:30:52 -070074
Christopher Ferris46756822014-01-14 20:16:30 -080075struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070076 thread_t thread;
Christopher Ferris20303f82014-01-10 16:33:16 -080077 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070078 int32_t* now;
79 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080080};
Christopher Ferris17e91d42013-10-21 13:30:52 -070081
82extern "C" {
83// Prototypes for functions in the test library.
84int test_level_one(int, int, int, int, void (*)(void*), void*);
85
86int test_recursive_call(int, void (*)(void*), void*);
87}
88
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070089static uint64_t NanoTime() {
Christopher Ferris17e91d42013-10-21 13:30:52 -070090 struct timespec t = { 0, 0 };
91 clock_gettime(CLOCK_MONOTONIC, &t);
92 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
93}
94
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070095static std::string DumpFrames(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -080096 if (backtrace->NumFrames() == 0) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -070097 return " No frames to dump.\n";
Christopher Ferris20303f82014-01-10 16:33:16 -080098 }
99
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700100 std::string frame;
Christopher Ferris20303f82014-01-10 16:33:16 -0800101 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700102 frame += " " + backtrace->FormatFrameData(i) + '\n';
Christopher Ferris17e91d42013-10-21 13:30:52 -0700103 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700104 return frame;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700105}
106
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700107static void WaitForStop(pid_t pid) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700108 uint64_t start = NanoTime();
109
110 siginfo_t si;
111 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
112 if ((NanoTime() - start) > NS_PER_SEC) {
113 printf("The process did not get to a stopping point in 1 second.\n");
114 break;
115 }
116 usleep(US_PER_MSEC);
117 }
118}
119
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700120static void CreateRemoteProcess(pid_t* pid) {
121 if ((*pid = fork()) == 0) {
122 while (true)
123 ;
124 _exit(0);
125 }
126 ASSERT_NE(-1, *pid);
127
128 ASSERT_TRUE(ptrace(PTRACE_ATTACH, *pid, 0, 0) == 0);
129
130 // Wait for the process to get to a stopping point.
131 WaitForStop(*pid);
132}
133
134static void FinishRemoteProcess(pid_t pid) {
135 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
136
137 kill(pid, SIGKILL);
138 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
139}
140
141static bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700142 // See if test_level_four is in the backtrace.
143 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800144 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
145 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700146 found = true;
147 break;
148 }
149 }
150
151 return found;
152}
153
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700154static void VerifyLevelDump(Backtrace* backtrace) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700155 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
156 << DumpFrames(backtrace);
157 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
158 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700159
160 // Look through the frames starting at the highest to find the
161 // frame we want.
162 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800163 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800164 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700165 frame_num = i;
166 break;
167 }
168 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700169 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
170 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700171
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700172 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one")
173 << DumpFrames(backtrace);
174 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two")
175 << DumpFrames(backtrace);
176 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three")
177 << DumpFrames(backtrace);
178 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four")
179 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700180}
181
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700182static void VerifyLevelBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700183 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800184 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700185 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800186 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800187 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700188
Christopher Ferris20303f82014-01-10 16:33:16 -0800189 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700190}
191
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700192static bool ReadyMaxBacktrace(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800193 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700194}
195
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700196static void VerifyMaxDump(Backtrace* backtrace) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700197 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
198 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700199 // Verify that the last frame is our recursive call.
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700200 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
201 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700202}
203
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700204static void VerifyMaxBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700205 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800206 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700207 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800208 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800209 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700210
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700212}
213
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700214static void ThreadSetState(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700215 thread_t* thread = reinterpret_cast<thread_t*>(data);
216 android_atomic_acquire_store(1, &thread->state);
217 volatile int i = 0;
218 while (thread->state) {
219 i++;
220 }
221}
222
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700223static bool WaitForNonZero(int32_t* value, uint64_t seconds) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700224 uint64_t start = NanoTime();
225 do {
226 if (android_atomic_acquire_load(value)) {
227 return true;
228 }
229 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
230 return false;
231}
232
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700233TEST(libbacktrace, local_no_unwind_frames) {
234 // Verify that a local unwind does not include any frames within
235 // libunwind or libbacktrace.
236 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700237 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700238 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800239 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700240
241 ASSERT_TRUE(backtrace->NumFrames() != 0);
242 for (const auto& frame : *backtrace ) {
243 if (BacktraceMap::IsValid(frame.map)) {
244 const std::string name = basename(frame.map.name.c_str());
245 ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
246 << DumpFrames(backtrace.get());
247 }
248 break;
249 }
250}
251
Christopher Ferris17e91d42013-10-21 13:30:52 -0700252TEST(libbacktrace, local_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700253 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700254}
255
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700256static void VerifyIgnoreFrames(Backtrace* bt_all, Backtrace* bt_ign1, Backtrace* bt_ign2,
257 const char* cur_proc) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700258 EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1)
259 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1);
260 EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2)
261 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 2 backtrace:\n" << DumpFrames(bt_ign2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700262
263 // Check all of the frames are the same > the current frame.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700264 bool check = (cur_proc == nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800265 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700266 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800267 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
268 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
269 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700270
Christopher Ferris20303f82014-01-10 16:33:16 -0800271 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
272 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
273 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700274 }
Christopher Ferris46756822014-01-14 20:16:30 -0800275 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700276 check = true;
277 }
278 }
279}
280
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700281static void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700282 std::unique_ptr<Backtrace> all(
Christopher Ferris20303f82014-01-10 16:33:16 -0800283 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700284 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800285 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800286 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700287
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700288 std::unique_ptr<Backtrace> ign1(
Christopher Ferris20303f82014-01-10 16:33:16 -0800289 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700290 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800291 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800292 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700293
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700294 std::unique_ptr<Backtrace> ign2(
Christopher Ferris20303f82014-01-10 16:33:16 -0800295 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700296 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800297 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800298 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700299
Christopher Ferris20303f82014-01-10 16:33:16 -0800300 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700301}
302
303TEST(libbacktrace, local_trace_ignore_frames) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700304 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700305}
306
307TEST(libbacktrace, local_max_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700308 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700309}
310
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700311static void VerifyProcTest(pid_t pid, pid_t tid, bool share_map, bool (*ReadyFunc)(Backtrace*),
312 void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700313 pid_t ptrace_tid;
314 if (tid < 0) {
315 ptrace_tid = pid;
316 } else {
317 ptrace_tid = tid;
318 }
319 uint64_t start = NanoTime();
320 bool verified = false;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700321 std::string last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700322 do {
323 usleep(US_PER_MSEC);
324 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
325 // Wait for the process to get to a stopping point.
326 WaitForStop(ptrace_tid);
327
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700328 std::unique_ptr<BacktraceMap> map;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800329 if (share_map) {
330 map.reset(BacktraceMap::Create(pid));
331 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700332 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700333 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700334 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800335 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris20303f82014-01-10 16:33:16 -0800336 if (ReadyFunc(backtrace.get())) {
337 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700338 verified = true;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700339 } else {
340 last_dump = DumpFrames(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700341 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800342
Christopher Ferris17e91d42013-10-21 13:30:52 -0700343 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
344 }
345 // If 5 seconds have passed, then we are done.
346 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700347 ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700348}
349
350TEST(libbacktrace, ptrace_trace) {
351 pid_t pid;
352 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700353 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800354 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700355 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800356 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
357
358 kill(pid, SIGKILL);
359 int status;
360 ASSERT_EQ(waitpid(pid, &status, 0), pid);
361}
362
363TEST(libbacktrace, ptrace_trace_shared_map) {
364 pid_t pid;
365 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700366 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800367 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800368 }
369
370 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700371
372 kill(pid, SIGKILL);
373 int status;
374 ASSERT_EQ(waitpid(pid, &status, 0), pid);
375}
376
377TEST(libbacktrace, ptrace_max_trace) {
378 pid_t pid;
379 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700380 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800381 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700382 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800383 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700384
385 kill(pid, SIGKILL);
386 int status;
387 ASSERT_EQ(waitpid(pid, &status, 0), pid);
388}
389
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700390static void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700391 std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
392 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800393 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800394 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700395
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700396 std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
397 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800398 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800399 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700400
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700401 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700402}
403
404TEST(libbacktrace, ptrace_ignore_frames) {
405 pid_t pid;
406 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700407 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800408 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700409 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800410 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700411
412 kill(pid, SIGKILL);
413 int status;
414 ASSERT_EQ(waitpid(pid, &status, 0), pid);
415}
416
417// Create a process with multiple threads and dump all of the threads.
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700418static void* PtraceThreadLevelRun(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700419 EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
420 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700421}
422
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700423static void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700424 // Get the list of tasks.
425 char task_path[128];
426 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
427
James Hawkins588a2ca2016-02-18 14:52:46 -0800428 std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700429 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700430 struct dirent* entry;
James Hawkins588a2ca2016-02-18 14:52:46 -0800431 while ((entry = readdir(tasks_dir.get())) != nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700432 char* end;
433 pid_t tid = strtoul(entry->d_name, &end, 10);
434 if (*end == '\0') {
435 threads->push_back(tid);
436 }
437 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700438}
439
440TEST(libbacktrace, ptrace_threads) {
441 pid_t pid;
442 if ((pid = fork()) == 0) {
443 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
444 pthread_attr_t attr;
445 pthread_attr_init(&attr);
446 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
447
448 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700449 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700450 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700451 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800452 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700453 }
454
455 // Check to see that all of the threads are running before unwinding.
456 std::vector<pid_t> threads;
457 uint64_t start = NanoTime();
458 do {
459 usleep(US_PER_MSEC);
460 threads.clear();
461 GetThreads(pid, &threads);
462 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
463 ((NanoTime() - start) <= 5 * NS_PER_SEC));
464 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
465
466 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
467 WaitForStop(pid);
468 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
469 // Skip the current forked process, we only care about the threads.
470 if (pid == *it) {
471 continue;
472 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800473 VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700474 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700475
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700476 FinishRemoteProcess(pid);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700477}
478
479void VerifyLevelThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700480 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
481 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800482 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800483 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700484
Christopher Ferris20303f82014-01-10 16:33:16 -0800485 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700486}
487
488TEST(libbacktrace, thread_current_level) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700489 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700490}
491
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700492static void VerifyMaxThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700493 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
494 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800496 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700497
Christopher Ferris20303f82014-01-10 16:33:16 -0800498 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700499}
500
501TEST(libbacktrace, thread_current_max) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700502 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700503}
504
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700505static void* ThreadLevelRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700506 thread_t* thread = reinterpret_cast<thread_t*>(data);
507
508 thread->tid = gettid();
509 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700510 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700511}
512
513TEST(libbacktrace, thread_level_trace) {
514 pthread_attr_t attr;
515 pthread_attr_init(&attr);
516 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
517
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700518 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700519 pthread_t thread;
520 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
521
522 // Wait up to 2 seconds for the tid to be set.
523 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
524
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700525 // Make sure that the thread signal used is not visible when compiled for
526 // the target.
527#if !defined(__GLIBC__)
528 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
529#endif
530
Christopher Ferris17e91d42013-10-21 13:30:52 -0700531 // Save the current signal action and make sure it is restored afterwards.
532 struct sigaction cur_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700533 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700534
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700535 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
536 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800537 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800538 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700539
Christopher Ferris20303f82014-01-10 16:33:16 -0800540 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700541
542 // Tell the thread to exit its infinite loop.
543 android_atomic_acquire_store(0, &thread_data.state);
544
545 // Verify that the old action was restored.
546 struct sigaction new_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700547 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700548 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800549 // The SA_RESTORER flag gets set behind our back, so a direct comparison
550 // doesn't work unless we mask the value off. Mips doesn't have this
551 // flag, so skip this on that platform.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700552#if defined(SA_RESTORER)
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800553 cur_action.sa_flags &= ~SA_RESTORER;
554 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700555#elif defined(__GLIBC__)
556 // Our host compiler doesn't appear to define this flag for some reason.
557 cur_action.sa_flags &= ~0x04000000;
558 new_action.sa_flags &= ~0x04000000;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800559#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700560 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
561}
562
563TEST(libbacktrace, thread_ignore_frames) {
564 pthread_attr_t attr;
565 pthread_attr_init(&attr);
566 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
567
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700568 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700569 pthread_t thread;
570 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
571
572 // Wait up to 2 seconds for the tid to be set.
573 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
574
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700575 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
576 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800577 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800578 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700579
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700580 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
581 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800582 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800583 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700584
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700585 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
586 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800587 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800588 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700589
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700590 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700591
592 // Tell the thread to exit its infinite loop.
593 android_atomic_acquire_store(0, &thread_data.state);
594}
595
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700596static void* ThreadMaxRun(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700597 thread_t* thread = reinterpret_cast<thread_t*>(data);
598
599 thread->tid = gettid();
600 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700601 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700602}
603
604TEST(libbacktrace, thread_max_trace) {
605 pthread_attr_t attr;
606 pthread_attr_init(&attr);
607 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
608
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700609 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700610 pthread_t thread;
611 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
612
613 // Wait for the tid to be set.
614 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
615
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700616 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
617 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800618 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800619 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700620
Christopher Ferris20303f82014-01-10 16:33:16 -0800621 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700622
623 // Tell the thread to exit its infinite loop.
624 android_atomic_acquire_store(0, &thread_data.state);
625}
626
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700627static void* ThreadDump(void* data) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700628 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
629 while (true) {
630 if (android_atomic_acquire_load(dump->now)) {
631 break;
632 }
633 }
634
Christopher Ferris17e91d42013-10-21 13:30:52 -0700635 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800636 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
637 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700638
639 android_atomic_acquire_store(1, &dump->done);
640
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700641 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700642}
643
644TEST(libbacktrace, thread_multiple_dump) {
645 // Dump NUM_THREADS simultaneously.
646 std::vector<thread_t> runners(NUM_THREADS);
647 std::vector<dump_thread_t> dumpers(NUM_THREADS);
648
649 pthread_attr_t attr;
650 pthread_attr_init(&attr);
651 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
652 for (size_t i = 0; i < NUM_THREADS; i++) {
653 // Launch the runners, they will spin in hard loops doing nothing.
654 runners[i].tid = 0;
655 runners[i].state = 0;
656 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
657 }
658
659 // Wait for tids to be set.
660 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800661 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700662 }
663
664 // Start all of the dumpers at once, they will spin until they are signalled
665 // to begin their dump run.
666 int32_t dump_now = 0;
667 for (size_t i = 0; i < NUM_THREADS; i++) {
668 dumpers[i].thread.tid = runners[i].tid;
669 dumpers[i].thread.state = 0;
670 dumpers[i].done = 0;
671 dumpers[i].now = &dump_now;
672
673 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
674 }
675
676 // Start all of the dumpers going at once.
677 android_atomic_acquire_store(1, &dump_now);
678
679 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800680 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700681
682 // Tell the runner thread to exit its infinite loop.
683 android_atomic_acquire_store(0, &runners[i].state);
684
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700685 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800686 VerifyMaxDump(dumpers[i].backtrace);
687
688 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700689 dumpers[i].backtrace = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700690 }
691}
692
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700693TEST(libbacktrace, thread_multiple_dump_same_thread) {
694 pthread_attr_t attr;
695 pthread_attr_init(&attr);
696 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
697 thread_t runner;
698 runner.tid = 0;
699 runner.state = 0;
700 ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
701
702 // Wait for tids to be set.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800703 ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700704
705 // Start all of the dumpers at once, they will spin until they are signalled
706 // to begin their dump run.
707 int32_t dump_now = 0;
708 // Dump the same thread NUM_THREADS simultaneously.
709 std::vector<dump_thread_t> dumpers(NUM_THREADS);
710 for (size_t i = 0; i < NUM_THREADS; i++) {
711 dumpers[i].thread.tid = runner.tid;
712 dumpers[i].thread.state = 0;
713 dumpers[i].done = 0;
714 dumpers[i].now = &dump_now;
715
716 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
717 }
718
719 // Start all of the dumpers going at once.
720 android_atomic_acquire_store(1, &dump_now);
721
722 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800723 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700724
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700725 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700726 VerifyMaxDump(dumpers[i].backtrace);
727
728 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700729 dumpers[i].backtrace = nullptr;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700730 }
731
732 // Tell the runner thread to exit its infinite loop.
733 android_atomic_acquire_store(0, &runner.state);
734}
735
Christopher Ferrisdf290612014-01-22 19:21:07 -0800736// This test is for UnwindMaps that should share the same map cursor when
737// multiple maps are created for the current process at the same time.
738TEST(libbacktrace, simultaneous_maps) {
739 BacktraceMap* map1 = BacktraceMap::Create(getpid());
740 BacktraceMap* map2 = BacktraceMap::Create(getpid());
741 BacktraceMap* map3 = BacktraceMap::Create(getpid());
742
743 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700744 ASSERT_TRUE(back1 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800745 EXPECT_TRUE(back1->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800746 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back1->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800747 delete back1;
748 delete map1;
749
750 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700751 ASSERT_TRUE(back2 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800752 EXPECT_TRUE(back2->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800753 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back2->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800754 delete back2;
755 delete map2;
756
757 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700758 ASSERT_TRUE(back3 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800759 EXPECT_TRUE(back3->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -0800760 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back3->GetError());
Christopher Ferrisdf290612014-01-22 19:21:07 -0800761 delete back3;
762 delete map3;
763}
764
Christopher Ferris12385e32015-02-06 13:22:01 -0800765TEST(libbacktrace, fillin_erases) {
766 BacktraceMap* back_map = BacktraceMap::Create(getpid());
767
768 backtrace_map_t map;
769
770 map.start = 1;
771 map.end = 3;
772 map.flags = 1;
773 map.name = "Initialized";
774 back_map->FillIn(0, &map);
775 delete back_map;
776
777 ASSERT_FALSE(BacktraceMap::IsValid(map));
778 ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
779 ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
780 ASSERT_EQ(0, map.flags);
781 ASSERT_EQ("", map.name);
782}
783
Christopher Ferris17e91d42013-10-21 13:30:52 -0700784TEST(libbacktrace, format_test) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700785 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
786 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700787
Christopher Ferris20303f82014-01-10 16:33:16 -0800788 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800789 frame.num = 1;
790 frame.pc = 2;
791 frame.sp = 0;
792 frame.stack_size = 0;
Christopher Ferris46756822014-01-14 20:16:30 -0800793 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700794
Christopher Ferris46756822014-01-14 20:16:30 -0800795 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800796 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700797#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800798 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700799#else
Christopher Ferris46756822014-01-14 20:16:30 -0800800 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700801#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800802 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700803
Christopher Ferris46756822014-01-14 20:16:30 -0800804 // Check map name empty, but exists.
Christopher Ferrisda750a72015-11-30 13:36:08 -0800805 frame.pc = 0xb0020;
806 frame.map.start = 0xb0000;
807 frame.map.end = 0xbffff;
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700808 frame.map.load_base = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700809#if defined(__LP64__)
Christopher Ferrisda750a72015-11-30 13:36:08 -0800810 EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700811#else
Christopher Ferrisda750a72015-11-30 13:36:08 -0800812 EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700813#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800814 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700815
Christopher Ferrisda750a72015-11-30 13:36:08 -0800816 // Check map name begins with a [.
817 frame.pc = 0xc0020;
818 frame.map.start = 0xc0000;
819 frame.map.end = 0xcffff;
820 frame.map.load_base = 0;
821 frame.map.name = "[anon:thread signal stack]";
822#if defined(__LP64__)
823 EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]",
824#else
825 EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]",
826#endif
827 backtrace->FormatFrameData(&frame));
Christopher Ferris46756822014-01-14 20:16:30 -0800828
829 // Check relative pc is set and map name is set.
830 frame.pc = 0x12345679;
Christopher Ferris12385e32015-02-06 13:22:01 -0800831 frame.map.name = "MapFake";
832 frame.map.start = 1;
833 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700834#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800835 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700836#else
Christopher Ferris46756822014-01-14 20:16:30 -0800837 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700838#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800839 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700840
Christopher Ferris46756822014-01-14 20:16:30 -0800841 // Check func_name is set, but no func offset.
842 frame.func_name = "ProcFake";
843#if defined(__LP64__)
844 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
845#else
846 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
847#endif
848 backtrace->FormatFrameData(&frame));
849
850 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800851 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700852#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800853 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700854#else
Christopher Ferris46756822014-01-14 20:16:30 -0800855 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700856#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800857 backtrace->FormatFrameData(&frame));
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700858
859 // Check func_name is set, func offset is non-zero, and load_base is non-zero.
860 frame.func_offset = 645;
861 frame.map.load_base = 100;
862#if defined(__LP64__)
863 EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)",
864#else
865 EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)",
866#endif
867 backtrace->FormatFrameData(&frame));
Christopher Ferrise0ab2322015-08-20 11:16:54 -0700868
869 // Check a non-zero map offset.
870 frame.map.offset = 0x1000;
871#if defined(__LP64__)
872 EXPECT_EQ("#01 pc 00000000123456dc MapFake (offset 0x1000) (ProcFake+645)",
873#else
874 EXPECT_EQ("#01 pc 123456dc MapFake (offset 0x1000) (ProcFake+645)",
875#endif
876 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700877}
Christopher Ferrise2960912014-03-07 19:42:19 -0800878
879struct map_test_t {
880 uintptr_t start;
881 uintptr_t end;
882};
883
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700884static bool map_sort(map_test_t i, map_test_t j) { return i.start < j.start; }
Christopher Ferrise2960912014-03-07 19:42:19 -0800885
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700886static void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800887 char buffer[4096];
888 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
889
890 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700891 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800892 std::vector<map_test_t> test_maps;
893 while (fgets(buffer, sizeof(buffer), map_file)) {
894 map_test_t map;
895 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
896 test_maps.push_back(map);
897 }
898 fclose(map_file);
899 std::sort(test_maps.begin(), test_maps.end(), map_sort);
900
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700901 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800902
903 // Basic test that verifies that the map is in the expected order.
Christopher Ferris3a140042016-06-15 15:49:50 -0700904 ScopedBacktraceMapIteratorLock lock(map.get());
Christopher Ferrise2960912014-03-07 19:42:19 -0800905 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
906 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
907 ASSERT_TRUE(test_it != test_maps.end());
908 ASSERT_EQ(test_it->start, it->start);
909 ASSERT_EQ(test_it->end, it->end);
910 ++test_it;
911 }
912 ASSERT_TRUE(test_it == test_maps.end());
913}
914
915TEST(libbacktrace, verify_map_remote) {
916 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700917 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -0800918
919 // The maps should match exactly since the forked process has been paused.
920 VerifyMap(pid);
921
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700922 FinishRemoteProcess(pid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700923}
924
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700925static void InitMemory(uint8_t* memory, size_t bytes) {
Christopher Ferris944f4172015-05-06 16:36:34 -0700926 for (size_t i = 0; i < bytes; i++) {
927 memory[i] = i;
928 if (memory[i] == '\0') {
929 // Don't use '\0' in our data so we can verify that an overread doesn't
930 // occur by using a '\0' as the character after the read data.
931 memory[i] = 23;
932 }
933 }
934}
935
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700936static void* ThreadReadTest(void* data) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700937 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
938
939 thread_data->tid = gettid();
940
941 // Create two map pages.
942 // Mark the second page as not-readable.
943 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
944 uint8_t* memory;
945 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
946 return reinterpret_cast<void*>(-1);
947 }
948
949 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
950 return reinterpret_cast<void*>(-1);
951 }
952
953 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -0700954 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700955
956 thread_data->data = memory;
957
958 // Tell the caller it's okay to start reading memory.
959 android_atomic_acquire_store(1, &thread_data->state);
960
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700961 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700962 while (thread_data->state) {
963 }
964
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700965 // Re-enable read-write on the page so that we don't crash if we try
966 // and access data on this page when freeing the memory.
967 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
968 return reinterpret_cast<void*>(-1);
969 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700970 free(memory);
971
972 android_atomic_acquire_store(1, &thread_data->state);
973
974 return nullptr;
975}
976
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700977static void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700978 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
979
980 // Create a page of data to use to do quick compares.
981 uint8_t* expected = new uint8_t[pagesize];
Christopher Ferris944f4172015-05-06 16:36:34 -0700982 InitMemory(expected, pagesize);
983
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700984 uint8_t* data = new uint8_t[2*pagesize];
985 // Verify that we can only read one page worth of data.
986 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
987 ASSERT_EQ(pagesize, bytes_read);
988 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
989
990 // Verify unaligned reads.
991 for (size_t i = 1; i < sizeof(word_t); i++) {
992 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
993 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
994 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
995 << "Offset at " << i << " failed";
996 }
Christopher Ferris944f4172015-05-06 16:36:34 -0700997
998 // Verify small unaligned reads.
999 for (size_t i = 1; i < sizeof(word_t); i++) {
1000 for (size_t j = 1; j < sizeof(word_t); j++) {
1001 // Set one byte past what we expect to read, to guarantee we don't overread.
1002 data[j] = '\0';
1003 bytes_read = backtrace->Read(read_addr + i, data, j);
1004 ASSERT_EQ(j, bytes_read);
1005 ASSERT_TRUE(memcmp(data, &expected[i], j) == 0)
1006 << "Offset at " << i << " length " << j << " miscompared";
1007 ASSERT_EQ('\0', data[j])
1008 << "Offset at " << i << " length " << j << " wrote too much data";
1009 }
1010 }
Pirama Arumuga Nainar837eff22015-07-09 10:50:04 -07001011 delete[] data;
1012 delete[] expected;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001013}
1014
1015TEST(libbacktrace, thread_read) {
1016 pthread_attr_t attr;
1017 pthread_attr_init(&attr);
1018 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1019 pthread_t thread;
1020 thread_t thread_data = { 0, 0, 0, nullptr };
1021 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
1022
1023 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1024
1025 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
1026 ASSERT_TRUE(backtrace.get() != nullptr);
1027
1028 RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
1029
1030 android_atomic_acquire_store(0, &thread_data.state);
1031
1032 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
1033}
1034
1035volatile uintptr_t g_ready = 0;
1036volatile uintptr_t g_addr = 0;
1037
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001038static void ForkedReadTest() {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001039 // Create two map pages.
1040 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1041 uint8_t* memory;
1042 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1043 perror("Failed to allocate memory\n");
1044 exit(1);
1045 }
1046
1047 // Mark the second page as not-readable.
1048 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1049 perror("Failed to mprotect memory\n");
1050 exit(1);
1051 }
1052
1053 // Set up a simple pattern in memory.
Christopher Ferris944f4172015-05-06 16:36:34 -07001054 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001055
1056 g_addr = reinterpret_cast<uintptr_t>(memory);
1057 g_ready = 1;
1058
1059 while (1) {
1060 usleep(US_PER_MSEC);
1061 }
1062}
1063
1064TEST(libbacktrace, process_read) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001065 g_ready = 0;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001066 pid_t pid;
1067 if ((pid = fork()) == 0) {
1068 ForkedReadTest();
1069 exit(0);
1070 }
1071 ASSERT_NE(-1, pid);
1072
1073 bool test_executed = false;
1074 uint64_t start = NanoTime();
1075 while (1) {
1076 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
1077 WaitForStop(pid);
1078
1079 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris97e00bb2015-04-02 14:22:31 -07001080 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001081
1082 uintptr_t read_addr;
1083 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
1084 reinterpret_cast<uint8_t*>(&read_addr),
1085 sizeof(uintptr_t));
1086 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1087 if (read_addr) {
1088 // The forked process is ready to be read.
1089 bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
1090 reinterpret_cast<uint8_t*>(&read_addr),
1091 sizeof(uintptr_t));
1092 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1093
1094 RunReadTest(backtrace.get(), read_addr);
1095
1096 test_executed = true;
1097 break;
1098 }
1099 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1100 }
1101 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1102 break;
1103 }
1104 usleep(US_PER_MSEC);
1105 }
1106 kill(pid, SIGKILL);
1107 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1108
1109 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001110}
1111
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001112static void VerifyFunctionsFound(const std::vector<std::string>& found_functions) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001113 // We expect to find these functions in libbacktrace_test. If we don't
1114 // find them, that's a bug in the memory read handling code in libunwind.
1115 std::list<std::string> expected_functions;
1116 expected_functions.push_back("test_recursive_call");
1117 expected_functions.push_back("test_level_one");
1118 expected_functions.push_back("test_level_two");
1119 expected_functions.push_back("test_level_three");
1120 expected_functions.push_back("test_level_four");
1121 for (const auto& found_function : found_functions) {
1122 for (const auto& expected_function : expected_functions) {
1123 if (found_function == expected_function) {
1124 expected_functions.remove(found_function);
1125 break;
1126 }
1127 }
1128 }
1129 ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
1130}
1131
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001132static const char* CopySharedLibrary() {
Christopher Ferris67aba682015-05-08 15:44:46 -07001133#if defined(__LP64__)
1134 const char* lib_name = "lib64";
1135#else
1136 const char* lib_name = "lib";
1137#endif
1138
1139#if defined(__BIONIC__)
1140 const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so";
1141 std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s",
1142 lib_name, tmp_so_name);
1143#else
1144 const char* tmp_so_name = "/tmp/libbacktrace_test.so";
1145 if (getenv("ANDROID_HOST_OUT") == NULL) {
1146 fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch.");
1147 return nullptr;
1148 }
1149 std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s",
1150 getenv("ANDROID_HOST_OUT"), lib_name,
1151 tmp_so_name);
1152#endif
1153
1154 // Copy the shared so to a tempory directory.
1155 system(cp_cmd.c_str());
1156
1157 return tmp_so_name;
1158}
1159
1160TEST(libbacktrace, check_unreadable_elf_local) {
1161 const char* tmp_so_name = CopySharedLibrary();
1162 ASSERT_TRUE(tmp_so_name != nullptr);
1163
1164 struct stat buf;
1165 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1166 uintptr_t map_size = buf.st_size;
1167
1168 int fd = open(tmp_so_name, O_RDONLY);
1169 ASSERT_TRUE(fd != -1);
1170
Christopher Ferris61c48ac2016-01-15 16:08:58 -08001171 void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001172 ASSERT_TRUE(map != MAP_FAILED);
1173 close(fd);
1174 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1175
1176 std::vector<std::string> found_functions;
1177 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1178 BACKTRACE_CURRENT_THREAD));
1179 ASSERT_TRUE(backtrace.get() != nullptr);
1180
1181 // Needed before GetFunctionName will work.
1182 backtrace->Unwind(0);
1183
1184 // Loop through the entire map, and get every function we can find.
1185 map_size += reinterpret_cast<uintptr_t>(map);
1186 std::string last_func;
1187 for (uintptr_t read_addr = reinterpret_cast<uintptr_t>(map);
1188 read_addr < map_size; read_addr += 4) {
1189 uintptr_t offset;
1190 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1191 if (!func_name.empty() && last_func != func_name) {
1192 found_functions.push_back(func_name);
1193 }
1194 last_func = func_name;
1195 }
1196
1197 ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uintptr_t>(map)) == 0);
1198
1199 VerifyFunctionsFound(found_functions);
1200}
1201
1202TEST(libbacktrace, check_unreadable_elf_remote) {
1203 const char* tmp_so_name = CopySharedLibrary();
1204 ASSERT_TRUE(tmp_so_name != nullptr);
1205
1206 g_ready = 0;
1207
1208 struct stat buf;
1209 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
1210 uintptr_t map_size = buf.st_size;
1211
1212 pid_t pid;
1213 if ((pid = fork()) == 0) {
1214 int fd = open(tmp_so_name, O_RDONLY);
1215 if (fd == -1) {
1216 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno));
1217 unlink(tmp_so_name);
1218 exit(0);
1219 }
1220
Christopher Ferris61c48ac2016-01-15 16:08:58 -08001221 void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
Christopher Ferris67aba682015-05-08 15:44:46 -07001222 if (map == MAP_FAILED) {
1223 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
1224 unlink(tmp_so_name);
1225 exit(0);
1226 }
1227 close(fd);
1228 if (unlink(tmp_so_name) == -1) {
1229 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
1230 exit(0);
1231 }
1232
1233 g_addr = reinterpret_cast<uintptr_t>(map);
1234 g_ready = 1;
1235 while (true) {
1236 usleep(US_PER_MSEC);
1237 }
1238 exit(0);
1239 }
1240 ASSERT_TRUE(pid > 0);
1241
1242 std::vector<std::string> found_functions;
1243 uint64_t start = NanoTime();
1244 while (true) {
1245 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1246
1247 // Wait for the process to get to a stopping point.
1248 WaitForStop(pid);
1249
1250 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1251 ASSERT_TRUE(backtrace.get() != nullptr);
1252
1253 uintptr_t read_addr;
1254 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1255 if (read_addr) {
1256 ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t)));
1257
1258 // Needed before GetFunctionName will work.
1259 backtrace->Unwind(0);
1260
1261 // Loop through the entire map, and get every function we can find.
1262 map_size += read_addr;
1263 std::string last_func;
1264 for (; read_addr < map_size; read_addr += 4) {
1265 uintptr_t offset;
1266 std::string func_name = backtrace->GetFunctionName(read_addr, &offset);
1267 if (!func_name.empty() && last_func != func_name) {
1268 found_functions.push_back(func_name);
1269 }
1270 last_func = func_name;
1271 }
1272 break;
1273 }
1274 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1275
1276 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1277 break;
1278 }
1279 usleep(US_PER_MSEC);
1280 }
1281
1282 kill(pid, SIGKILL);
1283 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1284
1285 VerifyFunctionsFound(found_functions);
1286}
1287
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001288static bool FindFuncFrameInBacktrace(Backtrace* backtrace, uintptr_t test_func, size_t* frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001289 backtrace_map_t map;
1290 backtrace->FillInMap(test_func, &map);
1291 if (!BacktraceMap::IsValid(map)) {
1292 return false;
1293 }
1294
1295 // Loop through the frames, and find the one that is in the map.
1296 *frame_num = 0;
1297 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
1298 if (BacktraceMap::IsValid(it->map) && map.start == it->map.start &&
1299 it->pc >= test_func) {
1300 *frame_num = it->num;
1301 return true;
1302 }
1303 }
1304 return false;
1305}
1306
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001307static void VerifyUnreadableElfFrame(Backtrace* backtrace, uintptr_t test_func, size_t frame_num) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001308 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
1309 << DumpFrames(backtrace);
1310
1311 ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace);
1312 // Make sure that there is at least one more frame above the test func call.
1313 ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace);
1314
1315 uintptr_t diff = backtrace->GetFrame(frame_num)->pc - test_func;
1316 ASSERT_LT(diff, 200U) << DumpFrames(backtrace);
1317}
1318
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001319static void VerifyUnreadableElfBacktrace(uintptr_t test_func) {
Christopher Ferris67aba682015-05-08 15:44:46 -07001320 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
1321 BACKTRACE_CURRENT_THREAD));
1322 ASSERT_TRUE(backtrace.get() != nullptr);
1323 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001324 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris67aba682015-05-08 15:44:46 -07001325
1326 size_t frame_num;
1327 ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num));
1328
1329 VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num);
1330}
1331
1332typedef int (*test_func_t)(int, int, int, int, void (*)(uintptr_t), uintptr_t);
1333
1334TEST(libbacktrace, unwind_through_unreadable_elf_local) {
1335 const char* tmp_so_name = CopySharedLibrary();
1336 ASSERT_TRUE(tmp_so_name != nullptr);
1337 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1338 ASSERT_TRUE(lib_handle != nullptr);
1339 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1340
1341 test_func_t test_func;
1342 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1343 ASSERT_TRUE(test_func != nullptr);
1344
1345 ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace,
1346 reinterpret_cast<uintptr_t>(test_func)), 0);
1347
1348 ASSERT_TRUE(dlclose(lib_handle) == 0);
1349}
1350
1351TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
1352 const char* tmp_so_name = CopySharedLibrary();
1353 ASSERT_TRUE(tmp_so_name != nullptr);
1354 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
1355 ASSERT_TRUE(lib_handle != nullptr);
1356 ASSERT_TRUE(unlink(tmp_so_name) != -1);
1357
1358 test_func_t test_func;
1359 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
1360 ASSERT_TRUE(test_func != nullptr);
1361
1362 pid_t pid;
1363 if ((pid = fork()) == 0) {
1364 test_func(1, 2, 3, 4, 0, 0);
1365 exit(0);
1366 }
1367 ASSERT_TRUE(pid > 0);
1368 ASSERT_TRUE(dlclose(lib_handle) == 0);
1369
1370 uint64_t start = NanoTime();
1371 bool done = false;
1372 while (!done) {
1373 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1374
1375 // Wait for the process to get to a stopping point.
1376 WaitForStop(pid);
1377
1378 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
1379 ASSERT_TRUE(backtrace.get() != nullptr);
1380 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001381 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferris67aba682015-05-08 15:44:46 -07001382
1383 size_t frame_num;
1384 if (FindFuncFrameInBacktrace(backtrace.get(),
1385 reinterpret_cast<uintptr_t>(test_func), &frame_num)) {
1386
1387 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uintptr_t>(test_func), frame_num);
1388 done = true;
1389 }
1390
1391 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1392
1393 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1394 break;
1395 }
1396 usleep(US_PER_MSEC);
1397 }
1398
1399 kill(pid, SIGKILL);
1400 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1401
1402 ASSERT_TRUE(done) << "Test function never found in unwind.";
1403}
1404
Christopher Ferris206a3b92016-03-09 14:35:54 -08001405TEST(libbacktrace, unwind_thread_doesnt_exist) {
1406 std::unique_ptr<Backtrace> backtrace(
1407 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, 99999999));
1408 ASSERT_TRUE(backtrace.get() != nullptr);
1409 ASSERT_FALSE(backtrace->Unwind(0));
1410 ASSERT_EQ(BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST, backtrace->GetError());
1411}
1412
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001413TEST(libbacktrace, local_get_function_name_before_unwind) {
1414 std::unique_ptr<Backtrace> backtrace(
1415 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
1416 ASSERT_TRUE(backtrace.get() != nullptr);
1417
1418 // Verify that trying to get a function name before doing an unwind works.
1419 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1420 size_t offset;
1421 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1422}
1423
1424TEST(libbacktrace, remote_get_function_name_before_unwind) {
1425 pid_t pid;
1426 CreateRemoteProcess(&pid);
1427
1428 // Now create an unwind object.
1429 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
1430
1431 // Verify that trying to get a function name before doing an unwind works.
1432 uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1;
1433 size_t offset;
1434 ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset));
1435
1436 FinishRemoteProcess(pid);
1437}
1438
Christopher Ferrise2960912014-03-07 19:42:19 -08001439#if defined(ENABLE_PSS_TESTS)
1440#include "GetPss.h"
1441
Chih-Hung Hsieh67867db2016-05-18 15:53:15 -07001442#define MAX_LEAK_BYTES (32*1024UL)
Christopher Ferrise2960912014-03-07 19:42:19 -08001443
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001444static void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001445 // Do a few runs to get the PSS stable.
1446 for (size_t i = 0; i < 100; i++) {
1447 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001448 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001449 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001450 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrise2960912014-03-07 19:42:19 -08001451 delete backtrace;
1452 }
1453 size_t stable_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001454 ASSERT_TRUE(stable_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001455
1456 // Loop enough that even a small leak should be detectable.
1457 for (size_t i = 0; i < 4096; i++) {
1458 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001459 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001460 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris206a3b92016-03-09 14:35:54 -08001461 ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError());
Christopher Ferrise2960912014-03-07 19:42:19 -08001462 delete backtrace;
1463 }
1464 size_t new_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001465 ASSERT_TRUE(new_pss != 0);
Christopher Ferris5ccdfa62016-03-07 19:18:31 -08001466 if (new_pss > stable_pss) {
1467 ASSERT_LE(new_pss - stable_pss, MAX_LEAK_BYTES);
1468 }
Christopher Ferrise2960912014-03-07 19:42:19 -08001469}
1470
1471TEST(libbacktrace, check_for_leak_local) {
1472 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1473}
1474
1475TEST(libbacktrace, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001476 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001477 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001478 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001479
1480 // Wait up to 2 seconds for the tid to be set.
1481 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1482
1483 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1484
1485 // Tell the thread to exit its infinite loop.
1486 android_atomic_acquire_store(0, &thread_data.state);
1487
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001488 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001489}
1490
1491TEST(libbacktrace, check_for_leak_remote) {
1492 pid_t pid;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001493 CreateRemoteProcess(&pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001494
1495 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1496
Christopher Ferris82f3bbd2017-03-14 15:22:26 -07001497 FinishRemoteProcess(pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001498}
1499#endif