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