Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 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" |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 18 | |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 19 | #include <androidfw/TypeWrappers.h> |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 20 | #include <androidfw/Util.h> |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 21 | #include <format/binary/ResChunkPullParser.h> |
| 22 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 24 | #include <map> |
| 25 | #include <memory> |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 26 | #include <queue> |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 27 | #include <set> |
| 28 | #include <vector> |
| 29 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | #include "ResourceTable.h" |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 31 | #include "ResourceUtils.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | #include "ResourceValues.h" |
| 33 | #include "ValueVisitor.h" |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 34 | #include "android-base/logging.h" |
| 35 | #include "android-base/stringprintf.h" |
felkachang | a1da277 | 2022-08-29 17:54:09 +0800 | [diff] [blame] | 36 | #include "androidfw/ResourceTypes.h" |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 37 | #include "idmap2/Policies.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 38 | #include "text/Printer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | #include "util/Util.h" |
| 40 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 41 | using ::aapt::text::Printer; |
| 42 | using ::android::StringPiece; |
| 43 | using ::android::base::StringPrintf; |
| 44 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 45 | using android::idmap2::policy::kPolicyStringToFlag; |
| 46 | |
| 47 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 48 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 49 | namespace aapt { |
| 50 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 51 | namespace { |
| 52 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 53 | class ValueHeadlinePrinter : public ConstValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | public: |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 55 | using ConstValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 57 | explicit ValueHeadlinePrinter(const std::string& package, Printer* printer) |
| 58 | : package_(package), printer_(printer) { |
| 59 | } |
| 60 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 61 | void Visit(const Attribute* attr) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 62 | 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 Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 85 | printer_->Print(parent_name.type.to_string()); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 86 | 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 | |
| 130 | class 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | if (attr->type_mask & kMask) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | for (const auto& symbol : attr->symbols) { |
Ryan Mitchell | 0c0cedf | 2019-04-15 16:47:58 -0700 | [diff] [blame] | 142 | 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 Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 151 | printer_->Print(symbol.symbol.id.value().to_string()); |
Ryan Mitchell | 0c0cedf | 2019-04-15 16:47:58 -0700 | [diff] [blame] | 152 | } else { |
| 153 | printer_->Print("???"); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 154 | } |
Ryan Mitchell | 0c0cedf | 2019-04-15 16:47:58 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 156 | printer_->Println(StringPrintf("=0x%08x", symbol.value)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 158 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 160 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 161 | void Visit(const Style* style) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | for (const auto& entry : style->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | if (entry.key.name) { |
| 164 | const ResourceName& name = entry.key.name.value(); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 165 | if (!name.package.empty() && name.package != package_) { |
| 166 | printer_->Print(name.package); |
| 167 | printer_->Print(":"); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 168 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 169 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 182 | printer_->Print("="); |
| 183 | PrintItem(*entry.value); |
| 184 | printer_->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 185 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 187 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 188 | void Visit(const Array* array) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 189 | const size_t count = array->elements.size(); |
| 190 | printer_->Print("["); |
Donald Chai | 42fe2b2 | 2019-05-07 20:03:27 -0700 | [diff] [blame] | 191 | for (size_t i = 0u; i < count; i++) { |
| 192 | if (i != 0u && i % 4u == 0u) { |
| 193 | printer_->Println(); |
| 194 | printer_->Print(" "); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 195 | } |
Donald Chai | 42fe2b2 | 2019-05-07 20:03:27 -0700 | [diff] [blame] | 196 | PrintItem(*array->elements[i]); |
| 197 | if (i != count - 1) { |
| 198 | printer_->Print(", "); |
| 199 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 200 | } |
Donald Chai | 42fe2b2 | 2019-05-07 20:03:27 -0700 | [diff] [blame] | 201 | printer_->Println("]"); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 202 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 204 | void Visit(const Plural* plural) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 205 | 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 Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 215 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 217 | void Visit(const Styleable* styleable) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | if (attr.name) { |
| 220 | const ResourceName& name = attr.name.value(); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 221 | if (!name.package.empty() && name.package != package_) { |
| 222 | printer_->Print(name.package); |
| 223 | printer_->Print(":"); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 224 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 225 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 233 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | if (attr.id) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 235 | printer_->Print(attr.id.value().to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 236 | } |
Adam Lesinski | 5a217b0 | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 237 | printer_->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 238 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 241 | void VisitItem(const Item* item) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 242 | // Intentionally left empty, we already printed the Items. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 243 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 244 | |
| 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 Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 257 | }; |
| 258 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 259 | } // namespace |
| 260 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 261 | void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options, |
| 262 | Printer* printer) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 263 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 267 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 268 | printer->Print("Package name="); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 269 | printer->Print(package.name); |
| 270 | if (package.id) { |
| 271 | printer->Print(StringPrintf(" id=%02x", package.id.value())); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 272 | } |
| 273 | printer->Println(); |
| 274 | |
| 275 | printer->Indent(); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 276 | for (const auto& type : package.types) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 277 | printer->Print("type "); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 278 | printer->Print(type.named_type.to_string()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 279 | if (type.id) { |
| 280 | printer->Print(StringPrintf(" id=%02x", type.id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 282 | printer->Println(StringPrintf(" entryCount=%zd", type.entries.size())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 284 | printer->Indent(); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 285 | for (const ResourceTableEntryView& entry : type.entries) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 286 | printer->Print("resource "); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 287 | printer->Print(ResourceId(package.id.value_or(0), type.id.value_or(0), entry.id.value_or(0)) |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 288 | .to_string()); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 289 | printer->Print(" "); |
| 290 | |
| 291 | // Write the name without the package (this is obvious and too verbose). |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 292 | printer->Print(type.named_type.to_string()); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 293 | printer->Print("/"); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 294 | printer->Print(entry.name); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 295 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 296 | switch (entry.visibility.level) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 297 | case Visibility::Level::kPublic: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 298 | printer->Print(" PUBLIC"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 299 | break; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 300 | case Visibility::Level::kPrivate: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 301 | printer->Print(" _PRIVATE_"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | break; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 303 | case Visibility::Level::kUndefined: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 304 | // Print nothing. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | break; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 306 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 307 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 308 | if (entry.visibility.staged_api) { |
Ryan Mitchell | 1499f50 | 2021-03-30 12:20:08 -0700 | [diff] [blame] | 309 | printer->Print(" STAGED"); |
| 310 | } |
| 311 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 312 | if (entry.overlayable_item) { |
MÃ¥rten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 313 | printer->Print(" OVERLAYABLE"); |
| 314 | } |
| 315 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 316 | if (entry.staged_id) { |
| 317 | printer->Print(" STAGED_ID="); |
| 318 | printer->Print(entry.staged_id.value().id.to_string()); |
| 319 | } |
| 320 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 321 | printer->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 322 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 323 | if (options.show_values) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 324 | printer->Indent(); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 325 | for (const auto& value : entry.values) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 326 | printer->Print("("); |
| 327 | printer->Print(value->config.to_string()); |
| 328 | printer->Print(") "); |
| 329 | value->value->Accept(&headline_printer); |
| 330 | if (options.show_sources && !value->value->GetSource().path.empty()) { |
| 331 | printer->Print(" src="); |
| 332 | printer->Print(value->value->GetSource().to_string()); |
| 333 | } |
| 334 | printer->Println(); |
| 335 | printer->Indent(); |
| 336 | value->value->Accept(&body_printer); |
| 337 | printer->Undent(); |
| 338 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 339 | printer->Undent(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 340 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 342 | printer->Undent(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 343 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 344 | printer->Undent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 345 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 348 | static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 349 | auto iter = std::lower_bound(names.begin(), names.end(), name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 350 | CHECK(iter != names.end()); |
| 351 | CHECK(*iter == name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 352 | return std::distance(names.begin(), iter); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 355 | void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 356 | std::map<ResourceName, std::set<ResourceName>> graph; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 357 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | std::queue<ResourceName> styles_to_visit; |
| 359 | styles_to_visit.push(target_style); |
| 360 | for (; !styles_to_visit.empty(); styles_to_visit.pop()) { |
| 361 | const ResourceName& style_name = styles_to_visit.front(); |
| 362 | std::set<ResourceName>& parents = graph[style_name]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | if (!parents.empty()) { |
| 364 | // We've already visited this style. |
| 365 | continue; |
| 366 | } |
| 367 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 368 | std::optional<ResourceTable::SearchResult> result = table->FindResource(style_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | if (result) { |
| 370 | ResourceEntry* entry = result.value().entry; |
| 371 | for (const auto& value : entry->values) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | if (Style* style = ValueCast<Style>(value->value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 373 | if (style->parent && style->parent.value().name) { |
| 374 | parents.insert(style->parent.value().name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 375 | styles_to_visit.push(style->parent.value().name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 376 | } |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 377 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 378 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 379 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 380 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 381 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | std::vector<ResourceName> names; |
| 383 | for (const auto& entry : graph) { |
| 384 | names.push_back(entry.first); |
| 385 | } |
| 386 | |
| 387 | std::cout << "digraph styles {\n"; |
| 388 | for (const auto& name : names) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 389 | std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | for (const auto& entry : graph) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 393 | const ResourceName& style_name = entry.first; |
| 394 | size_t style_node_index = GetNodeIndex(names, style_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 395 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 396 | for (const auto& parent_name : entry.second) { |
| 397 | std::cout << " node_" << style_node_index << " -> " |
| 398 | << "node_" << GetNodeIndex(names, parent_name) << ";\n"; |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 399 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 400 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 401 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 | std::cout << "}" << std::endl; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 405 | void Debug::DumpHex(const void* data, size_t len) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | const uint8_t* d = (const uint8_t*)data; |
| 407 | for (size_t i = 0; i < len; i++) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 408 | std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " "; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 409 | if (i % 8 == 7) { |
| 410 | std::cerr << "\n"; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 411 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 412 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 413 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 414 | if (len - 1 % 8 != 7) { |
| 415 | std::cerr << std::endl; |
| 416 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 419 | void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) { |
| 420 | using namespace android; |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 421 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 422 | if (pool->getError() == NO_INIT) { |
| 423 | printer->Print("String pool is unitialized.\n"); |
| 424 | return; |
| 425 | } else if (pool->getError() != NO_ERROR) { |
| 426 | printer->Print("String pool is corrupt/invalid.\n"); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | SortedVector<const void*> uniqueStrings; |
| 431 | const size_t N = pool->size(); |
| 432 | for (size_t i=0; i<N; i++) { |
| 433 | size_t len; |
| 434 | if (pool->isUTF8()) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 435 | uniqueStrings.add(UnpackOptionalString(pool->string8At(i), &len)); |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 436 | } else { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 437 | uniqueStrings.add(UnpackOptionalString(pool->stringAt(i), &len)); |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | printer->Print(StringPrintf("String pool of %zd unique %s %s strings, %zd entries and %zd styles " |
| 442 | "using %zd bytes:\n", uniqueStrings.size(), |
| 443 | pool->isUTF8() ? "UTF-8" : "UTF-16", |
| 444 | pool->isSorted() ? "sorted" : "non-sorted", N, pool->styleCount(), |
| 445 | pool->bytes())); |
| 446 | |
| 447 | const size_t NS = pool->size(); |
| 448 | for (size_t s=0; s<NS; s++) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 449 | auto str = pool->string8ObjectAt(s); |
Tomasz Wasilczyk | ade0631 | 2023-08-10 23:54:44 +0000 | [diff] [blame^] | 450 | printer->Print(StringPrintf("String #%zd : %s\n", s, str.has_value() ? str->c_str() : "")); |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 454 | namespace { |
| 455 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 456 | class XmlPrinter : public xml::ConstVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 457 | public: |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 458 | using xml::ConstVisitor::Visit; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 459 | |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 460 | explicit XmlPrinter(Printer* printer) : printer_(printer) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 463 | void Visit(const xml::Element* el) override { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 464 | for (const xml::NamespaceDecl& decl : el->namespace_decls) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 465 | printer_->Println(StringPrintf("N: %s=%s (line=%zu)", decl.prefix.c_str(), decl.uri.c_str(), |
| 466 | decl.line_number)); |
| 467 | printer_->Indent(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 470 | printer_->Print("E: "); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 471 | if (!el->namespace_uri.empty()) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 472 | printer_->Print(el->namespace_uri); |
| 473 | printer_->Print(":"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 474 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 475 | printer_->Println(StringPrintf("%s (line=%zu)", el->name.c_str(), el->line_number)); |
| 476 | printer_->Indent(); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 477 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 478 | for (const xml::Attribute& attr : el->attributes) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 479 | printer_->Print("A: "); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 480 | if (!attr.namespace_uri.empty()) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 481 | printer_->Print(attr.namespace_uri); |
| 482 | printer_->Print(":"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 483 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 484 | printer_->Print(attr.name); |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 485 | |
| 486 | if (attr.compiled_attribute) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 487 | printer_->Print("("); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 488 | printer_->Print(attr.compiled_attribute.value().id.value_or(ResourceId(0)).to_string()); |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 489 | printer_->Print(")"); |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 490 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 491 | printer_->Print("="); |
Shane Farmer | 6ed4061 | 2017-09-06 10:00:07 -0700 | [diff] [blame] | 492 | if (attr.compiled_value != nullptr) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 493 | attr.compiled_value->PrettyPrint(printer_); |
Shane Farmer | 6ed4061 | 2017-09-06 10:00:07 -0700 | [diff] [blame] | 494 | } else { |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 495 | printer_->Print("\""); |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 496 | printer_->Print(attr.value); |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 497 | printer_->Print("\""); |
| 498 | } |
| 499 | |
| 500 | if (!attr.value.empty()) { |
| 501 | printer_->Print(" (Raw: \""); |
| 502 | printer_->Print(attr.value); |
| 503 | printer_->Print("\")"); |
Shane Farmer | 6ed4061 | 2017-09-06 10:00:07 -0700 | [diff] [blame] | 504 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 505 | printer_->Println(); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 508 | printer_->Indent(); |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 509 | xml::ConstVisitor::Visit(el); |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 510 | printer_->Undent(); |
| 511 | printer_->Undent(); |
| 512 | |
| 513 | for (size_t i = 0; i < el->namespace_decls.size(); i++) { |
| 514 | printer_->Undent(); |
| 515 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 516 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 517 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 518 | void Visit(const xml::Text* text) override { |
felkachang | a1da277 | 2022-08-29 17:54:09 +0800 | [diff] [blame] | 519 | printer_->Println( |
| 520 | StringPrintf("T: '%s'", android::ResTable::normalizeForOutput(text->text.c_str()).c_str())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | private: |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 524 | Printer* printer_; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 525 | }; |
| 526 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 527 | } // namespace |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 528 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 529 | void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) { |
| 530 | XmlPrinter xml_visitor(printer); |
| 531 | doc.root->Accept(&xml_visitor); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 532 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 533 | |
MÃ¥rten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 534 | struct DumpOverlayableEntry { |
| 535 | std::string overlayable_section; |
| 536 | std::string policy_subsection; |
| 537 | std::string resource_name; |
| 538 | }; |
| 539 | |
| 540 | void Debug::DumpOverlayable(const ResourceTable& table, text::Printer* printer) { |
| 541 | std::vector<DumpOverlayableEntry> items; |
| 542 | for (const auto& package : table.packages) { |
| 543 | for (const auto& type : package->types) { |
| 544 | for (const auto& entry : type->entries) { |
| 545 | if (entry->overlayable_item) { |
| 546 | const auto& overlayable_item = entry->overlayable_item.value(); |
| 547 | const auto overlayable_section = StringPrintf(R"(name="%s" actor="%s")", |
| 548 | overlayable_item.overlayable->name.c_str(), |
| 549 | overlayable_item.overlayable->actor.c_str()); |
| 550 | const auto policy_subsection = StringPrintf(R"(policies="%s")", |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 551 | android::idmap2::policy::PoliciesToDebugString(overlayable_item.policies).c_str()); |
MÃ¥rten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 552 | const auto value = |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 553 | StringPrintf("%s/%s", type->named_type.to_string().data(), entry->name.c_str()); |
MÃ¥rten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 554 | items.push_back(DumpOverlayableEntry{overlayable_section, policy_subsection, value}); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | std::sort(items.begin(), items.end(), |
| 561 | [](const DumpOverlayableEntry& a, const DumpOverlayableEntry& b) { |
| 562 | if (a.overlayable_section != b.overlayable_section) { |
| 563 | return a.overlayable_section < b.overlayable_section; |
| 564 | } |
| 565 | if (a.policy_subsection != b.policy_subsection) { |
| 566 | return a.policy_subsection < b.policy_subsection; |
| 567 | } |
| 568 | return a.resource_name < b.resource_name; |
| 569 | }); |
| 570 | |
| 571 | std::string last_overlayable_section; |
| 572 | std::string last_policy_subsection; |
| 573 | for (const auto& item : items) { |
| 574 | if (last_overlayable_section != item.overlayable_section) { |
| 575 | printer->Println(item.overlayable_section); |
| 576 | last_overlayable_section = item.overlayable_section; |
| 577 | } |
| 578 | if (last_policy_subsection != item.policy_subsection) { |
| 579 | printer->Indent(); |
| 580 | printer->Println(item.policy_subsection); |
| 581 | last_policy_subsection = item.policy_subsection; |
| 582 | printer->Undent(); |
| 583 | } |
| 584 | printer->Indent(); |
| 585 | printer->Indent(); |
| 586 | printer->Println(item.resource_name); |
| 587 | printer->Undent(); |
| 588 | printer->Undent(); |
| 589 | } |
| 590 | } |
| 591 | |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 592 | namespace { |
| 593 | |
| 594 | using namespace android; |
| 595 | |
| 596 | class ChunkPrinter { |
| 597 | public: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 598 | ChunkPrinter(const void* data, size_t len, Printer* printer, android::IDiagnostics* diag) |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 599 | : data_(data), data_len_(len), printer_(printer), diag_(diag) { |
| 600 | } |
| 601 | |
| 602 | void PrintChunkHeader(const ResChunk_header* chunk) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 603 | switch (android::util::DeviceToHost16(chunk->type)) { |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 604 | case RES_STRING_POOL_TYPE: |
| 605 | printer_->Print("[RES_STRING_POOL_TYPE]"); |
| 606 | break; |
| 607 | case RES_TABLE_LIBRARY_TYPE: |
| 608 | printer_->Print("[RES_TABLE_LIBRARY_TYPE]"); |
| 609 | break; |
| 610 | case RES_TABLE_TYPE: |
| 611 | printer_->Print("[ResTable_header]"); |
| 612 | break; |
| 613 | case RES_TABLE_PACKAGE_TYPE: |
| 614 | printer_->Print("[ResTable_package]"); |
| 615 | break; |
| 616 | case RES_TABLE_TYPE_TYPE: |
| 617 | printer_->Print("[ResTable_type]"); |
| 618 | break; |
| 619 | case RES_TABLE_TYPE_SPEC_TYPE: |
| 620 | printer_->Print("[RES_TABLE_TYPE_SPEC_TYPE]"); |
| 621 | break; |
| 622 | default: |
| 623 | break; |
| 624 | } |
| 625 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 626 | printer_->Print(StringPrintf(" chunkSize: %u", android::util::DeviceToHost32(chunk->size))); |
| 627 | printer_->Print( |
| 628 | StringPrintf(" headerSize: %u", android::util::DeviceToHost32(chunk->headerSize))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | bool PrintTable(const ResTable_header* chunk) { |
| 632 | printer_->Print( |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 633 | StringPrintf(" Package count: %u\n", android::util::DeviceToHost32(chunk->packageCount))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 634 | |
| 635 | // Print the chunks contained within the table |
| 636 | printer_->Indent(); |
| 637 | bool success = PrintChunk( |
| 638 | ResChunkPullParser(GetChunkData(&chunk->header), GetChunkDataLen(&chunk->header))); |
| 639 | printer_->Undent(); |
| 640 | return success; |
| 641 | } |
| 642 | |
| 643 | void PrintResValue(const Res_value* value, const ConfigDescription& config, |
| 644 | const ResourceType* type) { |
| 645 | printer_->Print("[Res_value]"); |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 646 | printer_->Print(StringPrintf(" size: %u", android::util::DeviceToHost32(value->size))); |
| 647 | printer_->Print( |
| 648 | StringPrintf(" dataType: 0x%02x", android::util::DeviceToHost32(value->dataType))); |
| 649 | printer_->Print(StringPrintf(" data: 0x%08x", android::util::DeviceToHost32(value->data))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 650 | |
| 651 | if (type) { |
| 652 | auto item = |
| 653 | ResourceUtils::ParseBinaryResValue(*type, config, value_pool_, *value, &out_pool_); |
| 654 | printer_->Print(" ("); |
| 655 | item->PrettyPrint(printer_); |
| 656 | printer_->Print(")"); |
| 657 | } |
| 658 | |
| 659 | printer_->Print("\n"); |
| 660 | } |
| 661 | |
| 662 | bool PrintTableType(const ResTable_type* chunk) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 663 | printer_->Print(StringPrintf(" id: 0x%02x", android::util::DeviceToHost32(chunk->id))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 664 | printer_->Print(StringPrintf( |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 665 | " name: %s", |
| 666 | android::util::GetString(type_pool_, android::util::DeviceToHost32(chunk->id) - 1) |
| 667 | .c_str())); |
| 668 | printer_->Print(StringPrintf(" flags: 0x%02x", android::util::DeviceToHost32(chunk->flags))); |
| 669 | printer_->Print( |
| 670 | StringPrintf(" entryCount: %u", android::util::DeviceToHost32(chunk->entryCount))); |
| 671 | printer_->Print( |
| 672 | StringPrintf(" entryStart: %u", android::util::DeviceToHost32(chunk->entriesStart))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 673 | |
| 674 | ConfigDescription config; |
| 675 | config.copyFromDtoH(chunk->config); |
| 676 | printer_->Print(StringPrintf(" config: %s\n", config.to_string().c_str())); |
| 677 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 678 | const ResourceType* type = ParseResourceType( |
| 679 | android::util::GetString(type_pool_, android::util::DeviceToHost32(chunk->id) - 1)); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 680 | |
| 681 | printer_->Indent(); |
| 682 | |
| 683 | TypeVariant tv(chunk); |
| 684 | for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { |
| 685 | const ResTable_entry* entry = *it; |
| 686 | if (!entry) { |
| 687 | continue; |
| 688 | } |
| 689 | |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 690 | if (entry->is_complex()) { |
| 691 | printer_->Print("[ResTable_map_entry]"); |
| 692 | } else if (entry->is_compact()) { |
| 693 | printer_->Print("[ResTable_entry_compact]"); |
| 694 | } else { |
| 695 | printer_->Print("[ResTable_entry]"); |
| 696 | } |
| 697 | |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 698 | printer_->Print(StringPrintf(" id: 0x%04x", it.index())); |
| 699 | printer_->Print(StringPrintf( |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 700 | " name: %s", android::util::GetString(key_pool_, entry->key()).c_str())); |
| 701 | printer_->Print(StringPrintf(" keyIndex: %u", entry->key())); |
| 702 | printer_->Print(StringPrintf(" size: %zu", entry->size())); |
| 703 | printer_->Print(StringPrintf(" flags: 0x%04x", entry->flags())); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 704 | |
| 705 | printer_->Indent(); |
| 706 | |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 707 | if (auto map_entry = entry->map_entry()) { |
| 708 | uint32_t map_entry_count = android::util::DeviceToHost32(map_entry->count); |
| 709 | printer_->Print(StringPrintf(" count: 0x%04x", map_entry_count)); |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 710 | printer_->Print(StringPrintf(" parent: 0x%08x\n", |
| 711 | android::util::DeviceToHost32(map_entry->parent.ident))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 712 | |
| 713 | // Print the name and value mappings |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 714 | auto maps = (const ResTable_map*)((const uint8_t*)entry + entry->size()); |
| 715 | for (size_t i = 0; i < map_entry_count; i++) { |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 716 | PrintResValue(&(maps[i].value), config, type); |
| 717 | |
| 718 | printer_->Print(StringPrintf( |
| 719 | " name: %s name-id:%d\n", |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 720 | android::util::GetString(key_pool_, android::util::DeviceToHost32(maps[i].name.ident)) |
| 721 | .c_str(), |
| 722 | android::util::DeviceToHost32(maps[i].name.ident))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 723 | } |
| 724 | } else { |
| 725 | printer_->Print("\n"); |
| 726 | |
| 727 | // Print the value of the entry |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 728 | Res_value value = entry->value(); |
| 729 | PrintResValue(&value, config, type); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | printer_->Undent(); |
| 733 | } |
| 734 | |
| 735 | printer_->Undent(); |
| 736 | return true; |
| 737 | } |
| 738 | |
| 739 | void PrintStringPool(const ResStringPool_header* chunk) { |
| 740 | // Initialize the string pools |
| 741 | |
| 742 | ResStringPool* pool; |
| 743 | if (value_pool_.getError() == NO_INIT) { |
| 744 | pool = &value_pool_; |
| 745 | } else if (type_pool_.getError() == NO_INIT) { |
| 746 | pool = &type_pool_; |
| 747 | } else if (key_pool_.getError() == NO_INIT) { |
| 748 | pool = &key_pool_; |
| 749 | } else { |
| 750 | return; |
| 751 | } |
| 752 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 753 | pool->setTo(chunk, android::util::DeviceToHost32( |
| 754 | (reinterpret_cast<const ResChunk_header*>(chunk))->size)); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 755 | |
| 756 | printer_->Print("\n"); |
| 757 | |
| 758 | for (size_t i = 0; i < pool->size(); i++) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 759 | printer_->Print(StringPrintf("#%zd : %s\n", i, android::util::GetString(*pool, i).c_str())); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | bool PrintPackage(const ResTable_package* chunk) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 764 | printer_->Print(StringPrintf(" id: 0x%02x", android::util::DeviceToHost32(chunk->id))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 765 | |
| 766 | size_t len = strnlen16((const char16_t*)chunk->name, std::size(chunk->name)); |
| 767 | std::u16string package_name(len, u'\0'); |
| 768 | package_name.resize(len); |
| 769 | for (size_t i = 0; i < len; i++) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 770 | package_name[i] = android::util::DeviceToHost16(chunk->name[i]); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | printer_->Print(StringPrintf("name: %s", String8(package_name.c_str()).c_str())); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 774 | printer_->Print( |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 775 | StringPrintf(" typeStrings: %u", android::util::DeviceToHost32(chunk->typeStrings))); |
| 776 | printer_->Print( |
| 777 | StringPrintf(" lastPublicType: %u", android::util::DeviceToHost32(chunk->lastPublicType))); |
| 778 | printer_->Print( |
| 779 | StringPrintf(" keyStrings: %u", android::util::DeviceToHost32(chunk->keyStrings))); |
| 780 | printer_->Print( |
| 781 | StringPrintf(" lastPublicKey: %u", android::util::DeviceToHost32(chunk->lastPublicKey))); |
| 782 | printer_->Print( |
| 783 | StringPrintf(" typeIdOffset: %u\n", android::util::DeviceToHost32(chunk->typeIdOffset))); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 784 | |
| 785 | // Print the chunks contained within the table |
| 786 | printer_->Indent(); |
| 787 | bool success = PrintChunk( |
| 788 | ResChunkPullParser(GetChunkData(&chunk->header), GetChunkDataLen(&chunk->header))); |
| 789 | printer_->Undent(); |
| 790 | return success; |
| 791 | } |
| 792 | |
| 793 | bool PrintChunk(ResChunkPullParser&& parser) { |
| 794 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 795 | auto chunk = parser.chunk(); |
| 796 | PrintChunkHeader(chunk); |
| 797 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 798 | switch (android::util::DeviceToHost16(chunk->type)) { |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 799 | case RES_STRING_POOL_TYPE: |
| 800 | PrintStringPool(reinterpret_cast<const ResStringPool_header*>(chunk)); |
| 801 | break; |
| 802 | |
| 803 | case RES_TABLE_TYPE: |
| 804 | PrintTable(reinterpret_cast<const ResTable_header*>(chunk)); |
| 805 | break; |
| 806 | |
| 807 | case RES_TABLE_PACKAGE_TYPE: |
| 808 | type_pool_.uninit(); |
| 809 | key_pool_.uninit(); |
| 810 | PrintPackage(reinterpret_cast<const ResTable_package*>(chunk)); |
| 811 | break; |
| 812 | |
| 813 | case RES_TABLE_TYPE_TYPE: |
| 814 | PrintTableType(reinterpret_cast<const ResTable_type*>(chunk)); |
| 815 | break; |
| 816 | |
| 817 | default: |
| 818 | printer_->Print("\n"); |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 824 | diag_->Error(android::DiagMessage(source_) << "corrupt resource table: " << parser.error()); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 825 | return false; |
| 826 | } |
| 827 | |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | void Print() { |
| 832 | PrintChunk(ResChunkPullParser(data_, data_len_)); |
| 833 | printer_->Print("[End]\n"); |
| 834 | } |
| 835 | |
| 836 | private: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 837 | const android::Source source_; |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 838 | const void* data_; |
| 839 | const size_t data_len_; |
| 840 | Printer* printer_; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 841 | android::IDiagnostics* diag_; |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 842 | |
| 843 | // The standard value string pool for resource values. |
| 844 | ResStringPool value_pool_; |
| 845 | |
| 846 | // The string pool that holds the names of the types defined |
| 847 | // in this table. |
| 848 | ResStringPool type_pool_; |
| 849 | |
| 850 | // The string pool that holds the names of the entries defined |
| 851 | // in this table. |
| 852 | ResStringPool key_pool_; |
| 853 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 854 | android::StringPool out_pool_; |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 855 | }; |
| 856 | |
| 857 | } // namespace |
| 858 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 859 | void Debug::DumpChunks(const void* data, size_t len, Printer* printer, |
| 860 | android::IDiagnostics* diag) { |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 861 | ChunkPrinter chunk_printer(data, len, printer, diag); |
| 862 | chunk_printer.Print(); |
| 863 | } |
| 864 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 865 | } // namespace aapt |