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> |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 19 | #include <dlfcn.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 22 | #include <inttypes.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
| 24 | #include <signal.h> |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/ptrace.h> |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 30 | #include <sys/stat.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
| 35 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 36 | #include <algorithm> |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 37 | #include <list> |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 38 | #include <memory> |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 39 | #include <string> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
Dan Albert | 23f750b | 2015-04-30 12:52:21 -0700 | [diff] [blame] | 42 | #include <backtrace/Backtrace.h> |
| 43 | #include <backtrace/BacktraceMap.h> |
| 44 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 45 | #include <android-base/stringprintf.h> |
Dan Albert | 23f750b | 2015-04-30 12:52:21 -0700 | [diff] [blame] | 46 | #include <cutils/atomic.h> |
| 47 | #include <cutils/threads.h> |
| 48 | |
| 49 | #include <gtest/gtest.h> |
| 50 | |
| 51 | // For the THREAD_SIGNAL definition. |
| 52 | #include "BacktraceCurrent.h" |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 53 | #include "thread_utils.h" |
| 54 | |
| 55 | // Number of microseconds per milliseconds. |
| 56 | #define US_PER_MSEC 1000 |
| 57 | |
| 58 | // Number of nanoseconds in a second. |
| 59 | #define NS_PER_SEC 1000000000ULL |
| 60 | |
| 61 | // Number of simultaneous dumping operations to perform. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 62 | #define NUM_THREADS 40 |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 63 | |
| 64 | // Number of simultaneous threads running in our forked process. |
| 65 | #define NUM_PTRACE_THREADS 5 |
| 66 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 67 | struct thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 68 | pid_t tid; |
| 69 | int32_t state; |
| 70 | pthread_t threadId; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 71 | void* data; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 72 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 73 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 74 | struct dump_thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 75 | thread_t thread; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 76 | Backtrace* backtrace; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 77 | int32_t* now; |
| 78 | int32_t done; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 79 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 80 | |
| 81 | extern "C" { |
| 82 | // Prototypes for functions in the test library. |
| 83 | int test_level_one(int, int, int, int, void (*)(void*), void*); |
| 84 | |
| 85 | int test_recursive_call(int, void (*)(void*), void*); |
| 86 | } |
| 87 | |
| 88 | uint64_t NanoTime() { |
| 89 | struct timespec t = { 0, 0 }; |
| 90 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 91 | return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec); |
| 92 | } |
| 93 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 94 | std::string DumpFrames(Backtrace* backtrace) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 95 | if (backtrace->NumFrames() == 0) { |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 96 | return " No frames to dump.\n"; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 99 | std::string frame; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 100 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 101 | frame += " " + backtrace->FormatFrameData(i) + '\n'; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 102 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 103 | return frame; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void WaitForStop(pid_t pid) { |
| 107 | uint64_t start = NanoTime(); |
| 108 | |
| 109 | siginfo_t si; |
| 110 | while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) { |
| 111 | if ((NanoTime() - start) > NS_PER_SEC) { |
| 112 | printf("The process did not get to a stopping point in 1 second.\n"); |
| 113 | break; |
| 114 | } |
| 115 | usleep(US_PER_MSEC); |
| 116 | } |
| 117 | } |
| 118 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 119 | bool ReadyLevelBacktrace(Backtrace* backtrace) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 120 | // See if test_level_four is in the backtrace. |
| 121 | bool found = false; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 122 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { |
| 123 | if (it->func_name == "test_level_four") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 124 | found = true; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return found; |
| 130 | } |
| 131 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 132 | void VerifyLevelDump(Backtrace* backtrace) { |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 133 | ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0)) |
| 134 | << DumpFrames(backtrace); |
| 135 | ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) |
| 136 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 137 | |
| 138 | // Look through the frames starting at the highest to find the |
| 139 | // frame we want. |
| 140 | size_t frame_num = 0; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 141 | for (size_t i = backtrace->NumFrames()-1; i > 2; i--) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 142 | if (backtrace->GetFrame(i)->func_name == "test_level_one") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 143 | frame_num = i; |
| 144 | break; |
| 145 | } |
| 146 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 147 | ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace); |
| 148 | ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 149 | |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 150 | ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one") |
| 151 | << DumpFrames(backtrace); |
| 152 | ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two") |
| 153 | << DumpFrames(backtrace); |
| 154 | ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three") |
| 155 | << DumpFrames(backtrace); |
| 156 | ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four") |
| 157 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void VerifyLevelBacktrace(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 161 | std::unique_ptr<Backtrace> backtrace( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 162 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 163 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 164 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 165 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 166 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 169 | bool ReadyMaxBacktrace(Backtrace* backtrace) { |
| 170 | return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 173 | void VerifyMaxDump(Backtrace* backtrace) { |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 174 | ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) |
| 175 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 176 | // Verify that the last frame is our recursive call. |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 177 | ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call") |
| 178 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void VerifyMaxBacktrace(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 182 | std::unique_ptr<Backtrace> backtrace( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 183 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 184 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 185 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 186 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 187 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void ThreadSetState(void* data) { |
| 191 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 192 | android_atomic_acquire_store(1, &thread->state); |
| 193 | volatile int i = 0; |
| 194 | while (thread->state) { |
| 195 | i++; |
| 196 | } |
| 197 | } |
| 198 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 199 | void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 200 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid)); |
| 201 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 202 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 203 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 204 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | bool WaitForNonZero(int32_t* value, uint64_t seconds) { |
| 208 | uint64_t start = NanoTime(); |
| 209 | do { |
| 210 | if (android_atomic_acquire_load(value)) { |
| 211 | return true; |
| 212 | } |
| 213 | } while ((NanoTime() - start) < seconds * NS_PER_SEC); |
| 214 | return false; |
| 215 | } |
| 216 | |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 217 | TEST(libbacktrace, local_no_unwind_frames) { |
| 218 | // Verify that a local unwind does not include any frames within |
| 219 | // libunwind or libbacktrace. |
| 220 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid())); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 221 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 222 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 223 | |
| 224 | ASSERT_TRUE(backtrace->NumFrames() != 0); |
| 225 | for (const auto& frame : *backtrace ) { |
| 226 | if (BacktraceMap::IsValid(frame.map)) { |
| 227 | const std::string name = basename(frame.map.name.c_str()); |
| 228 | ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so") |
| 229 | << DumpFrames(backtrace.get()); |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 235 | TEST(libbacktrace, local_trace) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 236 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void VerifyIgnoreFrames( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 240 | Backtrace* bt_all, Backtrace* bt_ign1, |
| 241 | Backtrace* bt_ign2, const char* cur_proc) { |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 242 | EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1) |
| 243 | << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1); |
| 244 | EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2) |
| 245 | << "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] | 246 | |
| 247 | // Check all of the frames are the same > the current frame. |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 248 | bool check = (cur_proc == nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 249 | for (size_t i = 0; i < bt_ign2->NumFrames(); i++) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 250 | if (check) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 251 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc); |
| 252 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp); |
| 253 | 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] | 254 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 255 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc); |
| 256 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp); |
| 257 | 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] | 258 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 259 | if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 260 | check = true; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void VerifyLevelIgnoreFrames(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 266 | std::unique_ptr<Backtrace> all( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 267 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 268 | ASSERT_TRUE(all.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 269 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 270 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 271 | std::unique_ptr<Backtrace> ign1( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 272 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 273 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 274 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 275 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 276 | std::unique_ptr<Backtrace> ign2( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 277 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 278 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 279 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 280 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 281 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | TEST(libbacktrace, local_trace_ignore_frames) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 285 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | TEST(libbacktrace, local_max_trace) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 289 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 292 | void VerifyProcTest(pid_t pid, pid_t tid, bool share_map, |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 293 | bool (*ReadyFunc)(Backtrace*), |
| 294 | void (*VerifyFunc)(Backtrace*)) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 295 | pid_t ptrace_tid; |
| 296 | if (tid < 0) { |
| 297 | ptrace_tid = pid; |
| 298 | } else { |
| 299 | ptrace_tid = tid; |
| 300 | } |
| 301 | uint64_t start = NanoTime(); |
| 302 | bool verified = false; |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 303 | std::string last_dump; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 304 | do { |
| 305 | usleep(US_PER_MSEC); |
| 306 | if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) { |
| 307 | // Wait for the process to get to a stopping point. |
| 308 | WaitForStop(ptrace_tid); |
| 309 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 310 | std::unique_ptr<BacktraceMap> map; |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 311 | if (share_map) { |
| 312 | map.reset(BacktraceMap::Create(pid)); |
| 313 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 314 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get())); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 315 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 316 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 317 | if (ReadyFunc(backtrace.get())) { |
| 318 | VerifyFunc(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 319 | verified = true; |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 320 | } else { |
| 321 | last_dump = DumpFrames(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 322 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 323 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 324 | ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0); |
| 325 | } |
| 326 | // If 5 seconds have passed, then we are done. |
| 327 | } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 328 | ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | TEST(libbacktrace, ptrace_trace) { |
| 332 | pid_t pid; |
| 333 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 334 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 335 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 336 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 337 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump); |
| 338 | |
| 339 | kill(pid, SIGKILL); |
| 340 | int status; |
| 341 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 342 | } |
| 343 | |
| 344 | TEST(libbacktrace, ptrace_trace_shared_map) { |
| 345 | pid_t pid; |
| 346 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 347 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 348 | _exit(1); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 352 | |
| 353 | kill(pid, SIGKILL); |
| 354 | int status; |
| 355 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 356 | } |
| 357 | |
| 358 | TEST(libbacktrace, ptrace_max_trace) { |
| 359 | pid_t pid; |
| 360 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 361 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 362 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 363 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 364 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 365 | |
| 366 | kill(pid, SIGKILL); |
| 367 | int status; |
| 368 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 369 | } |
| 370 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 371 | void VerifyProcessIgnoreFrames(Backtrace* bt_all) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 372 | std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); |
| 373 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 374 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 375 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 376 | std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); |
| 377 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 378 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 379 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 380 | VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | TEST(libbacktrace, ptrace_ignore_frames) { |
| 384 | pid_t pid; |
| 385 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 386 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 387 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 388 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 389 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 390 | |
| 391 | kill(pid, SIGKILL); |
| 392 | int status; |
| 393 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 394 | } |
| 395 | |
| 396 | // Create a process with multiple threads and dump all of the threads. |
| 397 | void* PtraceThreadLevelRun(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 398 | EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
| 399 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void GetThreads(pid_t pid, std::vector<pid_t>* threads) { |
| 403 | // Get the list of tasks. |
| 404 | char task_path[128]; |
| 405 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); |
| 406 | |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 407 | std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 408 | ASSERT_TRUE(tasks_dir != nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 409 | struct dirent* entry; |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 410 | while ((entry = readdir(tasks_dir.get())) != nullptr) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 411 | char* end; |
| 412 | pid_t tid = strtoul(entry->d_name, &end, 10); |
| 413 | if (*end == '\0') { |
| 414 | threads->push_back(tid); |
| 415 | } |
| 416 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | TEST(libbacktrace, ptrace_threads) { |
| 420 | pid_t pid; |
| 421 | if ((pid = fork()) == 0) { |
| 422 | for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) { |
| 423 | pthread_attr_t attr; |
| 424 | pthread_attr_init(&attr); |
| 425 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 426 | |
| 427 | pthread_t thread; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 428 | ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 429 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 430 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 431 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Check to see that all of the threads are running before unwinding. |
| 435 | std::vector<pid_t> threads; |
| 436 | uint64_t start = NanoTime(); |
| 437 | do { |
| 438 | usleep(US_PER_MSEC); |
| 439 | threads.clear(); |
| 440 | GetThreads(pid, &threads); |
| 441 | } while ((threads.size() != NUM_PTRACE_THREADS + 1) && |
| 442 | ((NanoTime() - start) <= 5 * NS_PER_SEC)); |
| 443 | ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1)); |
| 444 | |
| 445 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 446 | WaitForStop(pid); |
| 447 | for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) { |
| 448 | // Skip the current forked process, we only care about the threads. |
| 449 | if (pid == *it) { |
| 450 | continue; |
| 451 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 452 | VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 453 | } |
| 454 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 455 | |
| 456 | kill(pid, SIGKILL); |
| 457 | int status; |
| 458 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 459 | } |
| 460 | |
| 461 | void VerifyLevelThread(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 462 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 463 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 464 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 465 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 466 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | TEST(libbacktrace, thread_current_level) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 470 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void VerifyMaxThread(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 474 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 475 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 476 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 477 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 478 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | TEST(libbacktrace, thread_current_max) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 482 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | void* ThreadLevelRun(void* data) { |
| 486 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 487 | |
| 488 | thread->tid = gettid(); |
| 489 | EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 490 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | TEST(libbacktrace, thread_level_trace) { |
| 494 | pthread_attr_t attr; |
| 495 | pthread_attr_init(&attr); |
| 496 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 497 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 498 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 499 | pthread_t thread; |
| 500 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); |
| 501 | |
| 502 | // Wait up to 2 seconds for the tid to be set. |
| 503 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 504 | |
Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 505 | // Make sure that the thread signal used is not visible when compiled for |
| 506 | // the target. |
| 507 | #if !defined(__GLIBC__) |
| 508 | ASSERT_LT(THREAD_SIGNAL, SIGRTMIN); |
| 509 | #endif |
| 510 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 511 | // Save the current signal action and make sure it is restored afterwards. |
| 512 | struct sigaction cur_action; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 513 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 514 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 515 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 516 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 517 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 518 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 519 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 520 | |
| 521 | // Tell the thread to exit its infinite loop. |
| 522 | android_atomic_acquire_store(0, &thread_data.state); |
| 523 | |
| 524 | // Verify that the old action was restored. |
| 525 | struct sigaction new_action; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 526 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 527 | EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction); |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 528 | // The SA_RESTORER flag gets set behind our back, so a direct comparison |
| 529 | // doesn't work unless we mask the value off. Mips doesn't have this |
| 530 | // flag, so skip this on that platform. |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 531 | #if defined(SA_RESTORER) |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 532 | cur_action.sa_flags &= ~SA_RESTORER; |
| 533 | new_action.sa_flags &= ~SA_RESTORER; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 534 | #elif defined(__GLIBC__) |
| 535 | // Our host compiler doesn't appear to define this flag for some reason. |
| 536 | cur_action.sa_flags &= ~0x04000000; |
| 537 | new_action.sa_flags &= ~0x04000000; |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 538 | #endif |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 539 | EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags); |
| 540 | } |
| 541 | |
| 542 | TEST(libbacktrace, thread_ignore_frames) { |
| 543 | pthread_attr_t attr; |
| 544 | pthread_attr_init(&attr); |
| 545 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 546 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 547 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 548 | pthread_t thread; |
| 549 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); |
| 550 | |
| 551 | // Wait up to 2 seconds for the tid to be set. |
| 552 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 553 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 554 | std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid)); |
| 555 | ASSERT_TRUE(all.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 556 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 557 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 558 | std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid)); |
| 559 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 560 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 561 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 562 | std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid)); |
| 563 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 564 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 565 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 566 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 567 | |
| 568 | // Tell the thread to exit its infinite loop. |
| 569 | android_atomic_acquire_store(0, &thread_data.state); |
| 570 | } |
| 571 | |
| 572 | void* ThreadMaxRun(void* data) { |
| 573 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 574 | |
| 575 | thread->tid = gettid(); |
| 576 | EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 577 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | TEST(libbacktrace, thread_max_trace) { |
| 581 | pthread_attr_t attr; |
| 582 | pthread_attr_init(&attr); |
| 583 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 584 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 585 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 586 | pthread_t thread; |
| 587 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0); |
| 588 | |
| 589 | // Wait for the tid to be set. |
| 590 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 591 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 592 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 593 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 594 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 595 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 596 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 597 | |
| 598 | // Tell the thread to exit its infinite loop. |
| 599 | android_atomic_acquire_store(0, &thread_data.state); |
| 600 | } |
| 601 | |
| 602 | void* ThreadDump(void* data) { |
| 603 | dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data); |
| 604 | while (true) { |
| 605 | if (android_atomic_acquire_load(dump->now)) { |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 610 | // The status of the actual unwind will be checked elsewhere. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 611 | dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid); |
| 612 | dump->backtrace->Unwind(0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 613 | |
| 614 | android_atomic_acquire_store(1, &dump->done); |
| 615 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 616 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | TEST(libbacktrace, thread_multiple_dump) { |
| 620 | // Dump NUM_THREADS simultaneously. |
| 621 | std::vector<thread_t> runners(NUM_THREADS); |
| 622 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 623 | |
| 624 | pthread_attr_t attr; |
| 625 | pthread_attr_init(&attr); |
| 626 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 627 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 628 | // Launch the runners, they will spin in hard loops doing nothing. |
| 629 | runners[i].tid = 0; |
| 630 | runners[i].state = 0; |
| 631 | ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0); |
| 632 | } |
| 633 | |
| 634 | // Wait for tids to be set. |
| 635 | 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] | 636 | ASSERT_TRUE(WaitForNonZero(&it->state, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // Start all of the dumpers at once, they will spin until they are signalled |
| 640 | // to begin their dump run. |
| 641 | int32_t dump_now = 0; |
| 642 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 643 | dumpers[i].thread.tid = runners[i].tid; |
| 644 | dumpers[i].thread.state = 0; |
| 645 | dumpers[i].done = 0; |
| 646 | dumpers[i].now = &dump_now; |
| 647 | |
| 648 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 649 | } |
| 650 | |
| 651 | // Start all of the dumpers going at once. |
| 652 | android_atomic_acquire_store(1, &dump_now); |
| 653 | |
| 654 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 655 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 656 | |
| 657 | // Tell the runner thread to exit its infinite loop. |
| 658 | android_atomic_acquire_store(0, &runners[i].state); |
| 659 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 660 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 661 | VerifyMaxDump(dumpers[i].backtrace); |
| 662 | |
| 663 | delete dumpers[i].backtrace; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 664 | dumpers[i].backtrace = nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 668 | TEST(libbacktrace, thread_multiple_dump_same_thread) { |
| 669 | pthread_attr_t attr; |
| 670 | pthread_attr_init(&attr); |
| 671 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 672 | thread_t runner; |
| 673 | runner.tid = 0; |
| 674 | runner.state = 0; |
| 675 | ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0); |
| 676 | |
| 677 | // Wait for tids to be set. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 678 | ASSERT_TRUE(WaitForNonZero(&runner.state, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 679 | |
| 680 | // Start all of the dumpers at once, they will spin until they are signalled |
| 681 | // to begin their dump run. |
| 682 | int32_t dump_now = 0; |
| 683 | // Dump the same thread NUM_THREADS simultaneously. |
| 684 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 685 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 686 | dumpers[i].thread.tid = runner.tid; |
| 687 | dumpers[i].thread.state = 0; |
| 688 | dumpers[i].done = 0; |
| 689 | dumpers[i].now = &dump_now; |
| 690 | |
| 691 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 692 | } |
| 693 | |
| 694 | // Start all of the dumpers going at once. |
| 695 | android_atomic_acquire_store(1, &dump_now); |
| 696 | |
| 697 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 698 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 699 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 700 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 701 | VerifyMaxDump(dumpers[i].backtrace); |
| 702 | |
| 703 | delete dumpers[i].backtrace; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 704 | dumpers[i].backtrace = nullptr; |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | // Tell the runner thread to exit its infinite loop. |
| 708 | android_atomic_acquire_store(0, &runner.state); |
| 709 | } |
| 710 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 711 | // This test is for UnwindMaps that should share the same map cursor when |
| 712 | // multiple maps are created for the current process at the same time. |
| 713 | TEST(libbacktrace, simultaneous_maps) { |
| 714 | BacktraceMap* map1 = BacktraceMap::Create(getpid()); |
| 715 | BacktraceMap* map2 = BacktraceMap::Create(getpid()); |
| 716 | BacktraceMap* map3 = BacktraceMap::Create(getpid()); |
| 717 | |
| 718 | Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 719 | ASSERT_TRUE(back1 != nullptr); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 720 | EXPECT_TRUE(back1->Unwind(0)); |
| 721 | delete back1; |
| 722 | delete map1; |
| 723 | |
| 724 | Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 725 | ASSERT_TRUE(back2 != nullptr); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 726 | EXPECT_TRUE(back2->Unwind(0)); |
| 727 | delete back2; |
| 728 | delete map2; |
| 729 | |
| 730 | Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 731 | ASSERT_TRUE(back3 != nullptr); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 732 | EXPECT_TRUE(back3->Unwind(0)); |
| 733 | delete back3; |
| 734 | delete map3; |
| 735 | } |
| 736 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 737 | TEST(libbacktrace, fillin_erases) { |
| 738 | BacktraceMap* back_map = BacktraceMap::Create(getpid()); |
| 739 | |
| 740 | backtrace_map_t map; |
| 741 | |
| 742 | map.start = 1; |
| 743 | map.end = 3; |
| 744 | map.flags = 1; |
| 745 | map.name = "Initialized"; |
| 746 | back_map->FillIn(0, &map); |
| 747 | delete back_map; |
| 748 | |
| 749 | ASSERT_FALSE(BacktraceMap::IsValid(map)); |
| 750 | ASSERT_EQ(static_cast<uintptr_t>(0), map.start); |
| 751 | ASSERT_EQ(static_cast<uintptr_t>(0), map.end); |
| 752 | ASSERT_EQ(0, map.flags); |
| 753 | ASSERT_EQ("", map.name); |
| 754 | } |
| 755 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 756 | TEST(libbacktrace, format_test) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 757 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD)); |
| 758 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 759 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 760 | backtrace_frame_data_t frame; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 761 | frame.num = 1; |
| 762 | frame.pc = 2; |
| 763 | frame.sp = 0; |
| 764 | frame.stack_size = 0; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 765 | frame.func_offset = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 766 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 767 | // Check no map set. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 768 | frame.num = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 769 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 770 | EXPECT_EQ("#01 pc 0000000000000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 771 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 772 | EXPECT_EQ("#01 pc 00000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 773 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 774 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 775 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 776 | // Check map name empty, but exists. |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 777 | frame.pc = 0xb0020; |
| 778 | frame.map.start = 0xb0000; |
| 779 | frame.map.end = 0xbffff; |
Christopher Ferris | 2106f4b | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 780 | frame.map.load_base = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 781 | #if defined(__LP64__) |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 782 | EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 783 | #else |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 784 | EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 785 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 786 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 787 | |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 788 | // Check map name begins with a [. |
| 789 | frame.pc = 0xc0020; |
| 790 | frame.map.start = 0xc0000; |
| 791 | frame.map.end = 0xcffff; |
| 792 | frame.map.load_base = 0; |
| 793 | frame.map.name = "[anon:thread signal stack]"; |
| 794 | #if defined(__LP64__) |
| 795 | EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]", |
| 796 | #else |
| 797 | EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]", |
| 798 | #endif |
| 799 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 800 | |
| 801 | // Check relative pc is set and map name is set. |
| 802 | frame.pc = 0x12345679; |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 803 | frame.map.name = "MapFake"; |
| 804 | frame.map.start = 1; |
| 805 | frame.map.end = 1; |
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", |
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", |
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 | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 812 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 813 | // Check func_name is set, but no func offset. |
| 814 | frame.func_name = "ProcFake"; |
| 815 | #if defined(__LP64__) |
| 816 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)", |
| 817 | #else |
| 818 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)", |
| 819 | #endif |
| 820 | backtrace->FormatFrameData(&frame)); |
| 821 | |
| 822 | // Check func_name is set, and func offset is non-zero. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 823 | frame.func_offset = 645; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 824 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 825 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 826 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 827 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 828 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 829 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 2106f4b | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 830 | |
| 831 | // Check func_name is set, func offset is non-zero, and load_base is non-zero. |
| 832 | frame.func_offset = 645; |
| 833 | frame.map.load_base = 100; |
| 834 | #if defined(__LP64__) |
| 835 | EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)", |
| 836 | #else |
| 837 | EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)", |
| 838 | #endif |
| 839 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | e0ab232 | 2015-08-20 11:16:54 -0700 | [diff] [blame] | 840 | |
| 841 | // Check a non-zero map offset. |
| 842 | frame.map.offset = 0x1000; |
| 843 | #if defined(__LP64__) |
| 844 | EXPECT_EQ("#01 pc 00000000123456dc MapFake (offset 0x1000) (ProcFake+645)", |
| 845 | #else |
| 846 | EXPECT_EQ("#01 pc 123456dc MapFake (offset 0x1000) (ProcFake+645)", |
| 847 | #endif |
| 848 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 849 | } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 850 | |
| 851 | struct map_test_t { |
| 852 | uintptr_t start; |
| 853 | uintptr_t end; |
| 854 | }; |
| 855 | |
| 856 | bool map_sort(map_test_t i, map_test_t j) { |
| 857 | return i.start < j.start; |
| 858 | } |
| 859 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 860 | void VerifyMap(pid_t pid) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 861 | char buffer[4096]; |
| 862 | snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid); |
| 863 | |
| 864 | FILE* map_file = fopen(buffer, "r"); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 865 | ASSERT_TRUE(map_file != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 866 | std::vector<map_test_t> test_maps; |
| 867 | while (fgets(buffer, sizeof(buffer), map_file)) { |
| 868 | map_test_t map; |
| 869 | ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end)); |
| 870 | test_maps.push_back(map); |
| 871 | } |
| 872 | fclose(map_file); |
| 873 | std::sort(test_maps.begin(), test_maps.end(), map_sort); |
| 874 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 875 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid)); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 876 | |
| 877 | // Basic test that verifies that the map is in the expected order. |
| 878 | std::vector<map_test_t>::const_iterator test_it = test_maps.begin(); |
| 879 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { |
| 880 | ASSERT_TRUE(test_it != test_maps.end()); |
| 881 | ASSERT_EQ(test_it->start, it->start); |
| 882 | ASSERT_EQ(test_it->end, it->end); |
| 883 | ++test_it; |
| 884 | } |
| 885 | ASSERT_TRUE(test_it == test_maps.end()); |
| 886 | } |
| 887 | |
| 888 | TEST(libbacktrace, verify_map_remote) { |
| 889 | pid_t pid; |
| 890 | |
| 891 | if ((pid = fork()) == 0) { |
| 892 | while (true) { |
| 893 | } |
| 894 | _exit(0); |
| 895 | } |
| 896 | ASSERT_LT(0, pid); |
| 897 | |
| 898 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 899 | |
| 900 | // Wait for the process to get to a stopping point. |
| 901 | WaitForStop(pid); |
| 902 | |
| 903 | // The maps should match exactly since the forked process has been paused. |
| 904 | VerifyMap(pid); |
| 905 | |
| 906 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 907 | |
| 908 | kill(pid, SIGKILL); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 909 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 910 | } |
| 911 | |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 912 | void InitMemory(uint8_t* memory, size_t bytes) { |
| 913 | for (size_t i = 0; i < bytes; i++) { |
| 914 | memory[i] = i; |
| 915 | if (memory[i] == '\0') { |
| 916 | // Don't use '\0' in our data so we can verify that an overread doesn't |
| 917 | // occur by using a '\0' as the character after the read data. |
| 918 | memory[i] = 23; |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 923 | void* ThreadReadTest(void* data) { |
| 924 | thread_t* thread_data = reinterpret_cast<thread_t*>(data); |
| 925 | |
| 926 | thread_data->tid = gettid(); |
| 927 | |
| 928 | // Create two map pages. |
| 929 | // Mark the second page as not-readable. |
| 930 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 931 | uint8_t* memory; |
| 932 | if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) { |
| 933 | return reinterpret_cast<void*>(-1); |
| 934 | } |
| 935 | |
| 936 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { |
| 937 | return reinterpret_cast<void*>(-1); |
| 938 | } |
| 939 | |
| 940 | // Set up a simple pattern in memory. |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 941 | InitMemory(memory, pagesize); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 942 | |
| 943 | thread_data->data = memory; |
| 944 | |
| 945 | // Tell the caller it's okay to start reading memory. |
| 946 | android_atomic_acquire_store(1, &thread_data->state); |
| 947 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 948 | // Loop waiting for the caller to finish reading the memory. |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 949 | while (thread_data->state) { |
| 950 | } |
| 951 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 952 | // Re-enable read-write on the page so that we don't crash if we try |
| 953 | // and access data on this page when freeing the memory. |
| 954 | if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) { |
| 955 | return reinterpret_cast<void*>(-1); |
| 956 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 957 | free(memory); |
| 958 | |
| 959 | android_atomic_acquire_store(1, &thread_data->state); |
| 960 | |
| 961 | return nullptr; |
| 962 | } |
| 963 | |
| 964 | void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) { |
| 965 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 966 | |
| 967 | // Create a page of data to use to do quick compares. |
| 968 | uint8_t* expected = new uint8_t[pagesize]; |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 969 | InitMemory(expected, pagesize); |
| 970 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 971 | uint8_t* data = new uint8_t[2*pagesize]; |
| 972 | // Verify that we can only read one page worth of data. |
| 973 | size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize); |
| 974 | ASSERT_EQ(pagesize, bytes_read); |
| 975 | ASSERT_TRUE(memcmp(data, expected, pagesize) == 0); |
| 976 | |
| 977 | // Verify unaligned reads. |
| 978 | for (size_t i = 1; i < sizeof(word_t); i++) { |
| 979 | bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t)); |
| 980 | ASSERT_EQ(2 * sizeof(word_t), bytes_read); |
| 981 | ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0) |
| 982 | << "Offset at " << i << " failed"; |
| 983 | } |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 984 | |
| 985 | // Verify small unaligned reads. |
| 986 | for (size_t i = 1; i < sizeof(word_t); i++) { |
| 987 | for (size_t j = 1; j < sizeof(word_t); j++) { |
| 988 | // Set one byte past what we expect to read, to guarantee we don't overread. |
| 989 | data[j] = '\0'; |
| 990 | bytes_read = backtrace->Read(read_addr + i, data, j); |
| 991 | ASSERT_EQ(j, bytes_read); |
| 992 | ASSERT_TRUE(memcmp(data, &expected[i], j) == 0) |
| 993 | << "Offset at " << i << " length " << j << " miscompared"; |
| 994 | ASSERT_EQ('\0', data[j]) |
| 995 | << "Offset at " << i << " length " << j << " wrote too much data"; |
| 996 | } |
| 997 | } |
Pirama Arumuga Nainar | 837eff2 | 2015-07-09 10:50:04 -0700 | [diff] [blame] | 998 | delete[] data; |
| 999 | delete[] expected; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | TEST(libbacktrace, thread_read) { |
| 1003 | pthread_attr_t attr; |
| 1004 | pthread_attr_init(&attr); |
| 1005 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 1006 | pthread_t thread; |
| 1007 | thread_t thread_data = { 0, 0, 0, nullptr }; |
| 1008 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0); |
| 1009 | |
| 1010 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); |
| 1011 | |
| 1012 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 1013 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1014 | |
| 1015 | RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data)); |
| 1016 | |
| 1017 | android_atomic_acquire_store(0, &thread_data.state); |
| 1018 | |
| 1019 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); |
| 1020 | } |
| 1021 | |
| 1022 | volatile uintptr_t g_ready = 0; |
| 1023 | volatile uintptr_t g_addr = 0; |
| 1024 | |
| 1025 | void ForkedReadTest() { |
| 1026 | // Create two map pages. |
| 1027 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 1028 | uint8_t* memory; |
| 1029 | if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) { |
| 1030 | perror("Failed to allocate memory\n"); |
| 1031 | exit(1); |
| 1032 | } |
| 1033 | |
| 1034 | // Mark the second page as not-readable. |
| 1035 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { |
| 1036 | perror("Failed to mprotect memory\n"); |
| 1037 | exit(1); |
| 1038 | } |
| 1039 | |
| 1040 | // Set up a simple pattern in memory. |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 1041 | InitMemory(memory, pagesize); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1042 | |
| 1043 | g_addr = reinterpret_cast<uintptr_t>(memory); |
| 1044 | g_ready = 1; |
| 1045 | |
| 1046 | while (1) { |
| 1047 | usleep(US_PER_MSEC); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | TEST(libbacktrace, process_read) { |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1052 | g_ready = 0; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1053 | pid_t pid; |
| 1054 | if ((pid = fork()) == 0) { |
| 1055 | ForkedReadTest(); |
| 1056 | exit(0); |
| 1057 | } |
| 1058 | ASSERT_NE(-1, pid); |
| 1059 | |
| 1060 | bool test_executed = false; |
| 1061 | uint64_t start = NanoTime(); |
| 1062 | while (1) { |
| 1063 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) { |
| 1064 | WaitForStop(pid); |
| 1065 | |
| 1066 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid)); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 1067 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1068 | |
| 1069 | uintptr_t read_addr; |
| 1070 | size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), |
| 1071 | reinterpret_cast<uint8_t*>(&read_addr), |
| 1072 | sizeof(uintptr_t)); |
| 1073 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); |
| 1074 | if (read_addr) { |
| 1075 | // The forked process is ready to be read. |
| 1076 | bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), |
| 1077 | reinterpret_cast<uint8_t*>(&read_addr), |
| 1078 | sizeof(uintptr_t)); |
| 1079 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); |
| 1080 | |
| 1081 | RunReadTest(backtrace.get(), read_addr); |
| 1082 | |
| 1083 | test_executed = true; |
| 1084 | break; |
| 1085 | } |
| 1086 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1087 | } |
| 1088 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { |
| 1089 | break; |
| 1090 | } |
| 1091 | usleep(US_PER_MSEC); |
| 1092 | } |
| 1093 | kill(pid, SIGKILL); |
| 1094 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 1095 | |
| 1096 | ASSERT_TRUE(test_executed); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1099 | void VerifyFunctionsFound(const std::vector<std::string>& found_functions) { |
| 1100 | // We expect to find these functions in libbacktrace_test. If we don't |
| 1101 | // find them, that's a bug in the memory read handling code in libunwind. |
| 1102 | std::list<std::string> expected_functions; |
| 1103 | expected_functions.push_back("test_recursive_call"); |
| 1104 | expected_functions.push_back("test_level_one"); |
| 1105 | expected_functions.push_back("test_level_two"); |
| 1106 | expected_functions.push_back("test_level_three"); |
| 1107 | expected_functions.push_back("test_level_four"); |
| 1108 | for (const auto& found_function : found_functions) { |
| 1109 | for (const auto& expected_function : expected_functions) { |
| 1110 | if (found_function == expected_function) { |
| 1111 | expected_functions.remove(found_function); |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library."; |
| 1117 | } |
| 1118 | |
| 1119 | const char* CopySharedLibrary() { |
| 1120 | #if defined(__LP64__) |
| 1121 | const char* lib_name = "lib64"; |
| 1122 | #else |
| 1123 | const char* lib_name = "lib"; |
| 1124 | #endif |
| 1125 | |
| 1126 | #if defined(__BIONIC__) |
| 1127 | const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so"; |
| 1128 | std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s", |
| 1129 | lib_name, tmp_so_name); |
| 1130 | #else |
| 1131 | const char* tmp_so_name = "/tmp/libbacktrace_test.so"; |
| 1132 | if (getenv("ANDROID_HOST_OUT") == NULL) { |
| 1133 | fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch."); |
| 1134 | return nullptr; |
| 1135 | } |
| 1136 | std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s", |
| 1137 | getenv("ANDROID_HOST_OUT"), lib_name, |
| 1138 | tmp_so_name); |
| 1139 | #endif |
| 1140 | |
| 1141 | // Copy the shared so to a tempory directory. |
| 1142 | system(cp_cmd.c_str()); |
| 1143 | |
| 1144 | return tmp_so_name; |
| 1145 | } |
| 1146 | |
| 1147 | TEST(libbacktrace, check_unreadable_elf_local) { |
| 1148 | const char* tmp_so_name = CopySharedLibrary(); |
| 1149 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1150 | |
| 1151 | struct stat buf; |
| 1152 | ASSERT_TRUE(stat(tmp_so_name, &buf) != -1); |
| 1153 | uintptr_t map_size = buf.st_size; |
| 1154 | |
| 1155 | int fd = open(tmp_so_name, O_RDONLY); |
| 1156 | ASSERT_TRUE(fd != -1); |
| 1157 | |
Christopher Ferris | 61c48ac | 2016-01-15 16:08:58 -0800 | [diff] [blame] | 1158 | void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1159 | ASSERT_TRUE(map != MAP_FAILED); |
| 1160 | close(fd); |
| 1161 | ASSERT_TRUE(unlink(tmp_so_name) != -1); |
| 1162 | |
| 1163 | std::vector<std::string> found_functions; |
| 1164 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, |
| 1165 | BACKTRACE_CURRENT_THREAD)); |
| 1166 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1167 | |
| 1168 | // Needed before GetFunctionName will work. |
| 1169 | backtrace->Unwind(0); |
| 1170 | |
| 1171 | // Loop through the entire map, and get every function we can find. |
| 1172 | map_size += reinterpret_cast<uintptr_t>(map); |
| 1173 | std::string last_func; |
| 1174 | for (uintptr_t read_addr = reinterpret_cast<uintptr_t>(map); |
| 1175 | read_addr < map_size; read_addr += 4) { |
| 1176 | uintptr_t offset; |
| 1177 | std::string func_name = backtrace->GetFunctionName(read_addr, &offset); |
| 1178 | if (!func_name.empty() && last_func != func_name) { |
| 1179 | found_functions.push_back(func_name); |
| 1180 | } |
| 1181 | last_func = func_name; |
| 1182 | } |
| 1183 | |
| 1184 | ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uintptr_t>(map)) == 0); |
| 1185 | |
| 1186 | VerifyFunctionsFound(found_functions); |
| 1187 | } |
| 1188 | |
| 1189 | TEST(libbacktrace, check_unreadable_elf_remote) { |
| 1190 | const char* tmp_so_name = CopySharedLibrary(); |
| 1191 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1192 | |
| 1193 | g_ready = 0; |
| 1194 | |
| 1195 | struct stat buf; |
| 1196 | ASSERT_TRUE(stat(tmp_so_name, &buf) != -1); |
| 1197 | uintptr_t map_size = buf.st_size; |
| 1198 | |
| 1199 | pid_t pid; |
| 1200 | if ((pid = fork()) == 0) { |
| 1201 | int fd = open(tmp_so_name, O_RDONLY); |
| 1202 | if (fd == -1) { |
| 1203 | fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno)); |
| 1204 | unlink(tmp_so_name); |
| 1205 | exit(0); |
| 1206 | } |
| 1207 | |
Christopher Ferris | 61c48ac | 2016-01-15 16:08:58 -0800 | [diff] [blame] | 1208 | void* map = mmap(NULL, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1209 | if (map == MAP_FAILED) { |
| 1210 | fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno)); |
| 1211 | unlink(tmp_so_name); |
| 1212 | exit(0); |
| 1213 | } |
| 1214 | close(fd); |
| 1215 | if (unlink(tmp_so_name) == -1) { |
| 1216 | fprintf(stderr, "Failed to unlink: %s\n", strerror(errno)); |
| 1217 | exit(0); |
| 1218 | } |
| 1219 | |
| 1220 | g_addr = reinterpret_cast<uintptr_t>(map); |
| 1221 | g_ready = 1; |
| 1222 | while (true) { |
| 1223 | usleep(US_PER_MSEC); |
| 1224 | } |
| 1225 | exit(0); |
| 1226 | } |
| 1227 | ASSERT_TRUE(pid > 0); |
| 1228 | |
| 1229 | std::vector<std::string> found_functions; |
| 1230 | uint64_t start = NanoTime(); |
| 1231 | while (true) { |
| 1232 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1233 | |
| 1234 | // Wait for the process to get to a stopping point. |
| 1235 | WaitForStop(pid); |
| 1236 | |
| 1237 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD)); |
| 1238 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1239 | |
| 1240 | uintptr_t read_addr; |
| 1241 | ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t))); |
| 1242 | if (read_addr) { |
| 1243 | ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t))); |
| 1244 | |
| 1245 | // Needed before GetFunctionName will work. |
| 1246 | backtrace->Unwind(0); |
| 1247 | |
| 1248 | // Loop through the entire map, and get every function we can find. |
| 1249 | map_size += read_addr; |
| 1250 | std::string last_func; |
| 1251 | for (; read_addr < map_size; read_addr += 4) { |
| 1252 | uintptr_t offset; |
| 1253 | std::string func_name = backtrace->GetFunctionName(read_addr, &offset); |
| 1254 | if (!func_name.empty() && last_func != func_name) { |
| 1255 | found_functions.push_back(func_name); |
| 1256 | } |
| 1257 | last_func = func_name; |
| 1258 | } |
| 1259 | break; |
| 1260 | } |
| 1261 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1262 | |
| 1263 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { |
| 1264 | break; |
| 1265 | } |
| 1266 | usleep(US_PER_MSEC); |
| 1267 | } |
| 1268 | |
| 1269 | kill(pid, SIGKILL); |
| 1270 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 1271 | |
| 1272 | VerifyFunctionsFound(found_functions); |
| 1273 | } |
| 1274 | |
| 1275 | bool FindFuncFrameInBacktrace(Backtrace* backtrace, uintptr_t test_func, size_t* frame_num) { |
| 1276 | backtrace_map_t map; |
| 1277 | backtrace->FillInMap(test_func, &map); |
| 1278 | if (!BacktraceMap::IsValid(map)) { |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
| 1282 | // Loop through the frames, and find the one that is in the map. |
| 1283 | *frame_num = 0; |
| 1284 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { |
| 1285 | if (BacktraceMap::IsValid(it->map) && map.start == it->map.start && |
| 1286 | it->pc >= test_func) { |
| 1287 | *frame_num = it->num; |
| 1288 | return true; |
| 1289 | } |
| 1290 | } |
| 1291 | return false; |
| 1292 | } |
| 1293 | |
| 1294 | void VerifyUnreadableElfFrame(Backtrace* backtrace, uintptr_t test_func, size_t frame_num) { |
| 1295 | ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) |
| 1296 | << DumpFrames(backtrace); |
| 1297 | |
| 1298 | ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace); |
| 1299 | // Make sure that there is at least one more frame above the test func call. |
| 1300 | ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace); |
| 1301 | |
| 1302 | uintptr_t diff = backtrace->GetFrame(frame_num)->pc - test_func; |
| 1303 | ASSERT_LT(diff, 200U) << DumpFrames(backtrace); |
| 1304 | } |
| 1305 | |
| 1306 | void VerifyUnreadableElfBacktrace(uintptr_t test_func) { |
| 1307 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, |
| 1308 | BACKTRACE_CURRENT_THREAD)); |
| 1309 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1310 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 1311 | |
| 1312 | size_t frame_num; |
| 1313 | ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num)); |
| 1314 | |
| 1315 | VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num); |
| 1316 | } |
| 1317 | |
| 1318 | typedef int (*test_func_t)(int, int, int, int, void (*)(uintptr_t), uintptr_t); |
| 1319 | |
| 1320 | TEST(libbacktrace, unwind_through_unreadable_elf_local) { |
| 1321 | const char* tmp_so_name = CopySharedLibrary(); |
| 1322 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1323 | void* lib_handle = dlopen(tmp_so_name, RTLD_NOW); |
| 1324 | ASSERT_TRUE(lib_handle != nullptr); |
| 1325 | ASSERT_TRUE(unlink(tmp_so_name) != -1); |
| 1326 | |
| 1327 | test_func_t test_func; |
| 1328 | test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one")); |
| 1329 | ASSERT_TRUE(test_func != nullptr); |
| 1330 | |
| 1331 | ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace, |
| 1332 | reinterpret_cast<uintptr_t>(test_func)), 0); |
| 1333 | |
| 1334 | ASSERT_TRUE(dlclose(lib_handle) == 0); |
| 1335 | } |
| 1336 | |
| 1337 | TEST(libbacktrace, unwind_through_unreadable_elf_remote) { |
| 1338 | const char* tmp_so_name = CopySharedLibrary(); |
| 1339 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1340 | void* lib_handle = dlopen(tmp_so_name, RTLD_NOW); |
| 1341 | ASSERT_TRUE(lib_handle != nullptr); |
| 1342 | ASSERT_TRUE(unlink(tmp_so_name) != -1); |
| 1343 | |
| 1344 | test_func_t test_func; |
| 1345 | test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one")); |
| 1346 | ASSERT_TRUE(test_func != nullptr); |
| 1347 | |
| 1348 | pid_t pid; |
| 1349 | if ((pid = fork()) == 0) { |
| 1350 | test_func(1, 2, 3, 4, 0, 0); |
| 1351 | exit(0); |
| 1352 | } |
| 1353 | ASSERT_TRUE(pid > 0); |
| 1354 | ASSERT_TRUE(dlclose(lib_handle) == 0); |
| 1355 | |
| 1356 | uint64_t start = NanoTime(); |
| 1357 | bool done = false; |
| 1358 | while (!done) { |
| 1359 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1360 | |
| 1361 | // Wait for the process to get to a stopping point. |
| 1362 | WaitForStop(pid); |
| 1363 | |
| 1364 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD)); |
| 1365 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1366 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 1367 | |
| 1368 | size_t frame_num; |
| 1369 | if (FindFuncFrameInBacktrace(backtrace.get(), |
| 1370 | reinterpret_cast<uintptr_t>(test_func), &frame_num)) { |
| 1371 | |
| 1372 | VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uintptr_t>(test_func), frame_num); |
| 1373 | done = true; |
| 1374 | } |
| 1375 | |
| 1376 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1377 | |
| 1378 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { |
| 1379 | break; |
| 1380 | } |
| 1381 | usleep(US_PER_MSEC); |
| 1382 | } |
| 1383 | |
| 1384 | kill(pid, SIGKILL); |
| 1385 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 1386 | |
| 1387 | ASSERT_TRUE(done) << "Test function never found in unwind."; |
| 1388 | } |
| 1389 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1390 | #if defined(ENABLE_PSS_TESTS) |
| 1391 | #include "GetPss.h" |
| 1392 | |
| 1393 | #define MAX_LEAK_BYTES 32*1024UL |
| 1394 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1395 | void CheckForLeak(pid_t pid, pid_t tid) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1396 | // Do a few runs to get the PSS stable. |
| 1397 | for (size_t i = 0; i < 100; i++) { |
| 1398 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1399 | ASSERT_TRUE(backtrace != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1400 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 1401 | delete backtrace; |
| 1402 | } |
| 1403 | size_t stable_pss = GetPssBytes(); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1404 | ASSERT_TRUE(stable_pss != 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1405 | |
| 1406 | // Loop enough that even a small leak should be detectable. |
| 1407 | for (size_t i = 0; i < 4096; i++) { |
| 1408 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1409 | ASSERT_TRUE(backtrace != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1410 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 1411 | delete backtrace; |
| 1412 | } |
| 1413 | size_t new_pss = GetPssBytes(); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1414 | ASSERT_TRUE(new_pss != 0); |
Christopher Ferris | 5ccdfa6 | 2016-03-07 19:18:31 -0800 | [diff] [blame^] | 1415 | if (new_pss > stable_pss) { |
| 1416 | ASSERT_LE(new_pss - stable_pss, MAX_LEAK_BYTES); |
| 1417 | } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | TEST(libbacktrace, check_for_leak_local) { |
| 1421 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD); |
| 1422 | } |
| 1423 | |
| 1424 | TEST(libbacktrace, check_for_leak_local_thread) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1425 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1426 | pthread_t thread; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1427 | ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1428 | |
| 1429 | // Wait up to 2 seconds for the tid to be set. |
| 1430 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 1431 | |
| 1432 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid); |
| 1433 | |
| 1434 | // Tell the thread to exit its infinite loop. |
| 1435 | android_atomic_acquire_store(0, &thread_data.state); |
| 1436 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1437 | ASSERT_TRUE(pthread_join(thread, nullptr) == 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | TEST(libbacktrace, check_for_leak_remote) { |
| 1441 | pid_t pid; |
| 1442 | |
| 1443 | if ((pid = fork()) == 0) { |
| 1444 | while (true) { |
| 1445 | } |
| 1446 | _exit(0); |
| 1447 | } |
| 1448 | ASSERT_LT(0, pid); |
| 1449 | |
| 1450 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1451 | |
| 1452 | // Wait for the process to get to a stopping point. |
| 1453 | WaitForStop(pid); |
| 1454 | |
| 1455 | CheckForLeak(pid, BACKTRACE_CURRENT_THREAD); |
| 1456 | |
| 1457 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1458 | |
| 1459 | kill(pid, SIGKILL); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1460 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1461 | } |
| 1462 | #endif |