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