blob: 5ed3c27e8ef2d7fdceccb8989d1f5536b979d7c7 [file] [log] [blame]
Christopher Ferris15fee822022-09-12 18:00:10 -07001/*
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 Ferrisbcaeacc2024-03-27 16:17:50 -070021#include <android-base/test_utils.h>
Christopher Ferris15fee822022-09-12 18:00:10 -070022#include <android-base/threads.h>
23#include <gtest/gtest.h>
24#include <utils/CallStack.h>
25
Christopher Ferrisbcaeacc2024-03-27 16:17:50 -070026#if defined(__ANDROID__)
27#include <log/log.h>
28#include <log/log_read.h>
29#endif
30
Elliott Hughesb795d6f2022-09-14 20:15:19 +000031__attribute__((__noinline__)) extern "C" void CurrentCaller(android::String8& backtrace) {
Christopher Ferris15fee822022-09-12 18:00:10 -070032 android::CallStack cs;
33 cs.update();
34 backtrace = cs.toString();
35}
36
37TEST(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 Hughesb795d6f2022-09-14 20:15:19 +000044__attribute__((__noinline__)) extern "C" void ThreadBusyWait(std::atomic<pid_t>* tid,
45 volatile bool* done) {
Christopher Ferris15fee822022-09-12 18:00:10 -070046 *tid = android::base::GetThreadId();
47 while (!*done) {
48 }
49}
50
51TEST(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 Ferrisbcaeacc2024-03-27 16:17:50 -070070
71#if defined(__ANDROID__)
72TEST(CallStackTest, log_stack) {
73 android::CallStack::logStack("callstack_test");
74 auto logger_list = android_logger_list_open(android_name_to_log_id("main"),
Steven Moreland47fde792024-06-26 17:50:14 +000075 ANDROID_LOG_NONBLOCK,
Elliott Hughesab772112024-07-01 13:21:36 +000076 INT_MAX /* tail */, getpid());
Christopher Ferrisbcaeacc2024-03-27 16:17:50 -070077 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