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 <map>
24 #include <optional>
25 #include <set>
26 #include <string>
27 #include <unordered_set>
28 #include <vector>
29 
30 #include <android-base/parseint.h>
31 #include "android-base/stringprintf.h"
32 
33 #include "art_field-inl.h"
34 #include "art_method-inl.h"
35 #include "base/array_ref.h"
36 #include "base/os.h"
37 #include "base/string_view_cpp20.h"
38 #include "base/unix_file/fd_file.h"
39 #include "class_linker.h"
40 #include "gc/heap.h"
41 #include "gc/space/image_space.h"
42 #include "image-inl.h"
43 #include "mirror/class-inl.h"
44 #include "mirror/object-inl.h"
45 #include "oat.h"
46 #include "oat_file.h"
47 #include "oat_file_manager.h"
48 #include "scoped_thread_state_change-inl.h"
49 
50 #include "backtrace/BacktraceMap.h"
51 #include "cmdline.h"
52 
53 #include <signal.h>
54 #include <sys/stat.h>
55 #include <sys/types.h>
56 
57 namespace art {
58 
59 using android::base::StringPrintf;
60 
61 namespace {
62 
63 constexpr size_t kMaxAddressPrint = 5;
64 
65 enum class ProcessType {
66   kZygote,
67   kRemote
68 };
69 
70 enum class RemoteProcesses {
71   kImageOnly,
72   kZygoteOnly,
73   kImageAndZygote
74 };
75 
76 struct MappingData {
77   // The count of pages that are considered dirty by the OS.
78   size_t dirty_pages = 0;
79   // The count of pages that differ by at least one byte.
80   size_t different_pages = 0;
81   // The count of differing bytes.
82   size_t different_bytes = 0;
83   // The count of differing four-byte units.
84   size_t different_int32s = 0;
85   // The count of pages that have mapping count == 1.
86   size_t private_pages = 0;
87   // The count of private pages that are also dirty.
88   size_t private_dirty_pages = 0;
89   // The count of pages that are marked dirty but do not differ.
90   size_t false_dirty_pages = 0;
91   // Set of the local virtual page indices that are dirty.
92   std::set<size_t> dirty_page_set;
93 };
94 
GetClassDescriptor(mirror::Class * klass)95 static std::string GetClassDescriptor(mirror::Class* klass)
96     REQUIRES_SHARED(Locks::mutator_lock_) {
97   CHECK(klass != nullptr);
98 
99   std::string descriptor;
100   const char* descriptor_str = klass->GetDescriptor(&descriptor /*out*/);
101 
102   return std::string(descriptor_str);
103 }
104 
PrettyFieldValue(ArtField * field,mirror::Object * object)105 static std::string PrettyFieldValue(ArtField* field, mirror::Object* object)
106     REQUIRES_SHARED(Locks::mutator_lock_) {
107   std::ostringstream oss;
108   switch (field->GetTypeAsPrimitiveType()) {
109     case Primitive::kPrimNot: {
110       oss << object->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
111           field->GetOffset());
112       break;
113     }
114     case Primitive::kPrimBoolean: {
115       oss << static_cast<bool>(object->GetFieldBoolean<kVerifyNone>(field->GetOffset()));
116       break;
117     }
118     case Primitive::kPrimByte: {
119       oss << static_cast<int32_t>(object->GetFieldByte<kVerifyNone>(field->GetOffset()));
120       break;
121     }
122     case Primitive::kPrimChar: {
123       oss << object->GetFieldChar<kVerifyNone>(field->GetOffset());
124       break;
125     }
126     case Primitive::kPrimShort: {
127       oss << object->GetFieldShort<kVerifyNone>(field->GetOffset());
128       break;
129     }
130     case Primitive::kPrimInt: {
131       oss << object->GetField32<kVerifyNone>(field->GetOffset());
132       break;
133     }
134     case Primitive::kPrimLong: {
135       oss << object->GetField64<kVerifyNone>(field->GetOffset());
136       break;
137     }
138     case Primitive::kPrimFloat: {
139       oss << object->GetField32<kVerifyNone>(field->GetOffset());
140       break;
141     }
142     case Primitive::kPrimDouble: {
143       oss << object->GetField64<kVerifyNone>(field->GetOffset());
144       break;
145     }
146     case Primitive::kPrimVoid: {
147       oss << "void";
148       break;
149     }
150   }
151   return oss.str();
152 }
153 
154 template <typename K, typename V, typename D>
SortByValueDesc(const std::map<K,D> map,std::function<V (const D &)> value_mapper=[](const D & d){})155 static std::vector<std::pair<V, K>> SortByValueDesc(
156     const std::map<K, D> map,
157     std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) {
158   // Store value->key so that we can use the default sort from pair which
159   // sorts by value first and then key
160   std::vector<std::pair<V, K>> value_key_vector;
161 
162   for (const auto& kv_pair : map) {
163     value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first));
164   }
165 
166   // Sort in reverse (descending order)
167   std::sort(value_key_vector.rbegin(), value_key_vector.rend());
168   return value_key_vector;
169 }
170 
171 // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
172 // Returned pointer will point to inside of remote_contents.
173 template <typename T>
FixUpRemotePointer(ObjPtr<T> remote_ptr,ArrayRef<uint8_t> remote_contents,const backtrace_map_t & boot_map)174 static ObjPtr<T> FixUpRemotePointer(ObjPtr<T> remote_ptr,
175                                     ArrayRef<uint8_t> remote_contents,
176                                     const backtrace_map_t& boot_map)
177     REQUIRES_SHARED(Locks::mutator_lock_) {
178   if (remote_ptr == nullptr) {
179     return nullptr;
180   }
181 
182   uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr.Ptr());
183 
184   // In the case the remote pointer is out of range, it probably belongs to another image.
185   // Just return null for this case.
186   if (remote < boot_map.start || remote >= boot_map.end) {
187     return nullptr;
188   }
189 
190   off_t boot_offset = remote - boot_map.start;
191 
192   return reinterpret_cast<T*>(&remote_contents[boot_offset]);
193 }
194 
195 template <typename T>
RemoteContentsPointerToLocal(ObjPtr<T> remote_ptr,ArrayRef<uint8_t> remote_contents,const ImageHeader & image_header)196 static ObjPtr<T> RemoteContentsPointerToLocal(ObjPtr<T> remote_ptr,
197                                               ArrayRef<uint8_t> remote_contents,
198                                               const ImageHeader& image_header)
199     REQUIRES_SHARED(Locks::mutator_lock_) {
200   if (remote_ptr == nullptr) {
201     return nullptr;
202   }
203 
204   uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr.Ptr());
205   ptrdiff_t boot_offset = remote - &remote_contents[0];
206 
207   const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
208 
209   return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
210 }
211 
212 template <typename T> size_t EntrySize(T* entry);
EntrySize(mirror::Object * object)213 template<> size_t EntrySize(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
214   return object->SizeOf();
215 }
EntrySize(ArtMethod * art_method)216 template<> size_t EntrySize(ArtMethod* art_method) REQUIRES_SHARED(Locks::mutator_lock_) {
217   return sizeof(*art_method);
218 }
219 
220 // entry1 and entry2 might be relocated, this means we must use the runtime image's entry
221 // (image_entry) to avoid crashes.
222 template <typename T>
EntriesDiffer(T * image_entry,T * entry1,T * entry2)223 static bool EntriesDiffer(T* image_entry,
224                           T* entry1,
225                           T* entry2) REQUIRES_SHARED(Locks::mutator_lock_) {
226   // Use the image entry since entry1 and entry2 might both be remote and relocated.
227   return memcmp(entry1, entry2, EntrySize(image_entry)) != 0;
228 }
229 
230 template <typename T>
231 struct RegionCommon {
232  public:
RegionCommonart::__anon79c55f3c0111::RegionCommon233   RegionCommon(std::ostream* os,
234                ArrayRef<uint8_t> remote_contents,
235                ArrayRef<uint8_t> zygote_contents,
236                const backtrace_map_t& boot_map,
237                const ImageHeader& image_header) :
238     os_(*os),
239     remote_contents_(remote_contents),
240     zygote_contents_(zygote_contents),
241     boot_map_(boot_map),
242     image_header_(image_header),
243     different_entries_(0),
244     dirty_entry_bytes_(0),
245     false_dirty_entry_bytes_(0) {
246     CHECK(!remote_contents.empty());
247   }
248 
DumpSamplesAndOffsetCountart::__anon79c55f3c0111::RegionCommon249   void DumpSamplesAndOffsetCount() {
250     os_ << "      sample object addresses: ";
251     for (size_t i = 0; i < dirty_entries_.size() && i < kMaxAddressPrint; ++i) {
252       T* entry = dirty_entries_[i];
253       os_ << reinterpret_cast<void*>(entry) << ", ";
254     }
255     os_ << "\n";
256     os_ << "      dirty byte +offset:count list = ";
257     std::vector<std::pair<size_t, off_t>> field_dirty_count_sorted =
258         SortByValueDesc<off_t, size_t, size_t>(field_dirty_count_);
259     for (const std::pair<size_t, off_t>& pair : field_dirty_count_sorted) {
260       off_t offset = pair.second;
261       size_t count = pair.first;
262       os_ << "+" << offset << ":" << count << ", ";
263     }
264     os_ << "\n";
265   }
266 
GetDifferentEntryCountart::__anon79c55f3c0111::RegionCommon267   size_t GetDifferentEntryCount() const { return different_entries_; }
GetDirtyEntryBytesart::__anon79c55f3c0111::RegionCommon268   size_t GetDirtyEntryBytes() const { return dirty_entry_bytes_; }
GetFalseDirtyEntryCountart::__anon79c55f3c0111::RegionCommon269   size_t GetFalseDirtyEntryCount() const { return false_dirty_entries_.size(); }
GetFalseDirtyEntryBytesart::__anon79c55f3c0111::RegionCommon270   size_t GetFalseDirtyEntryBytes() const { return false_dirty_entry_bytes_; }
GetZygoteDirtyEntryCountart::__anon79c55f3c0111::RegionCommon271   size_t GetZygoteDirtyEntryCount() const { return zygote_dirty_entries_.size(); }
272 
273  protected:
IsEntryOnDirtyPageart::__anon79c55f3c0111::RegionCommon274   bool IsEntryOnDirtyPage(T* entry, const std::set<size_t>& dirty_pages) const
275       REQUIRES_SHARED(Locks::mutator_lock_) {
276     size_t size = EntrySize(entry);
277     size_t page_off = 0;
278     size_t current_page_idx;
279     uintptr_t entry_address = reinterpret_cast<uintptr_t>(entry);
280     // Iterate every page this entry belongs to
281     do {
282       current_page_idx = entry_address / kPageSize + page_off;
283       if (dirty_pages.find(current_page_idx) != dirty_pages.end()) {
284         // This entry is on a dirty page
285         return true;
286       }
287       page_off++;
288     } while ((current_page_idx * kPageSize) < RoundUp(entry_address + size, kObjectAlignment));
289     return false;
290   }
291 
AddZygoteDirtyEntryart::__anon79c55f3c0111::RegionCommon292   void AddZygoteDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
293     zygote_dirty_entries_.insert(entry);
294   }
295 
AddImageDirtyEntryart::__anon79c55f3c0111::RegionCommon296   void AddImageDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
297     image_dirty_entries_.insert(entry);
298   }
299 
AddFalseDirtyEntryart::__anon79c55f3c0111::RegionCommon300   void AddFalseDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
301     false_dirty_entries_.push_back(entry);
302     false_dirty_entry_bytes_ += EntrySize(entry);
303   }
304 
305   // The output stream to write to.
306   std::ostream& os_;
307   // The byte contents of the remote (image) process' image.
308   ArrayRef<uint8_t> remote_contents_;
309   // The byte contents of the zygote process' image.
310   ArrayRef<uint8_t> zygote_contents_;
311   const backtrace_map_t& boot_map_;
312   const ImageHeader& image_header_;
313 
314   // Count of entries that are different.
315   size_t different_entries_;
316 
317   // Local entries that are dirty (differ in at least one byte).
318   size_t dirty_entry_bytes_;
319   std::vector<T*> dirty_entries_;
320 
321   // Local entries that are clean, but located on dirty pages.
322   size_t false_dirty_entry_bytes_;
323   std::vector<T*> false_dirty_entries_;
324 
325   // Image dirty entries
326   // If zygote_pid_only_ == true, these are shared dirty entries in the zygote.
327   // If zygote_pid_only_ == false, these are private dirty entries in the application.
328   std::set<T*> image_dirty_entries_;
329 
330   // Zygote dirty entries (probably private dirty).
331   // We only add entries here if they differed in both the image and the zygote, so
332   // they are probably private dirty.
333   std::set<T*> zygote_dirty_entries_;
334 
335   std::map<off_t /* field offset */, size_t /* count */> field_dirty_count_;
336 
337  private:
338   DISALLOW_COPY_AND_ASSIGN(RegionCommon);
339 };
340 
341 template <typename T>
342 class RegionSpecializedBase : public RegionCommon<T> {
343 };
344 
345 // Region analysis for mirror::Objects
346 class ImgObjectVisitor : public ObjectVisitor {
347  public:
348   using ComputeDirtyFunc = std::function<void(mirror::Object* object,
349                                               const uint8_t* begin_image_ptr,
350                                               const std::set<size_t>& dirty_pages)>;
ImgObjectVisitor(ComputeDirtyFunc dirty_func,const uint8_t * begin_image_ptr,const std::set<size_t> & dirty_pages)351   ImgObjectVisitor(ComputeDirtyFunc dirty_func,
352                    const uint8_t* begin_image_ptr,
353                    const std::set<size_t>& dirty_pages) :
354     dirty_func_(std::move(dirty_func)),
355     begin_image_ptr_(begin_image_ptr),
356     dirty_pages_(dirty_pages) { }
357 
~ImgObjectVisitor()358   ~ImgObjectVisitor() override { }
359 
Visit(mirror::Object * object)360   void Visit(mirror::Object* object) override REQUIRES_SHARED(Locks::mutator_lock_) {
361     // Sanity check that we are reading a real mirror::Object
362     CHECK(object->GetClass() != nullptr) << "Image object at address "
363                                          << object
364                                          << " has null class";
365     if (kUseBakerReadBarrier) {
366       object->AssertReadBarrierState();
367     }
368     dirty_func_(object, begin_image_ptr_, dirty_pages_);
369   }
370 
371  private:
372   const ComputeDirtyFunc dirty_func_;
373   const uint8_t* begin_image_ptr_;
374   const std::set<size_t>& dirty_pages_;
375 };
376 
377 template<>
378 class RegionSpecializedBase<mirror::Object> : public RegionCommon<mirror::Object> {
379  public:
RegionSpecializedBase(std::ostream * os,ArrayRef<uint8_t> remote_contents,ArrayRef<uint8_t> zygote_contents,const backtrace_map_t & boot_map,const ImageHeader & image_header,bool dump_dirty_objects)380   RegionSpecializedBase(std::ostream* os,
381                         ArrayRef<uint8_t> remote_contents,
382                         ArrayRef<uint8_t> zygote_contents,
383                         const backtrace_map_t& boot_map,
384                         const ImageHeader& image_header,
385                         bool dump_dirty_objects)
386       : RegionCommon<mirror::Object>(os, remote_contents, zygote_contents, boot_map, image_header),
387         os_(*os),
388         dump_dirty_objects_(dump_dirty_objects) { }
389 
390   // Define a common public type name for use by RegionData.
391   using VisitorClass = ImgObjectVisitor;
392 
VisitEntries(VisitorClass * visitor,uint8_t * base,PointerSize pointer_size)393   void VisitEntries(VisitorClass* visitor,
394                     uint8_t* base,
395                     PointerSize pointer_size)
396       REQUIRES_SHARED(Locks::mutator_lock_) {
397     RegionCommon<mirror::Object>::image_header_.VisitObjects(visitor, base, pointer_size);
398   }
399 
VisitEntry(mirror::Object * entry)400   void VisitEntry(mirror::Object* entry)
401       REQUIRES_SHARED(Locks::mutator_lock_) {
402     // Unconditionally store the class descriptor in case we need it later
403     mirror::Class* klass = entry->GetClass();
404     class_data_[klass].descriptor = GetClassDescriptor(klass);
405   }
406 
AddCleanEntry(mirror::Object * entry)407   void AddCleanEntry(mirror::Object* entry)
408       REQUIRES_SHARED(Locks::mutator_lock_) {
409     class_data_[entry->GetClass()].AddCleanObject();
410   }
411 
AddFalseDirtyEntry(mirror::Object * entry)412   void AddFalseDirtyEntry(mirror::Object* entry)
413       REQUIRES_SHARED(Locks::mutator_lock_) {
414     RegionCommon<mirror::Object>::AddFalseDirtyEntry(entry);
415     class_data_[entry->GetClass()].AddFalseDirtyObject(entry);
416   }
417 
AddDirtyEntry(mirror::Object * entry,mirror::Object * entry_remote)418   void AddDirtyEntry(mirror::Object* entry, mirror::Object* entry_remote)
419       REQUIRES_SHARED(Locks::mutator_lock_) {
420     size_t entry_size = EntrySize(entry);
421     ++different_entries_;
422     dirty_entry_bytes_ += entry_size;
423     // Log dirty count and objects for class objects only.
424     mirror::Class* klass = entry->GetClass();
425     if (klass->IsClassClass()) {
426       // Increment counts for the fields that are dirty
427       const uint8_t* current = reinterpret_cast<const uint8_t*>(entry);
428       const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(entry_remote);
429       for (size_t i = 0; i < entry_size; ++i) {
430         if (current[i] != current_remote[i]) {
431           field_dirty_count_[i]++;
432         }
433       }
434       dirty_entries_.push_back(entry);
435     }
436     class_data_[klass].AddDirtyObject(entry, entry_remote);
437   }
438 
DiffEntryContents(mirror::Object * entry,uint8_t * remote_bytes,const uint8_t * base_ptr,bool log_dirty_objects)439   void DiffEntryContents(mirror::Object* entry,
440                          uint8_t* remote_bytes,
441                          const uint8_t* base_ptr,
442                          bool log_dirty_objects)
443       REQUIRES_SHARED(Locks::mutator_lock_) {
444     const char* tabs = "    ";
445     // Attempt to find fields for all dirty bytes.
446     mirror::Class* klass = entry->GetClass();
447     if (entry->IsClass()) {
448       os_ << tabs
449           << "Class " << mirror::Class::PrettyClass(entry->AsClass()) << " " << entry << "\n";
450     } else {
451       os_ << tabs
452           << "Instance of " << mirror::Class::PrettyClass(klass) << " " << entry << "\n";
453     }
454 
455     std::unordered_set<ArtField*> dirty_instance_fields;
456     std::unordered_set<ArtField*> dirty_static_fields;
457     // Examine the bytes comprising the Object, computing which fields are dirty
458     // and recording them for later display.  If the Object is an array object,
459     // compute the dirty entries.
460     mirror::Object* remote_entry = reinterpret_cast<mirror::Object*>(remote_bytes);
461     for (size_t i = 0, count = entry->SizeOf(); i < count; ++i) {
462       if (base_ptr[i] != remote_bytes[i]) {
463         ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i);
464         if (field != nullptr) {
465           dirty_instance_fields.insert(field);
466         } else if (entry->IsClass()) {
467           field = ArtField::FindStaticFieldWithOffset</*exact*/false>(entry->AsClass(), i);
468           if (field != nullptr) {
469             dirty_static_fields.insert(field);
470           }
471         }
472         if (field == nullptr) {
473           if (klass->IsArrayClass()) {
474             ObjPtr<mirror::Class> component_type = klass->GetComponentType();
475             Primitive::Type primitive_type = component_type->GetPrimitiveType();
476             size_t component_size = Primitive::ComponentSize(primitive_type);
477             size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
478             DCHECK_ALIGNED_PARAM(data_offset, component_size);
479             if (i >= data_offset) {
480               os_ << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n";
481               // Skip the remaining bytes of this element to prevent spam.
482               DCHECK(IsPowerOfTwo(component_size));
483               i |= component_size - 1;
484               continue;
485             }
486           }
487           os_ << tabs << "No field for byte offset " << i << "\n";
488         }
489       }
490     }
491     // Dump different fields.
492     if (!dirty_instance_fields.empty()) {
493       os_ << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n";
494       for (ArtField* field : dirty_instance_fields) {
495         os_ << tabs << ArtField::PrettyField(field)
496             << " original=" << PrettyFieldValue(field, entry)
497             << " remote=" << PrettyFieldValue(field, remote_entry) << "\n";
498       }
499     }
500     if (!dirty_static_fields.empty()) {
501       if (dump_dirty_objects_ && log_dirty_objects) {
502         dirty_objects_.insert(entry);
503       }
504       os_ << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n";
505       for (ArtField* field : dirty_static_fields) {
506         os_ << tabs << ArtField::PrettyField(field)
507             << " original=" << PrettyFieldValue(field, entry)
508             << " remote=" << PrettyFieldValue(field, remote_entry) << "\n";
509       }
510     }
511     os_ << "\n";
512   }
513 
DumpDirtyObjects()514   void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) {
515     for (mirror::Object* obj : dirty_objects_) {
516       if (obj->IsClass()) {
517         os_ << "Private dirty object: " << obj->AsClass()->PrettyDescriptor() << "\n";
518       }
519     }
520   }
521 
DumpDirtyEntries()522   void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
523     // vector of pairs (size_t count, Class*)
524     auto dirty_object_class_values =
525         SortByValueDesc<mirror::Class*, size_t, ClassData>(
526             class_data_,
527             [](const ClassData& d) { return d.dirty_object_count; });
528     os_ << "\n" << "  Dirty object count by class:\n";
529     for (const auto& vk_pair : dirty_object_class_values) {
530       size_t dirty_object_count = vk_pair.first;
531       mirror::Class* klass = vk_pair.second;
532       ClassData& class_data = class_data_[klass];
533       size_t object_sizes = class_data.dirty_object_size_in_bytes;
534       float avg_dirty_bytes_per_class =
535           class_data.dirty_object_byte_count * 1.0f / object_sizes;
536       float avg_object_size = object_sizes * 1.0f / dirty_object_count;
537       const std::string& descriptor = class_data.descriptor;
538       os_ << "    " << mirror::Class::PrettyClass(klass) << " ("
539           << "objects: " << dirty_object_count << ", "
540           << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
541           << "avg object size: " << avg_object_size << ", "
542           << "class descriptor: '" << descriptor << "'"
543           << ")\n";
544       if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
545         DumpSamplesAndOffsetCount();
546         os_ << "      field contents:\n";
547         for (mirror::Object* object : class_data.dirty_objects) {
548           // remote class object
549           ObjPtr<mirror::Class> remote_klass =
550               ObjPtr<mirror::Class>::DownCast<mirror::Object>(object);
551           // local class object
552           ObjPtr<mirror::Class> local_klass =
553               RemoteContentsPointerToLocal(remote_klass,
554                                            RegionCommon<mirror::Object>::remote_contents_,
555                                            RegionCommon<mirror::Object>::image_header_);
556           os_ << "        " << reinterpret_cast<const void*>(object) << " ";
557           os_ << "  class_status (remote): " << remote_klass->GetStatus() << ", ";
558           os_ << "  class_status (local): " << local_klass->GetStatus();
559           os_ << "\n";
560         }
561       }
562     }
563   }
564 
DumpFalseDirtyEntries()565   void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
566     // vector of pairs (size_t count, Class*)
567     auto false_dirty_object_class_values =
568         SortByValueDesc<mirror::Class*, size_t, ClassData>(
569             class_data_,
570             [](const ClassData& d) { return d.false_dirty_object_count; });
571     os_ << "\n" << "  False-dirty object count by class:\n";
572     for (const auto& vk_pair : false_dirty_object_class_values) {
573       size_t object_count = vk_pair.first;
574       mirror::Class* klass = vk_pair.second;
575       ClassData& class_data = class_data_[klass];
576       size_t object_sizes = class_data.false_dirty_byte_count;
577       float avg_object_size = object_sizes * 1.0f / object_count;
578       const std::string& descriptor = class_data.descriptor;
579       os_ << "    " << mirror::Class::PrettyClass(klass) << " ("
580           << "objects: " << object_count << ", "
581           << "avg object size: " << avg_object_size << ", "
582           << "total bytes: " << object_sizes << ", "
583           << "class descriptor: '" << descriptor << "'"
584           << ")\n";
585     }
586   }
587 
DumpCleanEntries()588   void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
589     // vector of pairs (size_t count, Class*)
590     auto clean_object_class_values =
591         SortByValueDesc<mirror::Class*, size_t, ClassData>(
592             class_data_,
593             [](const ClassData& d) { return d.clean_object_count; });
594     os_ << "\n" << "  Clean object count by class:\n";
595     for (const auto& vk_pair : clean_object_class_values) {
596       os_ << "    " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
597     }
598   }
599 
600  private:
601   // Aggregate and detail class data from an image diff.
602   struct ClassData {
603     size_t dirty_object_count = 0;
604     // Track only the byte-per-byte dirtiness (in bytes)
605     size_t dirty_object_byte_count = 0;
606     // Track the object-by-object dirtiness (in bytes)
607     size_t dirty_object_size_in_bytes = 0;
608     size_t clean_object_count = 0;
609     std::string descriptor;
610     size_t false_dirty_byte_count = 0;
611     size_t false_dirty_object_count = 0;
612     std::vector<mirror::Object*> false_dirty_objects;
613     // Remote pointers to dirty objects
614     std::vector<mirror::Object*> dirty_objects;
615 
AddCleanObjectart::__anon79c55f3c0111::RegionSpecializedBase::ClassData616     void AddCleanObject() REQUIRES_SHARED(Locks::mutator_lock_) {
617       ++clean_object_count;
618     }
619 
AddDirtyObjectart::__anon79c55f3c0111::RegionSpecializedBase::ClassData620     void AddDirtyObject(mirror::Object* object, mirror::Object* object_remote)
621         REQUIRES_SHARED(Locks::mutator_lock_) {
622       ++dirty_object_count;
623       dirty_object_byte_count += CountDirtyBytes(object, object_remote);
624       dirty_object_size_in_bytes += EntrySize(object);
625       dirty_objects.push_back(object_remote);
626     }
627 
AddFalseDirtyObjectart::__anon79c55f3c0111::RegionSpecializedBase::ClassData628     void AddFalseDirtyObject(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
629       ++false_dirty_object_count;
630       false_dirty_objects.push_back(object);
631       false_dirty_byte_count += EntrySize(object);
632     }
633 
634    private:
635     // Go byte-by-byte and figure out what exactly got dirtied
CountDirtyBytesart::__anon79c55f3c0111::RegionSpecializedBase::ClassData636     static size_t CountDirtyBytes(mirror::Object* object1, mirror::Object* object2)
637         REQUIRES_SHARED(Locks::mutator_lock_) {
638       const uint8_t* cur1 = reinterpret_cast<const uint8_t*>(object1);
639       const uint8_t* cur2 = reinterpret_cast<const uint8_t*>(object2);
640       size_t dirty_bytes = 0;
641       size_t object_size = EntrySize(object1);
642       for (size_t i = 0; i < object_size; ++i) {
643         if (cur1[i] != cur2[i]) {
644           dirty_bytes++;
645         }
646       }
647       return dirty_bytes;
648     }
649   };
650 
651   std::ostream& os_;
652   bool dump_dirty_objects_;
653   std::unordered_set<mirror::Object*> dirty_objects_;
654   std::map<mirror::Class*, ClassData> class_data_;
655 
656   DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase);
657 };
658 
659 // Region analysis for ArtMethods.
660 class ImgArtMethodVisitor {
661  public:
662   using ComputeDirtyFunc = std::function<void(ArtMethod*,
663                                               const uint8_t*,
664                                               const std::set<size_t>&)>;
ImgArtMethodVisitor(ComputeDirtyFunc dirty_func,const uint8_t * begin_image_ptr,const std::set<size_t> & dirty_pages)665   ImgArtMethodVisitor(ComputeDirtyFunc dirty_func,
666                       const uint8_t* begin_image_ptr,
667                       const std::set<size_t>& dirty_pages) :
668     dirty_func_(std::move(dirty_func)),
669     begin_image_ptr_(begin_image_ptr),
670     dirty_pages_(dirty_pages) { }
operator ()(ArtMethod & method) const671   void operator()(ArtMethod& method) const {
672     dirty_func_(&method, begin_image_ptr_, dirty_pages_);
673   }
674 
675  private:
676   const ComputeDirtyFunc dirty_func_;
677   const uint8_t* begin_image_ptr_;
678   const std::set<size_t>& dirty_pages_;
679 };
680 
681 // Struct and functor for computing offsets of members of ArtMethods.
682 // template <typename RegionType>
683 struct MemberInfo {
684   template <typename T>
operator ()art::__anon79c55f3c0111::MemberInfo685   void operator() (const ArtMethod* method, const T* member_address, const std::string& name) {
686     // Check that member_address is a pointer inside *method.
687     DCHECK(reinterpret_cast<uintptr_t>(method) <= reinterpret_cast<uintptr_t>(member_address));
688     DCHECK(reinterpret_cast<uintptr_t>(member_address) + sizeof(T) <=
689            reinterpret_cast<uintptr_t>(method) + sizeof(ArtMethod));
690     size_t offset =
691         reinterpret_cast<uintptr_t>(member_address) - reinterpret_cast<uintptr_t>(method);
692     offset_to_name_size_.insert({offset, NameAndSize(sizeof(T), name)});
693   }
694 
695   struct NameAndSize {
696     size_t size_;
697     std::string name_;
NameAndSizeart::__anon79c55f3c0111::MemberInfo::NameAndSize698     NameAndSize(size_t size, const std::string& name) : size_(size), name_(name) { }
NameAndSizeart::__anon79c55f3c0111::MemberInfo::NameAndSize699     NameAndSize() : size_(0), name_("INVALID") { }
700   };
701 
702   std::map<size_t, NameAndSize> offset_to_name_size_;
703 };
704 
705 template<>
706 class RegionSpecializedBase<ArtMethod> : public RegionCommon<ArtMethod> {
707  public:
RegionSpecializedBase(std::ostream * os,ArrayRef<uint8_t> remote_contents,ArrayRef<uint8_t> zygote_contents,const backtrace_map_t & boot_map,const ImageHeader & image_header,bool dump_dirty_objects ATTRIBUTE_UNUSED)708   RegionSpecializedBase(std::ostream* os,
709                         ArrayRef<uint8_t> remote_contents,
710                         ArrayRef<uint8_t> zygote_contents,
711                         const backtrace_map_t& boot_map,
712                         const ImageHeader& image_header,
713                         bool dump_dirty_objects ATTRIBUTE_UNUSED)
714       : RegionCommon<ArtMethod>(os, remote_contents, zygote_contents, boot_map, image_header),
715         os_(*os) {
716     // Prepare the table for offset to member lookups.
717     ArtMethod* art_method = reinterpret_cast<ArtMethod*>(&remote_contents[0]);
718     art_method->VisitMembers(member_info_);
719     // Prepare the table for address to symbolic entry point names.
720     BuildEntryPointNames();
721     class_linker_ = Runtime::Current()->GetClassLinker();
722   }
723 
724   // Define a common public type name for use by RegionData.
725   using VisitorClass = ImgArtMethodVisitor;
726 
VisitEntries(VisitorClass * visitor,uint8_t * base,PointerSize pointer_size)727   void VisitEntries(VisitorClass* visitor,
728                     uint8_t* base,
729                     PointerSize pointer_size)
730       REQUIRES_SHARED(Locks::mutator_lock_) {
731     RegionCommon<ArtMethod>::image_header_.VisitPackedArtMethods(*visitor, base, pointer_size);
732   }
733 
VisitEntry(ArtMethod * method ATTRIBUTE_UNUSED)734   void VisitEntry(ArtMethod* method ATTRIBUTE_UNUSED)
735       REQUIRES_SHARED(Locks::mutator_lock_) {
736   }
737 
AddCleanEntry(ArtMethod * method ATTRIBUTE_UNUSED)738   void AddCleanEntry(ArtMethod* method ATTRIBUTE_UNUSED) {
739   }
740 
AddFalseDirtyEntry(ArtMethod * method)741   void AddFalseDirtyEntry(ArtMethod* method)
742       REQUIRES_SHARED(Locks::mutator_lock_) {
743     RegionCommon<ArtMethod>::AddFalseDirtyEntry(method);
744   }
745 
AddDirtyEntry(ArtMethod * method,ArtMethod * method_remote)746   void AddDirtyEntry(ArtMethod* method, ArtMethod* method_remote)
747       REQUIRES_SHARED(Locks::mutator_lock_) {
748     size_t entry_size = EntrySize(method);
749     ++different_entries_;
750     dirty_entry_bytes_ += entry_size;
751     // Increment counts for the fields that are dirty
752     const uint8_t* current = reinterpret_cast<const uint8_t*>(method);
753     const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(method_remote);
754     // ArtMethods always log their dirty count and entries.
755     for (size_t i = 0; i < entry_size; ++i) {
756       if (current[i] != current_remote[i]) {
757         field_dirty_count_[i]++;
758       }
759     }
760     dirty_entries_.push_back(method);
761   }
762 
DiffEntryContents(ArtMethod * method,uint8_t * remote_bytes,const uint8_t * base_ptr,bool log_dirty_objects ATTRIBUTE_UNUSED)763   void DiffEntryContents(ArtMethod* method,
764                          uint8_t* remote_bytes,
765                          const uint8_t* base_ptr,
766                          bool log_dirty_objects ATTRIBUTE_UNUSED)
767       REQUIRES_SHARED(Locks::mutator_lock_) {
768     const char* tabs = "    ";
769     os_ << tabs << "ArtMethod " << ArtMethod::PrettyMethod(method) << "\n";
770 
771     std::unordered_set<size_t> dirty_members;
772     // Examine the members comprising the ArtMethod, computing which members are dirty.
773     for (const std::pair<const size_t,
774                          MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) {
775       const size_t offset = p.first;
776       if (memcmp(base_ptr + offset, remote_bytes + offset, p.second.size_) != 0) {
777         dirty_members.insert(p.first);
778       }
779     }
780     // Dump different fields.
781     if (!dirty_members.empty()) {
782       os_ << tabs << "Dirty members " << dirty_members.size() << "\n";
783       for (size_t offset : dirty_members) {
784         const MemberInfo::NameAndSize& member_info = member_info_.offset_to_name_size_[offset];
785         os_ << tabs << member_info.name_
786             << " original=" << StringFromBytes(base_ptr + offset, member_info.size_)
787             << " remote=" << StringFromBytes(remote_bytes + offset, member_info.size_)
788             << "\n";
789       }
790     }
791     os_ << "\n";
792   }
793 
DumpDirtyObjects()794   void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) {
795   }
796 
DumpDirtyEntries()797   void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
798     DumpSamplesAndOffsetCount();
799     os_ << "      offset to field map:\n";
800     for (const std::pair<const size_t,
801                          MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) {
802       const size_t offset = p.first;
803       const size_t size = p.second.size_;
804       os_ << StringPrintf("        %zu-%zu: ", offset, offset + size - 1)
805           << p.second.name_
806           << std::endl;
807     }
808 
809     os_ << "      field contents:\n";
810     for (ArtMethod* method : dirty_entries_) {
811       // remote method
812       auto art_method = reinterpret_cast<ArtMethod*>(method);
813       // remote class
814       ObjPtr<mirror::Class> remote_declaring_class =
815         FixUpRemotePointer(art_method->GetDeclaringClass(),
816                            RegionCommon<ArtMethod>::remote_contents_,
817                            RegionCommon<ArtMethod>::boot_map_);
818       // local class
819       ObjPtr<mirror::Class> declaring_class =
820         RemoteContentsPointerToLocal(remote_declaring_class,
821                                      RegionCommon<ArtMethod>::remote_contents_,
822                                      RegionCommon<ArtMethod>::image_header_);
823       DumpOneArtMethod(art_method, declaring_class, remote_declaring_class);
824     }
825   }
826 
DumpFalseDirtyEntries()827   void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
828     os_ << "\n" << "  False-dirty ArtMethods\n";
829     os_ << "      field contents:\n";
830     for (ArtMethod* method : false_dirty_entries_) {
831       // local class
832       ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
833       DumpOneArtMethod(method, declaring_class, nullptr);
834     }
835   }
836 
DumpCleanEntries()837   void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
838   }
839 
840  private:
841   std::ostream& os_;
842   MemberInfo member_info_;
843   std::map<const void*, std::string> entry_point_names_;
844   ClassLinker* class_linker_;
845 
846   // Compute a map of addresses to names in the boot OAT file(s).
BuildEntryPointNames()847   void BuildEntryPointNames() {
848     OatFileManager& oat_file_manager = Runtime::Current()->GetOatFileManager();
849     std::vector<const OatFile*> boot_oat_files = oat_file_manager.GetBootOatFiles();
850     for (const OatFile* oat_file : boot_oat_files) {
851       const OatHeader& oat_header = oat_file->GetOatHeader();
852       const void* jdl = oat_header.GetJniDlsymLookupTrampoline();
853       if (jdl != nullptr) {
854         entry_point_names_[jdl] = "JniDlsymLookupTrampoline (from boot oat file)";
855       }
856       const void* jdlc = oat_header.GetJniDlsymLookupCriticalTrampoline();
857       if (jdlc != nullptr) {
858         entry_point_names_[jdlc] = "JniDlsymLookupCriticalTrampoline (from boot oat file)";
859       }
860       const void* qgjt = oat_header.GetQuickGenericJniTrampoline();
861       if (qgjt != nullptr) {
862         entry_point_names_[qgjt] = "QuickGenericJniTrampoline (from boot oat file)";
863       }
864       const void* qrt = oat_header.GetQuickResolutionTrampoline();
865       if (qrt != nullptr) {
866         entry_point_names_[qrt] = "QuickResolutionTrampoline (from boot oat file)";
867       }
868       const void* qict = oat_header.GetQuickImtConflictTrampoline();
869       if (qict != nullptr) {
870         entry_point_names_[qict] = "QuickImtConflictTrampoline (from boot oat file)";
871       }
872       const void* q2ib = oat_header.GetQuickToInterpreterBridge();
873       if (q2ib != nullptr) {
874         entry_point_names_[q2ib] = "QuickToInterpreterBridge (from boot oat file)";
875       }
876     }
877   }
878 
StringFromBytes(const uint8_t * bytes,size_t size)879   std::string StringFromBytes(const uint8_t* bytes, size_t size) {
880     switch (size) {
881       case 1:
882         return StringPrintf("%" PRIx8, *bytes);
883       case 2:
884         return StringPrintf("%" PRIx16, *reinterpret_cast<const uint16_t*>(bytes));
885       case 4:
886       case 8: {
887         // Compute an address if the bytes might contain one.
888         uint64_t intval;
889         if (size == 4) {
890           intval = *reinterpret_cast<const uint32_t*>(bytes);
891         } else {
892           intval = *reinterpret_cast<const uint64_t*>(bytes);
893         }
894         const void* addr = reinterpret_cast<const void*>(intval);
895         // Match the address against those that have Is* methods in the ClassLinker.
896         if (class_linker_->IsQuickToInterpreterBridge(addr)) {
897           return "QuickToInterpreterBridge";
898         } else if (class_linker_->IsQuickGenericJniStub(addr)) {
899           return "QuickGenericJniStub";
900         } else if (class_linker_->IsQuickResolutionStub(addr)) {
901           return "QuickResolutionStub";
902         } else if (class_linker_->IsJniDlsymLookupStub(addr)) {
903           return "JniDlsymLookupStub";
904         } else if (class_linker_->IsJniDlsymLookupCriticalStub(addr)) {
905           return "JniDlsymLookupCriticalStub";
906         }
907         // Match the address against those that we saved from the boot OAT files.
908         if (entry_point_names_.find(addr) != entry_point_names_.end()) {
909           return entry_point_names_[addr];
910         }
911         return StringPrintf("%" PRIx64, intval);
912       }
913       default:
914         LOG(WARNING) << "Don't know how to convert " << size << " bytes to integer";
915         return "<UNKNOWN>";
916     }
917   }
918 
DumpOneArtMethod(ArtMethod * art_method,ObjPtr<mirror::Class> declaring_class,ObjPtr<mirror::Class> remote_declaring_class)919   void DumpOneArtMethod(ArtMethod* art_method,
920                         ObjPtr<mirror::Class> declaring_class,
921                         ObjPtr<mirror::Class> remote_declaring_class)
922       REQUIRES_SHARED(Locks::mutator_lock_) {
923     PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet());
924     os_ << "        " << reinterpret_cast<const void*>(art_method) << " ";
925     os_ << "  entryPointFromJni: "
926         << reinterpret_cast<const void*>(art_method->GetDataPtrSize(pointer_size)) << ", ";
927     os_ << "  entryPointFromQuickCompiledCode: "
928         << reinterpret_cast<const void*>(
929                art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
930         << ", ";
931     os_ << "  isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
932     // Null for runtime metionds.
933     if (declaring_class != nullptr) {
934       os_ << "  class_status (local): " << declaring_class->GetStatus();
935     }
936     if (remote_declaring_class != nullptr) {
937       os_ << ",  class_status (remote): " << remote_declaring_class->GetStatus();
938     }
939     os_ << "\n";
940   }
941 
942   DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase);
943 };
944 
945 template <typename T>
946 class RegionData : public RegionSpecializedBase<T> {
947  public:
RegionData(std::ostream * os,ArrayRef<uint8_t> remote_contents,ArrayRef<uint8_t> zygote_contents,const backtrace_map_t & boot_map,const ImageHeader & image_header,bool dump_dirty_objects)948   RegionData(std::ostream* os,
949              ArrayRef<uint8_t> remote_contents,
950              ArrayRef<uint8_t> zygote_contents,
951              const backtrace_map_t& boot_map,
952              const ImageHeader& image_header,
953              bool dump_dirty_objects)
954       : RegionSpecializedBase<T>(os,
955                                  remote_contents,
956                                  zygote_contents,
957                                  boot_map,
958                                  image_header,
959                                  dump_dirty_objects),
960         os_(*os) {
961     CHECK(!remote_contents.empty());
962   }
963 
964   // Walk over the type T entries in theregion between begin_image_ptr and end_image_ptr,
965   // collecting and reporting data regarding dirty, difference, etc.
ProcessRegion(const MappingData & mapping_data,RemoteProcesses remotes,const uint8_t * begin_image_ptr)966   void ProcessRegion(const MappingData& mapping_data,
967                      RemoteProcesses remotes,
968                      const uint8_t* begin_image_ptr)
969       REQUIRES_SHARED(Locks::mutator_lock_) {
970     typename RegionSpecializedBase<T>::VisitorClass visitor(
971         [this](T* entry,
972                const uint8_t* begin_image_ptr,
973                const std::set<size_t>& dirty_page_set) REQUIRES_SHARED(Locks::mutator_lock_) {
974           this->ComputeEntryDirty(entry, begin_image_ptr, dirty_page_set);
975         },
976         begin_image_ptr,
977         mapping_data.dirty_page_set);
978     PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet());
979     RegionSpecializedBase<T>::VisitEntries(&visitor,
980                                            const_cast<uint8_t*>(begin_image_ptr),
981                                            pointer_size);
982 
983     // Looking at only dirty pages, figure out how many of those bytes belong to dirty entries.
984     // TODO: fix this now that there are multiple regions in a mapping.
985     float true_dirtied_percent =
986         RegionCommon<T>::GetDirtyEntryBytes() * 1.0f / (mapping_data.dirty_pages * kPageSize);
987 
988     // Entry specific statistics.
989     os_ << RegionCommon<T>::GetDifferentEntryCount() << " different entries, \n  "
990         << RegionCommon<T>::GetDirtyEntryBytes() << " different entry [bytes], \n  "
991         << RegionCommon<T>::GetFalseDirtyEntryCount() << " false dirty entries,\n  "
992         << RegionCommon<T>::GetFalseDirtyEntryBytes() << " false dirty entry [bytes], \n  "
993         << true_dirtied_percent << " different entries-vs-total in a dirty page;\n  "
994         << "\n";
995 
996     const uint8_t* base_ptr = begin_image_ptr;
997     switch (remotes) {
998       case RemoteProcesses::kZygoteOnly:
999         os_ << "  Zygote shared dirty entries: ";
1000         break;
1001       case RemoteProcesses::kImageAndZygote:
1002         os_ << "  Application dirty entries (private dirty): ";
1003         // If we are dumping private dirty, diff against the zygote map to make it clearer what
1004         // fields caused the page to be private dirty.
1005         base_ptr = RegionCommon<T>::zygote_contents_.data();
1006         break;
1007       case RemoteProcesses::kImageOnly:
1008         os_ << "  Application dirty entries (unknown whether private or shared dirty): ";
1009         break;
1010     }
1011     DiffDirtyEntries(ProcessType::kRemote,
1012                      begin_image_ptr,
1013                      RegionCommon<T>::remote_contents_,
1014                      base_ptr,
1015                      /*log_dirty_objects=*/true);
1016     // Print shared dirty after since it's less important.
1017     if (RegionCommon<T>::GetZygoteDirtyEntryCount() != 0) {
1018       // We only reach this point if both pids were specified.  Furthermore,
1019       // entries are only displayed here if they differed in both the image
1020       // and the zygote, so they are probably private dirty.
1021       CHECK(remotes == RemoteProcesses::kImageAndZygote);
1022       os_ << "\n" << "  Zygote dirty entries (probably shared dirty): ";
1023       DiffDirtyEntries(ProcessType::kZygote,
1024                        begin_image_ptr,
1025                        RegionCommon<T>::zygote_contents_,
1026                        begin_image_ptr,
1027                        /*log_dirty_objects=*/false);
1028     }
1029     RegionSpecializedBase<T>::DumpDirtyObjects();
1030     RegionSpecializedBase<T>::DumpDirtyEntries();
1031     RegionSpecializedBase<T>::DumpFalseDirtyEntries();
1032     RegionSpecializedBase<T>::DumpCleanEntries();
1033   }
1034 
1035  private:
1036   std::ostream& os_;
1037 
DiffDirtyEntries(ProcessType process_type,const uint8_t * begin_image_ptr,ArrayRef<uint8_t> contents,const uint8_t * base_ptr,bool log_dirty_objects)1038   void DiffDirtyEntries(ProcessType process_type,
1039                         const uint8_t* begin_image_ptr,
1040                         ArrayRef<uint8_t> contents,
1041                         const uint8_t* base_ptr,
1042                         bool log_dirty_objects)
1043       REQUIRES_SHARED(Locks::mutator_lock_) {
1044     os_ << RegionCommon<T>::dirty_entries_.size() << "\n";
1045     const std::set<T*>& entries =
1046         (process_type == ProcessType::kZygote) ?
1047             RegionCommon<T>::zygote_dirty_entries_:
1048             RegionCommon<T>::image_dirty_entries_;
1049     for (T* entry : entries) {
1050       uint8_t* entry_bytes = reinterpret_cast<uint8_t*>(entry);
1051       ptrdiff_t offset = entry_bytes - begin_image_ptr;
1052       uint8_t* remote_bytes = &contents[offset];
1053       RegionSpecializedBase<T>::DiffEntryContents(entry,
1054                                                   remote_bytes,
1055                                                   &base_ptr[offset],
1056                                                   log_dirty_objects);
1057     }
1058   }
1059 
ComputeEntryDirty(T * entry,const uint8_t * begin_image_ptr,const std::set<size_t> & dirty_pages)1060   void ComputeEntryDirty(T* entry,
1061                          const uint8_t* begin_image_ptr,
1062                          const std::set<size_t>& dirty_pages)
1063       REQUIRES_SHARED(Locks::mutator_lock_) {
1064     // Set up pointers in the remote and the zygote for comparison.
1065     uint8_t* current = reinterpret_cast<uint8_t*>(entry);
1066     ptrdiff_t offset = current - begin_image_ptr;
1067     T* entry_remote =
1068         reinterpret_cast<T*>(const_cast<uint8_t*>(&RegionCommon<T>::remote_contents_[offset]));
1069     const bool have_zygote = !RegionCommon<T>::zygote_contents_.empty();
1070     const uint8_t* current_zygote =
1071         have_zygote ? &RegionCommon<T>::zygote_contents_[offset] : nullptr;
1072     T* entry_zygote = reinterpret_cast<T*>(const_cast<uint8_t*>(current_zygote));
1073     // Visit and classify entries at the current location.
1074     RegionSpecializedBase<T>::VisitEntry(entry);
1075 
1076     // Test private dirty first.
1077     bool is_dirty = false;
1078     if (have_zygote) {
1079       bool private_dirty = EntriesDiffer(entry, entry_zygote, entry_remote);
1080       if (private_dirty) {
1081         // Private dirty, app vs zygote.
1082         is_dirty = true;
1083         RegionCommon<T>::AddImageDirtyEntry(entry);
1084       }
1085       if (EntriesDiffer(entry, entry_zygote, entry)) {
1086         // Shared dirty, zygote vs image.
1087         is_dirty = true;
1088         RegionCommon<T>::AddZygoteDirtyEntry(entry);
1089       }
1090     } else if (EntriesDiffer(entry, entry_remote, entry)) {
1091       // Shared or private dirty, app vs image.
1092       is_dirty = true;
1093       RegionCommon<T>::AddImageDirtyEntry(entry);
1094     }
1095     if (is_dirty) {
1096       // TODO: Add support dirty entries in zygote and image.
1097       RegionSpecializedBase<T>::AddDirtyEntry(entry, entry_remote);
1098     } else {
1099       RegionSpecializedBase<T>::AddCleanEntry(entry);
1100       if (RegionCommon<T>::IsEntryOnDirtyPage(entry, dirty_pages)) {
1101         // This entry was either never mutated or got mutated back to the same value.
1102         // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
1103         RegionSpecializedBase<T>::AddFalseDirtyEntry(entry);
1104       }
1105     }
1106   }
1107 
1108   DISALLOW_COPY_AND_ASSIGN(RegionData);
1109 };
1110 
1111 }  // namespace
1112 
1113 
1114 class ImgDiagDumper {
1115  public:
ImgDiagDumper(std::ostream * os,pid_t image_diff_pid,pid_t zygote_diff_pid,bool dump_dirty_objects)1116   explicit ImgDiagDumper(std::ostream* os,
1117                          pid_t image_diff_pid,
1118                          pid_t zygote_diff_pid,
1119                          bool dump_dirty_objects)
1120       : os_(os),
1121         image_diff_pid_(image_diff_pid),
1122         zygote_diff_pid_(zygote_diff_pid),
1123         dump_dirty_objects_(dump_dirty_objects),
1124         zygote_pid_only_(false) {}
1125 
Init()1126   bool Init() {
1127     std::ostream& os = *os_;
1128 
1129     if (image_diff_pid_ < 0 && zygote_diff_pid_ < 0) {
1130       os << "Either --image-diff-pid or --zygote-diff-pid (or both) must be specified.\n";
1131       return false;
1132     }
1133 
1134     // To avoid the combinations of command-line argument use cases:
1135     // If the user invoked with only --zygote-diff-pid, shuffle that to
1136     // image_diff_pid_, invalidate zygote_diff_pid_, and remember that
1137     // image_diff_pid_ is now special.
1138     if (image_diff_pid_ < 0) {
1139       image_diff_pid_ = zygote_diff_pid_;
1140       zygote_diff_pid_ = -1;
1141       zygote_pid_only_ = true;
1142     }
1143 
1144     {
1145       struct stat sts;
1146       std::string proc_pid_str =
1147           StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid_));  // NOLINT [runtime/int]
1148       if (stat(proc_pid_str.c_str(), &sts) == -1) {
1149         os << "Process does not exist";
1150         return false;
1151       }
1152     }
1153 
1154     auto open_proc_maps = [&os](pid_t pid, /*out*/ std::unique_ptr<BacktraceMap>* proc_maps) {
1155       // Open /proc/<pid>/maps to view memory maps.
1156       proc_maps->reset(BacktraceMap::Create(pid));
1157       if (*proc_maps == nullptr) {
1158         os << "Could not read backtrace maps for " << pid;
1159         return false;
1160       }
1161       return true;
1162     };
1163     auto open_file = [&os] (const char* file_name, /*out*/ std::unique_ptr<File>* file) {
1164       file->reset(OS::OpenFileForReading(file_name));
1165       if (*file == nullptr) {
1166         os << "Failed to open " << file_name << " for reading";
1167         return false;
1168       }
1169       return true;
1170     };
1171     auto open_mem_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* mem_file) {
1172       // Open /proc/<pid>/mem and for reading remote contents.
1173       std::string mem_file_name =
1174           StringPrintf("/proc/%ld/mem", static_cast<long>(pid));  // NOLINT [runtime/int]
1175       return open_file(mem_file_name.c_str(), mem_file);
1176     };
1177     auto open_pagemap_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* pagemap_file) {
1178       // Open /proc/<pid>/pagemap.
1179       std::string pagemap_file_name = StringPrintf(
1180           "/proc/%ld/pagemap", static_cast<long>(pid));  // NOLINT [runtime/int]
1181       return open_file(pagemap_file_name.c_str(), pagemap_file);
1182     };
1183 
1184     // Open files for inspecting image memory.
1185     std::unique_ptr<BacktraceMap> image_proc_maps;
1186     std::unique_ptr<File> image_mem_file;
1187     std::unique_ptr<File> image_pagemap_file;
1188     if (!open_proc_maps(image_diff_pid_, &image_proc_maps) ||
1189         !open_mem_file(image_diff_pid_, &image_mem_file) ||
1190         !open_pagemap_file(image_diff_pid_, &image_pagemap_file)) {
1191       return false;
1192     }
1193 
1194     // If zygote_diff_pid_ != -1, open files for inspecting zygote memory.
1195     std::unique_ptr<BacktraceMap> zygote_proc_maps;
1196     std::unique_ptr<File> zygote_mem_file;
1197     std::unique_ptr<File> zygote_pagemap_file;
1198     if (zygote_diff_pid_ != -1) {
1199       if (!open_proc_maps(zygote_diff_pid_, &zygote_proc_maps) ||
1200           !open_mem_file(zygote_diff_pid_, &zygote_mem_file) ||
1201           !open_pagemap_file(zygote_diff_pid_, &zygote_pagemap_file)) {
1202         return false;
1203       }
1204     }
1205 
1206     std::unique_ptr<File> clean_pagemap_file;
1207     std::unique_ptr<File> kpageflags_file;
1208     std::unique_ptr<File> kpagecount_file;
1209     if (!open_file("/proc/self/pagemap", &clean_pagemap_file) ||
1210         !open_file("/proc/kpageflags", &kpageflags_file) ||
1211         !open_file("/proc/kpagecount", &kpagecount_file)) {
1212       return false;
1213     }
1214 
1215     // Note: the boot image is not really clean but close enough.
1216     // For now, log pages found to be dirty.
1217     // TODO: Rewrite imgdiag to load boot image without creating a runtime.
1218     // FIXME: The following does not reliably detect dirty pages.
1219     Runtime* runtime = Runtime::Current();
1220     CHECK(!runtime->ShouldRelocate());
1221     size_t total_dirty_pages = 0u;
1222     for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
1223       const ImageHeader& image_header = space->GetImageHeader();
1224       const uint8_t* image_begin = image_header.GetImageBegin();
1225       const uint8_t* image_end = AlignUp(image_begin + image_header.GetImageSize(), kPageSize);
1226       size_t virtual_page_idx_begin = reinterpret_cast<uintptr_t>(image_begin) / kPageSize;
1227       size_t virtual_page_idx_end = reinterpret_cast<uintptr_t>(image_end) / kPageSize;
1228       size_t num_virtual_pages = virtual_page_idx_end - virtual_page_idx_begin;
1229 
1230       std::string error_msg;
1231       std::vector<uint64_t> page_frame_numbers(num_virtual_pages);
1232       if (!GetPageFrameNumbers(clean_pagemap_file.get(),
1233                                virtual_page_idx_begin,
1234                                ArrayRef<uint64_t>(page_frame_numbers),
1235                                &error_msg)) {
1236         os << "Failed to get page frame numbers for image space " << space->GetImageLocation()
1237            << ", error: " << error_msg;
1238         return false;
1239       }
1240 
1241       std::vector<uint64_t> page_flags(num_virtual_pages);
1242       if (!GetPageFlagsOrCounts(kpageflags_file.get(),
1243                                 ArrayRef<const uint64_t>(page_frame_numbers),
1244                                 ArrayRef<uint64_t>(page_flags),
1245                                 &error_msg)) {
1246         os << "Failed to get page flags for image space " << space->GetImageLocation()
1247            << ", error: " << error_msg;
1248         return false;
1249       }
1250 
1251       size_t num_dirty_pages = 0u;
1252       std::optional<size_t> first_dirty_page;
1253       for (size_t i = 0u, size = page_flags.size(); i != size; ++i) {
1254         if (UNLIKELY((page_flags[i] & kPageFlagsDirtyMask) != 0u)) {
1255           ++num_dirty_pages;
1256           if (!first_dirty_page.has_value()) {
1257             first_dirty_page = i;
1258           }
1259         }
1260       }
1261       if (num_dirty_pages != 0u) {
1262         DCHECK(first_dirty_page.has_value());
1263         os << "Found " << num_dirty_pages << " dirty pages for " << space->GetImageLocation()
1264            << ", first dirty page: " << first_dirty_page.value_or(0u);
1265         total_dirty_pages += num_dirty_pages;
1266       }
1267     }
1268 
1269     // Commit the mappings and files.
1270     image_proc_maps_ = std::move(image_proc_maps);
1271     image_mem_file_ = std::move(*image_mem_file);
1272     image_pagemap_file_ = std::move(*image_pagemap_file);
1273     if (zygote_diff_pid_ != -1) {
1274       zygote_proc_maps_ = std::move(zygote_proc_maps);
1275       zygote_mem_file_ = std::move(*zygote_mem_file);
1276       zygote_pagemap_file_ = std::move(*zygote_pagemap_file);
1277     }
1278     clean_pagemap_file_ = std::move(*clean_pagemap_file);
1279     kpageflags_file_ = std::move(*kpageflags_file);
1280     kpagecount_file_ = std::move(*kpagecount_file);
1281 
1282     return true;
1283   }
1284 
Dump(const ImageHeader & image_header,const std::string & image_location)1285   bool Dump(const ImageHeader& image_header, const std::string& image_location)
1286       REQUIRES_SHARED(Locks::mutator_lock_) {
1287     std::ostream& os = *os_;
1288     os << "IMAGE LOCATION: " << image_location << "\n\n";
1289 
1290     os << "MAGIC: " << image_header.GetMagic() << "\n\n";
1291 
1292     os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
1293 
1294     PrintPidLine("IMAGE", image_diff_pid_);
1295     os << "\n\n";
1296     PrintPidLine("ZYGOTE", zygote_diff_pid_);
1297     bool ret = true;
1298     if (image_diff_pid_ >= 0 || zygote_diff_pid_ >= 0) {
1299       ret = DumpImageDiff(image_header, image_location);
1300       os << "\n\n";
1301     }
1302 
1303     os << std::flush;
1304 
1305     return ret;
1306   }
1307 
1308  private:
DumpImageDiff(const ImageHeader & image_header,const std::string & image_location)1309   bool DumpImageDiff(const ImageHeader& image_header, const std::string& image_location)
1310       REQUIRES_SHARED(Locks::mutator_lock_) {
1311     return DumpImageDiffMap(image_header, image_location);
1312   }
1313 
ComputeDirtyBytes(const ImageHeader & image_header,const uint8_t * image_begin,const backtrace_map_t & boot_map,ArrayRef<uint8_t> remote_contents,MappingData * mapping_data)1314   bool ComputeDirtyBytes(const ImageHeader& image_header,
1315                          const uint8_t* image_begin,
1316                          const backtrace_map_t& boot_map,
1317                          ArrayRef<uint8_t> remote_contents,
1318                          MappingData* mapping_data /*out*/) {
1319     std::ostream& os = *os_;
1320 
1321     size_t virtual_page_idx = 0;   // Virtual page number (for an absolute memory address)
1322     size_t page_idx = 0;           // Page index relative to 0
1323     size_t previous_page_idx = 0;  // Previous page index relative to 0
1324 
1325 
1326     // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
1327     for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) {
1328       ptrdiff_t offset = begin - boot_map.start;
1329 
1330       // We treat the image header as part of the memory map for now
1331       // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
1332       // But it might still be interesting to see if any of the ImageHeader data mutated
1333       const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset;
1334       const uint8_t* remote_ptr = &remote_contents[offset];
1335 
1336       if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
1337         mapping_data->different_pages++;
1338 
1339         // Count the number of 32-bit integers that are different.
1340         for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
1341           const uint32_t* remote_ptr_int32 = reinterpret_cast<const uint32_t*>(remote_ptr);
1342           const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
1343 
1344           if (remote_ptr_int32[i] != local_ptr_int32[i]) {
1345             mapping_data->different_int32s++;
1346           }
1347         }
1348       }
1349     }
1350 
1351     std::vector<size_t> private_dirty_pages_for_section(ImageHeader::kSectionCount, 0u);
1352 
1353     // Iterate through one byte at a time.
1354     ptrdiff_t page_off_begin = image_header.GetImageBegin() - image_begin;
1355     for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) {
1356       previous_page_idx = page_idx;
1357       ptrdiff_t offset = begin - boot_map.start;
1358 
1359       // We treat the image header as part of the memory map for now
1360       // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
1361       // But it might still be interesting to see if any of the ImageHeader data mutated
1362       const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset;
1363       const uint8_t* remote_ptr = &remote_contents[offset];
1364 
1365       virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
1366 
1367       // Calculate the page index, relative to the 0th page where the image begins
1368       page_idx = (offset + page_off_begin) / kPageSize;
1369       if (*local_ptr != *remote_ptr) {
1370         // Track number of bytes that are different
1371         mapping_data->different_bytes++;
1372       }
1373 
1374       // Independently count the # of dirty pages on the remote side
1375       size_t remote_virtual_page_idx = begin / kPageSize;
1376       if (previous_page_idx != page_idx) {
1377         uint64_t page_count = 0xC0FFEE;
1378         // TODO: virtual_page_idx needs to be from the same process
1379         std::string error_msg;
1380         int dirtiness = (IsPageDirty(&image_pagemap_file_,     // Image-diff-pid procmap
1381                                      &clean_pagemap_file_,     // Self procmap
1382                                      &kpageflags_file_,
1383                                      &kpagecount_file_,
1384                                      remote_virtual_page_idx,  // potentially "dirty" page
1385                                      virtual_page_idx,         // true "clean" page
1386                                      &page_count,
1387                                      &error_msg));
1388         if (dirtiness < 0) {
1389           os << error_msg;
1390           return false;
1391         } else if (dirtiness > 0) {
1392           mapping_data->dirty_pages++;
1393           mapping_data->dirty_page_set.insert(mapping_data->dirty_page_set.end(), virtual_page_idx);
1394         }
1395 
1396         bool is_dirty = dirtiness > 0;
1397         bool is_private = page_count == 1;
1398 
1399         if (page_count == 1) {
1400           mapping_data->private_pages++;
1401         }
1402 
1403         if (is_dirty && is_private) {
1404           mapping_data->private_dirty_pages++;
1405           for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1406             const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i);
1407             if (image_header.GetImageSection(section).Contains(offset)) {
1408               ++private_dirty_pages_for_section[i];
1409             }
1410           }
1411         }
1412       }
1413     }
1414     mapping_data->false_dirty_pages = mapping_data->dirty_pages - mapping_data->different_pages;
1415     // Print low-level (bytes, int32s, pages) statistics.
1416     os << mapping_data->different_bytes << " differing bytes,\n  "
1417        << mapping_data->different_int32s << " differing int32s,\n  "
1418        << mapping_data->different_pages << " differing pages,\n  "
1419        << mapping_data->dirty_pages << " pages are dirty;\n  "
1420        << mapping_data->false_dirty_pages << " pages are false dirty;\n  "
1421        << mapping_data->private_pages << " pages are private;\n  "
1422        << mapping_data->private_dirty_pages << " pages are Private_Dirty\n  "
1423        << "\n";
1424 
1425     size_t total_private_dirty_pages = std::accumulate(private_dirty_pages_for_section.begin(),
1426                                                        private_dirty_pages_for_section.end(),
1427                                                        0u);
1428     os << "Image sections (total private dirty pages " << total_private_dirty_pages << ")\n";
1429     for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1430       const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i);
1431       os << section << " " << image_header.GetImageSection(section)
1432          << " private dirty pages=" << private_dirty_pages_for_section[i] << "\n";
1433     }
1434     os << "\n";
1435 
1436     return true;
1437   }
1438 
1439   // Look at /proc/$pid/mem and only diff the things from there
DumpImageDiffMap(const ImageHeader & image_header,const std::string & image_location)1440   bool DumpImageDiffMap(const ImageHeader& image_header, const std::string& image_location)
1441       REQUIRES_SHARED(Locks::mutator_lock_) {
1442     std::ostream& os = *os_;
1443     std::string error_msg;
1444 
1445     std::string image_location_base_name = GetImageLocationBaseName(image_location);
1446     // FIXME: BacktraceMap should provide a const_iterator so that we can take `maps` as const&.
1447     auto find_boot_map = [&os, &image_location_base_name](BacktraceMap& maps, const char* tag)
1448         -> std::optional<backtrace_map_t> {
1449       // Find the memory map for the current boot image component.
1450       for (const backtrace_map_t* map : maps) {
1451         // The map name ends with ']' if it's an anonymous memmap. We need to special case that
1452         // to find the boot image map in some cases.
1453         if (EndsWith(map->name, image_location_base_name) ||
1454             EndsWith(map->name, image_location_base_name + "]")) {
1455           if ((map->flags & PROT_WRITE) != 0) {
1456             return *map;
1457           }
1458           // In actuality there's more than 1 map, but the second one is read-only.
1459           // The one we care about is the write-able map.
1460           // The readonly maps are guaranteed to be identical, so its not interesting to compare
1461           // them.
1462         }
1463       }
1464       os << "Could not find map for " << image_location_base_name << " in " << tag;
1465       return std::nullopt;
1466     };
1467 
1468     // Find the current boot image mapping.
1469     std::optional<backtrace_map_t> maybe_boot_map = find_boot_map(*image_proc_maps_, "image");
1470     if (maybe_boot_map == std::nullopt) {
1471       return false;
1472     }
1473     backtrace_map_t boot_map = maybe_boot_map.value_or(backtrace_map_t{});
1474     // Sanity check boot_map_.
1475     CHECK(boot_map.end >= boot_map.start);
1476 
1477     // Adjust the `end` of the mapping. Some other mappings may have been
1478     // inserted within the image.
1479     boot_map.end = RoundUp(boot_map.start + image_header.GetImageSize(), kPageSize);
1480     // The size of the boot image mapping.
1481     size_t boot_map_size = boot_map.end - boot_map.start;
1482 
1483     // If zygote_diff_pid_ != -1, check that the zygote boot map is the same.
1484     if (zygote_diff_pid_ != -1) {
1485       std::optional<backtrace_map_t> maybe_zygote_boot_map =
1486           find_boot_map(*zygote_proc_maps_, "zygote");
1487       if (maybe_zygote_boot_map == std::nullopt) {
1488         return false;
1489       }
1490       backtrace_map_t zygote_boot_map = maybe_zygote_boot_map.value_or(backtrace_map_t{});
1491       // Adjust the `end` of the mapping. Some other mappings may have been
1492       // inserted within the image.
1493       zygote_boot_map.end = RoundUp(zygote_boot_map.start + image_header.GetImageSize(), kPageSize);
1494       if (zygote_boot_map.start != boot_map.start) {
1495         os << "Zygote boot map does not match image boot map: "
1496            << "zygote begin " << reinterpret_cast<const void*>(zygote_boot_map.start)
1497            << ", zygote end " << reinterpret_cast<const void*>(zygote_boot_map.end)
1498            << ", image begin " << reinterpret_cast<const void*>(boot_map.start)
1499            << ", image end " << reinterpret_cast<const void*>(boot_map.end);
1500         return false;
1501       }
1502     }
1503 
1504     // Walk the bytes and diff against our boot image
1505     os << "\nObserving boot image header at address "
1506        << reinterpret_cast<const void*>(&image_header)
1507        << "\n\n";
1508 
1509     const uint8_t* image_begin_unaligned = image_header.GetImageBegin();
1510     const uint8_t* image_end_unaligned = image_begin_unaligned + image_header.GetImageSize();
1511 
1512     // Adjust range to nearest page
1513     const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
1514     const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
1515 
1516     size_t image_size = image_end - image_begin;
1517     if (image_size != boot_map_size) {
1518       os << "Remote boot map size does not match local boot map size: "
1519          << "local size " << image_size
1520          << ", remote size " << boot_map_size;
1521       return false;
1522     }
1523 
1524     auto read_contents = [&](File* mem_file,
1525                              /*out*/ MemMap* map,
1526                              /*out*/ ArrayRef<uint8_t>* contents) {
1527       DCHECK_ALIGNED(boot_map.start, kPageSize);
1528       DCHECK_ALIGNED(boot_map_size, kPageSize);
1529       std::string name = "Contents of " + mem_file->GetPath();
1530       std::string local_error_msg;
1531       // We need to use low 4 GiB memory so that we can walk the objects using standard
1532       // functions that use ObjPtr<> which is checking that it fits into lower 4 GiB.
1533       *map = MemMap::MapAnonymous(name.c_str(),
1534                                   boot_map_size,
1535                                   PROT_READ | PROT_WRITE,
1536                                   /* low_4gb= */ true,
1537                                   &local_error_msg);
1538       if (!map->IsValid()) {
1539         os << "Failed to allocate anonymous mapping for " << boot_map_size << " bytes.\n";
1540         return false;
1541       }
1542       if (!mem_file->PreadFully(map->Begin(), boot_map_size, boot_map.start)) {
1543         os << "Could not fully read file " << image_mem_file_.GetPath();
1544         return false;
1545       }
1546       *contents = ArrayRef<uint8_t>(map->Begin(), boot_map_size);
1547       return true;
1548     };
1549     // The contents of /proc/<image_diff_pid_>/mem.
1550     MemMap remote_contents_map;
1551     ArrayRef<uint8_t> remote_contents;
1552     if (!read_contents(&image_mem_file_, &remote_contents_map, &remote_contents)) {
1553       return false;
1554     }
1555     // The contents of /proc/<zygote_diff_pid_>/mem.
1556     MemMap zygote_contents_map;
1557     ArrayRef<uint8_t> zygote_contents;
1558     if (zygote_diff_pid_ != -1) {
1559       if (!read_contents(&zygote_mem_file_, &zygote_contents_map, &zygote_contents)) {
1560         return false;
1561       }
1562     }
1563 
1564     // TODO: We need to update the entire diff to work with the ASLR. b/77856493
1565     // Since the images may be relocated, just check the sizes.
1566     if (static_cast<uintptr_t>(image_end - image_begin) != boot_map.end - boot_map.start) {
1567       os << "Remote boot map is a different size than local boot map: " <<
1568         "local begin " << reinterpret_cast<const void*>(image_begin) <<
1569         ", local end " << reinterpret_cast<const void*>(image_end) <<
1570         ", remote begin " << reinterpret_cast<const void*>(boot_map.start) <<
1571         ", remote end " << reinterpret_cast<const void*>(boot_map.end);
1572       return false;
1573       // For more validation should also check the ImageHeader from the file
1574     }
1575 
1576     MappingData mapping_data;
1577 
1578     os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", "
1579        << reinterpret_cast<void*>(boot_map.end) << ") had:\n  ";
1580     if (!ComputeDirtyBytes(image_header, image_begin, boot_map, remote_contents, &mapping_data)) {
1581       return false;
1582     }
1583     RemoteProcesses remotes;
1584     if (zygote_pid_only_) {
1585       remotes = RemoteProcesses::kZygoteOnly;
1586     } else if (zygote_diff_pid_ > 0) {
1587       remotes = RemoteProcesses::kImageAndZygote;
1588     } else {
1589       remotes = RemoteProcesses::kImageOnly;
1590     }
1591 
1592     // Check all the mirror::Object entries in the image.
1593     RegionData<mirror::Object> object_region_data(os_,
1594                                                   remote_contents,
1595                                                   zygote_contents,
1596                                                   boot_map,
1597                                                   image_header,
1598                                                   dump_dirty_objects_);
1599     object_region_data.ProcessRegion(mapping_data,
1600                                      remotes,
1601                                      image_begin_unaligned);
1602 
1603     // Check all the ArtMethod entries in the image.
1604     RegionData<ArtMethod> artmethod_region_data(os_,
1605                                                 remote_contents,
1606                                                 zygote_contents,
1607                                                 boot_map,
1608                                                 image_header,
1609                                                 dump_dirty_objects_);
1610     artmethod_region_data.ProcessRegion(mapping_data,
1611                                         remotes,
1612                                         image_begin_unaligned);
1613     return true;
1614   }
1615 
1616   // Note: On failure, `*page_frame_number` shall be clobbered.
GetPageFrameNumber(File * page_map_file,size_t virtual_page_index,uint64_t * page_frame_number,std::string * error_msg)1617   static bool GetPageFrameNumber(File* page_map_file,
1618                                  size_t virtual_page_index,
1619                                  /*out*/ uint64_t* page_frame_number,
1620                                  /*out*/ std::string* error_msg) {
1621     CHECK(page_frame_number != nullptr);
1622     return GetPageFrameNumbers(page_map_file,
1623                                virtual_page_index,
1624                                ArrayRef<uint64_t>(page_frame_number, 1u),
1625                                error_msg);
1626   }
1627 
1628   // Note: On failure, `page_frame_numbers[.]` shall be clobbered.
GetPageFrameNumbers(File * page_map_file,size_t virtual_page_index,ArrayRef<uint64_t> page_frame_numbers,std::string * error_msg)1629   static bool GetPageFrameNumbers(File* page_map_file,
1630                                   size_t virtual_page_index,
1631                                   /*out*/ ArrayRef<uint64_t> page_frame_numbers,
1632                                   /*out*/ std::string* error_msg) {
1633     CHECK(page_map_file != nullptr);
1634     CHECK_NE(page_frame_numbers.size(), 0u);
1635     CHECK(page_frame_numbers.data() != nullptr);
1636     CHECK(error_msg != nullptr);
1637 
1638     // Read 64-bit entries from /proc/$pid/pagemap to get the physical page frame numbers.
1639     if (!page_map_file->PreadFully(page_frame_numbers.data(),
1640                                    page_frame_numbers.size() * kPageMapEntrySize,
1641                                    virtual_page_index * kPageMapEntrySize)) {
1642       *error_msg = StringPrintf("Failed to read the virtual page index entries from %s, error: %s",
1643                                 page_map_file->GetPath().c_str(),
1644                                 strerror(errno));
1645       return false;
1646     }
1647 
1648     // Extract page frame numbers from pagemap entries.
1649     for (uint64_t& page_frame_number : page_frame_numbers) {
1650       page_frame_number &= kPageFrameNumberMask;
1651     }
1652 
1653     return true;
1654   }
1655 
1656   // Note: On failure, `page_flags_or_counts[.]` shall be clobbered.
GetPageFlagsOrCounts(File * kpage_file,ArrayRef<const uint64_t> page_frame_numbers,ArrayRef<uint64_t> page_flags_or_counts,std::string * error_msg)1657   static bool GetPageFlagsOrCounts(File* kpage_file,
1658                                    ArrayRef<const uint64_t> page_frame_numbers,
1659                                    /*out*/ ArrayRef<uint64_t> page_flags_or_counts,
1660                                    /*out*/ std::string* error_msg) {
1661     static_assert(kPageFlagsEntrySize == kPageCountEntrySize, "entry size check");
1662     CHECK_NE(page_frame_numbers.size(), 0u);
1663     CHECK_EQ(page_flags_or_counts.size(), page_frame_numbers.size());
1664     CHECK(kpage_file != nullptr);
1665     CHECK(page_frame_numbers.data() != nullptr);
1666     CHECK(page_flags_or_counts.data() != nullptr);
1667     CHECK(error_msg != nullptr);
1668 
1669     size_t size = page_frame_numbers.size();
1670     size_t i = 0;
1671     while (i != size) {
1672       size_t start = i;
1673       ++i;
1674       while (i != size && page_frame_numbers[i] - page_frame_numbers[start] == i - start) {
1675         ++i;
1676       }
1677       // Read 64-bit entries from /proc/kpageflags or /proc/kpagecount.
1678       if (!kpage_file->PreadFully(page_flags_or_counts.data() + start,
1679                                   (i - start) * kPageMapEntrySize,
1680                                   page_frame_numbers[start] * kPageFlagsEntrySize)) {
1681         *error_msg = StringPrintf("Failed to read the page flags or counts from %s, error: %s",
1682                                   kpage_file->GetPath().c_str(),
1683                                   strerror(errno));
1684         return false;
1685       }
1686     }
1687 
1688     return true;
1689   }
1690 
IsPageDirty(File * page_map_file,File * clean_pagemap_file,File * kpageflags_file,File * kpagecount_file,size_t virtual_page_idx,size_t clean_virtual_page_idx,uint64_t * page_count,std::string * error_msg)1691   static int IsPageDirty(File* page_map_file,
1692                          File* clean_pagemap_file,
1693                          File* kpageflags_file,
1694                          File* kpagecount_file,
1695                          size_t virtual_page_idx,
1696                          size_t clean_virtual_page_idx,
1697                          // Out parameters:
1698                          uint64_t* page_count, std::string* error_msg) {
1699     CHECK(page_map_file != nullptr);
1700     CHECK(clean_pagemap_file != nullptr);
1701     CHECK_NE(page_map_file, clean_pagemap_file);
1702     CHECK(kpageflags_file != nullptr);
1703     CHECK(kpagecount_file != nullptr);
1704     CHECK(page_count != nullptr);
1705     CHECK(error_msg != nullptr);
1706 
1707     // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
1708 
1709     uint64_t page_frame_number = 0;
1710     if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
1711       return -1;
1712     }
1713 
1714     uint64_t page_frame_number_clean = 0;
1715     if (!GetPageFrameNumber(clean_pagemap_file, clean_virtual_page_idx, &page_frame_number_clean,
1716                             error_msg)) {
1717       return -1;
1718     }
1719 
1720     // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
1721     uint64_t kpage_flags_entry = 0;
1722     if (!kpageflags_file->PreadFully(&kpage_flags_entry,
1723                                      kPageFlagsEntrySize,
1724                                      page_frame_number * kPageFlagsEntrySize)) {
1725       *error_msg = StringPrintf("Failed to read the page flags from %s",
1726                                 kpageflags_file->GetPath().c_str());
1727       return -1;
1728     }
1729 
1730     // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
1731     if (!kpagecount_file->PreadFully(page_count /*out*/,
1732                                      kPageCountEntrySize,
1733                                      page_frame_number * kPageCountEntrySize)) {
1734       *error_msg = StringPrintf("Failed to read the page count from %s",
1735                                 kpagecount_file->GetPath().c_str());
1736       return -1;
1737     }
1738 
1739     // There must be a page frame at the requested address.
1740     CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
1741     // The page frame must be memory mapped
1742     CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
1743 
1744     // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
1745     bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
1746 
1747     // page_frame_number_clean must come from the *same* process
1748     // but a *different* mmap than page_frame_number
1749     if (flags_dirty) {
1750       // FIXME: This check sometimes fails and the reason is not understood. b/123852774
1751       if (page_frame_number != page_frame_number_clean) {
1752         LOG(ERROR) << "Check failed: page_frame_number != page_frame_number_clean "
1753             << "(page_frame_number=" << page_frame_number
1754             << ", page_frame_number_clean=" << page_frame_number_clean << ")"
1755             << " count: " << *page_count << " flags: 0x" << std::hex << kpage_flags_entry;
1756       }
1757     }
1758 
1759     return (page_frame_number != page_frame_number_clean) ? 1 : 0;
1760   }
1761 
PrintPidLine(const std::string & kind,pid_t pid)1762   void PrintPidLine(const std::string& kind, pid_t pid) {
1763     if (pid < 0) {
1764       *os_ << kind << " DIFF PID: disabled\n\n";
1765     } else {
1766       *os_ << kind << " DIFF PID (" << pid << "): ";
1767     }
1768   }
1769 
1770   // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
BaseName(const std::string & str)1771   static std::string BaseName(const std::string& str) {
1772     size_t idx = str.rfind('/');
1773     if (idx == std::string::npos) {
1774       return str;
1775     }
1776 
1777     return str.substr(idx + 1);
1778   }
1779 
1780   // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
GetImageLocationBaseName(const std::string & image_location)1781   static std::string GetImageLocationBaseName(const std::string& image_location) {
1782     return BaseName(std::string(image_location));
1783   }
1784 
1785   static constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
1786   // bits 0-54 [in /proc/$pid/pagemap]
1787   static constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1;
1788 
1789   static constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
1790   static constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
1791   static constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4);  // in /proc/kpageflags
1792   static constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20);  // in /proc/kpageflags
1793   static constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11);  // in /proc/kpageflags
1794 
1795 
1796   std::ostream* os_;
1797   pid_t image_diff_pid_;  // Dump image diff against boot.art if pid is non-negative
1798   pid_t zygote_diff_pid_;  // Dump image diff against zygote boot.art if pid is non-negative
1799   bool dump_dirty_objects_;  // Adds dumping of objects that are dirty.
1800   bool zygote_pid_only_;  // The user only specified a pid for the zygote.
1801 
1802   // BacktraceMap used for finding the memory mapping of the image file.
1803   std::unique_ptr<BacktraceMap> image_proc_maps_;
1804   // A File for reading /proc/<image_diff_pid_>/mem.
1805   File image_mem_file_;
1806   // A File for reading /proc/<image_diff_pid_>/pagemap.
1807   File image_pagemap_file_;
1808 
1809   // BacktraceMap used for finding the memory mapping of the zygote image file.
1810   std::unique_ptr<BacktraceMap> zygote_proc_maps_;
1811   // A File for reading /proc/<zygote_diff_pid_>/mem.
1812   File zygote_mem_file_;
1813   // A File for reading /proc/<zygote_diff_pid_>/pagemap.
1814   File zygote_pagemap_file_;
1815 
1816   // A File for reading /proc/self/pagemap.
1817   File clean_pagemap_file_;
1818   // A File for reading /proc/kpageflags.
1819   File kpageflags_file_;
1820   // A File for reading /proc/kpagecount.
1821   File kpagecount_file_;
1822 
1823   DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
1824 };
1825 
DumpImage(Runtime * runtime,std::ostream * os,pid_t image_diff_pid,pid_t zygote_diff_pid,bool dump_dirty_objects)1826 static int DumpImage(Runtime* runtime,
1827                      std::ostream* os,
1828                      pid_t image_diff_pid,
1829                      pid_t zygote_diff_pid,
1830                      bool dump_dirty_objects) {
1831   ScopedObjectAccess soa(Thread::Current());
1832   gc::Heap* heap = runtime->GetHeap();
1833   const std::vector<gc::space::ImageSpace*>& image_spaces = heap->GetBootImageSpaces();
1834   CHECK(!image_spaces.empty());
1835   ImgDiagDumper img_diag_dumper(os,
1836                                 image_diff_pid,
1837                                 zygote_diff_pid,
1838                                 dump_dirty_objects);
1839   if (!img_diag_dumper.Init()) {
1840     return EXIT_FAILURE;
1841   }
1842   for (gc::space::ImageSpace* image_space : image_spaces) {
1843     const ImageHeader& image_header = image_space->GetImageHeader();
1844     if (!image_header.IsValid()) {
1845       fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
1846       return EXIT_FAILURE;
1847     }
1848 
1849     if (!img_diag_dumper.Dump(image_header, image_space->GetImageLocation())) {
1850       return EXIT_FAILURE;
1851     }
1852   }
1853   return EXIT_SUCCESS;
1854 }
1855 
1856 struct ImgDiagArgs : public CmdlineArgs {
1857  protected:
1858   using Base = CmdlineArgs;
1859 
ParseCustomart::ImgDiagArgs1860   ParseStatus ParseCustom(const char* raw_option,
1861                           size_t raw_option_length,
1862                           std::string* error_msg) override {
1863     DCHECK_EQ(strlen(raw_option), raw_option_length);
1864     {
1865       ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
1866       if (base_parse != kParseUnknownArgument) {
1867         return base_parse;
1868       }
1869     }
1870 
1871     std::string_view option(raw_option, raw_option_length);
1872     if (StartsWith(option, "--image-diff-pid=")) {
1873       const char* image_diff_pid = raw_option + strlen("--image-diff-pid=");
1874 
1875       if (!android::base::ParseInt(image_diff_pid, &image_diff_pid_)) {
1876         *error_msg = "Image diff pid out of range";
1877         return kParseError;
1878       }
1879     } else if (StartsWith(option, "--zygote-diff-pid=")) {
1880       const char* zygote_diff_pid = raw_option + strlen("--zygote-diff-pid=");
1881 
1882       if (!android::base::ParseInt(zygote_diff_pid, &zygote_diff_pid_)) {
1883         *error_msg = "Zygote diff pid out of range";
1884         return kParseError;
1885       }
1886     } else if (option == "--dump-dirty-objects") {
1887       dump_dirty_objects_ = true;
1888     } else {
1889       return kParseUnknownArgument;
1890     }
1891 
1892     return kParseOk;
1893   }
1894 
ParseChecksart::ImgDiagArgs1895   ParseStatus ParseChecks(std::string* error_msg) override {
1896     // Perform the parent checks.
1897     ParseStatus parent_checks = Base::ParseChecks(error_msg);
1898     if (parent_checks != kParseOk) {
1899       return parent_checks;
1900     }
1901 
1902     // Perform our own checks.
1903 
1904     if (kill(image_diff_pid_,
1905              /*sig*/0) != 0) {  // No signal is sent, perform error-checking only.
1906       // Check if the pid exists before proceeding.
1907       if (errno == ESRCH) {
1908         *error_msg = "Process specified does not exist";
1909       } else {
1910         *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
1911       }
1912       return kParseError;
1913     } else if (instruction_set_ != InstructionSet::kNone && instruction_set_ != kRuntimeISA) {
1914       // Don't allow different ISAs since the images are ISA-specific.
1915       // Right now the code assumes both the runtime ISA and the remote ISA are identical.
1916       *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
1917       return kParseError;
1918     }
1919 
1920     return kParseOk;
1921   }
1922 
GetUsageart::ImgDiagArgs1923   std::string GetUsage() const override {
1924     std::string usage;
1925 
1926     usage +=
1927         "Usage: imgdiag [options] ...\n"
1928         "    Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
1929         "    Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
1930         "\n";
1931 
1932     usage += Base::GetUsage();
1933 
1934     usage +=  // Optional.
1935         "  --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
1936         "      Example: --image-diff-pid=$(pid zygote)\n"
1937         "  --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff "
1938         "against.\n"
1939         "      Example: --zygote-diff-pid=$(pid zygote)\n"
1940         "  --dump-dirty-objects: additionally output dirty objects of interest.\n"
1941         "\n";
1942 
1943     return usage;
1944   }
1945 
1946  public:
1947   pid_t image_diff_pid_ = -1;
1948   pid_t zygote_diff_pid_ = -1;
1949   bool dump_dirty_objects_ = false;
1950 };
1951 
1952 struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
ExecuteWithRuntimeart::ImgDiagMain1953   bool ExecuteWithRuntime(Runtime* runtime) override {
1954     CHECK(args_ != nullptr);
1955 
1956     return DumpImage(runtime,
1957                      args_->os_,
1958                      args_->image_diff_pid_,
1959                      args_->zygote_diff_pid_,
1960                      args_->dump_dirty_objects_) == EXIT_SUCCESS;
1961   }
1962 };
1963 
1964 }  // namespace art
1965 
main(int argc,char ** argv)1966 int main(int argc, char** argv) {
1967   art::ImgDiagMain main;
1968   return main.Main(argc, argv);
1969 }
1970