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" |
| 18 | #include "ResourceTable.h" |
| 19 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/Util.h" |
| 21 | #include "ValueVisitor.h" |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 22 | |
| 23 | #include <algorithm> |
| 24 | #include <iostream> |
| 25 | #include <map> |
| 26 | #include <memory> |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 27 | #include <queue> |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 28 | #include <set> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace aapt { |
| 32 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 33 | class PrintVisitor : public ValueVisitor { |
| 34 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | using ValueVisitor::visit; |
| 36 | |
| 37 | void visit(Attribute* attr) override { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 38 | std::cout << "(attr) type="; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | attr->printMask(&std::cout); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 40 | static constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM | |
| 41 | android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | if (attr->typeMask & kMask) { |
| 43 | for (const auto& symbol : attr->symbols) { |
| 44 | std::cout << "\n " << symbol.symbol.name.value().entry; |
| 45 | if (symbol.symbol.id) { |
| 46 | std::cout << " (" << symbol.symbol.id.value() << ")"; |
| 47 | } |
| 48 | std::cout << " = " << symbol.value; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | void visit(Style* style) override { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 54 | std::cout << "(style)"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | if (style->parent) { |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 56 | const Reference& parentRef = style->parent.value(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 57 | std::cout << " parent="; |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 58 | if (parentRef.name) { |
| 59 | if (parentRef.privateReference) { |
| 60 | std::cout << "*"; |
| 61 | } |
| 62 | std::cout << parentRef.name.value() << " "; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 65 | if (parentRef.id) { |
| 66 | std::cout << parentRef.id.value(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | for (const auto& entry : style->entries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 71 | std::cout << "\n "; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | if (entry.key.name) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 73 | const ResourceName& name = entry.key.name.value(); |
| 74 | if (!name.package.empty()) { |
| 75 | std::cout << name.package << ":"; |
| 76 | } |
| 77 | std::cout << name.entry; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | if (entry.key.id) { |
| 81 | std::cout << "(" << entry.key.id.value() << ")"; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | std::cout << "=" << *entry.value; |
| 85 | } |
| 86 | } |
| 87 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | void visit(Array* array) override { |
| 89 | array->print(&std::cout); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | void visit(Plural* plural) override { |
| 93 | plural->print(&std::cout); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 96 | void visit(Styleable* styleable) override { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 97 | std::cout << "(styleable)"; |
| 98 | for (const auto& attr : styleable->entries) { |
| 99 | std::cout << "\n "; |
| 100 | if (attr.name) { |
| 101 | const ResourceName& name = attr.name.value(); |
| 102 | if (!name.package.empty()) { |
| 103 | std::cout << name.package << ":"; |
| 104 | } |
| 105 | std::cout << name.entry; |
| 106 | } |
| 107 | |
| 108 | if (attr.id) { |
| 109 | std::cout << "(" << attr.id.value() << ")"; |
| 110 | } |
| 111 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 114 | void visitItem(Item* item) override { |
| 115 | item->print(&std::cout); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 116 | } |
| 117 | }; |
| 118 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 119 | void Debug::printTable(ResourceTable* table, const DebugPrintTableOptions& options) { |
| 120 | PrintVisitor visitor; |
| 121 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | for (auto& package : table->packages) { |
| 123 | std::cout << "Package name=" << package->name; |
| 124 | if (package->id) { |
| 125 | std::cout << " id=" << std::hex << (int) package->id.value() << std::dec; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 126 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 127 | std::cout << std::endl; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 128 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 129 | for (const auto& type : package->types) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 130 | std::cout << "\n type " << type->type; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | if (type->id) { |
| 132 | std::cout << " id=" << std::hex << (int) type->id.value() << std::dec; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 133 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | std::cout << " entryCount=" << type->entries.size() << std::endl; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 135 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 136 | std::vector<const ResourceEntry*> sortedEntries; |
| 137 | for (const auto& entry : type->entries) { |
| 138 | auto iter = std::lower_bound(sortedEntries.begin(), sortedEntries.end(), entry.get(), |
| 139 | [](const ResourceEntry* a, const ResourceEntry* b) -> bool { |
| 140 | if (a->id && b->id) { |
| 141 | return a->id.value() < b->id.value(); |
| 142 | } else if (a->id) { |
| 143 | return true; |
| 144 | } else { |
| 145 | return false; |
| 146 | } |
| 147 | }); |
| 148 | sortedEntries.insert(iter, entry.get()); |
| 149 | } |
| 150 | |
| 151 | for (const ResourceEntry* entry : sortedEntries) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 152 | ResourceId id(package->id ? package->id.value() : uint8_t(0), |
| 153 | type->id ? type->id.value() : uint8_t(0), |
| 154 | entry->id ? entry->id.value() : uint16_t(0)); |
| 155 | ResourceName name(package->name, type->type, entry->name); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | std::cout << " spec resource " << id << " " << name; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 158 | switch (entry->symbolStatus.state) { |
| 159 | case SymbolState::kPublic: std::cout << " PUBLIC"; break; |
| 160 | case SymbolState::kPrivate: std::cout << " _PRIVATE_"; break; |
| 161 | default: break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 163 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 164 | std::cout << std::endl; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 166 | for (const auto& value : entry->values) { |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 167 | std::cout << " (" << value->config << ") "; |
| 168 | value->value->accept(&visitor); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 169 | if (options.showSources && !value->value->getSource().path.empty()) { |
| 170 | std::cout << " src=" << value->value->getSource(); |
| 171 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | std::cout << std::endl; |
| 173 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static size_t getNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) { |
| 180 | auto iter = std::lower_bound(names.begin(), names.end(), name); |
| 181 | assert(iter != names.end() && *iter == name); |
| 182 | return std::distance(names.begin(), iter); |
| 183 | } |
| 184 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 | void Debug::printStyleGraph(ResourceTable* table, const ResourceName& targetStyle) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 186 | std::map<ResourceName, std::set<ResourceName>> graph; |
| 187 | |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 188 | std::queue<ResourceName> stylesToVisit; |
| 189 | stylesToVisit.push(targetStyle); |
| 190 | for (; !stylesToVisit.empty(); stylesToVisit.pop()) { |
| 191 | const ResourceName& styleName = stylesToVisit.front(); |
| 192 | std::set<ResourceName>& parents = graph[styleName]; |
| 193 | if (!parents.empty()) { |
| 194 | // We've already visited this style. |
| 195 | continue; |
| 196 | } |
| 197 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | Maybe<ResourceTable::SearchResult> result = table->findResource(styleName); |
| 199 | if (result) { |
| 200 | ResourceEntry* entry = result.value().entry; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 201 | for (const auto& value : entry->values) { |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 202 | if (Style* style = valueCast<Style>(value->value.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 203 | if (style->parent && style->parent.value().name) { |
| 204 | parents.insert(style->parent.value().name.value()); |
| 205 | stylesToVisit.push(style->parent.value().name.value()); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 206 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 207 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 212 | std::vector<ResourceName> names; |
| 213 | for (const auto& entry : graph) { |
| 214 | names.push_back(entry.first); |
| 215 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 216 | |
| 217 | std::cout << "digraph styles {\n"; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 218 | for (const auto& name : names) { |
| 219 | std::cout << " node_" << getNodeIndex(names, name) |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 220 | << " [label=\"" << name << "\"];\n"; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | for (const auto& entry : graph) { |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 224 | const ResourceName& styleName = entry.first; |
| 225 | size_t styleNodeIndex = getNodeIndex(names, styleName); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 226 | |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 227 | for (const auto& parentName : entry.second) { |
| 228 | std::cout << " node_" << styleNodeIndex << " -> " |
| 229 | << "node_" << getNodeIndex(names, parentName) << ";\n"; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | std::cout << "}" << std::endl; |
| 234 | } |
| 235 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 236 | void Debug::dumpHex(const void* data, size_t len) { |
| 237 | const uint8_t* d = (const uint8_t*) data; |
| 238 | for (size_t i = 0; i < len; i++) { |
| 239 | std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t) d[i] << " "; |
| 240 | if (i % 8 == 7) { |
| 241 | std::cerr << "\n"; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (len - 1 % 8 != 7) { |
| 246 | std::cerr << std::endl; |
| 247 | } |
| 248 | } |
| 249 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 250 | namespace { |
| 251 | |
| 252 | class XmlPrinter : public xml::Visitor { |
| 253 | public: |
| 254 | using xml::Visitor::visit; |
| 255 | |
| 256 | void visit(xml::Element* el) override { |
| 257 | std::cerr << mPrefix; |
| 258 | std::cerr << "E: "; |
| 259 | if (!el->namespaceUri.empty()) { |
| 260 | std::cerr << el->namespaceUri << ":"; |
| 261 | } |
| 262 | std::cerr << el->name << " (line=" << el->lineNumber << ")\n"; |
| 263 | |
| 264 | for (const xml::Attribute& attr : el->attributes) { |
| 265 | std::cerr << mPrefix << " A: "; |
| 266 | if (!attr.namespaceUri.empty()) { |
| 267 | std::cerr << attr.namespaceUri << ":"; |
| 268 | } |
| 269 | std::cerr << attr.name << "=" << attr.value << "\n"; |
| 270 | } |
| 271 | |
| 272 | const size_t previousSize = mPrefix.size(); |
| 273 | mPrefix += " "; |
| 274 | xml::Visitor::visit(el); |
| 275 | mPrefix.resize(previousSize); |
| 276 | } |
| 277 | |
| 278 | void visit(xml::Namespace* ns) override { |
| 279 | std::cerr << mPrefix; |
| 280 | std::cerr << "N: " << ns->namespacePrefix << "=" << ns->namespaceUri |
| 281 | << " (line=" << ns->lineNumber << ")\n"; |
| 282 | |
| 283 | const size_t previousSize = mPrefix.size(); |
| 284 | mPrefix += " "; |
| 285 | xml::Visitor::visit(ns); |
| 286 | mPrefix.resize(previousSize); |
| 287 | } |
| 288 | |
| 289 | void visit(xml::Text* text) override { |
| 290 | std::cerr << mPrefix; |
| 291 | std::cerr << "T: '" << text->text << "'\n"; |
| 292 | } |
| 293 | |
| 294 | private: |
| 295 | std::string mPrefix; |
| 296 | }; |
| 297 | |
| 298 | } // namespace |
| 299 | |
| 300 | void Debug::dumpXml(xml::XmlResource* doc) { |
| 301 | XmlPrinter printer; |
| 302 | doc->root->accept(&printer); |
| 303 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 304 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 305 | } // namespace aapt |