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 | |
| 19 | #include <algorithm> |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <memory> |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 22 | #include <queue> |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 23 | #include <set> |
| 24 | #include <vector> |
| 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "android-base/logging.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | |
| 29 | #include "ResourceTable.h" |
| 30 | #include "ResourceValues.h" |
| 31 | #include "ValueVisitor.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 32 | #include "text/Printer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | #include "util/Util.h" |
| 34 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 35 | using ::aapt::text::Printer; |
| 36 | using ::android::StringPiece; |
| 37 | using ::android::base::StringPrintf; |
| 38 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 39 | namespace aapt { |
| 40 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 41 | namespace { |
| 42 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 43 | class ValueHeadlinePrinter : public ConstValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | public: |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 45 | using ConstValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 47 | explicit ValueHeadlinePrinter(const std::string& package, Printer* printer) |
| 48 | : package_(package), printer_(printer) { |
| 49 | } |
| 50 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 51 | void Visit(const Attribute* attr) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 52 | printer_->Print("(attr) type="); |
| 53 | printer_->Print(attr->MaskString()); |
| 54 | if (!attr->symbols.empty()) { |
| 55 | printer_->Print(StringPrintf(" size=%zd", attr->symbols.size())); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void Visit(const Style* style) override { |
| 60 | printer_->Print(StringPrintf("(style) size=%zd", style->entries.size())); |
| 61 | if (style->parent) { |
| 62 | printer_->Print(" parent="); |
| 63 | |
| 64 | const Reference& parent_ref = style->parent.value(); |
| 65 | if (parent_ref.name) { |
| 66 | if (parent_ref.private_reference) { |
| 67 | printer_->Print("*"); |
| 68 | } |
| 69 | |
| 70 | const ResourceName& parent_name = parent_ref.name.value(); |
| 71 | if (package_ != parent_name.package) { |
| 72 | printer_->Print(parent_name.package); |
| 73 | printer_->Print(":"); |
| 74 | } |
| 75 | printer_->Print(to_string(parent_name.type)); |
| 76 | printer_->Print("/"); |
| 77 | printer_->Print(parent_name.entry); |
| 78 | if (parent_ref.id) { |
| 79 | printer_->Print(" ("); |
| 80 | printer_->Print(parent_ref.id.value().to_string()); |
| 81 | printer_->Print(")"); |
| 82 | } |
| 83 | } else if (parent_ref.id) { |
| 84 | printer_->Print(parent_ref.id.value().to_string()); |
| 85 | } else { |
| 86 | printer_->Print("???"); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void Visit(const Array* array) override { |
| 92 | printer_->Print(StringPrintf("(array) size=%zd", array->elements.size())); |
| 93 | } |
| 94 | |
| 95 | void Visit(const Plural* plural) override { |
| 96 | size_t count = std::count_if(plural->values.begin(), plural->values.end(), |
| 97 | [](const std::unique_ptr<Item>& v) { return v != nullptr; }); |
| 98 | printer_->Print(StringPrintf("(plurals) size=%zd", count)); |
| 99 | } |
| 100 | |
| 101 | void Visit(const Styleable* styleable) override { |
| 102 | printer_->Println(StringPrintf("(styleable) size=%zd", styleable->entries.size())); |
| 103 | } |
| 104 | |
| 105 | void VisitItem(const Item* item) override { |
| 106 | // Pretty much guaranteed to be one line. |
| 107 | if (const Reference* ref = ValueCast<Reference>(item)) { |
| 108 | // Special case Reference so that we can print local resources without a package name. |
| 109 | ref->PrettyPrint(package_, printer_); |
| 110 | } else { |
| 111 | item->PrettyPrint(printer_); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | std::string package_; |
| 117 | Printer* printer_; |
| 118 | }; |
| 119 | |
| 120 | class ValueBodyPrinter : public ConstValueVisitor { |
| 121 | public: |
| 122 | using ConstValueVisitor::Visit; |
| 123 | |
| 124 | explicit ValueBodyPrinter(const std::string& package, Printer* printer) |
| 125 | : package_(package), printer_(printer) { |
| 126 | } |
| 127 | |
| 128 | void Visit(const Attribute* attr) override { |
| 129 | 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] | 130 | if (attr->type_mask & kMask) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | for (const auto& symbol : attr->symbols) { |
Ryan Mitchell | 0c0cedf | 2019-04-15 16:47:58 -0700 | [diff] [blame] | 132 | if (symbol.symbol.name) { |
| 133 | printer_->Print(symbol.symbol.name.value().entry); |
| 134 | |
| 135 | if (symbol.symbol.id) { |
| 136 | printer_->Print("("); |
| 137 | printer_->Print(symbol.symbol.id.value().to_string()); |
| 138 | printer_->Print(")"); |
| 139 | } |
| 140 | } else if (symbol.symbol.id) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 141 | printer_->Print(symbol.symbol.id.value().to_string()); |
Ryan Mitchell | 0c0cedf | 2019-04-15 16:47:58 -0700 | [diff] [blame] | 142 | } else { |
| 143 | printer_->Print("???"); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 144 | } |
Ryan Mitchell | 0c0cedf | 2019-04-15 16:47:58 -0700 | [diff] [blame] | 145 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 146 | printer_->Println(StringPrintf("=0x%08x", symbol.value)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 148 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 151 | void Visit(const Style* style) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | for (const auto& entry : style->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | if (entry.key.name) { |
| 154 | const ResourceName& name = entry.key.name.value(); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 155 | if (!name.package.empty() && name.package != package_) { |
| 156 | printer_->Print(name.package); |
| 157 | printer_->Print(":"); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 158 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 159 | printer_->Print(name.entry); |
| 160 | |
| 161 | if (entry.key.id) { |
| 162 | printer_->Print("("); |
| 163 | printer_->Print(entry.key.id.value().to_string()); |
| 164 | printer_->Print(")"); |
| 165 | } |
| 166 | } else if (entry.key.id) { |
| 167 | printer_->Print(entry.key.id.value().to_string()); |
| 168 | } else { |
| 169 | printer_->Print("???"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 172 | printer_->Print("="); |
| 173 | PrintItem(*entry.value); |
| 174 | printer_->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 175 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 178 | void Visit(const Array* array) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 179 | const size_t count = array->elements.size(); |
| 180 | printer_->Print("["); |
Donald Chai | 42fe2b2 | 2019-05-07 20:03:27 -0700 | [diff] [blame] | 181 | for (size_t i = 0u; i < count; i++) { |
| 182 | if (i != 0u && i % 4u == 0u) { |
| 183 | printer_->Println(); |
| 184 | printer_->Print(" "); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 185 | } |
Donald Chai | 42fe2b2 | 2019-05-07 20:03:27 -0700 | [diff] [blame] | 186 | PrintItem(*array->elements[i]); |
| 187 | if (i != count - 1) { |
| 188 | printer_->Print(", "); |
| 189 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 190 | } |
Donald Chai | 42fe2b2 | 2019-05-07 20:03:27 -0700 | [diff] [blame] | 191 | printer_->Println("]"); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 192 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 193 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 194 | void Visit(const Plural* plural) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 195 | constexpr std::array<const char*, Plural::Count> kPluralNames = { |
| 196 | {"zero", "one", "two", "few", "many", "other"}}; |
| 197 | |
| 198 | for (size_t i = 0; i < Plural::Count; i++) { |
| 199 | if (plural->values[i] != nullptr) { |
| 200 | printer_->Print(StringPrintf("%s=", kPluralNames[i])); |
| 201 | PrintItem(*plural->values[i]); |
| 202 | printer_->Println(); |
| 203 | } |
| 204 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 205 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 207 | void Visit(const Styleable* styleable) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | if (attr.name) { |
| 210 | const ResourceName& name = attr.name.value(); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 211 | if (!name.package.empty() && name.package != package_) { |
| 212 | printer_->Print(name.package); |
| 213 | printer_->Print(":"); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 214 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 215 | printer_->Print(name.entry); |
| 216 | |
| 217 | if (attr.id) { |
| 218 | printer_->Print("("); |
| 219 | printer_->Print(attr.id.value().to_string()); |
| 220 | printer_->Print(")"); |
| 221 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 223 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 224 | if (attr.id) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 225 | printer_->Print(attr.id.value().to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 226 | } |
Adam Lesinski | 5a217b0 | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 227 | printer_->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 228 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 231 | void VisitItem(const Item* item) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 232 | // Intentionally left empty, we already printed the Items. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 233 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 234 | |
| 235 | private: |
| 236 | void PrintItem(const Item& item) { |
| 237 | if (const Reference* ref = ValueCast<Reference>(&item)) { |
| 238 | // Special case Reference so that we can print local resources without a package name. |
| 239 | ref->PrettyPrint(package_, printer_); |
| 240 | } else { |
| 241 | item.PrettyPrint(printer_); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | std::string package_; |
| 246 | Printer* printer_; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 249 | } // namespace |
| 250 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 251 | void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options, |
| 252 | Printer* printer) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 253 | for (const auto& package : table.packages) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 254 | ValueHeadlinePrinter headline_printer(package->name, printer); |
| 255 | ValueBodyPrinter body_printer(package->name, printer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 257 | printer->Print("Package name="); |
| 258 | printer->Print(package->name); |
| 259 | if (package->id) { |
| 260 | printer->Print(StringPrintf(" id=%02x", package->id.value())); |
| 261 | } |
| 262 | printer->Println(); |
| 263 | |
| 264 | printer->Indent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | for (const auto& type : package->types) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 266 | printer->Print("type "); |
| 267 | printer->Print(to_string(type->type)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 268 | if (type->id) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 269 | printer->Print(StringPrintf(" id=%02x", type->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 271 | printer->Println(StringPrintf(" entryCount=%zd", type->entries.size())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 272 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | std::vector<const ResourceEntry*> sorted_entries; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 274 | for (const auto& entry : type->entries) { |
| 275 | auto iter = std::lower_bound( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | sorted_entries.begin(), sorted_entries.end(), entry.get(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 277 | [](const ResourceEntry* a, const ResourceEntry* b) -> bool { |
| 278 | if (a->id && b->id) { |
| 279 | return a->id.value() < b->id.value(); |
| 280 | } else if (a->id) { |
| 281 | return true; |
| 282 | } else { |
| 283 | return false; |
| 284 | } |
| 285 | }); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 286 | sorted_entries.insert(iter, entry.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 289 | printer->Indent(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | for (const ResourceEntry* entry : sorted_entries) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 291 | const ResourceId id(package->id.value_or_default(0), type->id.value_or_default(0), |
| 292 | entry->id.value_or_default(0)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 294 | printer->Print("resource "); |
| 295 | printer->Print(id.to_string()); |
| 296 | printer->Print(" "); |
| 297 | |
| 298 | // Write the name without the package (this is obvious and too verbose). |
| 299 | printer->Print(to_string(type->type)); |
| 300 | printer->Print("/"); |
| 301 | printer->Print(entry->name); |
| 302 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 303 | switch (entry->visibility.level) { |
| 304 | case Visibility::Level::kPublic: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 305 | printer->Print(" PUBLIC"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | break; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 307 | case Visibility::Level::kPrivate: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 308 | printer->Print(" _PRIVATE_"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 309 | break; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 310 | case Visibility::Level::kUndefined: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 311 | // Print nothing. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 312 | break; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 313 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 315 | printer->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 316 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 317 | if (options.show_values) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 318 | printer->Indent(); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 319 | for (const auto& value : entry->values) { |
| 320 | printer->Print("("); |
| 321 | printer->Print(value->config.to_string()); |
| 322 | printer->Print(") "); |
| 323 | value->value->Accept(&headline_printer); |
| 324 | if (options.show_sources && !value->value->GetSource().path.empty()) { |
| 325 | printer->Print(" src="); |
| 326 | printer->Print(value->value->GetSource().to_string()); |
| 327 | } |
| 328 | printer->Println(); |
| 329 | printer->Indent(); |
| 330 | value->value->Accept(&body_printer); |
| 331 | printer->Undent(); |
| 332 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 333 | printer->Undent(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 334 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 335 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 336 | printer->Undent(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 337 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 338 | printer->Undent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 339 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 342 | static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 343 | auto iter = std::lower_bound(names.begin(), names.end(), name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 344 | CHECK(iter != names.end()); |
| 345 | CHECK(*iter == name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 346 | return std::distance(names.begin(), iter); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 349 | void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | std::map<ResourceName, std::set<ResourceName>> graph; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 351 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | std::queue<ResourceName> styles_to_visit; |
| 353 | styles_to_visit.push(target_style); |
| 354 | for (; !styles_to_visit.empty(); styles_to_visit.pop()) { |
| 355 | const ResourceName& style_name = styles_to_visit.front(); |
| 356 | std::set<ResourceName>& parents = graph[style_name]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 357 | if (!parents.empty()) { |
| 358 | // We've already visited this style. |
| 359 | continue; |
| 360 | } |
| 361 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 362 | Maybe<ResourceTable::SearchResult> result = table->FindResource(style_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | if (result) { |
| 364 | ResourceEntry* entry = result.value().entry; |
| 365 | for (const auto& value : entry->values) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 366 | if (Style* style = ValueCast<Style>(value->value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | if (style->parent && style->parent.value().name) { |
| 368 | parents.insert(style->parent.value().name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | styles_to_visit.push(style->parent.value().name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | } |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 371 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 373 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 374 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 375 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 376 | std::vector<ResourceName> names; |
| 377 | for (const auto& entry : graph) { |
| 378 | names.push_back(entry.first); |
| 379 | } |
| 380 | |
| 381 | std::cout << "digraph styles {\n"; |
| 382 | for (const auto& name : names) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 383 | std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | for (const auto& entry : graph) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 387 | const ResourceName& style_name = entry.first; |
| 388 | size_t style_node_index = GetNodeIndex(names, style_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 390 | for (const auto& parent_name : entry.second) { |
| 391 | std::cout << " node_" << style_node_index << " -> " |
| 392 | << "node_" << GetNodeIndex(names, parent_name) << ";\n"; |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 393 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 394 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 395 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | std::cout << "}" << std::endl; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 399 | void Debug::DumpHex(const void* data, size_t len) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 400 | const uint8_t* d = (const uint8_t*)data; |
| 401 | for (size_t i = 0; i < len; i++) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 402 | 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] | 403 | if (i % 8 == 7) { |
| 404 | std::cerr << "\n"; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 405 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 407 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 408 | if (len - 1 % 8 != 7) { |
| 409 | std::cerr << std::endl; |
| 410 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 413 | void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) { |
| 414 | using namespace android; |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 415 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 416 | if (pool->getError() == NO_INIT) { |
| 417 | printer->Print("String pool is unitialized.\n"); |
| 418 | return; |
| 419 | } else if (pool->getError() != NO_ERROR) { |
| 420 | printer->Print("String pool is corrupt/invalid.\n"); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | SortedVector<const void*> uniqueStrings; |
| 425 | const size_t N = pool->size(); |
| 426 | for (size_t i=0; i<N; i++) { |
| 427 | size_t len; |
| 428 | if (pool->isUTF8()) { |
| 429 | uniqueStrings.add(pool->string8At(i, &len)); |
| 430 | } else { |
| 431 | uniqueStrings.add(pool->stringAt(i, &len)); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | printer->Print(StringPrintf("String pool of %zd unique %s %s strings, %zd entries and %zd styles " |
| 436 | "using %zd bytes:\n", uniqueStrings.size(), |
| 437 | pool->isUTF8() ? "UTF-8" : "UTF-16", |
| 438 | pool->isSorted() ? "sorted" : "non-sorted", N, pool->styleCount(), |
| 439 | pool->bytes())); |
| 440 | |
| 441 | const size_t NS = pool->size(); |
| 442 | for (size_t s=0; s<NS; s++) { |
| 443 | String8 str = pool->string8ObjectAt(s); |
Ryan Mitchell | 90b7a08 | 2019-02-15 17:39:58 +0000 | [diff] [blame] | 444 | printer->Print(StringPrintf("String #%zd : %s\n", s, str.string())); |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 448 | namespace { |
| 449 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 450 | class XmlPrinter : public xml::ConstVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 451 | public: |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 452 | using xml::ConstVisitor::Visit; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 453 | |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 454 | explicit XmlPrinter(Printer* printer) : printer_(printer) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 457 | void Visit(const xml::Element* el) override { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 458 | for (const xml::NamespaceDecl& decl : el->namespace_decls) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 459 | printer_->Println(StringPrintf("N: %s=%s (line=%zu)", decl.prefix.c_str(), decl.uri.c_str(), |
| 460 | decl.line_number)); |
| 461 | printer_->Indent(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 464 | printer_->Print("E: "); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 465 | if (!el->namespace_uri.empty()) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 466 | printer_->Print(el->namespace_uri); |
| 467 | printer_->Print(":"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 468 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 469 | printer_->Println(StringPrintf("%s (line=%zu)", el->name.c_str(), el->line_number)); |
| 470 | printer_->Indent(); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 471 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 472 | for (const xml::Attribute& attr : el->attributes) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 473 | printer_->Print("A: "); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 474 | if (!attr.namespace_uri.empty()) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 475 | printer_->Print(attr.namespace_uri); |
| 476 | printer_->Print(":"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 477 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 478 | printer_->Print(attr.name); |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 479 | |
| 480 | if (attr.compiled_attribute) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 481 | printer_->Print("("); |
| 482 | printer_->Print( |
| 483 | attr.compiled_attribute.value().id.value_or_default(ResourceId(0)).to_string()); |
| 484 | printer_->Print(")"); |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 485 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 486 | printer_->Print("="); |
Shane Farmer | 6ed4061 | 2017-09-06 10:00:07 -0700 | [diff] [blame] | 487 | if (attr.compiled_value != nullptr) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 488 | attr.compiled_value->PrettyPrint(printer_); |
Shane Farmer | 6ed4061 | 2017-09-06 10:00:07 -0700 | [diff] [blame] | 489 | } else { |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 490 | printer_->Print("\""); |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 491 | printer_->Print(attr.value); |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 492 | printer_->Print("\""); |
| 493 | } |
| 494 | |
| 495 | if (!attr.value.empty()) { |
| 496 | printer_->Print(" (Raw: \""); |
| 497 | printer_->Print(attr.value); |
| 498 | printer_->Print("\")"); |
Shane Farmer | 6ed4061 | 2017-09-06 10:00:07 -0700 | [diff] [blame] | 499 | } |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 500 | printer_->Println(); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 503 | printer_->Indent(); |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 504 | xml::ConstVisitor::Visit(el); |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 505 | printer_->Undent(); |
| 506 | printer_->Undent(); |
| 507 | |
| 508 | for (size_t i = 0; i < el->namespace_decls.size(); i++) { |
| 509 | printer_->Undent(); |
| 510 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 511 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 512 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 513 | void Visit(const xml::Text* text) override { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 514 | printer_->Println(StringPrintf("T: '%s'", text->text.c_str())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | private: |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 518 | Printer* printer_; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 519 | }; |
| 520 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 521 | } // namespace |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 522 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 523 | void Debug::DumpXml(const xml::XmlResource& doc, Printer* printer) { |
| 524 | XmlPrinter xml_visitor(printer); |
| 525 | doc.root->Accept(&xml_visitor); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 526 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 527 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 528 | } // namespace aapt |