blob: 6cb141f6881ec7407b37bd634e496513b95c9176 [file] [log] [blame]
David Sehrbeca4fe2017-03-30 17:50:24 -07001/*
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 Sehr55232f12017-04-19 14:06:49 -070018#include <inttypes.h>
David Sehrbeca4fe2017-03-30 17:50:24 -070019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <iostream>
24#include <memory>
David Sehr3f3ec672017-04-05 14:43:38 -070025#include <vector>
David Sehrbeca4fe2017-03-30 17:50:24 -070026
27#include "android-base/stringprintf.h"
28
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For InitLogging.
David Sehr8f4b0562018-03-02 12:01:51 -080030#include "base/mutex.h"
David Sehr3f3ec672017-04-05 14:43:38 -070031#include "base/stringpiece.h"
32
Mathieu Chartier75175552018-01-25 11:23:01 -080033#include "dexlayout.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
David Sehrbeca4fe2017-03-30 17:50:24 -070035#include "dex_ir.h"
36#include "dex_ir_builder.h"
David Sehr55232f12017-04-19 14:06:49 -070037#ifdef ART_TARGET_ANDROID
David Sehrbeca4fe2017-03-30 17:50:24 -070038#include "pagemap/pagemap.h"
David Sehr55232f12017-04-19 14:06:49 -070039#endif
David Sehrbeca4fe2017-03-30 17:50:24 -070040#include "runtime.h"
41#include "vdex_file.h"
42
43namespace art {
44
45using android::base::StringPrintf;
46
David Sehrbeca4fe2017-03-30 17:50:24 -070047static bool g_verbose = false;
David Sehr3f3ec672017-04-05 14:43:38 -070048
49// The width needed to print a file page offset (32-bit).
50static constexpr int kPageCountWidth =
51 static_cast<int>(std::numeric_limits<uint32_t>::digits10);
52// Display the sections.
53static constexpr char kSectionHeader[] = "Section name";
David Sehrbeca4fe2017-03-30 17:50:24 -070054
55struct DexSectionInfo {
56 public:
57 std::string name;
58 char letter;
59};
60
61static const std::map<uint16_t, DexSectionInfo> kDexSectionInfoMap = {
62 { DexFile::kDexTypeHeaderItem, { "Header", 'H' } },
63 { DexFile::kDexTypeStringIdItem, { "StringId", 'S' } },
64 { DexFile::kDexTypeTypeIdItem, { "TypeId", 'T' } },
65 { DexFile::kDexTypeProtoIdItem, { "ProtoId", 'P' } },
66 { DexFile::kDexTypeFieldIdItem, { "FieldId", 'F' } },
67 { DexFile::kDexTypeMethodIdItem, { "MethodId", 'M' } },
68 { DexFile::kDexTypeClassDefItem, { "ClassDef", 'C' } },
69 { DexFile::kDexTypeCallSiteIdItem, { "CallSiteId", 'z' } },
70 { DexFile::kDexTypeMethodHandleItem, { "MethodHandle", 'Z' } },
71 { DexFile::kDexTypeMapList, { "TypeMap", 'L' } },
72 { DexFile::kDexTypeTypeList, { "TypeList", 't' } },
73 { DexFile::kDexTypeAnnotationSetRefList, { "AnnotationSetReferenceItem", '1' } },
74 { DexFile::kDexTypeAnnotationSetItem, { "AnnotationSetItem", '2' } },
75 { DexFile::kDexTypeClassDataItem, { "ClassData", 'c' } },
76 { DexFile::kDexTypeCodeItem, { "CodeItem", 'X' } },
77 { DexFile::kDexTypeStringDataItem, { "StringData", 's' } },
78 { DexFile::kDexTypeDebugInfoItem, { "DebugInfo", 'D' } },
79 { DexFile::kDexTypeAnnotationItem, { "AnnotationItem", '3' } },
80 { DexFile::kDexTypeEncodedArrayItem, { "EncodedArrayItem", 'E' } },
81 { DexFile::kDexTypeAnnotationsDirectoryItem, { "AnnotationsDirectoryItem", '4' } }
82};
83
84class PageCount {
85 public:
86 PageCount() {
87 for (auto it = kDexSectionInfoMap.begin(); it != kDexSectionInfoMap.end(); ++it) {
88 map_[it->first] = 0;
89 }
90 }
91 void Increment(uint16_t type) {
92 map_[type]++;
93 }
94 size_t Get(uint16_t type) const {
95 return map_.at(type);
96 }
97 private:
98 std::map<uint16_t, size_t> map_;
99 DISALLOW_COPY_AND_ASSIGN(PageCount);
100};
101
David Sehr3f3ec672017-04-05 14:43:38 -0700102class Printer {
103 public:
104 Printer() : section_header_width_(ComputeHeaderWidth()) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700105 }
David Sehr3f3ec672017-04-05 14:43:38 -0700106
107 void PrintHeader() const {
108 std::cout << StringPrintf("%-*s %*s %*s %% of %% of",
109 section_header_width_,
110 kSectionHeader,
111 kPageCountWidth,
112 "resident",
113 kPageCountWidth,
114 "total"
115 )
116 << std::endl;
117 std::cout << StringPrintf("%-*s %*s %*s sect. total",
118 section_header_width_,
119 "",
120 kPageCountWidth,
121 "pages",
122 kPageCountWidth,
123 "pages")
124 << std::endl;
125 }
126
127 void PrintOne(const char* name,
128 size_t resident,
129 size_t mapped,
130 double percent_of_section,
131 double percent_of_total) const {
132 // 6.2 is sufficient to print 0-100% with two decimal places of accuracy.
133 std::cout << StringPrintf("%-*s %*zd %*zd %6.2f %6.2f",
134 section_header_width_,
135 name,
136 kPageCountWidth,
137 resident,
138 kPageCountWidth,
139 mapped,
140 percent_of_section,
141 percent_of_total)
142 << std::endl;
143 }
144
145 void PrintSkipLine() const { std::cout << std::endl; }
146
147 // Computes the width of the section header column in the table (for fixed formatting).
148 static int ComputeHeaderWidth() {
149 int header_width = 0;
150 for (const auto& pair : kDexSectionInfoMap) {
151 const DexSectionInfo& section_info = pair.second;
152 header_width = std::max(header_width, static_cast<int>(section_info.name.length()));
153 }
154 return header_width;
155 }
156
157 private:
158 const int section_header_width_;
159};
160
161static void PrintLetterKey() {
162 std::cout << "L pagetype" << std::endl;
163 for (const auto& pair : kDexSectionInfoMap) {
164 const DexSectionInfo& section_info = pair.second;
165 std::cout << section_info.letter << " " << section_info.name.c_str() << std::endl;
166 }
167 std::cout << "* (Executable page resident)" << std::endl;
168 std::cout << ". (Mapped page not resident)" << std::endl;
David Sehrbeca4fe2017-03-30 17:50:24 -0700169}
170
David Sehr55232f12017-04-19 14:06:49 -0700171#ifdef ART_TARGET_ANDROID
David Sehrbeca4fe2017-03-30 17:50:24 -0700172static char PageTypeChar(uint16_t type) {
173 if (kDexSectionInfoMap.find(type) == kDexSectionInfoMap.end()) {
174 return '-';
175 }
176 return kDexSectionInfoMap.find(type)->second.letter;
177}
178
179static uint16_t FindSectionTypeForPage(size_t page,
180 const std::vector<dex_ir::DexFileSection>& sections) {
181 for (const auto& section : sections) {
182 size_t first_page_of_section = section.offset / kPageSize;
183 // Only consider non-empty sections.
184 if (section.size == 0) {
185 continue;
186 }
187 // Attribute the page to the highest-offset section that starts before the page.
188 if (first_page_of_section <= page) {
189 return section.type;
190 }
191 }
192 // If there's no non-zero sized section with an offset below offset we're looking for, it
193 // must be the header.
194 return DexFile::kDexTypeHeaderItem;
195}
196
197static void ProcessPageMap(uint64_t* pagemap,
198 size_t start,
199 size_t end,
200 const std::vector<dex_ir::DexFileSection>& sections,
201 PageCount* page_counts) {
David Sehr55232f12017-04-19 14:06:49 -0700202 static constexpr size_t kLineLength = 32;
David Sehrbeca4fe2017-03-30 17:50:24 -0700203 for (size_t page = start; page < end; ++page) {
204 char type_char = '.';
205 if (PM_PAGEMAP_PRESENT(pagemap[page])) {
David Sehr093a6fb2017-05-09 15:41:09 -0700206 const size_t dex_page_offset = page - start;
207 uint16_t type = FindSectionTypeForPage(dex_page_offset, sections);
David Sehrbeca4fe2017-03-30 17:50:24 -0700208 page_counts->Increment(type);
209 type_char = PageTypeChar(type);
210 }
211 if (g_verbose) {
212 std::cout << type_char;
213 if ((page - start) % kLineLength == kLineLength - 1) {
214 std::cout << std::endl;
215 }
216 }
217 }
218 if (g_verbose) {
219 if ((end - start) % kLineLength != 0) {
220 std::cout << std::endl;
221 }
222 }
223}
224
225static void DisplayDexStatistics(size_t start,
226 size_t end,
227 const PageCount& resident_pages,
David Sehr3f3ec672017-04-05 14:43:38 -0700228 const std::vector<dex_ir::DexFileSection>& sections,
229 Printer* printer) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700230 // Compute the total possible sizes for sections.
231 PageCount mapped_pages;
232 DCHECK_GE(end, start);
233 size_t total_mapped_pages = end - start;
234 if (total_mapped_pages == 0) {
235 return;
236 }
237 for (size_t page = start; page < end; ++page) {
David Sehr093a6fb2017-05-09 15:41:09 -0700238 const size_t dex_page_offset = page - start;
239 mapped_pages.Increment(FindSectionTypeForPage(dex_page_offset, sections));
David Sehrbeca4fe2017-03-30 17:50:24 -0700240 }
241 size_t total_resident_pages = 0;
David Sehr3f3ec672017-04-05 14:43:38 -0700242 printer->PrintHeader();
David Sehrbeca4fe2017-03-30 17:50:24 -0700243 for (size_t i = sections.size(); i > 0; --i) {
244 const dex_ir::DexFileSection& section = sections[i - 1];
245 const uint16_t type = section.type;
246 const DexSectionInfo& section_info = kDexSectionInfoMap.find(type)->second;
247 size_t pages_resident = resident_pages.Get(type);
248 double percent_resident = 0;
249 if (mapped_pages.Get(type) > 0) {
250 percent_resident = 100.0 * pages_resident / mapped_pages.Get(type);
251 }
David Sehr3f3ec672017-04-05 14:43:38 -0700252 printer->PrintOne(section_info.name.c_str(),
253 pages_resident,
254 mapped_pages.Get(type),
255 percent_resident,
256 100.0 * pages_resident / total_mapped_pages);
David Sehrbeca4fe2017-03-30 17:50:24 -0700257 total_resident_pages += pages_resident;
258 }
David Sehr3f3ec672017-04-05 14:43:38 -0700259 double percent_of_total = 100.0 * total_resident_pages / total_mapped_pages;
260 printer->PrintOne("GRAND TOTAL",
261 total_resident_pages,
262 total_mapped_pages,
263 percent_of_total,
264 percent_of_total);
265 printer->PrintSkipLine();
David Sehrbeca4fe2017-03-30 17:50:24 -0700266}
267
268static void ProcessOneDexMapping(uint64_t* pagemap,
269 uint64_t map_start,
270 const DexFile* dex_file,
David Sehr3f3ec672017-04-05 14:43:38 -0700271 uint64_t vdex_start,
272 Printer* printer) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700273 uint64_t dex_file_start = reinterpret_cast<uint64_t>(dex_file->Begin());
274 size_t dex_file_size = dex_file->Size();
275 if (dex_file_start < vdex_start) {
276 std::cerr << "Dex file start offset for "
277 << dex_file->GetLocation().c_str()
278 << " is incorrect: map start "
David Sehr55232f12017-04-19 14:06:49 -0700279 << StringPrintf("%" PRIx64 " > dex start %" PRIx64 "\n", map_start, dex_file_start)
David Sehrbeca4fe2017-03-30 17:50:24 -0700280 << std::endl;
281 return;
282 }
David Sehrc0e638f2017-04-07 16:56:46 -0700283 uint64_t start_page = (dex_file_start - vdex_start) / kPageSize;
284 uint64_t start_address = start_page * kPageSize;
285 uint64_t end_page = RoundUp(start_address + dex_file_size, kPageSize) / kPageSize;
David Sehrbeca4fe2017-03-30 17:50:24 -0700286 std::cout << "DEX "
287 << dex_file->GetLocation().c_str()
David Sehr55232f12017-04-19 14:06:49 -0700288 << StringPrintf(": %" PRIx64 "-%" PRIx64,
David Sehrc0e638f2017-04-07 16:56:46 -0700289 map_start + start_page * kPageSize,
290 map_start + end_page * kPageSize)
David Sehrbeca4fe2017-03-30 17:50:24 -0700291 << std::endl;
292 // Build a list of the dex file section types, sorted from highest offset to lowest.
293 std::vector<dex_ir::DexFileSection> sections;
294 {
Mathieu Chartier75175552018-01-25 11:23:01 -0800295 Options options;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800296 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file,
Mathieu Chartier75175552018-01-25 11:23:01 -0800297 /*eagerly_assign_offsets*/ true,
298 options));
David Sehrbeca4fe2017-03-30 17:50:24 -0700299 sections = dex_ir::GetSortedDexFileSections(header.get(),
300 dex_ir::SortDirection::kSortDescending);
301 }
302 PageCount section_resident_pages;
David Sehrc0e638f2017-04-07 16:56:46 -0700303 ProcessPageMap(pagemap, start_page, end_page, sections, &section_resident_pages);
David Sehr3f3ec672017-04-05 14:43:38 -0700304 DisplayDexStatistics(start_page, end_page, section_resident_pages, sections, printer);
David Sehrbeca4fe2017-03-30 17:50:24 -0700305}
306
David Sehr592f8022017-05-04 13:58:29 -0700307static bool IsVdexFileMapping(const std::string& mapped_name) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700308 // Confirm that the map is from a vdex file.
309 static const char* suffixes[] = { ".vdex" };
David Sehr592f8022017-05-04 13:58:29 -0700310 for (const char* suffix : suffixes) {
311 size_t match_loc = mapped_name.find(suffix);
312 if (match_loc != std::string::npos && mapped_name.length() == match_loc + strlen(suffix)) {
313 return true;
David Sehrbeca4fe2017-03-30 17:50:24 -0700314 }
315 }
David Sehr592f8022017-05-04 13:58:29 -0700316 return false;
317}
318
319static bool DisplayMappingIfFromVdexFile(pm_map_t* map, Printer* printer) {
320 std::string vdex_name = pm_map_name(map);
David Sehrbeca4fe2017-03-30 17:50:24 -0700321 // Extract all the dex files from the vdex file.
322 std::string error_msg;
323 std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_name,
324 false /*writeable*/,
325 false /*low_4gb*/,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100326 false /*unquicken */,
David Sehrbeca4fe2017-03-30 17:50:24 -0700327 &error_msg /*out*/));
328 if (vdex == nullptr) {
329 std::cerr << "Could not open vdex file "
David Sehr3f3ec672017-04-05 14:43:38 -0700330 << vdex_name
David Sehrbeca4fe2017-03-30 17:50:24 -0700331 << ": error "
David Sehr3f3ec672017-04-05 14:43:38 -0700332 << error_msg
David Sehrbeca4fe2017-03-30 17:50:24 -0700333 << std::endl;
334 return false;
335 }
336
337 std::vector<std::unique_ptr<const DexFile>> dex_files;
338 if (!vdex->OpenAllDexFiles(&dex_files, &error_msg)) {
339 std::cerr << "Dex files could not be opened for "
David Sehr3f3ec672017-04-05 14:43:38 -0700340 << vdex_name
David Sehrbeca4fe2017-03-30 17:50:24 -0700341 << ": error "
David Sehr3f3ec672017-04-05 14:43:38 -0700342 << error_msg
David Sehrbeca4fe2017-03-30 17:50:24 -0700343 << std::endl;
David Sehr592f8022017-05-04 13:58:29 -0700344 return false;
David Sehrbeca4fe2017-03-30 17:50:24 -0700345 }
346 // Open the page mapping (one uint64_t per page) for the entire vdex mapping.
347 uint64_t* pagemap;
348 size_t len;
349 if (pm_map_pagemap(map, &pagemap, &len) != 0) {
350 std::cerr << "Error creating pagemap." << std::endl;
351 return false;
352 }
353 // Process the dex files.
354 std::cout << "MAPPING "
355 << pm_map_name(map)
David Sehr55232f12017-04-19 14:06:49 -0700356 << StringPrintf(": %" PRIx64 "-%" PRIx64, pm_map_start(map), pm_map_end(map))
David Sehrbeca4fe2017-03-30 17:50:24 -0700357 << std::endl;
358 for (const auto& dex_file : dex_files) {
359 ProcessOneDexMapping(pagemap,
360 pm_map_start(map),
361 dex_file.get(),
David Sehr3f3ec672017-04-05 14:43:38 -0700362 reinterpret_cast<uint64_t>(vdex->Begin()),
363 printer);
David Sehrbeca4fe2017-03-30 17:50:24 -0700364 }
365 free(pagemap);
366 return true;
367}
368
David Sehr3f3ec672017-04-05 14:43:38 -0700369static void ProcessOneOatMapping(uint64_t* pagemap, size_t size, Printer* printer) {
David Sehr55232f12017-04-19 14:06:49 -0700370 static constexpr size_t kLineLength = 32;
David Sehr3f3ec672017-04-05 14:43:38 -0700371 size_t resident_page_count = 0;
372 for (size_t page = 0; page < size; ++page) {
373 char type_char = '.';
374 if (PM_PAGEMAP_PRESENT(pagemap[page])) {
375 ++resident_page_count;
376 type_char = '*';
377 }
378 if (g_verbose) {
379 std::cout << type_char;
380 if (page % kLineLength == kLineLength - 1) {
381 std::cout << std::endl;
382 }
383 }
384 }
385 if (g_verbose) {
386 if (size % kLineLength != 0) {
387 std::cout << std::endl;
388 }
389 }
390 double percent_of_total = 100.0 * resident_page_count / size;
391 printer->PrintHeader();
392 printer->PrintOne("EXECUTABLE", resident_page_count, size, percent_of_total, percent_of_total);
393 printer->PrintSkipLine();
394}
395
David Sehr592f8022017-05-04 13:58:29 -0700396static bool IsOatFileMapping(const std::string& mapped_name) {
397 // Confirm that the map is from an oat file.
David Sehr3f3ec672017-04-05 14:43:38 -0700398 static const char* suffixes[] = { ".odex", ".oat" };
David Sehr592f8022017-05-04 13:58:29 -0700399 for (const char* suffix : suffixes) {
400 size_t match_loc = mapped_name.find(suffix);
401 if (match_loc != std::string::npos && mapped_name.length() == match_loc + strlen(suffix)) {
402 return true;
David Sehr3f3ec672017-04-05 14:43:38 -0700403 }
404 }
David Sehr592f8022017-05-04 13:58:29 -0700405 return false;
406}
407
408static bool DisplayMappingIfFromOatFile(pm_map_t* map, Printer* printer) {
David Sehr3f3ec672017-04-05 14:43:38 -0700409 // Open the page mapping (one uint64_t per page) for the entire vdex mapping.
410 uint64_t* pagemap;
411 size_t len;
412 if (pm_map_pagemap(map, &pagemap, &len) != 0) {
413 std::cerr << "Error creating pagemap." << std::endl;
414 return false;
415 }
416 // Process the dex files.
417 std::cout << "MAPPING "
418 << pm_map_name(map)
David Sehr55232f12017-04-19 14:06:49 -0700419 << StringPrintf(": %" PRIx64 "-%" PRIx64, pm_map_start(map), pm_map_end(map))
David Sehr3f3ec672017-04-05 14:43:38 -0700420 << std::endl;
421 ProcessOneOatMapping(pagemap, len, printer);
422 free(pagemap);
423 return true;
424}
425
426static bool FilterByNameContains(const std::string& mapped_file_name,
427 const std::vector<std::string>& name_filters) {
428 // If no filters were set, everything matches.
429 if (name_filters.empty()) {
430 return true;
431 }
432 for (const auto& name_contains : name_filters) {
433 if (mapped_file_name.find(name_contains) != std::string::npos) {
434 return true;
435 }
436 }
437 return false;
438}
David Sehr55232f12017-04-19 14:06:49 -0700439#endif
David Sehrbeca4fe2017-03-30 17:50:24 -0700440
441static void Usage(const char* cmd) {
David Sehr55232f12017-04-19 14:06:49 -0700442 std::cout << "Usage: " << cmd << " [options] pid" << std::endl
David Sehr3f3ec672017-04-05 14:43:38 -0700443 << " --contains=<string>: Display sections containing string." << std::endl
444 << " --help: Shows this message." << std::endl
445 << " --verbose: Makes displays verbose." << std::endl;
446 PrintLetterKey();
David Sehrbeca4fe2017-03-30 17:50:24 -0700447}
448
449static int DexDiagMain(int argc, char* argv[]) {
450 if (argc < 2) {
451 Usage(argv[0]);
452 return EXIT_FAILURE;
453 }
454
David Sehr3f3ec672017-04-05 14:43:38 -0700455 std::vector<std::string> name_filters;
David Sehrbeca4fe2017-03-30 17:50:24 -0700456 // TODO: add option to track usage by class name, etc.
457 for (int i = 1; i < argc - 1; ++i) {
David Sehr3f3ec672017-04-05 14:43:38 -0700458 const StringPiece option(argv[i]);
459 if (option == "--help") {
460 Usage(argv[0]);
461 return EXIT_SUCCESS;
462 } else if (option == "--verbose") {
David Sehrbeca4fe2017-03-30 17:50:24 -0700463 g_verbose = true;
David Sehr3f3ec672017-04-05 14:43:38 -0700464 } else if (option.starts_with("--contains=")) {
465 std::string contains(option.substr(strlen("--contains=")).data());
466 name_filters.push_back(contains);
David Sehrbeca4fe2017-03-30 17:50:24 -0700467 } else {
468 Usage(argv[0]);
469 return EXIT_FAILURE;
470 }
471 }
472
473 // Art specific set up.
David Sehr8f4b0562018-03-02 12:01:51 -0800474 Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700475 InitLogging(argv, Runtime::Abort);
David Sehrbeca4fe2017-03-30 17:50:24 -0700476 MemMap::Init();
477
David Sehr55232f12017-04-19 14:06:49 -0700478#ifdef ART_TARGET_ANDROID
David Sehrbeca4fe2017-03-30 17:50:24 -0700479 pid_t pid;
480 char* endptr;
481 pid = (pid_t)strtol(argv[argc - 1], &endptr, 10);
482 if (*endptr != '\0' || kill(pid, 0) != 0) {
483 std::cerr << StringPrintf("Invalid PID \"%s\".\n", argv[argc - 1]) << std::endl;
484 return EXIT_FAILURE;
485 }
486
487 // get libpagemap kernel information.
488 pm_kernel_t* ker;
489 if (pm_kernel_create(&ker) != 0) {
490 std::cerr << "Error creating kernel interface -- does this kernel have pagemap?" << std::endl;
491 return EXIT_FAILURE;
492 }
493
494 // get libpagemap process information.
495 pm_process_t* proc;
496 if (pm_process_create(ker, pid, &proc) != 0) {
497 std::cerr << "Error creating process interface -- does process "
498 << pid
499 << " really exist?"
500 << std::endl;
501 return EXIT_FAILURE;
502 }
503
504 // Get the set of mappings by the specified process.
505 pm_map_t** maps;
506 size_t num_maps;
507 if (pm_process_maps(proc, &maps, &num_maps) != 0) {
508 std::cerr << "Error listing maps." << std::endl;
509 return EXIT_FAILURE;
510 }
511
David Sehr55232f12017-04-19 14:06:49 -0700512 bool match_found = false;
513 // Process the mappings that are due to vdex or oat files.
David Sehr3f3ec672017-04-05 14:43:38 -0700514 Printer printer;
David Sehrbeca4fe2017-03-30 17:50:24 -0700515 for (size_t i = 0; i < num_maps; ++i) {
David Sehr3f3ec672017-04-05 14:43:38 -0700516 std::string mapped_file_name = pm_map_name(maps[i]);
517 // Filter by name contains options (if any).
518 if (!FilterByNameContains(mapped_file_name, name_filters)) {
519 continue;
520 }
David Sehr592f8022017-05-04 13:58:29 -0700521 if (IsVdexFileMapping(mapped_file_name)) {
522 if (!DisplayMappingIfFromVdexFile(maps[i], &printer)) {
523 return EXIT_FAILURE;
524 }
525 match_found = true;
526 } else if (IsOatFileMapping(mapped_file_name)) {
527 if (!DisplayMappingIfFromOatFile(maps[i], &printer)) {
528 return EXIT_FAILURE;
529 }
530 match_found = true;
David Sehrbeca4fe2017-03-30 17:50:24 -0700531 }
532 }
David Sehr55232f12017-04-19 14:06:49 -0700533 if (!match_found) {
David Sehr592f8022017-05-04 13:58:29 -0700534 std::cerr << "No relevant memory maps were found." << std::endl;
David Sehr55232f12017-04-19 14:06:49 -0700535 return EXIT_FAILURE;
536 }
537#endif
David Sehrbeca4fe2017-03-30 17:50:24 -0700538
David Sehr3f3ec672017-04-05 14:43:38 -0700539 return EXIT_SUCCESS;
David Sehrbeca4fe2017-03-30 17:50:24 -0700540}
541
542} // namespace art
543
544int main(int argc, char* argv[]) {
545 return art::DexDiagMain(argc, argv);
546}