| 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> | 
|  | 19 | #include <errno.h> | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 20 | #include <inttypes.h> | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 21 | #include <pthread.h> | 
|  | 22 | #include <signal.h> | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 23 | #include <stdint.h> | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 24 | #include <stdio.h> | 
|  | 25 | #include <stdlib.h> | 
|  | 26 | #include <string.h> | 
|  | 27 | #include <sys/ptrace.h> | 
|  | 28 | #include <sys/types.h> | 
|  | 29 | #include <sys/wait.h> | 
|  | 30 | #include <time.h> | 
|  | 31 | #include <unistd.h> | 
|  | 32 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 33 | #include <backtrace/Backtrace.h> | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 34 | #include <backtrace/BacktraceMap.h> | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 35 |  | 
| Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 36 | // For the THREAD_SIGNAL definition. | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 37 | #include "BacktraceCurrent.h" | 
| Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 38 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 39 | #include <cutils/atomic.h> | 
|  | 40 | #include <gtest/gtest.h> | 
|  | 41 |  | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 42 | #include <algorithm> | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 43 | #include <memory> | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 44 | #include <string> | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 45 | #include <vector> | 
|  | 46 |  | 
|  | 47 | #include "thread_utils.h" | 
|  | 48 |  | 
|  | 49 | // Number of microseconds per milliseconds. | 
|  | 50 | #define US_PER_MSEC             1000 | 
|  | 51 |  | 
|  | 52 | // Number of nanoseconds in a second. | 
|  | 53 | #define NS_PER_SEC              1000000000ULL | 
|  | 54 |  | 
|  | 55 | // Number of simultaneous dumping operations to perform. | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 56 | #define NUM_THREADS  40 | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 57 |  | 
|  | 58 | // Number of simultaneous threads running in our forked process. | 
|  | 59 | #define NUM_PTRACE_THREADS 5 | 
|  | 60 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 61 | struct thread_t { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 62 | pid_t tid; | 
|  | 63 | int32_t state; | 
|  | 64 | pthread_t threadId; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 65 | void* data; | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 66 | }; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 67 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 68 | struct dump_thread_t { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 69 | thread_t thread; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 70 | Backtrace* backtrace; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 71 | int32_t* now; | 
|  | 72 | int32_t done; | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 73 | }; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | extern "C" { | 
|  | 76 | // Prototypes for functions in the test library. | 
|  | 77 | int test_level_one(int, int, int, int, void (*)(void*), void*); | 
|  | 78 |  | 
|  | 79 | int test_recursive_call(int, void (*)(void*), void*); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | uint64_t NanoTime() { | 
|  | 83 | struct timespec t = { 0, 0 }; | 
|  | 84 | clock_gettime(CLOCK_MONOTONIC, &t); | 
|  | 85 | return static_cast<uint64_t>(t.tv_sec * NS_PER_SEC + t.tv_nsec); | 
|  | 86 | } | 
|  | 87 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 88 | std::string DumpFrames(Backtrace* backtrace) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 89 | if (backtrace->NumFrames() == 0) { | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 90 | return "   No frames to dump.\n"; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 93 | std::string frame; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 94 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 95 | frame += "   " + backtrace->FormatFrameData(i) + '\n'; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 96 | } | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 97 | return frame; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
|  | 100 | void WaitForStop(pid_t pid) { | 
|  | 101 | uint64_t start = NanoTime(); | 
|  | 102 |  | 
|  | 103 | siginfo_t si; | 
|  | 104 | while (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) < 0 && (errno == EINTR || errno == ESRCH)) { | 
|  | 105 | if ((NanoTime() - start) > NS_PER_SEC) { | 
|  | 106 | printf("The process did not get to a stopping point in 1 second.\n"); | 
|  | 107 | break; | 
|  | 108 | } | 
|  | 109 | usleep(US_PER_MSEC); | 
|  | 110 | } | 
|  | 111 | } | 
|  | 112 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 113 | bool ReadyLevelBacktrace(Backtrace* backtrace) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 114 | // See if test_level_four is in the backtrace. | 
|  | 115 | bool found = false; | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 116 | for (Backtrace::const_iterator it = backtrace->begin(); it != backtrace->end(); ++it) { | 
|  | 117 | if (it->func_name == "test_level_four") { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 118 | found = true; | 
|  | 119 | break; | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | return found; | 
|  | 124 | } | 
|  | 125 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 126 | void VerifyLevelDump(Backtrace* backtrace) { | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 127 | ASSERT_GT(backtrace->NumFrames(), static_cast<size_t>(0)) | 
|  | 128 | << DumpFrames(backtrace); | 
|  | 129 | ASSERT_LT(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) | 
|  | 130 | << DumpFrames(backtrace); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | // Look through the frames starting at the highest to find the | 
|  | 133 | // frame we want. | 
|  | 134 | size_t frame_num = 0; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 135 | for (size_t i = backtrace->NumFrames()-1; i > 2; i--) { | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 136 | if (backtrace->GetFrame(i)->func_name == "test_level_one") { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 137 | frame_num = i; | 
|  | 138 | break; | 
|  | 139 | } | 
|  | 140 | } | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 141 | ASSERT_LT(static_cast<size_t>(0), frame_num) << DumpFrames(backtrace); | 
|  | 142 | ASSERT_LE(static_cast<size_t>(3), frame_num) << DumpFrames(backtrace); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 143 |  | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 144 | ASSERT_EQ(backtrace->GetFrame(frame_num)->func_name, "test_level_one") | 
|  | 145 | << DumpFrames(backtrace); | 
|  | 146 | ASSERT_EQ(backtrace->GetFrame(frame_num-1)->func_name, "test_level_two") | 
|  | 147 | << DumpFrames(backtrace); | 
|  | 148 | ASSERT_EQ(backtrace->GetFrame(frame_num-2)->func_name, "test_level_three") | 
|  | 149 | << DumpFrames(backtrace); | 
|  | 150 | ASSERT_EQ(backtrace->GetFrame(frame_num-3)->func_name, "test_level_four") | 
|  | 151 | << DumpFrames(backtrace); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
|  | 154 | void VerifyLevelBacktrace(void*) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 155 | std::unique_ptr<Backtrace> backtrace( | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 156 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 157 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 158 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 159 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 160 | VerifyLevelDump(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 163 | bool ReadyMaxBacktrace(Backtrace* backtrace) { | 
|  | 164 | return (backtrace->NumFrames() == MAX_BACKTRACE_FRAMES); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 167 | void VerifyMaxDump(Backtrace* backtrace) { | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 168 | ASSERT_EQ(backtrace->NumFrames(), static_cast<size_t>(MAX_BACKTRACE_FRAMES)) | 
|  | 169 | << DumpFrames(backtrace); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 170 | // Verify that the last frame is our recursive call. | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 171 | ASSERT_EQ(backtrace->GetFrame(MAX_BACKTRACE_FRAMES-1)->func_name, "test_recursive_call") | 
|  | 172 | << DumpFrames(backtrace); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
|  | 175 | void VerifyMaxBacktrace(void*) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 176 | std::unique_ptr<Backtrace> backtrace( | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 177 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 178 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 179 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 180 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 181 | VerifyMaxDump(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
|  | 184 | void ThreadSetState(void* data) { | 
|  | 185 | thread_t* thread = reinterpret_cast<thread_t*>(data); | 
|  | 186 | android_atomic_acquire_store(1, &thread->state); | 
|  | 187 | volatile int i = 0; | 
|  | 188 | while (thread->state) { | 
|  | 189 | i++; | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 193 | void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 194 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid)); | 
|  | 195 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 196 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 197 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 198 | VerifyFunc(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
|  | 201 | bool WaitForNonZero(int32_t* value, uint64_t seconds) { | 
|  | 202 | uint64_t start = NanoTime(); | 
|  | 203 | do { | 
|  | 204 | if (android_atomic_acquire_load(value)) { | 
|  | 205 | return true; | 
|  | 206 | } | 
|  | 207 | } while ((NanoTime() - start) < seconds * NS_PER_SEC); | 
|  | 208 | return false; | 
|  | 209 | } | 
|  | 210 |  | 
| Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 211 | TEST(libbacktrace, local_no_unwind_frames) { | 
|  | 212 | // Verify that a local unwind does not include any frames within | 
|  | 213 | // libunwind or libbacktrace. | 
|  | 214 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid())); | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 215 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 216 | ASSERT_TRUE(backtrace->Unwind(0)); | 
|  | 217 |  | 
|  | 218 | ASSERT_TRUE(backtrace->NumFrames() != 0); | 
|  | 219 | for (const auto& frame : *backtrace ) { | 
|  | 220 | if (BacktraceMap::IsValid(frame.map)) { | 
|  | 221 | const std::string name = basename(frame.map.name.c_str()); | 
|  | 222 | ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so") | 
|  | 223 | << DumpFrames(backtrace.get()); | 
|  | 224 | } | 
|  | 225 | break; | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 229 | TEST(libbacktrace, local_trace) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 230 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
|  | 233 | void VerifyIgnoreFrames( | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 234 | Backtrace* bt_all, Backtrace* bt_ign1, | 
|  | 235 | Backtrace* bt_ign2, const char* cur_proc) { | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 236 | EXPECT_EQ(bt_all->NumFrames(), bt_ign1->NumFrames() + 1) | 
|  | 237 | << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 1 backtrace:\n" << DumpFrames(bt_ign1); | 
|  | 238 | EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2) | 
|  | 239 | << "All backtrace:\n" << DumpFrames(bt_all) << "Ignore 2 backtrace:\n" << DumpFrames(bt_ign2); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 240 |  | 
|  | 241 | // Check all of the frames are the same > the current frame. | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 242 | bool check = (cur_proc == nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 243 | for (size_t i = 0; i < bt_ign2->NumFrames(); i++) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 244 | if (check) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 245 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc); | 
|  | 246 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_ign1->GetFrame(i+1)->sp); | 
|  | 247 | 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] | 248 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 249 | EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_all->GetFrame(i+2)->pc); | 
|  | 250 | EXPECT_EQ(bt_ign2->GetFrame(i)->sp, bt_all->GetFrame(i+2)->sp); | 
|  | 251 | 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] | 252 | } | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 253 | if (!check && bt_ign2->GetFrame(i)->func_name == cur_proc) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 254 | check = true; | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | void VerifyLevelIgnoreFrames(void*) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 260 | std::unique_ptr<Backtrace> all( | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 261 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 262 | ASSERT_TRUE(all.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 263 | ASSERT_TRUE(all->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 264 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 265 | std::unique_ptr<Backtrace> ign1( | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 266 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 267 | ASSERT_TRUE(ign1.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 268 | ASSERT_TRUE(ign1->Unwind(1)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 269 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 270 | std::unique_ptr<Backtrace> ign2( | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 271 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 272 | ASSERT_TRUE(ign2.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 273 | ASSERT_TRUE(ign2->Unwind(2)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 274 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 275 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames"); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
|  | 278 | TEST(libbacktrace, local_trace_ignore_frames) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 279 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
|  | 282 | TEST(libbacktrace, local_max_trace) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 283 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 284 | } | 
|  | 285 |  | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 286 | void VerifyProcTest(pid_t pid, pid_t tid, bool share_map, | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 287 | bool (*ReadyFunc)(Backtrace*), | 
|  | 288 | void (*VerifyFunc)(Backtrace*)) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 289 | pid_t ptrace_tid; | 
|  | 290 | if (tid < 0) { | 
|  | 291 | ptrace_tid = pid; | 
|  | 292 | } else { | 
|  | 293 | ptrace_tid = tid; | 
|  | 294 | } | 
|  | 295 | uint64_t start = NanoTime(); | 
|  | 296 | bool verified = false; | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 297 | std::string last_dump; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 298 | do { | 
|  | 299 | usleep(US_PER_MSEC); | 
|  | 300 | if (ptrace(PTRACE_ATTACH, ptrace_tid, 0, 0) == 0) { | 
|  | 301 | // Wait for the process to get to a stopping point. | 
|  | 302 | WaitForStop(ptrace_tid); | 
|  | 303 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 304 | std::unique_ptr<BacktraceMap> map; | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 305 | if (share_map) { | 
|  | 306 | map.reset(BacktraceMap::Create(pid)); | 
|  | 307 | } | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 308 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get())); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 309 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 310 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 311 | if (ReadyFunc(backtrace.get())) { | 
|  | 312 | VerifyFunc(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 313 | verified = true; | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 314 | } else { | 
|  | 315 | last_dump = DumpFrames(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 316 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 317 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 318 | ASSERT_TRUE(ptrace(PTRACE_DETACH, ptrace_tid, 0, 0) == 0); | 
|  | 319 | } | 
|  | 320 | // If 5 seconds have passed, then we are done. | 
|  | 321 | } while (!verified && (NanoTime() - start) <= 5 * NS_PER_SEC); | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 322 | ASSERT_TRUE(verified) << "Last backtrace:\n" << last_dump; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
|  | 325 | TEST(libbacktrace, ptrace_trace) { | 
|  | 326 | pid_t pid; | 
|  | 327 | if ((pid = fork()) == 0) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 328 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 329 | _exit(1); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 330 | } | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 331 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump); | 
|  | 332 |  | 
|  | 333 | kill(pid, SIGKILL); | 
|  | 334 | int status; | 
|  | 335 | ASSERT_EQ(waitpid(pid, &status, 0), pid); | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | TEST(libbacktrace, ptrace_trace_shared_map) { | 
|  | 339 | pid_t pid; | 
|  | 340 | if ((pid = fork()) == 0) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 341 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 342 | _exit(1); | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 343 | } | 
|  | 344 |  | 
|  | 345 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 346 |  | 
|  | 347 | kill(pid, SIGKILL); | 
|  | 348 | int status; | 
|  | 349 | ASSERT_EQ(waitpid(pid, &status, 0), pid); | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | TEST(libbacktrace, ptrace_max_trace) { | 
|  | 353 | pid_t pid; | 
|  | 354 | if ((pid = fork()) == 0) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 355 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 356 | _exit(1); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 357 | } | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 358 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 359 |  | 
|  | 360 | kill(pid, SIGKILL); | 
|  | 361 | int status; | 
|  | 362 | ASSERT_EQ(waitpid(pid, &status, 0), pid); | 
|  | 363 | } | 
|  | 364 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 365 | void VerifyProcessIgnoreFrames(Backtrace* bt_all) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 366 | std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); | 
|  | 367 | ASSERT_TRUE(ign1.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 368 | ASSERT_TRUE(ign1->Unwind(1)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 369 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 370 | std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD)); | 
|  | 371 | ASSERT_TRUE(ign2.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 372 | ASSERT_TRUE(ign2->Unwind(2)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 373 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 374 | VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
|  | 377 | TEST(libbacktrace, ptrace_ignore_frames) { | 
|  | 378 | pid_t pid; | 
|  | 379 | if ((pid = fork()) == 0) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 380 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 381 | _exit(1); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 382 | } | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 383 | VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 384 |  | 
|  | 385 | kill(pid, SIGKILL); | 
|  | 386 | int status; | 
|  | 387 | ASSERT_EQ(waitpid(pid, &status, 0), pid); | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | // Create a process with multiple threads and dump all of the threads. | 
|  | 391 | void* PtraceThreadLevelRun(void*) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 392 | EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); | 
|  | 393 | return nullptr; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
|  | 396 | void GetThreads(pid_t pid, std::vector<pid_t>* threads) { | 
|  | 397 | // Get the list of tasks. | 
|  | 398 | char task_path[128]; | 
|  | 399 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); | 
|  | 400 |  | 
|  | 401 | DIR* tasks_dir = opendir(task_path); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 402 | ASSERT_TRUE(tasks_dir != nullptr); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 403 | struct dirent* entry; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 404 | while ((entry = readdir(tasks_dir)) != nullptr) { | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 405 | char* end; | 
|  | 406 | pid_t tid = strtoul(entry->d_name, &end, 10); | 
|  | 407 | if (*end == '\0') { | 
|  | 408 | threads->push_back(tid); | 
|  | 409 | } | 
|  | 410 | } | 
|  | 411 | closedir(tasks_dir); | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | TEST(libbacktrace, ptrace_threads) { | 
|  | 415 | pid_t pid; | 
|  | 416 | if ((pid = fork()) == 0) { | 
|  | 417 | for (size_t i = 0; i < NUM_PTRACE_THREADS; i++) { | 
|  | 418 | pthread_attr_t attr; | 
|  | 419 | pthread_attr_init(&attr); | 
|  | 420 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 421 |  | 
|  | 422 | pthread_t thread; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 423 | ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 424 | } | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 425 | ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 426 | _exit(1); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 427 | } | 
|  | 428 |  | 
|  | 429 | // Check to see that all of the threads are running before unwinding. | 
|  | 430 | std::vector<pid_t> threads; | 
|  | 431 | uint64_t start = NanoTime(); | 
|  | 432 | do { | 
|  | 433 | usleep(US_PER_MSEC); | 
|  | 434 | threads.clear(); | 
|  | 435 | GetThreads(pid, &threads); | 
|  | 436 | } while ((threads.size() != NUM_PTRACE_THREADS + 1) && | 
|  | 437 | ((NanoTime() - start) <= 5 * NS_PER_SEC)); | 
|  | 438 | ASSERT_EQ(threads.size(), static_cast<size_t>(NUM_PTRACE_THREADS + 1)); | 
|  | 439 |  | 
|  | 440 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); | 
|  | 441 | WaitForStop(pid); | 
|  | 442 | for (std::vector<int>::const_iterator it = threads.begin(); it != threads.end(); ++it) { | 
|  | 443 | // Skip the current forked process, we only care about the threads. | 
|  | 444 | if (pid == *it) { | 
|  | 445 | continue; | 
|  | 446 | } | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 447 | VerifyProcTest(pid, *it, false, ReadyLevelBacktrace, VerifyLevelDump); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 448 | } | 
|  | 449 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); | 
|  | 450 |  | 
|  | 451 | kill(pid, SIGKILL); | 
|  | 452 | int status; | 
|  | 453 | ASSERT_EQ(waitpid(pid, &status, 0), pid); | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | void VerifyLevelThread(void*) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 457 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); | 
|  | 458 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 459 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 460 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 461 | VerifyLevelDump(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 462 | } | 
|  | 463 |  | 
|  | 464 | TEST(libbacktrace, thread_current_level) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 465 | ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
|  | 468 | void VerifyMaxThread(void*) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 469 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid())); | 
|  | 470 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 471 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 472 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 473 | VerifyMaxDump(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 474 | } | 
|  | 475 |  | 
|  | 476 | TEST(libbacktrace, thread_current_max) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 477 | ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 478 | } | 
|  | 479 |  | 
|  | 480 | void* ThreadLevelRun(void* data) { | 
|  | 481 | thread_t* thread = reinterpret_cast<thread_t*>(data); | 
|  | 482 |  | 
|  | 483 | thread->tid = gettid(); | 
|  | 484 | EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 485 | return nullptr; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 486 | } | 
|  | 487 |  | 
|  | 488 | TEST(libbacktrace, thread_level_trace) { | 
|  | 489 | pthread_attr_t attr; | 
|  | 490 | pthread_attr_init(&attr); | 
|  | 491 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 492 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 493 | thread_t thread_data = { 0, 0, 0, nullptr }; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 494 | pthread_t thread; | 
|  | 495 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); | 
|  | 496 |  | 
|  | 497 | // Wait up to 2 seconds for the tid to be set. | 
|  | 498 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); | 
|  | 499 |  | 
| Christopher Ferris | aa63d9f | 2014-04-29 09:35:30 -0700 | [diff] [blame] | 500 | // Make sure that the thread signal used is not visible when compiled for | 
|  | 501 | // the target. | 
|  | 502 | #if !defined(__GLIBC__) | 
|  | 503 | ASSERT_LT(THREAD_SIGNAL, SIGRTMIN); | 
|  | 504 | #endif | 
|  | 505 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 506 | // Save the current signal action and make sure it is restored afterwards. | 
|  | 507 | struct sigaction cur_action; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 508 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 509 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 510 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); | 
|  | 511 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 512 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 513 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 514 | VerifyLevelDump(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 515 |  | 
|  | 516 | // Tell the thread to exit its infinite loop. | 
|  | 517 | android_atomic_acquire_store(0, &thread_data.state); | 
|  | 518 |  | 
|  | 519 | // Verify that the old action was restored. | 
|  | 520 | struct sigaction new_action; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 521 | ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 522 | EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction); | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 523 | // The SA_RESTORER flag gets set behind our back, so a direct comparison | 
|  | 524 | // doesn't work unless we mask the value off. Mips doesn't have this | 
|  | 525 | // flag, so skip this on that platform. | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 526 | #if defined(SA_RESTORER) | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 527 | cur_action.sa_flags &= ~SA_RESTORER; | 
|  | 528 | new_action.sa_flags &= ~SA_RESTORER; | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 529 | #elif defined(__GLIBC__) | 
|  | 530 | // Our host compiler doesn't appear to define this flag for some reason. | 
|  | 531 | cur_action.sa_flags &= ~0x04000000; | 
|  | 532 | new_action.sa_flags &= ~0x04000000; | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 533 | #endif | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 534 | EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags); | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | TEST(libbacktrace, thread_ignore_frames) { | 
|  | 538 | pthread_attr_t attr; | 
|  | 539 | pthread_attr_init(&attr); | 
|  | 540 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 541 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 542 | thread_t thread_data = { 0, 0, 0, nullptr }; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 543 | pthread_t thread; | 
|  | 544 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); | 
|  | 545 |  | 
|  | 546 | // Wait up to 2 seconds for the tid to be set. | 
|  | 547 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); | 
|  | 548 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 549 | std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid)); | 
|  | 550 | ASSERT_TRUE(all.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 551 | ASSERT_TRUE(all->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 552 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 553 | std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid)); | 
|  | 554 | ASSERT_TRUE(ign1.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 555 | ASSERT_TRUE(ign1->Unwind(1)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 556 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 557 | std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid)); | 
|  | 558 | ASSERT_TRUE(ign2.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 559 | ASSERT_TRUE(ign2->Unwind(2)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 560 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 561 | VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 562 |  | 
|  | 563 | // Tell the thread to exit its infinite loop. | 
|  | 564 | android_atomic_acquire_store(0, &thread_data.state); | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | void* ThreadMaxRun(void* data) { | 
|  | 568 | thread_t* thread = reinterpret_cast<thread_t*>(data); | 
|  | 569 |  | 
|  | 570 | thread->tid = gettid(); | 
|  | 571 | EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 572 | return nullptr; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 573 | } | 
|  | 574 |  | 
|  | 575 | TEST(libbacktrace, thread_max_trace) { | 
|  | 576 | pthread_attr_t attr; | 
|  | 577 | pthread_attr_init(&attr); | 
|  | 578 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 579 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 580 | thread_t thread_data = { 0, 0, 0, nullptr }; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 581 | pthread_t thread; | 
|  | 582 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0); | 
|  | 583 |  | 
|  | 584 | // Wait for the tid to be set. | 
|  | 585 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); | 
|  | 586 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 587 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); | 
|  | 588 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 589 | ASSERT_TRUE(backtrace->Unwind(0)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 590 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 591 | VerifyMaxDump(backtrace.get()); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 592 |  | 
|  | 593 | // Tell the thread to exit its infinite loop. | 
|  | 594 | android_atomic_acquire_store(0, &thread_data.state); | 
|  | 595 | } | 
|  | 596 |  | 
|  | 597 | void* ThreadDump(void* data) { | 
|  | 598 | dump_thread_t* dump = reinterpret_cast<dump_thread_t*>(data); | 
|  | 599 | while (true) { | 
|  | 600 | if (android_atomic_acquire_load(dump->now)) { | 
|  | 601 | break; | 
|  | 602 | } | 
|  | 603 | } | 
|  | 604 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 605 | // The status of the actual unwind will be checked elsewhere. | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 606 | dump->backtrace = Backtrace::Create(getpid(), dump->thread.tid); | 
|  | 607 | dump->backtrace->Unwind(0); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 608 |  | 
|  | 609 | android_atomic_acquire_store(1, &dump->done); | 
|  | 610 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 611 | return nullptr; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 612 | } | 
|  | 613 |  | 
|  | 614 | TEST(libbacktrace, thread_multiple_dump) { | 
|  | 615 | // Dump NUM_THREADS simultaneously. | 
|  | 616 | std::vector<thread_t> runners(NUM_THREADS); | 
|  | 617 | std::vector<dump_thread_t> dumpers(NUM_THREADS); | 
|  | 618 |  | 
|  | 619 | pthread_attr_t attr; | 
|  | 620 | pthread_attr_init(&attr); | 
|  | 621 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 622 | for (size_t i = 0; i < NUM_THREADS; i++) { | 
|  | 623 | // Launch the runners, they will spin in hard loops doing nothing. | 
|  | 624 | runners[i].tid = 0; | 
|  | 625 | runners[i].state = 0; | 
|  | 626 | ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0); | 
|  | 627 | } | 
|  | 628 |  | 
|  | 629 | // Wait for tids to be set. | 
|  | 630 | 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] | 631 | ASSERT_TRUE(WaitForNonZero(&it->state, 30)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 632 | } | 
|  | 633 |  | 
|  | 634 | // Start all of the dumpers at once, they will spin until they are signalled | 
|  | 635 | // to begin their dump run. | 
|  | 636 | int32_t dump_now = 0; | 
|  | 637 | for (size_t i = 0; i < NUM_THREADS; i++) { | 
|  | 638 | dumpers[i].thread.tid = runners[i].tid; | 
|  | 639 | dumpers[i].thread.state = 0; | 
|  | 640 | dumpers[i].done = 0; | 
|  | 641 | dumpers[i].now = &dump_now; | 
|  | 642 |  | 
|  | 643 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); | 
|  | 644 | } | 
|  | 645 |  | 
|  | 646 | // Start all of the dumpers going at once. | 
|  | 647 | android_atomic_acquire_store(1, &dump_now); | 
|  | 648 |  | 
|  | 649 | for (size_t i = 0; i < NUM_THREADS; i++) { | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 650 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 651 |  | 
|  | 652 | // Tell the runner thread to exit its infinite loop. | 
|  | 653 | android_atomic_acquire_store(0, &runners[i].state); | 
|  | 654 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 655 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 656 | VerifyMaxDump(dumpers[i].backtrace); | 
|  | 657 |  | 
|  | 658 | delete dumpers[i].backtrace; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 659 | dumpers[i].backtrace = nullptr; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 660 | } | 
|  | 661 | } | 
|  | 662 |  | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 663 | TEST(libbacktrace, thread_multiple_dump_same_thread) { | 
|  | 664 | pthread_attr_t attr; | 
|  | 665 | pthread_attr_init(&attr); | 
|  | 666 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 667 | thread_t runner; | 
|  | 668 | runner.tid = 0; | 
|  | 669 | runner.state = 0; | 
|  | 670 | ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0); | 
|  | 671 |  | 
|  | 672 | // Wait for tids to be set. | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 673 | ASSERT_TRUE(WaitForNonZero(&runner.state, 30)); | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 674 |  | 
|  | 675 | // Start all of the dumpers at once, they will spin until they are signalled | 
|  | 676 | // to begin their dump run. | 
|  | 677 | int32_t dump_now = 0; | 
|  | 678 | // Dump the same thread NUM_THREADS simultaneously. | 
|  | 679 | std::vector<dump_thread_t> dumpers(NUM_THREADS); | 
|  | 680 | for (size_t i = 0; i < NUM_THREADS; i++) { | 
|  | 681 | dumpers[i].thread.tid = runner.tid; | 
|  | 682 | dumpers[i].thread.state = 0; | 
|  | 683 | dumpers[i].done = 0; | 
|  | 684 | dumpers[i].now = &dump_now; | 
|  | 685 |  | 
|  | 686 | ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); | 
|  | 687 | } | 
|  | 688 |  | 
|  | 689 | // Start all of the dumpers going at once. | 
|  | 690 | android_atomic_acquire_store(1, &dump_now); | 
|  | 691 |  | 
|  | 692 | for (size_t i = 0; i < NUM_THREADS; i++) { | 
| Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 693 | ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30)); | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 694 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 695 | ASSERT_TRUE(dumpers[i].backtrace != nullptr); | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 696 | VerifyMaxDump(dumpers[i].backtrace); | 
|  | 697 |  | 
|  | 698 | delete dumpers[i].backtrace; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 699 | dumpers[i].backtrace = nullptr; | 
| Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 700 | } | 
|  | 701 |  | 
|  | 702 | // Tell the runner thread to exit its infinite loop. | 
|  | 703 | android_atomic_acquire_store(0, &runner.state); | 
|  | 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)); | 
|  | 716 | delete back1; | 
|  | 717 | delete map1; | 
|  | 718 |  | 
|  | 719 | Backtrace* back2 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map2); | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 720 | ASSERT_TRUE(back2 != nullptr); | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 721 | EXPECT_TRUE(back2->Unwind(0)); | 
|  | 722 | delete back2; | 
|  | 723 | delete map2; | 
|  | 724 |  | 
|  | 725 | Backtrace* back3 = Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD, map3); | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 726 | ASSERT_TRUE(back3 != nullptr); | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 727 | EXPECT_TRUE(back3->Unwind(0)); | 
|  | 728 | delete back3; | 
|  | 729 | delete map3; | 
|  | 730 | } | 
|  | 731 |  | 
| Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 732 | TEST(libbacktrace, fillin_erases) { | 
|  | 733 | BacktraceMap* back_map = BacktraceMap::Create(getpid()); | 
|  | 734 |  | 
|  | 735 | backtrace_map_t map; | 
|  | 736 |  | 
|  | 737 | map.start = 1; | 
|  | 738 | map.end = 3; | 
|  | 739 | map.flags = 1; | 
|  | 740 | map.name = "Initialized"; | 
|  | 741 | back_map->FillIn(0, &map); | 
|  | 742 | delete back_map; | 
|  | 743 |  | 
|  | 744 | ASSERT_FALSE(BacktraceMap::IsValid(map)); | 
|  | 745 | ASSERT_EQ(static_cast<uintptr_t>(0), map.start); | 
|  | 746 | ASSERT_EQ(static_cast<uintptr_t>(0), map.end); | 
|  | 747 | ASSERT_EQ(0, map.flags); | 
|  | 748 | ASSERT_EQ("", map.name); | 
|  | 749 | } | 
|  | 750 |  | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 751 | TEST(libbacktrace, format_test) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 752 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD)); | 
|  | 753 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 754 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 755 | backtrace_frame_data_t frame; | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 756 | frame.num = 1; | 
|  | 757 | frame.pc = 2; | 
|  | 758 | frame.sp = 0; | 
|  | 759 | frame.stack_size = 0; | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 760 | frame.func_offset = 0; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 761 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 762 | // Check no map set. | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 763 | frame.num = 1; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 764 | #if defined(__LP64__) | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 765 | EXPECT_EQ("#01 pc 0000000000000002  <unknown>", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 766 | #else | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 767 | EXPECT_EQ("#01 pc 00000002  <unknown>", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 768 | #endif | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 769 | backtrace->FormatFrameData(&frame)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 770 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 771 | // Check map name empty, but exists. | 
| Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 772 | frame.map.start = 1; | 
|  | 773 | frame.map.end = 1; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 774 | #if defined(__LP64__) | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 775 | EXPECT_EQ("#01 pc 0000000000000001  <unknown>", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 776 | #else | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 777 | EXPECT_EQ("#01 pc 00000001  <unknown>", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 778 | #endif | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 779 | backtrace->FormatFrameData(&frame)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 780 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 781 |  | 
|  | 782 | // Check relative pc is set and map name is set. | 
|  | 783 | frame.pc = 0x12345679; | 
| Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 784 | frame.map.name = "MapFake"; | 
|  | 785 | frame.map.start =  1; | 
|  | 786 | frame.map.end =  1; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 787 | #if defined(__LP64__) | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 788 | EXPECT_EQ("#01 pc 0000000012345678  MapFake", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 789 | #else | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 790 | EXPECT_EQ("#01 pc 12345678  MapFake", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 791 | #endif | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 792 | backtrace->FormatFrameData(&frame)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 793 |  | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 794 | // Check func_name is set, but no func offset. | 
|  | 795 | frame.func_name = "ProcFake"; | 
|  | 796 | #if defined(__LP64__) | 
|  | 797 | EXPECT_EQ("#01 pc 0000000012345678  MapFake (ProcFake)", | 
|  | 798 | #else | 
|  | 799 | EXPECT_EQ("#01 pc 12345678  MapFake (ProcFake)", | 
|  | 800 | #endif | 
|  | 801 | backtrace->FormatFrameData(&frame)); | 
|  | 802 |  | 
|  | 803 | // Check func_name is set, and func offset is non-zero. | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 804 | frame.func_offset = 645; | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 805 | #if defined(__LP64__) | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 806 | EXPECT_EQ("#01 pc 0000000012345678  MapFake (ProcFake+645)", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 807 | #else | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 808 | EXPECT_EQ("#01 pc 12345678  MapFake (ProcFake+645)", | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 809 | #endif | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 810 | backtrace->FormatFrameData(&frame)); | 
| Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 811 | } | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 812 |  | 
|  | 813 | struct map_test_t { | 
|  | 814 | uintptr_t start; | 
|  | 815 | uintptr_t end; | 
|  | 816 | }; | 
|  | 817 |  | 
|  | 818 | bool map_sort(map_test_t i, map_test_t j) { | 
|  | 819 | return i.start < j.start; | 
|  | 820 | } | 
|  | 821 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 822 | void VerifyMap(pid_t pid) { | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 823 | char buffer[4096]; | 
|  | 824 | snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid); | 
|  | 825 |  | 
|  | 826 | FILE* map_file = fopen(buffer, "r"); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 827 | ASSERT_TRUE(map_file != nullptr); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 828 | std::vector<map_test_t> test_maps; | 
|  | 829 | while (fgets(buffer, sizeof(buffer), map_file)) { | 
|  | 830 | map_test_t map; | 
|  | 831 | ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end)); | 
|  | 832 | test_maps.push_back(map); | 
|  | 833 | } | 
|  | 834 | fclose(map_file); | 
|  | 835 | std::sort(test_maps.begin(), test_maps.end(), map_sort); | 
|  | 836 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 837 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid)); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 838 |  | 
|  | 839 | // Basic test that verifies that the map is in the expected order. | 
|  | 840 | std::vector<map_test_t>::const_iterator test_it = test_maps.begin(); | 
|  | 841 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { | 
|  | 842 | ASSERT_TRUE(test_it != test_maps.end()); | 
|  | 843 | ASSERT_EQ(test_it->start, it->start); | 
|  | 844 | ASSERT_EQ(test_it->end, it->end); | 
|  | 845 | ++test_it; | 
|  | 846 | } | 
|  | 847 | ASSERT_TRUE(test_it == test_maps.end()); | 
|  | 848 | } | 
|  | 849 |  | 
|  | 850 | TEST(libbacktrace, verify_map_remote) { | 
|  | 851 | pid_t pid; | 
|  | 852 |  | 
|  | 853 | if ((pid = fork()) == 0) { | 
|  | 854 | while (true) { | 
|  | 855 | } | 
|  | 856 | _exit(0); | 
|  | 857 | } | 
|  | 858 | ASSERT_LT(0, pid); | 
|  | 859 |  | 
|  | 860 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); | 
|  | 861 |  | 
|  | 862 | // Wait for the process to get to a stopping point. | 
|  | 863 | WaitForStop(pid); | 
|  | 864 |  | 
|  | 865 | // The maps should match exactly since the forked process has been paused. | 
|  | 866 | VerifyMap(pid); | 
|  | 867 |  | 
|  | 868 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); | 
|  | 869 |  | 
|  | 870 | kill(pid, SIGKILL); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 871 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); | 
|  | 872 | } | 
|  | 873 |  | 
|  | 874 | void* ThreadReadTest(void* data) { | 
|  | 875 | thread_t* thread_data = reinterpret_cast<thread_t*>(data); | 
|  | 876 |  | 
|  | 877 | thread_data->tid = gettid(); | 
|  | 878 |  | 
|  | 879 | // Create two map pages. | 
|  | 880 | // Mark the second page as not-readable. | 
|  | 881 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); | 
|  | 882 | uint8_t* memory; | 
|  | 883 | if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) { | 
|  | 884 | return reinterpret_cast<void*>(-1); | 
|  | 885 | } | 
|  | 886 |  | 
|  | 887 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { | 
|  | 888 | return reinterpret_cast<void*>(-1); | 
|  | 889 | } | 
|  | 890 |  | 
|  | 891 | // Set up a simple pattern in memory. | 
|  | 892 | for (size_t i = 0; i < pagesize; i++) { | 
|  | 893 | memory[i] = i; | 
|  | 894 | } | 
|  | 895 |  | 
|  | 896 | thread_data->data = memory; | 
|  | 897 |  | 
|  | 898 | // Tell the caller it's okay to start reading memory. | 
|  | 899 | android_atomic_acquire_store(1, &thread_data->state); | 
|  | 900 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 901 | // Loop waiting for the caller to finish reading the memory. | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 902 | while (thread_data->state) { | 
|  | 903 | } | 
|  | 904 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 905 | // Re-enable read-write on the page so that we don't crash if we try | 
|  | 906 | // and access data on this page when freeing the memory. | 
|  | 907 | if (mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) != 0) { | 
|  | 908 | return reinterpret_cast<void*>(-1); | 
|  | 909 | } | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 910 | free(memory); | 
|  | 911 |  | 
|  | 912 | android_atomic_acquire_store(1, &thread_data->state); | 
|  | 913 |  | 
|  | 914 | return nullptr; | 
|  | 915 | } | 
|  | 916 |  | 
|  | 917 | void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) { | 
|  | 918 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); | 
|  | 919 |  | 
|  | 920 | // Create a page of data to use to do quick compares. | 
|  | 921 | uint8_t* expected = new uint8_t[pagesize]; | 
|  | 922 | for (size_t i = 0; i < pagesize; i++) { | 
|  | 923 | expected[i] = i; | 
|  | 924 | } | 
|  | 925 | uint8_t* data = new uint8_t[2*pagesize]; | 
|  | 926 | // Verify that we can only read one page worth of data. | 
|  | 927 | size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize); | 
|  | 928 | ASSERT_EQ(pagesize, bytes_read); | 
|  | 929 | ASSERT_TRUE(memcmp(data, expected, pagesize) == 0); | 
|  | 930 |  | 
|  | 931 | // Verify unaligned reads. | 
|  | 932 | for (size_t i = 1; i < sizeof(word_t); i++) { | 
|  | 933 | bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t)); | 
|  | 934 | ASSERT_EQ(2 * sizeof(word_t), bytes_read); | 
|  | 935 | ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0) | 
|  | 936 | << "Offset at " << i << " failed"; | 
|  | 937 | } | 
|  | 938 | delete data; | 
|  | 939 | delete expected; | 
|  | 940 | } | 
|  | 941 |  | 
|  | 942 | TEST(libbacktrace, thread_read) { | 
|  | 943 | pthread_attr_t attr; | 
|  | 944 | pthread_attr_init(&attr); | 
|  | 945 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 946 | pthread_t thread; | 
|  | 947 | thread_t thread_data = { 0, 0, 0, nullptr }; | 
|  | 948 | ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0); | 
|  | 949 |  | 
|  | 950 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); | 
|  | 951 |  | 
|  | 952 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); | 
|  | 953 | ASSERT_TRUE(backtrace.get() != nullptr); | 
|  | 954 |  | 
|  | 955 | RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data)); | 
|  | 956 |  | 
|  | 957 | android_atomic_acquire_store(0, &thread_data.state); | 
|  | 958 |  | 
|  | 959 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10)); | 
|  | 960 | } | 
|  | 961 |  | 
|  | 962 | volatile uintptr_t g_ready = 0; | 
|  | 963 | volatile uintptr_t g_addr = 0; | 
|  | 964 |  | 
|  | 965 | void ForkedReadTest() { | 
|  | 966 | // Create two map pages. | 
|  | 967 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); | 
|  | 968 | uint8_t* memory; | 
|  | 969 | if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) { | 
|  | 970 | perror("Failed to allocate memory\n"); | 
|  | 971 | exit(1); | 
|  | 972 | } | 
|  | 973 |  | 
|  | 974 | // Mark the second page as not-readable. | 
|  | 975 | if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) { | 
|  | 976 | perror("Failed to mprotect memory\n"); | 
|  | 977 | exit(1); | 
|  | 978 | } | 
|  | 979 |  | 
|  | 980 | // Set up a simple pattern in memory. | 
|  | 981 | for (size_t i = 0; i < pagesize; i++) { | 
|  | 982 | memory[i] = i; | 
|  | 983 | } | 
|  | 984 |  | 
|  | 985 | g_addr = reinterpret_cast<uintptr_t>(memory); | 
|  | 986 | g_ready = 1; | 
|  | 987 |  | 
|  | 988 | while (1) { | 
|  | 989 | usleep(US_PER_MSEC); | 
|  | 990 | } | 
|  | 991 | } | 
|  | 992 |  | 
|  | 993 | TEST(libbacktrace, process_read) { | 
|  | 994 | pid_t pid; | 
|  | 995 | if ((pid = fork()) == 0) { | 
|  | 996 | ForkedReadTest(); | 
|  | 997 | exit(0); | 
|  | 998 | } | 
|  | 999 | ASSERT_NE(-1, pid); | 
|  | 1000 |  | 
|  | 1001 | bool test_executed = false; | 
|  | 1002 | uint64_t start = NanoTime(); | 
|  | 1003 | while (1) { | 
|  | 1004 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) { | 
|  | 1005 | WaitForStop(pid); | 
|  | 1006 |  | 
|  | 1007 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid)); | 
| Christopher Ferris | 97e00bb | 2015-04-02 14:22:31 -0700 | [diff] [blame] | 1008 | ASSERT_TRUE(backtrace.get() != nullptr); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1009 |  | 
|  | 1010 | uintptr_t read_addr; | 
|  | 1011 | size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready), | 
|  | 1012 | reinterpret_cast<uint8_t*>(&read_addr), | 
|  | 1013 | sizeof(uintptr_t)); | 
|  | 1014 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); | 
|  | 1015 | if (read_addr) { | 
|  | 1016 | // The forked process is ready to be read. | 
|  | 1017 | bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr), | 
|  | 1018 | reinterpret_cast<uint8_t*>(&read_addr), | 
|  | 1019 | sizeof(uintptr_t)); | 
|  | 1020 | ASSERT_EQ(sizeof(uintptr_t), bytes_read); | 
|  | 1021 |  | 
|  | 1022 | RunReadTest(backtrace.get(), read_addr); | 
|  | 1023 |  | 
|  | 1024 | test_executed = true; | 
|  | 1025 | break; | 
|  | 1026 | } | 
|  | 1027 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); | 
|  | 1028 | } | 
|  | 1029 | if ((NanoTime() - start) > 5 * NS_PER_SEC) { | 
|  | 1030 | break; | 
|  | 1031 | } | 
|  | 1032 | usleep(US_PER_MSEC); | 
|  | 1033 | } | 
|  | 1034 | kill(pid, SIGKILL); | 
|  | 1035 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); | 
|  | 1036 |  | 
|  | 1037 | ASSERT_TRUE(test_executed); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1038 | } | 
|  | 1039 |  | 
|  | 1040 | #if defined(ENABLE_PSS_TESTS) | 
|  | 1041 | #include "GetPss.h" | 
|  | 1042 |  | 
|  | 1043 | #define MAX_LEAK_BYTES 32*1024UL | 
|  | 1044 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1045 | void CheckForLeak(pid_t pid, pid_t tid) { | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1046 | // Do a few runs to get the PSS stable. | 
|  | 1047 | for (size_t i = 0; i < 100; i++) { | 
|  | 1048 | Backtrace* backtrace = Backtrace::Create(pid, tid); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1049 | ASSERT_TRUE(backtrace != nullptr); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1050 | ASSERT_TRUE(backtrace->Unwind(0)); | 
|  | 1051 | delete backtrace; | 
|  | 1052 | } | 
|  | 1053 | size_t stable_pss = GetPssBytes(); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1054 | ASSERT_TRUE(stable_pss != 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1055 |  | 
|  | 1056 | // Loop enough that even a small leak should be detectable. | 
|  | 1057 | for (size_t i = 0; i < 4096; i++) { | 
|  | 1058 | Backtrace* backtrace = Backtrace::Create(pid, tid); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1059 | ASSERT_TRUE(backtrace != nullptr); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1060 | ASSERT_TRUE(backtrace->Unwind(0)); | 
|  | 1061 | delete backtrace; | 
|  | 1062 | } | 
|  | 1063 | size_t new_pss = GetPssBytes(); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1064 | ASSERT_TRUE(new_pss != 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1065 | size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss; | 
|  | 1066 | // As long as the new pss is within a certain amount, consider everything okay. | 
|  | 1067 | ASSERT_LE(abs_diff, MAX_LEAK_BYTES); | 
|  | 1068 | } | 
|  | 1069 |  | 
|  | 1070 | TEST(libbacktrace, check_for_leak_local) { | 
|  | 1071 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD); | 
|  | 1072 | } | 
|  | 1073 |  | 
|  | 1074 | TEST(libbacktrace, check_for_leak_local_thread) { | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1075 | thread_t thread_data = { 0, 0, 0, nullptr }; | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1076 | pthread_t thread; | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1077 | ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1078 |  | 
|  | 1079 | // Wait up to 2 seconds for the tid to be set. | 
|  | 1080 | ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); | 
|  | 1081 |  | 
|  | 1082 | CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid); | 
|  | 1083 |  | 
|  | 1084 | // Tell the thread to exit its infinite loop. | 
|  | 1085 | android_atomic_acquire_store(0, &thread_data.state); | 
|  | 1086 |  | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1087 | ASSERT_TRUE(pthread_join(thread, nullptr) == 0); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1088 | } | 
|  | 1089 |  | 
|  | 1090 | TEST(libbacktrace, check_for_leak_remote) { | 
|  | 1091 | pid_t pid; | 
|  | 1092 |  | 
|  | 1093 | if ((pid = fork()) == 0) { | 
|  | 1094 | while (true) { | 
|  | 1095 | } | 
|  | 1096 | _exit(0); | 
|  | 1097 | } | 
|  | 1098 | ASSERT_LT(0, pid); | 
|  | 1099 |  | 
|  | 1100 | ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0); | 
|  | 1101 |  | 
|  | 1102 | // Wait for the process to get to a stopping point. | 
|  | 1103 | WaitForStop(pid); | 
|  | 1104 |  | 
|  | 1105 | CheckForLeak(pid, BACKTRACE_CURRENT_THREAD); | 
|  | 1106 |  | 
|  | 1107 | ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0); | 
|  | 1108 |  | 
|  | 1109 | kill(pid, SIGKILL); | 
| Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 1110 | ASSERT_EQ(waitpid(pid, nullptr, 0), pid); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 1111 | } | 
|  | 1112 | #endif |