blob: 41896f622228aeac381ad9c53199dd17e375e767 [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>
20#include <format/binary/ResChunkPullParser.h>
21
Adam Lesinski330edcd2015-05-04 17:40:56 -070022#include <algorithm>
Adam Lesinski330edcd2015-05-04 17:40:56 -070023#include <map>
24#include <memory>
Adam Lesinskid13fb242015-05-12 20:40:48 -070025#include <queue>
Adam Lesinski330edcd2015-05-04 17:40:56 -070026#include <set>
27#include <vector>
28
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "ResourceTable.h"
Ryan Mitchell19b27092018-08-13 11:36:27 -070030#include "ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031#include "ResourceValues.h"
32#include "ValueVisitor.h"
Ryan Mitchell19b27092018-08-13 11:36:27 -070033#include "android-base/logging.h"
34#include "android-base/stringprintf.h"
35#include "idmap2/Policies.h"
Adam Lesinski93190b72017-11-03 15:20:17 -070036#include "text/Printer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037#include "util/Util.h"
38
Adam Lesinski93190b72017-11-03 15:20:17 -070039using ::aapt::text::Printer;
40using ::android::StringPiece;
41using ::android::base::StringPrintf;
42
Winson62ac8b52019-12-04 08:36:48 -080043using android::idmap2::policy::kPolicyStringToFlag;
44
45using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
46
Adam Lesinski330edcd2015-05-04 17:40:56 -070047namespace aapt {
48
Adam Lesinski6b372992017-08-09 10:54:23 -070049namespace {
50
Adam Lesinski93190b72017-11-03 15:20:17 -070051class ValueHeadlinePrinter : public ConstValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -070053 using ConstValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinski93190b72017-11-03 15:20:17 -070055 explicit ValueHeadlinePrinter(const std::string& package, Printer* printer)
56 : package_(package), printer_(printer) {
57 }
58
Adam Lesinskie59f0d82017-10-13 09:36:53 -070059 void Visit(const Attribute* attr) override {
Adam Lesinski93190b72017-11-03 15:20:17 -070060 printer_->Print("(attr) type=");
61 printer_->Print(attr->MaskString());
62 if (!attr->symbols.empty()) {
63 printer_->Print(StringPrintf(" size=%zd", attr->symbols.size()));
64 }
65 }
66
67 void Visit(const Style* style) override {
68 printer_->Print(StringPrintf("(style) size=%zd", style->entries.size()));
69 if (style->parent) {
70 printer_->Print(" parent=");
71
72 const Reference& parent_ref = style->parent.value();
73 if (parent_ref.name) {
74 if (parent_ref.private_reference) {
75 printer_->Print("*");
76 }
77
78 const ResourceName& parent_name = parent_ref.name.value();
79 if (package_ != parent_name.package) {
80 printer_->Print(parent_name.package);
81 printer_->Print(":");
82 }
Iurii Makhnocff10ce2022-02-15 19:33:50 +000083 printer_->Print(parent_name.type.to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -070084 printer_->Print("/");
85 printer_->Print(parent_name.entry);
86 if (parent_ref.id) {
87 printer_->Print(" (");
88 printer_->Print(parent_ref.id.value().to_string());
89 printer_->Print(")");
90 }
91 } else if (parent_ref.id) {
92 printer_->Print(parent_ref.id.value().to_string());
93 } else {
94 printer_->Print("???");
95 }
96 }
97 }
98
99 void Visit(const Array* array) override {
100 printer_->Print(StringPrintf("(array) size=%zd", array->elements.size()));
101 }
102
103 void Visit(const Plural* plural) override {
104 size_t count = std::count_if(plural->values.begin(), plural->values.end(),
105 [](const std::unique_ptr<Item>& v) { return v != nullptr; });
106 printer_->Print(StringPrintf("(plurals) size=%zd", count));
107 }
108
109 void Visit(const Styleable* styleable) override {
110 printer_->Println(StringPrintf("(styleable) size=%zd", styleable->entries.size()));
111 }
112
113 void VisitItem(const Item* item) override {
114 // Pretty much guaranteed to be one line.
115 if (const Reference* ref = ValueCast<Reference>(item)) {
116 // Special case Reference so that we can print local resources without a package name.
117 ref->PrettyPrint(package_, printer_);
118 } else {
119 item->PrettyPrint(printer_);
120 }
121 }
122
123 private:
124 std::string package_;
125 Printer* printer_;
126};
127
128class ValueBodyPrinter : public ConstValueVisitor {
129 public:
130 using ConstValueVisitor::Visit;
131
132 explicit ValueBodyPrinter(const std::string& package, Printer* printer)
133 : package_(package), printer_(printer) {
134 }
135
136 void Visit(const Attribute* attr) override {
137 constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 if (attr->type_mask & kMask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 for (const auto& symbol : attr->symbols) {
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700140 if (symbol.symbol.name) {
141 printer_->Print(symbol.symbol.name.value().entry);
142
143 if (symbol.symbol.id) {
144 printer_->Print("(");
145 printer_->Print(symbol.symbol.id.value().to_string());
146 printer_->Print(")");
147 }
148 } else if (symbol.symbol.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700149 printer_->Print(symbol.symbol.id.value().to_string());
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700150 } else {
151 printer_->Print("???");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700152 }
Ryan Mitchell0c0cedf2019-04-15 16:47:58 -0700153
Adam Lesinski93190b72017-11-03 15:20:17 -0700154 printer_->Println(StringPrintf("=0x%08x", symbol.value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700156 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700158
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700159 void Visit(const Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 for (const auto& entry : style->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 if (entry.key.name) {
162 const ResourceName& name = entry.key.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700163 if (!name.package.empty() && name.package != package_) {
164 printer_->Print(name.package);
165 printer_->Print(":");
Adam Lesinski330edcd2015-05-04 17:40:56 -0700166 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700167 printer_->Print(name.entry);
168
169 if (entry.key.id) {
170 printer_->Print("(");
171 printer_->Print(entry.key.id.value().to_string());
172 printer_->Print(")");
173 }
174 } else if (entry.key.id) {
175 printer_->Print(entry.key.id.value().to_string());
176 } else {
177 printer_->Print("???");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 }
179
Adam Lesinski93190b72017-11-03 15:20:17 -0700180 printer_->Print("=");
181 PrintItem(*entry.value);
182 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700183 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700185
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700186 void Visit(const Array* array) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700187 const size_t count = array->elements.size();
188 printer_->Print("[");
Donald Chai42fe2b22019-05-07 20:03:27 -0700189 for (size_t i = 0u; i < count; i++) {
190 if (i != 0u && i % 4u == 0u) {
191 printer_->Println();
192 printer_->Print(" ");
Adam Lesinski93190b72017-11-03 15:20:17 -0700193 }
Donald Chai42fe2b22019-05-07 20:03:27 -0700194 PrintItem(*array->elements[i]);
195 if (i != count - 1) {
196 printer_->Print(", ");
197 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700198 }
Donald Chai42fe2b22019-05-07 20:03:27 -0700199 printer_->Println("]");
Adam Lesinski6b372992017-08-09 10:54:23 -0700200 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700201
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700202 void Visit(const Plural* plural) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700203 constexpr std::array<const char*, Plural::Count> kPluralNames = {
204 {"zero", "one", "two", "few", "many", "other"}};
205
206 for (size_t i = 0; i < Plural::Count; i++) {
207 if (plural->values[i] != nullptr) {
208 printer_->Print(StringPrintf("%s=", kPluralNames[i]));
209 PrintItem(*plural->values[i]);
210 printer_->Println();
211 }
212 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700213 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700214
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700215 void Visit(const Styleable* styleable) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 for (const auto& attr : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 if (attr.name) {
218 const ResourceName& name = attr.name.value();
Adam Lesinski93190b72017-11-03 15:20:17 -0700219 if (!name.package.empty() && name.package != package_) {
220 printer_->Print(name.package);
221 printer_->Print(":");
Adam Lesinski355f2852016-02-13 20:26:45 -0800222 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700223 printer_->Print(name.entry);
224
225 if (attr.id) {
226 printer_->Print("(");
227 printer_->Print(attr.id.value().to_string());
228 printer_->Print(")");
229 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 if (attr.id) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700233 printer_->Print(attr.id.value().to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 }
Adam Lesinski5a217b02017-11-16 16:58:02 -0800235 printer_->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700236 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 }
238
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700239 void VisitItem(const Item* item) override {
Adam Lesinski93190b72017-11-03 15:20:17 -0700240 // Intentionally left empty, we already printed the Items.
Adam Lesinski6b372992017-08-09 10:54:23 -0700241 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700242
243 private:
244 void PrintItem(const Item& item) {
245 if (const Reference* ref = ValueCast<Reference>(&item)) {
246 // Special case Reference so that we can print local resources without a package name.
247 ref->PrettyPrint(package_, printer_);
248 } else {
249 item.PrettyPrint(printer_);
250 }
251 }
252
253 std::string package_;
254 Printer* printer_;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700255};
256
Adam Lesinski6b372992017-08-09 10:54:23 -0700257} // namespace
258
Adam Lesinski93190b72017-11-03 15:20:17 -0700259void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options,
260 Printer* printer) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700261 const auto table_view = table.GetPartitionedView();
262 for (const auto& package : table_view.packages) {
263 ValueHeadlinePrinter headline_printer(package.name, printer);
264 ValueBodyPrinter body_printer(package.name, printer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265
Adam Lesinski93190b72017-11-03 15:20:17 -0700266 printer->Print("Package name=");
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700267 printer->Print(package.name);
268 if (package.id) {
269 printer->Print(StringPrintf(" id=%02x", package.id.value()));
Adam Lesinski93190b72017-11-03 15:20:17 -0700270 }
271 printer->Println();
272
273 printer->Indent();
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700274 for (const auto& type : package.types) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700275 printer->Print("type ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000276 printer->Print(type.named_type.to_string());
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700277 if (type.id) {
278 printer->Print(StringPrintf(" id=%02x", type.id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700280 printer->Println(StringPrintf(" entryCount=%zd", type.entries.size()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281
Adam Lesinski93190b72017-11-03 15:20:17 -0700282 printer->Indent();
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700283 for (const ResourceTableEntryView& entry : type.entries) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700284 printer->Print("resource ");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700285 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 -0700286 .to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -0700287 printer->Print(" ");
288
289 // Write the name without the package (this is obvious and too verbose).
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000290 printer->Print(type.named_type.to_string());
Adam Lesinski93190b72017-11-03 15:20:17 -0700291 printer->Print("/");
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700292 printer->Print(entry.name);
Adam Lesinski93190b72017-11-03 15:20:17 -0700293
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700294 switch (entry.visibility.level) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800295 case Visibility::Level::kPublic:
Adam Lesinski93190b72017-11-03 15:20:17 -0700296 printer->Print(" PUBLIC");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800298 case Visibility::Level::kPrivate:
Adam Lesinski93190b72017-11-03 15:20:17 -0700299 printer->Print(" _PRIVATE_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800301 case Visibility::Level::kUndefined:
Adam Lesinski93190b72017-11-03 15:20:17 -0700302 // Print nothing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 break;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700304 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700306 if (entry.visibility.staged_api) {
Ryan Mitchell1499f502021-03-30 12:20:08 -0700307 printer->Print(" STAGED");
308 }
309
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700310 if (entry.overlayable_item) {
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200311 printer->Print(" OVERLAYABLE");
312 }
313
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700314 if (entry.staged_id) {
315 printer->Print(" STAGED_ID=");
316 printer->Print(entry.staged_id.value().id.to_string());
317 }
318
Adam Lesinski93190b72017-11-03 15:20:17 -0700319 printer->Println();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700320
Adam Lesinski71be7052017-12-12 16:48:07 -0800321 if (options.show_values) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700322 printer->Indent();
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700323 for (const auto& value : entry.values) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800324 printer->Print("(");
325 printer->Print(value->config.to_string());
326 printer->Print(") ");
327 value->value->Accept(&headline_printer);
328 if (options.show_sources && !value->value->GetSource().path.empty()) {
329 printer->Print(" src=");
330 printer->Print(value->value->GetSource().to_string());
331 }
332 printer->Println();
333 printer->Indent();
334 value->value->Accept(&body_printer);
335 printer->Undent();
336 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700337 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700338 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700340 printer->Undent();
Adam Lesinski330edcd2015-05-04 17:40:56 -0700341 }
Adam Lesinski93190b72017-11-03 15:20:17 -0700342 printer->Undent();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700344}
345
Adam Lesinski6b372992017-08-09 10:54:23 -0700346static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 auto iter = std::lower_bound(names.begin(), names.end(), name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 CHECK(iter != names.end());
349 CHECK(*iter == name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return std::distance(names.begin(), iter);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700351}
352
Adam Lesinski6b372992017-08-09 10:54:23 -0700353void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 std::map<ResourceName, std::set<ResourceName>> graph;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 std::queue<ResourceName> styles_to_visit;
357 styles_to_visit.push(target_style);
358 for (; !styles_to_visit.empty(); styles_to_visit.pop()) {
359 const ResourceName& style_name = styles_to_visit.front();
360 std::set<ResourceName>& parents = graph[style_name];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 if (!parents.empty()) {
362 // We've already visited this style.
363 continue;
364 }
365
Ryan Mitchell4382e442021-07-14 12:53:01 -0700366 std::optional<ResourceTable::SearchResult> result = table->FindResource(style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 if (result) {
368 ResourceEntry* entry = result.value().entry;
369 for (const auto& value : entry->values) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 if (Style* style = ValueCast<Style>(value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 if (style->parent && style->parent.value().name) {
372 parents.insert(style->parent.value().name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 styles_to_visit.push(style->parent.value().name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 }
Adam Lesinskid13fb242015-05-12 20:40:48 -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 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700379
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 std::vector<ResourceName> names;
381 for (const auto& entry : graph) {
382 names.push_back(entry.first);
383 }
384
385 std::cout << "digraph styles {\n";
386 for (const auto& name : names) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700387 std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 }
389
390 for (const auto& entry : graph) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 const ResourceName& style_name = entry.first;
392 size_t style_node_index = GetNodeIndex(names, style_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 for (const auto& parent_name : entry.second) {
395 std::cout << " node_" << style_node_index << " -> "
396 << "node_" << GetNodeIndex(names, parent_name) << ";\n";
Adam Lesinskid13fb242015-05-12 20:40:48 -0700397 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700399
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 std::cout << "}" << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700401}
402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403void Debug::DumpHex(const void* data, size_t len) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 const uint8_t* d = (const uint8_t*)data;
405 for (size_t i = 0; i < len; i++) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700406 std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " ";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 if (i % 8 == 7) {
408 std::cerr << "\n";
Adam Lesinski52364f72016-01-11 13:10:24 -0800409 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800411
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 if (len - 1 % 8 != 7) {
413 std::cerr << std::endl;
414 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800415}
416
Ryan Mitchell5d275512018-07-19 14:29:00 -0700417void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) {
418 using namespace android;
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800419
Ryan Mitchell5d275512018-07-19 14:29:00 -0700420 if (pool->getError() == NO_INIT) {
421 printer->Print("String pool is unitialized.\n");
422 return;
423 } else if (pool->getError() != NO_ERROR) {
424 printer->Print("String pool is corrupt/invalid.\n");
425 return;
426 }
427
428 SortedVector<const void*> uniqueStrings;
429 const size_t N = pool->size();
430 for (size_t i=0; i<N; i++) {
431 size_t len;
432 if (pool->isUTF8()) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000433 uniqueStrings.add(UnpackOptionalString(pool->string8At(i), &len));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700434 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000435 uniqueStrings.add(UnpackOptionalString(pool->stringAt(i), &len));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700436 }
437 }
438
439 printer->Print(StringPrintf("String pool of %zd unique %s %s strings, %zd entries and %zd styles "
440 "using %zd bytes:\n", uniqueStrings.size(),
441 pool->isUTF8() ? "UTF-8" : "UTF-16",
442 pool->isSorted() ? "sorted" : "non-sorted", N, pool->styleCount(),
443 pool->bytes()));
444
445 const size_t NS = pool->size();
446 for (size_t s=0; s<NS; s++) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000447 auto str = pool->string8ObjectAt(s);
448 printer->Print(StringPrintf("String #%zd : %s\n", s, str.has_value() ? str->string() : ""));
Ryan Mitchell5d275512018-07-19 14:29:00 -0700449 }
450}
451
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700452namespace {
453
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700454class XmlPrinter : public xml::ConstVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700456 using xml::ConstVisitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700457
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800458 explicit XmlPrinter(Printer* printer) : printer_(printer) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800459 }
460
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700461 void Visit(const xml::Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700462 for (const xml::NamespaceDecl& decl : el->namespace_decls) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800463 printer_->Println(StringPrintf("N: %s=%s (line=%zu)", decl.prefix.c_str(), decl.uri.c_str(),
464 decl.line_number));
465 printer_->Indent();
Adam Lesinski6b372992017-08-09 10:54:23 -0700466 }
467
Adam Lesinskida9eba32018-02-13 16:44:10 -0800468 printer_->Print("E: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 if (!el->namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800470 printer_->Print(el->namespace_uri);
471 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800473 printer_->Println(StringPrintf("%s (line=%zu)", el->name.c_str(), el->line_number));
474 printer_->Indent();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700475
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 for (const xml::Attribute& attr : el->attributes) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800477 printer_->Print("A: ");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if (!attr.namespace_uri.empty()) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800479 printer_->Print(attr.namespace_uri);
480 printer_->Print(":");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800482 printer_->Print(attr.name);
Adam Lesinski4ca56972017-04-26 21:49:53 -0700483
484 if (attr.compiled_attribute) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800485 printer_->Print("(");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700486 printer_->Print(attr.compiled_attribute.value().id.value_or(ResourceId(0)).to_string());
Adam Lesinskida9eba32018-02-13 16:44:10 -0800487 printer_->Print(")");
Adam Lesinski4ca56972017-04-26 21:49:53 -0700488 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800489 printer_->Print("=");
Shane Farmer6ed40612017-09-06 10:00:07 -0700490 if (attr.compiled_value != nullptr) {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800491 attr.compiled_value->PrettyPrint(printer_);
Shane Farmer6ed40612017-09-06 10:00:07 -0700492 } else {
Adam Lesinskibbf42972018-02-14 13:36:09 -0800493 printer_->Print("\"");
Adam Lesinskida9eba32018-02-13 16:44:10 -0800494 printer_->Print(attr.value);
Adam Lesinskibbf42972018-02-14 13:36:09 -0800495 printer_->Print("\"");
496 }
497
498 if (!attr.value.empty()) {
499 printer_->Print(" (Raw: \"");
500 printer_->Print(attr.value);
501 printer_->Print("\")");
Shane Farmer6ed40612017-09-06 10:00:07 -0700502 }
Adam Lesinskida9eba32018-02-13 16:44:10 -0800503 printer_->Println();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700504 }
505
Adam Lesinskida9eba32018-02-13 16:44:10 -0800506 printer_->Indent();
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700507 xml::ConstVisitor::Visit(el);
Adam Lesinskida9eba32018-02-13 16:44:10 -0800508 printer_->Undent();
509 printer_->Undent();
510
511 for (size_t i = 0; i < el->namespace_decls.size(); i++) {
512 printer_->Undent();
513 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700515
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700516 void Visit(const xml::Text* text) override {
Adam Lesinskida9eba32018-02-13 16:44:10 -0800517 printer_->Println(StringPrintf("T: '%s'", text->text.c_str()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 }
519
520 private:
Adam Lesinskida9eba32018-02-13 16:44:10 -0800521 Printer* printer_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700522};
523
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700525
Adam Lesinskida9eba32018-02-13 16:44:10 -0800526void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) {
527 XmlPrinter xml_visitor(printer);
528 doc.root->Accept(&xml_visitor);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700529}
Adam Lesinski52364f72016-01-11 13:10:24 -0800530
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200531struct DumpOverlayableEntry {
532 std::string overlayable_section;
533 std::string policy_subsection;
534 std::string resource_name;
535};
536
537void Debug::DumpOverlayable(const ResourceTable& table, text::Printer* printer) {
538 std::vector<DumpOverlayableEntry> items;
539 for (const auto& package : table.packages) {
540 for (const auto& type : package->types) {
541 for (const auto& entry : type->entries) {
542 if (entry->overlayable_item) {
543 const auto& overlayable_item = entry->overlayable_item.value();
544 const auto overlayable_section = StringPrintf(R"(name="%s" actor="%s")",
545 overlayable_item.overlayable->name.c_str(),
546 overlayable_item.overlayable->actor.c_str());
547 const auto policy_subsection = StringPrintf(R"(policies="%s")",
Ryan Mitchella7070132020-05-13 14:17:52 -0700548 android::idmap2::policy::PoliciesToDebugString(overlayable_item.policies).c_str());
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200549 const auto value =
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000550 StringPrintf("%s/%s", type->named_type.to_string().data(), entry->name.c_str());
MÃ¥rten Kongstad1d3b64852019-09-17 13:02:32 +0200551 items.push_back(DumpOverlayableEntry{overlayable_section, policy_subsection, value});
552 }
553 }
554 }
555 }
556
557 std::sort(items.begin(), items.end(),
558 [](const DumpOverlayableEntry& a, const DumpOverlayableEntry& b) {
559 if (a.overlayable_section != b.overlayable_section) {
560 return a.overlayable_section < b.overlayable_section;
561 }
562 if (a.policy_subsection != b.policy_subsection) {
563 return a.policy_subsection < b.policy_subsection;
564 }
565 return a.resource_name < b.resource_name;
566 });
567
568 std::string last_overlayable_section;
569 std::string last_policy_subsection;
570 for (const auto& item : items) {
571 if (last_overlayable_section != item.overlayable_section) {
572 printer->Println(item.overlayable_section);
573 last_overlayable_section = item.overlayable_section;
574 }
575 if (last_policy_subsection != item.policy_subsection) {
576 printer->Indent();
577 printer->Println(item.policy_subsection);
578 last_policy_subsection = item.policy_subsection;
579 printer->Undent();
580 }
581 printer->Indent();
582 printer->Indent();
583 printer->Println(item.resource_name);
584 printer->Undent();
585 printer->Undent();
586 }
587}
588
Ryan Mitchell19b27092018-08-13 11:36:27 -0700589namespace {
590
591using namespace android;
592
593class ChunkPrinter {
594 public:
595 ChunkPrinter(const void* data, size_t len, Printer* printer, IDiagnostics* diag)
596 : data_(data), data_len_(len), printer_(printer), diag_(diag) {
597 }
598
599 void PrintChunkHeader(const ResChunk_header* chunk) {
600 switch (util::DeviceToHost16(chunk->type)) {
601 case RES_STRING_POOL_TYPE:
602 printer_->Print("[RES_STRING_POOL_TYPE]");
603 break;
604 case RES_TABLE_LIBRARY_TYPE:
605 printer_->Print("[RES_TABLE_LIBRARY_TYPE]");
606 break;
607 case RES_TABLE_TYPE:
608 printer_->Print("[ResTable_header]");
609 break;
610 case RES_TABLE_PACKAGE_TYPE:
611 printer_->Print("[ResTable_package]");
612 break;
613 case RES_TABLE_TYPE_TYPE:
614 printer_->Print("[ResTable_type]");
615 break;
616 case RES_TABLE_TYPE_SPEC_TYPE:
617 printer_->Print("[RES_TABLE_TYPE_SPEC_TYPE]");
618 break;
619 default:
620 break;
621 }
622
623 printer_->Print(StringPrintf(" chunkSize: %u", util::DeviceToHost32(chunk->size)));
624 printer_->Print(StringPrintf(" headerSize: %u", util::DeviceToHost32(chunk->headerSize)));
625 }
626
627 bool PrintTable(const ResTable_header* chunk) {
628 printer_->Print(
629 StringPrintf(" Package count: %u\n", util::DeviceToHost32(chunk->packageCount)));
630
631 // Print the chunks contained within the table
632 printer_->Indent();
633 bool success = PrintChunk(
634 ResChunkPullParser(GetChunkData(&chunk->header), GetChunkDataLen(&chunk->header)));
635 printer_->Undent();
636 return success;
637 }
638
639 void PrintResValue(const Res_value* value, const ConfigDescription& config,
640 const ResourceType* type) {
641 printer_->Print("[Res_value]");
642 printer_->Print(StringPrintf(" size: %u", util::DeviceToHost32(value->size)));
643 printer_->Print(StringPrintf(" dataType: 0x%02x", util::DeviceToHost32(value->dataType)));
644 printer_->Print(StringPrintf(" data: 0x%08x", util::DeviceToHost32(value->data)));
645
646 if (type) {
647 auto item =
648 ResourceUtils::ParseBinaryResValue(*type, config, value_pool_, *value, &out_pool_);
649 printer_->Print(" (");
650 item->PrettyPrint(printer_);
651 printer_->Print(")");
652 }
653
654 printer_->Print("\n");
655 }
656
657 bool PrintTableType(const ResTable_type* chunk) {
658 printer_->Print(StringPrintf(" id: 0x%02x", util::DeviceToHost32(chunk->id)));
659 printer_->Print(StringPrintf(
660 " name: %s", util::GetString(type_pool_, util::DeviceToHost32(chunk->id) - 1).c_str()));
661 printer_->Print(StringPrintf(" flags: 0x%02x", util::DeviceToHost32(chunk->flags)));
662 printer_->Print(StringPrintf(" entryCount: %u", util::DeviceToHost32(chunk->entryCount)));
663 printer_->Print(StringPrintf(" entryStart: %u", util::DeviceToHost32(chunk->entriesStart)));
664
665 ConfigDescription config;
666 config.copyFromDtoH(chunk->config);
667 printer_->Print(StringPrintf(" config: %s\n", config.to_string().c_str()));
668
669 const ResourceType* type =
670 ParseResourceType(util::GetString(type_pool_, util::DeviceToHost32(chunk->id) - 1));
671
672 printer_->Indent();
673
674 TypeVariant tv(chunk);
675 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
676 const ResTable_entry* entry = *it;
677 if (!entry) {
678 continue;
679 }
680
681 printer_->Print((entry->flags & ResTable_entry::FLAG_COMPLEX) ? "[ResTable_map_entry]"
682 : "[ResTable_entry]");
683 printer_->Print(StringPrintf(" id: 0x%04x", it.index()));
684 printer_->Print(StringPrintf(
685 " name: %s", util::GetString(key_pool_, util::DeviceToHost32(entry->key.index)).c_str()));
686 printer_->Print(StringPrintf(" keyIndex: %u", util::DeviceToHost32(entry->key.index)));
687 printer_->Print(StringPrintf(" size: %u", util::DeviceToHost32(entry->size)));
688 printer_->Print(StringPrintf(" flags: 0x%04x", util::DeviceToHost32(entry->flags)));
689
690 printer_->Indent();
691
692 if (entry->flags & ResTable_entry::FLAG_COMPLEX) {
693 auto map_entry = (const ResTable_map_entry*)entry;
694 printer_->Print(StringPrintf(" count: 0x%04x", util::DeviceToHost32(map_entry->count)));
695 printer_->Print(
696 StringPrintf(" parent: 0x%08x\n", util::DeviceToHost32(map_entry->parent.ident)));
697
698 // Print the name and value mappings
699 auto maps =
700 (const ResTable_map*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
701 for (size_t i = 0, count = util::DeviceToHost32(map_entry->count); i < count; i++) {
702 PrintResValue(&(maps[i].value), config, type);
703
704 printer_->Print(StringPrintf(
705 " name: %s name-id:%d\n",
706 util::GetString(key_pool_, util::DeviceToHost32(maps[i].name.ident)).c_str(),
707 util::DeviceToHost32(maps[i].name.ident)));
708 }
709 } else {
710 printer_->Print("\n");
711
712 // Print the value of the entry
713 auto value = (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
714 PrintResValue(value, config, type);
715 }
716
717 printer_->Undent();
718 }
719
720 printer_->Undent();
721 return true;
722 }
723
724 void PrintStringPool(const ResStringPool_header* chunk) {
725 // Initialize the string pools
726
727 ResStringPool* pool;
728 if (value_pool_.getError() == NO_INIT) {
729 pool = &value_pool_;
730 } else if (type_pool_.getError() == NO_INIT) {
731 pool = &type_pool_;
732 } else if (key_pool_.getError() == NO_INIT) {
733 pool = &key_pool_;
734 } else {
735 return;
736 }
737
738 pool->setTo(chunk,
739 util::DeviceToHost32((reinterpret_cast<const ResChunk_header*>(chunk))->size));
740
741 printer_->Print("\n");
742
743 for (size_t i = 0; i < pool->size(); i++) {
744 printer_->Print(StringPrintf("#%zd : %s\n", i, util::GetString(*pool, i).c_str()));
745 }
746 }
747
748 bool PrintPackage(const ResTable_package* chunk) {
749 printer_->Print(StringPrintf(" id: 0x%02x", util::DeviceToHost32(chunk->id)));
750
751 size_t len = strnlen16((const char16_t*)chunk->name, std::size(chunk->name));
752 std::u16string package_name(len, u'\0');
753 package_name.resize(len);
754 for (size_t i = 0; i < len; i++) {
755 package_name[i] = util::DeviceToHost16(chunk->name[i]);
756 }
757
758 printer_->Print(StringPrintf("name: %s", String8(package_name.c_str()).c_str()));
759 printer_->Print(StringPrintf(" typeStrings: %u", util::DeviceToHost32(chunk->typeStrings)));
760 printer_->Print(
761 StringPrintf(" lastPublicType: %u", util::DeviceToHost32(chunk->lastPublicType)));
762 printer_->Print(StringPrintf(" keyStrings: %u", util::DeviceToHost32(chunk->keyStrings)));
763 printer_->Print(StringPrintf(" lastPublicKey: %u", util::DeviceToHost32(chunk->lastPublicKey)));
764 printer_->Print(StringPrintf(" typeIdOffset: %u\n", util::DeviceToHost32(chunk->typeIdOffset)));
765
766 // Print the chunks contained within the table
767 printer_->Indent();
768 bool success = PrintChunk(
769 ResChunkPullParser(GetChunkData(&chunk->header), GetChunkDataLen(&chunk->header)));
770 printer_->Undent();
771 return success;
772 }
773
774 bool PrintChunk(ResChunkPullParser&& parser) {
775 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
776 auto chunk = parser.chunk();
777 PrintChunkHeader(chunk);
778
779 switch (util::DeviceToHost16(chunk->type)) {
780 case RES_STRING_POOL_TYPE:
781 PrintStringPool(reinterpret_cast<const ResStringPool_header*>(chunk));
782 break;
783
784 case RES_TABLE_TYPE:
785 PrintTable(reinterpret_cast<const ResTable_header*>(chunk));
786 break;
787
788 case RES_TABLE_PACKAGE_TYPE:
789 type_pool_.uninit();
790 key_pool_.uninit();
791 PrintPackage(reinterpret_cast<const ResTable_package*>(chunk));
792 break;
793
794 case RES_TABLE_TYPE_TYPE:
795 PrintTableType(reinterpret_cast<const ResTable_type*>(chunk));
796 break;
797
798 default:
799 printer_->Print("\n");
800 break;
801 }
802 }
803
804 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
805 diag_->Error(DiagMessage(source_) << "corrupt resource table: " << parser.error());
806 return false;
807 }
808
809 return true;
810 }
811
812 void Print() {
813 PrintChunk(ResChunkPullParser(data_, data_len_));
814 printer_->Print("[End]\n");
815 }
816
817 private:
818 const Source source_;
819 const void* data_;
820 const size_t data_len_;
821 Printer* printer_;
822 IDiagnostics* diag_;
823
824 // The standard value string pool for resource values.
825 ResStringPool value_pool_;
826
827 // The string pool that holds the names of the types defined
828 // in this table.
829 ResStringPool type_pool_;
830
831 // The string pool that holds the names of the entries defined
832 // in this table.
833 ResStringPool key_pool_;
834
835 StringPool out_pool_;
836};
837
838} // namespace
839
840void Debug::DumpChunks(const void* data, size_t len, Printer* printer, IDiagnostics* diag) {
841 ChunkPrinter chunk_printer(data, len, printer, diag);
842 chunk_printer.Print();
843}
844
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700845} // namespace aapt