1/*
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 <functional>
22#include <iostream>
23#include <string>
24#include <vector>
25#include <set>
26#include <map>
27#include <unordered_set>
28
29#include "art_method-inl.h"
30#include "base/unix_file/fd_file.h"
31#include "base/stringprintf.h"
32#include "gc/space/image_space.h"
33#include "gc/heap.h"
34#include "mirror/class-inl.h"
35#include "mirror/object-inl.h"
36#include "image.h"
37#include "scoped_thread_state_change.h"
38#include "os.h"
39
40#include "cmdline.h"
41#include "backtrace/BacktraceMap.h"
42
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <signal.h>
46
47namespace art {
48
49class ImgDiagDumper {
50 public:
51  explicit ImgDiagDumper(std::ostream* os,
52                         const ImageHeader& image_header,
53                         const std::string& image_location,
54                         pid_t image_diff_pid,
55                         pid_t zygote_diff_pid)
56      : os_(os),
57        image_header_(image_header),
58        image_location_(image_location),
59        image_diff_pid_(image_diff_pid),
60        zygote_diff_pid_(zygote_diff_pid) {}
61
62  bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
63    std::ostream& os = *os_;
64    os << "IMAGE LOCATION: " << image_location_ << "\n\n";
65
66    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
67
68    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
69
70    bool ret = true;
71    if (image_diff_pid_ >= 0) {
72      os << "IMAGE DIFF PID (" << image_diff_pid_ << "): ";
73      ret = DumpImageDiff(image_diff_pid_, zygote_diff_pid_);
74      os << "\n\n";
75    } else {
76      os << "IMAGE DIFF PID: disabled\n\n";
77    }
78
79    os << std::flush;
80
81    return ret;
82  }
83
84 private:
85  static bool EndsWith(const std::string& str, const std::string& suffix) {
86    return str.size() >= suffix.size() &&
87           str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
88  }
89
90  // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
91  static std::string BaseName(const std::string& str) {
92    size_t idx = str.rfind("/");
93    if (idx == std::string::npos) {
94      return str;
95    }
96
97    return str.substr(idx + 1);
98  }
99
100  bool DumpImageDiff(pid_t image_diff_pid, pid_t zygote_diff_pid)
101      SHARED_REQUIRES(Locks::mutator_lock_) {
102    std::ostream& os = *os_;
103
104    {
105      struct stat sts;
106      std::string proc_pid_str =
107          StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid));  // NOLINT [runtime/int]
108      if (stat(proc_pid_str.c_str(), &sts) == -1) {
109        os << "Process does not exist";
110        return false;
111      }
112    }
113
114    // Open /proc/$pid/maps to view memory maps
115    auto proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid));
116    if (proc_maps == nullptr) {
117      os << "Could not read backtrace maps";
118      return false;
119    }
120
121    bool found_boot_map = false;
122    backtrace_map_t boot_map = backtrace_map_t();
123    // Find the memory map only for boot.art
124    for (const backtrace_map_t& map : *proc_maps) {
125      if (EndsWith(map.name, GetImageLocationBaseName())) {
126        if ((map.flags & PROT_WRITE) != 0) {
127          boot_map = map;
128          found_boot_map = true;
129          break;
130        }
131        // In actuality there's more than 1 map, but the second one is read-only.
132        // The one we care about is the write-able map.
133        // The readonly maps are guaranteed to be identical, so its not interesting to compare
134        // them.
135      }
136    }
137
138    if (!found_boot_map) {
139      os << "Could not find map for " << GetImageLocationBaseName();
140      return false;
141    }
142
143    // Future idea: diff against zygote so we can ignore the shared dirty pages.
144    return DumpImageDiffMap(image_diff_pid, zygote_diff_pid, boot_map);
145  }
146
147  static std::string PrettyFieldValue(ArtField* field, mirror::Object* obj)
148      SHARED_REQUIRES(Locks::mutator_lock_) {
149    std::ostringstream oss;
150    switch (field->GetTypeAsPrimitiveType()) {
151      case Primitive::kPrimNot: {
152        oss << obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
153            field->GetOffset());
154        break;
155      }
156      case Primitive::kPrimBoolean: {
157        oss << static_cast<bool>(obj->GetFieldBoolean<kVerifyNone>(field->GetOffset()));
158        break;
159      }
160      case Primitive::kPrimByte: {
161        oss << static_cast<int32_t>(obj->GetFieldByte<kVerifyNone>(field->GetOffset()));
162        break;
163      }
164      case Primitive::kPrimChar: {
165        oss << obj->GetFieldChar<kVerifyNone>(field->GetOffset());
166        break;
167      }
168      case Primitive::kPrimShort: {
169        oss << obj->GetFieldShort<kVerifyNone>(field->GetOffset());
170        break;
171      }
172      case Primitive::kPrimInt: {
173        oss << obj->GetField32<kVerifyNone>(field->GetOffset());
174        break;
175      }
176      case Primitive::kPrimLong: {
177        oss << obj->GetField64<kVerifyNone>(field->GetOffset());
178        break;
179      }
180      case Primitive::kPrimFloat: {
181        oss << obj->GetField32<kVerifyNone>(field->GetOffset());
182        break;
183      }
184      case Primitive::kPrimDouble: {
185        oss << obj->GetField64<kVerifyNone>(field->GetOffset());
186        break;
187      }
188      case Primitive::kPrimVoid: {
189        oss << "void";
190        break;
191      }
192    }
193    return oss.str();
194  }
195
196  // Aggregate and detail class data from an image diff.
197  struct ClassData {
198    int dirty_object_count = 0;
199
200    // Track only the byte-per-byte dirtiness (in bytes)
201    int dirty_object_byte_count = 0;
202
203    // Track the object-by-object dirtiness (in bytes)
204    int dirty_object_size_in_bytes = 0;
205
206    int clean_object_count = 0;
207
208    std::string descriptor;
209
210    int false_dirty_byte_count = 0;
211    int false_dirty_object_count = 0;
212    std::vector<mirror::Object*> false_dirty_objects;
213
214    // Remote pointers to dirty objects
215    std::vector<mirror::Object*> dirty_objects;
216  };
217
218  void DiffObjectContents(mirror::Object* obj,
219                          uint8_t* remote_bytes,
220                          std::ostream& os) SHARED_REQUIRES(Locks::mutator_lock_) {
221    const char* tabs = "    ";
222    // Attempt to find fields for all dirty bytes.
223    mirror::Class* klass = obj->GetClass();
224    if (obj->IsClass()) {
225      os << tabs << "Class " << PrettyClass(obj->AsClass()) << " " << obj << "\n";
226    } else {
227      os << tabs << "Instance of " << PrettyClass(klass) << " " << obj << "\n";
228    }
229
230    std::unordered_set<ArtField*> dirty_instance_fields;
231    std::unordered_set<ArtField*> dirty_static_fields;
232    const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj);
233    mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(remote_bytes);
234    for (size_t i = 0, count = obj->SizeOf(); i < count; ++i) {
235      if (obj_bytes[i] != remote_bytes[i]) {
236        ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i);
237        if (field != nullptr) {
238          dirty_instance_fields.insert(field);
239        } else if (obj->IsClass()) {
240          field = ArtField::FindStaticFieldWithOffset</*exact*/false>(obj->AsClass(), i);
241          if (field != nullptr) {
242            dirty_static_fields.insert(field);
243          }
244        }
245        if (field == nullptr) {
246          if (klass->IsArrayClass()) {
247            mirror::Class* component_type = klass->GetComponentType();
248            Primitive::Type primitive_type = component_type->GetPrimitiveType();
249            size_t component_size = Primitive::ComponentSize(primitive_type);
250            size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
251            if (i >= data_offset) {
252              os << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n";
253              // Skip to next element to prevent spam.
254              i += component_size - 1;
255              continue;
256            }
257          }
258          os << tabs << "No field for byte offset " << i << "\n";
259        }
260      }
261    }
262    // Dump different fields. TODO: Dump field contents.
263    if (!dirty_instance_fields.empty()) {
264      os << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n";
265      for (ArtField* field : dirty_instance_fields) {
266        os << tabs << PrettyField(field)
267           << " original=" << PrettyFieldValue(field, obj)
268           << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
269      }
270    }
271    if (!dirty_static_fields.empty()) {
272      os << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n";
273      for (ArtField* field : dirty_static_fields) {
274        os << tabs << PrettyField(field)
275           << " original=" << PrettyFieldValue(field, obj)
276           << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
277      }
278    }
279    os << "\n";
280  }
281
282  // Look at /proc/$pid/mem and only diff the things from there
283  bool DumpImageDiffMap(pid_t image_diff_pid,
284                        pid_t zygote_diff_pid,
285                        const backtrace_map_t& boot_map)
286    SHARED_REQUIRES(Locks::mutator_lock_) {
287    std::ostream& os = *os_;
288    const size_t pointer_size = InstructionSetPointerSize(
289        Runtime::Current()->GetInstructionSet());
290
291    std::string file_name =
292        StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid));  // NOLINT [runtime/int]
293
294    size_t boot_map_size = boot_map.end - boot_map.start;
295
296    // Open /proc/$pid/mem as a file
297    auto map_file = std::unique_ptr<File>(OS::OpenFileForReading(file_name.c_str()));
298    if (map_file == nullptr) {
299      os << "Failed to open " << file_name << " for reading";
300      return false;
301    }
302
303    // Memory-map /proc/$pid/mem subset from the boot map
304    CHECK(boot_map.end >= boot_map.start);
305
306    std::string error_msg;
307
308    // Walk the bytes and diff against our boot image
309    const ImageHeader& boot_image_header = image_header_;
310
311    os << "\nObserving boot image header at address "
312       << reinterpret_cast<const void*>(&boot_image_header)
313       << "\n\n";
314
315    const uint8_t* image_begin_unaligned = boot_image_header.GetImageBegin();
316    const uint8_t* image_mirror_end_unaligned = image_begin_unaligned +
317        boot_image_header.GetImageSection(ImageHeader::kSectionObjects).Size();
318    const uint8_t* image_end_unaligned = image_begin_unaligned + boot_image_header.GetImageSize();
319
320    // Adjust range to nearest page
321    const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
322    const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
323
324    ptrdiff_t page_off_begin = boot_image_header.GetImageBegin() - image_begin;
325
326    if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start ||
327        reinterpret_cast<uintptr_t>(image_end) < boot_map.end) {
328      // Sanity check that we aren't trying to read a completely different boot image
329      os << "Remote boot map is out of range of local boot map: " <<
330        "local begin " << reinterpret_cast<const void*>(image_begin) <<
331        ", local end " << reinterpret_cast<const void*>(image_end) <<
332        ", remote begin " << reinterpret_cast<const void*>(boot_map.start) <<
333        ", remote end " << reinterpret_cast<const void*>(boot_map.end);
334      return false;
335      // If we wanted even more validation we could map the ImageHeader from the file
336    }
337
338    std::vector<uint8_t> remote_contents(boot_map_size);
339    if (!map_file->PreadFully(&remote_contents[0], boot_map_size, boot_map.start)) {
340      os << "Could not fully read file " << file_name;
341      return false;
342    }
343
344    std::vector<uint8_t> zygote_contents;
345    std::unique_ptr<File> zygote_map_file;
346    if (zygote_diff_pid != -1) {
347      std::string zygote_file_name =
348          StringPrintf("/proc/%ld/mem", static_cast<long>(zygote_diff_pid));  // NOLINT [runtime/int]
349      zygote_map_file.reset(OS::OpenFileForReading(zygote_file_name.c_str()));
350      // The boot map should be at the same address.
351      zygote_contents.resize(boot_map_size);
352      if (!zygote_map_file->PreadFully(&zygote_contents[0], boot_map_size, boot_map.start)) {
353        LOG(WARNING) << "Could not fully read zygote file " << zygote_file_name;
354        zygote_contents.clear();
355      }
356    }
357
358    std::string page_map_file_name = StringPrintf(
359        "/proc/%ld/pagemap", static_cast<long>(image_diff_pid));  // NOLINT [runtime/int]
360    auto page_map_file = std::unique_ptr<File>(OS::OpenFileForReading(page_map_file_name.c_str()));
361    if (page_map_file == nullptr) {
362      os << "Failed to open " << page_map_file_name << " for reading: " << strerror(errno);
363      return false;
364    }
365
366    // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough
367    const char* clean_page_map_file_name = "/proc/self/pagemap";
368    auto clean_page_map_file = std::unique_ptr<File>(
369        OS::OpenFileForReading(clean_page_map_file_name));
370    if (clean_page_map_file == nullptr) {
371      os << "Failed to open " << clean_page_map_file_name << " for reading: " << strerror(errno);
372      return false;
373    }
374
375    auto kpage_flags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags"));
376    if (kpage_flags_file == nullptr) {
377      os << "Failed to open /proc/kpageflags for reading: " << strerror(errno);
378      return false;
379    }
380
381    auto kpage_count_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount"));
382    if (kpage_count_file == nullptr) {
383      os << "Failed to open /proc/kpagecount for reading:" << strerror(errno);
384      return false;
385    }
386
387    // Set of the remote virtual page indices that are dirty
388    std::set<size_t> dirty_page_set_remote;
389    // Set of the local virtual page indices that are dirty
390    std::set<size_t> dirty_page_set_local;
391
392    size_t different_int32s = 0;
393    size_t different_bytes = 0;
394    size_t different_pages = 0;
395    size_t virtual_page_idx = 0;   // Virtual page number (for an absolute memory address)
396    size_t page_idx = 0;           // Page index relative to 0
397    size_t previous_page_idx = 0;  // Previous page index relative to 0
398    size_t dirty_pages = 0;
399    size_t private_pages = 0;
400    size_t private_dirty_pages = 0;
401
402    // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
403    for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) {
404      ptrdiff_t offset = begin - boot_map.start;
405
406      // We treat the image header as part of the memory map for now
407      // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
408      // But it might still be interesting to see if any of the ImageHeader data mutated
409      const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
410      uint8_t* remote_ptr = &remote_contents[offset];
411
412      if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
413        different_pages++;
414
415        // Count the number of 32-bit integers that are different.
416        for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
417          uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr);
418          const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
419
420          if (remote_ptr_int32[i] != local_ptr_int32[i]) {
421            different_int32s++;
422          }
423        }
424      }
425    }
426
427    // Iterate through one byte at a time.
428    for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) {
429      previous_page_idx = page_idx;
430      ptrdiff_t offset = begin - boot_map.start;
431
432      // We treat the image header as part of the memory map for now
433      // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
434      // But it might still be interesting to see if any of the ImageHeader data mutated
435      const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
436      uint8_t* remote_ptr = &remote_contents[offset];
437
438      virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
439
440      // Calculate the page index, relative to the 0th page where the image begins
441      page_idx = (offset + page_off_begin) / kPageSize;
442      if (*local_ptr != *remote_ptr) {
443        // Track number of bytes that are different
444        different_bytes++;
445      }
446
447      // Independently count the # of dirty pages on the remote side
448      size_t remote_virtual_page_idx = begin / kPageSize;
449      if (previous_page_idx != page_idx) {
450        uint64_t page_count = 0xC0FFEE;
451        // TODO: virtual_page_idx needs to be from the same process
452        int dirtiness = (IsPageDirty(page_map_file.get(),        // Image-diff-pid procmap
453                                     clean_page_map_file.get(),  // Self procmap
454                                     kpage_flags_file.get(),
455                                     kpage_count_file.get(),
456                                     remote_virtual_page_idx,    // potentially "dirty" page
457                                     virtual_page_idx,           // true "clean" page
458                                     &page_count,
459                                     &error_msg));
460        if (dirtiness < 0) {
461          os << error_msg;
462          return false;
463        } else if (dirtiness > 0) {
464          dirty_pages++;
465          dirty_page_set_remote.insert(dirty_page_set_remote.end(), remote_virtual_page_idx);
466          dirty_page_set_local.insert(dirty_page_set_local.end(), virtual_page_idx);
467        }
468
469        bool is_dirty = dirtiness > 0;
470        bool is_private = page_count == 1;
471
472        if (page_count == 1) {
473          private_pages++;
474        }
475
476        if (is_dirty && is_private) {
477          private_dirty_pages++;
478        }
479      }
480    }
481
482    std::map<mirror::Class*, ClassData> class_data;
483
484    // Walk each object in the remote image space and compare it against ours
485    size_t different_objects = 0;
486
487    std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count;
488    std::vector<ArtMethod*> art_method_dirty_objects;
489
490    std::map<off_t /* field offset */, int /* count */> class_field_dirty_count;
491    std::vector<mirror::Class*> class_dirty_objects;
492
493    // List of local objects that are clean, but located on dirty pages.
494    std::vector<mirror::Object*> false_dirty_objects;
495    size_t false_dirty_object_bytes = 0;
496
497    // Look up remote classes by their descriptor
498    std::map<std::string, mirror::Class*> remote_class_map;
499    // Look up local classes by their descriptor
500    std::map<std::string, mirror::Class*> local_class_map;
501
502    // Objects that are dirty against the image (possibly shared or private dirty).
503    std::set<mirror::Object*> image_dirty_objects;
504
505    // Objects that are dirty against the zygote (probably private dirty).
506    std::set<mirror::Object*> zygote_dirty_objects;
507
508    size_t dirty_object_bytes = 0;
509    const uint8_t* begin_image_ptr = image_begin_unaligned;
510    const uint8_t* end_image_ptr = image_mirror_end_unaligned;
511
512    const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment);
513    while (reinterpret_cast<uintptr_t>(current) < reinterpret_cast<uintptr_t>(end_image_ptr)) {
514      CHECK_ALIGNED(current, kObjectAlignment);
515      mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current));
516
517      // Sanity check that we are reading a real object
518      CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
519      if (kUseBakerOrBrooksReadBarrier) {
520        obj->AssertReadBarrierPointer();
521      }
522
523      // Iterate every page this object belongs to
524      bool on_dirty_page = false;
525      size_t page_off = 0;
526      size_t current_page_idx;
527      uintptr_t object_address;
528      do {
529        object_address = reinterpret_cast<uintptr_t>(current);
530        current_page_idx = object_address / kPageSize + page_off;
531
532        if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) {
533          // This object is on a dirty page
534          on_dirty_page = true;
535        }
536
537        page_off++;
538      } while ((current_page_idx * kPageSize) <
539               RoundUp(object_address + obj->SizeOf(), kObjectAlignment));
540
541      mirror::Class* klass = obj->GetClass();
542
543      // Check against the other object and see if they are different
544      ptrdiff_t offset = current - begin_image_ptr;
545      const uint8_t* current_remote = &remote_contents[offset];
546      mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(
547          const_cast<uint8_t*>(current_remote));
548
549      bool different_image_object = memcmp(current, current_remote, obj->SizeOf()) != 0;
550      if (different_image_object) {
551        bool different_zygote_object = false;
552        if (!zygote_contents.empty()) {
553          const uint8_t* zygote_ptr = &zygote_contents[offset];
554          different_zygote_object = memcmp(current, zygote_ptr, obj->SizeOf()) != 0;
555        }
556        if (different_zygote_object) {
557          // Different from zygote.
558          zygote_dirty_objects.insert(obj);
559        } else {
560          // Just different from iamge.
561          image_dirty_objects.insert(obj);
562        }
563
564        different_objects++;
565        dirty_object_bytes += obj->SizeOf();
566
567        ++class_data[klass].dirty_object_count;
568
569        // Go byte-by-byte and figure out what exactly got dirtied
570        size_t dirty_byte_count_per_object = 0;
571        for (size_t i = 0; i < obj->SizeOf(); ++i) {
572          if (current[i] != current_remote[i]) {
573            dirty_byte_count_per_object++;
574          }
575        }
576        class_data[klass].dirty_object_byte_count += dirty_byte_count_per_object;
577        class_data[klass].dirty_object_size_in_bytes += obj->SizeOf();
578        class_data[klass].dirty_objects.push_back(remote_obj);
579      } else {
580        ++class_data[klass].clean_object_count;
581      }
582
583      std::string descriptor = GetClassDescriptor(klass);
584      if (different_image_object) {
585        if (klass->IsClassClass()) {
586          // this is a "Class"
587          mirror::Class* obj_as_class  = reinterpret_cast<mirror::Class*>(remote_obj);
588
589          // print the fields that are dirty
590          for (size_t i = 0; i < obj->SizeOf(); ++i) {
591            if (current[i] != current_remote[i]) {
592              class_field_dirty_count[i]++;
593            }
594          }
595
596          class_dirty_objects.push_back(obj_as_class);
597        } else if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
598          // this is an ArtMethod
599          ArtMethod* art_method = reinterpret_cast<ArtMethod*>(remote_obj);
600
601          // print the fields that are dirty
602          for (size_t i = 0; i < obj->SizeOf(); ++i) {
603            if (current[i] != current_remote[i]) {
604              art_method_field_dirty_count[i]++;
605            }
606          }
607
608          art_method_dirty_objects.push_back(art_method);
609        }
610      } else if (on_dirty_page) {
611        // This object was either never mutated or got mutated back to the same value.
612        // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
613        false_dirty_objects.push_back(obj);
614        class_data[klass].false_dirty_objects.push_back(obj);
615        false_dirty_object_bytes += obj->SizeOf();
616        class_data[obj->GetClass()].false_dirty_byte_count += obj->SizeOf();
617        class_data[obj->GetClass()].false_dirty_object_count += 1;
618      }
619
620      if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
621        local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj);
622        remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj);
623      }
624
625      // Unconditionally store the class descriptor in case we need it later
626      class_data[klass].descriptor = descriptor;
627      current += RoundUp(obj->SizeOf(), kObjectAlignment);
628    }
629
630    // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects.
631    float true_dirtied_percent = dirty_object_bytes * 1.0f / (dirty_pages * kPageSize);
632    size_t false_dirty_pages = dirty_pages - different_pages;
633
634    os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", "
635       << reinterpret_cast<void*>(boot_map.end) << ") had: \n  "
636       << different_bytes << " differing bytes, \n  "
637       << different_int32s << " differing int32s, \n  "
638       << different_objects << " different objects, \n  "
639       << dirty_object_bytes << " different object [bytes], \n  "
640       << false_dirty_objects.size() << " false dirty objects,\n  "
641       << false_dirty_object_bytes << " false dirty object [bytes], \n  "
642       << true_dirtied_percent << " different objects-vs-total in a dirty page;\n  "
643       << different_pages << " different pages; \n  "
644       << dirty_pages << " pages are dirty; \n  "
645       << false_dirty_pages << " pages are false dirty; \n  "
646       << private_pages << " pages are private; \n  "
647       << private_dirty_pages << " pages are Private_Dirty\n  "
648       << "";
649
650    // vector of pairs (int count, Class*)
651    auto dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
652        class_data, [](const ClassData& d) { return d.dirty_object_count; });
653    auto clean_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
654        class_data, [](const ClassData& d) { return d.clean_object_count; });
655
656    if (!zygote_dirty_objects.empty()) {
657      os << "\n" << "  Dirty objects compared to zygote (probably private dirty): "
658         << zygote_dirty_objects.size() << "\n";
659      for (mirror::Object* obj : zygote_dirty_objects) {
660        const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj);
661        ptrdiff_t offset = obj_bytes - begin_image_ptr;
662        uint8_t* remote_bytes = &zygote_contents[offset];
663        DiffObjectContents(obj, remote_bytes, os);
664      }
665    }
666    os << "\n" << "  Dirty objects compared to image (private or shared dirty): "
667       << image_dirty_objects.size() << "\n";
668    for (mirror::Object* obj : image_dirty_objects) {
669      const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj);
670      ptrdiff_t offset = obj_bytes - begin_image_ptr;
671      uint8_t* remote_bytes = &remote_contents[offset];
672      DiffObjectContents(obj, remote_bytes, os);
673    }
674
675    os << "\n" << "  Dirty object count by class:\n";
676    for (const auto& vk_pair : dirty_object_class_values) {
677      int dirty_object_count = vk_pair.first;
678      mirror::Class* klass = vk_pair.second;
679      int object_sizes = class_data[klass].dirty_object_size_in_bytes;
680      float avg_dirty_bytes_per_class =
681          class_data[klass].dirty_object_byte_count * 1.0f / object_sizes;
682      float avg_object_size = object_sizes * 1.0f / dirty_object_count;
683      const std::string& descriptor = class_data[klass].descriptor;
684      os << "    " << PrettyClass(klass) << " ("
685         << "objects: " << dirty_object_count << ", "
686         << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
687         << "avg object size: " << avg_object_size << ", "
688         << "class descriptor: '" << descriptor << "'"
689         << ")\n";
690
691      constexpr size_t kMaxAddressPrint = 5;
692      if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
693        os << "      sample object addresses: ";
694        for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
695          auto art_method = art_method_dirty_objects[i];
696
697          os << reinterpret_cast<void*>(art_method) << ", ";
698        }
699        os << "\n";
700
701        os << "      dirty byte +offset:count list = ";
702        auto art_method_field_dirty_count_sorted =
703            SortByValueDesc<off_t, int, int>(art_method_field_dirty_count);
704        for (auto pair : art_method_field_dirty_count_sorted) {
705          off_t offset = pair.second;
706          int count = pair.first;
707
708          os << "+" << offset << ":" << count << ", ";
709        }
710
711        os << "\n";
712
713        os << "      field contents:\n";
714        const auto& dirty_objects_list = class_data[klass].dirty_objects;
715        for (mirror::Object* obj : dirty_objects_list) {
716          // remote method
717          auto art_method = reinterpret_cast<ArtMethod*>(obj);
718
719          // remote class
720          mirror::Class* remote_declaring_class =
721            FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents, boot_map);
722
723          // local class
724          mirror::Class* declaring_class =
725            RemoteContentsPointerToLocal(remote_declaring_class,
726                                         remote_contents,
727                                         boot_image_header);
728
729          os << "        " << reinterpret_cast<void*>(obj) << " ";
730          os << "  entryPointFromJni: "
731             << reinterpret_cast<const void*>(
732                    art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
733          os << "  entryPointFromQuickCompiledCode: "
734             << reinterpret_cast<const void*>(
735                    art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
736             << ", ";
737          os << "  isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
738          os << "  class_status (local): " << declaring_class->GetStatus();
739          os << "  class_status (remote): " << remote_declaring_class->GetStatus();
740          os << "\n";
741        }
742      }
743      if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
744        os << "       sample object addresses: ";
745        for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
746          auto class_ptr = class_dirty_objects[i];
747
748          os << reinterpret_cast<void*>(class_ptr) << ", ";
749        }
750        os << "\n";
751
752        os << "       dirty byte +offset:count list = ";
753        auto class_field_dirty_count_sorted =
754            SortByValueDesc<off_t, int, int>(class_field_dirty_count);
755        for (auto pair : class_field_dirty_count_sorted) {
756          off_t offset = pair.second;
757          int count = pair.first;
758
759          os << "+" << offset << ":" << count << ", ";
760        }
761        os << "\n";
762
763        os << "      field contents:\n";
764        const auto& dirty_objects_list = class_data[klass].dirty_objects;
765        for (mirror::Object* obj : dirty_objects_list) {
766          // remote class object
767          auto remote_klass = reinterpret_cast<mirror::Class*>(obj);
768
769          // local class object
770          auto local_klass = RemoteContentsPointerToLocal(remote_klass,
771                                                          remote_contents,
772                                                          boot_image_header);
773
774          os << "        " << reinterpret_cast<void*>(obj) << " ";
775          os << "  class_status (remote): " << remote_klass->GetStatus() << ", ";
776          os << "  class_status (local): " << local_klass->GetStatus();
777          os << "\n";
778        }
779      }
780    }
781
782    auto false_dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
783        class_data, [](const ClassData& d) { return d.false_dirty_object_count; });
784
785    os << "\n" << "  False-dirty object count by class:\n";
786    for (const auto& vk_pair : false_dirty_object_class_values) {
787      int object_count = vk_pair.first;
788      mirror::Class* klass = vk_pair.second;
789      int object_sizes = class_data[klass].false_dirty_byte_count;
790      float avg_object_size = object_sizes * 1.0f / object_count;
791      const std::string& descriptor = class_data[klass].descriptor;
792      os << "    " << PrettyClass(klass) << " ("
793         << "objects: " << object_count << ", "
794         << "avg object size: " << avg_object_size << ", "
795         << "total bytes: " << object_sizes << ", "
796         << "class descriptor: '" << descriptor << "'"
797         << ")\n";
798
799      if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
800        auto& art_method_false_dirty_objects = class_data[klass].false_dirty_objects;
801
802        os << "      field contents:\n";
803        for (mirror::Object* obj : art_method_false_dirty_objects) {
804          // local method
805          auto art_method = reinterpret_cast<ArtMethod*>(obj);
806
807          // local class
808          mirror::Class* declaring_class = art_method->GetDeclaringClass();
809
810          os << "        " << reinterpret_cast<void*>(obj) << " ";
811          os << "  entryPointFromJni: "
812             << reinterpret_cast<const void*>(
813                    art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
814          os << "  entryPointFromQuickCompiledCode: "
815             << reinterpret_cast<const void*>(
816                    art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
817             << ", ";
818          os << "  isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
819          os << "  class_status (local): " << declaring_class->GetStatus();
820          os << "\n";
821        }
822      }
823    }
824
825    os << "\n" << "  Clean object count by class:\n";
826    for (const auto& vk_pair : clean_object_class_values) {
827      os << "    " << PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
828    }
829
830    return true;
831  }
832
833  // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
834  // Returned pointer will point to inside of remote_contents.
835  template <typename T>
836  static T* FixUpRemotePointer(T* remote_ptr,
837                               std::vector<uint8_t>& remote_contents,
838                               const backtrace_map_t& boot_map) {
839    if (remote_ptr == nullptr) {
840      return nullptr;
841    }
842
843    uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr);
844
845    CHECK_LE(boot_map.start, remote);
846    CHECK_GT(boot_map.end, remote);
847
848    off_t boot_offset = remote - boot_map.start;
849
850    return reinterpret_cast<T*>(&remote_contents[boot_offset]);
851  }
852
853  template <typename T>
854  static T* RemoteContentsPointerToLocal(T* remote_ptr,
855                                         std::vector<uint8_t>& remote_contents,
856                                         const ImageHeader& image_header) {
857    if (remote_ptr == nullptr) {
858      return nullptr;
859    }
860
861    uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr);
862    ptrdiff_t boot_offset = remote - &remote_contents[0];
863
864    const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
865
866    return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
867  }
868
869  static std::string GetClassDescriptor(mirror::Class* klass)
870    SHARED_REQUIRES(Locks::mutator_lock_) {
871    CHECK(klass != nullptr);
872
873    std::string descriptor;
874    const char* descriptor_str = klass->GetDescriptor(&descriptor);
875
876    return std::string(descriptor_str);
877  }
878
879  template <typename K, typename V, typename D>
880  static std::vector<std::pair<V, K>> SortByValueDesc(
881      const std::map<K, D> map,
882      std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) {
883    // Store value->key so that we can use the default sort from pair which
884    // sorts by value first and then key
885    std::vector<std::pair<V, K>> value_key_vector;
886
887    for (const auto& kv_pair : map) {
888      value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first));
889    }
890
891    // Sort in reverse (descending order)
892    std::sort(value_key_vector.rbegin(), value_key_vector.rend());
893    return value_key_vector;
894  }
895
896  static bool GetPageFrameNumber(File* page_map_file,
897                                size_t virtual_page_index,
898                                uint64_t* page_frame_number,
899                                std::string* error_msg) {
900    CHECK(page_map_file != nullptr);
901    CHECK(page_frame_number != nullptr);
902    CHECK(error_msg != nullptr);
903
904    constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
905    constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1;  // bits 0-54 [in /proc/$pid/pagemap]
906    constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55);  // bit 55 [in /proc/$pid/pagemap]
907
908    uint64_t page_map_entry = 0;
909
910    // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number
911    if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize,
912                                  virtual_page_index * kPageMapEntrySize)) {
913      *error_msg = StringPrintf("Failed to read the virtual page index entry from %s",
914                                page_map_file->GetPath().c_str());
915      return false;
916    }
917
918    // TODO: seems useless, remove this.
919    bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0;
920    if ((false)) {
921      LOG(VERBOSE) << soft_dirty;  // Suppress unused warning
922      UNREACHABLE();
923    }
924
925    *page_frame_number = page_map_entry & kPageFrameNumberMask;
926
927    return true;
928  }
929
930  static int IsPageDirty(File* page_map_file,
931                         File* clean_page_map_file,
932                         File* kpage_flags_file,
933                         File* kpage_count_file,
934                         size_t virtual_page_idx,
935                         size_t clean_virtual_page_idx,
936                         // Out parameters:
937                         uint64_t* page_count, std::string* error_msg) {
938    CHECK(page_map_file != nullptr);
939    CHECK(clean_page_map_file != nullptr);
940    CHECK_NE(page_map_file, clean_page_map_file);
941    CHECK(kpage_flags_file != nullptr);
942    CHECK(kpage_count_file != nullptr);
943    CHECK(page_count != nullptr);
944    CHECK(error_msg != nullptr);
945
946    // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
947
948    constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
949    constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
950    constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4);  // in /proc/kpageflags
951    constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20);  // in /proc/kpageflags
952    constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11);  // in /proc/kpageflags
953
954    uint64_t page_frame_number = 0;
955    if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
956      return -1;
957    }
958
959    uint64_t page_frame_number_clean = 0;
960    if (!GetPageFrameNumber(clean_page_map_file, clean_virtual_page_idx, &page_frame_number_clean,
961                            error_msg)) {
962      return -1;
963    }
964
965    // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
966    uint64_t kpage_flags_entry = 0;
967    if (!kpage_flags_file->PreadFully(&kpage_flags_entry,
968                                     kPageFlagsEntrySize,
969                                     page_frame_number * kPageFlagsEntrySize)) {
970      *error_msg = StringPrintf("Failed to read the page flags from %s",
971                                kpage_flags_file->GetPath().c_str());
972      return -1;
973    }
974
975    // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
976    if (!kpage_count_file->PreadFully(page_count /*out*/,
977                                     kPageCountEntrySize,
978                                     page_frame_number * kPageCountEntrySize)) {
979      *error_msg = StringPrintf("Failed to read the page count from %s",
980                                kpage_count_file->GetPath().c_str());
981      return -1;
982    }
983
984    // There must be a page frame at the requested address.
985    CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
986    // The page frame must be memory mapped
987    CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
988
989    // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
990    bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
991
992    // page_frame_number_clean must come from the *same* process
993    // but a *different* mmap than page_frame_number
994    if (flags_dirty) {
995      CHECK_NE(page_frame_number, page_frame_number_clean);
996    }
997
998    return page_frame_number != page_frame_number_clean;
999  }
1000
1001 private:
1002  // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
1003  std::string GetImageLocationBaseName() const {
1004    return BaseName(std::string(image_location_));
1005  }
1006
1007  std::ostream* os_;
1008  const ImageHeader& image_header_;
1009  const std::string image_location_;
1010  pid_t image_diff_pid_;  // Dump image diff against boot.art if pid is non-negative
1011  pid_t zygote_diff_pid_;  // Dump image diff against zygote boot.art if pid is non-negative
1012
1013  DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
1014};
1015
1016static int DumpImage(Runtime* runtime,
1017                     std::ostream* os,
1018                     pid_t image_diff_pid,
1019                     pid_t zygote_diff_pid) {
1020  ScopedObjectAccess soa(Thread::Current());
1021  gc::Heap* heap = runtime->GetHeap();
1022  std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
1023  CHECK(!image_spaces.empty());
1024  for (gc::space::ImageSpace* image_space : image_spaces) {
1025    const ImageHeader& image_header = image_space->GetImageHeader();
1026    if (!image_header.IsValid()) {
1027      fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
1028      return EXIT_FAILURE;
1029    }
1030
1031    ImgDiagDumper img_diag_dumper(os,
1032                                  image_header,
1033                                  image_space->GetImageLocation(),
1034                                  image_diff_pid,
1035                                  zygote_diff_pid);
1036    if (!img_diag_dumper.Dump()) {
1037      return EXIT_FAILURE;
1038    }
1039  }
1040  return EXIT_SUCCESS;
1041}
1042
1043struct ImgDiagArgs : public CmdlineArgs {
1044 protected:
1045  using Base = CmdlineArgs;
1046
1047  virtual ParseStatus ParseCustom(const StringPiece& option,
1048                                  std::string* error_msg) OVERRIDE {
1049    {
1050      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
1051      if (base_parse != kParseUnknownArgument) {
1052        return base_parse;
1053      }
1054    }
1055
1056    if (option.starts_with("--image-diff-pid=")) {
1057      const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data();
1058
1059      if (!ParseInt(image_diff_pid, &image_diff_pid_)) {
1060        *error_msg = "Image diff pid out of range";
1061        return kParseError;
1062      }
1063    } else if (option.starts_with("--zygote-diff-pid=")) {
1064      const char* zygote_diff_pid = option.substr(strlen("--zygote-diff-pid=")).data();
1065
1066      if (!ParseInt(zygote_diff_pid, &zygote_diff_pid_)) {
1067        *error_msg = "Zygote diff pid out of range";
1068        return kParseError;
1069      }
1070    } else {
1071      return kParseUnknownArgument;
1072    }
1073
1074    return kParseOk;
1075  }
1076
1077  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
1078    // Perform the parent checks.
1079    ParseStatus parent_checks = Base::ParseChecks(error_msg);
1080    if (parent_checks != kParseOk) {
1081      return parent_checks;
1082    }
1083
1084    // Perform our own checks.
1085
1086    if (kill(image_diff_pid_,
1087             /*sig*/0) != 0) {  // No signal is sent, perform error-checking only.
1088      // Check if the pid exists before proceeding.
1089      if (errno == ESRCH) {
1090        *error_msg = "Process specified does not exist";
1091      } else {
1092        *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
1093      }
1094      return kParseError;
1095    } else if (instruction_set_ != kRuntimeISA) {
1096      // Don't allow different ISAs since the images are ISA-specific.
1097      // Right now the code assumes both the runtime ISA and the remote ISA are identical.
1098      *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
1099      return kParseError;
1100    }
1101
1102    return kParseOk;
1103  }
1104
1105  virtual std::string GetUsage() const {
1106    std::string usage;
1107
1108    usage +=
1109        "Usage: imgdiag [options] ...\n"
1110        "    Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
1111        "    Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
1112        "\n";
1113
1114    usage += Base::GetUsage();
1115
1116    usage +=  // Optional.
1117        "  --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
1118        "      Example: --image-diff-pid=$(pid zygote)\n"
1119        "  --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff "
1120        "against.\n"
1121        "      Example: --zygote-diff-pid=$(pid zygote)\n"
1122        "\n";
1123
1124    return usage;
1125  }
1126
1127 public:
1128  pid_t image_diff_pid_ = -1;
1129  pid_t zygote_diff_pid_ = -1;
1130};
1131
1132struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
1133  virtual bool ExecuteWithRuntime(Runtime* runtime) {
1134    CHECK(args_ != nullptr);
1135
1136    return DumpImage(runtime,
1137                     args_->os_,
1138                     args_->image_diff_pid_,
1139                     args_->zygote_diff_pid_) == EXIT_SUCCESS;
1140  }
1141};
1142
1143}  // namespace art
1144
1145int main(int argc, char** argv) {
1146  art::ImgDiagMain main;
1147  return main.Main(argc, argv);
1148}
1149