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 | |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 17 | #define _GNU_SOURCE 1 |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 18 | #include <dirent.h> |
| 19 | #include <errno.h> |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
| 22 | #include <signal.h> |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 23 | #include <stdint.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/ptrace.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <time.h> |
| 31 | #include <unistd.h> |
| 32 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 33 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 34 | #include <backtrace/BacktraceMap.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. |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 37 | #include "BacktraceCurrent.h" |
Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 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 | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 43 | #include <memory> |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 44 | #include <string> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 45 | #include <vector> |
| 46 | |
| 47 | #include "thread_utils.h" |
| 48 | |
| 49 | // Number of microseconds per milliseconds. |
| 50 | #define US_PER_MSEC 1000 |
| 51 | |
| 52 | // Number of nanoseconds in a second. |
| 53 | #define NS_PER_SEC 1000000000ULL |
| 54 | |
| 55 | // Number of simultaneous dumping operations to perform. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 56 | #define NUM_THREADS 40 |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 57 | |
| 58 | // Number of simultaneous threads running in our forked process. |
| 59 | #define NUM_PTRACE_THREADS 5 |
| 60 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 61 | struct thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 62 | pid_t tid; |
| 63 | int32_t state; |
| 64 | pthread_t threadId; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 65 | void* data; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 66 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 67 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 68 | struct dump_thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 69 | thread_t thread; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 70 | Backtrace* backtrace; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 71 | int32_t* now; |
| 72 | int32_t done; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 73 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 74 | |
| 75 | extern "C" { |
| 76 | // Prototypes for functions in the test library. |
| 77 | int test_level_one(int, int, int, int, void (*)(void*), void*); |
| 78 | |
| 79 | int test_recursive_call(int, void (*)(void*), void*); |
| 80 | } |
| 81 | |
| 82 | uint64_t NanoTime() { |
| 83 | struct timespec t = { 0, 0 }; |
| 84 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 85 | return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec); |
| 86 | } |
| 87 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 88 | std::string DumpFrames(Backtrace* backtrace) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 89 | if (backtrace->NumFrames() == 0) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 90 | return " No frames to dump\n"; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 93 | std::string frame; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 94 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 95 | frame += " " + backtrace->FormatFrameData(i) + '\n'; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 96 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 97 | return frame; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void WaitForStop(pid_t pid) { |
| 101 | uint64_t start = NanoTime(); |
| 102 | |
| 103 | siginfo_t si; |
| 104 | while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) { |
| 105 | if ((NanoTime() - start) > NS_PER_SEC) { |
| 106 | printf("The process did not get to a stopping point in 1 second.\n"); |
| 107 | break; |
| 108 | } |
| 109 | usleep(US_PER_MSEC); |
| 110 | } |
| 111 | } |
| 112 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 113 | bool ReadyLevelBacktrace(Backtrace* backtrace) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 114 | // See if test_level_four is in the backtrace. |
| 115 | bool found = false; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 116 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { |
| 117 | if (it->func_name == "test_level_four") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 118 | found = true; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return found; |
| 124 | } |
| 125 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 126 | void VerifyLevelDump(Backtrace* backtrace) { |
| 127 | ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0)); |
| 128 | ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 129 | |
| 130 | // Look through the frames starting at the highest to find the |
| 131 | // frame we want. |
| 132 | size_t frame_num = 0; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 133 | for (size_t i = backtrace->NumFrames()-1; i > 2; i--) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 134 | if (backtrace->GetFrame(i)->func_name == "test_level_one") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 135 | frame_num = i; |
| 136 | break; |
| 137 | } |
| 138 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 139 | ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace); |
| 140 | ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 141 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 142 | ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one"); |
| 143 | ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two"); |
| 144 | ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three"); |
| 145 | ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void VerifyLevelBacktrace(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 149 | std::unique_ptr<Backtrace> backtrace( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 150 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 151 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 152 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 153 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 154 | VerifyLevelDump(backtrace.get()); |
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 | bool ReadyMaxBacktrace(Backtrace* backtrace) { |
| 158 | return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 161 | void VerifyMaxDump(Backtrace* backtrace) { |
| 162 | ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 163 | // Verify that the last frame is our recursive call. |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 164 | ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, |
| 165 | "test_recursive_call"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void VerifyMaxBacktrace(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 169 | std::unique_ptr<Backtrace> backtrace( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 170 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 171 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 172 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 173 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 174 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void ThreadSetState(void* data) { |
| 178 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 179 | android_atomic_acquire_store(1, &thread->state); |
| 180 | volatile int i = 0; |
| 181 | while (thread->state) { |
| 182 | i++; |
| 183 | } |
| 184 | } |
| 185 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 186 | void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 187 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid)); |
| 188 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 189 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 190 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 191 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | bool WaitForNonZero(int32_t* value, uint64_t seconds) { |
| 195 | uint64_t start = NanoTime(); |
| 196 | do { |
| 197 | if (android_atomic_acquire_load(value)) { |
| 198 | return true; |
| 199 | } |
| 200 | } while ((NanoTime() - start) < seconds * NS_PER_SEC); |
| 201 | return false; |
| 202 | } |
| 203 | |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 204 | TEST(libbacktrace, local_no_unwind_frames) { |
| 205 | // Verify that a local unwind does not include any frames within |
| 206 | // libunwind or libbacktrace. |
| 207 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid())); |
| 208 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 209 | |
| 210 | ASSERT_TRUE(backtrace->NumFrames() != 0); |
| 211 | for (const auto& frame : *backtrace ) { |
| 212 | if (BacktraceMap::IsValid(frame.map)) { |
| 213 | const std::string name = basename(frame.map.name.c_str()); |
| 214 | ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so") |
| 215 | << DumpFrames(backtrace.get()); |
| 216 | } |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 221 | TEST(libbacktrace, local_trace) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 222 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void VerifyIgnoreFrames( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 226 | Backtrace* bt_all, Backtrace* bt_ign1, |
| 227 | Backtrace* bt_ign2, const char* cur_proc) { |
| 228 | EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1); |
| 229 | EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 230 | |
| 231 | // Check all of the frames are the same > the current frame. |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 232 | bool check = (cur_proc == nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 233 | for (size_t i = 0; i < bt_ign2->NumFrames(); i++) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 234 | if (check) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 235 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc); |
| 236 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp); |
| 237 | 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] | 238 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 239 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc); |
| 240 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp); |
| 241 | 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] | 242 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 243 | if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 244 | check = true; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void VerifyLevelIgnoreFrames(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 250 | std::unique_ptr<Backtrace> all( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 251 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 252 | ASSERT_TRUE(all.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 253 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 254 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 255 | std::unique_ptr<Backtrace> ign1( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 256 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 257 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 258 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 259 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 260 | std::unique_ptr<Backtrace> ign2( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 261 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 262 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 263 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 264 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 265 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | TEST(libbacktrace, local_trace_ignore_frames) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 269 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | TEST(libbacktrace, local_max_trace) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 273 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 276 | void VerifyProcTest(pid_t pid, pid_t tid, bool share_map, |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 277 | bool (*ReadyFunc)(Backtrace*), |
| 278 | void (*VerifyFunc)(Backtrace*)) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 279 | pid_t ptrace_tid; |
| 280 | if (tid < 0) { |
| 281 | ptrace_tid = pid; |
| 282 | } else { |
| 283 | ptrace_tid = tid; |
| 284 | } |
| 285 | uint64_t start = NanoTime(); |
| 286 | bool verified = false; |
| 287 | do { |
| 288 | usleep(US_PER_MSEC); |
| 289 | if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) { |
| 290 | // Wait for the process to get to a stopping point. |
| 291 | WaitForStop(ptrace_tid); |
| 292 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 293 | std::unique_ptr<BacktraceMap> map; |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 294 | if (share_map) { |
| 295 | map.reset(BacktraceMap::Create(pid)); |
| 296 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 297 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get())); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 298 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 299 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 300 | if (ReadyFunc(backtrace.get())) { |
| 301 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 302 | verified = true; |
| 303 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 304 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 305 | ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0); |
| 306 | } |
| 307 | // If 5 seconds have passed, then we are done. |
| 308 | } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC); |
| 309 | ASSERT_TRUE(verified); |
| 310 | } |
| 311 | |
| 312 | TEST(libbacktrace, ptrace_trace) { |
| 313 | pid_t pid; |
| 314 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 315 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 316 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 317 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 318 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump); |
| 319 | |
| 320 | kill(pid, SIGKILL); |
| 321 | int status; |
| 322 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 323 | } |
| 324 | |
| 325 | TEST(libbacktrace, ptrace_trace_shared_map) { |
| 326 | pid_t pid; |
| 327 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 328 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 329 | _exit(1); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 333 | |
| 334 | kill(pid, SIGKILL); |
| 335 | int status; |
| 336 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 337 | } |
| 338 | |
| 339 | TEST(libbacktrace, ptrace_max_trace) { |
| 340 | pid_t pid; |
| 341 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 342 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 343 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 344 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 345 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 346 | |
| 347 | kill(pid, SIGKILL); |
| 348 | int status; |
| 349 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 350 | } |
| 351 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 352 | void VerifyProcessIgnoreFrames(Backtrace* bt_all) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 353 | std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); |
| 354 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 355 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 356 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 357 | std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); |
| 358 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 359 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 360 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 361 | VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST(libbacktrace, ptrace_ignore_frames) { |
| 365 | pid_t pid; |
| 366 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 367 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 368 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 369 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 370 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 371 | |
| 372 | kill(pid, SIGKILL); |
| 373 | int status; |
| 374 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 375 | } |
| 376 | |
| 377 | // Create a process with multiple threads and dump all of the threads. |
| 378 | void* PtraceThreadLevelRun(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 379 | EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
| 380 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void GetThreads(pid_t pid, std::vector<pid_t>* threads) { |
| 384 | // Get the list of tasks. |
| 385 | char task_path[128]; |
| 386 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); |
| 387 | |
| 388 | DIR* tasks_dir = opendir(task_path); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 389 | ASSERT_TRUE(tasks_dir != nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 390 | struct dirent* entry; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 391 | while ((entry = readdir(tasks_dir)) != nullptr) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 392 | char* end; |
| 393 | pid_t tid = strtoul(entry->d_name, &end, 10); |
| 394 | if (*end == '\0') { |
| 395 | threads->push_back(tid); |
| 396 | } |
| 397 | } |
| 398 | closedir(tasks_dir); |
| 399 | } |
| 400 | |
| 401 | TEST(libbacktrace, ptrace_threads) { |
| 402 | pid_t pid; |
| 403 | if ((pid = fork()) == 0) { |
| 404 | for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) { |
| 405 | pthread_attr_t attr; |
| 406 | pthread_attr_init(&attr); |
| 407 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 408 | |
| 409 | pthread_t thread; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 410 | ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 411 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 412 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 413 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Check to see that all of the threads are running before unwinding. |
| 417 | std::vector<pid_t> threads; |
| 418 | uint64_t start = NanoTime(); |
| 419 | do { |
| 420 | usleep(US_PER_MSEC); |
| 421 | threads.clear(); |
| 422 | GetThreads(pid, &threads); |
| 423 | } while ((threads.size() != NUM_PTRACE_THREADS + 1) && |
| 424 | ((NanoTime() - start) <= 5 * NS_PER_SEC)); |
| 425 | ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1)); |
| 426 | |
| 427 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 428 | WaitForStop(pid); |
| 429 | for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) { |
| 430 | // Skip the current forked process, we only care about the threads. |
| 431 | if (pid == *it) { |
| 432 | continue; |
| 433 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 434 | VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 435 | } |
| 436 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 437 | |
| 438 | kill(pid, SIGKILL); |
| 439 | int status; |
| 440 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 441 | } |
| 442 | |
| 443 | void VerifyLevelThread(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 444 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 445 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 446 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 447 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 448 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | TEST(libbacktrace, thread_current_level) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 452 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void VerifyMaxThread(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 456 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 457 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 458 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 459 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 460 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | TEST(libbacktrace, thread_current_max) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 464 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | void* ThreadLevelRun(void* data) { |
| 468 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 469 | |
| 470 | thread->tid = gettid(); |
| 471 | EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 472 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | TEST(libbacktrace, thread_level_trace) { |
| 476 | pthread_attr_t attr; |
| 477 | pthread_attr_init(&attr); |
| 478 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 479 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 480 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 481 | pthread_t thread; |
| 482 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); |
| 483 | |
| 484 | // Wait up to 2 seconds for the tid to be set. |
| 485 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 486 | |
Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 487 | // Make sure that the thread signal used is not visible when compiled for |
| 488 | // the target. |
| 489 | #if !defined(__GLIBC__) |
| 490 | ASSERT_LT(THREAD_SIGNAL, SIGRTMIN); |
| 491 | #endif |
| 492 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 493 | // Save the current signal action and make sure it is restored afterwards. |
| 494 | struct sigaction cur_action; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 495 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 496 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 497 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 498 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 499 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 500 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 501 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 502 | |
| 503 | // Tell the thread to exit its infinite loop. |
| 504 | android_atomic_acquire_store(0, &thread_data.state); |
| 505 | |
| 506 | // Verify that the old action was restored. |
| 507 | struct sigaction new_action; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 508 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 509 | EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction); |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 510 | // The SA_RESTORER flag gets set behind our back, so a direct comparison |
| 511 | // doesn't work unless we mask the value off. Mips doesn't have this |
| 512 | // flag, so skip this on that platform. |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 513 | #if defined(SA_RESTORER) |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 514 | cur_action.sa_flags &= ~SA_RESTORER; |
| 515 | new_action.sa_flags &= ~SA_RESTORER; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 516 | #elif defined(__GLIBC__) |
| 517 | // Our host compiler doesn't appear to define this flag for some reason. |
| 518 | cur_action.sa_flags &= ~0x04000000; |
| 519 | new_action.sa_flags &= ~0x04000000; |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 520 | #endif |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 521 | EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags); |
| 522 | } |
| 523 | |
| 524 | TEST(libbacktrace, thread_ignore_frames) { |
| 525 | pthread_attr_t attr; |
| 526 | pthread_attr_init(&attr); |
| 527 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 528 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 529 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 530 | pthread_t thread; |
| 531 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); |
| 532 | |
| 533 | // Wait up to 2 seconds for the tid to be set. |
| 534 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 535 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 536 | std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid)); |
| 537 | ASSERT_TRUE(all.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 538 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 539 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 540 | std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid)); |
| 541 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 542 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 543 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 544 | std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid)); |
| 545 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 546 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 547 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 548 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 549 | |
| 550 | // Tell the thread to exit its infinite loop. |
| 551 | android_atomic_acquire_store(0, &thread_data.state); |
| 552 | } |
| 553 | |
| 554 | void* ThreadMaxRun(void* data) { |
| 555 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 556 | |
| 557 | thread->tid = gettid(); |
| 558 | EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 559 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | TEST(libbacktrace, thread_max_trace) { |
| 563 | pthread_attr_t attr; |
| 564 | pthread_attr_init(&attr); |
| 565 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 566 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 567 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 568 | pthread_t thread; |
| 569 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0); |
| 570 | |
| 571 | // Wait for the tid to be set. |
| 572 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 573 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 574 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 575 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 576 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 577 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 578 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 579 | |
| 580 | // Tell the thread to exit its infinite loop. |
| 581 | android_atomic_acquire_store(0, &thread_data.state); |
| 582 | } |
| 583 | |
| 584 | void* ThreadDump(void* data) { |
| 585 | dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data); |
| 586 | while (true) { |
| 587 | if (android_atomic_acquire_load(dump->now)) { |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 592 | // The status of the actual unwind will be checked elsewhere. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 593 | dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid); |
| 594 | dump->backtrace->Unwind(0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 595 | |
| 596 | android_atomic_acquire_store(1, &dump->done); |
| 597 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 598 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | TEST(libbacktrace, thread_multiple_dump) { |
| 602 | // Dump NUM_THREADS simultaneously. |
| 603 | std::vector<thread_t> runners(NUM_THREADS); |
| 604 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 605 | |
| 606 | pthread_attr_t attr; |
| 607 | pthread_attr_init(&attr); |
| 608 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 609 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 610 | // Launch the runners, they will spin in hard loops doing nothing. |
| 611 | runners[i].tid = 0; |
| 612 | runners[i].state = 0; |
| 613 | ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0); |
| 614 | } |
| 615 | |
| 616 | // Wait for tids to be set. |
| 617 | 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] | 618 | ASSERT_TRUE(WaitForNonZero(&it->state, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // Start all of the dumpers at once, they will spin until they are signalled |
| 622 | // to begin their dump run. |
| 623 | int32_t dump_now = 0; |
| 624 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 625 | dumpers[i].thread.tid = runners[i].tid; |
| 626 | dumpers[i].thread.state = 0; |
| 627 | dumpers[i].done = 0; |
| 628 | dumpers[i].now = &dump_now; |
| 629 | |
| 630 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 631 | } |
| 632 | |
| 633 | // Start all of the dumpers going at once. |
| 634 | android_atomic_acquire_store(1, &dump_now); |
| 635 | |
| 636 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 637 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 638 | |
| 639 | // Tell the runner thread to exit its infinite loop. |
| 640 | android_atomic_acquire_store(0, &runners[i].state); |
| 641 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 642 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 643 | VerifyMaxDump(dumpers[i].backtrace); |
| 644 | |
| 645 | delete dumpers[i].backtrace; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 646 | dumpers[i].backtrace = nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 650 | TEST(libbacktrace, thread_multiple_dump_same_thread) { |
| 651 | pthread_attr_t attr; |
| 652 | pthread_attr_init(&attr); |
| 653 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 654 | thread_t runner; |
| 655 | runner.tid = 0; |
| 656 | runner.state = 0; |
| 657 | ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0); |
| 658 | |
| 659 | // Wait for tids to be set. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 660 | ASSERT_TRUE(WaitForNonZero(&runner.state, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 661 | |
| 662 | // Start all of the dumpers at once, they will spin until they are signalled |
| 663 | // to begin their dump run. |
| 664 | int32_t dump_now = 0; |
| 665 | // Dump the same thread NUM_THREADS simultaneously. |
| 666 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 667 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 668 | dumpers[i].thread.tid = runner.tid; |
| 669 | dumpers[i].thread.state = 0; |
| 670 | dumpers[i].done = 0; |
| 671 | dumpers[i].now = &dump_now; |
| 672 | |
| 673 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 674 | } |
| 675 | |
| 676 | // Start all of the dumpers going at once. |
| 677 | android_atomic_acquire_store(1, &dump_now); |
| 678 | |
| 679 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 680 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 681 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 682 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 683 | VerifyMaxDump(dumpers[i].backtrace); |
| 684 | |
| 685 | delete dumpers[i].backtrace; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 686 | dumpers[i].backtrace = nullptr; |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | // Tell the runner thread to exit its infinite loop. |
| 690 | android_atomic_acquire_store(0, &runner.state); |
| 691 | } |
| 692 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 693 | // This test is for UnwindMaps that should share the same map cursor when |
| 694 | // multiple maps are created for the current process at the same time. |
| 695 | TEST(libbacktrace, simultaneous_maps) { |
| 696 | BacktraceMap* map1 = BacktraceMap::Create(getpid()); |
| 697 | BacktraceMap* map2 = BacktraceMap::Create(getpid()); |
| 698 | BacktraceMap* map3 = BacktraceMap::Create(getpid()); |
| 699 | |
| 700 | Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1); |
| 701 | EXPECT_TRUE(back1->Unwind(0)); |
| 702 | delete back1; |
| 703 | delete map1; |
| 704 | |
| 705 | Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2); |
| 706 | EXPECT_TRUE(back2->Unwind(0)); |
| 707 | delete back2; |
| 708 | delete map2; |
| 709 | |
| 710 | Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3); |
| 711 | EXPECT_TRUE(back3->Unwind(0)); |
| 712 | delete back3; |
| 713 | delete map3; |
| 714 | } |
| 715 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 716 | TEST(libbacktrace, fillin_erases) { |
| 717 | BacktraceMap* back_map = BacktraceMap::Create(getpid()); |
| 718 | |
| 719 | backtrace_map_t map; |
| 720 | |
| 721 | map.start = 1; |
| 722 | map.end = 3; |
| 723 | map.flags = 1; |
| 724 | map.name = "Initialized"; |
| 725 | back_map->FillIn(0, &map); |
| 726 | delete back_map; |
| 727 | |
| 728 | ASSERT_FALSE(BacktraceMap::IsValid(map)); |
| 729 | ASSERT_EQ(static_cast<uintptr_t>(0), map.start); |
| 730 | ASSERT_EQ(static_cast<uintptr_t>(0), map.end); |
| 731 | ASSERT_EQ(0, map.flags); |
| 732 | ASSERT_EQ("", map.name); |
| 733 | } |
| 734 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 735 | TEST(libbacktrace, format_test) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 736 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD)); |
| 737 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 738 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 739 | backtrace_frame_data_t frame; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 740 | frame.num = 1; |
| 741 | frame.pc = 2; |
| 742 | frame.sp = 0; |
| 743 | frame.stack_size = 0; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 744 | frame.func_offset = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 745 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 746 | // Check no map set. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 747 | frame.num = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 748 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 749 | EXPECT_EQ("#01 pc 0000000000000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 750 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 751 | EXPECT_EQ("#01 pc 00000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 752 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 753 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 754 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 755 | // Check map name empty, but exists. |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 756 | frame.map.start = 1; |
| 757 | frame.map.end = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 758 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 759 | EXPECT_EQ("#01 pc 0000000000000001 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 760 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 761 | EXPECT_EQ("#01 pc 00000001 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 762 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 763 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 764 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 765 | |
| 766 | // Check relative pc is set and map name is set. |
| 767 | frame.pc = 0x12345679; |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 768 | frame.map.name = "MapFake"; |
| 769 | frame.map.start = 1; |
| 770 | frame.map.end = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 771 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 772 | EXPECT_EQ("#01 pc 0000000012345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 773 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 774 | EXPECT_EQ("#01 pc 12345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 775 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 776 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 777 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 778 | // Check func_name is set, but no func offset. |
| 779 | frame.func_name = "ProcFake"; |
| 780 | #if defined(__LP64__) |
| 781 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)", |
| 782 | #else |
| 783 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)", |
| 784 | #endif |
| 785 | backtrace->FormatFrameData(&frame)); |
| 786 | |
| 787 | // Check func_name is set, and func offset is non-zero. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 788 | frame.func_offset = 645; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 789 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 790 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 791 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 792 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 793 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 794 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 795 | } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 796 | |
| 797 | struct map_test_t { |
| 798 | uintptr_t start; |
| 799 | uintptr_t end; |
| 800 | }; |
| 801 | |
| 802 | bool map_sort(map_test_t i, map_test_t j) { |
| 803 | return i.start < j.start; |
| 804 | } |
| 805 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 806 | void VerifyMap(pid_t pid) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 807 | char buffer[4096]; |
| 808 | snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid); |
| 809 | |
| 810 | FILE* map_file = fopen(buffer, "r"); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 811 | ASSERT_TRUE(map_file != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 812 | std::vector<map_test_t> test_maps; |
| 813 | while (fgets(buffer, sizeof(buffer), map_file)) { |
| 814 | map_test_t map; |
| 815 | ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end)); |
| 816 | test_maps.push_back(map); |
| 817 | } |
| 818 | fclose(map_file); |
| 819 | std::sort(test_maps.begin(), test_maps.end(), map_sort); |
| 820 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 821 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid)); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 822 | |
| 823 | // Basic test that verifies that the map is in the expected order. |
| 824 | std::vector<map_test_t>::const_iterator test_it = test_maps.begin(); |
| 825 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { |
| 826 | ASSERT_TRUE(test_it != test_maps.end()); |
| 827 | ASSERT_EQ(test_it->start, it->start); |
| 828 | ASSERT_EQ(test_it->end, it->end); |
| 829 | ++test_it; |
| 830 | } |
| 831 | ASSERT_TRUE(test_it == test_maps.end()); |
| 832 | } |
| 833 | |
| 834 | TEST(libbacktrace, verify_map_remote) { |
| 835 | pid_t pid; |
| 836 | |
| 837 | if ((pid = fork()) == 0) { |
| 838 | while (true) { |
| 839 | } |
| 840 | _exit(0); |
| 841 | } |
| 842 | ASSERT_LT(0, pid); |
| 843 | |
| 844 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 845 | |
| 846 | // Wait for the process to get to a stopping point. |
| 847 | WaitForStop(pid); |
| 848 | |
| 849 | // The maps should match exactly since the forked process has been paused. |
| 850 | VerifyMap(pid); |
| 851 | |
| 852 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 853 | |
| 854 | kill(pid, SIGKILL); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 855 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 856 | } |
| 857 | |
| 858 | void* ThreadReadTest(void* data) { |
| 859 | thread_t* thread_data = reinterpret_cast<thread_t*>(data); |
| 860 | |
| 861 | thread_data->tid = gettid(); |
| 862 | |
| 863 | // Create two map pages. |
| 864 | // Mark the second page as not-readable. |
| 865 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 866 | uint8_t* memory; |
| 867 | if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) { |
| 868 | return reinterpret_cast<void*>(-1); |
| 869 | } |
| 870 | |
| 871 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { |
| 872 | return reinterpret_cast<void*>(-1); |
| 873 | } |
| 874 | |
| 875 | // Set up a simple pattern in memory. |
| 876 | for (size_t i = 0; i < pagesize; i++) { |
| 877 | memory[i] = i; |
| 878 | } |
| 879 | |
| 880 | thread_data->data = memory; |
| 881 | |
| 882 | // Tell the caller it's okay to start reading memory. |
| 883 | android_atomic_acquire_store(1, &thread_data->state); |
| 884 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 885 | // Loop waiting for the caller to finish reading the memory. |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 886 | while (thread_data->state) { |
| 887 | } |
| 888 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 889 | // Re-enable read-write on the page so that we don't crash if we try |
| 890 | // and access data on this page when freeing the memory. |
| 891 | if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) { |
| 892 | return reinterpret_cast<void*>(-1); |
| 893 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 894 | free(memory); |
| 895 | |
| 896 | android_atomic_acquire_store(1, &thread_data->state); |
| 897 | |
| 898 | return nullptr; |
| 899 | } |
| 900 | |
| 901 | void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) { |
| 902 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 903 | |
| 904 | // Create a page of data to use to do quick compares. |
| 905 | uint8_t* expected = new uint8_t[pagesize]; |
| 906 | for (size_t i = 0; i < pagesize; i++) { |
| 907 | expected[i] = i; |
| 908 | } |
| 909 | uint8_t* data = new uint8_t[2*pagesize]; |
| 910 | // Verify that we can only read one page worth of data. |
| 911 | size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize); |
| 912 | ASSERT_EQ(pagesize, bytes_read); |
| 913 | ASSERT_TRUE(memcmp(data, expected, pagesize) == 0); |
| 914 | |
| 915 | // Verify unaligned reads. |
| 916 | for (size_t i = 1; i < sizeof(word_t); i++) { |
| 917 | bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t)); |
| 918 | ASSERT_EQ(2 * sizeof(word_t), bytes_read); |
| 919 | ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0) |
| 920 | << "Offset at " << i << " failed"; |
| 921 | } |
| 922 | delete data; |
| 923 | delete expected; |
| 924 | } |
| 925 | |
| 926 | TEST(libbacktrace, thread_read) { |
| 927 | pthread_attr_t attr; |
| 928 | pthread_attr_init(&attr); |
| 929 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 930 | pthread_t thread; |
| 931 | thread_t thread_data = { 0, 0, 0, nullptr }; |
| 932 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0); |
| 933 | |
| 934 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); |
| 935 | |
| 936 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 937 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 938 | |
| 939 | RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data)); |
| 940 | |
| 941 | android_atomic_acquire_store(0, &thread_data.state); |
| 942 | |
| 943 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); |
| 944 | } |
| 945 | |
| 946 | volatile uintptr_t g_ready = 0; |
| 947 | volatile uintptr_t g_addr = 0; |
| 948 | |
| 949 | void ForkedReadTest() { |
| 950 | // Create two map pages. |
| 951 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 952 | uint8_t* memory; |
| 953 | if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) { |
| 954 | perror("Failed to allocate memory\n"); |
| 955 | exit(1); |
| 956 | } |
| 957 | |
| 958 | // Mark the second page as not-readable. |
| 959 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { |
| 960 | perror("Failed to mprotect memory\n"); |
| 961 | exit(1); |
| 962 | } |
| 963 | |
| 964 | // Set up a simple pattern in memory. |
| 965 | for (size_t i = 0; i < pagesize; i++) { |
| 966 | memory[i] = i; |
| 967 | } |
| 968 | |
| 969 | g_addr = reinterpret_cast<uintptr_t>(memory); |
| 970 | g_ready = 1; |
| 971 | |
| 972 | while (1) { |
| 973 | usleep(US_PER_MSEC); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | TEST(libbacktrace, process_read) { |
| 978 | pid_t pid; |
| 979 | if ((pid = fork()) == 0) { |
| 980 | ForkedReadTest(); |
| 981 | exit(0); |
| 982 | } |
| 983 | ASSERT_NE(-1, pid); |
| 984 | |
| 985 | bool test_executed = false; |
| 986 | uint64_t start = NanoTime(); |
| 987 | while (1) { |
| 988 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) { |
| 989 | WaitForStop(pid); |
| 990 | |
| 991 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid)); |
| 992 | |
| 993 | uintptr_t read_addr; |
| 994 | size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), |
| 995 | reinterpret_cast<uint8_t*>(&read_addr), |
| 996 | sizeof(uintptr_t)); |
| 997 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); |
| 998 | if (read_addr) { |
| 999 | // The forked process is ready to be read. |
| 1000 | bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), |
| 1001 | reinterpret_cast<uint8_t*>(&read_addr), |
| 1002 | sizeof(uintptr_t)); |
| 1003 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); |
| 1004 | |
| 1005 | RunReadTest(backtrace.get(), read_addr); |
| 1006 | |
| 1007 | test_executed = true; |
| 1008 | break; |
| 1009 | } |
| 1010 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1011 | } |
| 1012 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { |
| 1013 | break; |
| 1014 | } |
| 1015 | usleep(US_PER_MSEC); |
| 1016 | } |
| 1017 | kill(pid, SIGKILL); |
| 1018 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 1019 | |
| 1020 | ASSERT_TRUE(test_executed); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | #if defined(ENABLE_PSS_TESTS) |
| 1024 | #include "GetPss.h" |
| 1025 | |
| 1026 | #define MAX_LEAK_BYTES 32*1024UL |
| 1027 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1028 | void CheckForLeak(pid_t pid, pid_t tid) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1029 | // Do a few runs to get the PSS stable. |
| 1030 | for (size_t i = 0; i < 100; i++) { |
| 1031 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1032 | ASSERT_TRUE(backtrace != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1033 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 1034 | delete backtrace; |
| 1035 | } |
| 1036 | size_t stable_pss = GetPssBytes(); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1037 | ASSERT_TRUE(stable_pss != 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1038 | |
| 1039 | // Loop enough that even a small leak should be detectable. |
| 1040 | for (size_t i = 0; i < 4096; i++) { |
| 1041 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1042 | ASSERT_TRUE(backtrace != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1043 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 1044 | delete backtrace; |
| 1045 | } |
| 1046 | size_t new_pss = GetPssBytes(); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1047 | ASSERT_TRUE(new_pss != 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1048 | size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss; |
| 1049 | // As long as the new pss is within a certain amount, consider everything okay. |
| 1050 | ASSERT_LE(abs_diff, MAX_LEAK_BYTES); |
| 1051 | } |
| 1052 | |
| 1053 | TEST(libbacktrace, check_for_leak_local) { |
| 1054 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD); |
| 1055 | } |
| 1056 | |
| 1057 | TEST(libbacktrace, check_for_leak_local_thread) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1058 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1059 | pthread_t thread; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1060 | ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1061 | |
| 1062 | // Wait up to 2 seconds for the tid to be set. |
| 1063 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 1064 | |
| 1065 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid); |
| 1066 | |
| 1067 | // Tell the thread to exit its infinite loop. |
| 1068 | android_atomic_acquire_store(0, &thread_data.state); |
| 1069 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1070 | ASSERT_TRUE(pthread_join(thread, nullptr) == 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | TEST(libbacktrace, check_for_leak_remote) { |
| 1074 | pid_t pid; |
| 1075 | |
| 1076 | if ((pid = fork()) == 0) { |
| 1077 | while (true) { |
| 1078 | } |
| 1079 | _exit(0); |
| 1080 | } |
| 1081 | ASSERT_LT(0, pid); |
| 1082 | |
| 1083 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1084 | |
| 1085 | // Wait for the process to get to a stopping point. |
| 1086 | WaitForStop(pid); |
| 1087 | |
| 1088 | CheckForLeak(pid, BACKTRACE_CURRENT_THREAD); |
| 1089 | |
| 1090 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1091 | |
| 1092 | kill(pid, SIGKILL); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1093 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1094 | } |
| 1095 | #endif |