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 | |
| 36 | #include <cutils/atomic.h> |
| 37 | #include <gtest/gtest.h> |
| 38 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame^] | 39 | #include <algorithm> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 40 | #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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 56 | struct thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 57 | pid_t tid; |
| 58 | int32_t state; |
| 59 | pthread_t threadId; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 60 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 61 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 62 | struct dump_thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 63 | thread_t thread; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 64 | Backtrace* backtrace; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 65 | int32_t* now; |
| 66 | int32_t done; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 67 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 68 | |
| 69 | extern "C" { |
| 70 | // Prototypes for functions in the test library. |
| 71 | int test_level_one(int, int, int, int, void (*)(void*), void*); |
| 72 | |
| 73 | int test_recursive_call(int, void (*)(void*), void*); |
| 74 | } |
| 75 | |
| 76 | uint64_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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 82 | void DumpFrames(Backtrace* backtrace) { |
| 83 | if (backtrace->NumFrames() == 0) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 84 | printf(" No frames to dump\n"); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
| 88 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { |
| 89 | printf(" %s\n", backtrace->FormatFrameData(i).c_str()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | void 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 106 | bool ReadyLevelBacktrace(Backtrace* backtrace) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 107 | // See if test_level_four is in the backtrace. |
| 108 | bool found = false; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 109 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { |
| 110 | if (it->func_name == "test_level_four") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 111 | found = true; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return found; |
| 117 | } |
| 118 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 119 | void 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 122 | |
| 123 | // Look through the frames starting at the highest to find the |
| 124 | // frame we want. |
| 125 | size_t frame_num = 0; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 126 | for (size_t i = backtrace->NumFrames()-1; i > 2; i--) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 127 | if (backtrace->GetFrame(i)->func_name == "test_level_one") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 128 | frame_num = i; |
| 129 | break; |
| 130 | } |
| 131 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 132 | ASSERT_LT(static_cast<size_t>(0), frame_num); |
| 133 | ASSERT_LE(static_cast<size_t>(3), frame_num); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 134 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 135 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void VerifyLevelBacktrace(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 142 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 146 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 147 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 150 | bool ReadyMaxBacktrace(Backtrace* backtrace) { |
| 151 | return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 154 | void VerifyMaxDump(Backtrace* backtrace) { |
| 155 | ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 156 | // Verify that the last frame is our recursive call. |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 157 | ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, |
| 158 | "test_recursive_call"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void VerifyMaxBacktrace(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 162 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 166 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 167 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 179 | void 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 183 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 184 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | bool 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 | |
| 197 | TEST(libbacktrace, local_trace) { |
| 198 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, NULL), 0); |
| 199 | } |
| 200 | |
| 201 | void VerifyIgnoreFrames( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 202 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 206 | |
| 207 | // Check all of the frames are the same > the current frame. |
| 208 | bool check = (cur_proc == NULL); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 209 | for (size_t i = 0; i < bt_ign2->NumFrames(); i++) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 210 | if (check) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 211 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 214 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 215 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 218 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 219 | if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 220 | check = true; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void VerifyLevelIgnoreFrames(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 226 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 230 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 231 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 235 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 236 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 240 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 241 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TEST(libbacktrace, local_trace_ignore_frames) { |
| 245 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, NULL), 0); |
| 246 | } |
| 247 | |
| 248 | TEST(libbacktrace, local_max_trace) { |
| 249 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, NULL), 0); |
| 250 | } |
| 251 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 252 | void VerifyProcTest(pid_t pid, pid_t tid, bool share_map, |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 253 | bool (*ReadyFunc)(Backtrace*), |
| 254 | void (*VerifyFunc)(Backtrace*)) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 255 | 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 Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 269 | 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 274 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 275 | ASSERT_TRUE(backtrace.get() != NULL); |
| 276 | if (ReadyFunc(backtrace.get())) { |
| 277 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 278 | verified = true; |
| 279 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 280 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 281 | 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 | |
| 288 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame^] | 292 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 293 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 294 | 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 | |
| 301 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame^] | 305 | _exit(1); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 309 | |
| 310 | kill(pid, SIGKILL); |
| 311 | int status; |
| 312 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 313 | } |
| 314 | |
| 315 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame^] | 319 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 320 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 321 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 322 | |
| 323 | kill(pid, SIGKILL); |
| 324 | int status; |
| 325 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 326 | } |
| 327 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 328 | void 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 332 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 333 | UniquePtr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); |
| 334 | ASSERT_TRUE(ign2.get() != NULL); |
| 335 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 336 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 337 | VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), NULL); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame^] | 344 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 345 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 346 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 347 | |
| 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. |
| 354 | void* PtraceThreadLevelRun(void*) { |
| 355 | EXPECT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | void 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 | |
| 377 | TEST(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 Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame^] | 389 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 390 | } |
| 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 Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 410 | VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 411 | } |
| 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 | |
| 419 | void VerifyLevelThread(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 420 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 421 | ASSERT_TRUE(backtrace.get() != NULL); |
| 422 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 423 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 424 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | TEST(libbacktrace, thread_current_level) { |
| 428 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, NULL), 0); |
| 429 | } |
| 430 | |
| 431 | void VerifyMaxThread(void*) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 432 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 433 | ASSERT_TRUE(backtrace.get() != NULL); |
| 434 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 435 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 436 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | TEST(libbacktrace, thread_current_max) { |
| 440 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, NULL), 0); |
| 441 | } |
| 442 | |
| 443 | void* 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 | |
| 451 | TEST(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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 467 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 468 | ASSERT_TRUE(backtrace.get() != NULL); |
| 469 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 470 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 471 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 472 | |
| 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 | |
| 483 | TEST(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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 495 | UniquePtr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid)); |
| 496 | ASSERT_TRUE(all.get() != NULL); |
| 497 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 498 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 499 | UniquePtr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid)); |
| 500 | ASSERT_TRUE(ign1.get() != NULL); |
| 501 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 502 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 503 | UniquePtr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid)); |
| 504 | ASSERT_TRUE(ign2.get() != NULL); |
| 505 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 506 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 507 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), NULL); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 508 | |
| 509 | // Tell the thread to exit its infinite loop. |
| 510 | android_atomic_acquire_store(0, &thread_data.state); |
| 511 | } |
| 512 | |
| 513 | void* 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 | |
| 521 | TEST(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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 533 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 534 | ASSERT_TRUE(backtrace.get() != NULL); |
| 535 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 536 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 537 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 538 | |
| 539 | // Tell the thread to exit its infinite loop. |
| 540 | android_atomic_acquire_store(0, &thread_data.state); |
| 541 | } |
| 542 | |
| 543 | void* 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 551 | // The status of the actual unwind will be checked elsewhere. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 552 | dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid); |
| 553 | dump->backtrace->Unwind(0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 554 | |
| 555 | android_atomic_acquire_store(1, &dump->done); |
| 556 | |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | TEST(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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 601 | ASSERT_TRUE(dumpers[i].backtrace != NULL); |
| 602 | VerifyMaxDump(dumpers[i].backtrace); |
| 603 | |
| 604 | delete dumpers[i].backtrace; |
| 605 | dumpers[i].backtrace = NULL; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 609 | // 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. |
| 611 | TEST(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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 632 | TEST(libbacktrace, format_test) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 633 | UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD)); |
| 634 | ASSERT_TRUE(backtrace.get() != NULL); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 635 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 636 | backtrace_frame_data_t frame; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 637 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 643 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 644 | backtrace_map_t map; |
| 645 | map.start = 0; |
| 646 | map.end = 0; |
| 647 | |
| 648 | // Check no map set. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 649 | frame.num = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 650 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 651 | EXPECT_EQ("#01 pc 0000000000000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 652 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 653 | EXPECT_EQ("#01 pc 00000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 654 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 655 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 656 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 657 | // Check map name empty, but exists. |
| 658 | frame.map = ↦ |
| 659 | map.start = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 660 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 661 | EXPECT_EQ("#01 pc 0000000000000001 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 662 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 663 | EXPECT_EQ("#01 pc 00000001 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 664 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 665 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 666 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 667 | |
| 668 | // Check relative pc is set and map name is set. |
| 669 | frame.pc = 0x12345679; |
| 670 | frame.map = ↦ |
| 671 | map.name = "MapFake"; |
| 672 | map.start = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 673 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 674 | EXPECT_EQ("#01 pc 0000000012345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 675 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 676 | EXPECT_EQ("#01 pc 12345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 677 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 678 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 679 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 680 | // 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 Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 690 | frame.func_offset = 645; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 691 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 692 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 693 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 694 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 695 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 696 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 697 | } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame^] | 698 | |
| 699 | struct map_test_t { |
| 700 | uintptr_t start; |
| 701 | uintptr_t end; |
| 702 | }; |
| 703 | |
| 704 | bool map_sort(map_test_t i, map_test_t j) { |
| 705 | return i.start < j.start; |
| 706 | } |
| 707 | |
| 708 | static 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 | |
| 736 | TEST(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 | |
| 765 | static 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 | |
| 788 | TEST(libbacktrace, check_for_leak_local) { |
| 789 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD); |
| 790 | } |
| 791 | |
| 792 | TEST(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 | |
| 808 | TEST(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 |