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