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