blob: b872ebbeb159806a7f882fa507786464be0eb9e2 [file] [log] [blame]
Adam Lesinski330edcd2015-05-04 17:40:56 -07001/*
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"
Adam Lesinski330edcd2015-05-04 17:40:56 -070018
19#include <algorithm>
20#include <iostream>
21#include <map>
22#include <memory>
Adam Lesinskid13fb242015-05-12 20:40:48 -070023#include <queue>
Adam Lesinski330edcd2015-05-04 17:40:56 -070024#include <set>
25#include <vector>
26
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "android-base/logging.h"
28
29#include "ResourceTable.h"
30#include "ResourceValues.h"
31#include "ValueVisitor.h"
32#include "util/Util.h"
33
Adam Lesinski330edcd2015-05-04 17:40:56 -070034namespace aapt {
35
Adam Lesinski355f2852016-02-13 20:26:45 -080036class PrintVisitor : public ValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 using ValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 void Visit(Attribute* attr) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 std::cout << "(attr) type=";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 attr->PrintMask(&std::cout);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 static constexpr uint32_t kMask =
44 android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 if (attr->type_mask & kMask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 for (const auto& symbol : attr->symbols) {
47 std::cout << "\n " << symbol.symbol.name.value().entry;
48 if (symbol.symbol.id) {
49 std::cout << " (" << symbol.symbol.id.value() << ")";
Adam Lesinski330edcd2015-05-04 17:40:56 -070050 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 std::cout << " = " << symbol.value;
52 }
Adam Lesinski330edcd2015-05-04 17:40:56 -070053 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 }
Adam Lesinski330edcd2015-05-04 17:40:56 -070055
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 void Visit(Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 std::cout << "(style)";
58 if (style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 const Reference& parent_ref = style->parent.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 std::cout << " parent=";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 if (parent_ref.name) {
62 if (parent_ref.private_reference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 std::cout << "*";
Adam Lesinski330edcd2015-05-04 17:40:56 -070064 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::cout << parent_ref.name.value() << " ";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 }
Adam Lesinski330edcd2015-05-04 17:40:56 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 if (parent_ref.id) {
69 std::cout << parent_ref.id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 }
71 }
Adam Lesinski330edcd2015-05-04 17:40:56 -070072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 for (const auto& entry : style->entries) {
74 std::cout << "\n ";
75 if (entry.key.name) {
76 const ResourceName& name = entry.key.name.value();
77 if (!name.package.empty()) {
78 std::cout << name.package << ":";
Adam Lesinski330edcd2015-05-04 17:40:56 -070079 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 std::cout << name.entry;
81 }
82
83 if (entry.key.id) {
84 std::cout << "(" << entry.key.id.value() << ")";
85 }
86
87 std::cout << "=" << *entry.value;
Adam Lesinski330edcd2015-05-04 17:40:56 -070088 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 }
Adam Lesinski330edcd2015-05-04 17:40:56 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 void Visit(Array* array) override { array->Print(&std::cout); }
Adam Lesinski330edcd2015-05-04 17:40:56 -070092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 void Visit(Plural* plural) override { plural->Print(&std::cout); }
Adam Lesinski330edcd2015-05-04 17:40:56 -070094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 void Visit(Styleable* styleable) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 std::cout << "(styleable)";
97 for (const auto& attr : styleable->entries) {
98 std::cout << "\n ";
99 if (attr.name) {
100 const ResourceName& name = attr.name.value();
101 if (!name.package.empty()) {
102 std::cout << name.package << ":";
Adam Lesinski355f2852016-02-13 20:26:45 -0800103 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::cout << name.entry;
105 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 if (attr.id) {
108 std::cout << "(" << attr.id.value() << ")";
109 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700110 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 }
112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 void VisitItem(Item* item) override { item->Print(&std::cout); }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700114};
115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116void Debug::PrintTable(ResourceTable* table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 const DebugPrintTableOptions& options) {
118 PrintVisitor visitor;
Adam Lesinski355f2852016-02-13 20:26:45 -0800119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 for (auto& package : table->packages) {
121 std::cout << "Package name=" << package->name;
122 if (package->id) {
123 std::cout << " id=" << std::hex << (int)package->id.value() << std::dec;
124 }
125 std::cout << std::endl;
126
127 for (const auto& type : package->types) {
128 std::cout << "\n type " << type->type;
129 if (type->id) {
130 std::cout << " id=" << std::hex << (int)type->id.value() << std::dec;
131 }
132 std::cout << " entryCount=" << type->entries.size() << std::endl;
133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 std::vector<const ResourceEntry*> sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 for (const auto& entry : type->entries) {
136 auto iter = std::lower_bound(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 sorted_entries.begin(), sorted_entries.end(), entry.get(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 [](const ResourceEntry* a, const ResourceEntry* b) -> bool {
139 if (a->id && b->id) {
140 return a->id.value() < b->id.value();
141 } else if (a->id) {
142 return true;
143 } else {
144 return false;
145 }
146 });
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 sorted_entries.insert(iter, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 }
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 for (const ResourceEntry* entry : sorted_entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 ResourceId id(package->id ? package->id.value() : uint8_t(0),
152 type->id ? type->id.value() : uint8_t(0),
153 entry->id ? entry->id.value() : uint16_t(0));
154 ResourceName name(package->name, type->type, entry->name);
155
156 std::cout << " spec resource " << id << " " << name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 switch (entry->symbol_status.state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 case SymbolState::kPublic:
159 std::cout << " PUBLIC";
160 break;
161 case SymbolState::kPrivate:
162 std::cout << " _PRIVATE_";
163 break;
164 default:
165 break;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700166 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168 std::cout << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 for (const auto& value : entry->values) {
171 std::cout << " (" << value->config << ") ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 value->value->Accept(&visitor);
173 if (options.show_sources && !value->value->GetSource().path.empty()) {
174 std::cout << " src=" << value->value->GetSource();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 }
176 std::cout << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700177 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700179 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700181}
182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183static size_t GetNodeIndex(const std::vector<ResourceName>& names,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 const ResourceName& name) {
185 auto iter = std::lower_bound(names.begin(), names.end(), name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 CHECK(iter != names.end());
187 CHECK(*iter == name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 return std::distance(names.begin(), iter);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700189}
190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191void Debug::PrintStyleGraph(ResourceTable* table,
192 const ResourceName& target_style) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 std::map<ResourceName, std::set<ResourceName>> graph;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 std::queue<ResourceName> styles_to_visit;
196 styles_to_visit.push(target_style);
197 for (; !styles_to_visit.empty(); styles_to_visit.pop()) {
198 const ResourceName& style_name = styles_to_visit.front();
199 std::set<ResourceName>& parents = graph[style_name];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 if (!parents.empty()) {
201 // We've already visited this style.
202 continue;
203 }
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 Maybe<ResourceTable::SearchResult> result = table->FindResource(style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 if (result) {
207 ResourceEntry* entry = result.value().entry;
208 for (const auto& value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (Style* style = ValueCast<Style>(value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 if (style->parent && style->parent.value().name) {
211 parents.insert(style->parent.value().name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 styles_to_visit.push(style->parent.value().name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 }
Adam Lesinskid13fb242015-05-12 20:40:48 -0700214 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700216 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700218
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 std::vector<ResourceName> names;
220 for (const auto& entry : graph) {
221 names.push_back(entry.first);
222 }
223
224 std::cout << "digraph styles {\n";
225 for (const auto& name : names) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 << "\"];\n";
228 }
229
230 for (const auto& entry : graph) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 const ResourceName& style_name = entry.first;
232 size_t style_node_index = GetNodeIndex(names, style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 for (const auto& parent_name : entry.second) {
235 std::cout << " node_" << style_node_index << " -> "
236 << "node_" << GetNodeIndex(names, parent_name) << ";\n";
Adam Lesinskid13fb242015-05-12 20:40:48 -0700237 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 std::cout << "}" << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700241}
242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243void Debug::DumpHex(const void* data, size_t len) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 const uint8_t* d = (const uint8_t*)data;
245 for (size_t i = 0; i < len; i++) {
246 std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i]
247 << " ";
248 if (i % 8 == 7) {
249 std::cerr << "\n";
Adam Lesinski52364f72016-01-11 13:10:24 -0800250 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 if (len - 1 % 8 != 7) {
254 std::cerr << std::endl;
255 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800256}
257
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700258namespace {
259
260class XmlPrinter : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 using xml::Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 void Visit(xml::Element* el) override {
265 std::cerr << prefix_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 std::cerr << "E: ";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (!el->namespace_uri.empty()) {
268 std::cerr << el->namespace_uri << ":";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 std::cerr << el->name << " (line=" << el->line_number << ")\n";
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 for (const xml::Attribute& attr : el->attributes) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 std::cerr << prefix_ << " A: ";
274 if (!attr.namespace_uri.empty()) {
275 std::cerr << attr.namespace_uri << ":";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 }
Adam Lesinski4ca56972017-04-26 21:49:53 -0700277 std::cerr << attr.name;
278
279 if (attr.compiled_attribute) {
280 std::cerr << "(" << attr.compiled_attribute.value().id.value_or_default(ResourceId(0x0))
281 << ")";
282 }
283 std::cerr << "=" << attr.value << "\n";
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700284 }
285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 const size_t previous_size = prefix_.size();
287 prefix_ += " ";
288 xml::Visitor::Visit(el);
289 prefix_.resize(previous_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 void Visit(xml::Namespace* ns) override {
293 std::cerr << prefix_;
294 std::cerr << "N: " << ns->namespace_prefix << "=" << ns->namespace_uri
295 << " (line=" << ns->line_number << ")\n";
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 const size_t previous_size = prefix_.size();
298 prefix_ += " ";
299 xml::Visitor::Visit(ns);
300 prefix_.resize(previous_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700302
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 void Visit(xml::Text* text) override {
304 std::cerr << prefix_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 std::cerr << "T: '" << text->text << "'\n";
306 }
307
308 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 std::string prefix_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700310};
311
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314void Debug::DumpXml(xml::XmlResource* doc) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 XmlPrinter printer;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 doc->root->Accept(&printer);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700317}
Adam Lesinski52364f72016-01-11 13:10:24 -0800318
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319} // namespace aapt