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