blob: 3da22b4fb9fa3086266e97242309c103d117f66f [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>
Adam Lesinski330edcd2015-05-04 17:40:56 -070020#include <map>
21#include <memory>
Adam Lesinskid13fb242015-05-12 20:40:48 -070022#include <queue>
Adam Lesinski330edcd2015-05-04 17:40:56 -070023#include <set>
24#include <vector>
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "android-base/logging.h"
Adam Lesinski93190b72017-11-03 15:20:17 -070027#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028
29#include "ResourceTable.h"
30#include "ResourceValues.h"
31#include "ValueVisitor.h"
Adam Lesinski93190b72017-11-03 15:20:17 -070032#include "text/Printer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033#include "util/Util.h"
34
Adam Lesinski93190b72017-11-03 15:20:17 -070035using ::aapt::text::Printer;
36using ::android::StringPiece;
37using ::android::base::StringPrintf;
38
Adam Lesinski330edcd2015-05-04 17:40:56 -070039namespace aapt {
40
Adam Lesinski6b372992017-08-09 10:54:23 -070041namespace {
42
Adam Lesinski93190b72017-11-03 15:20:17 -070043class ValueHeadlinePrinter : public ConstValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -070045 using ConstValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinski93190b72017-11-03 15:20:17 -070047 explicit ValueHeadlinePrinter(const std::string& package, Printer* printer)
48 : package_(package), printer_(printer) {
49 }
50
Adam Lesinskie59f0d82017-10-13 09:36:53 -070051 void Visit(const Attribute* attr) override {
Adam Lesinski93190b72017-11-03 15:20:17 -070052 printer_->Print("(attr) type=");
53 printer_->Print(attr->MaskString());
54 if (!attr->symbols.empty()) {
55 printer_->Print(StringPrintf(" size=%zd", attr->symbols.size()));
56 }
57 }
58
59 void Visit(const Style* style) override {
60 printer_->Print(StringPrintf("(style) size=%zd", style->entries.size()));
61 if (style->parent) {
62 printer_->Print(" parent=");
63
64 const Reference& parent_ref = style->parent.value();
65 if (parent_ref.name) {
66 if (parent_ref.private_reference) {
67 printer_->Print("*");
68 }
69
70 const ResourceName& parent_name = parent_ref.name.value();
71 if (package_ != parent_name.package) {
72 printer_->Print(parent_name.package);
73 printer_->Print(":");
74 }
75 printer_->Print(to_string(parent_name.type));
76 printer_->Print("/");
77 printer_->Print(parent_name.entry);
78 if (parent_ref.id) {
79 printer_->Print(" (");
80 printer_->Print(parent_ref.id.value().to_string());
81 printer_->Print(")");
82 }
83 } else if (parent_ref.id) {
84 printer_->Print(parent_ref.id.value().to_string());
85 } else {
86 printer_->Print("???");
87 }
88 }
89 }
90
91 void Visit(const Array* array) override {
92 printer_->Print(StringPrintf("(array) size=%zd", array->elements.size()));
93 }
94
95 void Visit(const Plural* plural) override {
96 size_t count = std::count_if(plural->values.begin(), plural->values.end(),
97 [](const std::unique_ptr<Item>& v) { return v != nullptr; });
98 printer_->Print(StringPrintf("(plurals) size=%zd", count));
99 }
100
101 void Visit(const Styleable* styleable) override {
102 printer_->Println(StringPrintf("(styleable) size=%zd", styleable->entries.size()));
103 }
104
105 void VisitItem(const Item* item) override {
106 // Pretty much guaranteed to be one line.
107 if (const Reference* ref = ValueCast<Reference>(item)) {
108 // Special case Reference so that we can print local resources without a package name.
109 ref->PrettyPrint(package_, printer_);
110 } else {
111 item->PrettyPrint(printer_);
112 }
113 }
114
115 private:
116 std::string package_;
117 Printer* printer_;
118};
119
120class ValueBodyPrinter : public ConstValueVisitor {
121 public:
122 using ConstValueVisitor::Visit;
123
124 explicit ValueBodyPrinter(const std::string& package, Printer* printer)
125 : package_(package), printer_(printer) {
126 }
127
128 void Visit(const Attribute* attr) override {
129 constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 if (attr->type_mask & kMask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 for (const auto& symbol : attr->symbols) {
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700132 if (symbol.symbol.name) {
133 printer_->Print(symbol.symbol.name.value().entry);
134
135 if (symbol.symbol.id) {
136 printer_->Print("(");
137 printer_->Print(symbol.symbol.id.value().to_string());
138 printer_->Print(")");
139 }
140 } else if (symbol.symbol.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700141 printer_->Print(symbol.symbol.id.value().to_string());
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700142 } else {
143 printer_->Print("???");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700144 }
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700145
Adam Lesinski93190b72017-11-03 15:20:17 -0700146 printer_->Println(StringPrintf("=0x%08x", symbol.value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700148 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700150
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700151 void Visit(const Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 for (const auto& entry : style->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 if (entry.key.name) {
154 const ResourceName& name = entry.key.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700155 if (!name.package.empty() && name.package != package_) {
156 printer_->Print(name.package);
157 printer_->Print(":");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700158 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700159 printer_->Print(name.entry);
160
161 if (entry.key.id) {
162 printer_->Print("(");
163 printer_->Print(entry.key.id.value().to_string());
164 printer_->Print(")");
165 }
166 } else if (entry.key.id) {
167 printer_->Print(entry.key.id.value().to_string());
168 } else {
169 printer_->Print("???");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 }
171
Adam Lesinski93190b72017-11-03 15:20:17 -0700172 printer_->Print("=");
173 PrintItem(*entry.value);
174 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700175 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700177
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700178 void Visit(const Array* array) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700179 const size_t count = array->elements.size();
180 printer_->Print("[");
181 if (count > 0) {
182 for (size_t i = 0u; i < count; i++) {
183 if (i != 0u && i % 4u == 0u) {
184 printer_->Println();
185 printer_->Print(" ");
186 }
187 PrintItem(*array->elements[i]);
188 if (i != count - 1) {
189 printer_->Print(", ");
190 }
191 }
192 printer_->Println("]");
193 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700194 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700195
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700196 void Visit(const Plural* plural) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700197 constexpr std::array<const char*, Plural::Count> kPluralNames = {
198 {"zero", "one", "two", "few", "many", "other"}};
199
200 for (size_t i = 0; i < Plural::Count; i++) {
201 if (plural->values[i] != nullptr) {
202 printer_->Print(StringPrintf("%s=", kPluralNames[i]));
203 PrintItem(*plural->values[i]);
204 printer_->Println();
205 }
206 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700207 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700208
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700209 void Visit(const Styleable* styleable) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 for (const auto& attr : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 if (attr.name) {
212 const ResourceName& name = attr.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700213 if (!name.package.empty() && name.package != package_) {
214 printer_->Print(name.package);
215 printer_->Print(":");
Adam Lesinski355f2852016-02-13 20:26:45 -0800216 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700217 printer_->Print(name.entry);
218
219 if (attr.id) {
220 printer_->Print("(");
221 printer_->Print(attr.id.value().to_string());
222 printer_->Print(")");
223 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700225
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 if (attr.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700227 printer_->Print(attr.id.value().to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 }
Adam Lesinski5a217b02017-11-16 16:58:02 -0800229 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700230 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 }
232
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700233 void VisitItem(const Item* item) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700234 // Intentionally left empty, we already printed the Items.
Adam Lesinski6b372992017-08-09 10:54:23 -0700235 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700236
237 private:
238 void PrintItem(const Item& item) {
239 if (const Reference* ref = ValueCast<Reference>(&item)) {
240 // Special case Reference so that we can print local resources without a package name.
241 ref->PrettyPrint(package_, printer_);
242 } else {
243 item.PrettyPrint(printer_);
244 }
245 }
246
247 std::string package_;
248 Printer* printer_;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700249};
250
Adam Lesinski6b372992017-08-09 10:54:23 -0700251} // namespace
252
Adam Lesinski93190b72017-11-03 15:20:17 -0700253void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options,
254 Printer* printer) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700255 for (const auto& package : table.packages) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700256 ValueHeadlinePrinter headline_printer(package->name, printer);
257 ValueBodyPrinter body_printer(package->name, printer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258
Adam Lesinski93190b72017-11-03 15:20:17 -0700259 printer->Print("Package name=");
260 printer->Print(package->name);
261 if (package->id) {
262 printer->Print(StringPrintf(" id=%02x", package->id.value()));
263 }
264 printer->Println();
265
266 printer->Indent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 for (const auto& type : package->types) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700268 printer->Print("type ");
269 printer->Print(to_string(type->type));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 if (type->id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700271 printer->Print(StringPrintf(" id=%02x", type->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700273 printer->Println(StringPrintf(" entryCount=%zd", type->entries.size()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 std::vector<const ResourceEntry*> sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 for (const auto& entry : type->entries) {
277 auto iter = std::lower_bound(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 sorted_entries.begin(), sorted_entries.end(), entry.get(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 [](const ResourceEntry* a, const ResourceEntry* b) -> bool {
280 if (a->id && b->id) {
281 return a->id.value() < b->id.value();
282 } else if (a->id) {
283 return true;
284 } else {
285 return false;
286 }
287 });
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 sorted_entries.insert(iter, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 }
290
Adam Lesinski93190b72017-11-03 15:20:17 -0700291 printer->Indent();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 for (const ResourceEntry* entry : sorted_entries) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700293 const ResourceId id(package->id.value_or_default(0), type->id.value_or_default(0),
294 entry->id.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295
Adam Lesinski93190b72017-11-03 15:20:17 -0700296 printer->Print("resource ");
297 printer->Print(id.to_string());
298 printer->Print(" ");
299
300 // Write the name without the package (this is obvious and too verbose).
301 printer->Print(to_string(type->type));
302 printer->Print("/");
303 printer->Print(entry->name);
304
Adam Lesinski71be7052017-12-12 16:48:07 -0800305 switch (entry->visibility.level) {
306 case Visibility::Level::kPublic:
Adam Lesinski93190b72017-11-03 15:20:17 -0700307 printer->Print(" PUBLIC");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800309 case Visibility::Level::kPrivate:
Adam Lesinski93190b72017-11-03 15:20:17 -0700310 printer->Print(" _PRIVATE_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800312 case Visibility::Level::kUndefined:
Adam Lesinski93190b72017-11-03 15:20:17 -0700313 // Print nothing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 break;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700315 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316
Adam Lesinski93190b72017-11-03 15:20:17 -0700317 printer->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700318
Adam Lesinski71be7052017-12-12 16:48:07 -0800319 if (options.show_values) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700320 printer->Indent();
Adam Lesinski71be7052017-12-12 16:48:07 -0800321 for (const auto& value : entry->values) {
322 printer->Print("(");
323 printer->Print(value->config.to_string());
324 printer->Print(") ");
325 value->value->Accept(&headline_printer);
326 if (options.show_sources && !value->value->GetSource().path.empty()) {
327 printer->Print(" src=");
328 printer->Print(value->value->GetSource().to_string());
329 }
330 printer->Println();
331 printer->Indent();
332 value->value->Accept(&body_printer);
333 printer->Undent();
334 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700335 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700336 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700338 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700339 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700340 printer->Undent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700342}
343
Adam Lesinski6b372992017-08-09 10:54:23 -0700344static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 auto iter = std::lower_bound(names.begin(), names.end(), name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 CHECK(iter != names.end());
347 CHECK(*iter == name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 return std::distance(names.begin(), iter);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700349}
350
Adam Lesinski6b372992017-08-09 10:54:23 -0700351void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 std::map<ResourceName, std::set<ResourceName>> graph;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 std::queue<ResourceName> styles_to_visit;
355 styles_to_visit.push(target_style);
356 for (; !styles_to_visit.empty(); styles_to_visit.pop()) {
357 const ResourceName& style_name = styles_to_visit.front();
358 std::set<ResourceName>& parents = graph[style_name];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 if (!parents.empty()) {
360 // We've already visited this style.
361 continue;
362 }
363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 Maybe<ResourceTable::SearchResult> result = table->FindResource(style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 if (result) {
366 ResourceEntry* entry = result.value().entry;
367 for (const auto& value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 if (Style* style = ValueCast<Style>(value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 if (style->parent && style->parent.value().name) {
370 parents.insert(style->parent.value().name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 styles_to_visit.push(style->parent.value().name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 }
Adam Lesinskid13fb242015-05-12 20:40:48 -0700373 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700375 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700377
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 std::vector<ResourceName> names;
379 for (const auto& entry : graph) {
380 names.push_back(entry.first);
381 }
382
383 std::cout << "digraph styles {\n";
384 for (const auto& name : names) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700385 std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 }
387
388 for (const auto& entry : graph) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 const ResourceName& style_name = entry.first;
390 size_t style_node_index = GetNodeIndex(names, style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 for (const auto& parent_name : entry.second) {
393 std::cout << " node_" << style_node_index << " -> "
394 << "node_" << GetNodeIndex(names, parent_name) << ";\n";
Adam Lesinskid13fb242015-05-12 20:40:48 -0700395 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700397
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 std::cout << "}" << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700399}
400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401void Debug::DumpHex(const void* data, size_t len) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 const uint8_t* d = (const uint8_t*)data;
403 for (size_t i = 0; i < len; i++) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700404 std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " ";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 if (i % 8 == 7) {
406 std::cerr << "\n";
Adam Lesinski52364f72016-01-11 13:10:24 -0800407 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800409
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 if (len - 1 % 8 != 7) {
411 std::cerr << std::endl;
412 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800413}
414
Ryan Mitchell5d275512018-07-19 14:29:00 -0700415void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) {
416 using namespace android;
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800417
Ryan Mitchell5d275512018-07-19 14:29:00 -0700418 if (pool->getError() == NO_INIT) {
419 printer->Print("String pool is unitialized.\n");
420 return;
421 } else if (pool->getError() != NO_ERROR) {
422 printer->Print("String pool is corrupt/invalid.\n");
423 return;
424 }
425
426 SortedVector<const void*> uniqueStrings;
427 const size_t N = pool->size();
428 for (size_t i=0; i<N; i++) {
429 size_t len;
430 if (pool->isUTF8()) {
431 uniqueStrings.add(pool->string8At(i, &len));
432 } else {
433 uniqueStrings.add(pool->stringAt(i, &len));
434 }
435 }
436
437 printer->Print(StringPrintf("String pool of %zd unique %s %s strings, %zd entries and %zd styles "
438 "using %zd bytes:\n", uniqueStrings.size(),
439 pool->isUTF8() ? "UTF-8" : "UTF-16",
440 pool->isSorted() ? "sorted" : "non-sorted", N, pool->styleCount(),
441 pool->bytes()));
442
443 const size_t NS = pool->size();
444 for (size_t s=0; s<NS; s++) {
445 String8 str = pool->string8ObjectAt(s);
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000446 printer->Print(StringPrintf("String #%zd : %s\n", s, str.string()));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700447 }
448}
449
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700450namespace {
451
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700452class XmlPrinter : public xml::ConstVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700454 using xml::ConstVisitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700455
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800456 explicit XmlPrinter(Printer* printer) : printer_(printer) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800457 }
458
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700459 void Visit(const xml::Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700460 for (const xml::NamespaceDecl& decl : el->namespace_decls) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800461 printer_->Println(StringPrintf("N: %s=%s (line=%zu)", decl.prefix.c_str(), decl.uri.c_str(),
462 decl.line_number));
463 printer_->Indent();
Adam Lesinski6b372992017-08-09 10:54:23 -0700464 }
465
Adam Lesinskida9eba32018-02-13 16:44:10 -0800466 printer_->Print("E: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 if (!el->namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800468 printer_->Print(el->namespace_uri);
469 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800471 printer_->Println(StringPrintf("%s (line=%zu)", el->name.c_str(), el->line_number));
472 printer_->Indent();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700473
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 for (const xml::Attribute& attr : el->attributes) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800475 printer_->Print("A: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 if (!attr.namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800477 printer_->Print(attr.namespace_uri);
478 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800480 printer_->Print(attr.name);
Adam Lesinski4ca56972017-04-26 21:49:53 -0700481
482 if (attr.compiled_attribute) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800483 printer_->Print("(");
484 printer_->Print(
485 attr.compiled_attribute.value().id.value_or_default(ResourceId(0)).to_string());
486 printer_->Print(")");
Adam Lesinski4ca56972017-04-26 21:49:53 -0700487 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800488 printer_->Print("=");
Shane Farmer6ed40612017-09-06 10:00:07 -0700489 if (attr.compiled_value != nullptr) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800490 attr.compiled_value->PrettyPrint(printer_);
Shane Farmer6ed40612017-09-06 10:00:07 -0700491 } else {
Adam Lesinskibbf42972018-02-14 13:36:09 -0800492 printer_->Print("\"");
Adam Lesinskida9eba32018-02-13 16:44:10 -0800493 printer_->Print(attr.value);
Adam Lesinskibbf42972018-02-14 13:36:09 -0800494 printer_->Print("\"");
495 }
496
497 if (!attr.value.empty()) {
498 printer_->Print(" (Raw: \"");
499 printer_->Print(attr.value);
500 printer_->Print("\")");
Shane Farmer6ed40612017-09-06 10:00:07 -0700501 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800502 printer_->Println();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700503 }
504
Adam Lesinskida9eba32018-02-13 16:44:10 -0800505 printer_->Indent();
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700506 xml::ConstVisitor::Visit(el);
Adam Lesinskida9eba32018-02-13 16:44:10 -0800507 printer_->Undent();
508 printer_->Undent();
509
510 for (size_t i = 0; i < el->namespace_decls.size(); i++) {
511 printer_->Undent();
512 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700514
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700515 void Visit(const xml::Text* text) override {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800516 printer_->Println(StringPrintf("T: '%s'", text->text.c_str()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 }
518
519 private:
Adam Lesinskida9eba32018-02-13 16:44:10 -0800520 Printer* printer_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700521};
522
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700524
Adam Lesinskida9eba32018-02-13 16:44:10 -0800525void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) {
526 XmlPrinter xml_visitor(printer);
527 doc.root->Accept(&xml_visitor);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700528}
Adam Lesinski52364f72016-01-11 13:10:24 -0800529
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530} // namespace aapt