| 1 |
/* |
| 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" |
| 18 |
|
| 19 |
#include <androidfw/TypeWrappers.h> |
| 20 |
#include <format/binary/ResChunkPullParser.h> |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <map> |
| 24 |
#include <memory> |
| 25 |
#include <queue> |
| 26 |
#include <set> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "ResourceTable.h" |
| 30 |
#include "ResourceUtils.h" |
| 31 |
#include "ResourceValues.h" |
| 32 |
#include "ValueVisitor.h" |
| 33 |
#include "android-base/logging.h" |
| 34 |
#include "android-base/stringprintf.h" |
| 35 |
#include "idmap2/Policies.h" |
| 36 |
#include "text/Printer.h" |
| 37 |
#include "util/Util.h" |
| 38 |
|
| 39 |
using ::aapt::text::Printer; |
| 40 |
using ::android::StringPiece; |
| 41 |
using ::android::base::StringPrintf; |
| 42 |
|
| 43 |
using android::idmap2::policy::kPolicyStringToFlag; |
| 44 |
|
| 45 |
using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 46 |
|
| 47 |
namespace aapt { |
| 48 |
|
| 49 |
namespace { |
| 50 |
|
| 51 |
class ValueHeadlinePrinter : public ConstValueVisitor { |
| 52 |
public: |
| 53 |
using ConstValueVisitor::Visit; |
| 54 |
|
| 55 |
explicit ValueHeadlinePrinter(const std::string& package, Printer* printer) |
| 56 |
: package_(package), printer_(printer) { |
| 57 |
} |
| 58 |
|
| 59 |
void Visit(const Attribute* attr) override { |
| 60 |
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 |
} |
| 83 |
printer_->Print(parent_name.type.to_string()); |
| 84 |
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 |
|
| 128 |
class 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; |
| 138 |
if (attr->type_mask & kMask) { |
| 139 |
for (const auto& symbol : attr->symbols) { |
| 140 |
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) { |
| 149 |
printer_->Print(symbol.symbol.id.value().to_string()); |
| 150 |
} else { |
| 151 |
printer_->Print("???"); |
| 152 |
} |
| 153 |
|
| 154 |
printer_->Println(StringPrintf("=0x%08x", symbol.value)); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void Visit(const Style* style) override { |
| 160 |
for (const auto& entry : style->entries) { |
| 161 |
if (entry.key.name) { |
| 162 |
const ResourceName& name = entry.key.name.value(); |
| 163 |
if (!name.package.empty() && name.package != package_) { |
| 164 |
printer_->Print(name.package); |
| 165 |
printer_->Print(":"); |
| 166 |
} |
| 167 |
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("???"); |
| 178 |
} |
| 179 |
|
| 180 |
printer_->Print("="); |
| 181 |
PrintItem(*entry.value); |
| 182 |
printer_->Println(); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
void Visit(const Array* array) override { |
| 187 |
const size_t count = array->elements.size(); |
| 188 |
printer_->Print("["); |
| 189 |
for (size_t i = 0u; i < count; i++) { |
| 190 |
if (i != 0u && i % 4u == 0u) { |
| 191 |
printer_->Println(); |
| 192 |
printer_->Print(" "); |
| 193 |
} |
| 194 |
PrintItem(*array->elements[i]); |
| 195 |
if (i != count - 1) { |
| 196 |
printer_->Print(", "); |
| 197 |
} |
| 198 |
} |
| 199 |
printer_->Println("]"); |
| 200 |
} |
| 201 |
|
| 202 |
void Visit(const Plural* plural) override { |
| 203 |
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 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void Visit(const Styleable* styleable) override { |
| 216 |
for (const auto& attr : styleable->entries) { |
| 217 |
if (attr.name) { |
| 218 |
const ResourceName& name = attr.name.value(); |
| 219 |
if (!name.package.empty() && name.package != package_) { |
| 220 |
printer_->Print(name.package); |
| 221 |
printer_->Print(":"); |
| 222 |
} |
| 223 |
printer_->Print(name.entry); |
| 224 |
|
| 225 |
if (attr.id) { |
| 226 |
printer_->Print("("); |
| 227 |
printer_->Print(attr.id.value().to_string()); |
| 228 |
printer_->Print(")"); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if (attr.id) { |
| 233 |
printer_->Print(attr.id.value().to_string()); |
| 234 |
} |
| 235 |
printer_->Println(); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void VisitItem(const Item* item) override { |
| 240 |
// Intentionally left empty, we already printed the Items. |
| 241 |
} |
| 242 |
|
| 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_; |
| 255 |
}; |
| 256 |
|
| 257 |
} // namespace |
| 258 |
|
| 259 |
void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options, |
| 260 |
Printer* printer) { |
| 261 |
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); |
| 265 |
|
| 266 |
printer->Print("Package name="); |
| 267 |
printer->Print(package.name); |
| 268 |
if (package.id) { |
| 269 |
printer->Print(StringPrintf(" id=%02x", package.id.value())); |
| 270 |
} |
| 271 |
printer->Println(); |
| 272 |
|
| 273 |
printer->Indent(); |
| 274 |
for (const auto& type : package.types) { |
| 275 |
printer->Print("type "); |
| 276 |
printer->Print(to_string(type.type)); |
| 277 |
if (type.id) { |
| 278 |
printer->Print(StringPrintf(" id=%02x", type.id.value())); |
| 279 |
} |
| 280 |
printer->Println(StringPrintf(" entryCount=%zd", type.entries.size())); |
| 281 |
|
| 282 |
printer->Indent(); |
| 283 |
for (const ResourceTableEntryView& entry : type.entries) { |
| 284 |
printer->Print("resource "); |
| 285 |
printer->Print(ResourceId(package.id.value_or(0), type.id.value_or(0), entry.id.value_or(0)) |
| 286 |
.to_string()); |
| 287 |
printer->Print(" "); |
| 288 |
|
| 289 |
// Write the name without the package (this is obvious and too verbose). |
| 290 |
printer->Print(to_string(type.type)); |
| 291 |
printer->Print("/"); |
| 292 |
printer->Print(entry.name); |
| 293 |
|
| 294 |
switch (entry.visibility.level) { |
| 295 |
case Visibility::Level::kPublic: |
| 296 |
printer->Print(" PUBLIC"); |
| 297 |
break; |
| 298 |
case Visibility::Level::kPrivate: |
| 299 |
printer->Print(" _PRIVATE_"); |
| 300 |
break; |
| 301 |
case Visibility::Level::kUndefined: |
| 302 |
// Print nothing. |
| 303 |
break; |
| 304 |
} |
| 305 |
|
| 306 |
if (entry.visibility.staged_api) { |
| 307 |
printer->Print(" STAGED"); |
| 308 |
} |
| 309 |
|
| 310 |
if (entry.overlayable_item) { |
| 311 |
printer->Print(" OVERLAYABLE"); |
| 312 |
} |
| 313 |
|
| 314 |
if (entry.staged_id) { |
| 315 |
printer->Print(" STAGED_ID="); |
| 316 |
printer->Print(entry.staged_id.value().id.to_string()); |
| 317 |
} |
| 318 |
|
| 319 |
printer->Println(); |
| 320 |
|
| 321 |
if (options.show_values) { |
| 322 |
printer->Indent(); |
| 323 |
for (const auto& value : entry.values) { |
| 324 |
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 |
} |
| 337 |
printer->Undent(); |
| 338 |
} |
| 339 |
} |
| 340 |
printer->Undent(); |
| 341 |
} |
| 342 |
printer->Undent(); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) { |
| 347 |
auto iter = std::lower_bound(names.begin(), names.end(), name); |
| 348 |
CHECK(iter != names.end()); |
| 349 |
CHECK(*iter == name); |
| 350 |
return std::distance(names.begin(), iter); |
| 351 |
} |
| 352 |
|
| 353 |
void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) { |
| 354 |
std::map<ResourceName, std::set<ResourceName>> graph; |
| 355 |
|
| 356 |
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]; |
| 361 |
if (!parents.empty()) { |
| 362 |
// We've already visited this style. |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
std::optional<ResourceTable::SearchResult> result = table->FindResource(style_name); |
| 367 |
if (result) { |
| 368 |
ResourceEntry* entry = result.value().entry; |
| 369 |
for (const auto& value : entry->values) { |
| 370 |
if (Style* style = ValueCast<Style>(value->value.get())) { |
| 371 |
if (style->parent && style->parent.value().name) { |
| 372 |
parents.insert(style->parent.value().name.value()); |
| 373 |
styles_to_visit.push(style->parent.value().name.value()); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
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) { |
| 387 |
std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n"; |
| 388 |
} |
| 389 |
|
| 390 |
for (const auto& entry : graph) { |
| 391 |
const ResourceName& style_name = entry.first; |
| 392 |
size_t style_node_index = GetNodeIndex(names, style_name); |
| 393 |
|
| 394 |
for (const auto& parent_name : entry.second) { |
| 395 |
std::cout << " node_" << style_node_index << " -> " |
| 396 |
<< "node_" << GetNodeIndex(names, parent_name) << ";\n"; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
std::cout << "}" << std::endl; |
| 401 |
} |
| 402 |
|
| 403 |
void Debug::DumpHex(const void* data, size_t len) { |
| 404 |
const uint8_t* d = (const uint8_t*)data; |
| 405 |
for (size_t i = 0; i < len; i++) { |
| 406 |
std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " "; |
| 407 |
if (i % 8 == 7) { |
| 408 |
std::cerr << "\n"; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
if (len - 1 % 8 != 7) { |
| 413 |
std::cerr << std::endl; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) { |
| 418 |
using namespace android; |
| 419 |
|
| 420 |
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()) { |
| 433 |
uniqueStrings.add(UnpackOptionalString(pool->string8At(i), &len)); |
| 434 |
} else { |
| 435 |
uniqueStrings.add(UnpackOptionalString(pool->stringAt(i), &len)); |
| 436 |
} |
| 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++) { |
| 447 |
auto str = pool->string8ObjectAt(s); |
| •448 |
printer->Print(StringPrintf("String #%zd : %s\n", s, str.has_value() ? str->c_str() : "")); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
namespace { |
| 453 |
|
| 454 |
class XmlPrinter : public xml::ConstVisitor { |
| 455 |
public: |
| 456 |
using xml::ConstVisitor::Visit; |
| 457 |
|
| 458 |
explicit XmlPrinter(Printer* printer) : printer_(printer) { |
| 459 |
} |
| 460 |
|
| 461 |
void Visit(const xml::Element* el) override { |
| 462 |
for (const xml::NamespaceDecl& decl : el->namespace_decls) { |
| 463 |
printer_->Println(StringPrintf("N: %s=%s (line=%zu)", decl.prefix.c_str(), decl.uri.c_str(), |
| 464 |
decl.line_number)); |
| 465 |
printer_->Indent(); |
| 466 |
} |
| 467 |
|
| 468 |
printer_->Print("E: "); |
| 469 |
if (!el->namespace_uri.empty()) { |
| 470 |
printer_->Print(el->namespace_uri); |
| 471 |
printer_->Print(":"); |
| 472 |
} |
| 473 |
printer_->Println(StringPrintf("%s (line=%zu)", el->name.c_str(), el->line_number)); |
| 474 |
printer_->Indent(); |
| 475 |
|
| 476 |
for (const xml::Attribute& attr : el->attributes) { |
| 477 |
printer_->Print("A: "); |
| 478 |
if (!attr.namespace_uri.empty()) { |
| 479 |
printer_->Print(attr.namespace_uri); |
| 480 |
printer_->Print(":"); |
| 481 |
} |
| 482 |
printer_->Print(attr.name); |
| 483 |
|
| 484 |
if (attr.compiled_attribute) { |
| 485 |
printer_->Print("("); |
| 486 |
printer_->Print(attr.compiled_attribute.value().id.value_or(ResourceId(0)).to_string()); |
| 487 |
printer_->Print(")"); |
| 488 |
} |
| 489 |
printer_->Print("="); |
| 490 |
if (attr.compiled_value != nullptr) { |
| 491 |
attr.compiled_value->PrettyPrint(printer_); |
| 492 |
} else { |
| 493 |
printer_->Print("\""); |
| 494 |
printer_->Print(attr.value); |
| 495 |
printer_->Print("\""); |
| 496 |
} |
| 497 |
|
| 498 |
if (!attr.value.empty()) { |
| 499 |
printer_->Print(" (Raw: \""); |
| 500 |
printer_->Print(attr.value); |
| 501 |
printer_->Print("\")"); |
| 502 |
} |
| 503 |
printer_->Println(); |
| 504 |
} |
| 505 |
|
| 506 |
printer_->Indent(); |
| 507 |
xml::ConstVisitor::Visit(el); |
| 508 |
printer_->Undent(); |
| 509 |
printer_->Undent(); |
| 510 |
|
| 511 |
for (size_t i = 0; i < el->namespace_decls.size(); i++) { |
| 512 |
printer_->Undent(); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
void Visit(const xml::Text* text) override { |
| 517 |
printer_->Println(StringPrintf("T: '%s'", text->text.c_str())); |
| 518 |
} |
| 519 |
|
| 520 |
private: |
| 521 |
Printer* printer_; |
| 522 |
}; |
| 523 |
|
| 524 |
} // namespace |
| 525 |
|
| 526 |
void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) { |
| 527 |
XmlPrinter xml_visitor(printer); |
| 528 |
doc.root->Accept(&xml_visitor); |
| 529 |
} |
| 530 |
|
| 531 |
struct DumpOverlayableEntry { |
| 532 |
std::string overlayable_section; |
| 533 |
std::string policy_subsection; |
| 534 |
std::string resource_name; |
| 535 |
}; |
| 536 |
|
| 537 |
void 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")", |
| 548 |
android::idmap2::policy::PoliciesToDebugString(overlayable_item.policies).c_str()); |
| 549 |
const auto value = |
| 550 |
StringPrintf("%s/%s", to_string(type->type).data(), entry->name.c_str()); |
| 551 |
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 |
|
| 589 |
namespace { |
| 590 |
|
| 591 |
using namespace android; |
| 592 |
|
| 593 |
class 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 |
|
| 840 |
void 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 |
|
| 845 |
} // namespace aapt |