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