David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <errno.h> |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 18 | #include <inttypes.h> |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <iostream> |
| 24 | #include <memory> |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 25 | #include <vector> |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 26 | |
| 27 | #include "android-base/stringprintf.h" |
| 28 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include "base/logging.h" // For InitLogging. |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 30 | #include "base/stringpiece.h" |
| 31 | |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 32 | #include "dexlayout.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 33 | #include "dex/dex_file.h" |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 34 | #include "dex_ir.h" |
| 35 | #include "dex_ir_builder.h" |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 36 | #ifdef ART_TARGET_ANDROID |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 37 | #include "pagemap/pagemap.h" |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 38 | #endif |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 39 | #include "vdex_file.h" |
| 40 | |
| 41 | namespace art { |
| 42 | |
| 43 | using android::base::StringPrintf; |
| 44 | |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 45 | static bool g_verbose = false; |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 46 | |
| 47 | // The width needed to print a file page offset (32-bit). |
| 48 | static constexpr int kPageCountWidth = |
| 49 | static_cast<int>(std::numeric_limits<uint32_t>::digits10); |
| 50 | // Display the sections. |
| 51 | static constexpr char kSectionHeader[] = "Section name"; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 52 | |
| 53 | struct DexSectionInfo { |
| 54 | public: |
| 55 | std::string name; |
| 56 | char letter; |
| 57 | }; |
| 58 | |
| 59 | static const std::map<uint16_t, DexSectionInfo> kDexSectionInfoMap = { |
| 60 | { DexFile::kDexTypeHeaderItem, { "Header", 'H' } }, |
| 61 | { DexFile::kDexTypeStringIdItem, { "StringId", 'S' } }, |
| 62 | { DexFile::kDexTypeTypeIdItem, { "TypeId", 'T' } }, |
| 63 | { DexFile::kDexTypeProtoIdItem, { "ProtoId", 'P' } }, |
| 64 | { DexFile::kDexTypeFieldIdItem, { "FieldId", 'F' } }, |
| 65 | { DexFile::kDexTypeMethodIdItem, { "MethodId", 'M' } }, |
| 66 | { DexFile::kDexTypeClassDefItem, { "ClassDef", 'C' } }, |
| 67 | { DexFile::kDexTypeCallSiteIdItem, { "CallSiteId", 'z' } }, |
| 68 | { DexFile::kDexTypeMethodHandleItem, { "MethodHandle", 'Z' } }, |
| 69 | { DexFile::kDexTypeMapList, { "TypeMap", 'L' } }, |
| 70 | { DexFile::kDexTypeTypeList, { "TypeList", 't' } }, |
| 71 | { DexFile::kDexTypeAnnotationSetRefList, { "AnnotationSetReferenceItem", '1' } }, |
| 72 | { DexFile::kDexTypeAnnotationSetItem, { "AnnotationSetItem", '2' } }, |
| 73 | { DexFile::kDexTypeClassDataItem, { "ClassData", 'c' } }, |
| 74 | { DexFile::kDexTypeCodeItem, { "CodeItem", 'X' } }, |
| 75 | { DexFile::kDexTypeStringDataItem, { "StringData", 's' } }, |
| 76 | { DexFile::kDexTypeDebugInfoItem, { "DebugInfo", 'D' } }, |
| 77 | { DexFile::kDexTypeAnnotationItem, { "AnnotationItem", '3' } }, |
| 78 | { DexFile::kDexTypeEncodedArrayItem, { "EncodedArrayItem", 'E' } }, |
| 79 | { DexFile::kDexTypeAnnotationsDirectoryItem, { "AnnotationsDirectoryItem", '4' } } |
| 80 | }; |
| 81 | |
| 82 | class PageCount { |
| 83 | public: |
| 84 | PageCount() { |
| 85 | for (auto it = kDexSectionInfoMap.begin(); it != kDexSectionInfoMap.end(); ++it) { |
| 86 | map_[it->first] = 0; |
| 87 | } |
| 88 | } |
| 89 | void Increment(uint16_t type) { |
| 90 | map_[type]++; |
| 91 | } |
| 92 | size_t Get(uint16_t type) const { |
| 93 | return map_.at(type); |
| 94 | } |
| 95 | private: |
| 96 | std::map<uint16_t, size_t> map_; |
| 97 | DISALLOW_COPY_AND_ASSIGN(PageCount); |
| 98 | }; |
| 99 | |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 100 | class Printer { |
| 101 | public: |
| 102 | Printer() : section_header_width_(ComputeHeaderWidth()) { |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 103 | } |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 104 | |
| 105 | void PrintHeader() const { |
| 106 | std::cout << StringPrintf("%-*s %*s %*s %% of %% of", |
| 107 | section_header_width_, |
| 108 | kSectionHeader, |
| 109 | kPageCountWidth, |
| 110 | "resident", |
| 111 | kPageCountWidth, |
| 112 | "total" |
| 113 | ) |
| 114 | << std::endl; |
| 115 | std::cout << StringPrintf("%-*s %*s %*s sect. total", |
| 116 | section_header_width_, |
| 117 | "", |
| 118 | kPageCountWidth, |
| 119 | "pages", |
| 120 | kPageCountWidth, |
| 121 | "pages") |
| 122 | << std::endl; |
| 123 | } |
| 124 | |
| 125 | void PrintOne(const char* name, |
| 126 | size_t resident, |
| 127 | size_t mapped, |
| 128 | double percent_of_section, |
| 129 | double percent_of_total) const { |
| 130 | // 6.2 is sufficient to print 0-100% with two decimal places of accuracy. |
| 131 | std::cout << StringPrintf("%-*s %*zd %*zd %6.2f %6.2f", |
| 132 | section_header_width_, |
| 133 | name, |
| 134 | kPageCountWidth, |
| 135 | resident, |
| 136 | kPageCountWidth, |
| 137 | mapped, |
| 138 | percent_of_section, |
| 139 | percent_of_total) |
| 140 | << std::endl; |
| 141 | } |
| 142 | |
| 143 | void PrintSkipLine() const { std::cout << std::endl; } |
| 144 | |
| 145 | // Computes the width of the section header column in the table (for fixed formatting). |
| 146 | static int ComputeHeaderWidth() { |
| 147 | int header_width = 0; |
| 148 | for (const auto& pair : kDexSectionInfoMap) { |
| 149 | const DexSectionInfo& section_info = pair.second; |
| 150 | header_width = std::max(header_width, static_cast<int>(section_info.name.length())); |
| 151 | } |
| 152 | return header_width; |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | const int section_header_width_; |
| 157 | }; |
| 158 | |
| 159 | static void PrintLetterKey() { |
| 160 | std::cout << "L pagetype" << std::endl; |
| 161 | for (const auto& pair : kDexSectionInfoMap) { |
| 162 | const DexSectionInfo& section_info = pair.second; |
| 163 | std::cout << section_info.letter << " " << section_info.name.c_str() << std::endl; |
| 164 | } |
| 165 | std::cout << "* (Executable page resident)" << std::endl; |
| 166 | std::cout << ". (Mapped page not resident)" << std::endl; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 167 | } |
| 168 | |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 169 | #ifdef ART_TARGET_ANDROID |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 170 | static char PageTypeChar(uint16_t type) { |
| 171 | if (kDexSectionInfoMap.find(type) == kDexSectionInfoMap.end()) { |
| 172 | return '-'; |
| 173 | } |
| 174 | return kDexSectionInfoMap.find(type)->second.letter; |
| 175 | } |
| 176 | |
| 177 | static uint16_t FindSectionTypeForPage(size_t page, |
| 178 | const std::vector<dex_ir::DexFileSection>& sections) { |
| 179 | for (const auto& section : sections) { |
| 180 | size_t first_page_of_section = section.offset / kPageSize; |
| 181 | // Only consider non-empty sections. |
| 182 | if (section.size == 0) { |
| 183 | continue; |
| 184 | } |
| 185 | // Attribute the page to the highest-offset section that starts before the page. |
| 186 | if (first_page_of_section <= page) { |
| 187 | return section.type; |
| 188 | } |
| 189 | } |
| 190 | // If there's no non-zero sized section with an offset below offset we're looking for, it |
| 191 | // must be the header. |
| 192 | return DexFile::kDexTypeHeaderItem; |
| 193 | } |
| 194 | |
| 195 | static void ProcessPageMap(uint64_t* pagemap, |
| 196 | size_t start, |
| 197 | size_t end, |
| 198 | const std::vector<dex_ir::DexFileSection>& sections, |
| 199 | PageCount* page_counts) { |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 200 | static constexpr size_t kLineLength = 32; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 201 | for (size_t page = start; page < end; ++page) { |
| 202 | char type_char = '.'; |
| 203 | if (PM_PAGEMAP_PRESENT(pagemap[page])) { |
David Sehr | 093a6fb | 2017-05-09 15:41:09 -0700 | [diff] [blame] | 204 | const size_t dex_page_offset = page - start; |
| 205 | uint16_t type = FindSectionTypeForPage(dex_page_offset, sections); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 206 | page_counts->Increment(type); |
| 207 | type_char = PageTypeChar(type); |
| 208 | } |
| 209 | if (g_verbose) { |
| 210 | std::cout << type_char; |
| 211 | if ((page - start) % kLineLength == kLineLength - 1) { |
| 212 | std::cout << std::endl; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | if (g_verbose) { |
| 217 | if ((end - start) % kLineLength != 0) { |
| 218 | std::cout << std::endl; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static void DisplayDexStatistics(size_t start, |
| 224 | size_t end, |
| 225 | const PageCount& resident_pages, |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 226 | const std::vector<dex_ir::DexFileSection>& sections, |
| 227 | Printer* printer) { |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 228 | // Compute the total possible sizes for sections. |
| 229 | PageCount mapped_pages; |
| 230 | DCHECK_GE(end, start); |
| 231 | size_t total_mapped_pages = end - start; |
| 232 | if (total_mapped_pages == 0) { |
| 233 | return; |
| 234 | } |
| 235 | for (size_t page = start; page < end; ++page) { |
David Sehr | 093a6fb | 2017-05-09 15:41:09 -0700 | [diff] [blame] | 236 | const size_t dex_page_offset = page - start; |
| 237 | mapped_pages.Increment(FindSectionTypeForPage(dex_page_offset, sections)); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 238 | } |
| 239 | size_t total_resident_pages = 0; |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 240 | printer->PrintHeader(); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 241 | for (size_t i = sections.size(); i > 0; --i) { |
| 242 | const dex_ir::DexFileSection& section = sections[i - 1]; |
| 243 | const uint16_t type = section.type; |
| 244 | const DexSectionInfo& section_info = kDexSectionInfoMap.find(type)->second; |
| 245 | size_t pages_resident = resident_pages.Get(type); |
| 246 | double percent_resident = 0; |
| 247 | if (mapped_pages.Get(type) > 0) { |
| 248 | percent_resident = 100.0 * pages_resident / mapped_pages.Get(type); |
| 249 | } |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 250 | printer->PrintOne(section_info.name.c_str(), |
| 251 | pages_resident, |
| 252 | mapped_pages.Get(type), |
| 253 | percent_resident, |
| 254 | 100.0 * pages_resident / total_mapped_pages); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 255 | total_resident_pages += pages_resident; |
| 256 | } |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 257 | double percent_of_total = 100.0 * total_resident_pages / total_mapped_pages; |
| 258 | printer->PrintOne("GRAND TOTAL", |
| 259 | total_resident_pages, |
| 260 | total_mapped_pages, |
| 261 | percent_of_total, |
| 262 | percent_of_total); |
| 263 | printer->PrintSkipLine(); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void ProcessOneDexMapping(uint64_t* pagemap, |
| 267 | uint64_t map_start, |
| 268 | const DexFile* dex_file, |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 269 | uint64_t vdex_start, |
| 270 | Printer* printer) { |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 271 | uint64_t dex_file_start = reinterpret_cast<uint64_t>(dex_file->Begin()); |
| 272 | size_t dex_file_size = dex_file->Size(); |
| 273 | if (dex_file_start < vdex_start) { |
| 274 | std::cerr << "Dex file start offset for " |
| 275 | << dex_file->GetLocation().c_str() |
| 276 | << " is incorrect: map start " |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 277 | << StringPrintf("%" PRIx64 " > dex start %" PRIx64 "\n", map_start, dex_file_start) |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 278 | << std::endl; |
| 279 | return; |
| 280 | } |
David Sehr | c0e638f | 2017-04-07 16:56:46 -0700 | [diff] [blame] | 281 | uint64_t start_page = (dex_file_start - vdex_start) / kPageSize; |
| 282 | uint64_t start_address = start_page * kPageSize; |
| 283 | uint64_t end_page = RoundUp(start_address + dex_file_size, kPageSize) / kPageSize; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 284 | std::cout << "DEX " |
| 285 | << dex_file->GetLocation().c_str() |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 286 | << StringPrintf(": %" PRIx64 "-%" PRIx64, |
David Sehr | c0e638f | 2017-04-07 16:56:46 -0700 | [diff] [blame] | 287 | map_start + start_page * kPageSize, |
| 288 | map_start + end_page * kPageSize) |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 289 | << std::endl; |
| 290 | // Build a list of the dex file section types, sorted from highest offset to lowest. |
| 291 | std::vector<dex_ir::DexFileSection> sections; |
| 292 | { |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 293 | Options options; |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 294 | std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file, |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 295 | /*eagerly_assign_offsets*/ true, |
| 296 | options)); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 297 | sections = dex_ir::GetSortedDexFileSections(header.get(), |
| 298 | dex_ir::SortDirection::kSortDescending); |
| 299 | } |
| 300 | PageCount section_resident_pages; |
David Sehr | c0e638f | 2017-04-07 16:56:46 -0700 | [diff] [blame] | 301 | ProcessPageMap(pagemap, start_page, end_page, sections, §ion_resident_pages); |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 302 | DisplayDexStatistics(start_page, end_page, section_resident_pages, sections, printer); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 303 | } |
| 304 | |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 305 | static bool IsVdexFileMapping(const std::string& mapped_name) { |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 306 | // Confirm that the map is from a vdex file. |
| 307 | static const char* suffixes[] = { ".vdex" }; |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 308 | for (const char* suffix : suffixes) { |
| 309 | size_t match_loc = mapped_name.find(suffix); |
| 310 | if (match_loc != std::string::npos && mapped_name.length() == match_loc + strlen(suffix)) { |
| 311 | return true; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 314 | return false; |
| 315 | } |
| 316 | |
| 317 | static bool DisplayMappingIfFromVdexFile(pm_map_t* map, Printer* printer) { |
| 318 | std::string vdex_name = pm_map_name(map); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 319 | // Extract all the dex files from the vdex file. |
| 320 | std::string error_msg; |
| 321 | std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_name, |
| 322 | false /*writeable*/, |
| 323 | false /*low_4gb*/, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 324 | false /*unquicken */, |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 325 | &error_msg /*out*/)); |
| 326 | if (vdex == nullptr) { |
| 327 | std::cerr << "Could not open vdex file " |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 328 | << vdex_name |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 329 | << ": error " |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 330 | << error_msg |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 331 | << std::endl; |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 336 | if (!vdex->OpenAllDexFiles(&dex_files, &error_msg)) { |
| 337 | std::cerr << "Dex files could not be opened for " |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 338 | << vdex_name |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 339 | << ": error " |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 340 | << error_msg |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 341 | << std::endl; |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 342 | return false; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 343 | } |
| 344 | // Open the page mapping (one uint64_t per page) for the entire vdex mapping. |
| 345 | uint64_t* pagemap; |
| 346 | size_t len; |
| 347 | if (pm_map_pagemap(map, &pagemap, &len) != 0) { |
| 348 | std::cerr << "Error creating pagemap." << std::endl; |
| 349 | return false; |
| 350 | } |
| 351 | // Process the dex files. |
| 352 | std::cout << "MAPPING " |
| 353 | << pm_map_name(map) |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 354 | << StringPrintf(": %" PRIx64 "-%" PRIx64, pm_map_start(map), pm_map_end(map)) |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 355 | << std::endl; |
| 356 | for (const auto& dex_file : dex_files) { |
| 357 | ProcessOneDexMapping(pagemap, |
| 358 | pm_map_start(map), |
| 359 | dex_file.get(), |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 360 | reinterpret_cast<uint64_t>(vdex->Begin()), |
| 361 | printer); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 362 | } |
| 363 | free(pagemap); |
| 364 | return true; |
| 365 | } |
| 366 | |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 367 | static void ProcessOneOatMapping(uint64_t* pagemap, size_t size, Printer* printer) { |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 368 | static constexpr size_t kLineLength = 32; |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 369 | size_t resident_page_count = 0; |
| 370 | for (size_t page = 0; page < size; ++page) { |
| 371 | char type_char = '.'; |
| 372 | if (PM_PAGEMAP_PRESENT(pagemap[page])) { |
| 373 | ++resident_page_count; |
| 374 | type_char = '*'; |
| 375 | } |
| 376 | if (g_verbose) { |
| 377 | std::cout << type_char; |
| 378 | if (page % kLineLength == kLineLength - 1) { |
| 379 | std::cout << std::endl; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | if (g_verbose) { |
| 384 | if (size % kLineLength != 0) { |
| 385 | std::cout << std::endl; |
| 386 | } |
| 387 | } |
| 388 | double percent_of_total = 100.0 * resident_page_count / size; |
| 389 | printer->PrintHeader(); |
| 390 | printer->PrintOne("EXECUTABLE", resident_page_count, size, percent_of_total, percent_of_total); |
| 391 | printer->PrintSkipLine(); |
| 392 | } |
| 393 | |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 394 | static bool IsOatFileMapping(const std::string& mapped_name) { |
| 395 | // Confirm that the map is from an oat file. |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 396 | static const char* suffixes[] = { ".odex", ".oat" }; |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 397 | for (const char* suffix : suffixes) { |
| 398 | size_t match_loc = mapped_name.find(suffix); |
| 399 | if (match_loc != std::string::npos && mapped_name.length() == match_loc + strlen(suffix)) { |
| 400 | return true; |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | |
| 406 | static bool DisplayMappingIfFromOatFile(pm_map_t* map, Printer* printer) { |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 407 | // Open the page mapping (one uint64_t per page) for the entire vdex mapping. |
| 408 | uint64_t* pagemap; |
| 409 | size_t len; |
| 410 | if (pm_map_pagemap(map, &pagemap, &len) != 0) { |
| 411 | std::cerr << "Error creating pagemap." << std::endl; |
| 412 | return false; |
| 413 | } |
| 414 | // Process the dex files. |
| 415 | std::cout << "MAPPING " |
| 416 | << pm_map_name(map) |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 417 | << StringPrintf(": %" PRIx64 "-%" PRIx64, pm_map_start(map), pm_map_end(map)) |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 418 | << std::endl; |
| 419 | ProcessOneOatMapping(pagemap, len, printer); |
| 420 | free(pagemap); |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | static bool FilterByNameContains(const std::string& mapped_file_name, |
| 425 | const std::vector<std::string>& name_filters) { |
| 426 | // If no filters were set, everything matches. |
| 427 | if (name_filters.empty()) { |
| 428 | return true; |
| 429 | } |
| 430 | for (const auto& name_contains : name_filters) { |
| 431 | if (mapped_file_name.find(name_contains) != std::string::npos) { |
| 432 | return true; |
| 433 | } |
| 434 | } |
| 435 | return false; |
| 436 | } |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 437 | #endif |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 438 | |
| 439 | static void Usage(const char* cmd) { |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 440 | std::cout << "Usage: " << cmd << " [options] pid" << std::endl |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 441 | << " --contains=<string>: Display sections containing string." << std::endl |
| 442 | << " --help: Shows this message." << std::endl |
| 443 | << " --verbose: Makes displays verbose." << std::endl; |
| 444 | PrintLetterKey(); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 445 | } |
| 446 | |
David Sehr | 671af6c | 2018-05-17 11:00:35 -0700 | [diff] [blame] | 447 | NO_RETURN static void Abort(const char* msg) { |
| 448 | std::cerr << msg; |
| 449 | exit(1); |
| 450 | } |
| 451 | |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 452 | static int DexDiagMain(int argc, char* argv[]) { |
| 453 | if (argc < 2) { |
| 454 | Usage(argv[0]); |
| 455 | return EXIT_FAILURE; |
| 456 | } |
| 457 | |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 458 | std::vector<std::string> name_filters; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 459 | // TODO: add option to track usage by class name, etc. |
| 460 | for (int i = 1; i < argc - 1; ++i) { |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 461 | const StringPiece option(argv[i]); |
| 462 | if (option == "--help") { |
| 463 | Usage(argv[0]); |
| 464 | return EXIT_SUCCESS; |
| 465 | } else if (option == "--verbose") { |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 466 | g_verbose = true; |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 467 | } else if (option.starts_with("--contains=")) { |
| 468 | std::string contains(option.substr(strlen("--contains=")).data()); |
| 469 | name_filters.push_back(contains); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 470 | } else { |
| 471 | Usage(argv[0]); |
| 472 | return EXIT_FAILURE; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // Art specific set up. |
David Sehr | 671af6c | 2018-05-17 11:00:35 -0700 | [diff] [blame] | 477 | InitLogging(argv, Abort); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 478 | MemMap::Init(); |
| 479 | |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 480 | #ifdef ART_TARGET_ANDROID |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 481 | pid_t pid; |
| 482 | char* endptr; |
| 483 | pid = (pid_t)strtol(argv[argc - 1], &endptr, 10); |
| 484 | if (*endptr != '\0' || kill(pid, 0) != 0) { |
| 485 | std::cerr << StringPrintf("Invalid PID \"%s\".\n", argv[argc - 1]) << std::endl; |
| 486 | return EXIT_FAILURE; |
| 487 | } |
| 488 | |
| 489 | // get libpagemap kernel information. |
| 490 | pm_kernel_t* ker; |
| 491 | if (pm_kernel_create(&ker) != 0) { |
| 492 | std::cerr << "Error creating kernel interface -- does this kernel have pagemap?" << std::endl; |
| 493 | return EXIT_FAILURE; |
| 494 | } |
| 495 | |
| 496 | // get libpagemap process information. |
| 497 | pm_process_t* proc; |
| 498 | if (pm_process_create(ker, pid, &proc) != 0) { |
| 499 | std::cerr << "Error creating process interface -- does process " |
| 500 | << pid |
| 501 | << " really exist?" |
| 502 | << std::endl; |
| 503 | return EXIT_FAILURE; |
| 504 | } |
| 505 | |
| 506 | // Get the set of mappings by the specified process. |
| 507 | pm_map_t** maps; |
| 508 | size_t num_maps; |
| 509 | if (pm_process_maps(proc, &maps, &num_maps) != 0) { |
| 510 | std::cerr << "Error listing maps." << std::endl; |
| 511 | return EXIT_FAILURE; |
| 512 | } |
| 513 | |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 514 | bool match_found = false; |
| 515 | // Process the mappings that are due to vdex or oat files. |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 516 | Printer printer; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 517 | for (size_t i = 0; i < num_maps; ++i) { |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 518 | std::string mapped_file_name = pm_map_name(maps[i]); |
| 519 | // Filter by name contains options (if any). |
| 520 | if (!FilterByNameContains(mapped_file_name, name_filters)) { |
| 521 | continue; |
| 522 | } |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 523 | if (IsVdexFileMapping(mapped_file_name)) { |
| 524 | if (!DisplayMappingIfFromVdexFile(maps[i], &printer)) { |
| 525 | return EXIT_FAILURE; |
| 526 | } |
| 527 | match_found = true; |
| 528 | } else if (IsOatFileMapping(mapped_file_name)) { |
| 529 | if (!DisplayMappingIfFromOatFile(maps[i], &printer)) { |
| 530 | return EXIT_FAILURE; |
| 531 | } |
| 532 | match_found = true; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 535 | if (!match_found) { |
David Sehr | 592f802 | 2017-05-04 13:58:29 -0700 | [diff] [blame] | 536 | std::cerr << "No relevant memory maps were found." << std::endl; |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 537 | return EXIT_FAILURE; |
| 538 | } |
| 539 | #endif |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 540 | |
David Sehr | 3f3ec67 | 2017-04-05 14:43:38 -0700 | [diff] [blame] | 541 | return EXIT_SUCCESS; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | } // namespace art |
| 545 | |
| 546 | int main(int argc, char* argv[]) { |
| 547 | return art::DexDiagMain(argc, argv); |
| 548 | } |