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 | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 39 | #include <ostream> |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 40 | #include <string> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 41 | #include <vector> |
| 42 | |
Dan Albert | 23f750b | 2015-04-30 12:52:21 -0700 | [diff] [blame] | 43 | #include <backtrace/Backtrace.h> |
| 44 | #include <backtrace/BacktraceMap.h> |
| 45 | |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 46 | #include <android-base/macros.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 48 | #include <android-base/unique_fd.h> |
Dan Albert | 23f750b | 2015-04-30 12:52:21 -0700 | [diff] [blame] | 49 | #include <cutils/atomic.h> |
| 50 | #include <cutils/threads.h> |
| 51 | |
| 52 | #include <gtest/gtest.h> |
| 53 | |
| 54 | // For the THREAD_SIGNAL definition. |
| 55 | #include "BacktraceCurrent.h" |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 56 | #include "backtrace_testlib.h" |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 57 | #include "thread_utils.h" |
| 58 | |
| 59 | // Number of microseconds per milliseconds. |
| 60 | #define US_PER_MSEC 1000 |
| 61 | |
| 62 | // Number of nanoseconds in a second. |
| 63 | #define NS_PER_SEC 1000000000ULL |
| 64 | |
| 65 | // Number of simultaneous dumping operations to perform. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 66 | #define NUM_THREADS 40 |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 67 | |
| 68 | // Number of simultaneous threads running in our forked process. |
| 69 | #define NUM_PTRACE_THREADS 5 |
| 70 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 71 | struct thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 72 | pid_t tid; |
| 73 | int32_t state; |
| 74 | pthread_t threadId; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 75 | void* data; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 76 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 77 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 78 | struct dump_thread_t { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 79 | thread_t thread; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 80 | Backtrace* backtrace; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 81 | int32_t* now; |
| 82 | int32_t done; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 83 | }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 84 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 85 | typedef Backtrace* (*create_func_t)(pid_t, pid_t, BacktraceMap*); |
| 86 | typedef BacktraceMap* (*map_create_func_t)(pid_t, bool); |
| 87 | |
| 88 | static void VerifyLevelDump(Backtrace* backtrace, create_func_t create_func = nullptr, |
| 89 | map_create_func_t map_func = nullptr); |
| 90 | static void VerifyMaxDump(Backtrace* backtrace, create_func_t create_func = nullptr, |
| 91 | map_create_func_t map_func = nullptr); |
| 92 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 93 | static uint64_t NanoTime() { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 94 | struct timespec t = { 0, 0 }; |
| 95 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 96 | return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec); |
| 97 | } |
| 98 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 99 | static std::string DumpFrames(Backtrace* backtrace) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 100 | if (backtrace->NumFrames() == 0) { |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 101 | return " No frames to dump.\n"; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 104 | std::string frame; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 105 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 106 | frame += " " + backtrace->FormatFrameData(i) + '\n'; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 107 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 108 | return frame; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 111 | static void WaitForStop(pid_t pid) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 112 | uint64_t start = NanoTime(); |
| 113 | |
| 114 | siginfo_t si; |
| 115 | while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) { |
| 116 | if ((NanoTime() - start) > NS_PER_SEC) { |
| 117 | printf("The process did not get to a stopping point in 1 second.\n"); |
| 118 | break; |
| 119 | } |
| 120 | usleep(US_PER_MSEC); |
| 121 | } |
| 122 | } |
| 123 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 124 | static void CreateRemoteProcess(pid_t* pid) { |
| 125 | if ((*pid = fork()) == 0) { |
| 126 | while (true) |
| 127 | ; |
| 128 | _exit(0); |
| 129 | } |
| 130 | ASSERT_NE(-1, *pid); |
| 131 | |
| 132 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, *pid, 0, 0) == 0); |
| 133 | |
| 134 | // Wait for the process to get to a stopping point. |
| 135 | WaitForStop(*pid); |
| 136 | } |
| 137 | |
| 138 | static void FinishRemoteProcess(pid_t pid) { |
| 139 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 140 | |
| 141 | kill(pid, SIGKILL); |
| 142 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 143 | } |
| 144 | |
| 145 | static bool ReadyLevelBacktrace(Backtrace* backtrace) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 146 | // See if test_level_four is in the backtrace. |
| 147 | bool found = false; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 148 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { |
| 149 | if (it->func_name == "test_level_four") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 150 | found = true; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return found; |
| 156 | } |
| 157 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 158 | static void VerifyLevelDump(Backtrace* backtrace, create_func_t, map_create_func_t) { |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 159 | ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0)) |
| 160 | << DumpFrames(backtrace); |
| 161 | ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) |
| 162 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 163 | |
| 164 | // Look through the frames starting at the highest to find the |
| 165 | // frame we want. |
| 166 | size_t frame_num = 0; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 167 | for (size_t i = backtrace->NumFrames()-1; i > 2; i--) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 168 | if (backtrace->GetFrame(i)->func_name == "test_level_one") { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 169 | frame_num = i; |
| 170 | break; |
| 171 | } |
| 172 | } |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 173 | ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace); |
| 174 | ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 175 | |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 176 | ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one") |
| 177 | << DumpFrames(backtrace); |
| 178 | ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two") |
| 179 | << DumpFrames(backtrace); |
| 180 | ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three") |
| 181 | << DumpFrames(backtrace); |
| 182 | ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four") |
| 183 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 186 | static void VerifyLevelBacktrace(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 187 | std::unique_ptr<Backtrace> backtrace( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 188 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 189 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 190 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 191 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 192 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 193 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 196 | static bool ReadyMaxBacktrace(Backtrace* backtrace) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 197 | return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 200 | static void VerifyMaxDump(Backtrace* backtrace, create_func_t, map_create_func_t) { |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 201 | ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) |
| 202 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 203 | // Verify that the last frame is our recursive call. |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 204 | ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call") |
| 205 | << DumpFrames(backtrace); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 208 | static void VerifyMaxBacktrace(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 209 | std::unique_ptr<Backtrace> backtrace( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 210 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 211 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 212 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 213 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 214 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 215 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 218 | static void ThreadSetState(void* data) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 219 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 220 | android_atomic_acquire_store(1, &thread->state); |
| 221 | volatile int i = 0; |
| 222 | while (thread->state) { |
| 223 | i++; |
| 224 | } |
| 225 | } |
| 226 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 227 | static bool WaitForNonZero(int32_t* value, uint64_t seconds) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 228 | uint64_t start = NanoTime(); |
| 229 | do { |
| 230 | if (android_atomic_acquire_load(value)) { |
| 231 | return true; |
| 232 | } |
| 233 | } while ((NanoTime() - start) < seconds * NS_PER_SEC); |
| 234 | return false; |
| 235 | } |
| 236 | |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 237 | TEST(libbacktrace, local_no_unwind_frames) { |
| 238 | // Verify that a local unwind does not include any frames within |
| 239 | // libunwind or libbacktrace. |
| 240 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid())); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 241 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 242 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 243 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 244 | |
| 245 | ASSERT_TRUE(backtrace->NumFrames() != 0); |
| 246 | for (const auto& frame : *backtrace ) { |
| 247 | if (BacktraceMap::IsValid(frame.map)) { |
| 248 | const std::string name = basename(frame.map.name.c_str()); |
| 249 | ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so") |
| 250 | << DumpFrames(backtrace.get()); |
| 251 | } |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 256 | TEST(libbacktrace, local_trace) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 257 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 260 | static void VerifyIgnoreFrames(Backtrace* bt_all, Backtrace* bt_ign1, Backtrace* bt_ign2, |
| 261 | const char* cur_proc) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 262 | ASSERT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1) << "All backtrace:\n" |
| 263 | << DumpFrames(bt_all) |
| 264 | << "Ignore 1 backtrace:\n" |
| 265 | << DumpFrames(bt_ign1); |
| 266 | ASSERT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2) << "All backtrace:\n" |
| 267 | << DumpFrames(bt_all) |
| 268 | << "Ignore 2 backtrace:\n" |
| 269 | << DumpFrames(bt_ign2); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 270 | |
| 271 | // Check all of the frames are the same > the current frame. |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 272 | bool check = (cur_proc == nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 273 | for (size_t i = 0; i < bt_ign2->NumFrames(); i++) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 274 | if (check) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 275 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc); |
| 276 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp); |
| 277 | 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] | 278 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 279 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc); |
| 280 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp); |
| 281 | 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] | 282 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 283 | if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 284 | check = true; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 289 | static void VerifyLevelIgnoreFrames(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 290 | std::unique_ptr<Backtrace> all( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 291 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 292 | ASSERT_TRUE(all.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 293 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 294 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 295 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 296 | std::unique_ptr<Backtrace> ign1( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 297 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 298 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 299 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 300 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 301 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 302 | std::unique_ptr<Backtrace> ign2( |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 303 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 304 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 305 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 306 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 307 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 308 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames"); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST(libbacktrace, local_trace_ignore_frames) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 312 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | TEST(libbacktrace, local_max_trace) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 316 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 319 | static void VerifyProcTest(pid_t pid, pid_t tid, bool (*ReadyFunc)(Backtrace*), |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 320 | void (*VerifyFunc)(Backtrace*, create_func_t, map_create_func_t), |
| 321 | create_func_t create_func, map_create_func_t map_create_func) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 322 | pid_t ptrace_tid; |
| 323 | if (tid < 0) { |
| 324 | ptrace_tid = pid; |
| 325 | } else { |
| 326 | ptrace_tid = tid; |
| 327 | } |
| 328 | uint64_t start = NanoTime(); |
| 329 | bool verified = false; |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 330 | std::string last_dump; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 331 | do { |
| 332 | usleep(US_PER_MSEC); |
| 333 | if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) { |
| 334 | // Wait for the process to get to a stopping point. |
| 335 | WaitForStop(ptrace_tid); |
| 336 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 337 | std::unique_ptr<BacktraceMap> map; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 338 | map.reset(map_create_func(pid, false)); |
| 339 | std::unique_ptr<Backtrace> backtrace(create_func(pid, tid, map.get())); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 340 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 341 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 342 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 343 | if (ReadyFunc(backtrace.get())) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 344 | VerifyFunc(backtrace.get(), create_func, map_create_func); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 345 | verified = true; |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 346 | } else { |
| 347 | last_dump = DumpFrames(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 348 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 349 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 350 | ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0); |
| 351 | } |
| 352 | // If 5 seconds have passed, then we are done. |
| 353 | } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 354 | ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | TEST(libbacktrace, ptrace_trace) { |
| 358 | pid_t pid; |
| 359 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 360 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 361 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 362 | } |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 363 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyLevelDump, |
| 364 | Backtrace::Create, BacktraceMap::Create); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 365 | |
| 366 | kill(pid, SIGKILL); |
| 367 | int status; |
| 368 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 369 | } |
| 370 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 371 | TEST(libbacktrace, ptrace_trace_new) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 372 | pid_t pid; |
| 373 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 374 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 375 | _exit(1); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 376 | } |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 377 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyLevelDump, |
| 378 | Backtrace::CreateNew, BacktraceMap::CreateNew); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 379 | |
| 380 | kill(pid, SIGKILL); |
| 381 | int status; |
| 382 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 383 | } |
| 384 | |
| 385 | TEST(libbacktrace, ptrace_max_trace) { |
| 386 | pid_t pid; |
| 387 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 388 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 389 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 390 | } |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 391 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyMaxBacktrace, VerifyMaxDump, Backtrace::Create, |
| 392 | BacktraceMap::Create); |
| 393 | |
| 394 | kill(pid, SIGKILL); |
| 395 | int status; |
| 396 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 397 | } |
| 398 | |
| 399 | TEST(libbacktrace, ptrace_max_trace_new) { |
| 400 | pid_t pid; |
| 401 | if ((pid = fork()) == 0) { |
| 402 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES + 10, nullptr, nullptr), 0); |
| 403 | _exit(1); |
| 404 | } |
| 405 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyMaxBacktrace, VerifyMaxDump, |
| 406 | Backtrace::CreateNew, BacktraceMap::CreateNew); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 407 | |
| 408 | kill(pid, SIGKILL); |
| 409 | int status; |
| 410 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 411 | } |
| 412 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 413 | static void VerifyProcessIgnoreFrames(Backtrace* bt_all, create_func_t create_func, |
| 414 | map_create_func_t map_create_func) { |
| 415 | std::unique_ptr<BacktraceMap> map(map_create_func(bt_all->Pid(), false)); |
| 416 | std::unique_ptr<Backtrace> ign1(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get())); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 417 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 418 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 419 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 420 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 421 | std::unique_ptr<Backtrace> ign2(create_func(bt_all->Pid(), BACKTRACE_CURRENT_THREAD, map.get())); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 422 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 423 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 424 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 425 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 426 | VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | TEST(libbacktrace, ptrace_ignore_frames) { |
| 430 | pid_t pid; |
| 431 | if ((pid = fork()) == 0) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 432 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 433 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 434 | } |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 435 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyProcessIgnoreFrames, |
| 436 | Backtrace::Create, BacktraceMap::Create); |
| 437 | |
| 438 | kill(pid, SIGKILL); |
| 439 | int status; |
| 440 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 441 | } |
| 442 | |
| 443 | TEST(libbacktrace, ptrace_ignore_frames_new) { |
| 444 | pid_t pid; |
| 445 | if ((pid = fork()) == 0) { |
| 446 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
| 447 | _exit(1); |
| 448 | } |
| 449 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, ReadyLevelBacktrace, VerifyProcessIgnoreFrames, |
| 450 | Backtrace::CreateNew, BacktraceMap::CreateNew); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 451 | |
| 452 | kill(pid, SIGKILL); |
| 453 | int status; |
| 454 | ASSERT_EQ(waitpid(pid, &status, 0), pid); |
| 455 | } |
| 456 | |
| 457 | // Create a process with multiple threads and dump all of the threads. |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 458 | static void* PtraceThreadLevelRun(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 459 | EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
| 460 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 463 | static void GetThreads(pid_t pid, std::vector<pid_t>* threads) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 464 | // Get the list of tasks. |
| 465 | char task_path[128]; |
| 466 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); |
| 467 | |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 468 | std::unique_ptr<DIR, decltype(&closedir)> tasks_dir(opendir(task_path), closedir); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 469 | ASSERT_TRUE(tasks_dir != nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 470 | struct dirent* entry; |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 471 | while ((entry = readdir(tasks_dir.get())) != nullptr) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 472 | char* end; |
| 473 | pid_t tid = strtoul(entry->d_name, &end, 10); |
| 474 | if (*end == '\0') { |
| 475 | threads->push_back(tid); |
| 476 | } |
| 477 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | TEST(libbacktrace, ptrace_threads) { |
| 481 | pid_t pid; |
| 482 | if ((pid = fork()) == 0) { |
| 483 | for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) { |
| 484 | pthread_attr_t attr; |
| 485 | pthread_attr_init(&attr); |
| 486 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 487 | |
| 488 | pthread_t thread; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 489 | ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 490 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 491 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 492 | _exit(1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // Check to see that all of the threads are running before unwinding. |
| 496 | std::vector<pid_t> threads; |
| 497 | uint64_t start = NanoTime(); |
| 498 | do { |
| 499 | usleep(US_PER_MSEC); |
| 500 | threads.clear(); |
| 501 | GetThreads(pid, &threads); |
| 502 | } while ((threads.size() != NUM_PTRACE_THREADS + 1) && |
| 503 | ((NanoTime() - start) <= 5 * NS_PER_SEC)); |
| 504 | ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1)); |
| 505 | |
| 506 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 507 | WaitForStop(pid); |
| 508 | for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) { |
| 509 | // Skip the current forked process, we only care about the threads. |
| 510 | if (pid == *it) { |
| 511 | continue; |
| 512 | } |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 513 | VerifyProcTest(pid, *it, ReadyLevelBacktrace, VerifyLevelDump, Backtrace::Create, |
| 514 | BacktraceMap::Create); |
| 515 | } |
| 516 | |
| 517 | FinishRemoteProcess(pid); |
| 518 | } |
| 519 | |
| 520 | TEST(libbacktrace, ptrace_threads_new) { |
| 521 | pid_t pid; |
| 522 | if ((pid = fork()) == 0) { |
| 523 | for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) { |
| 524 | pthread_attr_t attr; |
| 525 | pthread_attr_init(&attr); |
| 526 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 527 | |
| 528 | pthread_t thread; |
| 529 | ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0); |
| 530 | } |
| 531 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); |
| 532 | _exit(1); |
| 533 | } |
| 534 | |
| 535 | // Check to see that all of the threads are running before unwinding. |
| 536 | std::vector<pid_t> threads; |
| 537 | uint64_t start = NanoTime(); |
| 538 | do { |
| 539 | usleep(US_PER_MSEC); |
| 540 | threads.clear(); |
| 541 | GetThreads(pid, &threads); |
| 542 | } while ((threads.size() != NUM_PTRACE_THREADS + 1) && ((NanoTime() - start) <= 5 * NS_PER_SEC)); |
| 543 | ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1)); |
| 544 | |
| 545 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 546 | WaitForStop(pid); |
| 547 | for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) { |
| 548 | // Skip the current forked process, we only care about the threads. |
| 549 | if (pid == *it) { |
| 550 | continue; |
| 551 | } |
| 552 | VerifyProcTest(pid, *it, ReadyLevelBacktrace, VerifyLevelDump, Backtrace::CreateNew, |
| 553 | BacktraceMap::CreateNew); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 554 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 555 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 556 | FinishRemoteProcess(pid); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void VerifyLevelThread(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 560 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 561 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 562 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 563 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 564 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 565 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | TEST(libbacktrace, thread_current_level) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 569 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 572 | static void VerifyMaxThread(void*) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 573 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); |
| 574 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 575 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 576 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 577 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 578 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | TEST(libbacktrace, thread_current_max) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 582 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 585 | static void* ThreadLevelRun(void* data) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 586 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 587 | |
| 588 | thread->tid = gettid(); |
| 589 | EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 590 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | TEST(libbacktrace, thread_level_trace) { |
| 594 | pthread_attr_t attr; |
| 595 | pthread_attr_init(&attr); |
| 596 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 597 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 598 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 599 | pthread_t thread; |
| 600 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); |
| 601 | |
| 602 | // Wait up to 2 seconds for the tid to be set. |
| 603 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 604 | |
Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 605 | // Make sure that the thread signal used is not visible when compiled for |
| 606 | // the target. |
| 607 | #if !defined(__GLIBC__) |
| 608 | ASSERT_LT(THREAD_SIGNAL, SIGRTMIN); |
| 609 | #endif |
| 610 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 611 | // Save the current signal action and make sure it is restored afterwards. |
| 612 | struct sigaction cur_action; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 613 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 614 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 615 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 616 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 617 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 618 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 619 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 620 | VerifyLevelDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 621 | |
| 622 | // Tell the thread to exit its infinite loop. |
| 623 | android_atomic_acquire_store(0, &thread_data.state); |
| 624 | |
| 625 | // Verify that the old action was restored. |
| 626 | struct sigaction new_action; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 627 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 628 | EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction); |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 629 | // The SA_RESTORER flag gets set behind our back, so a direct comparison |
| 630 | // doesn't work unless we mask the value off. Mips doesn't have this |
| 631 | // flag, so skip this on that platform. |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 632 | #if defined(SA_RESTORER) |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 633 | cur_action.sa_flags &= ~SA_RESTORER; |
| 634 | new_action.sa_flags &= ~SA_RESTORER; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 635 | #elif defined(__GLIBC__) |
| 636 | // Our host compiler doesn't appear to define this flag for some reason. |
| 637 | cur_action.sa_flags &= ~0x04000000; |
| 638 | new_action.sa_flags &= ~0x04000000; |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 639 | #endif |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 640 | EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags); |
| 641 | } |
| 642 | |
| 643 | TEST(libbacktrace, thread_ignore_frames) { |
| 644 | pthread_attr_t attr; |
| 645 | pthread_attr_init(&attr); |
| 646 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 647 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 648 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 649 | pthread_t thread; |
| 650 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); |
| 651 | |
| 652 | // Wait up to 2 seconds for the tid to be set. |
| 653 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 654 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 655 | std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid)); |
| 656 | ASSERT_TRUE(all.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 657 | ASSERT_TRUE(all->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 658 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, all->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 659 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 660 | std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid)); |
| 661 | ASSERT_TRUE(ign1.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 662 | ASSERT_TRUE(ign1->Unwind(1)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 663 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign1->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 664 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 665 | std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid)); |
| 666 | ASSERT_TRUE(ign2.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 667 | ASSERT_TRUE(ign2->Unwind(2)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 668 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, ign2->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 669 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 670 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 671 | |
| 672 | // Tell the thread to exit its infinite loop. |
| 673 | android_atomic_acquire_store(0, &thread_data.state); |
| 674 | } |
| 675 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 676 | static void* ThreadMaxRun(void* data) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 677 | thread_t* thread = reinterpret_cast<thread_t*>(data); |
| 678 | |
| 679 | thread->tid = gettid(); |
| 680 | EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 681 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | TEST(libbacktrace, thread_max_trace) { |
| 685 | pthread_attr_t attr; |
| 686 | pthread_attr_init(&attr); |
| 687 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 688 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 689 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 690 | pthread_t thread; |
| 691 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0); |
| 692 | |
| 693 | // Wait for the tid to be set. |
| 694 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 695 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 696 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 697 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 698 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 699 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 700 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 701 | VerifyMaxDump(backtrace.get()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 702 | |
| 703 | // Tell the thread to exit its infinite loop. |
| 704 | android_atomic_acquire_store(0, &thread_data.state); |
| 705 | } |
| 706 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 707 | static void* ThreadDump(void* data) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 708 | dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data); |
| 709 | while (true) { |
| 710 | if (android_atomic_acquire_load(dump->now)) { |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 715 | // The status of the actual unwind will be checked elsewhere. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 716 | dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid); |
| 717 | dump->backtrace->Unwind(0); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 718 | |
| 719 | android_atomic_acquire_store(1, &dump->done); |
| 720 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 721 | return nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | TEST(libbacktrace, thread_multiple_dump) { |
| 725 | // Dump NUM_THREADS simultaneously. |
| 726 | std::vector<thread_t> runners(NUM_THREADS); |
| 727 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 728 | |
| 729 | pthread_attr_t attr; |
| 730 | pthread_attr_init(&attr); |
| 731 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 732 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 733 | // Launch the runners, they will spin in hard loops doing nothing. |
| 734 | runners[i].tid = 0; |
| 735 | runners[i].state = 0; |
| 736 | ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0); |
| 737 | } |
| 738 | |
| 739 | // Wait for tids to be set. |
| 740 | 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] | 741 | ASSERT_TRUE(WaitForNonZero(&it->state, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // Start all of the dumpers at once, they will spin until they are signalled |
| 745 | // to begin their dump run. |
| 746 | int32_t dump_now = 0; |
| 747 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 748 | dumpers[i].thread.tid = runners[i].tid; |
| 749 | dumpers[i].thread.state = 0; |
| 750 | dumpers[i].done = 0; |
| 751 | dumpers[i].now = &dump_now; |
| 752 | |
| 753 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 754 | } |
| 755 | |
| 756 | // Start all of the dumpers going at once. |
| 757 | android_atomic_acquire_store(1, &dump_now); |
| 758 | |
| 759 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 760 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 761 | |
| 762 | // Tell the runner thread to exit its infinite loop. |
| 763 | android_atomic_acquire_store(0, &runners[i].state); |
| 764 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 765 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 766 | VerifyMaxDump(dumpers[i].backtrace); |
| 767 | |
| 768 | delete dumpers[i].backtrace; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 769 | dumpers[i].backtrace = nullptr; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 773 | TEST(libbacktrace, thread_multiple_dump_same_thread) { |
| 774 | pthread_attr_t attr; |
| 775 | pthread_attr_init(&attr); |
| 776 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 777 | thread_t runner; |
| 778 | runner.tid = 0; |
| 779 | runner.state = 0; |
| 780 | ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0); |
| 781 | |
| 782 | // Wait for tids to be set. |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 783 | ASSERT_TRUE(WaitForNonZero(&runner.state, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 784 | |
| 785 | // Start all of the dumpers at once, they will spin until they are signalled |
| 786 | // to begin their dump run. |
| 787 | int32_t dump_now = 0; |
| 788 | // Dump the same thread NUM_THREADS simultaneously. |
| 789 | std::vector<dump_thread_t> dumpers(NUM_THREADS); |
| 790 | for (size_t i = 0; i < NUM_THREADS; i++) { |
| 791 | dumpers[i].thread.tid = runner.tid; |
| 792 | dumpers[i].thread.state = 0; |
| 793 | dumpers[i].done = 0; |
| 794 | dumpers[i].now = &dump_now; |
| 795 | |
| 796 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); |
| 797 | } |
| 798 | |
| 799 | // Start all of the dumpers going at once. |
| 800 | android_atomic_acquire_store(1, &dump_now); |
| 801 | |
| 802 | for (size_t i = 0; i < NUM_THREADS; i++) { |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 803 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 804 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 805 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 806 | VerifyMaxDump(dumpers[i].backtrace); |
| 807 | |
| 808 | delete dumpers[i].backtrace; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 809 | dumpers[i].backtrace = nullptr; |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Tell the runner thread to exit its infinite loop. |
| 813 | android_atomic_acquire_store(0, &runner.state); |
| 814 | } |
| 815 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 816 | // This test is for UnwindMaps that should share the same map cursor when |
| 817 | // multiple maps are created for the current process at the same time. |
| 818 | TEST(libbacktrace, simultaneous_maps) { |
| 819 | BacktraceMap* map1 = BacktraceMap::Create(getpid()); |
| 820 | BacktraceMap* map2 = BacktraceMap::Create(getpid()); |
| 821 | BacktraceMap* map3 = BacktraceMap::Create(getpid()); |
| 822 | |
| 823 | Backtrace* back1 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map1); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 824 | ASSERT_TRUE(back1 != nullptr); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 825 | EXPECT_TRUE(back1->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 826 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back1->GetError()); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 827 | delete back1; |
| 828 | delete map1; |
| 829 | |
| 830 | Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 831 | ASSERT_TRUE(back2 != nullptr); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 832 | EXPECT_TRUE(back2->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 833 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back2->GetError()); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 834 | delete back2; |
| 835 | delete map2; |
| 836 | |
| 837 | Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 838 | ASSERT_TRUE(back3 != nullptr); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 839 | EXPECT_TRUE(back3->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 840 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, back3->GetError()); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 841 | delete back3; |
| 842 | delete map3; |
| 843 | } |
| 844 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 845 | TEST(libbacktrace, fillin_erases) { |
| 846 | BacktraceMap* back_map = BacktraceMap::Create(getpid()); |
| 847 | |
| 848 | backtrace_map_t map; |
| 849 | |
| 850 | map.start = 1; |
| 851 | map.end = 3; |
| 852 | map.flags = 1; |
| 853 | map.name = "Initialized"; |
| 854 | back_map->FillIn(0, &map); |
| 855 | delete back_map; |
| 856 | |
| 857 | ASSERT_FALSE(BacktraceMap::IsValid(map)); |
| 858 | ASSERT_EQ(static_cast<uintptr_t>(0), map.start); |
| 859 | ASSERT_EQ(static_cast<uintptr_t>(0), map.end); |
| 860 | ASSERT_EQ(0, map.flags); |
| 861 | ASSERT_EQ("", map.name); |
| 862 | } |
| 863 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 864 | TEST(libbacktrace, format_test) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 865 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD)); |
| 866 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 867 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 868 | backtrace_frame_data_t frame; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 869 | frame.num = 1; |
| 870 | frame.pc = 2; |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 871 | frame.rel_pc = 2; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 872 | frame.sp = 0; |
| 873 | frame.stack_size = 0; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 874 | frame.func_offset = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 875 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 876 | // Check no map set. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 877 | frame.num = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 878 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 879 | EXPECT_EQ("#01 pc 0000000000000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 880 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 881 | EXPECT_EQ("#01 pc 00000002 <unknown>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 882 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 883 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 884 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 885 | // Check map name empty, but exists. |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 886 | frame.pc = 0xb0020; |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 887 | frame.rel_pc = 0x20; |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 888 | frame.map.start = 0xb0000; |
| 889 | frame.map.end = 0xbffff; |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 890 | frame.map.load_bias = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 891 | #if defined(__LP64__) |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 892 | EXPECT_EQ("#01 pc 0000000000000020 <anonymous:00000000000b0000>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 893 | #else |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 894 | EXPECT_EQ("#01 pc 00000020 <anonymous:000b0000>", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 895 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 896 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 897 | |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 898 | // Check map name begins with a [. |
| 899 | frame.pc = 0xc0020; |
| 900 | frame.map.start = 0xc0000; |
| 901 | frame.map.end = 0xcffff; |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 902 | frame.map.load_bias = 0; |
Christopher Ferris | da750a7 | 2015-11-30 13:36:08 -0800 | [diff] [blame] | 903 | frame.map.name = "[anon:thread signal stack]"; |
| 904 | #if defined(__LP64__) |
| 905 | EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]", |
| 906 | #else |
| 907 | EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]", |
| 908 | #endif |
| 909 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 910 | |
| 911 | // Check relative pc is set and map name is set. |
| 912 | frame.pc = 0x12345679; |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 913 | frame.rel_pc = 0x12345678; |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 914 | frame.map.name = "MapFake"; |
| 915 | frame.map.start = 1; |
| 916 | frame.map.end = 1; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 917 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 918 | EXPECT_EQ("#01 pc 0000000012345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 919 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 920 | EXPECT_EQ("#01 pc 12345678 MapFake", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 921 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 922 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 923 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 924 | // Check func_name is set, but no func offset. |
| 925 | frame.func_name = "ProcFake"; |
| 926 | #if defined(__LP64__) |
| 927 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake)", |
| 928 | #else |
| 929 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake)", |
| 930 | #endif |
| 931 | backtrace->FormatFrameData(&frame)); |
| 932 | |
| 933 | // Check func_name is set, and func offset is non-zero. |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 934 | frame.func_offset = 645; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 935 | #if defined(__LP64__) |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 936 | EXPECT_EQ("#01 pc 0000000012345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 937 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 938 | EXPECT_EQ("#01 pc 12345678 MapFake (ProcFake+645)", |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 939 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 940 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 2106f4b | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 941 | |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 942 | // Check func_name is set, func offset is non-zero, and load_bias is non-zero. |
| 943 | frame.rel_pc = 0x123456dc; |
Christopher Ferris | 2106f4b | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 944 | frame.func_offset = 645; |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 945 | frame.map.load_bias = 100; |
Christopher Ferris | 2106f4b | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 946 | #if defined(__LP64__) |
| 947 | EXPECT_EQ("#01 pc 00000000123456dc MapFake (ProcFake+645)", |
| 948 | #else |
| 949 | EXPECT_EQ("#01 pc 123456dc MapFake (ProcFake+645)", |
| 950 | #endif |
| 951 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | e0ab232 | 2015-08-20 11:16:54 -0700 | [diff] [blame] | 952 | |
| 953 | // Check a non-zero map offset. |
| 954 | frame.map.offset = 0x1000; |
| 955 | #if defined(__LP64__) |
| 956 | EXPECT_EQ("#01 pc 00000000123456dc MapFake (offset 0x1000) (ProcFake+645)", |
| 957 | #else |
| 958 | EXPECT_EQ("#01 pc 123456dc MapFake (offset 0x1000) (ProcFake+645)", |
| 959 | #endif |
| 960 | backtrace->FormatFrameData(&frame)); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 961 | } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 962 | |
| 963 | struct map_test_t { |
| 964 | uintptr_t start; |
| 965 | uintptr_t end; |
| 966 | }; |
| 967 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 968 | static bool map_sort(map_test_t i, map_test_t j) { return i.start < j.start; } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 969 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 970 | static void VerifyMap(pid_t pid) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 971 | char buffer[4096]; |
| 972 | snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid); |
| 973 | |
| 974 | FILE* map_file = fopen(buffer, "r"); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 975 | ASSERT_TRUE(map_file != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 976 | std::vector<map_test_t> test_maps; |
| 977 | while (fgets(buffer, sizeof(buffer), map_file)) { |
| 978 | map_test_t map; |
| 979 | ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end)); |
| 980 | test_maps.push_back(map); |
| 981 | } |
| 982 | fclose(map_file); |
| 983 | std::sort(test_maps.begin(), test_maps.end(), map_sort); |
| 984 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 985 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid)); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 986 | |
| 987 | // Basic test that verifies that the map is in the expected order. |
Christopher Ferris | 3a14004 | 2016-06-15 15:49:50 -0700 | [diff] [blame] | 988 | ScopedBacktraceMapIteratorLock lock(map.get()); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 989 | std::vector<map_test_t>::const_iterator test_it = test_maps.begin(); |
| 990 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { |
| 991 | ASSERT_TRUE(test_it != test_maps.end()); |
| 992 | ASSERT_EQ(test_it->start, it->start); |
| 993 | ASSERT_EQ(test_it->end, it->end); |
| 994 | ++test_it; |
| 995 | } |
| 996 | ASSERT_TRUE(test_it == test_maps.end()); |
| 997 | } |
| 998 | |
| 999 | TEST(libbacktrace, verify_map_remote) { |
| 1000 | pid_t pid; |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1001 | CreateRemoteProcess(&pid); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1002 | |
| 1003 | // The maps should match exactly since the forked process has been paused. |
| 1004 | VerifyMap(pid); |
| 1005 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1006 | FinishRemoteProcess(pid); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1009 | static void InitMemory(uint8_t* memory, size_t bytes) { |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 1010 | for (size_t i = 0; i < bytes; i++) { |
| 1011 | memory[i] = i; |
| 1012 | if (memory[i] == '\0') { |
| 1013 | // Don't use '\0' in our data so we can verify that an overread doesn't |
| 1014 | // occur by using a '\0' as the character after the read data. |
| 1015 | memory[i] = 23; |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1020 | static void* ThreadReadTest(void* data) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1021 | thread_t* thread_data = reinterpret_cast<thread_t*>(data); |
| 1022 | |
| 1023 | thread_data->tid = gettid(); |
| 1024 | |
| 1025 | // Create two map pages. |
| 1026 | // Mark the second page as not-readable. |
| 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 | return reinterpret_cast<void*>(-1); |
| 1031 | } |
| 1032 | |
| 1033 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { |
| 1034 | return reinterpret_cast<void*>(-1); |
| 1035 | } |
| 1036 | |
| 1037 | // Set up a simple pattern in memory. |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 1038 | InitMemory(memory, pagesize); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1039 | |
| 1040 | thread_data->data = memory; |
| 1041 | |
| 1042 | // Tell the caller it's okay to start reading memory. |
| 1043 | android_atomic_acquire_store(1, &thread_data->state); |
| 1044 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1045 | // Loop waiting for the caller to finish reading the memory. |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1046 | while (thread_data->state) { |
| 1047 | } |
| 1048 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1049 | // Re-enable read-write on the page so that we don't crash if we try |
| 1050 | // and access data on this page when freeing the memory. |
| 1051 | if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) { |
| 1052 | return reinterpret_cast<void*>(-1); |
| 1053 | } |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1054 | free(memory); |
| 1055 | |
| 1056 | android_atomic_acquire_store(1, &thread_data->state); |
| 1057 | |
| 1058 | return nullptr; |
| 1059 | } |
| 1060 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1061 | static void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1062 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 1063 | |
| 1064 | // Create a page of data to use to do quick compares. |
| 1065 | uint8_t* expected = new uint8_t[pagesize]; |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 1066 | InitMemory(expected, pagesize); |
| 1067 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1068 | uint8_t* data = new uint8_t[2*pagesize]; |
| 1069 | // Verify that we can only read one page worth of data. |
| 1070 | size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize); |
| 1071 | ASSERT_EQ(pagesize, bytes_read); |
| 1072 | ASSERT_TRUE(memcmp(data, expected, pagesize) == 0); |
| 1073 | |
| 1074 | // Verify unaligned reads. |
| 1075 | for (size_t i = 1; i < sizeof(word_t); i++) { |
| 1076 | bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t)); |
| 1077 | ASSERT_EQ(2 * sizeof(word_t), bytes_read); |
| 1078 | ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0) |
| 1079 | << "Offset at " << i << " failed"; |
| 1080 | } |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 1081 | |
| 1082 | // Verify small unaligned reads. |
| 1083 | for (size_t i = 1; i < sizeof(word_t); i++) { |
| 1084 | for (size_t j = 1; j < sizeof(word_t); j++) { |
| 1085 | // Set one byte past what we expect to read, to guarantee we don't overread. |
| 1086 | data[j] = '\0'; |
| 1087 | bytes_read = backtrace->Read(read_addr + i, data, j); |
| 1088 | ASSERT_EQ(j, bytes_read); |
| 1089 | ASSERT_TRUE(memcmp(data, &expected[i], j) == 0) |
| 1090 | << "Offset at " << i << " length " << j << " miscompared"; |
| 1091 | ASSERT_EQ('\0', data[j]) |
| 1092 | << "Offset at " << i << " length " << j << " wrote too much data"; |
| 1093 | } |
| 1094 | } |
Pirama Arumuga Nainar | 837eff2 | 2015-07-09 10:50:04 -0700 | [diff] [blame] | 1095 | delete[] data; |
| 1096 | delete[] expected; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | TEST(libbacktrace, thread_read) { |
| 1100 | pthread_attr_t attr; |
| 1101 | pthread_attr_init(&attr); |
| 1102 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 1103 | pthread_t thread; |
| 1104 | thread_t thread_data = { 0, 0, 0, nullptr }; |
| 1105 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0); |
| 1106 | |
| 1107 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); |
| 1108 | |
| 1109 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); |
| 1110 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1111 | |
| 1112 | RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data)); |
| 1113 | |
| 1114 | android_atomic_acquire_store(0, &thread_data.state); |
| 1115 | |
| 1116 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); |
| 1117 | } |
| 1118 | |
| 1119 | volatile uintptr_t g_ready = 0; |
| 1120 | volatile uintptr_t g_addr = 0; |
| 1121 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1122 | static void ForkedReadTest() { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1123 | // Create two map pages. |
| 1124 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 1125 | uint8_t* memory; |
| 1126 | if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) { |
| 1127 | perror("Failed to allocate memory\n"); |
| 1128 | exit(1); |
| 1129 | } |
| 1130 | |
| 1131 | // Mark the second page as not-readable. |
| 1132 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { |
| 1133 | perror("Failed to mprotect memory\n"); |
| 1134 | exit(1); |
| 1135 | } |
| 1136 | |
| 1137 | // Set up a simple pattern in memory. |
Christopher Ferris | 944f417 | 2015-05-06 16:36:34 -0700 | [diff] [blame] | 1138 | InitMemory(memory, pagesize); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1139 | |
| 1140 | g_addr = reinterpret_cast<uintptr_t>(memory); |
| 1141 | g_ready = 1; |
| 1142 | |
| 1143 | while (1) { |
| 1144 | usleep(US_PER_MSEC); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | TEST(libbacktrace, process_read) { |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1149 | g_ready = 0; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1150 | pid_t pid; |
| 1151 | if ((pid = fork()) == 0) { |
| 1152 | ForkedReadTest(); |
| 1153 | exit(0); |
| 1154 | } |
| 1155 | ASSERT_NE(-1, pid); |
| 1156 | |
| 1157 | bool test_executed = false; |
| 1158 | uint64_t start = NanoTime(); |
| 1159 | while (1) { |
| 1160 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) { |
| 1161 | WaitForStop(pid); |
| 1162 | |
| 1163 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid)); |
Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 1164 | ASSERT_TRUE(backtrace.get() != nullptr); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1165 | |
| 1166 | uintptr_t read_addr; |
| 1167 | size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), |
| 1168 | reinterpret_cast<uint8_t*>(&read_addr), |
| 1169 | sizeof(uintptr_t)); |
| 1170 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); |
| 1171 | if (read_addr) { |
| 1172 | // The forked process is ready to be read. |
| 1173 | bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), |
| 1174 | reinterpret_cast<uint8_t*>(&read_addr), |
| 1175 | sizeof(uintptr_t)); |
| 1176 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); |
| 1177 | |
| 1178 | RunReadTest(backtrace.get(), read_addr); |
| 1179 | |
| 1180 | test_executed = true; |
| 1181 | break; |
| 1182 | } |
| 1183 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1184 | } |
| 1185 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { |
| 1186 | break; |
| 1187 | } |
| 1188 | usleep(US_PER_MSEC); |
| 1189 | } |
| 1190 | kill(pid, SIGKILL); |
| 1191 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 1192 | |
| 1193 | ASSERT_TRUE(test_executed); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1196 | static void VerifyFunctionsFound(const std::vector<std::string>& found_functions) { |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1197 | // We expect to find these functions in libbacktrace_test. If we don't |
| 1198 | // find them, that's a bug in the memory read handling code in libunwind. |
| 1199 | std::list<std::string> expected_functions; |
| 1200 | expected_functions.push_back("test_recursive_call"); |
| 1201 | expected_functions.push_back("test_level_one"); |
| 1202 | expected_functions.push_back("test_level_two"); |
| 1203 | expected_functions.push_back("test_level_three"); |
| 1204 | expected_functions.push_back("test_level_four"); |
| 1205 | for (const auto& found_function : found_functions) { |
| 1206 | for (const auto& expected_function : expected_functions) { |
| 1207 | if (found_function == expected_function) { |
| 1208 | expected_functions.remove(found_function); |
| 1209 | break; |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library."; |
| 1214 | } |
| 1215 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1216 | static const char* CopySharedLibrary() { |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1217 | #if defined(__LP64__) |
| 1218 | const char* lib_name = "lib64"; |
| 1219 | #else |
| 1220 | const char* lib_name = "lib"; |
| 1221 | #endif |
| 1222 | |
| 1223 | #if defined(__BIONIC__) |
| 1224 | const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so"; |
| 1225 | std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s", |
| 1226 | lib_name, tmp_so_name); |
| 1227 | #else |
| 1228 | const char* tmp_so_name = "/tmp/libbacktrace_test.so"; |
| 1229 | if (getenv("ANDROID_HOST_OUT") == NULL) { |
| 1230 | fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch."); |
| 1231 | return nullptr; |
| 1232 | } |
| 1233 | std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s", |
| 1234 | getenv("ANDROID_HOST_OUT"), lib_name, |
| 1235 | tmp_so_name); |
| 1236 | #endif |
| 1237 | |
| 1238 | // Copy the shared so to a tempory directory. |
| 1239 | system(cp_cmd.c_str()); |
| 1240 | |
| 1241 | return tmp_so_name; |
| 1242 | } |
| 1243 | |
| 1244 | TEST(libbacktrace, check_unreadable_elf_local) { |
| 1245 | const char* tmp_so_name = CopySharedLibrary(); |
| 1246 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1247 | |
| 1248 | struct stat buf; |
| 1249 | ASSERT_TRUE(stat(tmp_so_name, &buf) != -1); |
| 1250 | uintptr_t map_size = buf.st_size; |
| 1251 | |
| 1252 | int fd = open(tmp_so_name, O_RDONLY); |
| 1253 | ASSERT_TRUE(fd != -1); |
| 1254 | |
Christopher Ferris | 61c48ac | 2016-01-15 16:08:58 -0800 | [diff] [blame] | 1255 | 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] | 1256 | ASSERT_TRUE(map != MAP_FAILED); |
| 1257 | close(fd); |
| 1258 | ASSERT_TRUE(unlink(tmp_so_name) != -1); |
| 1259 | |
| 1260 | std::vector<std::string> found_functions; |
| 1261 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, |
| 1262 | BACKTRACE_CURRENT_THREAD)); |
| 1263 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 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 += reinterpret_cast<uintptr_t>(map); |
| 1270 | std::string last_func; |
| 1271 | for (uintptr_t read_addr = reinterpret_cast<uintptr_t>(map); |
| 1272 | read_addr < map_size; read_addr += 4) { |
| 1273 | uintptr_t offset; |
| 1274 | std::string func_name = backtrace->GetFunctionName(read_addr, &offset); |
| 1275 | if (!func_name.empty() && last_func != func_name) { |
| 1276 | found_functions.push_back(func_name); |
| 1277 | } |
| 1278 | last_func = func_name; |
| 1279 | } |
| 1280 | |
| 1281 | ASSERT_TRUE(munmap(map, map_size - reinterpret_cast<uintptr_t>(map)) == 0); |
| 1282 | |
| 1283 | VerifyFunctionsFound(found_functions); |
| 1284 | } |
| 1285 | |
| 1286 | TEST(libbacktrace, check_unreadable_elf_remote) { |
| 1287 | const char* tmp_so_name = CopySharedLibrary(); |
| 1288 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1289 | |
| 1290 | g_ready = 0; |
| 1291 | |
| 1292 | struct stat buf; |
| 1293 | ASSERT_TRUE(stat(tmp_so_name, &buf) != -1); |
| 1294 | uintptr_t map_size = buf.st_size; |
| 1295 | |
| 1296 | pid_t pid; |
| 1297 | if ((pid = fork()) == 0) { |
| 1298 | int fd = open(tmp_so_name, O_RDONLY); |
| 1299 | if (fd == -1) { |
| 1300 | fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno)); |
| 1301 | unlink(tmp_so_name); |
| 1302 | exit(0); |
| 1303 | } |
| 1304 | |
Christopher Ferris | 61c48ac | 2016-01-15 16:08:58 -0800 | [diff] [blame] | 1305 | 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] | 1306 | if (map == MAP_FAILED) { |
| 1307 | fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno)); |
| 1308 | unlink(tmp_so_name); |
| 1309 | exit(0); |
| 1310 | } |
| 1311 | close(fd); |
| 1312 | if (unlink(tmp_so_name) == -1) { |
| 1313 | fprintf(stderr, "Failed to unlink: %s\n", strerror(errno)); |
| 1314 | exit(0); |
| 1315 | } |
| 1316 | |
| 1317 | g_addr = reinterpret_cast<uintptr_t>(map); |
| 1318 | g_ready = 1; |
| 1319 | while (true) { |
| 1320 | usleep(US_PER_MSEC); |
| 1321 | } |
| 1322 | exit(0); |
| 1323 | } |
| 1324 | ASSERT_TRUE(pid > 0); |
| 1325 | |
| 1326 | std::vector<std::string> found_functions; |
| 1327 | uint64_t start = NanoTime(); |
| 1328 | while (true) { |
| 1329 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1330 | |
| 1331 | // Wait for the process to get to a stopping point. |
| 1332 | WaitForStop(pid); |
| 1333 | |
| 1334 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD)); |
| 1335 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1336 | |
| 1337 | uintptr_t read_addr; |
| 1338 | ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t))); |
| 1339 | if (read_addr) { |
| 1340 | ASSERT_EQ(sizeof(uintptr_t), backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), reinterpret_cast<uint8_t*>(&read_addr), sizeof(uintptr_t))); |
| 1341 | |
| 1342 | // Needed before GetFunctionName will work. |
| 1343 | backtrace->Unwind(0); |
| 1344 | |
| 1345 | // Loop through the entire map, and get every function we can find. |
| 1346 | map_size += read_addr; |
| 1347 | std::string last_func; |
| 1348 | for (; read_addr < map_size; read_addr += 4) { |
| 1349 | uintptr_t offset; |
| 1350 | std::string func_name = backtrace->GetFunctionName(read_addr, &offset); |
| 1351 | if (!func_name.empty() && last_func != func_name) { |
| 1352 | found_functions.push_back(func_name); |
| 1353 | } |
| 1354 | last_func = func_name; |
| 1355 | } |
| 1356 | break; |
| 1357 | } |
| 1358 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1359 | |
| 1360 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { |
| 1361 | break; |
| 1362 | } |
| 1363 | usleep(US_PER_MSEC); |
| 1364 | } |
| 1365 | |
| 1366 | kill(pid, SIGKILL); |
| 1367 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 1368 | |
| 1369 | VerifyFunctionsFound(found_functions); |
| 1370 | } |
| 1371 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1372 | static bool FindFuncFrameInBacktrace(Backtrace* backtrace, uintptr_t test_func, size_t* frame_num) { |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1373 | backtrace_map_t map; |
| 1374 | backtrace->FillInMap(test_func, &map); |
| 1375 | if (!BacktraceMap::IsValid(map)) { |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
| 1379 | // Loop through the frames, and find the one that is in the map. |
| 1380 | *frame_num = 0; |
| 1381 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { |
| 1382 | if (BacktraceMap::IsValid(it->map) && map.start == it->map.start && |
| 1383 | it->pc >= test_func) { |
| 1384 | *frame_num = it->num; |
| 1385 | return true; |
| 1386 | } |
| 1387 | } |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1391 | static void VerifyUnreadableElfFrame(Backtrace* backtrace, uintptr_t test_func, size_t frame_num) { |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1392 | ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) |
| 1393 | << DumpFrames(backtrace); |
| 1394 | |
| 1395 | ASSERT_TRUE(frame_num != 0) << DumpFrames(backtrace); |
| 1396 | // Make sure that there is at least one more frame above the test func call. |
| 1397 | ASSERT_LT(frame_num, backtrace->NumFrames()) << DumpFrames(backtrace); |
| 1398 | |
| 1399 | uintptr_t diff = backtrace->GetFrame(frame_num)->pc - test_func; |
| 1400 | ASSERT_LT(diff, 200U) << DumpFrames(backtrace); |
| 1401 | } |
| 1402 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1403 | static void VerifyUnreadableElfBacktrace(uintptr_t test_func) { |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1404 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, |
| 1405 | BACKTRACE_CURRENT_THREAD)); |
| 1406 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1407 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 1408 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1409 | |
| 1410 | size_t frame_num; |
| 1411 | ASSERT_TRUE(FindFuncFrameInBacktrace(backtrace.get(), test_func, &frame_num)); |
| 1412 | |
| 1413 | VerifyUnreadableElfFrame(backtrace.get(), test_func, frame_num); |
| 1414 | } |
| 1415 | |
| 1416 | typedef int (*test_func_t)(int, int, int, int, void (*)(uintptr_t), uintptr_t); |
| 1417 | |
| 1418 | TEST(libbacktrace, unwind_through_unreadable_elf_local) { |
| 1419 | const char* tmp_so_name = CopySharedLibrary(); |
| 1420 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1421 | void* lib_handle = dlopen(tmp_so_name, RTLD_NOW); |
| 1422 | ASSERT_TRUE(lib_handle != nullptr); |
| 1423 | ASSERT_TRUE(unlink(tmp_so_name) != -1); |
| 1424 | |
| 1425 | test_func_t test_func; |
| 1426 | test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one")); |
| 1427 | ASSERT_TRUE(test_func != nullptr); |
| 1428 | |
| 1429 | ASSERT_NE(test_func(1, 2, 3, 4, VerifyUnreadableElfBacktrace, |
| 1430 | reinterpret_cast<uintptr_t>(test_func)), 0); |
| 1431 | |
| 1432 | ASSERT_TRUE(dlclose(lib_handle) == 0); |
| 1433 | } |
| 1434 | |
| 1435 | TEST(libbacktrace, unwind_through_unreadable_elf_remote) { |
| 1436 | const char* tmp_so_name = CopySharedLibrary(); |
| 1437 | ASSERT_TRUE(tmp_so_name != nullptr); |
| 1438 | void* lib_handle = dlopen(tmp_so_name, RTLD_NOW); |
| 1439 | ASSERT_TRUE(lib_handle != nullptr); |
| 1440 | ASSERT_TRUE(unlink(tmp_so_name) != -1); |
| 1441 | |
| 1442 | test_func_t test_func; |
| 1443 | test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one")); |
| 1444 | ASSERT_TRUE(test_func != nullptr); |
| 1445 | |
| 1446 | pid_t pid; |
| 1447 | if ((pid = fork()) == 0) { |
| 1448 | test_func(1, 2, 3, 4, 0, 0); |
| 1449 | exit(0); |
| 1450 | } |
| 1451 | ASSERT_TRUE(pid > 0); |
| 1452 | ASSERT_TRUE(dlclose(lib_handle) == 0); |
| 1453 | |
| 1454 | uint64_t start = NanoTime(); |
| 1455 | bool done = false; |
| 1456 | while (!done) { |
| 1457 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1458 | |
| 1459 | // Wait for the process to get to a stopping point. |
| 1460 | WaitForStop(pid); |
| 1461 | |
| 1462 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD)); |
| 1463 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1464 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 1465 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | 67aba68 | 2015-05-08 15:44:46 -0700 | [diff] [blame] | 1466 | |
| 1467 | size_t frame_num; |
| 1468 | if (FindFuncFrameInBacktrace(backtrace.get(), |
| 1469 | reinterpret_cast<uintptr_t>(test_func), &frame_num)) { |
| 1470 | |
| 1471 | VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uintptr_t>(test_func), frame_num); |
| 1472 | done = true; |
| 1473 | } |
| 1474 | |
| 1475 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1476 | |
| 1477 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { |
| 1478 | break; |
| 1479 | } |
| 1480 | usleep(US_PER_MSEC); |
| 1481 | } |
| 1482 | |
| 1483 | kill(pid, SIGKILL); |
| 1484 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); |
| 1485 | |
| 1486 | ASSERT_TRUE(done) << "Test function never found in unwind."; |
| 1487 | } |
| 1488 | |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 1489 | TEST(libbacktrace, unwind_thread_doesnt_exist) { |
| 1490 | std::unique_ptr<Backtrace> backtrace( |
| 1491 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, 99999999)); |
| 1492 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1493 | ASSERT_FALSE(backtrace->Unwind(0)); |
| 1494 | ASSERT_EQ(BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST, backtrace->GetError()); |
| 1495 | } |
| 1496 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1497 | TEST(libbacktrace, local_get_function_name_before_unwind) { |
| 1498 | std::unique_ptr<Backtrace> backtrace( |
| 1499 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
| 1500 | ASSERT_TRUE(backtrace.get() != nullptr); |
| 1501 | |
| 1502 | // Verify that trying to get a function name before doing an unwind works. |
| 1503 | uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1; |
| 1504 | size_t offset; |
| 1505 | ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset)); |
| 1506 | } |
| 1507 | |
| 1508 | TEST(libbacktrace, remote_get_function_name_before_unwind) { |
| 1509 | pid_t pid; |
| 1510 | CreateRemoteProcess(&pid); |
| 1511 | |
| 1512 | // Now create an unwind object. |
| 1513 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid)); |
| 1514 | |
| 1515 | // Verify that trying to get a function name before doing an unwind works. |
| 1516 | uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1; |
| 1517 | size_t offset; |
| 1518 | ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset)); |
| 1519 | |
| 1520 | FinishRemoteProcess(pid); |
| 1521 | } |
| 1522 | |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 1523 | static void SetUcontextSp(uintptr_t sp, ucontext_t* ucontext) { |
| 1524 | #if defined(__arm__) |
| 1525 | ucontext->uc_mcontext.arm_sp = sp; |
| 1526 | #elif defined(__aarch64__) |
| 1527 | ucontext->uc_mcontext.sp = sp; |
| 1528 | #elif defined(__i386__) |
| 1529 | ucontext->uc_mcontext.gregs[REG_ESP] = sp; |
| 1530 | #elif defined(__x86_64__) |
| 1531 | ucontext->uc_mcontext.gregs[REG_RSP] = sp; |
| 1532 | #else |
| 1533 | UNUSED(sp); |
| 1534 | UNUSED(ucontext); |
| 1535 | ASSERT_TRUE(false) << "Unsupported architecture"; |
| 1536 | #endif |
| 1537 | } |
| 1538 | |
| 1539 | static void SetUcontextPc(uintptr_t pc, ucontext_t* ucontext) { |
| 1540 | #if defined(__arm__) |
| 1541 | ucontext->uc_mcontext.arm_pc = pc; |
| 1542 | #elif defined(__aarch64__) |
| 1543 | ucontext->uc_mcontext.pc = pc; |
| 1544 | #elif defined(__i386__) |
| 1545 | ucontext->uc_mcontext.gregs[REG_EIP] = pc; |
| 1546 | #elif defined(__x86_64__) |
| 1547 | ucontext->uc_mcontext.gregs[REG_RIP] = pc; |
| 1548 | #else |
| 1549 | UNUSED(pc); |
| 1550 | UNUSED(ucontext); |
| 1551 | ASSERT_TRUE(false) << "Unsupported architecture"; |
| 1552 | #endif |
| 1553 | } |
| 1554 | |
| 1555 | static void SetUcontextLr(uintptr_t lr, ucontext_t* ucontext) { |
| 1556 | #if defined(__arm__) |
| 1557 | ucontext->uc_mcontext.arm_lr = lr; |
| 1558 | #elif defined(__aarch64__) |
| 1559 | ucontext->uc_mcontext.regs[30] = lr; |
| 1560 | #elif defined(__i386__) |
| 1561 | // The lr is on the stack. |
| 1562 | ASSERT_TRUE(lr != 0); |
| 1563 | ASSERT_TRUE(ucontext != nullptr); |
| 1564 | #elif defined(__x86_64__) |
| 1565 | // The lr is on the stack. |
| 1566 | ASSERT_TRUE(lr != 0); |
| 1567 | ASSERT_TRUE(ucontext != nullptr); |
| 1568 | #else |
| 1569 | UNUSED(lr); |
| 1570 | UNUSED(ucontext); |
| 1571 | ASSERT_TRUE(false) << "Unsupported architecture"; |
| 1572 | #endif |
| 1573 | } |
| 1574 | |
| 1575 | static constexpr size_t DEVICE_MAP_SIZE = 1024; |
| 1576 | |
| 1577 | static void SetupDeviceMap(void** device_map) { |
| 1578 | // Make sure that anything in a device map will result in fails |
| 1579 | // to read. |
| 1580 | android::base::unique_fd device_fd(open("/dev/zero", O_RDONLY | O_CLOEXEC)); |
| 1581 | |
| 1582 | *device_map = mmap(nullptr, 1024, PROT_READ, MAP_PRIVATE, device_fd, 0); |
| 1583 | ASSERT_TRUE(*device_map != MAP_FAILED); |
| 1584 | |
| 1585 | // Make sure the map is readable. |
| 1586 | ASSERT_EQ(0, reinterpret_cast<int*>(*device_map)[0]); |
| 1587 | } |
| 1588 | |
| 1589 | static void UnwindFromDevice(Backtrace* backtrace, void* device_map) { |
| 1590 | uintptr_t device_map_uint = reinterpret_cast<uintptr_t>(device_map); |
| 1591 | |
| 1592 | backtrace_map_t map; |
| 1593 | backtrace->FillInMap(device_map_uint, &map); |
| 1594 | // Verify the flag is set. |
| 1595 | ASSERT_EQ(PROT_DEVICE_MAP, map.flags & PROT_DEVICE_MAP); |
| 1596 | |
| 1597 | // Quick sanity checks. |
| 1598 | size_t offset; |
| 1599 | ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset)); |
| 1600 | ASSERT_EQ(std::string(""), backtrace->GetFunctionName(device_map_uint, &offset, &map)); |
| 1601 | ASSERT_EQ(std::string(""), backtrace->GetFunctionName(0, &offset)); |
| 1602 | |
| 1603 | uintptr_t cur_func_offset = reinterpret_cast<uintptr_t>(&test_level_one) + 1; |
| 1604 | // Now verify the device map flag actually causes the function name to be empty. |
| 1605 | backtrace->FillInMap(cur_func_offset, &map); |
| 1606 | ASSERT_TRUE((map.flags & PROT_DEVICE_MAP) == 0); |
| 1607 | ASSERT_NE(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map)); |
| 1608 | map.flags |= PROT_DEVICE_MAP; |
| 1609 | ASSERT_EQ(std::string(""), backtrace->GetFunctionName(cur_func_offset, &offset, &map)); |
| 1610 | |
| 1611 | ucontext_t ucontext; |
| 1612 | |
| 1613 | // Create a context that has the pc in the device map, but the sp |
| 1614 | // in a non-device map. |
| 1615 | memset(&ucontext, 0, sizeof(ucontext)); |
| 1616 | SetUcontextSp(reinterpret_cast<uintptr_t>(&ucontext), &ucontext); |
| 1617 | SetUcontextPc(device_map_uint, &ucontext); |
| 1618 | SetUcontextLr(cur_func_offset, &ucontext); |
| 1619 | |
| 1620 | ASSERT_TRUE(backtrace->Unwind(0, &ucontext)); |
| 1621 | |
| 1622 | // The buffer should only be a single element. |
| 1623 | ASSERT_EQ(1U, backtrace->NumFrames()); |
| 1624 | const backtrace_frame_data_t* frame = backtrace->GetFrame(0); |
| 1625 | ASSERT_EQ(device_map_uint, frame->pc); |
| 1626 | ASSERT_EQ(reinterpret_cast<uintptr_t>(&ucontext), frame->sp); |
| 1627 | |
| 1628 | // Check what happens when skipping the first frame. |
| 1629 | ASSERT_TRUE(backtrace->Unwind(1, &ucontext)); |
| 1630 | ASSERT_EQ(0U, backtrace->NumFrames()); |
| 1631 | |
| 1632 | // Create a context that has the sp in the device map, but the pc |
| 1633 | // in a non-device map. |
| 1634 | memset(&ucontext, 0, sizeof(ucontext)); |
| 1635 | SetUcontextSp(device_map_uint, &ucontext); |
| 1636 | SetUcontextPc(cur_func_offset, &ucontext); |
| 1637 | SetUcontextLr(cur_func_offset, &ucontext); |
| 1638 | |
| 1639 | ASSERT_TRUE(backtrace->Unwind(0, &ucontext)); |
| 1640 | |
| 1641 | // The buffer should only be a single element. |
| 1642 | ASSERT_EQ(1U, backtrace->NumFrames()); |
| 1643 | frame = backtrace->GetFrame(0); |
| 1644 | ASSERT_EQ(cur_func_offset, frame->pc); |
| 1645 | ASSERT_EQ(device_map_uint, frame->sp); |
| 1646 | |
| 1647 | // Check what happens when skipping the first frame. |
| 1648 | ASSERT_TRUE(backtrace->Unwind(1, &ucontext)); |
| 1649 | ASSERT_EQ(0U, backtrace->NumFrames()); |
| 1650 | } |
| 1651 | |
| 1652 | TEST(libbacktrace, unwind_disallow_device_map_local) { |
| 1653 | void* device_map; |
| 1654 | SetupDeviceMap(&device_map); |
| 1655 | |
| 1656 | // Now create an unwind object. |
| 1657 | std::unique_ptr<Backtrace> backtrace( |
| 1658 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); |
| 1659 | ASSERT_TRUE(backtrace); |
| 1660 | |
| 1661 | UnwindFromDevice(backtrace.get(), device_map); |
| 1662 | |
| 1663 | munmap(device_map, DEVICE_MAP_SIZE); |
| 1664 | } |
| 1665 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 1666 | TEST(libbacktrace, unwind_disallow_device_map_remote_new) { |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 1667 | void* device_map; |
| 1668 | SetupDeviceMap(&device_map); |
| 1669 | |
| 1670 | // Fork a process to do a remote backtrace. |
| 1671 | pid_t pid; |
| 1672 | CreateRemoteProcess(&pid); |
| 1673 | |
| 1674 | // Now create an unwind object. |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 1675 | std::unique_ptr<BacktraceMap> map(BacktraceMap::CreateNew(pid)); |
| 1676 | ASSERT_TRUE(map.get() != nullptr); |
| 1677 | std::unique_ptr<Backtrace> backtrace(Backtrace::CreateNew(pid, pid, map.get())); |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 1678 | |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 1679 | UnwindFromDevice(backtrace.get(), device_map); |
Christopher Ferris | f5e568e | 2017-03-22 13:18:31 -0700 | [diff] [blame] | 1680 | |
| 1681 | FinishRemoteProcess(pid); |
| 1682 | |
| 1683 | munmap(device_map, DEVICE_MAP_SIZE); |
| 1684 | } |
| 1685 | |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 1686 | class ScopedSignalHandler { |
| 1687 | public: |
| 1688 | ScopedSignalHandler(int signal_number, void (*handler)(int)) : signal_number_(signal_number) { |
| 1689 | memset(&action_, 0, sizeof(action_)); |
| 1690 | action_.sa_handler = handler; |
| 1691 | sigaction(signal_number_, &action_, &old_action_); |
| 1692 | } |
| 1693 | |
| 1694 | ScopedSignalHandler(int signal_number, void (*action)(int, siginfo_t*, void*)) |
| 1695 | : signal_number_(signal_number) { |
| 1696 | memset(&action_, 0, sizeof(action_)); |
| 1697 | action_.sa_flags = SA_SIGINFO; |
| 1698 | action_.sa_sigaction = action; |
| 1699 | sigaction(signal_number_, &action_, &old_action_); |
| 1700 | } |
| 1701 | |
| 1702 | ~ScopedSignalHandler() { sigaction(signal_number_, &old_action_, nullptr); } |
| 1703 | |
| 1704 | private: |
| 1705 | struct sigaction action_; |
| 1706 | struct sigaction old_action_; |
| 1707 | const int signal_number_; |
| 1708 | }; |
| 1709 | |
| 1710 | static void SetValueAndLoop(void* data) { |
| 1711 | volatile int* value = reinterpret_cast<volatile int*>(data); |
| 1712 | |
| 1713 | *value = 1; |
| 1714 | for (volatile int i = 0;; i++) |
| 1715 | ; |
| 1716 | } |
| 1717 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 1718 | static void UnwindThroughSignal(bool use_action, create_func_t create_func, |
| 1719 | map_create_func_t map_create_func) { |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 1720 | volatile int value = 0; |
| 1721 | pid_t pid; |
| 1722 | if ((pid = fork()) == 0) { |
| 1723 | if (use_action) { |
| 1724 | ScopedSignalHandler ssh(SIGUSR1, test_signal_action); |
| 1725 | |
| 1726 | test_level_one(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value)); |
| 1727 | } else { |
| 1728 | ScopedSignalHandler ssh(SIGUSR1, test_signal_handler); |
| 1729 | |
| 1730 | test_level_one(1, 2, 3, 4, SetValueAndLoop, const_cast<int*>(&value)); |
| 1731 | } |
| 1732 | } |
| 1733 | ASSERT_NE(-1, pid); |
| 1734 | |
| 1735 | int read_value = 0; |
| 1736 | uint64_t start = NanoTime(); |
| 1737 | while (read_value == 0) { |
| 1738 | usleep(1000); |
| 1739 | |
| 1740 | // Loop until the remote function gets into the final function. |
| 1741 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1742 | |
| 1743 | WaitForStop(pid); |
| 1744 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 1745 | std::unique_ptr<BacktraceMap> map(map_create_func(pid, false)); |
| 1746 | std::unique_ptr<Backtrace> backtrace(create_func(pid, pid, map.get())); |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 1747 | |
| 1748 | size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(const_cast<int*>(&value)), |
| 1749 | reinterpret_cast<uint8_t*>(&read_value), sizeof(read_value)); |
| 1750 | ASSERT_EQ(sizeof(read_value), bytes_read); |
| 1751 | |
| 1752 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1753 | |
| 1754 | ASSERT_TRUE(NanoTime() - start < 5 * NS_PER_SEC) |
| 1755 | << "Remote process did not execute far enough in 5 seconds."; |
| 1756 | } |
| 1757 | |
| 1758 | // Now need to send a signal to the remote process. |
| 1759 | kill(pid, SIGUSR1); |
| 1760 | |
| 1761 | // Wait for the process to get to the signal handler loop. |
| 1762 | Backtrace::const_iterator frame_iter; |
| 1763 | start = NanoTime(); |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 1764 | std::unique_ptr<BacktraceMap> map; |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 1765 | std::unique_ptr<Backtrace> backtrace; |
| 1766 | while (true) { |
| 1767 | usleep(1000); |
| 1768 | |
| 1769 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); |
| 1770 | |
| 1771 | WaitForStop(pid); |
| 1772 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 1773 | map.reset(map_create_func(pid, false)); |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 1774 | ASSERT_TRUE(map.get() != nullptr); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 1775 | backtrace.reset(create_func(pid, pid, map.get())); |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 1776 | ASSERT_TRUE(backtrace->Unwind(0)); |
| 1777 | bool found = false; |
| 1778 | for (frame_iter = backtrace->begin(); frame_iter != backtrace->end(); ++frame_iter) { |
| 1779 | if (frame_iter->func_name == "test_loop_forever") { |
| 1780 | ++frame_iter; |
| 1781 | found = true; |
| 1782 | break; |
| 1783 | } |
| 1784 | } |
| 1785 | if (found) { |
| 1786 | break; |
| 1787 | } |
| 1788 | |
| 1789 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); |
| 1790 | |
| 1791 | ASSERT_TRUE(NanoTime() - start < 5 * NS_PER_SEC) |
| 1792 | << "Remote process did not get in signal handler in 5 seconds." << std::endl |
| 1793 | << DumpFrames(backtrace.get()); |
| 1794 | } |
| 1795 | |
| 1796 | std::vector<std::string> names; |
| 1797 | // Loop through the frames, and save the function names. |
| 1798 | size_t frame = 0; |
| 1799 | for (; frame_iter != backtrace->end(); ++frame_iter) { |
| 1800 | if (frame_iter->func_name == "test_level_four") { |
| 1801 | frame = names.size() + 1; |
| 1802 | } |
| 1803 | names.push_back(frame_iter->func_name); |
| 1804 | } |
| 1805 | ASSERT_NE(0U, frame) << "Unable to find test_level_four in backtrace" << std::endl |
| 1806 | << DumpFrames(backtrace.get()); |
| 1807 | |
| 1808 | // The expected order of the frames: |
| 1809 | // test_loop_forever |
| 1810 | // test_signal_handler|test_signal_action |
| 1811 | // <OPTIONAL_FRAME> May or may not exist. |
| 1812 | // SetValueAndLoop (but the function name might be empty) |
| 1813 | // test_level_four |
| 1814 | // test_level_three |
| 1815 | // test_level_two |
| 1816 | // test_level_one |
| 1817 | ASSERT_LE(frame + 2, names.size()) << DumpFrames(backtrace.get()); |
| 1818 | ASSERT_LE(2U, frame) << DumpFrames(backtrace.get()); |
| 1819 | if (use_action) { |
| 1820 | ASSERT_EQ("test_signal_action", names[0]) << DumpFrames(backtrace.get()); |
| 1821 | } else { |
| 1822 | ASSERT_EQ("test_signal_handler", names[0]) << DumpFrames(backtrace.get()); |
| 1823 | } |
| 1824 | ASSERT_EQ("test_level_three", names[frame]) << DumpFrames(backtrace.get()); |
| 1825 | ASSERT_EQ("test_level_two", names[frame + 1]) << DumpFrames(backtrace.get()); |
| 1826 | ASSERT_EQ("test_level_one", names[frame + 2]) << DumpFrames(backtrace.get()); |
| 1827 | |
| 1828 | FinishRemoteProcess(pid); |
| 1829 | } |
| 1830 | |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 1831 | TEST(libbacktrace, unwind_remote_through_signal_using_handler) { |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 1832 | UnwindThroughSignal(false, Backtrace::Create, BacktraceMap::Create); |
| 1833 | } |
| 1834 | |
| 1835 | TEST(libbacktrace, unwind_remote_through_signal_using_handler_new) { |
| 1836 | UnwindThroughSignal(false, Backtrace::CreateNew, BacktraceMap::CreateNew); |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 1837 | } |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 1838 | |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 1839 | TEST(libbacktrace, unwind_remote_through_signal_using_action) { |
Christopher Ferris | 458cc66 | 2017-08-28 16:31:18 -0700 | [diff] [blame] | 1840 | UnwindThroughSignal(true, Backtrace::Create, BacktraceMap::Create); |
| 1841 | } |
| 1842 | |
| 1843 | TEST(libbacktrace, unwind_remote_through_signal_using_action_new) { |
| 1844 | UnwindThroughSignal(true, Backtrace::CreateNew, BacktraceMap::CreateNew); |
Christopher Ferris | 96722b0 | 2017-07-19 14:20:46 -0700 | [diff] [blame] | 1845 | } |
Christopher Ferris | 5ea2c1f | 2017-03-23 14:55:01 -0700 | [diff] [blame] | 1846 | |
Josh Gao | cd546c1 | 2017-10-25 15:21:45 -0700 | [diff] [blame^] | 1847 | static void TestFrameSkipNumbering(create_func_t create_func, map_create_func_t map_create_func) { |
| 1848 | std::unique_ptr<BacktraceMap> map(map_create_func(getpid(), false)); |
| 1849 | std::unique_ptr<Backtrace> backtrace(create_func(getpid(), gettid(), map.get())); |
| 1850 | backtrace->Unwind(1); |
| 1851 | ASSERT_NE(0U, backtrace->NumFrames()); |
| 1852 | ASSERT_EQ(0U, backtrace->GetFrame(0)->num); |
| 1853 | } |
| 1854 | |
| 1855 | TEST(libbacktrace, unwind_frame_skip_numbering) { |
| 1856 | TestFrameSkipNumbering(Backtrace::Create, BacktraceMap::Create); |
| 1857 | } |
| 1858 | |
| 1859 | TEST(libbacktrace, unwind_frame_skip_numbering_new) { |
| 1860 | TestFrameSkipNumbering(Backtrace::CreateNew, BacktraceMap::CreateNew); |
| 1861 | } |
| 1862 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1863 | #if defined(ENABLE_PSS_TESTS) |
| 1864 | #include "GetPss.h" |
| 1865 | |
Chih-Hung Hsieh | 67867db | 2016-05-18 15:53:15 -0700 | [diff] [blame] | 1866 | #define MAX_LEAK_BYTES (32*1024UL) |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1867 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1868 | static void CheckForLeak(pid_t pid, pid_t tid) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1869 | // Do a few runs to get the PSS stable. |
| 1870 | for (size_t i = 0; i < 100; i++) { |
| 1871 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1872 | ASSERT_TRUE(backtrace != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1873 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 1874 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1875 | delete backtrace; |
| 1876 | } |
| 1877 | size_t stable_pss = GetPssBytes(); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1878 | ASSERT_TRUE(stable_pss != 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1879 | |
| 1880 | // Loop enough that even a small leak should be detectable. |
| 1881 | for (size_t i = 0; i < 4096; i++) { |
| 1882 | Backtrace* backtrace = Backtrace::Create(pid, tid); |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1883 | ASSERT_TRUE(backtrace != nullptr); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1884 | ASSERT_TRUE(backtrace->Unwind(0)); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 1885 | ASSERT_EQ(BACKTRACE_UNWIND_NO_ERROR, backtrace->GetError()); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1886 | delete backtrace; |
| 1887 | } |
| 1888 | size_t new_pss = GetPssBytes(); |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1889 | ASSERT_TRUE(new_pss != 0); |
Christopher Ferris | 5ccdfa6 | 2016-03-07 19:18:31 -0800 | [diff] [blame] | 1890 | if (new_pss > stable_pss) { |
| 1891 | ASSERT_LE(new_pss - stable_pss, MAX_LEAK_BYTES); |
| 1892 | } |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | TEST(libbacktrace, check_for_leak_local) { |
| 1896 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD); |
| 1897 | } |
| 1898 | |
| 1899 | TEST(libbacktrace, check_for_leak_local_thread) { |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1900 | thread_t thread_data = { 0, 0, 0, nullptr }; |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1901 | pthread_t thread; |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1902 | ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1903 | |
| 1904 | // Wait up to 2 seconds for the tid to be set. |
| 1905 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); |
| 1906 | |
| 1907 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid); |
| 1908 | |
| 1909 | // Tell the thread to exit its infinite loop. |
| 1910 | android_atomic_acquire_store(0, &thread_data.state); |
| 1911 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1912 | ASSERT_TRUE(pthread_join(thread, nullptr) == 0); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | TEST(libbacktrace, check_for_leak_remote) { |
| 1916 | pid_t pid; |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1917 | CreateRemoteProcess(&pid); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1918 | |
| 1919 | CheckForLeak(pid, BACKTRACE_CURRENT_THREAD); |
| 1920 | |
Christopher Ferris | 82f3bbd | 2017-03-14 15:22:26 -0700 | [diff] [blame] | 1921 | FinishRemoteProcess(pid); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1922 | } |
| 1923 | #endif |