blob: df444ba7b169d6e3e267fe8d95b25dedd6aa1fe4 [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
Winson62ac8b52019-12-04 08:36:48 -080035#include "idmap2/Policies.h"
36
Adam Lesinski93190b72017-11-03 15:20:17 -070037using ::aapt::text::Printer;
38using ::android::StringPiece;
39using ::android::base::StringPrintf;
40
Winson62ac8b52019-12-04 08:36:48 -080041using android::idmap2::policy::kPolicyStringToFlag;
42
43using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
44
Adam Lesinski330edcd2015-05-04 17:40:56 -070045namespace aapt {
46
Adam Lesinski6b372992017-08-09 10:54:23 -070047namespace {
48
Adam Lesinski93190b72017-11-03 15:20:17 -070049class ValueHeadlinePrinter : public ConstValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -070051 using ConstValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052
Adam Lesinski93190b72017-11-03 15:20:17 -070053 explicit ValueHeadlinePrinter(const std::string& package, Printer* printer)
54 : package_(package), printer_(printer) {
55 }
56
Adam Lesinskie59f0d82017-10-13 09:36:53 -070057 void Visit(const Attribute* attr) override {
Adam Lesinski93190b72017-11-03 15:20:17 -070058 printer_->Print("(attr) type=");
59 printer_->Print(attr->MaskString());
60 if (!attr->symbols.empty()) {
61 printer_->Print(StringPrintf(" size=%zd", attr->symbols.size()));
62 }
63 }
64
65 void Visit(const Style* style) override {
66 printer_->Print(StringPrintf("(style) size=%zd", style->entries.size()));
67 if (style->parent) {
68 printer_->Print(" parent=");
69
70 const Reference& parent_ref = style->parent.value();
71 if (parent_ref.name) {
72 if (parent_ref.private_reference) {
73 printer_->Print("*");
74 }
75
76 const ResourceName& parent_name = parent_ref.name.value();
77 if (package_ != parent_name.package) {
78 printer_->Print(parent_name.package);
79 printer_->Print(":");
80 }
81 printer_->Print(to_string(parent_name.type));
82 printer_->Print("/");
83 printer_->Print(parent_name.entry);
84 if (parent_ref.id) {
85 printer_->Print(" (");
86 printer_->Print(parent_ref.id.value().to_string());
87 printer_->Print(")");
88 }
89 } else if (parent_ref.id) {
90 printer_->Print(parent_ref.id.value().to_string());
91 } else {
92 printer_->Print("???");
93 }
94 }
95 }
96
97 void Visit(const Array* array) override {
98 printer_->Print(StringPrintf("(array) size=%zd", array->elements.size()));
99 }
100
101 void Visit(const Plural* plural) override {
102 size_t count = std::count_if(plural->values.begin(), plural->values.end(),
103 [](const std::unique_ptr<Item>& v) { return v != nullptr; });
104 printer_->Print(StringPrintf("(plurals) size=%zd", count));
105 }
106
107 void Visit(const Styleable* styleable) override {
108 printer_->Println(StringPrintf("(styleable) size=%zd", styleable->entries.size()));
109 }
110
111 void VisitItem(const Item* item) override {
112 // Pretty much guaranteed to be one line.
113 if (const Reference* ref = ValueCast<Reference>(item)) {
114 // Special case Reference so that we can print local resources without a package name.
115 ref->PrettyPrint(package_, printer_);
116 } else {
117 item->PrettyPrint(printer_);
118 }
119 }
120
121 private:
122 std::string package_;
123 Printer* printer_;
124};
125
126class ValueBodyPrinter : public ConstValueVisitor {
127 public:
128 using ConstValueVisitor::Visit;
129
130 explicit ValueBodyPrinter(const std::string& package, Printer* printer)
131 : package_(package), printer_(printer) {
132 }
133
134 void Visit(const Attribute* attr) override {
135 constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 if (attr->type_mask & kMask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 for (const auto& symbol : attr->symbols) {
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700138 if (symbol.symbol.name) {
139 printer_->Print(symbol.symbol.name.value().entry);
140
141 if (symbol.symbol.id) {
142 printer_->Print("(");
143 printer_->Print(symbol.symbol.id.value().to_string());
144 printer_->Print(")");
145 }
146 } else if (symbol.symbol.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700147 printer_->Print(symbol.symbol.id.value().to_string());
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700148 } else {
149 printer_->Print("???");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700150 }
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700151
Adam Lesinski93190b72017-11-03 15:20:17 -0700152 printer_->Println(StringPrintf("=0x%08x", symbol.value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700154 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700156
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700157 void Visit(const Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 for (const auto& entry : style->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 if (entry.key.name) {
160 const ResourceName& name = entry.key.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700161 if (!name.package.empty() && name.package != package_) {
162 printer_->Print(name.package);
163 printer_->Print(":");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700164 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700165 printer_->Print(name.entry);
166
167 if (entry.key.id) {
168 printer_->Print("(");
169 printer_->Print(entry.key.id.value().to_string());
170 printer_->Print(")");
171 }
172 } else if (entry.key.id) {
173 printer_->Print(entry.key.id.value().to_string());
174 } else {
175 printer_->Print("???");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 }
177
Adam Lesinski93190b72017-11-03 15:20:17 -0700178 printer_->Print("=");
179 PrintItem(*entry.value);
180 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700181 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700183
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700184 void Visit(const Array* array) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700185 const size_t count = array->elements.size();
186 printer_->Print("[");
Donald Chai42fe2b22019-05-07 20:03:27 -0700187 for (size_t i = 0u; i < count; i++) {
188 if (i != 0u && i % 4u == 0u) {
189 printer_->Println();
190 printer_->Print(" ");
Adam Lesinski93190b72017-11-03 15:20:17 -0700191 }
Donald Chai42fe2b22019-05-07 20:03:27 -0700192 PrintItem(*array->elements[i]);
193 if (i != count - 1) {
194 printer_->Print(", ");
195 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700196 }
Donald Chai42fe2b22019-05-07 20:03:27 -0700197 printer_->Println("]");
Adam Lesinski6b372992017-08-09 10:54:23 -0700198 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700199
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700200 void Visit(const Plural* plural) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700201 constexpr std::array<const char*, Plural::Count> kPluralNames = {
202 {"zero", "one", "two", "few", "many", "other"}};
203
204 for (size_t i = 0; i < Plural::Count; i++) {
205 if (plural->values[i] != nullptr) {
206 printer_->Print(StringPrintf("%s=", kPluralNames[i]));
207 PrintItem(*plural->values[i]);
208 printer_->Println();
209 }
210 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700211 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700212
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700213 void Visit(const Styleable* styleable) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 for (const auto& attr : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 if (attr.name) {
216 const ResourceName& name = attr.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700217 if (!name.package.empty() && name.package != package_) {
218 printer_->Print(name.package);
219 printer_->Print(":");
Adam Lesinski355f2852016-02-13 20:26:45 -0800220 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700221 printer_->Print(name.entry);
222
223 if (attr.id) {
224 printer_->Print("(");
225 printer_->Print(attr.id.value().to_string());
226 printer_->Print(")");
227 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700229
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 if (attr.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700231 printer_->Print(attr.id.value().to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 }
Adam Lesinski5a217b02017-11-16 16:58:02 -0800233 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700234 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 }
236
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700237 void VisitItem(const Item* item) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700238 // Intentionally left empty, we already printed the Items.
Adam Lesinski6b372992017-08-09 10:54:23 -0700239 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700240
241 private:
242 void PrintItem(const Item& item) {
243 if (const Reference* ref = ValueCast<Reference>(&item)) {
244 // Special case Reference so that we can print local resources without a package name.
245 ref->PrettyPrint(package_, printer_);
246 } else {
247 item.PrettyPrint(printer_);
248 }
249 }
250
251 std::string package_;
252 Printer* printer_;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700253};
254
Adam Lesinski6b372992017-08-09 10:54:23 -0700255} // namespace
256
Adam Lesinski93190b72017-11-03 15:20:17 -0700257void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options,
258 Printer* printer) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700259 const auto table_view = table.GetPartitionedView();
260 for (const auto& package : table_view.packages) {
261 ValueHeadlinePrinter headline_printer(package.name, printer);
262 ValueBodyPrinter body_printer(package.name, printer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263
Adam Lesinski93190b72017-11-03 15:20:17 -0700264 printer->Print("Package name=");
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700265 printer->Print(package.name);
266 if (package.id) {
267 printer->Print(StringPrintf(" id=%02x", package.id.value()));
Adam Lesinski93190b72017-11-03 15:20:17 -0700268 }
269 printer->Println();
270
271 printer->Indent();
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700272 for (const auto& type : package.types) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700273 printer->Print("type ");
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700274 printer->Print(to_string(type.type));
275 if (type.id) {
276 printer->Print(StringPrintf(" id=%02x", type.id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700278 printer->Println(StringPrintf(" entryCount=%zd", type.entries.size()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279
Adam Lesinski93190b72017-11-03 15:20:17 -0700280 printer->Indent();
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700281 for (const ResourceTableEntryView& entry : type.entries) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700282 printer->Print("resource ");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700283 printer->Print(ResourceId(package.id.value_or(0), type.id.value_or(0), entry.id.value_or(0))
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700284 .to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -0700285 printer->Print(" ");
286
287 // Write the name without the package (this is obvious and too verbose).
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700288 printer->Print(to_string(type.type));
Adam Lesinski93190b72017-11-03 15:20:17 -0700289 printer->Print("/");
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700290 printer->Print(entry.name);
Adam Lesinski93190b72017-11-03 15:20:17 -0700291
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700292 switch (entry.visibility.level) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800293 case Visibility::Level::kPublic:
Adam Lesinski93190b72017-11-03 15:20:17 -0700294 printer->Print(" PUBLIC");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800296 case Visibility::Level::kPrivate:
Adam Lesinski93190b72017-11-03 15:20:17 -0700297 printer->Print(" _PRIVATE_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800299 case Visibility::Level::kUndefined:
Adam Lesinski93190b72017-11-03 15:20:17 -0700300 // Print nothing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 break;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700302 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700304 if (entry.visibility.staged_api) {
Ryan Mitchell1499f502021-03-30 12:20:08 -0700305 printer->Print(" STAGED");
306 }
307
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700308 if (entry.overlayable_item) {
Mårten Kongstad1d3b64852019-09-17 13:02:32 +0200309 printer->Print(" OVERLAYABLE");
310 }
311
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700312 if (entry.staged_id) {
313 printer->Print(" STAGED_ID=");
314 printer->Print(entry.staged_id.value().id.to_string());
315 }
316
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();
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700321 for (const auto& value : entry.values) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800322 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
Ryan Mitchell4382e442021-07-14 12:53:01 -0700364 std::optional<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()) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000431 uniqueStrings.add(UnpackOptionalString(pool->string8At(i), &len));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700432 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000433 uniqueStrings.add(UnpackOptionalString(pool->stringAt(i), &len));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700434 }
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++) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000445 auto str = pool->string8ObjectAt(s);
446 printer->Print(StringPrintf("String #%zd : %s\n", s, str.has_value() ? 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("(");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700484 printer_->Print(attr.compiled_attribute.value().id.value_or(ResourceId(0)).to_string());
Adam Lesinskida9eba32018-02-13 16:44:10 -0800485 printer_->Print(")");
Adam Lesinski4ca56972017-04-26 21:49:53 -0700486 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800487 printer_->Print("=");
Shane Farmer6ed40612017-09-06 10:00:07 -0700488 if (attr.compiled_value != nullptr) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800489 attr.compiled_value->PrettyPrint(printer_);
Shane Farmer6ed40612017-09-06 10:00:07 -0700490 } else {
Adam Lesinskibbf42972018-02-14 13:36:09 -0800491 printer_->Print("\"");
Adam Lesinskida9eba32018-02-13 16:44:10 -0800492 printer_->Print(attr.value);
Adam Lesinskibbf42972018-02-14 13:36:09 -0800493 printer_->Print("\"");
494 }
495
496 if (!attr.value.empty()) {
497 printer_->Print(" (Raw: \"");
498 printer_->Print(attr.value);
499 printer_->Print("\")");
Shane Farmer6ed40612017-09-06 10:00:07 -0700500 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800501 printer_->Println();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700502 }
503
Adam Lesinskida9eba32018-02-13 16:44:10 -0800504 printer_->Indent();
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700505 xml::ConstVisitor::Visit(el);
Adam Lesinskida9eba32018-02-13 16:44:10 -0800506 printer_->Undent();
507 printer_->Undent();
508
509 for (size_t i = 0; i < el->namespace_decls.size(); i++) {
510 printer_->Undent();
511 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700513
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700514 void Visit(const xml::Text* text) override {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800515 printer_->Println(StringPrintf("T: '%s'", text->text.c_str()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 }
517
518 private:
Adam Lesinskida9eba32018-02-13 16:44:10 -0800519 Printer* printer_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700520};
521
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700523
Adam Lesinskida9eba32018-02-13 16:44:10 -0800524void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) {
525 XmlPrinter xml_visitor(printer);
526 doc.root->Accept(&xml_visitor);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700527}
Adam Lesinski52364f72016-01-11 13:10:24 -0800528
Mårten Kongstad1d3b64852019-09-17 13:02:32 +0200529struct DumpOverlayableEntry {
530 std::string overlayable_section;
531 std::string policy_subsection;
532 std::string resource_name;
533};
534
535void Debug::DumpOverlayable(const ResourceTable& table, text::Printer* printer) {
536 std::vector<DumpOverlayableEntry> items;
537 for (const auto& package : table.packages) {
538 for (const auto& type : package->types) {
539 for (const auto& entry : type->entries) {
540 if (entry->overlayable_item) {
541 const auto& overlayable_item = entry->overlayable_item.value();
542 const auto overlayable_section = StringPrintf(R"(name="%s" actor="%s")",
543 overlayable_item.overlayable->name.c_str(),
544 overlayable_item.overlayable->actor.c_str());
545 const auto policy_subsection = StringPrintf(R"(policies="%s")",
Ryan Mitchella7070132020-05-13 14:17:52 -0700546 android::idmap2::policy::PoliciesToDebugString(overlayable_item.policies).c_str());
Mårten Kongstad1d3b64852019-09-17 13:02:32 +0200547 const auto value =
548 StringPrintf("%s/%s", to_string(type->type).data(), entry->name.c_str());
549 items.push_back(DumpOverlayableEntry{overlayable_section, policy_subsection, value});
550 }
551 }
552 }
553 }
554
555 std::sort(items.begin(), items.end(),
556 [](const DumpOverlayableEntry& a, const DumpOverlayableEntry& b) {
557 if (a.overlayable_section != b.overlayable_section) {
558 return a.overlayable_section < b.overlayable_section;
559 }
560 if (a.policy_subsection != b.policy_subsection) {
561 return a.policy_subsection < b.policy_subsection;
562 }
563 return a.resource_name < b.resource_name;
564 });
565
566 std::string last_overlayable_section;
567 std::string last_policy_subsection;
568 for (const auto& item : items) {
569 if (last_overlayable_section != item.overlayable_section) {
570 printer->Println(item.overlayable_section);
571 last_overlayable_section = item.overlayable_section;
572 }
573 if (last_policy_subsection != item.policy_subsection) {
574 printer->Indent();
575 printer->Println(item.policy_subsection);
576 last_policy_subsection = item.policy_subsection;
577 printer->Undent();
578 }
579 printer->Indent();
580 printer->Indent();
581 printer->Println(item.resource_name);
582 printer->Undent();
583 printer->Undent();
584 }
585}
586
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587} // namespace aapt