blob: 2ea191168b94956101f59b9390b7d0ffac968a93 [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
21#include <android-base/threads.h>
22#include <gtest/gtest.h>
23#include <utils/CallStack.h>
24
25[[clang::noinline]] extern "C" void CurrentCaller(android::String8& backtrace) {
26 android::CallStack cs;
27 cs.update();
28 backtrace = cs.toString();
29}
30
31TEST(CallStackTest, current_backtrace) {
32 android::String8 backtrace;
33 CurrentCaller(backtrace);
34
35 ASSERT_NE(-1, backtrace.find("(CurrentCaller")) << "Full backtrace:\n" << backtrace;
36}
37
38[[clang::noinline]] extern "C" void ThreadBusyWait(std::atomic<pid_t>* tid, volatile bool* done) {
39 *tid = android::base::GetThreadId();
40 while (!*done) {
41 }
42}
43
44TEST(CallStackTest, thread_backtrace) {
45 // Use a volatile to avoid any problems unwinding since sometimes
46 // accessing a std::atomic does not include unwind data at every
47 // instruction and leads to failed unwinds.
48 volatile bool done = false;
49 std::atomic<pid_t> tid = -1;
50 std::thread thread([&tid, &done]() { ThreadBusyWait(&tid, &done); });
51
52 while (tid == -1) {
53 }
54
55 android::CallStack cs;
56 cs.update(0, tid);
57
58 done = true;
59 thread.join();
60
61 ASSERT_NE(-1, cs.toString().find("(ThreadBusyWait")) << "Full backtrace:\n" << cs.toString();
62}