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