blob: 4af65923edcb7039afc98ac4b7ba92d85e2813f5 [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 Ferris97e00bb2015-04-02 14:22:31 -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) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700127 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0))
128 << DumpFrames(backtrace);
129 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
130 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700131
132 // Look through the frames starting at the highest to find the
133 // frame we want.
134 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800135 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800136 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700137 frame_num = i;
138 break;
139 }
140 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700141 ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace);
142 ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700143
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700144 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one")
145 << DumpFrames(backtrace);
146 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two")
147 << DumpFrames(backtrace);
148 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three")
149 << DumpFrames(backtrace);
150 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four")
151 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700152}
153
154void VerifyLevelBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700155 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800156 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700157 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800158 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700159
Christopher Ferris20303f82014-01-10 16:33:16 -0800160 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700161}
162
Christopher Ferris20303f82014-01-10 16:33:16 -0800163bool ReadyMaxBacktrace(Backtrace* backtrace) {
164 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700165}
166
Christopher Ferris20303f82014-01-10 16:33:16 -0800167void VerifyMaxDump(Backtrace* backtrace) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700168 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES))
169 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700170 // Verify that the last frame is our recursive call.
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700171 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call")
172 << DumpFrames(backtrace);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700173}
174
175void VerifyMaxBacktrace(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700176 std::unique_ptr<Backtrace> backtrace(
Christopher Ferris20303f82014-01-10 16:33:16 -0800177 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700178 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800179 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700180
Christopher Ferris20303f82014-01-10 16:33:16 -0800181 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700182}
183
184void ThreadSetState(void* data) {
185 thread_t* thread = reinterpret_cast<thread_t*>(data);
186 android_atomic_acquire_store(1, &thread->state);
187 volatile int i = 0;
188 while (thread->state) {
189 i++;
190 }
191}
192
Christopher Ferris20303f82014-01-10 16:33:16 -0800193void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700194 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
195 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800196 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700197
Christopher Ferris20303f82014-01-10 16:33:16 -0800198 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700199}
200
201bool WaitForNonZero(int32_t* value, uint64_t seconds) {
202 uint64_t start = NanoTime();
203 do {
204 if (android_atomic_acquire_load(value)) {
205 return true;
206 }
207 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
208 return false;
209}
210
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700211TEST(libbacktrace, local_no_unwind_frames) {
212 // Verify that a local unwind does not include any frames within
213 // libunwind or libbacktrace.
214 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700215 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700216 ASSERT_TRUE(backtrace->Unwind(0));
217
218 ASSERT_TRUE(backtrace->NumFrames() != 0);
219 for (const auto& frame : *backtrace ) {
220 if (BacktraceMap::IsValid(frame.map)) {
221 const std::string name = basename(frame.map.name.c_str());
222 ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
223 << DumpFrames(backtrace.get());
224 }
225 break;
226 }
227}
228
Christopher Ferris17e91d42013-10-21 13:30:52 -0700229TEST(libbacktrace, local_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700230 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700231}
232
233void VerifyIgnoreFrames(
Christopher Ferris20303f82014-01-10 16:33:16 -0800234 Backtrace* bt_all, Backtrace* bt_ign1,
235 Backtrace* bt_ign2, const char* cur_proc) {
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700236 EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1)
237 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1);
238 EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2)
239 << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 2 backtrace:\n" << DumpFrames(bt_ign2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700240
241 // Check all of the frames are the same > the current frame.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700242 bool check = (cur_proc == nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800243 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700244 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800245 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
246 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
247 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700248
Christopher Ferris20303f82014-01-10 16:33:16 -0800249 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
250 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
251 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700252 }
Christopher Ferris46756822014-01-14 20:16:30 -0800253 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700254 check = true;
255 }
256 }
257}
258
259void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700260 std::unique_ptr<Backtrace> all(
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(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800263 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700264
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700265 std::unique_ptr<Backtrace> ign1(
Christopher Ferris20303f82014-01-10 16:33:16 -0800266 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700267 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800268 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700269
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700270 std::unique_ptr<Backtrace> ign2(
Christopher Ferris20303f82014-01-10 16:33:16 -0800271 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700272 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800273 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700274
Christopher Ferris20303f82014-01-10 16:33:16 -0800275 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700276}
277
278TEST(libbacktrace, local_trace_ignore_frames) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700279 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700280}
281
282TEST(libbacktrace, local_max_trace) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700283 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700284}
285
Christopher Ferrisdf290612014-01-22 19:21:07 -0800286void VerifyProcTest(pid_t pid, pid_t tid, bool share_map,
Christopher Ferris20303f82014-01-10 16:33:16 -0800287 bool (*ReadyFunc)(Backtrace*),
288 void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700289 pid_t ptrace_tid;
290 if (tid < 0) {
291 ptrace_tid = pid;
292 } else {
293 ptrace_tid = tid;
294 }
295 uint64_t start = NanoTime();
296 bool verified = false;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700297 std::string last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700298 do {
299 usleep(US_PER_MSEC);
300 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
301 // Wait for the process to get to a stopping point.
302 WaitForStop(ptrace_tid);
303
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700304 std::unique_ptr<BacktraceMap> map;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800305 if (share_map) {
306 map.reset(BacktraceMap::Create(pid));
307 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700308 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700309 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700310 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris20303f82014-01-10 16:33:16 -0800311 if (ReadyFunc(backtrace.get())) {
312 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700313 verified = true;
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700314 } else {
315 last_dump = DumpFrames(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700316 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800317
Christopher Ferris17e91d42013-10-21 13:30:52 -0700318 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
319 }
320 // If 5 seconds have passed, then we are done.
321 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700322 ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700323}
324
325TEST(libbacktrace, ptrace_trace) {
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 Ferris17e91d42013-10-21 13:30:52 -0700330 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800331 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
332
333 kill(pid, SIGKILL);
334 int status;
335 ASSERT_EQ(waitpid(pid, &status, 0), pid);
336}
337
338TEST(libbacktrace, ptrace_trace_shared_map) {
339 pid_t pid;
340 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700341 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800342 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800343 }
344
345 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
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
352TEST(libbacktrace, ptrace_max_trace) {
353 pid_t pid;
354 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700355 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800356 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700357 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800358 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700359
360 kill(pid, SIGKILL);
361 int status;
362 ASSERT_EQ(waitpid(pid, &status, 0), pid);
363}
364
Christopher Ferris20303f82014-01-10 16:33:16 -0800365void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700366 std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
367 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800368 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700369
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700370 std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
371 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800372 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700373
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700374 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700375}
376
377TEST(libbacktrace, ptrace_ignore_frames) {
378 pid_t pid;
379 if ((pid = fork()) == 0) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700380 ASSERT_NE(test_level_one(1, 2, 3, 4, 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, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
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
390// Create a process with multiple threads and dump all of the threads.
391void* PtraceThreadLevelRun(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700392 EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
393 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700394}
395
396void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
397 // Get the list of tasks.
398 char task_path[128];
399 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
400
401 DIR* tasks_dir = opendir(task_path);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700402 ASSERT_TRUE(tasks_dir != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700403 struct dirent* entry;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700404 while ((entry = readdir(tasks_dir)) != nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700405 char* end;
406 pid_t tid = strtoul(entry->d_name, &end, 10);
407 if (*end == '\0') {
408 threads->push_back(tid);
409 }
410 }
411 closedir(tasks_dir);
412}
413
414TEST(libbacktrace, ptrace_threads) {
415 pid_t pid;
416 if ((pid = fork()) == 0) {
417 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
418 pthread_attr_t attr;
419 pthread_attr_init(&attr);
420 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
421
422 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700423 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700424 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700425 ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800426 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700427 }
428
429 // Check to see that all of the threads are running before unwinding.
430 std::vector<pid_t> threads;
431 uint64_t start = NanoTime();
432 do {
433 usleep(US_PER_MSEC);
434 threads.clear();
435 GetThreads(pid, &threads);
436 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
437 ((NanoTime() - start) <= 5 * NS_PER_SEC));
438 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
439
440 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
441 WaitForStop(pid);
442 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
443 // Skip the current forked process, we only care about the threads.
444 if (pid == *it) {
445 continue;
446 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800447 VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700448 }
449 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
450
451 kill(pid, SIGKILL);
452 int status;
453 ASSERT_EQ(waitpid(pid, &status, 0), pid);
454}
455
456void VerifyLevelThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700457 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
458 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800459 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700460
Christopher Ferris20303f82014-01-10 16:33:16 -0800461 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700462}
463
464TEST(libbacktrace, thread_current_level) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700465 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700466}
467
468void VerifyMaxThread(void*) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700469 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
470 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700472
Christopher Ferris20303f82014-01-10 16:33:16 -0800473 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700474}
475
476TEST(libbacktrace, thread_current_max) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700477 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700478}
479
480void* ThreadLevelRun(void* data) {
481 thread_t* thread = reinterpret_cast<thread_t*>(data);
482
483 thread->tid = gettid();
484 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700485 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700486}
487
488TEST(libbacktrace, thread_level_trace) {
489 pthread_attr_t attr;
490 pthread_attr_init(&attr);
491 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
492
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700493 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700494 pthread_t thread;
495 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
496
497 // Wait up to 2 seconds for the tid to be set.
498 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
499
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700500 // Make sure that the thread signal used is not visible when compiled for
501 // the target.
502#if !defined(__GLIBC__)
503 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
504#endif
505
Christopher Ferris17e91d42013-10-21 13:30:52 -0700506 // Save the current signal action and make sure it is restored afterwards.
507 struct sigaction cur_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700508 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700509
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700510 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
511 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800512 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700513
Christopher Ferris20303f82014-01-10 16:33:16 -0800514 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700515
516 // Tell the thread to exit its infinite loop.
517 android_atomic_acquire_store(0, &thread_data.state);
518
519 // Verify that the old action was restored.
520 struct sigaction new_action;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700521 ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700522 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800523 // The SA_RESTORER flag gets set behind our back, so a direct comparison
524 // doesn't work unless we mask the value off. Mips doesn't have this
525 // flag, so skip this on that platform.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700526#if defined(SA_RESTORER)
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800527 cur_action.sa_flags &= ~SA_RESTORER;
528 new_action.sa_flags &= ~SA_RESTORER;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700529#elif defined(__GLIBC__)
530 // Our host compiler doesn't appear to define this flag for some reason.
531 cur_action.sa_flags &= ~0x04000000;
532 new_action.sa_flags &= ~0x04000000;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800533#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -0700534 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
535}
536
537TEST(libbacktrace, thread_ignore_frames) {
538 pthread_attr_t attr;
539 pthread_attr_init(&attr);
540 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
541
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700542 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700543 pthread_t thread;
544 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
545
546 // Wait up to 2 seconds for the tid to be set.
547 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
548
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700549 std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
550 ASSERT_TRUE(all.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800551 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700552
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700553 std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
554 ASSERT_TRUE(ign1.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800555 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700556
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700557 std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
558 ASSERT_TRUE(ign2.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800559 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700560
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700561 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700562
563 // Tell the thread to exit its infinite loop.
564 android_atomic_acquire_store(0, &thread_data.state);
565}
566
567void* ThreadMaxRun(void* data) {
568 thread_t* thread = reinterpret_cast<thread_t*>(data);
569
570 thread->tid = gettid();
571 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700572 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700573}
574
575TEST(libbacktrace, thread_max_trace) {
576 pthread_attr_t attr;
577 pthread_attr_init(&attr);
578 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
579
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700580 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferris17e91d42013-10-21 13:30:52 -0700581 pthread_t thread;
582 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
583
584 // Wait for the tid to be set.
585 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
586
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700587 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
588 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800589 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700590
Christopher Ferris20303f82014-01-10 16:33:16 -0800591 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700592
593 // Tell the thread to exit its infinite loop.
594 android_atomic_acquire_store(0, &thread_data.state);
595}
596
597void* ThreadDump(void* data) {
598 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
599 while (true) {
600 if (android_atomic_acquire_load(dump->now)) {
601 break;
602 }
603 }
604
Christopher Ferris17e91d42013-10-21 13:30:52 -0700605 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800606 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
607 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700608
609 android_atomic_acquire_store(1, &dump->done);
610
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700611 return nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700612}
613
614TEST(libbacktrace, thread_multiple_dump) {
615 // Dump NUM_THREADS simultaneously.
616 std::vector<thread_t> runners(NUM_THREADS);
617 std::vector<dump_thread_t> dumpers(NUM_THREADS);
618
619 pthread_attr_t attr;
620 pthread_attr_init(&attr);
621 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
622 for (size_t i = 0; i < NUM_THREADS; i++) {
623 // Launch the runners, they will spin in hard loops doing nothing.
624 runners[i].tid = 0;
625 runners[i].state = 0;
626 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
627 }
628
629 // Wait for tids to be set.
630 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800631 ASSERT_TRUE(WaitForNonZero(&it->state, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700632 }
633
634 // Start all of the dumpers at once, they will spin until they are signalled
635 // to begin their dump run.
636 int32_t dump_now = 0;
637 for (size_t i = 0; i < NUM_THREADS; i++) {
638 dumpers[i].thread.tid = runners[i].tid;
639 dumpers[i].thread.state = 0;
640 dumpers[i].done = 0;
641 dumpers[i].now = &dump_now;
642
643 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
644 }
645
646 // Start all of the dumpers going at once.
647 android_atomic_acquire_store(1, &dump_now);
648
649 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800650 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700651
652 // Tell the runner thread to exit its infinite loop.
653 android_atomic_acquire_store(0, &runners[i].state);
654
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700655 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800656 VerifyMaxDump(dumpers[i].backtrace);
657
658 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700659 dumpers[i].backtrace = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700660 }
661}
662
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700663TEST(libbacktrace, thread_multiple_dump_same_thread) {
664 pthread_attr_t attr;
665 pthread_attr_init(&attr);
666 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
667 thread_t runner;
668 runner.tid = 0;
669 runner.state = 0;
670 ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
671
672 // Wait for tids to be set.
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800673 ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700674
675 // Start all of the dumpers at once, they will spin until they are signalled
676 // to begin their dump run.
677 int32_t dump_now = 0;
678 // Dump the same thread NUM_THREADS simultaneously.
679 std::vector<dump_thread_t> dumpers(NUM_THREADS);
680 for (size_t i = 0; i < NUM_THREADS; i++) {
681 dumpers[i].thread.tid = runner.tid;
682 dumpers[i].thread.state = 0;
683 dumpers[i].done = 0;
684 dumpers[i].now = &dump_now;
685
686 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
687 }
688
689 // Start all of the dumpers going at once.
690 android_atomic_acquire_store(1, &dump_now);
691
692 for (size_t i = 0; i < NUM_THREADS; i++) {
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -0800693 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700694
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700695 ASSERT_TRUE(dumpers[i].backtrace != nullptr);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700696 VerifyMaxDump(dumpers[i].backtrace);
697
698 delete dumpers[i].backtrace;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700699 dumpers[i].backtrace = nullptr;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700700 }
701
702 // Tell the runner thread to exit its infinite loop.
703 android_atomic_acquire_store(0, &runner.state);
704}
705
Christopher Ferrisdf290612014-01-22 19:21:07 -0800706// This test is for UnwindMaps that should share the same map cursor when
707// multiple maps are created for the current process at the same time.
708TEST(libbacktrace, simultaneous_maps) {
709 BacktraceMap* map1 = BacktraceMap::Create(getpid());
710 BacktraceMap* map2 = BacktraceMap::Create(getpid());
711 BacktraceMap* map3 = BacktraceMap::Create(getpid());
712
713 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700714 ASSERT_TRUE(back1 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800715 EXPECT_TRUE(back1->Unwind(0));
716 delete back1;
717 delete map1;
718
719 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700720 ASSERT_TRUE(back2 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800721 EXPECT_TRUE(back2->Unwind(0));
722 delete back2;
723 delete map2;
724
725 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
Christopher Ferris97e00bb2015-04-02 14:22:31 -0700726 ASSERT_TRUE(back3 != nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800727 EXPECT_TRUE(back3->Unwind(0));
728 delete back3;
729 delete map3;
730}
731
Christopher Ferris12385e32015-02-06 13:22:01 -0800732TEST(libbacktrace, fillin_erases) {
733 BacktraceMap* back_map = BacktraceMap::Create(getpid());
734
735 backtrace_map_t map;
736
737 map.start = 1;
738 map.end = 3;
739 map.flags = 1;
740 map.name = "Initialized";
741 back_map->FillIn(0, &map);
742 delete back_map;
743
744 ASSERT_FALSE(BacktraceMap::IsValid(map));
745 ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
746 ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
747 ASSERT_EQ(0, map.flags);
748 ASSERT_EQ("", map.name);
749}
750
Christopher Ferris17e91d42013-10-21 13:30:52 -0700751TEST(libbacktrace, format_test) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700752 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
753 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700754
Christopher Ferris20303f82014-01-10 16:33:16 -0800755 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800756 frame.num = 1;
757 frame.pc = 2;
758 frame.sp = 0;
759 frame.stack_size = 0;
Christopher Ferris46756822014-01-14 20:16:30 -0800760 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700761
Christopher Ferris46756822014-01-14 20:16:30 -0800762 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800763 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700764#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800765 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700766#else
Christopher Ferris46756822014-01-14 20:16:30 -0800767 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700768#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800769 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700770
Christopher Ferris46756822014-01-14 20:16:30 -0800771 // Check map name empty, but exists.
Christopher Ferris12385e32015-02-06 13:22:01 -0800772 frame.map.start = 1;
773 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700774#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800775 EXPECT_EQ("#01 pc 0000000000000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700776#else
Christopher Ferris46756822014-01-14 20:16:30 -0800777 EXPECT_EQ("#01 pc 00000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700778#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800779 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700780
Christopher Ferris46756822014-01-14 20:16:30 -0800781
782 // Check relative pc is set and map name is set.
783 frame.pc = 0x12345679;
Christopher Ferris12385e32015-02-06 13:22:01 -0800784 frame.map.name = "MapFake";
785 frame.map.start = 1;
786 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700787#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800788 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700789#else
Christopher Ferris46756822014-01-14 20:16:30 -0800790 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700791#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800792 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700793
Christopher Ferris46756822014-01-14 20:16:30 -0800794 // Check func_name is set, but no func offset.
795 frame.func_name = "ProcFake";
796#if defined(__LP64__)
797 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
798#else
799 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
800#endif
801 backtrace->FormatFrameData(&frame));
802
803 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800804 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700805#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800806 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700807#else
Christopher Ferris46756822014-01-14 20:16:30 -0800808 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700809#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800810 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700811}
Christopher Ferrise2960912014-03-07 19:42:19 -0800812
813struct map_test_t {
814 uintptr_t start;
815 uintptr_t end;
816};
817
818bool map_sort(map_test_t i, map_test_t j) {
819 return i.start < j.start;
820}
821
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700822void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800823 char buffer[4096];
824 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
825
826 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700827 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800828 std::vector<map_test_t> test_maps;
829 while (fgets(buffer, sizeof(buffer), map_file)) {
830 map_test_t map;
831 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
832 test_maps.push_back(map);
833 }
834 fclose(map_file);
835 std::sort(test_maps.begin(), test_maps.end(), map_sort);
836
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700837 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800838
839 // Basic test that verifies that the map is in the expected order.
840 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
841 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
842 ASSERT_TRUE(test_it != test_maps.end());
843 ASSERT_EQ(test_it->start, it->start);
844 ASSERT_EQ(test_it->end, it->end);
845 ++test_it;
846 }
847 ASSERT_TRUE(test_it == test_maps.end());
848}
849
850TEST(libbacktrace, verify_map_remote) {
851 pid_t pid;
852
853 if ((pid = fork()) == 0) {
854 while (true) {
855 }
856 _exit(0);
857 }
858 ASSERT_LT(0, pid);
859
860 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
861
862 // Wait for the process to get to a stopping point.
863 WaitForStop(pid);
864
865 // The maps should match exactly since the forked process has been paused.
866 VerifyMap(pid);
867
868 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
869
870 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700871 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
872}
873
874void* ThreadReadTest(void* data) {
875 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
876
877 thread_data->tid = gettid();
878
879 // Create two map pages.
880 // Mark the second page as not-readable.
881 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
882 uint8_t* memory;
883 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
884 return reinterpret_cast<void*>(-1);
885 }
886
887 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
888 return reinterpret_cast<void*>(-1);
889 }
890
891 // Set up a simple pattern in memory.
892 for (size_t i = 0; i < pagesize; i++) {
893 memory[i] = i;
894 }
895
896 thread_data->data = memory;
897
898 // Tell the caller it's okay to start reading memory.
899 android_atomic_acquire_store(1, &thread_data->state);
900
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700901 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700902 while (thread_data->state) {
903 }
904
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700905 // Re-enable read-write on the page so that we don't crash if we try
906 // and access data on this page when freeing the memory.
907 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
908 return reinterpret_cast<void*>(-1);
909 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700910 free(memory);
911
912 android_atomic_acquire_store(1, &thread_data->state);
913
914 return nullptr;
915}
916
917void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
918 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
919
920 // Create a page of data to use to do quick compares.
921 uint8_t* expected = new uint8_t[pagesize];
922 for (size_t i = 0; i < pagesize; i++) {
923 expected[i] = i;
924 }
925 uint8_t* data = new uint8_t[2*pagesize];
926 // Verify that we can only read one page worth of data.
927 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
928 ASSERT_EQ(pagesize, bytes_read);
929 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
930
931 // Verify unaligned reads.
932 for (size_t i = 1; i < sizeof(word_t); i++) {
933 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
934 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
935 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
936 << "Offset at " << i << " failed";
937 }
938 delete data;
939 delete expected;
940}
941
942TEST(libbacktrace, thread_read) {
943 pthread_attr_t attr;
944 pthread_attr_init(&attr);
945 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
946 pthread_t thread;
947 thread_t thread_data = { 0, 0, 0, nullptr };
948 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
949
950 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
951
952 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
953 ASSERT_TRUE(backtrace.get() != nullptr);
954
955 RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
956
957 android_atomic_acquire_store(0, &thread_data.state);
958
959 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
960}
961
962volatile uintptr_t g_ready = 0;
963volatile uintptr_t g_addr = 0;
964
965void ForkedReadTest() {
966 // Create two map pages.
967 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
968 uint8_t* memory;
969 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
970 perror("Failed to allocate memory\n");
971 exit(1);
972 }
973
974 // Mark the second page as not-readable.
975 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
976 perror("Failed to mprotect memory\n");
977 exit(1);
978 }
979
980 // Set up a simple pattern in memory.
981 for (size_t i = 0; i < pagesize; i++) {
982 memory[i] = i;
983 }
984
985 g_addr = reinterpret_cast<uintptr_t>(memory);
986 g_ready = 1;
987
988 while (1) {
989 usleep(US_PER_MSEC);
990 }
991}
992
993TEST(libbacktrace, process_read) {
994 pid_t pid;
995 if ((pid = fork()) == 0) {
996 ForkedReadTest();
997 exit(0);
998 }
999 ASSERT_NE(-1, pid);
1000
1001 bool test_executed = false;
1002 uint64_t start = NanoTime();
1003 while (1) {
1004 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
1005 WaitForStop(pid);
1006
1007 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris97e00bb2015-04-02 14:22:31 -07001008 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001009
1010 uintptr_t read_addr;
1011 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
1012 reinterpret_cast<uint8_t*>(&read_addr),
1013 sizeof(uintptr_t));
1014 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1015 if (read_addr) {
1016 // The forked process is ready to be read.
1017 bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
1018 reinterpret_cast<uint8_t*>(&read_addr),
1019 sizeof(uintptr_t));
1020 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1021
1022 RunReadTest(backtrace.get(), read_addr);
1023
1024 test_executed = true;
1025 break;
1026 }
1027 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1028 }
1029 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1030 break;
1031 }
1032 usleep(US_PER_MSEC);
1033 }
1034 kill(pid, SIGKILL);
1035 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1036
1037 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001038}
1039
1040#if defined(ENABLE_PSS_TESTS)
1041#include "GetPss.h"
1042
1043#define MAX_LEAK_BYTES 32*1024UL
1044
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001045void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001046 // Do a few runs to get the PSS stable.
1047 for (size_t i = 0; i < 100; i++) {
1048 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001049 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001050 ASSERT_TRUE(backtrace->Unwind(0));
1051 delete backtrace;
1052 }
1053 size_t stable_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001054 ASSERT_TRUE(stable_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001055
1056 // Loop enough that even a small leak should be detectable.
1057 for (size_t i = 0; i < 4096; i++) {
1058 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001059 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001060 ASSERT_TRUE(backtrace->Unwind(0));
1061 delete backtrace;
1062 }
1063 size_t new_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001064 ASSERT_TRUE(new_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001065 size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
1066 // As long as the new pss is within a certain amount, consider everything okay.
1067 ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
1068}
1069
1070TEST(libbacktrace, check_for_leak_local) {
1071 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1072}
1073
1074TEST(libbacktrace, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001075 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001076 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001077 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001078
1079 // Wait up to 2 seconds for the tid to be set.
1080 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1081
1082 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1083
1084 // Tell the thread to exit its infinite loop.
1085 android_atomic_acquire_store(0, &thread_data.state);
1086
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001087 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001088}
1089
1090TEST(libbacktrace, check_for_leak_remote) {
1091 pid_t pid;
1092
1093 if ((pid = fork()) == 0) {
1094 while (true) {
1095 }
1096 _exit(0);
1097 }
1098 ASSERT_LT(0, pid);
1099
1100 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1101
1102 // Wait for the process to get to a stopping point.
1103 WaitForStop(pid);
1104
1105 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1106
1107 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1108
1109 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001110 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001111}
1112#endif