blob: 583f14ac0cbda7cf408083540a62c6f15d4d14e0 [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) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700132 printer_->Print(symbol.symbol.name.value().entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 if (symbol.symbol.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700134 printer_->Print("(");
135 printer_->Print(symbol.symbol.id.value().to_string());
136 printer_->Print(")");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700137 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700138 printer_->Println(StringPrintf("=0x%08x", symbol.value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700140 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700142
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700143 void Visit(const Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 for (const auto& entry : style->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 if (entry.key.name) {
146 const ResourceName& name = entry.key.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700147 if (!name.package.empty() && name.package != package_) {
148 printer_->Print(name.package);
149 printer_->Print(":");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700150 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700151 printer_->Print(name.entry);
152
153 if (entry.key.id) {
154 printer_->Print("(");
155 printer_->Print(entry.key.id.value().to_string());
156 printer_->Print(")");
157 }
158 } else if (entry.key.id) {
159 printer_->Print(entry.key.id.value().to_string());
160 } else {
161 printer_->Print("???");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 }
163
Adam Lesinski93190b72017-11-03 15:20:17 -0700164 printer_->Print("=");
165 PrintItem(*entry.value);
166 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700167 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700169
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700170 void Visit(const Array* array) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700171 const size_t count = array->elements.size();
172 printer_->Print("[");
173 if (count > 0) {
174 for (size_t i = 0u; i < count; i++) {
175 if (i != 0u && i % 4u == 0u) {
176 printer_->Println();
177 printer_->Print(" ");
178 }
179 PrintItem(*array->elements[i]);
180 if (i != count - 1) {
181 printer_->Print(", ");
182 }
183 }
184 printer_->Println("]");
185 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700186 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700187
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700188 void Visit(const Plural* plural) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700189 constexpr std::array<const char*, Plural::Count> kPluralNames = {
190 {"zero", "one", "two", "few", "many", "other"}};
191
192 for (size_t i = 0; i < Plural::Count; i++) {
193 if (plural->values[i] != nullptr) {
194 printer_->Print(StringPrintf("%s=", kPluralNames[i]));
195 PrintItem(*plural->values[i]);
196 printer_->Println();
197 }
198 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700199 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700200
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700201 void Visit(const Styleable* styleable) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 for (const auto& attr : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 if (attr.name) {
204 const ResourceName& name = attr.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700205 if (!name.package.empty() && name.package != package_) {
206 printer_->Print(name.package);
207 printer_->Print(":");
Adam Lesinski355f2852016-02-13 20:26:45 -0800208 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700209 printer_->Print(name.entry);
210
211 if (attr.id) {
212 printer_->Print("(");
213 printer_->Print(attr.id.value().to_string());
214 printer_->Print(")");
215 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700217
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 if (attr.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700219 printer_->Print(attr.id.value().to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 }
Adam Lesinski5a217b02017-11-16 16:58:02 -0800221 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700222 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 }
224
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700225 void VisitItem(const Item* item) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700226 // Intentionally left empty, we already printed the Items.
Adam Lesinski6b372992017-08-09 10:54:23 -0700227 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700228
229 private:
230 void PrintItem(const Item& item) {
231 if (const Reference* ref = ValueCast<Reference>(&item)) {
232 // Special case Reference so that we can print local resources without a package name.
233 ref->PrettyPrint(package_, printer_);
234 } else {
235 item.PrettyPrint(printer_);
236 }
237 }
238
239 std::string package_;
240 Printer* printer_;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700241};
242
Adam Lesinski6b372992017-08-09 10:54:23 -0700243} // namespace
244
Adam Lesinski93190b72017-11-03 15:20:17 -0700245void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options,
246 Printer* printer) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700247 for (const auto& package : table.packages) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700248 ValueHeadlinePrinter headline_printer(package->name, printer);
249 ValueBodyPrinter body_printer(package->name, printer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250
Adam Lesinski93190b72017-11-03 15:20:17 -0700251 printer->Print("Package name=");
252 printer->Print(package->name);
253 if (package->id) {
254 printer->Print(StringPrintf(" id=%02x", package->id.value()));
255 }
256 printer->Println();
257
258 printer->Indent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 for (const auto& type : package->types) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700260 printer->Print("type ");
261 printer->Print(to_string(type->type));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 if (type->id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700263 printer->Print(StringPrintf(" id=%02x", type->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700265 printer->Println(StringPrintf(" entryCount=%zd", type->entries.size()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 std::vector<const ResourceEntry*> sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 for (const auto& entry : type->entries) {
269 auto iter = std::lower_bound(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 sorted_entries.begin(), sorted_entries.end(), entry.get(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 [](const ResourceEntry* a, const ResourceEntry* b) -> bool {
272 if (a->id && b->id) {
273 return a->id.value() < b->id.value();
274 } else if (a->id) {
275 return true;
276 } else {
277 return false;
278 }
279 });
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 sorted_entries.insert(iter, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 }
282
Adam Lesinski93190b72017-11-03 15:20:17 -0700283 printer->Indent();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 for (const ResourceEntry* entry : sorted_entries) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700285 const ResourceId id(package->id.value_or_default(0), type->id.value_or_default(0),
286 entry->id.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287
Adam Lesinski93190b72017-11-03 15:20:17 -0700288 printer->Print("resource ");
289 printer->Print(id.to_string());
290 printer->Print(" ");
291
292 // Write the name without the package (this is obvious and too verbose).
293 printer->Print(to_string(type->type));
294 printer->Print("/");
295 printer->Print(entry->name);
296
Adam Lesinski71be7052017-12-12 16:48:07 -0800297 switch (entry->visibility.level) {
298 case Visibility::Level::kPublic:
Adam Lesinski93190b72017-11-03 15:20:17 -0700299 printer->Print(" PUBLIC");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800301 case Visibility::Level::kPrivate:
Adam Lesinski93190b72017-11-03 15:20:17 -0700302 printer->Print(" _PRIVATE_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800304 case Visibility::Level::kUndefined:
Adam Lesinski93190b72017-11-03 15:20:17 -0700305 // Print nothing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 break;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700307 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700309 for (size_t i = 0; i < entry->overlayable_declarations.size(); i++) {
310 printer->Print((i == 0) ? " " : "|");
311 printer->Print("OVERLAYABLE");
312
313 if (entry->overlayable_declarations[i].policy) {
314 switch (entry->overlayable_declarations[i].policy.value()) {
315 case Overlayable::Policy::kProduct:
316 printer->Print("_PRODUCT");
317 break;
318 case Overlayable::Policy::kProductServices:
319 printer->Print("_PRODUCT_SERVICES");
320 break;
321 case Overlayable::Policy::kSystem:
322 printer->Print("_SYSTEM");
323 break;
324 case Overlayable::Policy::kVendor:
325 printer->Print("_VENDOR");
326 break;
327 case Overlayable::Policy::kPublic:
328 printer->Print("_PUBLIC");
329 break;
330 }
331 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800332 }
333
Adam Lesinski93190b72017-11-03 15:20:17 -0700334 printer->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700335
Adam Lesinski71be7052017-12-12 16:48:07 -0800336 if (options.show_values) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700337 printer->Indent();
Adam Lesinski71be7052017-12-12 16:48:07 -0800338 for (const auto& value : entry->values) {
339 printer->Print("(");
340 printer->Print(value->config.to_string());
341 printer->Print(") ");
342 value->value->Accept(&headline_printer);
343 if (options.show_sources && !value->value->GetSource().path.empty()) {
344 printer->Print(" src=");
345 printer->Print(value->value->GetSource().to_string());
346 }
347 printer->Println();
348 printer->Indent();
349 value->value->Accept(&body_printer);
350 printer->Undent();
351 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700352 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700353 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700355 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700356 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700357 printer->Undent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700359}
360
Adam Lesinski6b372992017-08-09 10:54:23 -0700361static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 auto iter = std::lower_bound(names.begin(), names.end(), name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 CHECK(iter != names.end());
364 CHECK(*iter == name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 return std::distance(names.begin(), iter);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700366}
367
Adam Lesinski6b372992017-08-09 10:54:23 -0700368void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 std::map<ResourceName, std::set<ResourceName>> graph;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700370
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 std::queue<ResourceName> styles_to_visit;
372 styles_to_visit.push(target_style);
373 for (; !styles_to_visit.empty(); styles_to_visit.pop()) {
374 const ResourceName& style_name = styles_to_visit.front();
375 std::set<ResourceName>& parents = graph[style_name];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 if (!parents.empty()) {
377 // We've already visited this style.
378 continue;
379 }
380
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 Maybe<ResourceTable::SearchResult> result = table->FindResource(style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 if (result) {
383 ResourceEntry* entry = result.value().entry;
384 for (const auto& value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 if (Style* style = ValueCast<Style>(value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 if (style->parent && style->parent.value().name) {
387 parents.insert(style->parent.value().name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 styles_to_visit.push(style->parent.value().name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 }
Adam Lesinskid13fb242015-05-12 20:40:48 -0700390 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700392 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700394
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 std::vector<ResourceName> names;
396 for (const auto& entry : graph) {
397 names.push_back(entry.first);
398 }
399
400 std::cout << "digraph styles {\n";
401 for (const auto& name : names) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700402 std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 }
404
405 for (const auto& entry : graph) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 const ResourceName& style_name = entry.first;
407 size_t style_node_index = GetNodeIndex(names, style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 for (const auto& parent_name : entry.second) {
410 std::cout << " node_" << style_node_index << " -> "
411 << "node_" << GetNodeIndex(names, parent_name) << ";\n";
Adam Lesinskid13fb242015-05-12 20:40:48 -0700412 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700414
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 std::cout << "}" << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700416}
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418void Debug::DumpHex(const void* data, size_t len) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 const uint8_t* d = (const uint8_t*)data;
420 for (size_t i = 0; i < len; i++) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700421 std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " ";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 if (i % 8 == 7) {
423 std::cerr << "\n";
Adam Lesinski52364f72016-01-11 13:10:24 -0800424 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800426
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 if (len - 1 % 8 != 7) {
428 std::cerr << std::endl;
429 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800430}
431
Ryan Mitchell5d275512018-07-19 14:29:00 -0700432void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) {
433 using namespace android;
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800434
Ryan Mitchell5d275512018-07-19 14:29:00 -0700435 if (pool->getError() == NO_INIT) {
436 printer->Print("String pool is unitialized.\n");
437 return;
438 } else if (pool->getError() != NO_ERROR) {
439 printer->Print("String pool is corrupt/invalid.\n");
440 return;
441 }
442
443 SortedVector<const void*> uniqueStrings;
444 const size_t N = pool->size();
445 for (size_t i=0; i<N; i++) {
446 size_t len;
447 if (pool->isUTF8()) {
448 uniqueStrings.add(pool->string8At(i, &len));
449 } else {
450 uniqueStrings.add(pool->stringAt(i, &len));
451 }
452 }
453
454 printer->Print(StringPrintf("String pool of %zd unique %s %s strings, %zd entries and %zd styles "
455 "using %zd bytes:\n", uniqueStrings.size(),
456 pool->isUTF8() ? "UTF-8" : "UTF-16",
457 pool->isSorted() ? "sorted" : "non-sorted", N, pool->styleCount(),
458 pool->bytes()));
459
460 const size_t NS = pool->size();
461 for (size_t s=0; s<NS; s++) {
462 String8 str = pool->string8ObjectAt(s);
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800463 printer->Print(StringPrintf("String #%zd: %s\n", s, str.string()));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700464 }
465}
466
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700467namespace {
468
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700469class XmlPrinter : public xml::ConstVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700471 using xml::ConstVisitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700472
Adam Lesinskida9eba32018-02-13 16:44:10 -0800473 XmlPrinter(Printer* printer) : printer_(printer) {
474 }
475
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700476 void Visit(const xml::Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700477 for (const xml::NamespaceDecl& decl : el->namespace_decls) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800478 printer_->Println(StringPrintf("N: %s=%s (line=%zu)", decl.prefix.c_str(), decl.uri.c_str(),
479 decl.line_number));
480 printer_->Indent();
Adam Lesinski6b372992017-08-09 10:54:23 -0700481 }
482
Adam Lesinskida9eba32018-02-13 16:44:10 -0800483 printer_->Print("E: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 if (!el->namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800485 printer_->Print(el->namespace_uri);
486 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800488 printer_->Println(StringPrintf("%s (line=%zu)", el->name.c_str(), el->line_number));
489 printer_->Indent();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700490
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 for (const xml::Attribute& attr : el->attributes) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800492 printer_->Print("A: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 if (!attr.namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800494 printer_->Print(attr.namespace_uri);
495 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800497 printer_->Print(attr.name);
Adam Lesinski4ca56972017-04-26 21:49:53 -0700498
499 if (attr.compiled_attribute) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800500 printer_->Print("(");
501 printer_->Print(
502 attr.compiled_attribute.value().id.value_or_default(ResourceId(0)).to_string());
503 printer_->Print(")");
Adam Lesinski4ca56972017-04-26 21:49:53 -0700504 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800505 printer_->Print("=");
Shane Farmer6ed40612017-09-06 10:00:07 -0700506 if (attr.compiled_value != nullptr) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800507 attr.compiled_value->PrettyPrint(printer_);
Shane Farmer6ed40612017-09-06 10:00:07 -0700508 } else {
Adam Lesinskibbf42972018-02-14 13:36:09 -0800509 printer_->Print("\"");
Adam Lesinskida9eba32018-02-13 16:44:10 -0800510 printer_->Print(attr.value);
Adam Lesinskibbf42972018-02-14 13:36:09 -0800511 printer_->Print("\"");
512 }
513
514 if (!attr.value.empty()) {
515 printer_->Print(" (Raw: \"");
516 printer_->Print(attr.value);
517 printer_->Print("\")");
Shane Farmer6ed40612017-09-06 10:00:07 -0700518 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800519 printer_->Println();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700520 }
521
Adam Lesinskida9eba32018-02-13 16:44:10 -0800522 printer_->Indent();
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700523 xml::ConstVisitor::Visit(el);
Adam Lesinskida9eba32018-02-13 16:44:10 -0800524 printer_->Undent();
525 printer_->Undent();
526
527 for (size_t i = 0; i < el->namespace_decls.size(); i++) {
528 printer_->Undent();
529 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700531
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700532 void Visit(const xml::Text* text) override {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800533 printer_->Println(StringPrintf("T: '%s'", text->text.c_str()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 }
535
536 private:
Adam Lesinskida9eba32018-02-13 16:44:10 -0800537 Printer* printer_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700538};
539
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700541
Adam Lesinskida9eba32018-02-13 16:44:10 -0800542void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) {
543 XmlPrinter xml_visitor(printer);
544 doc.root->Accept(&xml_visitor);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700545}
Adam Lesinski52364f72016-01-11 13:10:24 -0800546
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547} // namespace aapt