xref: /aosp_15_r20/system/extras/simpleperf/thread_tree_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include "thread_tree.h"
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include "read_symbol_map.h"
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
24*288bf522SAndroid Build Coastguard Worker 
25*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
26*288bf522SAndroid Build Coastguard Worker class ThreadTreeTest : public ::testing::Test {
27*288bf522SAndroid Build Coastguard Worker  protected:
AddMap(uint64_t start,uint64_t end,const std::string & name)28*288bf522SAndroid Build Coastguard Worker   void AddMap(uint64_t start, uint64_t end, const std::string& name) {
29*288bf522SAndroid Build Coastguard Worker     thread_tree_.AddThreadMap(0, 0, start, end - start, start, name);
30*288bf522SAndroid Build Coastguard Worker     if (expected_names_.size() < end) {
31*288bf522SAndroid Build Coastguard Worker       expected_names_.resize(end);
32*288bf522SAndroid Build Coastguard Worker     }
33*288bf522SAndroid Build Coastguard Worker     for (uint64_t i = start; i < end; ++i) {
34*288bf522SAndroid Build Coastguard Worker       expected_names_[i] = name;
35*288bf522SAndroid Build Coastguard Worker     }
36*288bf522SAndroid Build Coastguard Worker   }
37*288bf522SAndroid Build Coastguard Worker 
CheckMaps()38*288bf522SAndroid Build Coastguard Worker   void CheckMaps() {
39*288bf522SAndroid Build Coastguard Worker     std::vector<std::string> names;
40*288bf522SAndroid Build Coastguard Worker     ThreadEntry* thread = thread_tree_.FindThreadOrNew(0, 0);
41*288bf522SAndroid Build Coastguard Worker     ASSERT_TRUE(thread != nullptr);
42*288bf522SAndroid Build Coastguard Worker     ASSERT_TRUE(thread->maps != nullptr);
43*288bf522SAndroid Build Coastguard Worker     uint64_t prev_end = 0;
44*288bf522SAndroid Build Coastguard Worker     for (auto& pair : thread->maps->maps) {
45*288bf522SAndroid Build Coastguard Worker       ASSERT_GE(pair.first, prev_end);
46*288bf522SAndroid Build Coastguard Worker       prev_end = pair.second->get_end_addr();
47*288bf522SAndroid Build Coastguard Worker       ASSERT_EQ(pair.second->start_addr, pair.first);
48*288bf522SAndroid Build Coastguard Worker       ASSERT_GT(pair.second->len, 0u);
49*288bf522SAndroid Build Coastguard Worker       ASSERT_EQ(pair.second->pgoff, pair.first);
50*288bf522SAndroid Build Coastguard Worker       if (names.size() < pair.second->get_end_addr()) {
51*288bf522SAndroid Build Coastguard Worker         names.resize(pair.second->get_end_addr());
52*288bf522SAndroid Build Coastguard Worker       }
53*288bf522SAndroid Build Coastguard Worker       for (uint64_t i = pair.first; i < pair.second->get_end_addr(); ++i) {
54*288bf522SAndroid Build Coastguard Worker         names[i] = pair.second->dso->Path();
55*288bf522SAndroid Build Coastguard Worker       }
56*288bf522SAndroid Build Coastguard Worker     }
57*288bf522SAndroid Build Coastguard Worker     ASSERT_EQ(names, expected_names_);
58*288bf522SAndroid Build Coastguard Worker     // Check result of ThreadTree::FindMap.
59*288bf522SAndroid Build Coastguard Worker     for (size_t i = 0; i < expected_names_.size(); ++i) {
60*288bf522SAndroid Build Coastguard Worker       const MapEntry* entry = thread_tree_.FindMap(thread, i, false);
61*288bf522SAndroid Build Coastguard Worker       ASSERT_TRUE(entry != nullptr);
62*288bf522SAndroid Build Coastguard Worker       if (expected_names_[i].empty()) {
63*288bf522SAndroid Build Coastguard Worker         ASSERT_TRUE(thread_tree_.IsUnknownDso(entry->dso));
64*288bf522SAndroid Build Coastguard Worker       } else {
65*288bf522SAndroid Build Coastguard Worker         ASSERT_EQ(entry->dso->Path(), expected_names_[i]);
66*288bf522SAndroid Build Coastguard Worker       }
67*288bf522SAndroid Build Coastguard Worker     }
68*288bf522SAndroid Build Coastguard Worker   }
69*288bf522SAndroid Build Coastguard Worker 
FindSymbol(int pid,int tid,uint64_t ip,bool in_kernel=false)70*288bf522SAndroid Build Coastguard Worker   const Symbol* FindSymbol(int pid, int tid, uint64_t ip, bool in_kernel = false) {
71*288bf522SAndroid Build Coastguard Worker     auto thread = thread_tree_.FindThreadOrNew(pid, tid);
72*288bf522SAndroid Build Coastguard Worker     auto map = thread_tree_.FindMap(thread, ip, in_kernel);
73*288bf522SAndroid Build Coastguard Worker     return thread_tree_.FindSymbol(map, ip, nullptr, nullptr);
74*288bf522SAndroid Build Coastguard Worker   }
75*288bf522SAndroid Build Coastguard Worker 
76*288bf522SAndroid Build Coastguard Worker   std::vector<std::string> expected_names_;
77*288bf522SAndroid Build Coastguard Worker   ThreadTree thread_tree_;
78*288bf522SAndroid Build Coastguard Worker };
79*288bf522SAndroid Build Coastguard Worker 
80*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST_F(ThreadTreeTest,maps_smoke)81*288bf522SAndroid Build Coastguard Worker TEST_F(ThreadTreeTest, maps_smoke) {
82*288bf522SAndroid Build Coastguard Worker   AddMap(0, 5, "0");
83*288bf522SAndroid Build Coastguard Worker   AddMap(10, 15, "1");
84*288bf522SAndroid Build Coastguard Worker   CheckMaps();
85*288bf522SAndroid Build Coastguard Worker 
86*288bf522SAndroid Build Coastguard Worker   // Overlap left.
87*288bf522SAndroid Build Coastguard Worker   AddMap(1, 6, "2");
88*288bf522SAndroid Build Coastguard Worker   CheckMaps();
89*288bf522SAndroid Build Coastguard Worker   AddMap(4, 5, "3");
90*288bf522SAndroid Build Coastguard Worker   CheckMaps();
91*288bf522SAndroid Build Coastguard Worker 
92*288bf522SAndroid Build Coastguard Worker   // Overlap right.
93*288bf522SAndroid Build Coastguard Worker   AddMap(9, 12, "4");
94*288bf522SAndroid Build Coastguard Worker   CheckMaps();
95*288bf522SAndroid Build Coastguard Worker   AddMap(8, 15, "5");
96*288bf522SAndroid Build Coastguard Worker   CheckMaps();
97*288bf522SAndroid Build Coastguard Worker   AddMap(7, 16, "6");
98*288bf522SAndroid Build Coastguard Worker   CheckMaps();
99*288bf522SAndroid Build Coastguard Worker 
100*288bf522SAndroid Build Coastguard Worker   // Overlap all.
101*288bf522SAndroid Build Coastguard Worker   AddMap(0, 17, "7");
102*288bf522SAndroid Build Coastguard Worker   CheckMaps();
103*288bf522SAndroid Build Coastguard Worker }
104*288bf522SAndroid Build Coastguard Worker 
105*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST_F(ThreadTreeTest,jit_maps_before_fork)106*288bf522SAndroid Build Coastguard Worker TEST_F(ThreadTreeTest, jit_maps_before_fork) {
107*288bf522SAndroid Build Coastguard Worker   // Maps for JIT symfiles can arrive before fork records.
108*288bf522SAndroid Build Coastguard Worker   thread_tree_.AddThreadMap(0, 0, 0, 1, 0, "0", map_flags::PROT_JIT_SYMFILE_MAP);
109*288bf522SAndroid Build Coastguard Worker   thread_tree_.AddThreadMap(1, 1, 1, 1, 1, "1");
110*288bf522SAndroid Build Coastguard Worker   thread_tree_.ForkThread(0, 0, 1, 1);
111*288bf522SAndroid Build Coastguard Worker   expected_names_ = {"0", "1"};
112*288bf522SAndroid Build Coastguard Worker   CheckMaps();
113*288bf522SAndroid Build Coastguard Worker   ThreadEntry* thread = thread_tree_.FindThreadOrNew(0, 0);
114*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(thread != nullptr);
115*288bf522SAndroid Build Coastguard Worker   const MapEntry* map = thread_tree_.FindMap(thread, 0);
116*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(map != nullptr);
117*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(map->flags, map_flags::PROT_JIT_SYMFILE_MAP);
118*288bf522SAndroid Build Coastguard Worker }
119*288bf522SAndroid Build Coastguard Worker 
120*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST_F(ThreadTreeTest,reused_tid)121*288bf522SAndroid Build Coastguard Worker TEST_F(ThreadTreeTest, reused_tid) {
122*288bf522SAndroid Build Coastguard Worker   // Process 1 has thread 1 and 2.
123*288bf522SAndroid Build Coastguard Worker   thread_tree_.ForkThread(1, 2, 1, 1);
124*288bf522SAndroid Build Coastguard Worker   // Thread 2 exits.
125*288bf522SAndroid Build Coastguard Worker   thread_tree_.ExitThread(1, 2);
126*288bf522SAndroid Build Coastguard Worker   // Thread 1 forks process 2.
127*288bf522SAndroid Build Coastguard Worker   thread_tree_.ForkThread(2, 2, 1, 1);
128*288bf522SAndroid Build Coastguard Worker }
129*288bf522SAndroid Build Coastguard Worker 
130*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST_F(ThreadTreeTest,reused_tid_without_thread_exit)131*288bf522SAndroid Build Coastguard Worker TEST_F(ThreadTreeTest, reused_tid_without_thread_exit) {
132*288bf522SAndroid Build Coastguard Worker   // Similar to the above test, but the thread exit record is missing.
133*288bf522SAndroid Build Coastguard Worker   thread_tree_.ForkThread(1, 2, 1, 1);
134*288bf522SAndroid Build Coastguard Worker   thread_tree_.ForkThread(2, 2, 1, 1);
135*288bf522SAndroid Build Coastguard Worker }
136*288bf522SAndroid Build Coastguard Worker 
137*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST_F(ThreadTreeTest,add_symbols_for_process)138*288bf522SAndroid Build Coastguard Worker TEST_F(ThreadTreeTest, add_symbols_for_process) {
139*288bf522SAndroid Build Coastguard Worker   std::string symbol_map(
140*288bf522SAndroid Build Coastguard Worker       "0x2000 0x20 two\n"
141*288bf522SAndroid Build Coastguard Worker       "0x1000 0x10 one\n"
142*288bf522SAndroid Build Coastguard Worker       "0x3000 0x30 three\n");
143*288bf522SAndroid Build Coastguard Worker 
144*288bf522SAndroid Build Coastguard Worker   auto symbols = ReadSymbolMapFromString(symbol_map);
145*288bf522SAndroid Build Coastguard Worker 
146*288bf522SAndroid Build Coastguard Worker   thread_tree_.AddSymbolsForProcess(1, &symbols);
147*288bf522SAndroid Build Coastguard Worker 
148*288bf522SAndroid Build Coastguard Worker   ASSERT_STREQ("one", FindSymbol(1, 1, 0x1000)->Name());
149*288bf522SAndroid Build Coastguard Worker   ASSERT_STREQ("two", FindSymbol(1, 1, 0x2010)->Name());
150*288bf522SAndroid Build Coastguard Worker   ASSERT_STREQ("three", FindSymbol(1, 1, 0x302f)->Name());
151*288bf522SAndroid Build Coastguard Worker }
152*288bf522SAndroid Build Coastguard Worker 
153*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST_F(ThreadTreeTest,invalid_fork)154*288bf522SAndroid Build Coastguard Worker TEST_F(ThreadTreeTest, invalid_fork) {
155*288bf522SAndroid Build Coastguard Worker   // tid == ptid
156*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(thread_tree_.ForkThread(1, 2, 1, 2));
157*288bf522SAndroid Build Coastguard Worker   // pid != tid && pid != ppid
158*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(thread_tree_.ForkThread(1, 2, 3, 1));
159*288bf522SAndroid Build Coastguard Worker }
160