blob: 9744922db9c89a42a1204d1393d1807d4c7c1910 [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
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -070036// For the THREAD_SIGNAL definition.
37#include "BacktraceThread.h"
38
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 Ferris17e91d42013-10-21 13:30:52 -070043#include <vector>
44
45#include "thread_utils.h"
46
47// Number of microseconds per milliseconds.
48#define US_PER_MSEC 1000
49
50// Number of nanoseconds in a second.
51#define NS_PER_SEC 1000000000ULL
52
53// Number of simultaneous dumping operations to perform.
54#define NUM_THREADS 20
55
56// Number of simultaneous threads running in our forked process.
57#define NUM_PTRACE_THREADS 5
58
Christopher Ferris46756822014-01-14 20:16:30 -080059struct thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070060 pid_t tid;
61 int32_t state;
62 pthread_t threadId;
Christopher Ferris46756822014-01-14 20:16:30 -080063};
Christopher Ferris17e91d42013-10-21 13:30:52 -070064
Christopher Ferris46756822014-01-14 20:16:30 -080065struct dump_thread_t {
Christopher Ferris17e91d42013-10-21 13:30:52 -070066 thread_t thread;
Christopher Ferris20303f82014-01-10 16:33:16 -080067 Backtrace* backtrace;
Christopher Ferris17e91d42013-10-21 13:30:52 -070068 int32_t* now;
69 int32_t done;
Christopher Ferris46756822014-01-14 20:16:30 -080070};
Christopher Ferris17e91d42013-10-21 13:30:52 -070071
72extern "C" {
73// Prototypes for functions in the test library.
74int test_level_one(int, int, int, int, void (*)(void*), void*);
75
76int test_recursive_call(int, void (*)(void*), void*);
77}
78
79uint64_t NanoTime() {
80 struct timespec t = { 0, 0 };
81 clock_gettime(CLOCK_MONOTONIC, &t);
82 return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec);
83}
84
Christopher Ferris20303f82014-01-10 16:33:16 -080085void DumpFrames(Backtrace* backtrace) {
86 if (backtrace->NumFrames() == 0) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070087 printf(" No frames to dump\n");
Christopher Ferris20303f82014-01-10 16:33:16 -080088 return;
89 }
90
91 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
92 printf(" %s\n", backtrace->FormatFrameData(i).c_str());
Christopher Ferris17e91d42013-10-21 13:30:52 -070093 }
94}
95
96void WaitForStop(pid_t pid) {
97 uint64_t start = NanoTime();
98
99 siginfo_t si;
100 while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) {
101 if ((NanoTime() - start) > NS_PER_SEC) {
102 printf("The process did not get to a stopping point in 1 second.\n");
103 break;
104 }
105 usleep(US_PER_MSEC);
106 }
107}
108
Christopher Ferris20303f82014-01-10 16:33:16 -0800109bool ReadyLevelBacktrace(Backtrace* backtrace) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700110 // See if test_level_four is in the backtrace.
111 bool found = false;
Christopher Ferris46756822014-01-14 20:16:30 -0800112 for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) {
113 if (it->func_name == "test_level_four") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700114 found = true;
115 break;
116 }
117 }
118
119 return found;
120}
121
Christopher Ferris20303f82014-01-10 16:33:16 -0800122void VerifyLevelDump(Backtrace* backtrace) {
123 ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0));
124 ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700125
126 // Look through the frames starting at the highest to find the
127 // frame we want.
128 size_t frame_num = 0;
Christopher Ferris20303f82014-01-10 16:33:16 -0800129 for (size_t i = backtrace->NumFrames()-1; i > 2; i--) {
Christopher Ferris46756822014-01-14 20:16:30 -0800130 if (backtrace->GetFrame(i)->func_name == "test_level_one") {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700131 frame_num = i;
132 break;
133 }
134 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800135 ASSERT_LT(static_cast<size_t>(0), frame_num);
136 ASSERT_LE(static_cast<size_t>(3), frame_num);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700137
Christopher Ferris46756822014-01-14 20:16:30 -0800138 ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one");
139 ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two");
140 ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three");
141 ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700142}
143
144void VerifyLevelBacktrace(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800145 UniquePtr<Backtrace> backtrace(
146 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
147 ASSERT_TRUE(backtrace.get() != NULL);
148 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700149
Christopher Ferris20303f82014-01-10 16:33:16 -0800150 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700151}
152
Christopher Ferris20303f82014-01-10 16:33:16 -0800153bool ReadyMaxBacktrace(Backtrace* backtrace) {
154 return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700155}
156
Christopher Ferris20303f82014-01-10 16:33:16 -0800157void VerifyMaxDump(Backtrace* backtrace) {
158 ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700159 // Verify that the last frame is our recursive call.
Christopher Ferris46756822014-01-14 20:16:30 -0800160 ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name,
161 "test_recursive_call");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700162}
163
164void VerifyMaxBacktrace(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800165 UniquePtr<Backtrace> backtrace(
166 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
167 ASSERT_TRUE(backtrace.get() != NULL);
168 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700169
Christopher Ferris20303f82014-01-10 16:33:16 -0800170 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700171}
172
173void ThreadSetState(void* data) {
174 thread_t* thread = reinterpret_cast<thread_t*>(data);
175 android_atomic_acquire_store(1, &thread->state);
176 volatile int i = 0;
177 while (thread->state) {
178 i++;
179 }
180}
181
Christopher Ferris20303f82014-01-10 16:33:16 -0800182void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) {
183 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
184 ASSERT_TRUE(backtrace.get() != NULL);
185 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700186
Christopher Ferris20303f82014-01-10 16:33:16 -0800187 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700188}
189
190bool WaitForNonZero(int32_t* value, uint64_t seconds) {
191 uint64_t start = NanoTime();
192 do {
193 if (android_atomic_acquire_load(value)) {
194 return true;
195 }
196 } while ((NanoTime() - start) < seconds * NS_PER_SEC);
197 return false;
198}
199
200TEST(libbacktrace, local_trace) {
201 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, NULL), 0);
202}
203
204void VerifyIgnoreFrames(
Christopher Ferris20303f82014-01-10 16:33:16 -0800205 Backtrace* bt_all, Backtrace* bt_ign1,
206 Backtrace* bt_ign2, const char* cur_proc) {
207 EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1);
208 EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700209
210 // Check all of the frames are the same > the current frame.
211 bool check = (cur_proc == NULL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800212 for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700213 if (check) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800214 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
215 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp);
216 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_ign1->GetFrame(i+1)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700217
Christopher Ferris20303f82014-01-10 16:33:16 -0800218 EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc);
219 EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp);
220 EXPECT_EQ(bt_ign2->GetFrame(i)->stack_size, bt_all->GetFrame(i+2)->stack_size);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700221 }
Christopher Ferris46756822014-01-14 20:16:30 -0800222 if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700223 check = true;
224 }
225 }
226}
227
228void VerifyLevelIgnoreFrames(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800229 UniquePtr<Backtrace> all(
230 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
231 ASSERT_TRUE(all.get() != NULL);
232 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700233
Christopher Ferris20303f82014-01-10 16:33:16 -0800234 UniquePtr<Backtrace> ign1(
235 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
236 ASSERT_TRUE(ign1.get() != NULL);
237 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700238
Christopher Ferris20303f82014-01-10 16:33:16 -0800239 UniquePtr<Backtrace> ign2(
240 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
241 ASSERT_TRUE(ign2.get() != NULL);
242 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700243
Christopher Ferris20303f82014-01-10 16:33:16 -0800244 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
Christopher Ferris17e91d42013-10-21 13:30:52 -0700245}
246
247TEST(libbacktrace, local_trace_ignore_frames) {
248 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, NULL), 0);
249}
250
251TEST(libbacktrace, local_max_trace) {
252 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, NULL), 0);
253}
254
Christopher Ferrisdf290612014-01-22 19:21:07 -0800255void VerifyProcTest(pid_t pid, pid_t tid, bool share_map,
Christopher Ferris20303f82014-01-10 16:33:16 -0800256 bool (*ReadyFunc)(Backtrace*),
257 void (*VerifyFunc)(Backtrace*)) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700258 pid_t ptrace_tid;
259 if (tid < 0) {
260 ptrace_tid = pid;
261 } else {
262 ptrace_tid = tid;
263 }
264 uint64_t start = NanoTime();
265 bool verified = false;
266 do {
267 usleep(US_PER_MSEC);
268 if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) {
269 // Wait for the process to get to a stopping point.
270 WaitForStop(ptrace_tid);
271
Christopher Ferrisdf290612014-01-22 19:21:07 -0800272 UniquePtr<BacktraceMap> map;
273 if (share_map) {
274 map.reset(BacktraceMap::Create(pid));
275 }
276 UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
Christopher Ferris20303f82014-01-10 16:33:16 -0800277 ASSERT_TRUE(backtrace->Unwind(0));
278 ASSERT_TRUE(backtrace.get() != NULL);
279 if (ReadyFunc(backtrace.get())) {
280 VerifyFunc(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700281 verified = true;
282 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800283
Christopher Ferris17e91d42013-10-21 13:30:52 -0700284 ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0);
285 }
286 // If 5 seconds have passed, then we are done.
287 } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC);
288 ASSERT_TRUE(verified);
289}
290
291TEST(libbacktrace, ptrace_trace) {
292 pid_t pid;
293 if ((pid = fork()) == 0) {
294 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800295 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700296 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800297 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
298
299 kill(pid, SIGKILL);
300 int status;
301 ASSERT_EQ(waitpid(pid, &status, 0), pid);
302}
303
304TEST(libbacktrace, ptrace_trace_shared_map) {
305 pid_t pid;
306 if ((pid = fork()) == 0) {
307 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800308 _exit(1);
Christopher Ferrisdf290612014-01-22 19:21:07 -0800309 }
310
311 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700312
313 kill(pid, SIGKILL);
314 int status;
315 ASSERT_EQ(waitpid(pid, &status, 0), pid);
316}
317
318TEST(libbacktrace, ptrace_max_trace) {
319 pid_t pid;
320 if ((pid = fork()) == 0) {
321 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800322 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700323 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800324 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700325
326 kill(pid, SIGKILL);
327 int status;
328 ASSERT_EQ(waitpid(pid, &status, 0), pid);
329}
330
Christopher Ferris20303f82014-01-10 16:33:16 -0800331void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
332 UniquePtr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
333 ASSERT_TRUE(ign1.get() != NULL);
334 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700335
Christopher Ferris20303f82014-01-10 16:33:16 -0800336 UniquePtr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
337 ASSERT_TRUE(ign2.get() != NULL);
338 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700339
Christopher Ferris20303f82014-01-10 16:33:16 -0800340 VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), NULL);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700341}
342
343TEST(libbacktrace, ptrace_ignore_frames) {
344 pid_t pid;
345 if ((pid = fork()) == 0) {
346 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800347 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700348 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800349 VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700350
351 kill(pid, SIGKILL);
352 int status;
353 ASSERT_EQ(waitpid(pid, &status, 0), pid);
354}
355
356// Create a process with multiple threads and dump all of the threads.
357void* PtraceThreadLevelRun(void*) {
358 EXPECT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
359 return NULL;
360}
361
362void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
363 // Get the list of tasks.
364 char task_path[128];
365 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
366
367 DIR* tasks_dir = opendir(task_path);
368 ASSERT_TRUE(tasks_dir != NULL);
369 struct dirent* entry;
370 while ((entry = readdir(tasks_dir)) != NULL) {
371 char* end;
372 pid_t tid = strtoul(entry->d_name, &end, 10);
373 if (*end == '\0') {
374 threads->push_back(tid);
375 }
376 }
377 closedir(tasks_dir);
378}
379
380TEST(libbacktrace, ptrace_threads) {
381 pid_t pid;
382 if ((pid = fork()) == 0) {
383 for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) {
384 pthread_attr_t attr;
385 pthread_attr_init(&attr);
386 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
387
388 pthread_t thread;
389 ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, NULL) == 0);
390 }
391 ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
Christopher Ferrise2960912014-03-07 19:42:19 -0800392 _exit(1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700393 }
394
395 // Check to see that all of the threads are running before unwinding.
396 std::vector<pid_t> threads;
397 uint64_t start = NanoTime();
398 do {
399 usleep(US_PER_MSEC);
400 threads.clear();
401 GetThreads(pid, &threads);
402 } while ((threads.size() != NUM_PTRACE_THREADS + 1) &&
403 ((NanoTime() - start) <= 5 * NS_PER_SEC));
404 ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1));
405
406 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
407 WaitForStop(pid);
408 for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) {
409 // Skip the current forked process, we only care about the threads.
410 if (pid == *it) {
411 continue;
412 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800413 VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700414 }
415 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
416
417 kill(pid, SIGKILL);
418 int status;
419 ASSERT_EQ(waitpid(pid, &status, 0), pid);
420}
421
422void VerifyLevelThread(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800423 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
424 ASSERT_TRUE(backtrace.get() != NULL);
425 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700426
Christopher Ferris20303f82014-01-10 16:33:16 -0800427 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700428}
429
430TEST(libbacktrace, thread_current_level) {
431 ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, NULL), 0);
432}
433
434void VerifyMaxThread(void*) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800435 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
436 ASSERT_TRUE(backtrace.get() != NULL);
437 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700438
Christopher Ferris20303f82014-01-10 16:33:16 -0800439 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700440}
441
442TEST(libbacktrace, thread_current_max) {
443 ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, NULL), 0);
444}
445
446void* ThreadLevelRun(void* data) {
447 thread_t* thread = reinterpret_cast<thread_t*>(data);
448
449 thread->tid = gettid();
450 EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
451 return NULL;
452}
453
454TEST(libbacktrace, thread_level_trace) {
455 pthread_attr_t attr;
456 pthread_attr_init(&attr);
457 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
458
459 thread_t thread_data = { 0, 0, 0 };
460 pthread_t thread;
461 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
462
463 // Wait up to 2 seconds for the tid to be set.
464 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
465
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700466 // Make sure that the thread signal used is not visible when compiled for
467 // the target.
468#if !defined(__GLIBC__)
469 ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
470#endif
471
Christopher Ferris17e91d42013-10-21 13:30:52 -0700472 // Save the current signal action and make sure it is restored afterwards.
473 struct sigaction cur_action;
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700474 ASSERT_TRUE(sigaction(THREAD_SIGNAL, NULL, &cur_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700475
Christopher Ferris20303f82014-01-10 16:33:16 -0800476 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
477 ASSERT_TRUE(backtrace.get() != NULL);
478 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700479
Christopher Ferris20303f82014-01-10 16:33:16 -0800480 VerifyLevelDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700481
482 // Tell the thread to exit its infinite loop.
483 android_atomic_acquire_store(0, &thread_data.state);
484
485 // Verify that the old action was restored.
486 struct sigaction new_action;
Christopher Ferrisaa63d9f2014-04-29 09:35:30 -0700487 ASSERT_TRUE(sigaction(THREAD_SIGNAL, NULL, &new_action) == 0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700488 EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
489 EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
490}
491
492TEST(libbacktrace, thread_ignore_frames) {
493 pthread_attr_t attr;
494 pthread_attr_init(&attr);
495 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
496
497 thread_t thread_data = { 0, 0, 0 };
498 pthread_t thread;
499 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
500
501 // Wait up to 2 seconds for the tid to be set.
502 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
503
Christopher Ferris20303f82014-01-10 16:33:16 -0800504 UniquePtr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
505 ASSERT_TRUE(all.get() != NULL);
506 ASSERT_TRUE(all->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700507
Christopher Ferris20303f82014-01-10 16:33:16 -0800508 UniquePtr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
509 ASSERT_TRUE(ign1.get() != NULL);
510 ASSERT_TRUE(ign1->Unwind(1));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700511
Christopher Ferris20303f82014-01-10 16:33:16 -0800512 UniquePtr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
513 ASSERT_TRUE(ign2.get() != NULL);
514 ASSERT_TRUE(ign2->Unwind(2));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700515
Christopher Ferris20303f82014-01-10 16:33:16 -0800516 VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), NULL);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700517
518 // Tell the thread to exit its infinite loop.
519 android_atomic_acquire_store(0, &thread_data.state);
520}
521
522void* ThreadMaxRun(void* data) {
523 thread_t* thread = reinterpret_cast<thread_t*>(data);
524
525 thread->tid = gettid();
526 EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
527 return NULL;
528}
529
530TEST(libbacktrace, thread_max_trace) {
531 pthread_attr_t attr;
532 pthread_attr_init(&attr);
533 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
534
535 thread_t thread_data = { 0, 0, 0 };
536 pthread_t thread;
537 ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
538
539 // Wait for the tid to be set.
540 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
541
Christopher Ferris20303f82014-01-10 16:33:16 -0800542 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
543 ASSERT_TRUE(backtrace.get() != NULL);
544 ASSERT_TRUE(backtrace->Unwind(0));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700545
Christopher Ferris20303f82014-01-10 16:33:16 -0800546 VerifyMaxDump(backtrace.get());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700547
548 // Tell the thread to exit its infinite loop.
549 android_atomic_acquire_store(0, &thread_data.state);
550}
551
552void* ThreadDump(void* data) {
553 dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data);
554 while (true) {
555 if (android_atomic_acquire_load(dump->now)) {
556 break;
557 }
558 }
559
Christopher Ferris17e91d42013-10-21 13:30:52 -0700560 // The status of the actual unwind will be checked elsewhere.
Christopher Ferris20303f82014-01-10 16:33:16 -0800561 dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid);
562 dump->backtrace->Unwind(0);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700563
564 android_atomic_acquire_store(1, &dump->done);
565
566 return NULL;
567}
568
569TEST(libbacktrace, thread_multiple_dump) {
570 // Dump NUM_THREADS simultaneously.
571 std::vector<thread_t> runners(NUM_THREADS);
572 std::vector<dump_thread_t> dumpers(NUM_THREADS);
573
574 pthread_attr_t attr;
575 pthread_attr_init(&attr);
576 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
577 for (size_t i = 0; i < NUM_THREADS; i++) {
578 // Launch the runners, they will spin in hard loops doing nothing.
579 runners[i].tid = 0;
580 runners[i].state = 0;
581 ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0);
582 }
583
584 // Wait for tids to be set.
585 for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
586 ASSERT_TRUE(WaitForNonZero(&it->state, 10));
587 }
588
589 // Start all of the dumpers at once, they will spin until they are signalled
590 // to begin their dump run.
591 int32_t dump_now = 0;
592 for (size_t i = 0; i < NUM_THREADS; i++) {
593 dumpers[i].thread.tid = runners[i].tid;
594 dumpers[i].thread.state = 0;
595 dumpers[i].done = 0;
596 dumpers[i].now = &dump_now;
597
598 ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
599 }
600
601 // Start all of the dumpers going at once.
602 android_atomic_acquire_store(1, &dump_now);
603
604 for (size_t i = 0; i < NUM_THREADS; i++) {
605 ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 10));
606
607 // Tell the runner thread to exit its infinite loop.
608 android_atomic_acquire_store(0, &runners[i].state);
609
Christopher Ferris20303f82014-01-10 16:33:16 -0800610 ASSERT_TRUE(dumpers[i].backtrace != NULL);
611 VerifyMaxDump(dumpers[i].backtrace);
612
613 delete dumpers[i].backtrace;
614 dumpers[i].backtrace = NULL;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700615 }
616}
617
Christopher Ferrisdf290612014-01-22 19:21:07 -0800618// This test is for UnwindMaps that should share the same map cursor when
619// multiple maps are created for the current process at the same time.
620TEST(libbacktrace, simultaneous_maps) {
621 BacktraceMap* map1 = BacktraceMap::Create(getpid());
622 BacktraceMap* map2 = BacktraceMap::Create(getpid());
623 BacktraceMap* map3 = BacktraceMap::Create(getpid());
624
625 Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1);
626 EXPECT_TRUE(back1->Unwind(0));
627 delete back1;
628 delete map1;
629
630 Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2);
631 EXPECT_TRUE(back2->Unwind(0));
632 delete back2;
633 delete map2;
634
635 Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3);
636 EXPECT_TRUE(back3->Unwind(0));
637 delete back3;
638 delete map3;
639}
640
Christopher Ferris17e91d42013-10-21 13:30:52 -0700641TEST(libbacktrace, format_test) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800642 UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
643 ASSERT_TRUE(backtrace.get() != NULL);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700644
Christopher Ferris20303f82014-01-10 16:33:16 -0800645 backtrace_frame_data_t frame;
Christopher Ferris46756822014-01-14 20:16:30 -0800646 frame.num = 1;
647 frame.pc = 2;
648 frame.sp = 0;
649 frame.stack_size = 0;
650 frame.map = NULL;
651 frame.func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700652
Christopher Ferris46756822014-01-14 20:16:30 -0800653 backtrace_map_t map;
654 map.start = 0;
655 map.end = 0;
656
657 // Check no map set.
Christopher Ferris20303f82014-01-10 16:33:16 -0800658 frame.num = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700659#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800660 EXPECT_EQ("#01 pc 0000000000000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700661#else
Christopher Ferris46756822014-01-14 20:16:30 -0800662 EXPECT_EQ("#01 pc 00000002 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700663#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800664 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700665
Christopher Ferris46756822014-01-14 20:16:30 -0800666 // Check map name empty, but exists.
667 frame.map = &map;
668 map.start = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700669#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800670 EXPECT_EQ("#01 pc 0000000000000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700671#else
Christopher Ferris46756822014-01-14 20:16:30 -0800672 EXPECT_EQ("#01 pc 00000001 <unknown>",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700673#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800674 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700675
Christopher Ferris46756822014-01-14 20:16:30 -0800676
677 // Check relative pc is set and map name is set.
678 frame.pc = 0x12345679;
679 frame.map = &map;
680 map.name = "MapFake";
681 map.start = 1;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700682#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800683 EXPECT_EQ("#01 pc 0000000012345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700684#else
Christopher Ferris46756822014-01-14 20:16:30 -0800685 EXPECT_EQ("#01 pc 12345678 MapFake",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700686#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800687 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700688
Christopher Ferris46756822014-01-14 20:16:30 -0800689 // Check func_name is set, but no func offset.
690 frame.func_name = "ProcFake";
691#if defined(__LP64__)
692 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)",
693#else
694 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)",
695#endif
696 backtrace->FormatFrameData(&frame));
697
698 // Check func_name is set, and func offset is non-zero.
Christopher Ferris20303f82014-01-10 16:33:16 -0800699 frame.func_offset = 645;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700700#if defined(__LP64__)
Christopher Ferris46756822014-01-14 20:16:30 -0800701 EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700702#else
Christopher Ferris46756822014-01-14 20:16:30 -0800703 EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)",
Christopher Ferris17e91d42013-10-21 13:30:52 -0700704#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800705 backtrace->FormatFrameData(&frame));
Christopher Ferris17e91d42013-10-21 13:30:52 -0700706}
Christopher Ferrise2960912014-03-07 19:42:19 -0800707
708struct map_test_t {
709 uintptr_t start;
710 uintptr_t end;
711};
712
713bool map_sort(map_test_t i, map_test_t j) {
714 return i.start < j.start;
715}
716
717static void VerifyMap(pid_t pid) {
718 char buffer[4096];
719 snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
720
721 FILE* map_file = fopen(buffer, "r");
722 ASSERT_TRUE(map_file != NULL);
723 std::vector<map_test_t> test_maps;
724 while (fgets(buffer, sizeof(buffer), map_file)) {
725 map_test_t map;
726 ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
727 test_maps.push_back(map);
728 }
729 fclose(map_file);
730 std::sort(test_maps.begin(), test_maps.end(), map_sort);
731
732 UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid));
733
734 // Basic test that verifies that the map is in the expected order.
735 std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
736 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
737 ASSERT_TRUE(test_it != test_maps.end());
738 ASSERT_EQ(test_it->start, it->start);
739 ASSERT_EQ(test_it->end, it->end);
740 ++test_it;
741 }
742 ASSERT_TRUE(test_it == test_maps.end());
743}
744
745TEST(libbacktrace, verify_map_remote) {
746 pid_t pid;
747
748 if ((pid = fork()) == 0) {
749 while (true) {
750 }
751 _exit(0);
752 }
753 ASSERT_LT(0, pid);
754
755 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
756
757 // Wait for the process to get to a stopping point.
758 WaitForStop(pid);
759
760 // The maps should match exactly since the forked process has been paused.
761 VerifyMap(pid);
762
763 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
764
765 kill(pid, SIGKILL);
766 ASSERT_EQ(waitpid(pid, NULL, 0), pid);
767}
768
769#if defined(ENABLE_PSS_TESTS)
770#include "GetPss.h"
771
772#define MAX_LEAK_BYTES 32*1024UL
773
774static void CheckForLeak(pid_t pid, pid_t tid) {
775 // Do a few runs to get the PSS stable.
776 for (size_t i = 0; i < 100; 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 stable_pss = GetPssBytes();
783
784 // Loop enough that even a small leak should be detectable.
785 for (size_t i = 0; i < 4096; i++) {
786 Backtrace* backtrace = Backtrace::Create(pid, tid);
787 ASSERT_TRUE(backtrace != NULL);
788 ASSERT_TRUE(backtrace->Unwind(0));
789 delete backtrace;
790 }
791 size_t new_pss = GetPssBytes();
792 size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
793 // As long as the new pss is within a certain amount, consider everything okay.
794 ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
795}
796
797TEST(libbacktrace, check_for_leak_local) {
798 CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
799}
800
801TEST(libbacktrace, check_for_leak_local_thread) {
802 thread_t thread_data = { 0, 0, 0 };
803 pthread_t thread;
804 ASSERT_TRUE(pthread_create(&thread, NULL, ThreadLevelRun, &thread_data) == 0);
805
806 // Wait up to 2 seconds for the tid to be set.
807 ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
808
809 CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
810
811 // Tell the thread to exit its infinite loop.
812 android_atomic_acquire_store(0, &thread_data.state);
813
814 ASSERT_TRUE(pthread_join(thread, NULL) == 0);
815}
816
817TEST(libbacktrace, check_for_leak_remote) {
818 pid_t pid;
819
820 if ((pid = fork()) == 0) {
821 while (true) {
822 }
823 _exit(0);
824 }
825 ASSERT_LT(0, pid);
826
827 ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
828
829 // Wait for the process to get to a stopping point.
830 WaitForStop(pid);
831
832 CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
833
834 ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
835
836 kill(pid, SIGKILL);
837 ASSERT_EQ(waitpid(pid, NULL, 0), pid);
838}
839#endif