blob: aa4e6d031e9f3ee3cca7fdb10ef7f029c81ca0ce [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 Sehr3f3ec672017-04-05 14:43:38 -070030#include "base/stringpiece.h"
31
Mathieu Chartier75175552018-01-25 11:23:01 -080032#include "dexlayout.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file.h"
David Sehrbeca4fe2017-03-30 17:50:24 -070034#include "dex_ir.h"
35#include "dex_ir_builder.h"
David Sehr55232f12017-04-19 14:06:49 -070036#ifdef ART_TARGET_ANDROID
David Sehrbeca4fe2017-03-30 17:50:24 -070037#include "pagemap/pagemap.h"
David Sehr55232f12017-04-19 14:06:49 -070038#endif
David Sehrbeca4fe2017-03-30 17:50:24 -070039#include "vdex_file.h"
40
41namespace art {
42
43using android::base::StringPrintf;
44
David Sehrbeca4fe2017-03-30 17:50:24 -070045static bool g_verbose = false;
David Sehr3f3ec672017-04-05 14:43:38 -070046
47// The width needed to print a file page offset (32-bit).
48static constexpr int kPageCountWidth =
49 static_cast<int>(std::numeric_limits<uint32_t>::digits10);
50// Display the sections.
51static constexpr char kSectionHeader[] = "Section name";
David Sehrbeca4fe2017-03-30 17:50:24 -070052
53struct DexSectionInfo {
54 public:
55 std::string name;
56 char letter;
57};
58
59static 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
82class 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 Sehr3f3ec672017-04-05 14:43:38 -0700100class Printer {
101 public:
102 Printer() : section_header_width_(ComputeHeaderWidth()) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700103 }
David Sehr3f3ec672017-04-05 14:43:38 -0700104
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
159static 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 Sehrbeca4fe2017-03-30 17:50:24 -0700167}
168
David Sehr55232f12017-04-19 14:06:49 -0700169#ifdef ART_TARGET_ANDROID
David Sehrbeca4fe2017-03-30 17:50:24 -0700170static char PageTypeChar(uint16_t type) {
171 if (kDexSectionInfoMap.find(type) == kDexSectionInfoMap.end()) {
172 return '-';
173 }
174 return kDexSectionInfoMap.find(type)->second.letter;
175}
176
177static 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
195static 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 Sehr55232f12017-04-19 14:06:49 -0700200 static constexpr size_t kLineLength = 32;
David Sehrbeca4fe2017-03-30 17:50:24 -0700201 for (size_t page = start; page < end; ++page) {
202 char type_char = '.';
203 if (PM_PAGEMAP_PRESENT(pagemap[page])) {
David Sehr093a6fb2017-05-09 15:41:09 -0700204 const size_t dex_page_offset = page - start;
205 uint16_t type = FindSectionTypeForPage(dex_page_offset, sections);
David Sehrbeca4fe2017-03-30 17:50:24 -0700206 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
223static void DisplayDexStatistics(size_t start,
224 size_t end,
225 const PageCount& resident_pages,
David Sehr3f3ec672017-04-05 14:43:38 -0700226 const std::vector<dex_ir::DexFileSection>& sections,
227 Printer* printer) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700228 // 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 Sehr093a6fb2017-05-09 15:41:09 -0700236 const size_t dex_page_offset = page - start;
237 mapped_pages.Increment(FindSectionTypeForPage(dex_page_offset, sections));
David Sehrbeca4fe2017-03-30 17:50:24 -0700238 }
239 size_t total_resident_pages = 0;
David Sehr3f3ec672017-04-05 14:43:38 -0700240 printer->PrintHeader();
David Sehrbeca4fe2017-03-30 17:50:24 -0700241 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 Sehr3f3ec672017-04-05 14:43:38 -0700250 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 Sehrbeca4fe2017-03-30 17:50:24 -0700255 total_resident_pages += pages_resident;
256 }
David Sehr3f3ec672017-04-05 14:43:38 -0700257 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 Sehrbeca4fe2017-03-30 17:50:24 -0700264}
265
266static void ProcessOneDexMapping(uint64_t* pagemap,
267 uint64_t map_start,
268 const DexFile* dex_file,
David Sehr3f3ec672017-04-05 14:43:38 -0700269 uint64_t vdex_start,
270 Printer* printer) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700271 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 Sehr55232f12017-04-19 14:06:49 -0700277 << StringPrintf("%" PRIx64 " > dex start %" PRIx64 "\n", map_start, dex_file_start)
David Sehrbeca4fe2017-03-30 17:50:24 -0700278 << std::endl;
279 return;
280 }
David Sehrc0e638f2017-04-07 16:56:46 -0700281 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 Sehrbeca4fe2017-03-30 17:50:24 -0700284 std::cout << "DEX "
285 << dex_file->GetLocation().c_str()
David Sehr55232f12017-04-19 14:06:49 -0700286 << StringPrintf(": %" PRIx64 "-%" PRIx64,
David Sehrc0e638f2017-04-07 16:56:46 -0700287 map_start + start_page * kPageSize,
288 map_start + end_page * kPageSize)
David Sehrbeca4fe2017-03-30 17:50:24 -0700289 << 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 Chartier75175552018-01-25 11:23:01 -0800293 Options options;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800294 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file,
Mathieu Chartier75175552018-01-25 11:23:01 -0800295 /*eagerly_assign_offsets*/ true,
296 options));
David Sehrbeca4fe2017-03-30 17:50:24 -0700297 sections = dex_ir::GetSortedDexFileSections(header.get(),
298 dex_ir::SortDirection::kSortDescending);
299 }
300 PageCount section_resident_pages;
David Sehrc0e638f2017-04-07 16:56:46 -0700301 ProcessPageMap(pagemap, start_page, end_page, sections, &section_resident_pages);
David Sehr3f3ec672017-04-05 14:43:38 -0700302 DisplayDexStatistics(start_page, end_page, section_resident_pages, sections, printer);
David Sehrbeca4fe2017-03-30 17:50:24 -0700303}
304
David Sehr592f8022017-05-04 13:58:29 -0700305static bool IsVdexFileMapping(const std::string& mapped_name) {
David Sehrbeca4fe2017-03-30 17:50:24 -0700306 // Confirm that the map is from a vdex file.
307 static const char* suffixes[] = { ".vdex" };
David Sehr592f8022017-05-04 13:58:29 -0700308 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 Sehrbeca4fe2017-03-30 17:50:24 -0700312 }
313 }
David Sehr592f8022017-05-04 13:58:29 -0700314 return false;
315}
316
317static bool DisplayMappingIfFromVdexFile(pm_map_t* map, Printer* printer) {
318 std::string vdex_name = pm_map_name(map);
David Sehrbeca4fe2017-03-30 17:50:24 -0700319 // 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 Geoffray4e868fa2017-04-21 17:16:44 +0100324 false /*unquicken */,
David Sehrbeca4fe2017-03-30 17:50:24 -0700325 &error_msg /*out*/));
326 if (vdex == nullptr) {
327 std::cerr << "Could not open vdex file "
David Sehr3f3ec672017-04-05 14:43:38 -0700328 << vdex_name
David Sehrbeca4fe2017-03-30 17:50:24 -0700329 << ": error "
David Sehr3f3ec672017-04-05 14:43:38 -0700330 << error_msg
David Sehrbeca4fe2017-03-30 17:50:24 -0700331 << 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 Sehr3f3ec672017-04-05 14:43:38 -0700338 << vdex_name
David Sehrbeca4fe2017-03-30 17:50:24 -0700339 << ": error "
David Sehr3f3ec672017-04-05 14:43:38 -0700340 << error_msg
David Sehrbeca4fe2017-03-30 17:50:24 -0700341 << std::endl;
David Sehr592f8022017-05-04 13:58:29 -0700342 return false;
David Sehrbeca4fe2017-03-30 17:50:24 -0700343 }
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 Sehr55232f12017-04-19 14:06:49 -0700354 << StringPrintf(": %" PRIx64 "-%" PRIx64, pm_map_start(map), pm_map_end(map))
David Sehrbeca4fe2017-03-30 17:50:24 -0700355 << std::endl;
356 for (const auto& dex_file : dex_files) {
357 ProcessOneDexMapping(pagemap,
358 pm_map_start(map),
359 dex_file.get(),
David Sehr3f3ec672017-04-05 14:43:38 -0700360 reinterpret_cast<uint64_t>(vdex->Begin()),
361 printer);
David Sehrbeca4fe2017-03-30 17:50:24 -0700362 }
363 free(pagemap);
364 return true;
365}
366
David Sehr3f3ec672017-04-05 14:43:38 -0700367static void ProcessOneOatMapping(uint64_t* pagemap, size_t size, Printer* printer) {
David Sehr55232f12017-04-19 14:06:49 -0700368 static constexpr size_t kLineLength = 32;
David Sehr3f3ec672017-04-05 14:43:38 -0700369 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 Sehr592f8022017-05-04 13:58:29 -0700394static bool IsOatFileMapping(const std::string& mapped_name) {
395 // Confirm that the map is from an oat file.
David Sehr3f3ec672017-04-05 14:43:38 -0700396 static const char* suffixes[] = { ".odex", ".oat" };
David Sehr592f8022017-05-04 13:58:29 -0700397 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 Sehr3f3ec672017-04-05 14:43:38 -0700401 }
402 }
David Sehr592f8022017-05-04 13:58:29 -0700403 return false;
404}
405
406static bool DisplayMappingIfFromOatFile(pm_map_t* map, Printer* printer) {
David Sehr3f3ec672017-04-05 14:43:38 -0700407 // 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 Sehr55232f12017-04-19 14:06:49 -0700417 << StringPrintf(": %" PRIx64 "-%" PRIx64, pm_map_start(map), pm_map_end(map))
David Sehr3f3ec672017-04-05 14:43:38 -0700418 << std::endl;
419 ProcessOneOatMapping(pagemap, len, printer);
420 free(pagemap);
421 return true;
422}
423
424static 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 Sehr55232f12017-04-19 14:06:49 -0700437#endif
David Sehrbeca4fe2017-03-30 17:50:24 -0700438
439static void Usage(const char* cmd) {
David Sehr55232f12017-04-19 14:06:49 -0700440 std::cout << "Usage: " << cmd << " [options] pid" << std::endl
David Sehr3f3ec672017-04-05 14:43:38 -0700441 << " --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 Sehrbeca4fe2017-03-30 17:50:24 -0700445}
446
David Sehr671af6c2018-05-17 11:00:35 -0700447NO_RETURN static void Abort(const char* msg) {
448 std::cerr << msg;
449 exit(1);
450}
451
David Sehrbeca4fe2017-03-30 17:50:24 -0700452static int DexDiagMain(int argc, char* argv[]) {
453 if (argc < 2) {
454 Usage(argv[0]);
455 return EXIT_FAILURE;
456 }
457
David Sehr3f3ec672017-04-05 14:43:38 -0700458 std::vector<std::string> name_filters;
David Sehrbeca4fe2017-03-30 17:50:24 -0700459 // TODO: add option to track usage by class name, etc.
460 for (int i = 1; i < argc - 1; ++i) {
David Sehr3f3ec672017-04-05 14:43:38 -0700461 const StringPiece option(argv[i]);
462 if (option == "--help") {
463 Usage(argv[0]);
464 return EXIT_SUCCESS;
465 } else if (option == "--verbose") {
David Sehrbeca4fe2017-03-30 17:50:24 -0700466 g_verbose = true;
David Sehr3f3ec672017-04-05 14:43:38 -0700467 } else if (option.starts_with("--contains=")) {
468 std::string contains(option.substr(strlen("--contains=")).data());
469 name_filters.push_back(contains);
David Sehrbeca4fe2017-03-30 17:50:24 -0700470 } else {
471 Usage(argv[0]);
472 return EXIT_FAILURE;
473 }
474 }
475
476 // Art specific set up.
David Sehr671af6c2018-05-17 11:00:35 -0700477 InitLogging(argv, Abort);
David Sehrbeca4fe2017-03-30 17:50:24 -0700478 MemMap::Init();
479
David Sehr55232f12017-04-19 14:06:49 -0700480#ifdef ART_TARGET_ANDROID
David Sehrbeca4fe2017-03-30 17:50:24 -0700481 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 Sehr55232f12017-04-19 14:06:49 -0700514 bool match_found = false;
515 // Process the mappings that are due to vdex or oat files.
David Sehr3f3ec672017-04-05 14:43:38 -0700516 Printer printer;
David Sehrbeca4fe2017-03-30 17:50:24 -0700517 for (size_t i = 0; i < num_maps; ++i) {
David Sehr3f3ec672017-04-05 14:43:38 -0700518 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 Sehr592f8022017-05-04 13:58:29 -0700523 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 Sehrbeca4fe2017-03-30 17:50:24 -0700533 }
534 }
David Sehr55232f12017-04-19 14:06:49 -0700535 if (!match_found) {
David Sehr592f8022017-05-04 13:58:29 -0700536 std::cerr << "No relevant memory maps were found." << std::endl;
David Sehr55232f12017-04-19 14:06:49 -0700537 return EXIT_FAILURE;
538 }
539#endif
David Sehrbeca4fe2017-03-30 17:50:24 -0700540
David Sehr3f3ec672017-04-05 14:43:38 -0700541 return EXIT_SUCCESS;
David Sehrbeca4fe2017-03-30 17:50:24 -0700542}
543
544} // namespace art
545
546int main(int argc, char* argv[]) {
547 return art::DexDiagMain(argc, argv);
548}