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