1 /*
2 * Copyright (C) 2015 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 "Debug.h"
18 #include "ResourceTable.h"
19 #include "ResourceValues.h"
20 #include "Util.h"
21
22 #include <algorithm>
23 #include <iostream>
24 #include <map>
25 #include <memory>
26 #include <queue>
27 #include <set>
28 #include <vector>
29
30 namespace aapt {
31
32 struct PrintVisitor : ConstValueVisitor {
visitaapt::PrintVisitor33 void visit(const Attribute& attr, ValueVisitorArgs&) override {
34 std::cout << "(attr) type=";
35 attr.printMask(std::cout);
36 static constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM |
37 android::ResTable_map::TYPE_FLAGS;
38 if (attr.typeMask & kMask) {
39 for (const auto& symbol : attr.symbols) {
40 std::cout << "\n "
41 << symbol.symbol.name.entry << " (" << symbol.symbol.id << ") = "
42 << symbol.value;
43 }
44 }
45 }
46
visitaapt::PrintVisitor47 void visit(const Style& style, ValueVisitorArgs&) override {
48 std::cout << "(style)";
49 if (style.parent.name.isValid() || style.parent.id.isValid()) {
50 std::cout << " parent=";
51 if (style.parent.name.isValid()) {
52 std::cout << style.parent.name << " ";
53 }
54
55 if (style.parent.id.isValid()) {
56 std::cout << style.parent.id;
57 }
58 }
59
60 for (const auto& entry : style.entries) {
61 std::cout << "\n ";
62 if (entry.key.name.isValid()) {
63 std::cout << entry.key.name.package << ":" << entry.key.name.entry;
64 }
65
66 if (entry.key.id.isValid()) {
67 std::cout << "(" << entry.key.id << ")";
68 }
69
70 std::cout << "=" << *entry.value;
71 }
72 }
73
visitaapt::PrintVisitor74 void visit(const Array& array, ValueVisitorArgs&) override {
75 array.print(std::cout);
76 }
77
visitaapt::PrintVisitor78 void visit(const Plural& plural, ValueVisitorArgs&) override {
79 plural.print(std::cout);
80 }
81
visitaapt::PrintVisitor82 void visit(const Styleable& styleable, ValueVisitorArgs&) override {
83 styleable.print(std::cout);
84 }
85
visitItemaapt::PrintVisitor86 void visitItem(const Item& item, ValueVisitorArgs& args) override {
87 item.print(std::cout);
88 }
89 };
90
printTable(const std::shared_ptr<ResourceTable> & table)91 void Debug::printTable(const std::shared_ptr<ResourceTable>& table) {
92 std::cout << "Package name=" << table->getPackage();
93 if (table->getPackageId() != ResourceTable::kUnsetPackageId) {
94 std::cout << " id=" << std::hex << table->getPackageId() << std::dec;
95 }
96 std::cout << std::endl;
97
98 for (const auto& type : *table) {
99 std::cout << " type " << type->type;
100 if (type->typeId != ResourceTableType::kUnsetTypeId) {
101 std::cout << " id=" << std::hex << type->typeId << std::dec;
102 }
103 std::cout << " entryCount=" << type->entries.size() << std::endl;
104
105 std::vector<const ResourceEntry*> sortedEntries;
106 for (const auto& entry : type->entries) {
107 auto iter = std::lower_bound(sortedEntries.begin(), sortedEntries.end(), entry.get(),
108 [](const ResourceEntry* a, const ResourceEntry* b) -> bool {
109 return a->entryId < b->entryId;
110 });
111 sortedEntries.insert(iter, entry.get());
112 }
113
114 for (const ResourceEntry* entry : sortedEntries) {
115 ResourceId id = { table->getPackageId(), type->typeId, entry->entryId };
116 ResourceName name = { table->getPackage(), type->type, entry->name };
117 std::cout << " spec resource " << id << " " << name;
118 if (entry->publicStatus.isPublic) {
119 std::cout << " PUBLIC";
120 }
121 std::cout << std::endl;
122
123 PrintVisitor visitor;
124 for (const auto& value : entry->values) {
125 std::cout << " (" << value.config << ") ";
126 value.value->accept(visitor, {});
127 std::cout << std::endl;
128 }
129 }
130 }
131 }
132
getNodeIndex(const std::vector<ResourceName> & names,const ResourceName & name)133 static size_t getNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) {
134 auto iter = std::lower_bound(names.begin(), names.end(), name);
135 assert(iter != names.end() && *iter == name);
136 return std::distance(names.begin(), iter);
137 }
138
printStyleGraph(const std::shared_ptr<ResourceTable> & table,const ResourceName & targetStyle)139 void Debug::printStyleGraph(const std::shared_ptr<ResourceTable>& table,
140 const ResourceName& targetStyle) {
141 std::map<ResourceName, std::set<ResourceName>> graph;
142
143 std::queue<ResourceName> stylesToVisit;
144 stylesToVisit.push(targetStyle);
145 for (; !stylesToVisit.empty(); stylesToVisit.pop()) {
146 const ResourceName& styleName = stylesToVisit.front();
147 std::set<ResourceName>& parents = graph[styleName];
148 if (!parents.empty()) {
149 // We've already visited this style.
150 continue;
151 }
152
153 const ResourceTableType* type;
154 const ResourceEntry* entry;
155 std::tie(type, entry) = table->findResource(styleName);
156 if (entry) {
157 for (const auto& value : entry->values) {
158 visitFunc<Style>(*value.value, [&](const Style& style) {
159 if (style.parent.name.isValid()) {
160 parents.insert(style.parent.name);
161 stylesToVisit.push(style.parent.name);
162 }
163 });
164 }
165 }
166 }
167
168 std::vector<ResourceName> names;
169 for (const auto& entry : graph) {
170 names.push_back(entry.first);
171 }
172
173 std::cout << "digraph styles {\n";
174 for (const auto& name : names) {
175 std::cout << " node_" << getNodeIndex(names, name)
176 << " [label=\"" << name << "\"];\n";
177 }
178
179 for (const auto& entry : graph) {
180 const ResourceName& styleName = entry.first;
181 size_t styleNodeIndex = getNodeIndex(names, styleName);
182
183 for (const auto& parentName : entry.second) {
184 std::cout << " node_" << styleNodeIndex << " -> "
185 << "node_" << getNodeIndex(names, parentName) << ";\n";
186 }
187 }
188
189 std::cout << "}" << std::endl;
190 }
191
192 } // namespace aapt
193