blob: b8a72afb68f489d7673b0f600658720092eaf3cf [file] [log] [blame]
Igor Murashkin37743352014-11-13 14:38:00 -08001/*
2 * Copyright (C) 2014 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 <stdio.h>
18#include <stdlib.h>
19
20#include <fstream>
21#include <iostream>
22#include <string>
23#include <vector>
24#include <set>
25#include <map>
26
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "art_method-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080028#include "base/unix_file/fd_file.h"
29#include "base/stringprintf.h"
30#include "gc/space/image_space.h"
31#include "gc/heap.h"
32#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080034#include "image.h"
35#include "scoped_thread_state_change.h"
36#include "os.h"
37#include "gc_map.h"
38
39#include "cmdline.h"
40#include "backtrace/BacktraceMap.h"
41
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <signal.h>
45
46namespace art {
47
48class ImgDiagDumper {
49 public:
50 explicit ImgDiagDumper(std::ostream* os,
51 const ImageHeader& image_header,
52 const char* image_location,
53 pid_t image_diff_pid)
54 : os_(os),
55 image_header_(image_header),
56 image_location_(image_location),
57 image_diff_pid_(image_diff_pid) {}
58
Mathieu Chartier90443472015-07-16 20:32:27 -070059 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -080060 std::ostream& os = *os_;
61 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
62
63 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
64
65 bool ret = true;
66 if (image_diff_pid_ >= 0) {
67 os << "IMAGE DIFF PID (" << image_diff_pid_ << "): ";
68 ret = DumpImageDiff(image_diff_pid_);
69 os << "\n\n";
70 } else {
71 os << "IMAGE DIFF PID: disabled\n\n";
72 }
73
74 os << std::flush;
75
76 return ret;
77 }
78
79 private:
80 static bool EndsWith(const std::string& str, const std::string& suffix) {
81 return str.size() >= suffix.size() &&
82 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
83 }
84
85 // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
86 static std::string BaseName(const std::string& str) {
87 size_t idx = str.rfind("/");
88 if (idx == std::string::npos) {
89 return str;
90 }
91
92 return str.substr(idx + 1);
93 }
94
Mathieu Chartier90443472015-07-16 20:32:27 -070095 bool DumpImageDiff(pid_t image_diff_pid) SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -080096 std::ostream& os = *os_;
97
98 {
99 struct stat sts;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700100 std::string proc_pid_str =
101 StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int]
Igor Murashkin37743352014-11-13 14:38:00 -0800102 if (stat(proc_pid_str.c_str(), &sts) == -1) {
103 os << "Process does not exist";
104 return false;
105 }
106 }
107
108 // Open /proc/$pid/maps to view memory maps
109 auto proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid));
110 if (proc_maps == nullptr) {
111 os << "Could not read backtrace maps";
112 return false;
113 }
114
115 bool found_boot_map = false;
116 backtrace_map_t boot_map = backtrace_map_t();
117 // Find the memory map only for boot.art
118 for (const backtrace_map_t& map : *proc_maps) {
119 if (EndsWith(map.name, GetImageLocationBaseName())) {
120 if ((map.flags & PROT_WRITE) != 0) {
121 boot_map = map;
122 found_boot_map = true;
123 break;
124 }
125 // In actuality there's more than 1 map, but the second one is read-only.
126 // The one we care about is the write-able map.
127 // The readonly maps are guaranteed to be identical, so its not interesting to compare
128 // them.
129 }
130 }
131
132 if (!found_boot_map) {
133 os << "Could not find map for " << GetImageLocationBaseName();
134 return false;
135 }
136
137 // Future idea: diff against zygote so we can ignore the shared dirty pages.
138 return DumpImageDiffMap(image_diff_pid, boot_map);
139 }
140
141 // Look at /proc/$pid/mem and only diff the things from there
142 bool DumpImageDiffMap(pid_t image_diff_pid, const backtrace_map_t& boot_map)
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -0800144 std::ostream& os = *os_;
145 const size_t pointer_size = InstructionSetPointerSize(
146 Runtime::Current()->GetInstructionSet());
147
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700148 std::string file_name =
149 StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int]
Igor Murashkin37743352014-11-13 14:38:00 -0800150
151 size_t boot_map_size = boot_map.end - boot_map.start;
152
153 // Open /proc/$pid/mem as a file
154 auto map_file = std::unique_ptr<File>(OS::OpenFileForReading(file_name.c_str()));
155 if (map_file == nullptr) {
156 os << "Failed to open " << file_name << " for reading";
157 return false;
158 }
159
160 // Memory-map /proc/$pid/mem subset from the boot map
161 CHECK(boot_map.end >= boot_map.start);
162
163 std::string error_msg;
164
165 // Walk the bytes and diff against our boot image
166 const ImageHeader& boot_image_header = GetBootImageHeader();
167
168 os << "\nObserving boot image header at address "
169 << reinterpret_cast<const void*>(&boot_image_header)
170 << "\n\n";
171
172 const uint8_t* image_begin_unaligned = boot_image_header.GetImageBegin();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700173 const uint8_t* image_mirror_end_unaligned = image_begin_unaligned +
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 boot_image_header.GetImageSection(ImageHeader::kSectionObjects).Size();
175 const uint8_t* image_end_unaligned = image_begin_unaligned + boot_image_header.GetImageSize();
Igor Murashkin37743352014-11-13 14:38:00 -0800176
177 // Adjust range to nearest page
178 const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
179 const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
180
181 ptrdiff_t page_off_begin = boot_image_header.GetImageBegin() - image_begin;
182
183 if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start ||
184 reinterpret_cast<uintptr_t>(image_end) < boot_map.end) {
185 // Sanity check that we aren't trying to read a completely different boot image
186 os << "Remote boot map is out of range of local boot map: " <<
187 "local begin " << reinterpret_cast<const void*>(image_begin) <<
188 ", local end " << reinterpret_cast<const void*>(image_end) <<
189 ", remote begin " << reinterpret_cast<const void*>(boot_map.start) <<
190 ", remote end " << reinterpret_cast<const void*>(boot_map.end);
191 return false;
192 // If we wanted even more validation we could map the ImageHeader from the file
193 }
194
195 std::vector<uint8_t> remote_contents(boot_map_size);
196 if (!map_file->PreadFully(&remote_contents[0], boot_map_size, boot_map.start)) {
197 os << "Could not fully read file " << file_name;
198 return false;
199 }
200
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700201 std::string page_map_file_name = StringPrintf(
202 "/proc/%ld/pagemap", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int]
Igor Murashkin37743352014-11-13 14:38:00 -0800203 auto page_map_file = std::unique_ptr<File>(OS::OpenFileForReading(page_map_file_name.c_str()));
204 if (page_map_file == nullptr) {
205 os << "Failed to open " << page_map_file_name << " for reading: " << strerror(errno);
206 return false;
207 }
208
209 // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough
210 const char* clean_page_map_file_name = "/proc/self/pagemap";
211 auto clean_page_map_file = std::unique_ptr<File>(
212 OS::OpenFileForReading(clean_page_map_file_name));
213 if (clean_page_map_file == nullptr) {
214 os << "Failed to open " << clean_page_map_file_name << " for reading: " << strerror(errno);
215 return false;
216 }
217
218 auto kpage_flags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags"));
219 if (kpage_flags_file == nullptr) {
220 os << "Failed to open /proc/kpageflags for reading: " << strerror(errno);
221 return false;
222 }
223
224 auto kpage_count_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount"));
225 if (kpage_count_file == nullptr) {
226 os << "Failed to open /proc/kpagecount for reading:" << strerror(errno);
227 return false;
228 }
229
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700230 // Set of the remote virtual page indices that are dirty
231 std::set<size_t> dirty_page_set_remote;
232 // Set of the local virtual page indices that are dirty
233 std::set<size_t> dirty_page_set_local;
Igor Murashkin37743352014-11-13 14:38:00 -0800234
235 size_t different_int32s = 0;
236 size_t different_bytes = 0;
237 size_t different_pages = 0;
238 size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address)
239 size_t page_idx = 0; // Page index relative to 0
240 size_t previous_page_idx = 0; // Previous page index relative to 0
241 size_t dirty_pages = 0;
242 size_t private_pages = 0;
243 size_t private_dirty_pages = 0;
244
245 // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
246 for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) {
247 ptrdiff_t offset = begin - boot_map.start;
248
249 // We treat the image header as part of the memory map for now
250 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
251 // But it might still be interesting to see if any of the ImageHeader data mutated
252 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
253 uint8_t* remote_ptr = &remote_contents[offset];
254
255 if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
256 different_pages++;
257
258 // Count the number of 32-bit integers that are different.
259 for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
260 uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr);
261 const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
262
263 if (remote_ptr_int32[i] != local_ptr_int32[i]) {
264 different_int32s++;
265 }
266 }
267 }
268 }
269
270 // Iterate through one byte at a time.
271 for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) {
272 previous_page_idx = page_idx;
273 ptrdiff_t offset = begin - boot_map.start;
274
275 // We treat the image header as part of the memory map for now
276 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
277 // But it might still be interesting to see if any of the ImageHeader data mutated
278 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
279 uint8_t* remote_ptr = &remote_contents[offset];
280
281 virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
282
283 // Calculate the page index, relative to the 0th page where the image begins
284 page_idx = (offset + page_off_begin) / kPageSize;
285 if (*local_ptr != *remote_ptr) {
286 // Track number of bytes that are different
287 different_bytes++;
288 }
289
290 // Independently count the # of dirty pages on the remote side
291 size_t remote_virtual_page_idx = begin / kPageSize;
292 if (previous_page_idx != page_idx) {
293 uint64_t page_count = 0xC0FFEE;
294 // TODO: virtual_page_idx needs to be from the same process
295 int dirtiness = (IsPageDirty(page_map_file.get(), // Image-diff-pid procmap
296 clean_page_map_file.get(), // Self procmap
297 kpage_flags_file.get(),
298 kpage_count_file.get(),
299 remote_virtual_page_idx, // potentially "dirty" page
300 virtual_page_idx, // true "clean" page
301 &page_count,
302 &error_msg));
303 if (dirtiness < 0) {
304 os << error_msg;
305 return false;
306 } else if (dirtiness > 0) {
307 dirty_pages++;
308 dirty_page_set_remote.insert(dirty_page_set_remote.end(), remote_virtual_page_idx);
309 dirty_page_set_local.insert(dirty_page_set_local.end(), virtual_page_idx);
310 }
311
312 bool is_dirty = dirtiness > 0;
313 bool is_private = page_count == 1;
314
315 if (page_count == 1) {
316 private_pages++;
317 }
318
319 if (is_dirty && is_private) {
320 private_dirty_pages++;
321 }
322 }
323 }
324
325 // Walk each object in the remote image space and compare it against ours
326 size_t different_objects = 0;
327 std::map<mirror::Class*, int /*count*/> dirty_object_class_map;
328 // Track only the byte-per-byte dirtiness (in bytes)
329 std::map<mirror::Class*, int /*byte_count*/> dirty_object_byte_count;
330 // Track the object-by-object dirtiness (in bytes)
331 std::map<mirror::Class*, int /*byte_count*/> dirty_object_size_in_bytes;
332 std::map<mirror::Class*, int /*count*/> clean_object_class_map;
333
334 std::map<mirror::Class*, std::string> class_to_descriptor_map;
335
336 std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 std::vector<ArtMethod*> art_method_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800338
339 std::map<off_t /* field offset */, int /* count */> class_field_dirty_count;
340 std::vector<mirror::Class*> class_dirty_objects;
341
342 // List of local objects that are clean, but located on dirty pages.
343 std::vector<mirror::Object*> false_dirty_objects;
344 std::map<mirror::Class*, int /*byte_count*/> false_dirty_byte_count;
345 std::map<mirror::Class*, int /*object_count*/> false_dirty_object_count;
346 std::map<mirror::Class*, std::vector<mirror::Object*>> false_dirty_objects_map;
347 size_t false_dirty_object_bytes = 0;
348
349 // Remote pointers to dirty objects
350 std::map<mirror::Class*, std::vector<mirror::Object*>> dirty_objects_by_class;
351 // Look up remote classes by their descriptor
352 std::map<std::string, mirror::Class*> remote_class_map;
353 // Look up local classes by their descriptor
354 std::map<std::string, mirror::Class*> local_class_map;
355
356 size_t dirty_object_bytes = 0;
357 {
358 const uint8_t* begin_image_ptr = image_begin_unaligned;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700359 const uint8_t* end_image_ptr = image_mirror_end_unaligned;
Igor Murashkin37743352014-11-13 14:38:00 -0800360
361 const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment);
362 while (reinterpret_cast<const uintptr_t>(current)
363 < reinterpret_cast<const uintptr_t>(end_image_ptr)) {
364 CHECK_ALIGNED(current, kObjectAlignment);
365 mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current));
366
367 // Sanity check that we are reading a real object
368 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
369 if (kUseBakerOrBrooksReadBarrier) {
370 obj->AssertReadBarrierPointer();
371 }
372
373 // Iterate every page this object belongs to
374 bool on_dirty_page = false;
375 size_t page_off = 0;
376 size_t current_page_idx;
377 uintptr_t object_address;
378 do {
379 object_address = reinterpret_cast<uintptr_t>(current);
380 current_page_idx = object_address / kPageSize + page_off;
381
382 if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) {
383 // This object is on a dirty page
384 on_dirty_page = true;
385 }
386
387 page_off++;
388 } while ((current_page_idx * kPageSize) <
389 RoundUp(object_address + obj->SizeOf(), kObjectAlignment));
390
391 mirror::Class* klass = obj->GetClass();
392
393 bool different_object = false;
394
395 // Check against the other object and see if they are different
396 ptrdiff_t offset = current - begin_image_ptr;
397 const uint8_t* current_remote = &remote_contents[offset];
398 mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(
399 const_cast<uint8_t*>(current_remote));
400 if (memcmp(current, current_remote, obj->SizeOf()) != 0) {
401 different_objects++;
402 dirty_object_bytes += obj->SizeOf();
403
404 ++dirty_object_class_map[klass];
405
406 // Go byte-by-byte and figure out what exactly got dirtied
407 size_t dirty_byte_count_per_object = 0;
408 for (size_t i = 0; i < obj->SizeOf(); ++i) {
409 if (current[i] != current_remote[i]) {
410 dirty_byte_count_per_object++;
411 }
412 }
413 dirty_object_byte_count[klass] += dirty_byte_count_per_object;
414 dirty_object_size_in_bytes[klass] += obj->SizeOf();
415
416 different_object = true;
417
418 dirty_objects_by_class[klass].push_back(remote_obj);
419 } else {
420 ++clean_object_class_map[klass];
421 }
422
423 std::string descriptor = GetClassDescriptor(klass);
424 if (different_object) {
425 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
426 // this is a "Class"
427 mirror::Class* obj_as_class = reinterpret_cast<mirror::Class*>(remote_obj);
428
429 // print the fields that are dirty
430 for (size_t i = 0; i < obj->SizeOf(); ++i) {
431 if (current[i] != current_remote[i]) {
432 class_field_dirty_count[i]++;
433 }
434 }
435
436 class_dirty_objects.push_back(obj_as_class);
437 } else if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
438 // this is an ArtMethod
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 ArtMethod* art_method = reinterpret_cast<ArtMethod*>(remote_obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800440
441 // print the fields that are dirty
442 for (size_t i = 0; i < obj->SizeOf(); ++i) {
443 if (current[i] != current_remote[i]) {
444 art_method_field_dirty_count[i]++;
445 }
446 }
447
448 art_method_dirty_objects.push_back(art_method);
449 }
450 } else if (on_dirty_page) {
451 // This object was either never mutated or got mutated back to the same value.
452 // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
453 false_dirty_objects.push_back(obj);
454 false_dirty_objects_map[klass].push_back(obj);
455 false_dirty_object_bytes += obj->SizeOf();
456 false_dirty_byte_count[obj->GetClass()] += obj->SizeOf();
457 false_dirty_object_count[obj->GetClass()] += 1;
458 }
459
460 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
461 local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj);
462 remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj);
463 }
464
465 // Unconditionally store the class descriptor in case we need it later
466 class_to_descriptor_map[klass] = descriptor;
467 current += RoundUp(obj->SizeOf(), kObjectAlignment);
468 }
469 }
470
471 // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects.
472 float true_dirtied_percent = dirty_object_bytes * 1.0f / (dirty_pages * kPageSize);
473 size_t false_dirty_pages = dirty_pages - different_pages;
474
475 os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", "
476 << reinterpret_cast<void*>(boot_map.end) << ") had: \n "
477 << different_bytes << " differing bytes, \n "
478 << different_int32s << " differing int32s, \n "
479 << different_objects << " different objects, \n "
480 << dirty_object_bytes << " different object [bytes], \n "
481 << false_dirty_objects.size() << " false dirty objects,\n "
482 << false_dirty_object_bytes << " false dirty object [bytes], \n "
483 << true_dirtied_percent << " different objects-vs-total in a dirty page;\n "
484 << different_pages << " different pages; \n "
485 << dirty_pages << " pages are dirty; \n "
486 << false_dirty_pages << " pages are false dirty; \n "
487 << private_pages << " pages are private; \n "
488 << private_dirty_pages << " pages are Private_Dirty\n "
489 << "";
490
491 // vector of pairs (int count, Class*)
492 auto dirty_object_class_values = SortByValueDesc(dirty_object_class_map);
493 auto clean_object_class_values = SortByValueDesc(clean_object_class_map);
494
495 os << "\n" << " Dirty object count by class:\n";
496 for (const auto& vk_pair : dirty_object_class_values) {
497 int dirty_object_count = vk_pair.first;
498 mirror::Class* klass = vk_pair.second;
499 int object_sizes = dirty_object_size_in_bytes[klass];
500 float avg_dirty_bytes_per_class = dirty_object_byte_count[klass] * 1.0f / object_sizes;
501 float avg_object_size = object_sizes * 1.0f / dirty_object_count;
502 const std::string& descriptor = class_to_descriptor_map[klass];
503 os << " " << PrettyClass(klass) << " ("
504 << "objects: " << dirty_object_count << ", "
505 << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
506 << "avg object size: " << avg_object_size << ", "
507 << "class descriptor: '" << descriptor << "'"
508 << ")\n";
509
510 constexpr size_t kMaxAddressPrint = 5;
511 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
512 os << " sample object addresses: ";
513 for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
514 auto art_method = art_method_dirty_objects[i];
515
516 os << reinterpret_cast<void*>(art_method) << ", ";
517 }
518 os << "\n";
519
520 os << " dirty byte +offset:count list = ";
521 auto art_method_field_dirty_count_sorted = SortByValueDesc(art_method_field_dirty_count);
522 for (auto pair : art_method_field_dirty_count_sorted) {
523 off_t offset = pair.second;
524 int count = pair.first;
525
526 os << "+" << offset << ":" << count << ", ";
527 }
528
529 os << "\n";
530
531 os << " field contents:\n";
532 const auto& dirty_objects_list = dirty_objects_by_class[klass];
533 for (mirror::Object* obj : dirty_objects_list) {
534 // remote method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700535 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800536
537 // remote class
538 mirror::Class* remote_declaring_class =
539 FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents, boot_map);
540
541 // local class
542 mirror::Class* declaring_class =
543 RemoteContentsPointerToLocal(remote_declaring_class,
544 remote_contents,
545 boot_image_header);
546
547 os << " " << reinterpret_cast<void*>(obj) << " ";
548 os << " entryPointFromJni: "
549 << reinterpret_cast<const void*>(
550 art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800551 os << " entryPointFromQuickCompiledCode: "
552 << reinterpret_cast<const void*>(
553 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
554 << ", ";
555 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
556 os << " class_status (local): " << declaring_class->GetStatus();
557 os << " class_status (remote): " << remote_declaring_class->GetStatus();
558 os << "\n";
559 }
560 }
561 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
562 os << " sample object addresses: ";
563 for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
564 auto class_ptr = class_dirty_objects[i];
565
566 os << reinterpret_cast<void*>(class_ptr) << ", ";
567 }
568 os << "\n";
569
570 os << " dirty byte +offset:count list = ";
571 auto class_field_dirty_count_sorted = SortByValueDesc(class_field_dirty_count);
572 for (auto pair : class_field_dirty_count_sorted) {
573 off_t offset = pair.second;
574 int count = pair.first;
575
576 os << "+" << offset << ":" << count << ", ";
577 }
578 os << "\n";
579
580 os << " field contents:\n";
581 const auto& dirty_objects_list = dirty_objects_by_class[klass];
582 for (mirror::Object* obj : dirty_objects_list) {
583 // remote class object
584 auto remote_klass = reinterpret_cast<mirror::Class*>(obj);
585
586 // local class object
587 auto local_klass = RemoteContentsPointerToLocal(remote_klass,
588 remote_contents,
589 boot_image_header);
590
591 os << " " << reinterpret_cast<void*>(obj) << " ";
592 os << " class_status (remote): " << remote_klass->GetStatus() << ", ";
593 os << " class_status (local): " << local_klass->GetStatus();
594 os << "\n";
595 }
596 }
597 }
598
599 auto false_dirty_object_class_values = SortByValueDesc(false_dirty_object_count);
600
601 os << "\n" << " False-dirty object count by class:\n";
602 for (const auto& vk_pair : false_dirty_object_class_values) {
603 int object_count = vk_pair.first;
604 mirror::Class* klass = vk_pair.second;
605 int object_sizes = false_dirty_byte_count[klass];
606 float avg_object_size = object_sizes * 1.0f / object_count;
607 const std::string& descriptor = class_to_descriptor_map[klass];
608 os << " " << PrettyClass(klass) << " ("
609 << "objects: " << object_count << ", "
610 << "avg object size: " << avg_object_size << ", "
611 << "total bytes: " << object_sizes << ", "
612 << "class descriptor: '" << descriptor << "'"
613 << ")\n";
614
615 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
616 auto& art_method_false_dirty_objects = false_dirty_objects_map[klass];
617
618 os << " field contents:\n";
619 for (mirror::Object* obj : art_method_false_dirty_objects) {
620 // local method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700621 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800622
623 // local class
624 mirror::Class* declaring_class = art_method->GetDeclaringClass();
625
626 os << " " << reinterpret_cast<void*>(obj) << " ";
627 os << " entryPointFromJni: "
628 << reinterpret_cast<const void*>(
629 art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800630 os << " entryPointFromQuickCompiledCode: "
631 << reinterpret_cast<const void*>(
632 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
633 << ", ";
634 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
635 os << " class_status (local): " << declaring_class->GetStatus();
636 os << "\n";
637 }
638 }
639 }
640
641 os << "\n" << " Clean object count by class:\n";
642 for (const auto& vk_pair : clean_object_class_values) {
643 os << " " << PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
644 }
645
646 return true;
647 }
648
649 // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
650 // Returned pointer will point to inside of remote_contents.
651 template <typename T>
652 static T* FixUpRemotePointer(T* remote_ptr,
653 std::vector<uint8_t>& remote_contents,
654 const backtrace_map_t& boot_map) {
655 if (remote_ptr == nullptr) {
656 return nullptr;
657 }
658
659 uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr);
660
661 CHECK_LE(boot_map.start, remote);
662 CHECK_GT(boot_map.end, remote);
663
664 off_t boot_offset = remote - boot_map.start;
665
666 return reinterpret_cast<T*>(&remote_contents[boot_offset]);
667 }
668
669 template <typename T>
670 static T* RemoteContentsPointerToLocal(T* remote_ptr,
671 std::vector<uint8_t>& remote_contents,
672 const ImageHeader& image_header) {
673 if (remote_ptr == nullptr) {
674 return nullptr;
675 }
676
677 uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr);
678 ptrdiff_t boot_offset = remote - &remote_contents[0];
679
680 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
681
682 return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
683 }
684
685 static std::string GetClassDescriptor(mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700686 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -0800687 CHECK(klass != nullptr);
688
689 std::string descriptor;
690 const char* descriptor_str = klass->GetDescriptor(&descriptor);
691
692 return std::string(descriptor_str);
693 }
694
695 template <typename K, typename V>
696 static std::vector<std::pair<V, K>> SortByValueDesc(const std::map<K, V> map) {
697 // Store value->key so that we can use the default sort from pair which
698 // sorts by value first and then key
699 std::vector<std::pair<V, K>> value_key_vector;
700
701 for (const auto& kv_pair : map) {
702 value_key_vector.push_back(std::make_pair(kv_pair.second, kv_pair.first));
703 }
704
705 // Sort in reverse (descending order)
706 std::sort(value_key_vector.rbegin(), value_key_vector.rend());
707 return value_key_vector;
708 }
709
710 static bool GetPageFrameNumber(File* page_map_file,
711 size_t virtual_page_index,
712 uint64_t* page_frame_number,
713 std::string* error_msg) {
714 CHECK(page_map_file != nullptr);
715 CHECK(page_frame_number != nullptr);
716 CHECK(error_msg != nullptr);
717
718 constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
719 constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; // bits 0-54 [in /proc/$pid/pagemap]
720 constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55); // bit 55 [in /proc/$pid/pagemap]
721
722 uint64_t page_map_entry = 0;
723
724 // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number
725 if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize,
726 virtual_page_index * kPageMapEntrySize)) {
727 *error_msg = StringPrintf("Failed to read the virtual page index entry from %s",
728 page_map_file->GetPath().c_str());
729 return false;
730 }
731
732 // TODO: seems useless, remove this.
733 bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0;
734 if ((false)) {
735 LOG(VERBOSE) << soft_dirty; // Suppress unused warning
736 UNREACHABLE();
737 }
738
739 *page_frame_number = page_map_entry & kPageFrameNumberMask;
740
741 return true;
742 }
743
744 static int IsPageDirty(File* page_map_file,
745 File* clean_page_map_file,
746 File* kpage_flags_file,
747 File* kpage_count_file,
748 size_t virtual_page_idx,
749 size_t clean_virtual_page_idx,
750 // Out parameters:
751 uint64_t* page_count, std::string* error_msg) {
752 CHECK(page_map_file != nullptr);
753 CHECK(clean_page_map_file != nullptr);
754 CHECK_NE(page_map_file, clean_page_map_file);
755 CHECK(kpage_flags_file != nullptr);
756 CHECK(kpage_count_file != nullptr);
757 CHECK(page_count != nullptr);
758 CHECK(error_msg != nullptr);
759
760 // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
761
762 constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
763 constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
764 constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags
765 constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags
766 constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags
767
768 uint64_t page_frame_number = 0;
769 if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
770 return -1;
771 }
772
773 uint64_t page_frame_number_clean = 0;
774 if (!GetPageFrameNumber(clean_page_map_file, clean_virtual_page_idx, &page_frame_number_clean,
775 error_msg)) {
776 return -1;
777 }
778
779 // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
780 uint64_t kpage_flags_entry = 0;
781 if (!kpage_flags_file->PreadFully(&kpage_flags_entry,
782 kPageFlagsEntrySize,
783 page_frame_number * kPageFlagsEntrySize)) {
784 *error_msg = StringPrintf("Failed to read the page flags from %s",
785 kpage_flags_file->GetPath().c_str());
786 return -1;
787 }
788
789 // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
790 if (!kpage_count_file->PreadFully(page_count /*out*/,
791 kPageCountEntrySize,
792 page_frame_number * kPageCountEntrySize)) {
793 *error_msg = StringPrintf("Failed to read the page count from %s",
794 kpage_count_file->GetPath().c_str());
795 return -1;
796 }
797
798 // There must be a page frame at the requested address.
799 CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
800 // The page frame must be memory mapped
801 CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
802
803 // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
804 bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
805
806 // page_frame_number_clean must come from the *same* process
807 // but a *different* mmap than page_frame_number
808 if (flags_dirty) {
809 CHECK_NE(page_frame_number, page_frame_number_clean);
810 }
811
812 return page_frame_number != page_frame_number_clean;
813 }
814
815 static const ImageHeader& GetBootImageHeader() {
816 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800817 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
818 CHECK(!image_spaces.empty());
819 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Igor Murashkin37743352014-11-13 14:38:00 -0800820 return image_header;
821 }
822
823 private:
824 // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
825 std::string GetImageLocationBaseName() const {
826 return BaseName(std::string(image_location_));
827 }
828
829 std::ostream* os_;
830 const ImageHeader& image_header_;
831 const char* image_location_;
832 pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative
833
834 DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
835};
836
Jeff Haodcdc85b2015-12-04 14:06:18 -0800837static int DumpImage(Runtime* runtime, std::ostream* os, pid_t image_diff_pid) {
Igor Murashkin37743352014-11-13 14:38:00 -0800838 ScopedObjectAccess soa(Thread::Current());
839 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800840 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
841 CHECK(!image_spaces.empty());
842 for (gc::space::ImageSpace* image_space : image_spaces) {
843 const ImageHeader& image_header = image_space->GetImageHeader();
844 if (!image_header.IsValid()) {
845 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
846 return EXIT_FAILURE;
847 }
848
849 ImgDiagDumper img_diag_dumper(
850 os, image_header, image_space->GetImageLocation().c_str(), image_diff_pid);
851 if (!img_diag_dumper.Dump()) {
852 return EXIT_FAILURE;
853 }
Igor Murashkin37743352014-11-13 14:38:00 -0800854 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800855 return EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -0800856}
857
858struct ImgDiagArgs : public CmdlineArgs {
859 protected:
860 using Base = CmdlineArgs;
861
862 virtual ParseStatus ParseCustom(const StringPiece& option,
863 std::string* error_msg) OVERRIDE {
864 {
865 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
866 if (base_parse != kParseUnknownArgument) {
867 return base_parse;
868 }
869 }
870
871 if (option.starts_with("--image-diff-pid=")) {
872 const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data();
873
874 if (!ParseInt(image_diff_pid, &image_diff_pid_)) {
875 *error_msg = "Image diff pid out of range";
876 return kParseError;
877 }
878 } else {
879 return kParseUnknownArgument;
880 }
881
882 return kParseOk;
883 }
884
885 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
886 // Perform the parent checks.
887 ParseStatus parent_checks = Base::ParseChecks(error_msg);
888 if (parent_checks != kParseOk) {
889 return parent_checks;
890 }
891
892 // Perform our own checks.
893
894 if (kill(image_diff_pid_,
895 /*sig*/0) != 0) { // No signal is sent, perform error-checking only.
896 // Check if the pid exists before proceeding.
897 if (errno == ESRCH) {
898 *error_msg = "Process specified does not exist";
899 } else {
900 *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
901 }
902 return kParseError;
903 } else if (instruction_set_ != kRuntimeISA) {
904 // Don't allow different ISAs since the images are ISA-specific.
905 // Right now the code assumes both the runtime ISA and the remote ISA are identical.
906 *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
907 return kParseError;
908 }
909
910 return kParseOk;
911 }
912
913 virtual std::string GetUsage() const {
914 std::string usage;
915
916 usage +=
917 "Usage: imgdiag [options] ...\n"
918 " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
919 " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
920 "\n";
921
922 usage += Base::GetUsage();
923
924 usage += // Optional.
925 " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
926 " Example: --image-diff-pid=$(pid zygote)\n"
927 "\n";
928
929 return usage;
930 }
931
932 public:
933 pid_t image_diff_pid_ = -1;
934};
935
936struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
937 virtual bool ExecuteWithRuntime(Runtime* runtime) {
938 CHECK(args_ != nullptr);
939
940 return DumpImage(runtime,
Igor Murashkin37743352014-11-13 14:38:00 -0800941 args_->os_,
942 args_->image_diff_pid_) == EXIT_SUCCESS;
943 }
944};
945
946} // namespace art
947
948int main(int argc, char** argv) {
949 art::ImgDiagMain main;
950 return main.Main(argc, argv);
951}