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