blob: 064b4617b0a22a5de425d3aa3daacac591369d56 [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
Ryan Mitchell19b27092018-08-13 11:36:27 -070019#include <androidfw/TypeWrappers.h>
Jeremy Meyer56f36e82022-05-20 20:35:42 +000020#include <androidfw/Util.h>
Ryan Mitchell19b27092018-08-13 11:36:27 -070021#include <format/binary/ResChunkPullParser.h>
22
Adam Lesinski330edcd2015-05-04 17:40:56 -070023#include <algorithm>
Adam Lesinski330edcd2015-05-04 17:40:56 -070024#include <map>
25#include <memory>
Adam Lesinskid13fb242015-05-12 20:40:48 -070026#include <queue>
Adam Lesinski330edcd2015-05-04 17:40:56 -070027#include <set>
28#include <vector>
29
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030#include "ResourceTable.h"
Ryan Mitchell19b27092018-08-13 11:36:27 -070031#include "ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032#include "ResourceValues.h"
33#include "ValueVisitor.h"
Ryan Mitchell19b27092018-08-13 11:36:27 -070034#include "android-base/logging.h"
35#include "android-base/stringprintf.h"
felkachanga1da2772022-08-29 17:54:09 +080036#include "androidfw/ResourceTypes.h"
Ryan Mitchell19b27092018-08-13 11:36:27 -070037#include "idmap2/Policies.h"
Adam Lesinski93190b72017-11-03 15:20:17 -070038#include "text/Printer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039#include "util/Util.h"
40
Adam Lesinski93190b72017-11-03 15:20:17 -070041using ::aapt::text::Printer;
42using ::android::StringPiece;
43using ::android::base::StringPrintf;
44
Winson62ac8b52019-12-04 08:36:48 -080045using android::idmap2::policy::kPolicyStringToFlag;
46
47using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
48
Adam Lesinski330edcd2015-05-04 17:40:56 -070049namespace aapt {
50
Adam Lesinski6b372992017-08-09 10:54:23 -070051namespace {
52
Adam Lesinski93190b72017-11-03 15:20:17 -070053class ValueHeadlinePrinter : public ConstValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -070055 using ConstValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinski93190b72017-11-03 15:20:17 -070057 explicit ValueHeadlinePrinter(const std::string& package, Printer* printer)
58 : package_(package), printer_(printer) {
59 }
60
Adam Lesinskie59f0d82017-10-13 09:36:53 -070061 void Visit(const Attribute* attr) override {
Adam Lesinski93190b72017-11-03 15:20:17 -070062 printer_->Print("(attr) type=");
63 printer_->Print(attr->MaskString());
64 if (!attr->symbols.empty()) {
65 printer_->Print(StringPrintf(" size=%zd", attr->symbols.size()));
66 }
67 }
68
69 void Visit(const Style* style) override {
70 printer_->Print(StringPrintf("(style) size=%zd", style->entries.size()));
71 if (style->parent) {
72 printer_->Print(" parent=");
73
74 const Reference& parent_ref = style->parent.value();
75 if (parent_ref.name) {
76 if (parent_ref.private_reference) {
77 printer_->Print("*");
78 }
79
80 const ResourceName& parent_name = parent_ref.name.value();
81 if (package_ != parent_name.package) {
82 printer_->Print(parent_name.package);
83 printer_->Print(":");
84 }
Iurii Makhnocff10ce2022-02-15 19:33:50 +000085 printer_->Print(parent_name.type.to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -070086 printer_->Print("/");
87 printer_->Print(parent_name.entry);
88 if (parent_ref.id) {
89 printer_->Print(" (");
90 printer_->Print(parent_ref.id.value().to_string());
91 printer_->Print(")");
92 }
93 } else if (parent_ref.id) {
94 printer_->Print(parent_ref.id.value().to_string());
95 } else {
96 printer_->Print("???");
97 }
98 }
99 }
100
101 void Visit(const Array* array) override {
102 printer_->Print(StringPrintf("(array) size=%zd", array->elements.size()));
103 }
104
105 void Visit(const Plural* plural) override {
106 size_t count = std::count_if(plural->values.begin(), plural->values.end(),
107 [](const std::unique_ptr<Item>& v) { return v != nullptr; });
108 printer_->Print(StringPrintf("(plurals) size=%zd", count));
109 }
110
111 void Visit(const Styleable* styleable) override {
112 printer_->Println(StringPrintf("(styleable) size=%zd", styleable->entries.size()));
113 }
114
115 void VisitItem(const Item* item) override {
116 // Pretty much guaranteed to be one line.
117 if (const Reference* ref = ValueCast<Reference>(item)) {
118 // Special case Reference so that we can print local resources without a package name.
119 ref->PrettyPrint(package_, printer_);
120 } else {
121 item->PrettyPrint(printer_);
122 }
123 }
124
125 private:
126 std::string package_;
127 Printer* printer_;
128};
129
130class ValueBodyPrinter : public ConstValueVisitor {
131 public:
132 using ConstValueVisitor::Visit;
133
134 explicit ValueBodyPrinter(const std::string& package, Printer* printer)
135 : package_(package), printer_(printer) {
136 }
137
138 void Visit(const Attribute* attr) override {
139 constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 if (attr->type_mask & kMask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 for (const auto& symbol : attr->symbols) {
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700142 if (symbol.symbol.name) {
143 printer_->Print(symbol.symbol.name.value().entry);
144
145 if (symbol.symbol.id) {
146 printer_->Print("(");
147 printer_->Print(symbol.symbol.id.value().to_string());
148 printer_->Print(")");
149 }
150 } else if (symbol.symbol.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700151 printer_->Print(symbol.symbol.id.value().to_string());
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700152 } else {
153 printer_->Print("???");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700154 }
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700155
Adam Lesinski93190b72017-11-03 15:20:17 -0700156 printer_->Println(StringPrintf("=0x%08x", symbol.value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700158 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700160
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700161 void Visit(const Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 for (const auto& entry : style->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 if (entry.key.name) {
164 const ResourceName& name = entry.key.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700165 if (!name.package.empty() && name.package != package_) {
166 printer_->Print(name.package);
167 printer_->Print(":");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700168 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700169 printer_->Print(name.entry);
170
171 if (entry.key.id) {
172 printer_->Print("(");
173 printer_->Print(entry.key.id.value().to_string());
174 printer_->Print(")");
175 }
176 } else if (entry.key.id) {
177 printer_->Print(entry.key.id.value().to_string());
178 } else {
179 printer_->Print("???");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
181
Adam Lesinski93190b72017-11-03 15:20:17 -0700182 printer_->Print("=");
183 PrintItem(*entry.value);
184 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700185 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700187
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700188 void Visit(const Array* array) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700189 const size_t count = array->elements.size();
190 printer_->Print("[");
Donald Chai42fe2b22019-05-07 20:03:27 -0700191 for (size_t i = 0u; i < count; i++) {
192 if (i != 0u && i % 4u == 0u) {
193 printer_->Println();
194 printer_->Print(" ");
Adam Lesinski93190b72017-11-03 15:20:17 -0700195 }
Donald Chai42fe2b22019-05-07 20:03:27 -0700196 PrintItem(*array->elements[i]);
197 if (i != count - 1) {
198 printer_->Print(", ");
199 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700200 }
Donald Chai42fe2b22019-05-07 20:03:27 -0700201 printer_->Println("]");
Adam Lesinski6b372992017-08-09 10:54:23 -0700202 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700203
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700204 void Visit(const Plural* plural) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700205 constexpr std::array<const char*, Plural::Count> kPluralNames = {
206 {"zero", "one", "two", "few", "many", "other"}};
207
208 for (size_t i = 0; i < Plural::Count; i++) {
209 if (plural->values[i] != nullptr) {
210 printer_->Print(StringPrintf("%s=", kPluralNames[i]));
211 PrintItem(*plural->values[i]);
212 printer_->Println();
213 }
214 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700215 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700216
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700217 void Visit(const Styleable* styleable) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 for (const auto& attr : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 if (attr.name) {
220 const ResourceName& name = attr.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700221 if (!name.package.empty() && name.package != package_) {
222 printer_->Print(name.package);
223 printer_->Print(":");
Adam Lesinski355f2852016-02-13 20:26:45 -0800224 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700225 printer_->Print(name.entry);
226
227 if (attr.id) {
228 printer_->Print("(");
229 printer_->Print(attr.id.value().to_string());
230 printer_->Print(")");
231 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 if (attr.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700235 printer_->Print(attr.id.value().to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 }
Adam Lesinski5a217b02017-11-16 16:58:02 -0800237 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700238 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 }
240
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700241 void VisitItem(const Item* item) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700242 // Intentionally left empty, we already printed the Items.
Adam Lesinski6b372992017-08-09 10:54:23 -0700243 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700244
245 private:
246 void PrintItem(const Item& item) {
247 if (const Reference* ref = ValueCast<Reference>(&item)) {
248 // Special case Reference so that we can print local resources without a package name.
249 ref->PrettyPrint(package_, printer_);
250 } else {
251 item.PrettyPrint(printer_);
252 }
253 }
254
255 std::string package_;
256 Printer* printer_;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700257};
258
Adam Lesinski6b372992017-08-09 10:54:23 -0700259} // namespace
260
Adam Lesinski93190b72017-11-03 15:20:17 -0700261void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options,
262 Printer* printer) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700263 const auto table_view = table.GetPartitionedView();
264 for (const auto& package : table_view.packages) {
265 ValueHeadlinePrinter headline_printer(package.name, printer);
266 ValueBodyPrinter body_printer(package.name, printer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267
Yurii Zubrytskyicf91ab82023-04-24 18:34:13 -0700268 auto& dynamicRefTable = table.GetReferencedPackages();
269 if (!dynamicRefTable.empty()) {
270 printer->Println(StringPrintf("DynamicRefTable entryCount=%d", int(dynamicRefTable.size())));
271 printer->Indent();
272 for (auto&& [id, name] : dynamicRefTable) {
273 printer->Println(StringPrintf("0x%02x -> %s", id, name.c_str()));
274 }
275 printer->Undent();
276 }
277
Adam Lesinski93190b72017-11-03 15:20:17 -0700278 printer->Print("Package name=");
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700279 printer->Print(package.name);
280 if (package.id) {
281 printer->Print(StringPrintf(" id=%02x", package.id.value()));
Adam Lesinski93190b72017-11-03 15:20:17 -0700282 }
283 printer->Println();
284
285 printer->Indent();
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700286 for (const auto& type : package.types) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700287 printer->Print("type ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000288 printer->Print(type.named_type.to_string());
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700289 if (type.id) {
290 printer->Print(StringPrintf(" id=%02x", type.id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700292 printer->Println(StringPrintf(" entryCount=%zd", type.entries.size()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293
Adam Lesinski93190b72017-11-03 15:20:17 -0700294 printer->Indent();
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700295 for (const ResourceTableEntryView& entry : type.entries) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700296 printer->Print("resource ");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700297 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 -0700298 .to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -0700299 printer->Print(" ");
300
301 // Write the name without the package (this is obvious and too verbose).
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000302 printer->Print(type.named_type.to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -0700303 printer->Print("/");
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700304 printer->Print(entry.name);
Adam Lesinski93190b72017-11-03 15:20:17 -0700305
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700306 switch (entry.visibility.level) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800307 case Visibility::Level::kPublic:
Adam Lesinski93190b72017-11-03 15:20:17 -0700308 printer->Print(" PUBLIC");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800310 case Visibility::Level::kPrivate:
Adam Lesinski93190b72017-11-03 15:20:17 -0700311 printer->Print(" _PRIVATE_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800313 case Visibility::Level::kUndefined:
Adam Lesinski93190b72017-11-03 15:20:17 -0700314 // Print nothing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 break;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700316 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700318 if (entry.visibility.staged_api) {
Ryan Mitchell1499f502021-03-30 12:20:08 -0700319 printer->Print(" STAGED");
320 }
321
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700322 if (entry.overlayable_item) {
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200323 printer->Print(" OVERLAYABLE");
324 }
325
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700326 if (entry.staged_id) {
327 printer->Print(" STAGED_ID=");
328 printer->Print(entry.staged_id.value().id.to_string());
329 }
330
Adam Lesinski93190b72017-11-03 15:20:17 -0700331 printer->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700332
Adam Lesinski71be7052017-12-12 16:48:07 -0800333 if (options.show_values) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700334 printer->Indent();
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700335 for (const auto& value : entry.values) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800336 printer->Print("(");
337 printer->Print(value->config.to_string());
338 printer->Print(") ");
339 value->value->Accept(&headline_printer);
340 if (options.show_sources && !value->value->GetSource().path.empty()) {
341 printer->Print(" src=");
342 printer->Print(value->value->GetSource().to_string());
343 }
344 printer->Println();
345 printer->Indent();
346 value->value->Accept(&body_printer);
347 printer->Undent();
348 }
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700349 printer->Println("Flag disabled values:");
350 for (const auto& value : entry.flag_disabled_values) {
351 printer->Print("(");
352 printer->Print(value->config.to_string());
353 printer->Print(") ");
354 value->value->Accept(&headline_printer);
355 if (options.show_sources && !value->value->GetSource().path.empty()) {
356 printer->Print(" src=");
357 printer->Print(value->value->GetSource().to_string());
358 }
359 printer->Println();
360 printer->Indent();
361 value->value->Accept(&body_printer);
362 printer->Undent();
363 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700364 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700365 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700367 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700368 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700369 printer->Undent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700371}
372
Adam Lesinski6b372992017-08-09 10:54:23 -0700373static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 auto iter = std::lower_bound(names.begin(), names.end(), name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 CHECK(iter != names.end());
376 CHECK(*iter == name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 return std::distance(names.begin(), iter);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700378}
379
Adam Lesinski6b372992017-08-09 10:54:23 -0700380void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 std::map<ResourceName, std::set<ResourceName>> graph;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700382
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 std::queue<ResourceName> styles_to_visit;
384 styles_to_visit.push(target_style);
385 for (; !styles_to_visit.empty(); styles_to_visit.pop()) {
386 const ResourceName& style_name = styles_to_visit.front();
387 std::set<ResourceName>& parents = graph[style_name];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 if (!parents.empty()) {
389 // We've already visited this style.
390 continue;
391 }
392
Ryan Mitchell4382e442021-07-14 12:53:01 -0700393 std::optional<ResourceTable::SearchResult> result = table->FindResource(style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 if (result) {
395 ResourceEntry* entry = result.value().entry;
396 for (const auto& value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 if (Style* style = ValueCast<Style>(value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 if (style->parent && style->parent.value().name) {
399 parents.insert(style->parent.value().name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 styles_to_visit.push(style->parent.value().name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 }
Adam Lesinskid13fb242015-05-12 20:40:48 -0700402 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700404 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700406
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 std::vector<ResourceName> names;
408 for (const auto& entry : graph) {
409 names.push_back(entry.first);
410 }
411
412 std::cout << "digraph styles {\n";
413 for (const auto& name : names) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700414 std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 }
416
417 for (const auto& entry : graph) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 const ResourceName& style_name = entry.first;
419 size_t style_node_index = GetNodeIndex(names, style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 for (const auto& parent_name : entry.second) {
422 std::cout << " node_" << style_node_index << " -> "
423 << "node_" << GetNodeIndex(names, parent_name) << ";\n";
Adam Lesinskid13fb242015-05-12 20:40:48 -0700424 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700426
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 std::cout << "}" << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700428}
429
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430void Debug::DumpHex(const void* data, size_t len) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 const uint8_t* d = (const uint8_t*)data;
432 for (size_t i = 0; i < len; i++) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700433 std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " ";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 if (i % 8 == 7) {
435 std::cerr << "\n";
Adam Lesinski52364f72016-01-11 13:10:24 -0800436 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800438
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 if (len - 1 % 8 != 7) {
440 std::cerr << std::endl;
441 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800442}
443
Ryan Mitchell5d275512018-07-19 14:29:00 -0700444void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) {
445 using namespace android;
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800446
Ryan Mitchell5d275512018-07-19 14:29:00 -0700447 if (pool->getError() == NO_INIT) {
448 printer->Print("String pool is unitialized.\n");
449 return;
450 } else if (pool->getError() != NO_ERROR) {
451 printer->Print("String pool is corrupt/invalid.\n");
452 return;
453 }
454
455 SortedVector<const void*> uniqueStrings;
456 const size_t N = pool->size();
457 for (size_t i=0; i<N; i++) {
458 size_t len;
459 if (pool->isUTF8()) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000460 uniqueStrings.add(UnpackOptionalString(pool->string8At(i), &len));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700461 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000462 uniqueStrings.add(UnpackOptionalString(pool->stringAt(i), &len));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700463 }
464 }
465
466 printer->Print(StringPrintf("String pool of %zd unique %s %s strings, %zd entries and %zd styles "
467 "using %zd bytes:\n", uniqueStrings.size(),
468 pool->isUTF8() ? "UTF-8" : "UTF-16",
469 pool->isSorted() ? "sorted" : "non-sorted", N, pool->styleCount(),
470 pool->bytes()));
471
472 const size_t NS = pool->size();
473 for (size_t s=0; s<NS; s++) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000474 auto str = pool->string8ObjectAt(s);
Tomasz Wasilczykade06312023-08-10 23:54:44 +0000475 printer->Print(StringPrintf("String #%zd : %s\n", s, str.has_value() ? str->c_str() : ""));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700476 }
477}
478
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700479namespace {
480
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700481class XmlPrinter : public xml::ConstVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700483 using xml::ConstVisitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700484
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800485 explicit XmlPrinter(Printer* printer) : printer_(printer) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800486 }
487
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700488 void Visit(const xml::Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700489 for (const xml::NamespaceDecl& decl : el->namespace_decls) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800490 printer_->Println(StringPrintf("N: %s=%s (line=%zu)", decl.prefix.c_str(), decl.uri.c_str(),
491 decl.line_number));
492 printer_->Indent();
Adam Lesinski6b372992017-08-09 10:54:23 -0700493 }
494
Adam Lesinskida9eba32018-02-13 16:44:10 -0800495 printer_->Print("E: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (!el->namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800497 printer_->Print(el->namespace_uri);
498 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800500 printer_->Println(StringPrintf("%s (line=%zu)", el->name.c_str(), el->line_number));
501 printer_->Indent();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700502
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 for (const xml::Attribute& attr : el->attributes) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800504 printer_->Print("A: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 if (!attr.namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800506 printer_->Print(attr.namespace_uri);
507 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800509 printer_->Print(attr.name);
Adam Lesinski4ca56972017-04-26 21:49:53 -0700510
511 if (attr.compiled_attribute) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800512 printer_->Print("(");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700513 printer_->Print(attr.compiled_attribute.value().id.value_or(ResourceId(0)).to_string());
Adam Lesinskida9eba32018-02-13 16:44:10 -0800514 printer_->Print(")");
Adam Lesinski4ca56972017-04-26 21:49:53 -0700515 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800516 printer_->Print("=");
Shane Farmer6ed40612017-09-06 10:00:07 -0700517 if (attr.compiled_value != nullptr) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800518 attr.compiled_value->PrettyPrint(printer_);
Shane Farmer6ed40612017-09-06 10:00:07 -0700519 } else {
Adam Lesinskibbf42972018-02-14 13:36:09 -0800520 printer_->Print("\"");
Adam Lesinskida9eba32018-02-13 16:44:10 -0800521 printer_->Print(attr.value);
Adam Lesinskibbf42972018-02-14 13:36:09 -0800522 printer_->Print("\"");
523 }
524
525 if (!attr.value.empty()) {
526 printer_->Print(" (Raw: \"");
527 printer_->Print(attr.value);
528 printer_->Print("\")");
Shane Farmer6ed40612017-09-06 10:00:07 -0700529 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800530 printer_->Println();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700531 }
532
Adam Lesinskida9eba32018-02-13 16:44:10 -0800533 printer_->Indent();
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700534 xml::ConstVisitor::Visit(el);
Adam Lesinskida9eba32018-02-13 16:44:10 -0800535 printer_->Undent();
536 printer_->Undent();
537
538 for (size_t i = 0; i < el->namespace_decls.size(); i++) {
539 printer_->Undent();
540 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700542
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700543 void Visit(const xml::Text* text) override {
felkachanga1da2772022-08-29 17:54:09 +0800544 printer_->Println(
545 StringPrintf("T: '%s'", android::ResTable::normalizeForOutput(text->text.c_str()).c_str()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 }
547
548 private:
Adam Lesinskida9eba32018-02-13 16:44:10 -0800549 Printer* printer_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700550};
551
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700553
Adam Lesinskida9eba32018-02-13 16:44:10 -0800554void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) {
555 XmlPrinter xml_visitor(printer);
556 doc.root->Accept(&xml_visitor);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700557}
Adam Lesinski52364f72016-01-11 13:10:24 -0800558
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200559struct DumpOverlayableEntry {
560 std::string overlayable_section;
561 std::string policy_subsection;
562 std::string resource_name;
563};
564
565void Debug::DumpOverlayable(const ResourceTable& table, text::Printer* printer) {
566 std::vector<DumpOverlayableEntry> items;
567 for (const auto& package : table.packages) {
568 for (const auto& type : package->types) {
569 for (const auto& entry : type->entries) {
570 if (entry->overlayable_item) {
571 const auto& overlayable_item = entry->overlayable_item.value();
572 const auto overlayable_section = StringPrintf(R"(name="%s" actor="%s")",
573 overlayable_item.overlayable->name.c_str(),
574 overlayable_item.overlayable->actor.c_str());
575 const auto policy_subsection = StringPrintf(R"(policies="%s")",
Ryan Mitchella7070132020-05-13 14:17:52 -0700576 android::idmap2::policy::PoliciesToDebugString(overlayable_item.policies).c_str());
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200577 const auto value =
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000578 StringPrintf("%s/%s", type->named_type.to_string().data(), entry->name.c_str());
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200579 items.push_back(DumpOverlayableEntry{overlayable_section, policy_subsection, value});
580 }
581 }
582 }
583 }
584
585 std::sort(items.begin(), items.end(),
586 [](const DumpOverlayableEntry& a, const DumpOverlayableEntry& b) {
587 if (a.overlayable_section != b.overlayable_section) {
588 return a.overlayable_section < b.overlayable_section;
589 }
590 if (a.policy_subsection != b.policy_subsection) {
591 return a.policy_subsection < b.policy_subsection;
592 }
593 return a.resource_name < b.resource_name;
594 });
595
596 std::string last_overlayable_section;
597 std::string last_policy_subsection;
598 for (const auto& item : items) {
599 if (last_overlayable_section != item.overlayable_section) {
600 printer->Println(item.overlayable_section);
601 last_overlayable_section = item.overlayable_section;
602 }
603 if (last_policy_subsection != item.policy_subsection) {
604 printer->Indent();
605 printer->Println(item.policy_subsection);
606 last_policy_subsection = item.policy_subsection;
607 printer->Undent();
608 }
609 printer->Indent();
610 printer->Indent();
611 printer->Println(item.resource_name);
612 printer->Undent();
613 printer->Undent();
614 }
615}
616
Ryan Mitchell19b27092018-08-13 11:36:27 -0700617namespace {
618
619using namespace android;
620
621class ChunkPrinter {
622 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000623 ChunkPrinter(const void* data, size_t len, Printer* printer, android::IDiagnostics* diag)
Ryan Mitchell19b27092018-08-13 11:36:27 -0700624 : data_(data), data_len_(len), printer_(printer), diag_(diag) {
625 }
626
627 void PrintChunkHeader(const ResChunk_header* chunk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000628 switch (android::util::DeviceToHost16(chunk->type)) {
Ryan Mitchell19b27092018-08-13 11:36:27 -0700629 case RES_STRING_POOL_TYPE:
630 printer_->Print("[RES_STRING_POOL_TYPE]");
631 break;
632 case RES_TABLE_LIBRARY_TYPE:
633 printer_->Print("[RES_TABLE_LIBRARY_TYPE]");
634 break;
635 case RES_TABLE_TYPE:
636 printer_->Print("[ResTable_header]");
637 break;
638 case RES_TABLE_PACKAGE_TYPE:
639 printer_->Print("[ResTable_package]");
640 break;
641 case RES_TABLE_TYPE_TYPE:
642 printer_->Print("[ResTable_type]");
643 break;
644 case RES_TABLE_TYPE_SPEC_TYPE:
645 printer_->Print("[RES_TABLE_TYPE_SPEC_TYPE]");
646 break;
647 default:
648 break;
649 }
650
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000651 printer_->Print(StringPrintf(" chunkSize: %u", android::util::DeviceToHost32(chunk->size)));
652 printer_->Print(
653 StringPrintf(" headerSize: %u", android::util::DeviceToHost32(chunk->headerSize)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700654 }
655
656 bool PrintTable(const ResTable_header* chunk) {
657 printer_->Print(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000658 StringPrintf(" Package count: %u\n", android::util::DeviceToHost32(chunk->packageCount)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700659
660 // Print the chunks contained within the table
661 printer_->Indent();
662 bool success = PrintChunk(
663 ResChunkPullParser(GetChunkData(&chunk->header), GetChunkDataLen(&chunk->header)));
664 printer_->Undent();
665 return success;
666 }
667
668 void PrintResValue(const Res_value* value, const ConfigDescription& config,
669 const ResourceType* type) {
670 printer_->Print("[Res_value]");
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000671 printer_->Print(StringPrintf(" size: %u", android::util::DeviceToHost32(value->size)));
672 printer_->Print(
673 StringPrintf(" dataType: 0x%02x", android::util::DeviceToHost32(value->dataType)));
674 printer_->Print(StringPrintf(" data: 0x%08x", android::util::DeviceToHost32(value->data)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700675
676 if (type) {
677 auto item =
678 ResourceUtils::ParseBinaryResValue(*type, config, value_pool_, *value, &out_pool_);
679 printer_->Print(" (");
680 item->PrettyPrint(printer_);
681 printer_->Print(")");
682 }
683
684 printer_->Print("\n");
685 }
686
687 bool PrintTableType(const ResTable_type* chunk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000688 printer_->Print(StringPrintf(" id: 0x%02x", android::util::DeviceToHost32(chunk->id)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700689 printer_->Print(StringPrintf(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000690 " name: %s",
691 android::util::GetString(type_pool_, android::util::DeviceToHost32(chunk->id) - 1)
692 .c_str()));
693 printer_->Print(StringPrintf(" flags: 0x%02x", android::util::DeviceToHost32(chunk->flags)));
694 printer_->Print(
695 StringPrintf(" entryCount: %u", android::util::DeviceToHost32(chunk->entryCount)));
696 printer_->Print(
697 StringPrintf(" entryStart: %u", android::util::DeviceToHost32(chunk->entriesStart)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700698
699 ConfigDescription config;
700 config.copyFromDtoH(chunk->config);
701 printer_->Print(StringPrintf(" config: %s\n", config.to_string().c_str()));
702
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000703 const ResourceType* type = ParseResourceType(
704 android::util::GetString(type_pool_, android::util::DeviceToHost32(chunk->id) - 1));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700705
706 printer_->Indent();
707
708 TypeVariant tv(chunk);
709 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
710 const ResTable_entry* entry = *it;
711 if (!entry) {
712 continue;
713 }
714
Eric Miao368cd192022-09-09 15:46:14 -0700715 if (entry->is_complex()) {
716 printer_->Print("[ResTable_map_entry]");
717 } else if (entry->is_compact()) {
718 printer_->Print("[ResTable_entry_compact]");
719 } else {
720 printer_->Print("[ResTable_entry]");
721 }
722
Ryan Mitchell19b27092018-08-13 11:36:27 -0700723 printer_->Print(StringPrintf(" id: 0x%04x", it.index()));
724 printer_->Print(StringPrintf(
Eric Miao368cd192022-09-09 15:46:14 -0700725 " name: %s", android::util::GetString(key_pool_, entry->key()).c_str()));
726 printer_->Print(StringPrintf(" keyIndex: %u", entry->key()));
727 printer_->Print(StringPrintf(" size: %zu", entry->size()));
728 printer_->Print(StringPrintf(" flags: 0x%04x", entry->flags()));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700729
730 printer_->Indent();
731
Eric Miao368cd192022-09-09 15:46:14 -0700732 if (auto map_entry = entry->map_entry()) {
733 uint32_t map_entry_count = android::util::DeviceToHost32(map_entry->count);
734 printer_->Print(StringPrintf(" count: 0x%04x", map_entry_count));
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000735 printer_->Print(StringPrintf(" parent: 0x%08x\n",
736 android::util::DeviceToHost32(map_entry->parent.ident)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700737
738 // Print the name and value mappings
Eric Miao368cd192022-09-09 15:46:14 -0700739 auto maps = (const ResTable_map*)((const uint8_t*)entry + entry->size());
740 for (size_t i = 0; i < map_entry_count; i++) {
Ryan Mitchell19b27092018-08-13 11:36:27 -0700741 PrintResValue(&(maps[i].value), config, type);
742
743 printer_->Print(StringPrintf(
744 " name: %s name-id:%d\n",
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000745 android::util::GetString(key_pool_, android::util::DeviceToHost32(maps[i].name.ident))
746 .c_str(),
747 android::util::DeviceToHost32(maps[i].name.ident)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700748 }
749 } else {
750 printer_->Print("\n");
751
752 // Print the value of the entry
Eric Miao368cd192022-09-09 15:46:14 -0700753 Res_value value = entry->value();
754 PrintResValue(&value, config, type);
Ryan Mitchell19b27092018-08-13 11:36:27 -0700755 }
756
757 printer_->Undent();
758 }
759
760 printer_->Undent();
761 return true;
762 }
763
764 void PrintStringPool(const ResStringPool_header* chunk) {
765 // Initialize the string pools
766
767 ResStringPool* pool;
768 if (value_pool_.getError() == NO_INIT) {
769 pool = &value_pool_;
770 } else if (type_pool_.getError() == NO_INIT) {
771 pool = &type_pool_;
772 } else if (key_pool_.getError() == NO_INIT) {
773 pool = &key_pool_;
774 } else {
775 return;
776 }
777
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000778 pool->setTo(chunk, android::util::DeviceToHost32(
779 (reinterpret_cast<const ResChunk_header*>(chunk))->size));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700780
Yurii Zubrytskyi97f0f1e2024-07-02 15:31:01 -0700781 printer_->Print(StringPrintf(" strings: %zd styles %zd flags: %s|%s\n", pool->size(),
782 pool->styleCount(), pool->isUTF8() ? "UTF-8" : "UTF-16",
783 pool->isSorted() ? "SORTED" : "NON-SORTED"));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700784
785 for (size_t i = 0; i < pool->size(); i++) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000786 printer_->Print(StringPrintf("#%zd : %s\n", i, android::util::GetString(*pool, i).c_str()));
Yurii Zubrytskyi97f0f1e2024-07-02 15:31:01 -0700787 if (i < pool->styleCount()) {
788 printer_->Print(" [Style] ");
789 auto maybe_style = pool->styleAt(i);
790 if (!maybe_style) {
791 printer_->Print("??? missing\n");
792 } else {
793 std::vector<const ResStringPool_span*> spans;
794 for (auto style = maybe_style.value().unsafe_ptr();
795 style->name.index != android::ResStringPool_span::END; ++style) {
796 spans.push_back(style);
797 }
798 printer_->Print(StringPrintf("(%zd)", spans.size()));
799 if (!spans.empty()) {
800 printer_->Print(" :");
801 for (const auto& span : spans) {
802 printer_->Print(StringPrintf(
803 " %s:%u,%u", android::util::GetString(*pool, span->name.index).c_str(),
804 span->firstChar, span->lastChar));
805 }
806 printer_->Print("\n");
807 }
808 }
809 }
Ryan Mitchell19b27092018-08-13 11:36:27 -0700810 }
811 }
812
813 bool PrintPackage(const ResTable_package* chunk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000814 printer_->Print(StringPrintf(" id: 0x%02x", android::util::DeviceToHost32(chunk->id)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700815
816 size_t len = strnlen16((const char16_t*)chunk->name, std::size(chunk->name));
817 std::u16string package_name(len, u'\0');
818 package_name.resize(len);
819 for (size_t i = 0; i < len; i++) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000820 package_name[i] = android::util::DeviceToHost16(chunk->name[i]);
Ryan Mitchell19b27092018-08-13 11:36:27 -0700821 }
822
823 printer_->Print(StringPrintf("name: %s", String8(package_name.c_str()).c_str()));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700824 printer_->Print(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000825 StringPrintf(" typeStrings: %u", android::util::DeviceToHost32(chunk->typeStrings)));
826 printer_->Print(
827 StringPrintf(" lastPublicType: %u", android::util::DeviceToHost32(chunk->lastPublicType)));
828 printer_->Print(
829 StringPrintf(" keyStrings: %u", android::util::DeviceToHost32(chunk->keyStrings)));
830 printer_->Print(
831 StringPrintf(" lastPublicKey: %u", android::util::DeviceToHost32(chunk->lastPublicKey)));
832 printer_->Print(
833 StringPrintf(" typeIdOffset: %u\n", android::util::DeviceToHost32(chunk->typeIdOffset)));
Ryan Mitchell19b27092018-08-13 11:36:27 -0700834
835 // Print the chunks contained within the table
836 printer_->Indent();
837 bool success = PrintChunk(
838 ResChunkPullParser(GetChunkData(&chunk->header), GetChunkDataLen(&chunk->header)));
839 printer_->Undent();
840 return success;
841 }
842
843 bool PrintChunk(ResChunkPullParser&& parser) {
844 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
845 auto chunk = parser.chunk();
846 PrintChunkHeader(chunk);
847
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000848 switch (android::util::DeviceToHost16(chunk->type)) {
Ryan Mitchell19b27092018-08-13 11:36:27 -0700849 case RES_STRING_POOL_TYPE:
850 PrintStringPool(reinterpret_cast<const ResStringPool_header*>(chunk));
851 break;
852
853 case RES_TABLE_TYPE:
854 PrintTable(reinterpret_cast<const ResTable_header*>(chunk));
855 break;
856
857 case RES_TABLE_PACKAGE_TYPE:
858 type_pool_.uninit();
859 key_pool_.uninit();
860 PrintPackage(reinterpret_cast<const ResTable_package*>(chunk));
861 break;
862
863 case RES_TABLE_TYPE_TYPE:
864 PrintTableType(reinterpret_cast<const ResTable_type*>(chunk));
865 break;
866
867 default:
868 printer_->Print("\n");
869 break;
870 }
871 }
872
873 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000874 diag_->Error(android::DiagMessage(source_) << "corrupt resource table: " << parser.error());
Ryan Mitchell19b27092018-08-13 11:36:27 -0700875 return false;
876 }
877
878 return true;
879 }
880
881 void Print() {
882 PrintChunk(ResChunkPullParser(data_, data_len_));
883 printer_->Print("[End]\n");
884 }
885
886 private:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000887 const android::Source source_;
Ryan Mitchell19b27092018-08-13 11:36:27 -0700888 const void* data_;
889 const size_t data_len_;
890 Printer* printer_;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000891 android::IDiagnostics* diag_;
Ryan Mitchell19b27092018-08-13 11:36:27 -0700892
893 // The standard value string pool for resource values.
894 ResStringPool value_pool_;
895
896 // The string pool that holds the names of the types defined
897 // in this table.
898 ResStringPool type_pool_;
899
900 // The string pool that holds the names of the entries defined
901 // in this table.
902 ResStringPool key_pool_;
903
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000904 android::StringPool out_pool_;
Ryan Mitchell19b27092018-08-13 11:36:27 -0700905};
906
907} // namespace
908
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000909void Debug::DumpChunks(const void* data, size_t len, Printer* printer,
910 android::IDiagnostics* diag) {
Ryan Mitchell19b27092018-08-13 11:36:27 -0700911 ChunkPrinter chunk_printer(data, len, printer, diag);
912 chunk_printer.Print();
913}
914
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915} // namespace aapt