blob: ba3cbb58de02986070400f7151d7a58c3592d591 [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 Ferris329ed7d2015-05-01 15:02:03 -0700774 frame.map.load_base = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700775#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800776 EXPECT_EQ("#01 pc 0000000000000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700777#else
Christopher Ferris46756822014-01-14 20:16:30 -0800778 EXPECT_EQ("#01 pc 00000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700779#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800780 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700781
Christopher Ferris46756822014-01-14 20:16:30 -0800782
783 // Check relative pc is set and map name is set.
784 frame.pc = 0x12345679;
Christopher Ferris12385e32015-02-06 13:22:01 -0800785 frame.map.name = "MapFake";
786 frame.map.start = 1;
787 frame.map.end = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700788#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800789 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700790#else
Christopher Ferris46756822014-01-14 20:16:30 -0800791 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700792#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800793 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700794
Christopher Ferris46756822014-01-14 20:16:30 -0800795 // Check func_name is set, but no func offset.
796 frame.func_name = "ProcFake";
797#if defined(__LP64__)
798 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
799#else
800 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
801#endif
802 backtrace->FormatFrameData(&frame));
803
804 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800805 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700806#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800807 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700808#else
Christopher Ferris46756822014-01-14 20:16:30 -0800809 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700810#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800811 backtrace->FormatFrameData(&frame));
Christopher Ferris329ed7d2015-05-01 15:02:03 -0700812
813 // Check func_name is set, func offset is non-zero, and load_base is non-zero.
814 frame.func_offset = 645;
815 frame.map.load_base = 100;
816#if defined(__LP64__)
817 EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)",
818#else
819 EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)",
820#endif
821 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700822}
Christopher Ferrise2960912014-03-07 19:42:19 -0800823
824struct map_test_t {
825 uintptr_t start;
826 uintptr_t end;
827};
828
829bool map_sort(map_test_t i, map_test_t j) {
830 return i.start < j.start;
831}
832
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700833void VerifyMap(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800834 char buffer[4096];
835 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
836
837 FILE* map_file = fopen(buffer, "r");
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700838 ASSERT_TRUE(map_file != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -0800839 std::vector<map_test_t> test_maps;
840 while (fgets(buffer, sizeof(buffer), map_file)) {
841 map_test_t map;
842 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
843 test_maps.push_back(map);
844 }
845 fclose(map_file);
846 std::sort(test_maps.begin(), test_maps.end(), map_sort);
847
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700848 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
Christopher Ferrise2960912014-03-07 19:42:19 -0800849
850 // Basic test that verifies that the map is in the expected order.
851 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
852 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
853 ASSERT_TRUE(test_it != test_maps.end());
854 ASSERT_EQ(test_it->start, it->start);
855 ASSERT_EQ(test_it->end, it->end);
856 ++test_it;
857 }
858 ASSERT_TRUE(test_it == test_maps.end());
859}
860
861TEST(libbacktrace, verify_map_remote) {
862 pid_t pid;
863
864 if ((pid = fork()) == 0) {
865 while (true) {
866 }
867 _exit(0);
868 }
869 ASSERT_LT(0, pid);
870
871 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
872
873 // Wait for the process to get to a stopping point.
874 WaitForStop(pid);
875
876 // The maps should match exactly since the forked process has been paused.
877 VerifyMap(pid);
878
879 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
880
881 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700882 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
883}
884
Christopher Ferris8bd4a4e2015-05-06 16:36:34 -0700885void InitMemory(uint8_t* memory, size_t bytes) {
886 for (size_t i = 0; i < bytes; i++) {
887 memory[i] = i;
888 if (memory[i] == '\0') {
889 // Don't use '\0' in our data so we can verify that an overread doesn't
890 // occur by using a '\0' as the character after the read data.
891 memory[i] = 23;
892 }
893 }
894}
895
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700896void* ThreadReadTest(void* data) {
897 thread_t* thread_data = reinterpret_cast<thread_t*>(data);
898
899 thread_data->tid = gettid();
900
901 // Create two map pages.
902 // Mark the second page as not-readable.
903 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
904 uint8_t* memory;
905 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
906 return reinterpret_cast<void*>(-1);
907 }
908
909 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
910 return reinterpret_cast<void*>(-1);
911 }
912
913 // Set up a simple pattern in memory.
Christopher Ferris8bd4a4e2015-05-06 16:36:34 -0700914 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700915
916 thread_data->data = memory;
917
918 // Tell the caller it's okay to start reading memory.
919 android_atomic_acquire_store(1, &thread_data->state);
920
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700921 // Loop waiting for the caller to finish reading the memory.
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700922 while (thread_data->state) {
923 }
924
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700925 // Re-enable read-write on the page so that we don't crash if we try
926 // and access data on this page when freeing the memory.
927 if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) {
928 return reinterpret_cast<void*>(-1);
929 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700930 free(memory);
931
932 android_atomic_acquire_store(1, &thread_data->state);
933
934 return nullptr;
935}
936
937void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
938 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
939
940 // Create a page of data to use to do quick compares.
941 uint8_t* expected = new uint8_t[pagesize];
Christopher Ferris8bd4a4e2015-05-06 16:36:34 -0700942 InitMemory(expected, pagesize);
943
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700944 uint8_t* data = new uint8_t[2*pagesize];
945 // Verify that we can only read one page worth of data.
946 size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
947 ASSERT_EQ(pagesize, bytes_read);
948 ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
949
950 // Verify unaligned reads.
951 for (size_t i = 1; i < sizeof(word_t); i++) {
952 bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
953 ASSERT_EQ(2 * sizeof(word_t), bytes_read);
954 ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
955 << "Offset at " << i << " failed";
956 }
Christopher Ferris8bd4a4e2015-05-06 16:36:34 -0700957
958 // Verify small unaligned reads.
959 for (size_t i = 1; i < sizeof(word_t); i++) {
960 for (size_t j = 1; j < sizeof(word_t); j++) {
961 // Set one byte past what we expect to read, to guarantee we don't overread.
962 data[j] = '\0';
963 bytes_read = backtrace->Read(read_addr + i, data, j);
964 ASSERT_EQ(j, bytes_read);
965 ASSERT_TRUE(memcmp(data, &expected[i], j) == 0)
966 << "Offset at " << i << " length " << j << " miscompared";
967 ASSERT_EQ('\0', data[j])
968 << "Offset at " << i << " length " << j << " wrote too much data";
969 }
970 }
Christopher Ferris2b4a63f2015-03-17 14:42:03 -0700971 delete data;
972 delete expected;
973}
974
975TEST(libbacktrace, thread_read) {
976 pthread_attr_t attr;
977 pthread_attr_init(&attr);
978 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
979 pthread_t thread;
980 thread_t thread_data = { 0, 0, 0, nullptr };
981 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
982
983 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
984
985 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
986 ASSERT_TRUE(backtrace.get() != nullptr);
987
988 RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
989
990 android_atomic_acquire_store(0, &thread_data.state);
991
992 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
993}
994
995volatile uintptr_t g_ready = 0;
996volatile uintptr_t g_addr = 0;
997
998void ForkedReadTest() {
999 // Create two map pages.
1000 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
1001 uint8_t* memory;
1002 if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
1003 perror("Failed to allocate memory\n");
1004 exit(1);
1005 }
1006
1007 // Mark the second page as not-readable.
1008 if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
1009 perror("Failed to mprotect memory\n");
1010 exit(1);
1011 }
1012
1013 // Set up a simple pattern in memory.
Christopher Ferris8bd4a4e2015-05-06 16:36:34 -07001014 InitMemory(memory, pagesize);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001015
1016 g_addr = reinterpret_cast<uintptr_t>(memory);
1017 g_ready = 1;
1018
1019 while (1) {
1020 usleep(US_PER_MSEC);
1021 }
1022}
1023
1024TEST(libbacktrace, process_read) {
1025 pid_t pid;
1026 if ((pid = fork()) == 0) {
1027 ForkedReadTest();
1028 exit(0);
1029 }
1030 ASSERT_NE(-1, pid);
1031
1032 bool test_executed = false;
1033 uint64_t start = NanoTime();
1034 while (1) {
1035 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
1036 WaitForStop(pid);
1037
1038 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
Christopher Ferris97e00bb2015-04-02 14:22:31 -07001039 ASSERT_TRUE(backtrace.get() != nullptr);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001040
1041 uintptr_t read_addr;
1042 size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
1043 reinterpret_cast<uint8_t*>(&read_addr),
1044 sizeof(uintptr_t));
1045 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1046 if (read_addr) {
1047 // The forked process is ready to be read.
1048 bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
1049 reinterpret_cast<uint8_t*>(&read_addr),
1050 sizeof(uintptr_t));
1051 ASSERT_EQ(sizeof(uintptr_t), bytes_read);
1052
1053 RunReadTest(backtrace.get(), read_addr);
1054
1055 test_executed = true;
1056 break;
1057 }
1058 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1059 }
1060 if ((NanoTime() - start) > 5 * NS_PER_SEC) {
1061 break;
1062 }
1063 usleep(US_PER_MSEC);
1064 }
1065 kill(pid, SIGKILL);
1066 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
1067
1068 ASSERT_TRUE(test_executed);
Christopher Ferrise2960912014-03-07 19:42:19 -08001069}
1070
1071#if defined(ENABLE_PSS_TESTS)
1072#include "GetPss.h"
1073
1074#define MAX_LEAK_BYTES 32*1024UL
1075
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001076void CheckForLeak(pid_t pid, pid_t tid) {
Christopher Ferrise2960912014-03-07 19:42:19 -08001077 // Do a few runs to get the PSS stable.
1078 for (size_t i = 0; i < 100; i++) {
1079 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001080 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001081 ASSERT_TRUE(backtrace->Unwind(0));
1082 delete backtrace;
1083 }
1084 size_t stable_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001085 ASSERT_TRUE(stable_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001086
1087 // Loop enough that even a small leak should be detectable.
1088 for (size_t i = 0; i < 4096; i++) {
1089 Backtrace* backtrace = Backtrace::Create(pid, tid);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001090 ASSERT_TRUE(backtrace != nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -08001091 ASSERT_TRUE(backtrace->Unwind(0));
1092 delete backtrace;
1093 }
1094 size_t new_pss = GetPssBytes();
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001095 ASSERT_TRUE(new_pss != 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001096 size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
1097 // As long as the new pss is within a certain amount, consider everything okay.
1098 ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
1099}
1100
1101TEST(libbacktrace, check_for_leak_local) {
1102 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
1103}
1104
1105TEST(libbacktrace, check_for_leak_local_thread) {
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001106 thread_t thread_data = { 0, 0, 0, nullptr };
Christopher Ferrise2960912014-03-07 19:42:19 -08001107 pthread_t thread;
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001108 ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001109
1110 // Wait up to 2 seconds for the tid to be set.
1111 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
1112
1113 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
1114
1115 // Tell the thread to exit its infinite loop.
1116 android_atomic_acquire_store(0, &thread_data.state);
1117
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001118 ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
Christopher Ferrise2960912014-03-07 19:42:19 -08001119}
1120
1121TEST(libbacktrace, check_for_leak_remote) {
1122 pid_t pid;
1123
1124 if ((pid = fork()) == 0) {
1125 while (true) {
1126 }
1127 _exit(0);
1128 }
1129 ASSERT_LT(0, pid);
1130
1131 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
1132
1133 // Wait for the process to get to a stopping point.
1134 WaitForStop(pid);
1135
1136 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
1137
1138 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
1139
1140 kill(pid, SIGKILL);
Christopher Ferris2b4a63f2015-03-17 14:42:03 -07001141 ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
Christopher Ferrise2960912014-03-07 19:42:19 -08001142}
1143#endif