blob: a5e141b2cbc60d180ca5bc544c91ac21557139d6 [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
17#include <dirent.h>
18#include <errno.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080019#include <inttypes.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070020#include <pthread.h>
21#include <signal.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080022#include <stdint.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/ptrace.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <time.h>
30#include <unistd.h>
31
Christopher Ferris20303f82014-01-10 16:33:16 -080032#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080033#include <backtrace/BacktraceMap.h>
Christopher Ferris20303f82014-01-10 16:33:16 -080034#include <UniquePtr.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070035
36#include <cutils/atomic.h>
37#include <gtest/gtest.h>
38
Christopher Ferrise2960912014-03-07 19:42:19 -080039#include <algorithm>
Christopher Ferris17e91d42013-10-21 13:30:52 -070040#include <vector>
41
42#include "thread_utils.h"
43
44// Number of microseconds per milliseconds.
45#define US_PER_MSEC 1000
46
47// Number of nanoseconds in a second.
48#define NS_PER_SEC 1000000000ULL
49
50// Number of simultaneous dumping operations to perform.
51#define NUM_THREADS 20
52
53// Number of simultaneous threads running in our forked process.
54#define NUM_PTRACE_THREADS 5
55
Christopher Ferris46756822014-01-14 20:16:30 -080056struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070057 pid_t tid;
58 int32_t state;
59 pthread_t threadId;
Christopher Ferris46756822014-01-14 20:16:30 -080060};
Christopher Ferris17e91d42013-10-21 13:30:52 -070061
Christopher Ferris46756822014-01-14 20:16:30 -080062struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070063 thread_t thread;
Christopher Ferris20303f82014-01-10 16:33:16 -080064 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070065 int32_t* now;
66 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080067};
Christopher Ferris17e91d42013-10-21 13:30:52 -070068
69extern "C" {
70// Prototypes for functions in the test library.
71int test_level_one(int, int, int, int, void (*)(void*), void*);
72
73int test_recursive_call(int, void (*)(void*), void*);
74}
75
76uint64_t NanoTime() {
77 struct timespec t = { 0, 0 };
78 clock_gettime(CLOCK_MONOTONIC, &t);
79 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
80}
81
Christopher Ferris20303f82014-01-10 16:33:16 -080082void DumpFrames(Backtrace* backtrace) {
83 if (backtrace->NumFrames() == 0) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070084 printf(" No frames to dump\n");
Christopher Ferris20303f82014-01-10 16:33:16 -080085 return;
86 }
87
88 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
89 printf(" %s\n", backtrace->FormatFrameData(i).c_str());
Christopher Ferris17e91d42013-10-21 13:30:52 -070090 }
91}
92
93void WaitForStop(pid_t pid) {
94 uint64_t start = NanoTime();
95
96 siginfo_t si;
97 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
98 if ((NanoTime() - start) > NS_PER_SEC) {
99 printf("The process did not get to a stopping point in 1 second.\n");
100 break;
101 }
102 usleep(US_PER_MSEC);
103 }
104}
105
Christopher Ferris20303f82014-01-10 16:33:16 -0800106bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700107 // See if test_level_four is in the backtrace.
108 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800109 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
110 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700111 found = true;
112 break;
113 }
114 }
115
116 return found;
117}
118
Christopher Ferris20303f82014-01-10 16:33:16 -0800119void VerifyLevelDump(Backtrace* backtrace) {
120 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0));
121 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700122
123 // Look through the frames starting at the highest to find the
124 // frame we want.
125 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800126 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800127 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700128 frame_num = i;
129 break;
130 }
131 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800132 ASSERT_LT(static_cast<size_t>(0), frame_num);
133 ASSERT_LE(static_cast<size_t>(3), frame_num);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700134
Christopher Ferris46756822014-01-14 20:16:30 -0800135 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one");
136 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two");
137 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three");
138 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700139}
140
141void VerifyLevelBacktrace(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800142 UniquePtr<Backtrace> backtrace(
143 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
144 ASSERT_TRUE(backtrace.get() != NULL);
145 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700146
Christopher Ferris20303f82014-01-10 16:33:16 -0800147 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700148}
149
Christopher Ferris20303f82014-01-10 16:33:16 -0800150bool ReadyMaxBacktrace(Backtrace* backtrace) {
151 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700152}
153
Christopher Ferris20303f82014-01-10 16:33:16 -0800154void VerifyMaxDump(Backtrace* backtrace) {
155 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700156 // Verify that the last frame is our recursive call.
Christopher Ferris46756822014-01-14 20:16:30 -0800157 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name,
158 "test_recursive_call");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700159}
160
161void VerifyMaxBacktrace(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800162 UniquePtr<Backtrace> backtrace(
163 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
164 ASSERT_TRUE(backtrace.get() != NULL);
165 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700166
Christopher Ferris20303f82014-01-10 16:33:16 -0800167 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700168}
169
170void ThreadSetState(void* data) {
171 thread_t* thread = reinterpret_cast<thread_t*>(data);
172 android_atomic_acquire_store(1, &thread->state);
173 volatile int i = 0;
174 while (thread->state) {
175 i++;
176 }
177}
178
Christopher Ferris20303f82014-01-10 16:33:16 -0800179void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) {
180 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
181 ASSERT_TRUE(backtrace.get() != NULL);
182 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700183
Christopher Ferris20303f82014-01-10 16:33:16 -0800184 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700185}
186
187bool WaitForNonZero(int32_t* value, uint64_t seconds) {
188 uint64_t start = NanoTime();
189 do {
190 if (android_atomic_acquire_load(value)) {
191 return true;
192 }
193 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
194 return false;
195}
196
197TEST(libbacktrace, local_trace) {
198 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, NULL), 0);
199}
200
201void VerifyIgnoreFrames(
Christopher Ferris20303f82014-01-10 16:33:16 -0800202 Backtrace* bt_all, Backtrace* bt_ign1,
203 Backtrace* bt_ign2, const char* cur_proc) {
204 EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1);
205 EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700206
207 // Check all of the frames are the same > the current frame.
208 bool check = (cur_proc == NULL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800209 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700210 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
212 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
213 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700214
Christopher Ferris20303f82014-01-10 16:33:16 -0800215 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
216 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
217 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700218 }
Christopher Ferris46756822014-01-14 20:16:30 -0800219 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700220 check = true;
221 }
222 }
223}
224
225void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800226 UniquePtr<Backtrace> all(
227 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
228 ASSERT_TRUE(all.get() != NULL);
229 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700230
Christopher Ferris20303f82014-01-10 16:33:16 -0800231 UniquePtr<Backtrace> ign1(
232 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
233 ASSERT_TRUE(ign1.get() != NULL);
234 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700235
Christopher Ferris20303f82014-01-10 16:33:16 -0800236 UniquePtr<Backtrace> ign2(
237 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
238 ASSERT_TRUE(ign2.get() != NULL);
239 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700240
Christopher Ferris20303f82014-01-10 16:33:16 -0800241 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700242}
243
244TEST(libbacktrace, local_trace_ignore_frames) {
245 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, NULL), 0);
246}
247
248TEST(libbacktrace, local_max_trace) {
249 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, NULL), 0);
250}
251
Christopher Ferrisdf290612014-01-22 19:21:07 -0800252void VerifyProcTest(pid_t pid, pid_t tid, bool share_map,
Christopher Ferris20303f82014-01-10 16:33:16 -0800253 bool (*ReadyFunc)(Backtrace*),
254 void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700255 pid_t ptrace_tid;
256 if (tid < 0) {
257 ptrace_tid = pid;
258 } else {
259 ptrace_tid = tid;
260 }
261 uint64_t start = NanoTime();
262 bool verified = false;
263 do {
264 usleep(US_PER_MSEC);
265 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
266 // Wait for the process to get to a stopping point.
267 WaitForStop(ptrace_tid);
268
Christopher Ferrisdf290612014-01-22 19:21:07 -0800269 UniquePtr<BacktraceMap> map;
270 if (share_map) {
271 map.reset(BacktraceMap::Create(pid));
272 }
273 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris20303f82014-01-10 16:33:16 -0800274 ASSERT_TRUE(backtrace->Unwind(0));
275 ASSERT_TRUE(backtrace.get() != NULL);
276 if (ReadyFunc(backtrace.get())) {
277 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700278 verified = true;
279 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800280
Christopher Ferris17e91d42013-10-21 13:30:52 -0700281 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
282 }
283 // If 5 seconds have passed, then we are done.
284 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
285 ASSERT_TRUE(verified);
286}
287
288TEST(libbacktrace, ptrace_trace) {
289 pid_t pid;
290 if ((pid = fork()) == 0) {
291 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800292 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700293 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800294 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
295
296 kill(pid, SIGKILL);
297 int status;
298 ASSERT_EQ(waitpid(pid, &status, 0), pid);
299}
300
301TEST(libbacktrace, ptrace_trace_shared_map) {
302 pid_t pid;
303 if ((pid = fork()) == 0) {
304 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800305 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800306 }
307
308 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700309
310 kill(pid, SIGKILL);
311 int status;
312 ASSERT_EQ(waitpid(pid, &status, 0), pid);
313}
314
315TEST(libbacktrace, ptrace_max_trace) {
316 pid_t pid;
317 if ((pid = fork()) == 0) {
318 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800319 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700320 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800321 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700322
323 kill(pid, SIGKILL);
324 int status;
325 ASSERT_EQ(waitpid(pid, &status, 0), pid);
326}
327
Christopher Ferris20303f82014-01-10 16:33:16 -0800328void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
329 UniquePtr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
330 ASSERT_TRUE(ign1.get() != NULL);
331 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700332
Christopher Ferris20303f82014-01-10 16:33:16 -0800333 UniquePtr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
334 ASSERT_TRUE(ign2.get() != NULL);
335 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700336
Christopher Ferris20303f82014-01-10 16:33:16 -0800337 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), NULL);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700338}
339
340TEST(libbacktrace, ptrace_ignore_frames) {
341 pid_t pid;
342 if ((pid = fork()) == 0) {
343 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800344 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700345 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800346 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700347
348 kill(pid, SIGKILL);
349 int status;
350 ASSERT_EQ(waitpid(pid, &status, 0), pid);
351}
352
353// Create a process with multiple threads and dump all of the threads.
354void* PtraceThreadLevelRun(void*) {
355 EXPECT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
356 return NULL;
357}
358
359void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
360 // Get the list of tasks.
361 char task_path[128];
362 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
363
364 DIR* tasks_dir = opendir(task_path);
365 ASSERT_TRUE(tasks_dir != NULL);
366 struct dirent* entry;
367 while ((entry = readdir(tasks_dir)) != NULL) {
368 char* end;
369 pid_t tid = strtoul(entry->d_name, &end, 10);
370 if (*end == '\0') {
371 threads->push_back(tid);
372 }
373 }
374 closedir(tasks_dir);
375}
376
377TEST(libbacktrace, ptrace_threads) {
378 pid_t pid;
379 if ((pid = fork()) == 0) {
380 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
381 pthread_attr_t attr;
382 pthread_attr_init(&attr);
383 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
384
385 pthread_t thread;
386 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, NULL) == 0);
387 }
388 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800389 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700390 }
391
392 // Check to see that all of the threads are running before unwinding.
393 std::vector<pid_t> threads;
394 uint64_t start = NanoTime();
395 do {
396 usleep(US_PER_MSEC);
397 threads.clear();
398 GetThreads(pid, &threads);
399 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
400 ((NanoTime() - start) <= 5 * NS_PER_SEC));
401 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
402
403 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
404 WaitForStop(pid);
405 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
406 // Skip the current forked process, we only care about the threads.
407 if (pid == *it) {
408 continue;
409 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800410 VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700411 }
412 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
413
414 kill(pid, SIGKILL);
415 int status;
416 ASSERT_EQ(waitpid(pid, &status, 0), pid);
417}
418
419void VerifyLevelThread(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800420 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
421 ASSERT_TRUE(backtrace.get() != NULL);
422 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700423
Christopher Ferris20303f82014-01-10 16:33:16 -0800424 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700425}
426
427TEST(libbacktrace, thread_current_level) {
428 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, NULL), 0);
429}
430
431void VerifyMaxThread(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800432 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
433 ASSERT_TRUE(backtrace.get() != NULL);
434 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700435
Christopher Ferris20303f82014-01-10 16:33:16 -0800436 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700437}
438
439TEST(libbacktrace, thread_current_max) {
440 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, NULL), 0);
441}
442
443void* ThreadLevelRun(void* data) {
444 thread_t* thread = reinterpret_cast<thread_t*>(data);
445
446 thread->tid = gettid();
447 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
448 return NULL;
449}
450
451TEST(libbacktrace, thread_level_trace) {
452 pthread_attr_t attr;
453 pthread_attr_init(&attr);
454 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
455
456 thread_t thread_data = { 0, 0, 0 };
457 pthread_t thread;
458 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
459
460 // Wait up to 2 seconds for the tid to be set.
461 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
462
463 // Save the current signal action and make sure it is restored afterwards.
464 struct sigaction cur_action;
465 ASSERT_TRUE(sigaction(SIGURG, NULL, &cur_action) == 0);
466
Christopher Ferris20303f82014-01-10 16:33:16 -0800467 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
468 ASSERT_TRUE(backtrace.get() != NULL);
469 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700470
Christopher Ferris20303f82014-01-10 16:33:16 -0800471 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700472
473 // Tell the thread to exit its infinite loop.
474 android_atomic_acquire_store(0, &thread_data.state);
475
476 // Verify that the old action was restored.
477 struct sigaction new_action;
478 ASSERT_TRUE(sigaction(SIGURG, NULL, &new_action) == 0);
479 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
480 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
481}
482
483TEST(libbacktrace, thread_ignore_frames) {
484 pthread_attr_t attr;
485 pthread_attr_init(&attr);
486 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
487
488 thread_t thread_data = { 0, 0, 0 };
489 pthread_t thread;
490 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
491
492 // Wait up to 2 seconds for the tid to be set.
493 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
494
Christopher Ferris20303f82014-01-10 16:33:16 -0800495 UniquePtr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
496 ASSERT_TRUE(all.get() != NULL);
497 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700498
Christopher Ferris20303f82014-01-10 16:33:16 -0800499 UniquePtr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
500 ASSERT_TRUE(ign1.get() != NULL);
501 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700502
Christopher Ferris20303f82014-01-10 16:33:16 -0800503 UniquePtr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
504 ASSERT_TRUE(ign2.get() != NULL);
505 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700506
Christopher Ferris20303f82014-01-10 16:33:16 -0800507 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), NULL);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700508
509 // Tell the thread to exit its infinite loop.
510 android_atomic_acquire_store(0, &thread_data.state);
511}
512
513void* ThreadMaxRun(void* data) {
514 thread_t* thread = reinterpret_cast<thread_t*>(data);
515
516 thread->tid = gettid();
517 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
518 return NULL;
519}
520
521TEST(libbacktrace, thread_max_trace) {
522 pthread_attr_t attr;
523 pthread_attr_init(&attr);
524 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
525
526 thread_t thread_data = { 0, 0, 0 };
527 pthread_t thread;
528 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
529
530 // Wait for the tid to be set.
531 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
532
Christopher Ferris20303f82014-01-10 16:33:16 -0800533 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
534 ASSERT_TRUE(backtrace.get() != NULL);
535 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700536
Christopher Ferris20303f82014-01-10 16:33:16 -0800537 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700538
539 // Tell the thread to exit its infinite loop.
540 android_atomic_acquire_store(0, &thread_data.state);
541}
542
543void* ThreadDump(void* data) {
544 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
545 while (true) {
546 if (android_atomic_acquire_load(dump->now)) {
547 break;
548 }
549 }
550
Christopher Ferris17e91d42013-10-21 13:30:52 -0700551 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800552 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
553 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700554
555 android_atomic_acquire_store(1, &dump->done);
556
557 return NULL;
558}
559
560TEST(libbacktrace, thread_multiple_dump) {
561 // Dump NUM_THREADS simultaneously.
562 std::vector<thread_t> runners(NUM_THREADS);
563 std::vector<dump_thread_t> dumpers(NUM_THREADS);
564
565 pthread_attr_t attr;
566 pthread_attr_init(&attr);
567 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
568 for (size_t i = 0; i < NUM_THREADS; i++) {
569 // Launch the runners, they will spin in hard loops doing nothing.
570 runners[i].tid = 0;
571 runners[i].state = 0;
572 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
573 }
574
575 // Wait for tids to be set.
576 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
577 ASSERT_TRUE(WaitForNonZero(&it->state, 10));
578 }
579
580 // Start all of the dumpers at once, they will spin until they are signalled
581 // to begin their dump run.
582 int32_t dump_now = 0;
583 for (size_t i = 0; i < NUM_THREADS; i++) {
584 dumpers[i].thread.tid = runners[i].tid;
585 dumpers[i].thread.state = 0;
586 dumpers[i].done = 0;
587 dumpers[i].now = &dump_now;
588
589 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
590 }
591
592 // Start all of the dumpers going at once.
593 android_atomic_acquire_store(1, &dump_now);
594
595 for (size_t i = 0; i < NUM_THREADS; i++) {
596 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 10));
597
598 // Tell the runner thread to exit its infinite loop.
599 android_atomic_acquire_store(0, &runners[i].state);
600
Christopher Ferris20303f82014-01-10 16:33:16 -0800601 ASSERT_TRUE(dumpers[i].backtrace != NULL);
602 VerifyMaxDump(dumpers[i].backtrace);
603
604 delete dumpers[i].backtrace;
605 dumpers[i].backtrace = NULL;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700606 }
607}
608
Christopher Ferrisdf290612014-01-22 19:21:07 -0800609// This test is for UnwindMaps that should share the same map cursor when
610// multiple maps are created for the current process at the same time.
611TEST(libbacktrace, simultaneous_maps) {
612 BacktraceMap* map1 = BacktraceMap::Create(getpid());
613 BacktraceMap* map2 = BacktraceMap::Create(getpid());
614 BacktraceMap* map3 = BacktraceMap::Create(getpid());
615
616 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
617 EXPECT_TRUE(back1->Unwind(0));
618 delete back1;
619 delete map1;
620
621 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
622 EXPECT_TRUE(back2->Unwind(0));
623 delete back2;
624 delete map2;
625
626 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
627 EXPECT_TRUE(back3->Unwind(0));
628 delete back3;
629 delete map3;
630}
631
Christopher Ferris17e91d42013-10-21 13:30:52 -0700632TEST(libbacktrace, format_test) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800633 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
634 ASSERT_TRUE(backtrace.get() != NULL);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700635
Christopher Ferris20303f82014-01-10 16:33:16 -0800636 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800637 frame.num = 1;
638 frame.pc = 2;
639 frame.sp = 0;
640 frame.stack_size = 0;
641 frame.map = NULL;
642 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700643
Christopher Ferris46756822014-01-14 20:16:30 -0800644 backtrace_map_t map;
645 map.start = 0;
646 map.end = 0;
647
648 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800649 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700650#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800651 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700652#else
Christopher Ferris46756822014-01-14 20:16:30 -0800653 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700654#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800655 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700656
Christopher Ferris46756822014-01-14 20:16:30 -0800657 // Check map name empty, but exists.
658 frame.map = &map;
659 map.start = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700660#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800661 EXPECT_EQ("#01 pc 0000000000000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700662#else
Christopher Ferris46756822014-01-14 20:16:30 -0800663 EXPECT_EQ("#01 pc 00000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700664#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800665 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700666
Christopher Ferris46756822014-01-14 20:16:30 -0800667
668 // Check relative pc is set and map name is set.
669 frame.pc = 0x12345679;
670 frame.map = &map;
671 map.name = "MapFake";
672 map.start = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700673#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800674 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700675#else
Christopher Ferris46756822014-01-14 20:16:30 -0800676 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700677#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800678 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700679
Christopher Ferris46756822014-01-14 20:16:30 -0800680 // Check func_name is set, but no func offset.
681 frame.func_name = "ProcFake";
682#if defined(__LP64__)
683 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
684#else
685 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
686#endif
687 backtrace->FormatFrameData(&frame));
688
689 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800690 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700691#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800692 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700693#else
Christopher Ferris46756822014-01-14 20:16:30 -0800694 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700695#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800696 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700697}
Christopher Ferrise2960912014-03-07 19:42:19 -0800698
699struct map_test_t {
700 uintptr_t start;
701 uintptr_t end;
702};
703
704bool map_sort(map_test_t i, map_test_t j) {
705 return i.start < j.start;
706}
707
708static void VerifyMap(pid_t pid) {
709 char buffer[4096];
710 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
711
712 FILE* map_file = fopen(buffer, "r");
713 ASSERT_TRUE(map_file != NULL);
714 std::vector<map_test_t> test_maps;
715 while (fgets(buffer, sizeof(buffer), map_file)) {
716 map_test_t map;
717 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
718 test_maps.push_back(map);
719 }
720 fclose(map_file);
721 std::sort(test_maps.begin(), test_maps.end(), map_sort);
722
723 UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid));
724
725 // Basic test that verifies that the map is in the expected order.
726 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
727 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
728 ASSERT_TRUE(test_it != test_maps.end());
729 ASSERT_EQ(test_it->start, it->start);
730 ASSERT_EQ(test_it->end, it->end);
731 ++test_it;
732 }
733 ASSERT_TRUE(test_it == test_maps.end());
734}
735
736TEST(libbacktrace, verify_map_remote) {
737 pid_t pid;
738
739 if ((pid = fork()) == 0) {
740 while (true) {
741 }
742 _exit(0);
743 }
744 ASSERT_LT(0, pid);
745
746 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
747
748 // Wait for the process to get to a stopping point.
749 WaitForStop(pid);
750
751 // The maps should match exactly since the forked process has been paused.
752 VerifyMap(pid);
753
754 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
755
756 kill(pid, SIGKILL);
757 ASSERT_EQ(waitpid(pid, NULL, 0), pid);
758}
759
760#if defined(ENABLE_PSS_TESTS)
761#include "GetPss.h"
762
763#define MAX_LEAK_BYTES 32*1024UL
764
765static void CheckForLeak(pid_t pid, pid_t tid) {
766 // Do a few runs to get the PSS stable.
767 for (size_t i = 0; i < 100; i++) {
768 Backtrace* backtrace = Backtrace::Create(pid, tid);
769 ASSERT_TRUE(backtrace != NULL);
770 ASSERT_TRUE(backtrace->Unwind(0));
771 delete backtrace;
772 }
773 size_t stable_pss = GetPssBytes();
774
775 // Loop enough that even a small leak should be detectable.
776 for (size_t i = 0; i < 4096; i++) {
777 Backtrace* backtrace = Backtrace::Create(pid, tid);
778 ASSERT_TRUE(backtrace != NULL);
779 ASSERT_TRUE(backtrace->Unwind(0));
780 delete backtrace;
781 }
782 size_t new_pss = GetPssBytes();
783 size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
784 // As long as the new pss is within a certain amount, consider everything okay.
785 ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
786}
787
788TEST(libbacktrace, check_for_leak_local) {
789 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
790}
791
792TEST(libbacktrace, check_for_leak_local_thread) {
793 thread_t thread_data = { 0, 0, 0 };
794 pthread_t thread;
795 ASSERT_TRUE(pthread_create(&thread, NULL, ThreadLevelRun, &thread_data) == 0);
796
797 // Wait up to 2 seconds for the tid to be set.
798 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
799
800 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
801
802 // Tell the thread to exit its infinite loop.
803 android_atomic_acquire_store(0, &thread_data.state);
804
805 ASSERT_TRUE(pthread_join(thread, NULL) == 0);
806}
807
808TEST(libbacktrace, check_for_leak_remote) {
809 pid_t pid;
810
811 if ((pid = fork()) == 0) {
812 while (true) {
813 }
814 _exit(0);
815 }
816 ASSERT_LT(0, pid);
817
818 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
819
820 // Wait for the process to get to a stopping point.
821 WaitForStop(pid);
822
823 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
824
825 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
826
827 kill(pid, SIGKILL);
828 ASSERT_EQ(waitpid(pid, NULL, 0), pid);
829}
830#endif