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