Christopher Ferris | 15fee82 | 2022-09-12 18:00:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include <thread> |
| 20 | |
Christopher Ferris | bcaeacc | 2024-03-27 16:17:50 -0700 | [diff] [blame] | 21 | #include <android-base/test_utils.h> |
Christopher Ferris | 15fee82 | 2022-09-12 18:00:10 -0700 | [diff] [blame] | 22 | #include <android-base/threads.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <utils/CallStack.h> |
| 25 | |
Christopher Ferris | bcaeacc | 2024-03-27 16:17:50 -0700 | [diff] [blame] | 26 | #if defined(__ANDROID__) |
| 27 | #include <log/log.h> |
| 28 | #include <log/log_read.h> |
| 29 | #endif |
| 30 | |
Elliott Hughes | b795d6f | 2022-09-14 20:15:19 +0000 | [diff] [blame] | 31 | __attribute__((__noinline__)) extern "C" void CurrentCaller(android::String8& backtrace) { |
Christopher Ferris | 15fee82 | 2022-09-12 18:00:10 -0700 | [diff] [blame] | 32 | android::CallStack cs; |
| 33 | cs.update(); |
| 34 | backtrace = cs.toString(); |
| 35 | } |
| 36 | |
| 37 | TEST(CallStackTest, current_backtrace) { |
| 38 | android::String8 backtrace; |
| 39 | CurrentCaller(backtrace); |
| 40 | |
| 41 | ASSERT_NE(-1, backtrace.find("(CurrentCaller")) << "Full backtrace:\n" << backtrace; |
| 42 | } |
| 43 | |
Elliott Hughes | b795d6f | 2022-09-14 20:15:19 +0000 | [diff] [blame] | 44 | __attribute__((__noinline__)) extern "C" void ThreadBusyWait(std::atomic<pid_t>* tid, |
| 45 | volatile bool* done) { |
Christopher Ferris | 15fee82 | 2022-09-12 18:00:10 -0700 | [diff] [blame] | 46 | *tid = android::base::GetThreadId(); |
| 47 | while (!*done) { |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | TEST(CallStackTest, thread_backtrace) { |
| 52 | // Use a volatile to avoid any problems unwinding since sometimes |
| 53 | // accessing a std::atomic does not include unwind data at every |
| 54 | // instruction and leads to failed unwinds. |
| 55 | volatile bool done = false; |
| 56 | std::atomic<pid_t> tid = -1; |
| 57 | std::thread thread([&tid, &done]() { ThreadBusyWait(&tid, &done); }); |
| 58 | |
| 59 | while (tid == -1) { |
| 60 | } |
| 61 | |
| 62 | android::CallStack cs; |
| 63 | cs.update(0, tid); |
| 64 | |
| 65 | done = true; |
| 66 | thread.join(); |
| 67 | |
| 68 | ASSERT_NE(-1, cs.toString().find("(ThreadBusyWait")) << "Full backtrace:\n" << cs.toString(); |
| 69 | } |
Christopher Ferris | bcaeacc | 2024-03-27 16:17:50 -0700 | [diff] [blame] | 70 | |
| 71 | #if defined(__ANDROID__) |
| 72 | TEST(CallStackTest, log_stack) { |
| 73 | android::CallStack::logStack("callstack_test"); |
| 74 | auto logger_list = android_logger_list_open(android_name_to_log_id("main"), |
Steven Moreland | 47fde79 | 2024-06-26 17:50:14 +0000 | [diff] [blame^] | 75 | ANDROID_LOG_NONBLOCK, |
| 76 | 10000 /* tail */, getpid()); |
Christopher Ferris | bcaeacc | 2024-03-27 16:17:50 -0700 | [diff] [blame] | 77 | ASSERT_NE(nullptr, logger_list); |
| 78 | std::string log; |
| 79 | while (true) { |
| 80 | log_msg log_msg; |
| 81 | auto ret = android_logger_list_read(logger_list, &log_msg); |
| 82 | if (ret == -EAGAIN) { |
| 83 | break; |
| 84 | } |
| 85 | ASSERT_GT(ret, 0); |
| 86 | if (log_msg.msg() == nullptr) { |
| 87 | continue; |
| 88 | } |
| 89 | // First get the tag. |
| 90 | char* msg = &log_msg.msg()[1]; |
| 91 | if (std::string(msg) != "callstack_test") { |
| 92 | continue; |
| 93 | } |
| 94 | // Now move past the tag. |
| 95 | msg = &msg[strlen(msg) + 1]; |
| 96 | log += msg; |
| 97 | log += '\n'; |
| 98 | } |
| 99 | ASSERT_NE("", log) << "No messages found in the log from the test."; |
| 100 | // Look for a backtrace line such as: |
| 101 | // #00 pc 00000000000536e4 libutils_test (testing::Test::Run()+436) |
| 102 | ASSERT_MATCH(log, "#\\d+ pc \\d+"); |
| 103 | android_logger_list_close(logger_list); |
| 104 | } |
| 105 | #endif |