Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <signal.h> |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 23 | #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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 32 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 33 | #include <backtrace/BacktraceMap.h> |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 34 | #include <UniquePtr.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 35 | |
Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 36 | // For the THREAD_SIGNAL definition. |
| 37 | #include "BacktraceThread.h" |
| 38 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 39 | #include <cutils/atomic.h> |
| 40 | #include <gtest/gtest.h> |
| 41 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 42 | #include <algorithm> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 43 | #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. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 54 | #define NUM_THREADS 40 |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 55 | |
| 56 | // Number of simultaneous threads running in our forked process. |
| 57 | #define NUM_PTRACE_THREADS 5 |
| 58 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 59 | struct thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 60 | pid_t tid; |
| 61 | int32_t state; |
| 62 | pthread_t threadId; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 63 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 64 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 65 | struct dump_thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 66 | thread_t thread; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 67 | Backtrace* backtrace; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 68 | int32_t* now; |
| 69 | int32_t done; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 70 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 71 | |
| 72 | extern "C" { |
| 73 | // Prototypes for functions in the test library. |
| 74 | int test_level_one(int, int, int, int, void (*)(void*), void*); |
| 75 | |
| 76 | int test_recursive_call(int, void (*)(void*), void*); |
| 77 | } |
| 78 | |
| 79 | uint64_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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 85 | void DumpFrames(Backtrace* backtrace) { |
| 86 | if (backtrace->NumFrames() == 0) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 87 | printf(" No frames to dump\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
| 91 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { |
| 92 | printf(" %s\n", backtrace->FormatFrameData(i).c_str()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 109 | bool ReadyLevelBacktrace(Backtrace* backtrace) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 110 | // See if test_level_four is in the backtrace. |
| 111 | bool found = false; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 112 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { |
| 113 | if (it->func_name == "test_level_four") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 114 | found = true; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return found; |
| 120 | } |
| 121 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 122 | void 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 125 | |
| 126 | // Look through the frames starting at the highest to find the |
| 127 | // frame we want. |
| 128 | size_t frame_num = 0; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 129 | for (size_t i = backtrace->NumFrames()-1; i > 2; i--) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 130 | if (backtrace->GetFrame(i)->func_name == "test_level_one") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 131 | frame_num = i; |
| 132 | break; |
| 133 | } |
| 134 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 135 | ASSERT_LT(static_cast<size_t>(0), frame_num); |
| 136 | ASSERT_LE(static_cast<size_t>(3), frame_num); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 137 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 138 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void VerifyLevelBacktrace(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 145 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 149 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 150 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 153 | bool ReadyMaxBacktrace(Backtrace* backtrace) { |
| 154 | return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 157 | void VerifyMaxDump(Backtrace* backtrace) { |
| 158 | ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 159 | // Verify that the last frame is our recursive call. |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 160 | ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, |
| 161 | "test_recursive_call"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void VerifyMaxBacktrace(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 165 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 169 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 170 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 182 | void 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 186 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 187 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | bool 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 | |
| 200 | TEST(libbacktrace, local_trace) { |
| 201 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, NULL), 0); |
| 202 | } |
| 203 | |
| 204 | void VerifyIgnoreFrames( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 205 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 209 | |
| 210 | // Check all of the frames are the same > the current frame. |
| 211 | bool check = (cur_proc == NULL); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 212 | for (size_t i = 0; i < bt_ign2->NumFrames(); i++) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 213 | if (check) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 214 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 217 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 218 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 221 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 222 | if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 223 | check = true; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void VerifyLevelIgnoreFrames(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 229 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 233 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 234 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 238 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 239 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 243 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 244 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | TEST(libbacktrace, local_trace_ignore_frames) { |
| 248 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, NULL), 0); |
| 249 | } |
| 250 | |
| 251 | TEST(libbacktrace, local_max_trace) { |
| 252 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, NULL), 0); |
| 253 | } |
| 254 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 255 | void VerifyProcTest(pid_t pid, pid_t tid, bool share_map, |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 256 | bool (*ReadyFunc)(Backtrace*), |
| 257 | void (*VerifyFunc)(Backtrace*)) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 258 | 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 Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 272 | 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 277 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 278 | ASSERT_TRUE(backtrace.get() != NULL); |
| 279 | if (ReadyFunc(backtrace.get())) { |
| 280 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 281 | verified = true; |
| 282 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 283 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 284 | 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 | |
| 291 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 295 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 296 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 297 | 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 | |
| 304 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 308 | _exit(1); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 312 | |
| 313 | kill(pid, SIGKILL); |
| 314 | int status; |
| 315 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 316 | } |
| 317 | |
| 318 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 322 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 323 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 324 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 325 | |
| 326 | kill(pid, SIGKILL); |
| 327 | int status; |
| 328 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 329 | } |
| 330 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 331 | void 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 335 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 336 | UniquePtr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); |
| 337 | ASSERT_TRUE(ign2.get() != NULL); |
| 338 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 339 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 340 | VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), NULL); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 347 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 348 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 349 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 350 | |
| 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. |
| 357 | void* PtraceThreadLevelRun(void*) { |
| 358 | EXPECT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0); |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | void 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 | |
| 380 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 392 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 393 | } |
| 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 Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 413 | VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 414 | } |
| 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 | |
| 422 | void VerifyLevelThread(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 423 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 424 | ASSERT_TRUE(backtrace.get() != NULL); |
| 425 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 426 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 427 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | TEST(libbacktrace, thread_current_level) { |
| 431 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, NULL), 0); |
| 432 | } |
| 433 | |
| 434 | void VerifyMaxThread(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 435 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 436 | ASSERT_TRUE(backtrace.get() != NULL); |
| 437 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 438 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 439 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | TEST(libbacktrace, thread_current_max) { |
| 443 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, NULL), 0); |
| 444 | } |
| 445 | |
| 446 | void* 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 | |
| 454 | TEST(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 Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 466 | // 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 472 | // Save the current signal action and make sure it is restored afterwards. |
| 473 | struct sigaction cur_action; |
Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 474 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, NULL, &cur_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 475 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 476 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 477 | ASSERT_TRUE(backtrace.get() != NULL); |
| 478 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 479 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 480 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 481 | |
| 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 Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 487 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, NULL, &new_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 488 | EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction); |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 489 | // The SA_RESTORER flag gets set behind our back, so a direct comparison |
| 490 | // doesn't work unless we mask the value off. Mips doesn't have this |
| 491 | // flag, so skip this on that platform. |
| 492 | #ifdef SA_RESTORER |
| 493 | cur_action.sa_flags &= ~SA_RESTORER; |
| 494 | new_action.sa_flags &= ~SA_RESTORER; |
| 495 | #endif |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 496 | EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags); |
| 497 | } |
| 498 | |
| 499 | TEST(libbacktrace, thread_ignore_frames) { |
| 500 | pthread_attr_t attr; |
| 501 | pthread_attr_init(&attr); |
| 502 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 503 | |
| 504 | thread_t thread_data = { 0, 0, 0 }; |
| 505 | pthread_t thread; |
| 506 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); |
| 507 | |
| 508 | // Wait up to 2 seconds for the tid to be set. |
| 509 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 510 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 511 | UniquePtr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid)); |
| 512 | ASSERT_TRUE(all.get() != NULL); |
| 513 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 514 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 515 | UniquePtr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid)); |
| 516 | ASSERT_TRUE(ign1.get() != NULL); |
| 517 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 518 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 519 | UniquePtr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid)); |
| 520 | ASSERT_TRUE(ign2.get() != NULL); |
| 521 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 522 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 523 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), NULL); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 524 | |
| 525 | // Tell the thread to exit its infinite loop. |
| 526 | android_atomic_acquire_store(0, &thread_data.state); |
| 527 | } |
| 528 | |
| 529 | void* ThreadMaxRun(void* data) { |
| 530 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 531 | |
| 532 | thread->tid = gettid(); |
| 533 | EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | TEST(libbacktrace, thread_max_trace) { |
| 538 | pthread_attr_t attr; |
| 539 | pthread_attr_init(&attr); |
| 540 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 541 | |
| 542 | thread_t thread_data = { 0, 0, 0 }; |
| 543 | pthread_t thread; |
| 544 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0); |
| 545 | |
| 546 | // Wait for the tid to be set. |
| 547 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 548 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 549 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 550 | ASSERT_TRUE(backtrace.get() != NULL); |
| 551 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 552 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 553 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 554 | |
| 555 | // Tell the thread to exit its infinite loop. |
| 556 | android_atomic_acquire_store(0, &thread_data.state); |
| 557 | } |
| 558 | |
| 559 | void* ThreadDump(void* data) { |
| 560 | dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data); |
| 561 | while (true) { |
| 562 | if (android_atomic_acquire_load(dump->now)) { |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 567 | // The status of the actual unwind will be checked elsewhere. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 568 | dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid); |
| 569 | dump->backtrace->Unwind(0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 570 | |
| 571 | android_atomic_acquire_store(1, &dump->done); |
| 572 | |
| 573 | return NULL; |
| 574 | } |
| 575 | |
| 576 | TEST(libbacktrace, thread_multiple_dump) { |
| 577 | // Dump NUM_THREADS simultaneously. |
| 578 | std::vector<thread_t> runners(NUM_THREADS); |
| 579 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 580 | |
| 581 | pthread_attr_t attr; |
| 582 | pthread_attr_init(&attr); |
| 583 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 584 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 585 | // Launch the runners, they will spin in hard loops doing nothing. |
| 586 | runners[i].tid = 0; |
| 587 | runners[i].state = 0; |
| 588 | ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0); |
| 589 | } |
| 590 | |
| 591 | // Wait for tids to be set. |
| 592 | for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 593 | ASSERT_TRUE(WaitForNonZero(&it->state, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // Start all of the dumpers at once, they will spin until they are signalled |
| 597 | // to begin their dump run. |
| 598 | int32_t dump_now = 0; |
| 599 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 600 | dumpers[i].thread.tid = runners[i].tid; |
| 601 | dumpers[i].thread.state = 0; |
| 602 | dumpers[i].done = 0; |
| 603 | dumpers[i].now = &dump_now; |
| 604 | |
| 605 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 606 | } |
| 607 | |
| 608 | // Start all of the dumpers going at once. |
| 609 | android_atomic_acquire_store(1, &dump_now); |
| 610 | |
| 611 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 612 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 613 | |
| 614 | // Tell the runner thread to exit its infinite loop. |
| 615 | android_atomic_acquire_store(0, &runners[i].state); |
| 616 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 617 | ASSERT_TRUE(dumpers[i].backtrace != NULL); |
| 618 | VerifyMaxDump(dumpers[i].backtrace); |
| 619 | |
| 620 | delete dumpers[i].backtrace; |
| 621 | dumpers[i].backtrace = NULL; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 625 | TEST(libbacktrace, thread_multiple_dump_same_thread) { |
| 626 | pthread_attr_t attr; |
| 627 | pthread_attr_init(&attr); |
| 628 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 629 | thread_t runner; |
| 630 | runner.tid = 0; |
| 631 | runner.state = 0; |
| 632 | ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0); |
| 633 | |
| 634 | // Wait for tids to be set. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 635 | ASSERT_TRUE(WaitForNonZero(&runner.state, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 636 | |
| 637 | // Start all of the dumpers at once, they will spin until they are signalled |
| 638 | // to begin their dump run. |
| 639 | int32_t dump_now = 0; |
| 640 | // Dump the same thread NUM_THREADS simultaneously. |
| 641 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 642 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 643 | dumpers[i].thread.tid = runner.tid; |
| 644 | dumpers[i].thread.state = 0; |
| 645 | dumpers[i].done = 0; |
| 646 | dumpers[i].now = &dump_now; |
| 647 | |
| 648 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 649 | } |
| 650 | |
| 651 | // Start all of the dumpers going at once. |
| 652 | android_atomic_acquire_store(1, &dump_now); |
| 653 | |
| 654 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 655 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 656 | |
| 657 | ASSERT_TRUE(dumpers[i].backtrace != NULL); |
| 658 | VerifyMaxDump(dumpers[i].backtrace); |
| 659 | |
| 660 | delete dumpers[i].backtrace; |
| 661 | dumpers[i].backtrace = NULL; |
| 662 | } |
| 663 | |
| 664 | // Tell the runner thread to exit its infinite loop. |
| 665 | android_atomic_acquire_store(0, &runner.state); |
| 666 | } |
| 667 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 668 | // This test is for UnwindMaps that should share the same map cursor when |
| 669 | // multiple maps are created for the current process at the same time. |
| 670 | TEST(libbacktrace, simultaneous_maps) { |
| 671 | BacktraceMap* map1 = BacktraceMap::Create(getpid()); |
| 672 | BacktraceMap* map2 = BacktraceMap::Create(getpid()); |
| 673 | BacktraceMap* map3 = BacktraceMap::Create(getpid()); |
| 674 | |
| 675 | Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1); |
| 676 | EXPECT_TRUE(back1->Unwind(0)); |
| 677 | delete back1; |
| 678 | delete map1; |
| 679 | |
| 680 | Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2); |
| 681 | EXPECT_TRUE(back2->Unwind(0)); |
| 682 | delete back2; |
| 683 | delete map2; |
| 684 | |
| 685 | Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3); |
| 686 | EXPECT_TRUE(back3->Unwind(0)); |
| 687 | delete back3; |
| 688 | delete map3; |
| 689 | } |
| 690 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 691 | TEST(libbacktrace, fillin_erases) { |
| 692 | BacktraceMap* back_map = BacktraceMap::Create(getpid()); |
| 693 | |
| 694 | backtrace_map_t map; |
| 695 | |
| 696 | map.start = 1; |
| 697 | map.end = 3; |
| 698 | map.flags = 1; |
| 699 | map.name = "Initialized"; |
| 700 | back_map->FillIn(0, &map); |
| 701 | delete back_map; |
| 702 | |
| 703 | ASSERT_FALSE(BacktraceMap::IsValid(map)); |
| 704 | ASSERT_EQ(static_cast<uintptr_t>(0), map.start); |
| 705 | ASSERT_EQ(static_cast<uintptr_t>(0), map.end); |
| 706 | ASSERT_EQ(0, map.flags); |
| 707 | ASSERT_EQ("", map.name); |
| 708 | } |
| 709 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 710 | TEST(libbacktrace, format_test) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 711 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD)); |
| 712 | ASSERT_TRUE(backtrace.get() != NULL); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 713 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 714 | backtrace_frame_data_t frame; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 715 | frame.num = 1; |
| 716 | frame.pc = 2; |
| 717 | frame.sp = 0; |
| 718 | frame.stack_size = 0; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 719 | frame.func_offset = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 720 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 721 | // Check no map set. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 722 | frame.num = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 723 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 724 | EXPECT_EQ("#01 pc 0000000000000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 725 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 726 | EXPECT_EQ("#01 pc 00000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 727 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 728 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 729 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 730 | // Check map name empty, but exists. |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 731 | frame.map.start = 1; |
| 732 | frame.map.end = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 733 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 734 | EXPECT_EQ("#01 pc 0000000000000001 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 735 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 736 | EXPECT_EQ("#01 pc 00000001 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 737 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 738 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 739 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 740 | |
| 741 | // Check relative pc is set and map name is set. |
| 742 | frame.pc = 0x12345679; |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 743 | frame.map.name = "MapFake"; |
| 744 | frame.map.start = 1; |
| 745 | frame.map.end = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 746 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 747 | EXPECT_EQ("#01 pc 0000000012345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 748 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 749 | EXPECT_EQ("#01 pc 12345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 750 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 751 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 752 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 753 | // Check func_name is set, but no func offset. |
| 754 | frame.func_name = "ProcFake"; |
| 755 | #if defined(__LP64__) |
| 756 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)", |
| 757 | #else |
| 758 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)", |
| 759 | #endif |
| 760 | backtrace->FormatFrameData(&frame)); |
| 761 | |
| 762 | // Check func_name is set, and func offset is non-zero. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 763 | frame.func_offset = 645; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 764 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 765 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 766 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 767 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 768 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 769 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 770 | } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 771 | |
| 772 | struct map_test_t { |
| 773 | uintptr_t start; |
| 774 | uintptr_t end; |
| 775 | }; |
| 776 | |
| 777 | bool map_sort(map_test_t i, map_test_t j) { |
| 778 | return i.start < j.start; |
| 779 | } |
| 780 | |
| 781 | static void VerifyMap(pid_t pid) { |
| 782 | char buffer[4096]; |
| 783 | snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid); |
| 784 | |
| 785 | FILE* map_file = fopen(buffer, "r"); |
| 786 | ASSERT_TRUE(map_file != NULL); |
| 787 | std::vector<map_test_t> test_maps; |
| 788 | while (fgets(buffer, sizeof(buffer), map_file)) { |
| 789 | map_test_t map; |
| 790 | ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end)); |
| 791 | test_maps.push_back(map); |
| 792 | } |
| 793 | fclose(map_file); |
| 794 | std::sort(test_maps.begin(), test_maps.end(), map_sort); |
| 795 | |
| 796 | UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid)); |
| 797 | |
| 798 | // Basic test that verifies that the map is in the expected order. |
| 799 | std::vector<map_test_t>::const_iterator test_it = test_maps.begin(); |
| 800 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { |
| 801 | ASSERT_TRUE(test_it != test_maps.end()); |
| 802 | ASSERT_EQ(test_it->start, it->start); |
| 803 | ASSERT_EQ(test_it->end, it->end); |
| 804 | ++test_it; |
| 805 | } |
| 806 | ASSERT_TRUE(test_it == test_maps.end()); |
| 807 | } |
| 808 | |
| 809 | TEST(libbacktrace, verify_map_remote) { |
| 810 | pid_t pid; |
| 811 | |
| 812 | if ((pid = fork()) == 0) { |
| 813 | while (true) { |
| 814 | } |
| 815 | _exit(0); |
| 816 | } |
| 817 | ASSERT_LT(0, pid); |
| 818 | |
| 819 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 820 | |
| 821 | // Wait for the process to get to a stopping point. |
| 822 | WaitForStop(pid); |
| 823 | |
| 824 | // The maps should match exactly since the forked process has been paused. |
| 825 | VerifyMap(pid); |
| 826 | |
| 827 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 828 | |
| 829 | kill(pid, SIGKILL); |
| 830 | ASSERT_EQ(waitpid(pid, NULL, 0), pid); |
| 831 | } |
| 832 | |
| 833 | #if defined(ENABLE_PSS_TESTS) |
| 834 | #include "GetPss.h" |
| 835 | |
| 836 | #define MAX_LEAK_BYTES 32*1024UL |
| 837 | |
| 838 | static void CheckForLeak(pid_t pid, pid_t tid) { |
| 839 | // Do a few runs to get the PSS stable. |
| 840 | for (size_t i = 0; i < 100; i++) { |
| 841 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
| 842 | ASSERT_TRUE(backtrace != NULL); |
| 843 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 844 | delete backtrace; |
| 845 | } |
| 846 | size_t stable_pss = GetPssBytes(); |
| 847 | |
| 848 | // Loop enough that even a small leak should be detectable. |
| 849 | for (size_t i = 0; i < 4096; i++) { |
| 850 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
| 851 | ASSERT_TRUE(backtrace != NULL); |
| 852 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 853 | delete backtrace; |
| 854 | } |
| 855 | size_t new_pss = GetPssBytes(); |
| 856 | size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss; |
| 857 | // As long as the new pss is within a certain amount, consider everything okay. |
| 858 | ASSERT_LE(abs_diff, MAX_LEAK_BYTES); |
| 859 | } |
| 860 | |
| 861 | TEST(libbacktrace, check_for_leak_local) { |
| 862 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD); |
| 863 | } |
| 864 | |
| 865 | TEST(libbacktrace, check_for_leak_local_thread) { |
| 866 | thread_t thread_data = { 0, 0, 0 }; |
| 867 | pthread_t thread; |
| 868 | ASSERT_TRUE(pthread_create(&thread, NULL, ThreadLevelRun, &thread_data) == 0); |
| 869 | |
| 870 | // Wait up to 2 seconds for the tid to be set. |
| 871 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 872 | |
| 873 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid); |
| 874 | |
| 875 | // Tell the thread to exit its infinite loop. |
| 876 | android_atomic_acquire_store(0, &thread_data.state); |
| 877 | |
| 878 | ASSERT_TRUE(pthread_join(thread, NULL) == 0); |
| 879 | } |
| 880 | |
| 881 | TEST(libbacktrace, check_for_leak_remote) { |
| 882 | pid_t pid; |
| 883 | |
| 884 | if ((pid = fork()) == 0) { |
| 885 | while (true) { |
| 886 | } |
| 887 | _exit(0); |
| 888 | } |
| 889 | ASSERT_LT(0, pid); |
| 890 | |
| 891 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 892 | |
| 893 | // Wait for the process to get to a stopping point. |
| 894 | WaitForStop(pid); |
| 895 | |
| 896 | CheckForLeak(pid, BACKTRACE_CURRENT_THREAD); |
| 897 | |
| 898 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 899 | |
| 900 | kill(pid, SIGKILL); |
| 901 | ASSERT_EQ(waitpid(pid, NULL, 0), pid); |
| 902 | } |
| 903 | #endif |