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