blob: 5de80b1ac26f17e0e417febb7434d664dc66c5e5 [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>
19#include <errno.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080020#include <inttypes.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070021#include <pthread.h>
22#include <signal.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080023#include <stdint.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/ptrace.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <time.h>
31#include <unistd.h>
32
Christopher Ferris20303f82014-01-10 16:33:16 -080033#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080034#include <backtrace/BacktraceMap.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070035
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -070036// For the THREAD_SIGNAL definition.
Christopher Ferris2c43cff2015-03-26 19:18:36 -070037#include "BacktraceCurrent.h"
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -070038
Christopher Ferris17e91d42013-10-21 13:30:52 -070039#include <cutils/atomic.h>
40#include <gtest/gtest.h>
41
Christopher Ferrise2960912014-03-07 19:42:19 -080042#include <algorithm>
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070043#include <memory>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070044#include <string>
Christopher Ferris17e91d42013-10-21 13:30:52 -070045#include <vector>
46
47#include "thread_utils.h"
48
49// Number of microseconds per milliseconds.
50#define US_PER_MSEC 1000
51
52// Number of nanoseconds in a second.
53#define NS_PER_SEC 1000000000ULL
54
55// Number of simultaneous dumping operations to perform.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080056#define NUM_THREADS 40
Christopher Ferris17e91d42013-10-21 13:30:52 -070057
58// Number of simultaneous threads running in our forked process.
59#define NUM_PTRACE_THREADS 5
60
Christopher Ferris46756822014-01-14 20:16:30 -080061struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070062 pid_t tid;
63 int32_t state;
64 pthread_t threadId;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070065 void* data;
Christopher Ferris46756822014-01-14 20:16:30 -080066};
Christopher Ferris17e91d42013-10-21 13:30:52 -070067
Christopher Ferris46756822014-01-14 20:16:30 -080068struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070069 thread_t thread;
Christopher Ferris20303f82014-01-10 16:33:16 -080070 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070071 int32_t* now;
72 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080073};
Christopher Ferris17e91d42013-10-21 13:30:52 -070074
75extern "C" {
76// Prototypes for functions in the test library.
77int test_level_one(int, int, int, int, void (*)(void*), void*);
78
79int test_recursive_call(int, void (*)(void*), void*);
80}
81
82uint64_t NanoTime() {
83 struct timespec t = { 0, 0 };
84 clock_gettime(CLOCK_MONOTONIC, &t);
85 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
86}
87
Christopher Ferris2c43cff2015-03-26 19:18:36 -070088std::string DumpFrames(Backtrace* backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -080089 if (backtrace->NumFrames() == 0) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070090 return " No frames to dump\n";
Christopher Ferris20303f82014-01-10 16:33:16 -080091 }
92
Christopher Ferris2c43cff2015-03-26 19:18:36 -070093 std::string frame;
Christopher Ferris20303f82014-01-10 16:33:16 -080094 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070095 frame += " " + backtrace->FormatFrameData(i) + '\n';
Christopher Ferris17e91d42013-10-21 13:30:52 -070096 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -070097 return frame;
Christopher Ferris17e91d42013-10-21 13:30:52 -070098}
99
100void WaitForStop(pid_t pid) {
101 uint64_t start = NanoTime();
102
103 siginfo_t si;
104 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
105 if ((NanoTime() - start) > NS_PER_SEC) {
106 printf("The process did not get to a stopping point in 1 second.\n");
107 break;
108 }
109 usleep(US_PER_MSEC);
110 }
111}
112
Christopher Ferris20303f82014-01-10 16:33:16 -0800113bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700114 // See if test_level_four is in the backtrace.
115 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800116 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
117 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700118 found = true;
119 break;
120 }
121 }
122
123 return found;
124}
125
Christopher Ferris20303f82014-01-10 16:33:16 -0800126void VerifyLevelDump(Backtrace* backtrace) {
127 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0));
128 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700129
130 // Look through the frames starting at the highest to find the
131 // frame we want.
132 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800133 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800134 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700135 frame_num = i;
136 break;
137 }
138 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700139 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
140 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700141
Christopher Ferris46756822014-01-14 20:16:30 -0800142 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one");
143 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two");
144 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three");
145 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700146}
147
148void VerifyLevelBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700149 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800150 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700151 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800152 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700153
Christopher Ferris20303f82014-01-10 16:33:16 -0800154 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700155}
156
Christopher Ferris20303f82014-01-10 16:33:16 -0800157bool ReadyMaxBacktrace(Backtrace* backtrace) {
158 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700159}
160
Christopher Ferris20303f82014-01-10 16:33:16 -0800161void VerifyMaxDump(Backtrace* backtrace) {
162 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700163 // Verify that the last frame is our recursive call.
Christopher Ferris46756822014-01-14 20:16:30 -0800164 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name,
165 "test_recursive_call");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700166}
167
168void VerifyMaxBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700169 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800170 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700171 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800172 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700173
Christopher Ferris20303f82014-01-10 16:33:16 -0800174 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700175}
176
177void ThreadSetState(void* data) {
178 thread_t* thread = reinterpret_cast<thread_t*>(data);
179 android_atomic_acquire_store(1, &thread->state);
180 volatile int i = 0;
181 while (thread->state) {
182 i++;
183 }
184}
185
Christopher Ferris20303f82014-01-10 16:33:16 -0800186void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700187 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
188 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800189 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700190
Christopher Ferris20303f82014-01-10 16:33:16 -0800191 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700192}
193
194bool WaitForNonZero(int32_t* value, uint64_t seconds) {
195 uint64_t start = NanoTime();
196 do {
197 if (android_atomic_acquire_load(value)) {
198 return true;
199 }
200 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
201 return false;
202}
203
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700204TEST(libbacktrace, local_no_unwind_frames) {
205 // Verify that a local unwind does not include any frames within
206 // libunwind or libbacktrace.
207 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
208 ASSERT_TRUE(backtrace->Unwind(0));
209
210 ASSERT_TRUE(backtrace->NumFrames() != 0);
211 for (const auto& frame : *backtrace ) {
212 if (BacktraceMap::IsValid(frame.map)) {
213 const std::string name = basename(frame.map.name.c_str());
214 ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
215 << DumpFrames(backtrace.get());
216 }
217 break;
218 }
219}
220
Christopher Ferris17e91d42013-10-21 13:30:52 -0700221TEST(libbacktrace, local_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700222 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700223}
224
225void VerifyIgnoreFrames(
Christopher Ferris20303f82014-01-10 16:33:16 -0800226 Backtrace* bt_all, Backtrace* bt_ign1,
227 Backtrace* bt_ign2, const char* cur_proc) {
228 EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1);
229 EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700230
231 // Check all of the frames are the same > the current frame.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700232 bool check = (cur_proc == nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800233 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700234 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800235 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
236 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
237 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700238
Christopher Ferris20303f82014-01-10 16:33:16 -0800239 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
240 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
241 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700242 }
Christopher Ferris46756822014-01-14 20:16:30 -0800243 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700244 check = true;
245 }
246 }
247}
248
249void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700250 std::unique_ptr<Backtrace> all(
Christopher Ferris20303f82014-01-10 16:33:16 -0800251 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700252 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700254
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700255 std::unique_ptr<Backtrace> ign1(
Christopher Ferris20303f82014-01-10 16:33:16 -0800256 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700257 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800258 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700259
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700260 std::unique_ptr<Backtrace> ign2(
Christopher Ferris20303f82014-01-10 16:33:16 -0800261 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700262 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800263 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700264
Christopher Ferris20303f82014-01-10 16:33:16 -0800265 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700266}
267
268TEST(libbacktrace, local_trace_ignore_frames) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700269 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700270}
271
272TEST(libbacktrace, local_max_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700273 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700274}
275
Christopher Ferrisdf290612014-01-22 19:21:07 -0800276void VerifyProcTest(pid_t pid, pid_t tid, bool share_map,
Christopher Ferris20303f82014-01-10 16:33:16 -0800277 bool (*ReadyFunc)(Backtrace*),
278 void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700279 pid_t ptrace_tid;
280 if (tid < 0) {
281 ptrace_tid = pid;
282 } else {
283 ptrace_tid = tid;
284 }
285 uint64_t start = NanoTime();
286 bool verified = false;
287 do {
288 usleep(US_PER_MSEC);
289 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
290 // Wait for the process to get to a stopping point.
291 WaitForStop(ptrace_tid);
292
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700293 std::unique_ptr<BacktraceMap> map;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800294 if (share_map) {
295 map.reset(BacktraceMap::Create(pid));
296 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700297 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris20303f82014-01-10 16:33:16 -0800298 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700299 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800300 if (ReadyFunc(backtrace.get())) {
301 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700302 verified = true;
303 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800304
Christopher Ferris17e91d42013-10-21 13:30:52 -0700305 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
306 }
307 // If 5 seconds have passed, then we are done.
308 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
309 ASSERT_TRUE(verified);
310}
311
312TEST(libbacktrace, ptrace_trace) {
313 pid_t pid;
314 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700315 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800316 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700317 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800318 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
319
320 kill(pid, SIGKILL);
321 int status;
322 ASSERT_EQ(waitpid(pid, &status, 0), pid);
323}
324
325TEST(libbacktrace, ptrace_trace_shared_map) {
326 pid_t pid;
327 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700328 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800329 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800330 }
331
332 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700333
334 kill(pid, SIGKILL);
335 int status;
336 ASSERT_EQ(waitpid(pid, &status, 0), pid);
337}
338
339TEST(libbacktrace, ptrace_max_trace) {
340 pid_t pid;
341 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700342 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800343 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700344 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800345 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700346
347 kill(pid, SIGKILL);
348 int status;
349 ASSERT_EQ(waitpid(pid, &status, 0), pid);
350}
351
Christopher Ferris20303f82014-01-10 16:33:16 -0800352void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700353 std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
354 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800355 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700356
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700357 std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
358 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800359 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700360
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700361 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700362}
363
364TEST(libbacktrace, ptrace_ignore_frames) {
365 pid_t pid;
366 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700367 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800368 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700369 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800370 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
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
377// Create a process with multiple threads and dump all of the threads.
378void* PtraceThreadLevelRun(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700379 EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
380 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700381}
382
383void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
384 // Get the list of tasks.
385 char task_path[128];
386 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
387
388 DIR* tasks_dir = opendir(task_path);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700389 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700390 struct dirent* entry;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700391 while ((entry = readdir(tasks_dir)) != nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700392 char* end;
393 pid_t tid = strtoul(entry->d_name, &end, 10);
394 if (*end == '\0') {
395 threads->push_back(tid);
396 }
397 }
398 closedir(tasks_dir);
399}
400
401TEST(libbacktrace, ptrace_threads) {
402 pid_t pid;
403 if ((pid = fork()) == 0) {
404 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
405 pthread_attr_t attr;
406 pthread_attr_init(&attr);
407 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
408
409 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700410 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700411 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700412 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800413 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700414 }
415
416 // Check to see that all of the threads are running before unwinding.
417 std::vector<pid_t> threads;
418 uint64_t start = NanoTime();
419 do {
420 usleep(US_PER_MSEC);
421 threads.clear();
422 GetThreads(pid, &threads);
423 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
424 ((NanoTime() - start) <= 5 * NS_PER_SEC));
425 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
426
427 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
428 WaitForStop(pid);
429 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
430 // Skip the current forked process, we only care about the threads.
431 if (pid == *it) {
432 continue;
433 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800434 VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700435 }
436 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
437
438 kill(pid, SIGKILL);
439 int status;
440 ASSERT_EQ(waitpid(pid, &status, 0), pid);
441}
442
443void VerifyLevelThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700444 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
445 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800446 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700447
Christopher Ferris20303f82014-01-10 16:33:16 -0800448 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700449}
450
451TEST(libbacktrace, thread_current_level) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700452 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700453}
454
455void VerifyMaxThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700456 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
457 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800458 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700459
Christopher Ferris20303f82014-01-10 16:33:16 -0800460 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700461}
462
463TEST(libbacktrace, thread_current_max) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700464 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700465}
466
467void* ThreadLevelRun(void* data) {
468 thread_t* thread = reinterpret_cast<thread_t*>(data);
469
470 thread->tid = gettid();
471 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700472 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700473}
474
475TEST(libbacktrace, thread_level_trace) {
476 pthread_attr_t attr;
477 pthread_attr_init(&attr);
478 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
479
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700480 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700481 pthread_t thread;
482 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
483
484 // Wait up to 2 seconds for the tid to be set.
485 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
486
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700487 // Make sure that the thread signal used is not visible when compiled for
488 // the target.
489#if !defined(__GLIBC__)
490 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
491#endif
492
Christopher Ferris17e91d42013-10-21 13:30:52 -0700493 // Save the current signal action and make sure it is restored afterwards.
494 struct sigaction cur_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700495 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700496
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700497 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
498 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800499 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700500
Christopher Ferris20303f82014-01-10 16:33:16 -0800501 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700502
503 // Tell the thread to exit its infinite loop.
504 android_atomic_acquire_store(0, &thread_data.state);
505
506 // Verify that the old action was restored.
507 struct sigaction new_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700508 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700509 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800510 // The SA_RESTORER flag gets set behind our back, so a direct comparison
511 // doesn't work unless we mask the value off. Mips doesn't have this
512 // flag, so skip this on that platform.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700513#if defined(SA_RESTORER)
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800514 cur_action.sa_flags &= ~SA_RESTORER;
515 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700516#elif defined(__GLIBC__)
517 // Our host compiler doesn't appear to define this flag for some reason.
518 cur_action.sa_flags &= ~0x04000000;
519 new_action.sa_flags &= ~0x04000000;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800520#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700521 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
522}
523
524TEST(libbacktrace, thread_ignore_frames) {
525 pthread_attr_t attr;
526 pthread_attr_init(&attr);
527 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
528
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700529 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700530 pthread_t thread;
531 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
532
533 // Wait up to 2 seconds for the tid to be set.
534 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
535
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700536 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
537 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800538 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700539
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700540 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
541 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800542 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700543
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700544 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
545 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800546 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700547
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700548 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700549
550 // Tell the thread to exit its infinite loop.
551 android_atomic_acquire_store(0, &thread_data.state);
552}
553
554void* ThreadMaxRun(void* data) {
555 thread_t* thread = reinterpret_cast<thread_t*>(data);
556
557 thread->tid = gettid();
558 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700559 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700560}
561
562TEST(libbacktrace, thread_max_trace) {
563 pthread_attr_t attr;
564 pthread_attr_init(&attr);
565 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
566
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700567 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700568 pthread_t thread;
569 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
570
571 // Wait for the tid to be set.
572 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
573
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700574 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
575 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800576 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700577
Christopher Ferris20303f82014-01-10 16:33:16 -0800578 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700579
580 // Tell the thread to exit its infinite loop.
581 android_atomic_acquire_store(0, &thread_data.state);
582}
583
584void* ThreadDump(void* data) {
585 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
586 while (true) {
587 if (android_atomic_acquire_load(dump->now)) {
588 break;
589 }
590 }
591
Christopher Ferris17e91d42013-10-21 13:30:52 -0700592 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800593 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
594 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700595
596 android_atomic_acquire_store(1, &dump->done);
597
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700598 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700599}
600
601TEST(libbacktrace, thread_multiple_dump) {
602 // Dump NUM_THREADS simultaneously.
603 std::vector<thread_t> runners(NUM_THREADS);
604 std::vector<dump_thread_t> dumpers(NUM_THREADS);
605
606 pthread_attr_t attr;
607 pthread_attr_init(&attr);
608 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
609 for (size_t i = 0; i < NUM_THREADS; i++) {
610 // Launch the runners, they will spin in hard loops doing nothing.
611 runners[i].tid = 0;
612 runners[i].state = 0;
613 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
614 }
615
616 // Wait for tids to be set.
617 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800618 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700619 }
620
621 // Start all of the dumpers at once, they will spin until they are signalled
622 // to begin their dump run.
623 int32_t dump_now = 0;
624 for (size_t i = 0; i < NUM_THREADS; i++) {
625 dumpers[i].thread.tid = runners[i].tid;
626 dumpers[i].thread.state = 0;
627 dumpers[i].done = 0;
628 dumpers[i].now = &dump_now;
629
630 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
631 }
632
633 // Start all of the dumpers going at once.
634 android_atomic_acquire_store(1, &dump_now);
635
636 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800637 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700638
639 // Tell the runner thread to exit its infinite loop.
640 android_atomic_acquire_store(0, &runners[i].state);
641
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700642 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800643 VerifyMaxDump(dumpers[i].backtrace);
644
645 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700646 dumpers[i].backtrace = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700647 }
648}
649
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700650TEST(libbacktrace, thread_multiple_dump_same_thread) {
651 pthread_attr_t attr;
652 pthread_attr_init(&attr);
653 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
654 thread_t runner;
655 runner.tid = 0;
656 runner.state = 0;
657 ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
658
659 // Wait for tids to be set.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800660 ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700661
662 // Start all of the dumpers at once, they will spin until they are signalled
663 // to begin their dump run.
664 int32_t dump_now = 0;
665 // Dump the same thread NUM_THREADS simultaneously.
666 std::vector<dump_thread_t> dumpers(NUM_THREADS);
667 for (size_t i = 0; i < NUM_THREADS; i++) {
668 dumpers[i].thread.tid = runner.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 Ferrisa2efd3a2014-05-06 15:23:59 -0700681
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700682 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700683 VerifyMaxDump(dumpers[i].backtrace);
684
685 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700686 dumpers[i].backtrace = nullptr;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700687 }
688
689 // Tell the runner thread to exit its infinite loop.
690 android_atomic_acquire_store(0, &runner.state);
691}
692
Christopher Ferrisdf290612014-01-22 19:21:07 -0800693// This test is for UnwindMaps that should share the same map cursor when
694// multiple maps are created for the current process at the same time.
695TEST(libbacktrace, simultaneous_maps) {
696 BacktraceMap* map1 = BacktraceMap::Create(getpid());
697 BacktraceMap* map2 = BacktraceMap::Create(getpid());
698 BacktraceMap* map3 = BacktraceMap::Create(getpid());
699
700 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
701 EXPECT_TRUE(back1->Unwind(0));
702 delete back1;
703 delete map1;
704
705 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
706 EXPECT_TRUE(back2->Unwind(0));
707 delete back2;
708 delete map2;
709
710 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
711 EXPECT_TRUE(back3->Unwind(0));
712 delete back3;
713 delete map3;
714}
715
Christopher Ferris12385e32015-02-06 13:22:01 -0800716TEST(libbacktrace, fillin_erases) {
717 BacktraceMap* back_map = BacktraceMap::Create(getpid());
718
719 backtrace_map_t map;
720
721 map.start = 1;
722 map.end = 3;
723 map.flags = 1;
724 map.name = "Initialized";
725 back_map->FillIn(0, &map);
726 delete back_map;
727
728 ASSERT_FALSE(BacktraceMap::IsValid(map));
729 ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
730 ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
731 ASSERT_EQ(0, map.flags);
732 ASSERT_EQ("", map.name);
733}
734
Christopher Ferris17e91d42013-10-21 13:30:52 -0700735TEST(libbacktrace, format_test) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700736 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
737 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700738
Christopher Ferris20303f82014-01-10 16:33:16 -0800739 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800740 frame.num = 1;
741 frame.pc = 2;
742 frame.sp = 0;
743 frame.stack_size = 0;
Christopher Ferris46756822014-01-14 20:16:30 -0800744 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700745
Christopher Ferris46756822014-01-14 20:16:30 -0800746 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800747 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700748#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800749 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700750#else
Christopher Ferris46756822014-01-14 20:16:30 -0800751 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700752#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800753 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700754
Christopher Ferris46756822014-01-14 20:16:30 -0800755 // Check map name empty, but exists.
Christopher Ferris12385e32015-02-06 13:22:01 -0800756 frame.map.start = 1;
757 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700758#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800759 EXPECT_EQ("#01 pc 0000000000000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700760#else
Christopher Ferris46756822014-01-14 20:16:30 -0800761 EXPECT_EQ("#01 pc 00000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700762#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800763 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700764
Christopher Ferris46756822014-01-14 20:16:30 -0800765
766 // Check relative pc is set and map name is set.
767 frame.pc = 0x12345679;
Christopher Ferris12385e32015-02-06 13:22:01 -0800768 frame.map.name = "MapFake";
769 frame.map.start = 1;
770 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700771#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800772 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700773#else
Christopher Ferris46756822014-01-14 20:16:30 -0800774 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700775#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800776 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700777
Christopher Ferris46756822014-01-14 20:16:30 -0800778 // Check func_name is set, but no func offset.
779 frame.func_name = "ProcFake";
780#if defined(__LP64__)
781 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
782#else
783 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
784#endif
785 backtrace->FormatFrameData(&frame));
786
787 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800788 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700789#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800790 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700791#else
Christopher Ferris46756822014-01-14 20:16:30 -0800792 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700793#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800794 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700795}
Christopher Ferrise2960912014-03-07 19:42:19 -0800796
797struct map_test_t {
798 uintptr_t start;
799 uintptr_t end;
800};
801
802bool map_sort(map_test_t i, map_test_t j) {
803 return i.start < j.start;
804}
805
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700806void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800807 char buffer[4096];
808 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
809
810 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700811 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800812 std::vector<map_test_t> test_maps;
813 while (fgets(buffer, sizeof(buffer), map_file)) {
814 map_test_t map;
815 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
816 test_maps.push_back(map);
817 }
818 fclose(map_file);
819 std::sort(test_maps.begin(), test_maps.end(), map_sort);
820
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700821 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800822
823 // Basic test that verifies that the map is in the expected order.
824 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
825 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
826 ASSERT_TRUE(test_it != test_maps.end());
827 ASSERT_EQ(test_it->start, it->start);
828 ASSERT_EQ(test_it->end, it->end);
829 ++test_it;
830 }
831 ASSERT_TRUE(test_it == test_maps.end());
832}
833
834TEST(libbacktrace, verify_map_remote) {
835 pid_t pid;
836
837 if ((pid = fork()) == 0) {
838 while (true) {
839 }
840 _exit(0);
841 }
842 ASSERT_LT(0, pid);
843
844 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
845
846 // Wait for the process to get to a stopping point.
847 WaitForStop(pid);
848
849 // The maps should match exactly since the forked process has been paused.
850 VerifyMap(pid);
851
852 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
853
854 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700855 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
856}
857
858void* ThreadReadTest(void* data) {
859 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
860
861 thread_data->tid = gettid();
862
863 // Create two map pages.
864 // Mark the second page as not-readable.
865 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
866 uint8_t* memory;
867 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
868 return reinterpret_cast<void*>(-1);
869 }
870
871 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
872 return reinterpret_cast<void*>(-1);
873 }
874
875 // Set up a simple pattern in memory.
876 for (size_t i = 0; i < pagesize; i++) {
877 memory[i] = i;
878 }
879
880 thread_data->data = memory;
881
882 // Tell the caller it's okay to start reading memory.
883 android_atomic_acquire_store(1, &thread_data->state);
884
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700885 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700886 while (thread_data->state) {
887 }
888
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700889 // Re-enable read-write on the page so that we don't crash if we try
890 // and access data on this page when freeing the memory.
891 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
892 return reinterpret_cast<void*>(-1);
893 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700894 free(memory);
895
896 android_atomic_acquire_store(1, &thread_data->state);
897
898 return nullptr;
899}
900
901void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
902 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
903
904 // Create a page of data to use to do quick compares.
905 uint8_t* expected = new uint8_t[pagesize];
906 for (size_t i = 0; i < pagesize; i++) {
907 expected[i] = i;
908 }
909 uint8_t* data = new uint8_t[2*pagesize];
910 // Verify that we can only read one page worth of data.
911 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
912 ASSERT_EQ(pagesize, bytes_read);
913 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
914
915 // Verify unaligned reads.
916 for (size_t i = 1; i < sizeof(word_t); i++) {
917 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
918 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
919 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
920 << "Offset at " << i << " failed";
921 }
922 delete data;
923 delete expected;
924}
925
926TEST(libbacktrace, thread_read) {
927 pthread_attr_t attr;
928 pthread_attr_init(&attr);
929 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
930 pthread_t thread;
931 thread_t thread_data = { 0, 0, 0, nullptr };
932 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
933
934 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
935
936 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
937 ASSERT_TRUE(backtrace.get() != nullptr);
938
939 RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
940
941 android_atomic_acquire_store(0, &thread_data.state);
942
943 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
944}
945
946volatile uintptr_t g_ready = 0;
947volatile uintptr_t g_addr = 0;
948
949void ForkedReadTest() {
950 // Create two map pages.
951 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
952 uint8_t* memory;
953 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
954 perror("Failed to allocate memory\n");
955 exit(1);
956 }
957
958 // Mark the second page as not-readable.
959 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
960 perror("Failed to mprotect memory\n");
961 exit(1);
962 }
963
964 // Set up a simple pattern in memory.
965 for (size_t i = 0; i < pagesize; i++) {
966 memory[i] = i;
967 }
968
969 g_addr = reinterpret_cast<uintptr_t>(memory);
970 g_ready = 1;
971
972 while (1) {
973 usleep(US_PER_MSEC);
974 }
975}
976
977TEST(libbacktrace, process_read) {
978 pid_t pid;
979 if ((pid = fork()) == 0) {
980 ForkedReadTest();
981 exit(0);
982 }
983 ASSERT_NE(-1, pid);
984
985 bool test_executed = false;
986 uint64_t start = NanoTime();
987 while (1) {
988 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
989 WaitForStop(pid);
990
991 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
992
993 uintptr_t read_addr;
994 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
995 reinterpret_cast<uint8_t*>(&read_addr),
996 sizeof(uintptr_t));
997 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
998 if (read_addr) {
999 // The forked process is ready to be read.
1000 bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
1001 reinterpret_cast<uint8_t*>(&read_addr),
1002 sizeof(uintptr_t));
1003 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1004
1005 RunReadTest(backtrace.get(), read_addr);
1006
1007 test_executed = true;
1008 break;
1009 }
1010 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1011 }
1012 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1013 break;
1014 }
1015 usleep(US_PER_MSEC);
1016 }
1017 kill(pid, SIGKILL);
1018 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1019
1020 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001021}
1022
1023#if defined(ENABLE_PSS_TESTS)
1024#include "GetPss.h"
1025
1026#define MAX_LEAK_BYTES 32*1024UL
1027
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001028void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001029 // Do a few runs to get the PSS stable.
1030 for (size_t i = 0; i < 100; i++) {
1031 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001032 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001033 ASSERT_TRUE(backtrace->Unwind(0));
1034 delete backtrace;
1035 }
1036 size_t stable_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001037 ASSERT_TRUE(stable_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001038
1039 // Loop enough that even a small leak should be detectable.
1040 for (size_t i = 0; i < 4096; i++) {
1041 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001042 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001043 ASSERT_TRUE(backtrace->Unwind(0));
1044 delete backtrace;
1045 }
1046 size_t new_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001047 ASSERT_TRUE(new_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001048 size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
1049 // As long as the new pss is within a certain amount, consider everything okay.
1050 ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
1051}
1052
1053TEST(libbacktrace, check_for_leak_local) {
1054 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1055}
1056
1057TEST(libbacktrace, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001058 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001059 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001060 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001061
1062 // Wait up to 2 seconds for the tid to be set.
1063 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1064
1065 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1066
1067 // Tell the thread to exit its infinite loop.
1068 android_atomic_acquire_store(0, &thread_data.state);
1069
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001070 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001071}
1072
1073TEST(libbacktrace, check_for_leak_remote) {
1074 pid_t pid;
1075
1076 if ((pid = fork()) == 0) {
1077 while (true) {
1078 }
1079 _exit(0);
1080 }
1081 ASSERT_LT(0, pid);
1082
1083 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1084
1085 // Wait for the process to get to a stopping point.
1086 WaitForStop(pid);
1087
1088 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1089
1090 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1091
1092 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001093 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001094}
1095#endif