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