blob: d38f4669d3634e2e93c25ac30e6e70cf728bfaca [file] [log] [blame]
Igor Murashkin37743352014-11-13 14:38:00 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19
20#include <fstream>
Andreas Gampe7ad71d02016-04-04 13:49:18 -070021#include <functional>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080023#include <map>
Vladimir Marko1f146b72019-03-08 16:28:08 +000024#include <optional>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include <set>
26#include <string>
Mathieu Chartiercb044bc2016-04-01 13:56:41 -070027#include <unordered_set>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include <vector>
Igor Murashkin37743352014-11-13 14:38:00 -080029
Andreas Gampef9411702018-09-06 17:16:57 -070030#include <android-base/parseint.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080031#include "android-base/stringprintf.h"
32
Andreas Gampea1d2f952017-04-20 22:53:58 -070033#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "art_method-inl.h"
Vladimir Marko1f146b72019-03-08 16:28:08 +000035#include "base/array_ref.h"
David Sehrc431b9d2018-03-02 12:01:51 -080036#include "base/os.h"
Vladimir Marko1f146b72019-03-08 16:28:08 +000037#include "base/string_view_cpp20.h"
Igor Murashkin37743352014-11-13 14:38:00 -080038#include "base/unix_file/fd_file.h"
David Sehra49e0532017-08-25 08:05:29 -070039#include "class_linker.h"
Igor Murashkin37743352014-11-13 14:38:00 -080040#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070041#include "gc/space/image_space.h"
Vladimir Marko4df2d802018-09-27 16:42:44 +000042#include "image-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080043#include "mirror/class-inl.h"
44#include "mirror/object-inl.h"
David Sehra49e0532017-08-25 08:05:29 -070045#include "oat.h"
46#include "oat_file.h"
47#include "oat_file_manager.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070048#include "scoped_thread_state_change-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080049
Igor Murashkin37743352014-11-13 14:38:00 -080050#include "backtrace/BacktraceMap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070051#include "cmdline.h"
Igor Murashkin37743352014-11-13 14:38:00 -080052
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070053#include <signal.h>
Igor Murashkin37743352014-11-13 14:38:00 -080054#include <sys/stat.h>
55#include <sys/types.h>
Igor Murashkin37743352014-11-13 14:38:00 -080056
57namespace art {
58
Andreas Gampe46ee31b2016-12-14 10:11:49 -080059using android::base::StringPrintf;
60
David Sehrb4005f02017-06-20 19:11:40 -070061namespace {
62
63constexpr size_t kMaxAddressPrint = 5;
64
65enum class ProcessType {
66 kZygote,
67 kRemote
68};
69
70enum class RemoteProcesses {
71 kImageOnly,
72 kZygoteOnly,
73 kImageAndZygote
74};
75
76struct 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
95static 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
105static 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
154template <typename K, typename V, typename D>
155static 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.
173template <typename T>
Vladimir Markod93e3742018-07-18 10:58:13 +0100174static ObjPtr<T> FixUpRemotePointer(ObjPtr<T> remote_ptr,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100175 ArrayRef<uint8_t> remote_contents,
Vladimir Markod93e3742018-07-18 10:58:13 +0100176 const backtrace_map_t& boot_map)
177 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehrb4005f02017-06-20 19:11:40 -0700178 if (remote_ptr == nullptr) {
179 return nullptr;
180 }
181
Vladimir Markod93e3742018-07-18 10:58:13 +0100182 uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr.Ptr());
David Sehrb4005f02017-06-20 19:11:40 -0700183
Mathieu Chartier21f7ac12018-07-09 16:18:27 -0700184 // 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 }
David Sehrb4005f02017-06-20 19:11:40 -0700189
190 off_t boot_offset = remote - boot_map.start;
191
192 return reinterpret_cast<T*>(&remote_contents[boot_offset]);
193}
194
195template <typename T>
Vladimir Markod93e3742018-07-18 10:58:13 +0100196static ObjPtr<T> RemoteContentsPointerToLocal(ObjPtr<T> remote_ptr,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100197 ArrayRef<uint8_t> remote_contents,
Vladimir Markod93e3742018-07-18 10:58:13 +0100198 const ImageHeader& image_header)
199 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehrb4005f02017-06-20 19:11:40 -0700200 if (remote_ptr == nullptr) {
201 return nullptr;
202 }
203
Vladimir Markod93e3742018-07-18 10:58:13 +0100204 uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr.Ptr());
David Sehrb4005f02017-06-20 19:11:40 -0700205 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
212template <typename T> size_t EntrySize(T* entry);
213template<> size_t EntrySize(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
214 return object->SizeOf();
215}
216template<> size_t EntrySize(ArtMethod* art_method) REQUIRES_SHARED(Locks::mutator_lock_) {
217 return sizeof(*art_method);
218}
219
220template <typename T>
221static bool EntriesDiffer(T* entry1, T* entry2) REQUIRES_SHARED(Locks::mutator_lock_) {
222 return memcmp(entry1, entry2, EntrySize(entry1)) != 0;
223}
224
225template <typename T>
226struct RegionCommon {
227 public:
228 RegionCommon(std::ostream* os,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100229 ArrayRef<uint8_t> remote_contents,
230 ArrayRef<uint8_t> zygote_contents,
David Sehrb4005f02017-06-20 19:11:40 -0700231 const backtrace_map_t& boot_map,
232 const ImageHeader& image_header) :
233 os_(*os),
234 remote_contents_(remote_contents),
235 zygote_contents_(zygote_contents),
236 boot_map_(boot_map),
237 image_header_(image_header),
238 different_entries_(0),
239 dirty_entry_bytes_(0),
240 false_dirty_entry_bytes_(0) {
Vladimir Marko71d614f2019-04-01 15:19:40 +0100241 CHECK(!remote_contents.empty());
David Sehrb4005f02017-06-20 19:11:40 -0700242 }
243
244 void DumpSamplesAndOffsetCount() {
245 os_ << " sample object addresses: ";
246 for (size_t i = 0; i < dirty_entries_.size() && i < kMaxAddressPrint; ++i) {
247 T* entry = dirty_entries_[i];
248 os_ << reinterpret_cast<void*>(entry) << ", ";
249 }
250 os_ << "\n";
251 os_ << " dirty byte +offset:count list = ";
252 std::vector<std::pair<size_t, off_t>> field_dirty_count_sorted =
253 SortByValueDesc<off_t, size_t, size_t>(field_dirty_count_);
254 for (const std::pair<size_t, off_t>& pair : field_dirty_count_sorted) {
255 off_t offset = pair.second;
256 size_t count = pair.first;
257 os_ << "+" << offset << ":" << count << ", ";
258 }
259 os_ << "\n";
260 }
261
262 size_t GetDifferentEntryCount() const { return different_entries_; }
263 size_t GetDirtyEntryBytes() const { return dirty_entry_bytes_; }
264 size_t GetFalseDirtyEntryCount() const { return false_dirty_entries_.size(); }
265 size_t GetFalseDirtyEntryBytes() const { return false_dirty_entry_bytes_; }
266 size_t GetZygoteDirtyEntryCount() const { return zygote_dirty_entries_.size(); }
267
268 protected:
269 bool IsEntryOnDirtyPage(T* entry, const std::set<size_t>& dirty_pages) const
270 REQUIRES_SHARED(Locks::mutator_lock_) {
271 size_t size = EntrySize(entry);
272 size_t page_off = 0;
273 size_t current_page_idx;
274 uintptr_t entry_address = reinterpret_cast<uintptr_t>(entry);
275 // Iterate every page this entry belongs to
276 do {
277 current_page_idx = entry_address / kPageSize + page_off;
278 if (dirty_pages.find(current_page_idx) != dirty_pages.end()) {
279 // This entry is on a dirty page
280 return true;
281 }
282 page_off++;
283 } while ((current_page_idx * kPageSize) < RoundUp(entry_address + size, kObjectAlignment));
284 return false;
285 }
286
287 void AddZygoteDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
288 zygote_dirty_entries_.insert(entry);
289 }
290
291 void AddImageDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
292 image_dirty_entries_.insert(entry);
293 }
294
295 void AddFalseDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
296 false_dirty_entries_.push_back(entry);
297 false_dirty_entry_bytes_ += EntrySize(entry);
298 }
299
300 // The output stream to write to.
301 std::ostream& os_;
302 // The byte contents of the remote (image) process' image.
Vladimir Marko71d614f2019-04-01 15:19:40 +0100303 ArrayRef<uint8_t> remote_contents_;
David Sehrb4005f02017-06-20 19:11:40 -0700304 // The byte contents of the zygote process' image.
Vladimir Marko71d614f2019-04-01 15:19:40 +0100305 ArrayRef<uint8_t> zygote_contents_;
David Sehrb4005f02017-06-20 19:11:40 -0700306 const backtrace_map_t& boot_map_;
307 const ImageHeader& image_header_;
308
309 // Count of entries that are different.
310 size_t different_entries_;
311
312 // Local entries that are dirty (differ in at least one byte).
313 size_t dirty_entry_bytes_;
314 std::vector<T*> dirty_entries_;
315
316 // Local entries that are clean, but located on dirty pages.
317 size_t false_dirty_entry_bytes_;
318 std::vector<T*> false_dirty_entries_;
319
320 // Image dirty entries
321 // If zygote_pid_only_ == true, these are shared dirty entries in the zygote.
322 // If zygote_pid_only_ == false, these are private dirty entries in the application.
323 std::set<T*> image_dirty_entries_;
324
325 // Zygote dirty entries (probably private dirty).
326 // We only add entries here if they differed in both the image and the zygote, so
327 // they are probably private dirty.
328 std::set<T*> zygote_dirty_entries_;
329
330 std::map<off_t /* field offset */, size_t /* count */> field_dirty_count_;
331
332 private:
333 DISALLOW_COPY_AND_ASSIGN(RegionCommon);
334};
335
336template <typename T>
337class RegionSpecializedBase : public RegionCommon<T> {
338};
339
340// Region analysis for mirror::Objects
David Sehra49e0532017-08-25 08:05:29 -0700341class ImgObjectVisitor : public ObjectVisitor {
342 public:
343 using ComputeDirtyFunc = std::function<void(mirror::Object* object,
344 const uint8_t* begin_image_ptr,
345 const std::set<size_t>& dirty_pages)>;
Andreas Gampe68562142018-06-20 21:49:11 +0000346 ImgObjectVisitor(ComputeDirtyFunc dirty_func,
David Sehra49e0532017-08-25 08:05:29 -0700347 const uint8_t* begin_image_ptr,
348 const std::set<size_t>& dirty_pages) :
Andreas Gampebc802de2018-06-20 17:24:11 -0700349 dirty_func_(std::move(dirty_func)),
David Sehra49e0532017-08-25 08:05:29 -0700350 begin_image_ptr_(begin_image_ptr),
351 dirty_pages_(dirty_pages) { }
352
Roland Levillainf73caca2018-08-24 17:19:07 +0100353 ~ImgObjectVisitor() override { }
David Sehra49e0532017-08-25 08:05:29 -0700354
Roland Levillainf73caca2018-08-24 17:19:07 +0100355 void Visit(mirror::Object* object) override REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehra49e0532017-08-25 08:05:29 -0700356 // Sanity check that we are reading a real mirror::Object
357 CHECK(object->GetClass() != nullptr) << "Image object at address "
358 << object
359 << " has null class";
360 if (kUseBakerReadBarrier) {
361 object->AssertReadBarrierState();
362 }
363 dirty_func_(object, begin_image_ptr_, dirty_pages_);
364 }
365
366 private:
Andreas Gampebc802de2018-06-20 17:24:11 -0700367 const ComputeDirtyFunc dirty_func_;
David Sehra49e0532017-08-25 08:05:29 -0700368 const uint8_t* begin_image_ptr_;
369 const std::set<size_t>& dirty_pages_;
370};
371
David Sehrb4005f02017-06-20 19:11:40 -0700372template<>
373class RegionSpecializedBase<mirror::Object> : public RegionCommon<mirror::Object> {
374 public:
375 RegionSpecializedBase(std::ostream* os,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100376 ArrayRef<uint8_t> remote_contents,
377 ArrayRef<uint8_t> zygote_contents,
David Sehrb4005f02017-06-20 19:11:40 -0700378 const backtrace_map_t& boot_map,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700379 const ImageHeader& image_header,
380 bool dump_dirty_objects)
381 : RegionCommon<mirror::Object>(os, remote_contents, zygote_contents, boot_map, image_header),
382 os_(*os),
383 dump_dirty_objects_(dump_dirty_objects) { }
David Sehrb4005f02017-06-20 19:11:40 -0700384
David Sehra49e0532017-08-25 08:05:29 -0700385 // Define a common public type name for use by RegionData.
386 using VisitorClass = ImgObjectVisitor;
David Sehrb4005f02017-06-20 19:11:40 -0700387
David Sehra49e0532017-08-25 08:05:29 -0700388 void VisitEntries(VisitorClass* visitor,
389 uint8_t* base,
390 PointerSize pointer_size)
David Sehrb4005f02017-06-20 19:11:40 -0700391 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehra49e0532017-08-25 08:05:29 -0700392 RegionCommon<mirror::Object>::image_header_.VisitObjects(visitor, base, pointer_size);
David Sehrb4005f02017-06-20 19:11:40 -0700393 }
394
395 void VisitEntry(mirror::Object* entry)
396 REQUIRES_SHARED(Locks::mutator_lock_) {
397 // Unconditionally store the class descriptor in case we need it later
398 mirror::Class* klass = entry->GetClass();
399 class_data_[klass].descriptor = GetClassDescriptor(klass);
400 }
401
402 void AddCleanEntry(mirror::Object* entry)
403 REQUIRES_SHARED(Locks::mutator_lock_) {
404 class_data_[entry->GetClass()].AddCleanObject();
405 }
406
407 void AddFalseDirtyEntry(mirror::Object* entry)
408 REQUIRES_SHARED(Locks::mutator_lock_) {
409 RegionCommon<mirror::Object>::AddFalseDirtyEntry(entry);
410 class_data_[entry->GetClass()].AddFalseDirtyObject(entry);
411 }
412
413 void AddDirtyEntry(mirror::Object* entry, mirror::Object* entry_remote)
414 REQUIRES_SHARED(Locks::mutator_lock_) {
415 size_t entry_size = EntrySize(entry);
416 ++different_entries_;
417 dirty_entry_bytes_ += entry_size;
418 // Log dirty count and objects for class objects only.
419 mirror::Class* klass = entry->GetClass();
420 if (klass->IsClassClass()) {
421 // Increment counts for the fields that are dirty
422 const uint8_t* current = reinterpret_cast<const uint8_t*>(entry);
423 const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(entry_remote);
424 for (size_t i = 0; i < entry_size; ++i) {
425 if (current[i] != current_remote[i]) {
426 field_dirty_count_[i]++;
427 }
428 }
429 dirty_entries_.push_back(entry);
430 }
431 class_data_[klass].AddDirtyObject(entry, entry_remote);
432 }
433
Jeff Haoc23b0c02017-07-27 18:19:38 -0700434 void DiffEntryContents(mirror::Object* entry,
435 uint8_t* remote_bytes,
436 const uint8_t* base_ptr,
437 bool log_dirty_objects)
David Sehrb4005f02017-06-20 19:11:40 -0700438 REQUIRES_SHARED(Locks::mutator_lock_) {
439 const char* tabs = " ";
440 // Attempt to find fields for all dirty bytes.
441 mirror::Class* klass = entry->GetClass();
442 if (entry->IsClass()) {
443 os_ << tabs
444 << "Class " << mirror::Class::PrettyClass(entry->AsClass()) << " " << entry << "\n";
445 } else {
446 os_ << tabs
447 << "Instance of " << mirror::Class::PrettyClass(klass) << " " << entry << "\n";
448 }
449
450 std::unordered_set<ArtField*> dirty_instance_fields;
451 std::unordered_set<ArtField*> dirty_static_fields;
452 // Examine the bytes comprising the Object, computing which fields are dirty
453 // and recording them for later display. If the Object is an array object,
454 // compute the dirty entries.
David Sehrb4005f02017-06-20 19:11:40 -0700455 mirror::Object* remote_entry = reinterpret_cast<mirror::Object*>(remote_bytes);
456 for (size_t i = 0, count = entry->SizeOf(); i < count; ++i) {
Mathieu Chartier51e79652017-07-24 15:43:38 -0700457 if (base_ptr[i] != remote_bytes[i]) {
David Sehrb4005f02017-06-20 19:11:40 -0700458 ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i);
459 if (field != nullptr) {
460 dirty_instance_fields.insert(field);
461 } else if (entry->IsClass()) {
462 field = ArtField::FindStaticFieldWithOffset</*exact*/false>(entry->AsClass(), i);
463 if (field != nullptr) {
464 dirty_static_fields.insert(field);
465 }
466 }
467 if (field == nullptr) {
468 if (klass->IsArrayClass()) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000469 ObjPtr<mirror::Class> component_type = klass->GetComponentType();
David Sehrb4005f02017-06-20 19:11:40 -0700470 Primitive::Type primitive_type = component_type->GetPrimitiveType();
471 size_t component_size = Primitive::ComponentSize(primitive_type);
472 size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
Vladimir Marko1f146b72019-03-08 16:28:08 +0000473 DCHECK_ALIGNED_PARAM(data_offset, component_size);
David Sehrb4005f02017-06-20 19:11:40 -0700474 if (i >= data_offset) {
475 os_ << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n";
Vladimir Marko1f146b72019-03-08 16:28:08 +0000476 // Skip the remaining bytes of this element to prevent spam.
477 DCHECK(IsPowerOfTwo(component_size));
478 i |= component_size - 1;
David Sehrb4005f02017-06-20 19:11:40 -0700479 continue;
480 }
481 }
482 os_ << tabs << "No field for byte offset " << i << "\n";
483 }
484 }
485 }
486 // Dump different fields.
487 if (!dirty_instance_fields.empty()) {
488 os_ << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n";
489 for (ArtField* field : dirty_instance_fields) {
490 os_ << tabs << ArtField::PrettyField(field)
491 << " original=" << PrettyFieldValue(field, entry)
492 << " remote=" << PrettyFieldValue(field, remote_entry) << "\n";
493 }
494 }
495 if (!dirty_static_fields.empty()) {
Jeff Haoc23b0c02017-07-27 18:19:38 -0700496 if (dump_dirty_objects_ && log_dirty_objects) {
497 dirty_objects_.insert(entry);
498 }
David Sehrb4005f02017-06-20 19:11:40 -0700499 os_ << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n";
500 for (ArtField* field : dirty_static_fields) {
501 os_ << tabs << ArtField::PrettyField(field)
502 << " original=" << PrettyFieldValue(field, entry)
503 << " remote=" << PrettyFieldValue(field, remote_entry) << "\n";
504 }
505 }
506 os_ << "\n";
507 }
508
Jeff Haoc23b0c02017-07-27 18:19:38 -0700509 void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) {
510 for (mirror::Object* obj : dirty_objects_) {
511 if (obj->IsClass()) {
512 os_ << "Private dirty object: " << obj->AsClass()->PrettyDescriptor() << "\n";
513 }
514 }
515 }
516
David Sehrb4005f02017-06-20 19:11:40 -0700517 void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
518 // vector of pairs (size_t count, Class*)
519 auto dirty_object_class_values =
520 SortByValueDesc<mirror::Class*, size_t, ClassData>(
521 class_data_,
522 [](const ClassData& d) { return d.dirty_object_count; });
523 os_ << "\n" << " Dirty object count by class:\n";
524 for (const auto& vk_pair : dirty_object_class_values) {
525 size_t dirty_object_count = vk_pair.first;
526 mirror::Class* klass = vk_pair.second;
527 ClassData& class_data = class_data_[klass];
528 size_t object_sizes = class_data.dirty_object_size_in_bytes;
529 float avg_dirty_bytes_per_class =
530 class_data.dirty_object_byte_count * 1.0f / object_sizes;
531 float avg_object_size = object_sizes * 1.0f / dirty_object_count;
532 const std::string& descriptor = class_data.descriptor;
533 os_ << " " << mirror::Class::PrettyClass(klass) << " ("
534 << "objects: " << dirty_object_count << ", "
535 << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
536 << "avg object size: " << avg_object_size << ", "
537 << "class descriptor: '" << descriptor << "'"
538 << ")\n";
539 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
540 DumpSamplesAndOffsetCount();
541 os_ << " field contents:\n";
542 for (mirror::Object* object : class_data.dirty_objects) {
543 // remote class object
Vladimir Markod93e3742018-07-18 10:58:13 +0100544 ObjPtr<mirror::Class> remote_klass =
545 ObjPtr<mirror::Class>::DownCast<mirror::Object>(object);
David Sehrb4005f02017-06-20 19:11:40 -0700546 // local class object
Vladimir Markod93e3742018-07-18 10:58:13 +0100547 ObjPtr<mirror::Class> local_klass =
David Sehrb4005f02017-06-20 19:11:40 -0700548 RemoteContentsPointerToLocal(remote_klass,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100549 RegionCommon<mirror::Object>::remote_contents_,
David Sehrb4005f02017-06-20 19:11:40 -0700550 RegionCommon<mirror::Object>::image_header_);
551 os_ << " " << reinterpret_cast<const void*>(object) << " ";
552 os_ << " class_status (remote): " << remote_klass->GetStatus() << ", ";
553 os_ << " class_status (local): " << local_klass->GetStatus();
554 os_ << "\n";
555 }
556 }
557 }
558 }
559
560 void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
561 // vector of pairs (size_t count, Class*)
562 auto false_dirty_object_class_values =
563 SortByValueDesc<mirror::Class*, size_t, ClassData>(
564 class_data_,
565 [](const ClassData& d) { return d.false_dirty_object_count; });
566 os_ << "\n" << " False-dirty object count by class:\n";
567 for (const auto& vk_pair : false_dirty_object_class_values) {
568 size_t object_count = vk_pair.first;
569 mirror::Class* klass = vk_pair.second;
570 ClassData& class_data = class_data_[klass];
571 size_t object_sizes = class_data.false_dirty_byte_count;
572 float avg_object_size = object_sizes * 1.0f / object_count;
573 const std::string& descriptor = class_data.descriptor;
574 os_ << " " << mirror::Class::PrettyClass(klass) << " ("
575 << "objects: " << object_count << ", "
576 << "avg object size: " << avg_object_size << ", "
577 << "total bytes: " << object_sizes << ", "
578 << "class descriptor: '" << descriptor << "'"
579 << ")\n";
580 }
581 }
582
583 void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
584 // vector of pairs (size_t count, Class*)
585 auto clean_object_class_values =
586 SortByValueDesc<mirror::Class*, size_t, ClassData>(
587 class_data_,
588 [](const ClassData& d) { return d.clean_object_count; });
589 os_ << "\n" << " Clean object count by class:\n";
590 for (const auto& vk_pair : clean_object_class_values) {
591 os_ << " " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
592 }
593 }
594
595 private:
596 // Aggregate and detail class data from an image diff.
597 struct ClassData {
598 size_t dirty_object_count = 0;
599 // Track only the byte-per-byte dirtiness (in bytes)
600 size_t dirty_object_byte_count = 0;
601 // Track the object-by-object dirtiness (in bytes)
602 size_t dirty_object_size_in_bytes = 0;
603 size_t clean_object_count = 0;
604 std::string descriptor;
605 size_t false_dirty_byte_count = 0;
606 size_t false_dirty_object_count = 0;
607 std::vector<mirror::Object*> false_dirty_objects;
608 // Remote pointers to dirty objects
609 std::vector<mirror::Object*> dirty_objects;
610
611 void AddCleanObject() REQUIRES_SHARED(Locks::mutator_lock_) {
612 ++clean_object_count;
613 }
614
615 void AddDirtyObject(mirror::Object* object, mirror::Object* object_remote)
616 REQUIRES_SHARED(Locks::mutator_lock_) {
617 ++dirty_object_count;
618 dirty_object_byte_count += CountDirtyBytes(object, object_remote);
619 dirty_object_size_in_bytes += EntrySize(object);
620 dirty_objects.push_back(object_remote);
621 }
622
623 void AddFalseDirtyObject(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
624 ++false_dirty_object_count;
625 false_dirty_objects.push_back(object);
626 false_dirty_byte_count += EntrySize(object);
627 }
628
629 private:
630 // Go byte-by-byte and figure out what exactly got dirtied
631 static size_t CountDirtyBytes(mirror::Object* object1, mirror::Object* object2)
632 REQUIRES_SHARED(Locks::mutator_lock_) {
633 const uint8_t* cur1 = reinterpret_cast<const uint8_t*>(object1);
634 const uint8_t* cur2 = reinterpret_cast<const uint8_t*>(object2);
635 size_t dirty_bytes = 0;
636 size_t object_size = EntrySize(object1);
637 for (size_t i = 0; i < object_size; ++i) {
638 if (cur1[i] != cur2[i]) {
639 dirty_bytes++;
640 }
641 }
642 return dirty_bytes;
643 }
644 };
645
646 std::ostream& os_;
Jeff Haoc23b0c02017-07-27 18:19:38 -0700647 bool dump_dirty_objects_;
648 std::unordered_set<mirror::Object*> dirty_objects_;
David Sehrb4005f02017-06-20 19:11:40 -0700649 std::map<mirror::Class*, ClassData> class_data_;
650
651 DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase);
652};
653
654// Region analysis for ArtMethods.
Mathieu Chartier9d5956a2019-03-22 11:29:08 -0700655class ImgArtMethodVisitor {
David Sehra49e0532017-08-25 08:05:29 -0700656 public:
657 using ComputeDirtyFunc = std::function<void(ArtMethod*,
658 const uint8_t*,
659 const std::set<size_t>&)>;
Andreas Gampe68562142018-06-20 21:49:11 +0000660 ImgArtMethodVisitor(ComputeDirtyFunc dirty_func,
David Sehra49e0532017-08-25 08:05:29 -0700661 const uint8_t* begin_image_ptr,
662 const std::set<size_t>& dirty_pages) :
Andreas Gampebc802de2018-06-20 17:24:11 -0700663 dirty_func_(std::move(dirty_func)),
David Sehra49e0532017-08-25 08:05:29 -0700664 begin_image_ptr_(begin_image_ptr),
665 dirty_pages_(dirty_pages) { }
Mathieu Chartier9d5956a2019-03-22 11:29:08 -0700666 void operator()(ArtMethod& method) const {
667 dirty_func_(&method, begin_image_ptr_, dirty_pages_);
David Sehra49e0532017-08-25 08:05:29 -0700668 }
669
670 private:
Andreas Gampebc802de2018-06-20 17:24:11 -0700671 const ComputeDirtyFunc dirty_func_;
David Sehra49e0532017-08-25 08:05:29 -0700672 const uint8_t* begin_image_ptr_;
673 const std::set<size_t>& dirty_pages_;
674};
675
676// Struct and functor for computing offsets of members of ArtMethods.
677// template <typename RegionType>
678struct MemberInfo {
679 template <typename T>
680 void operator() (const ArtMethod* method, const T* member_address, const std::string& name) {
681 // Check that member_address is a pointer inside *method.
682 DCHECK(reinterpret_cast<uintptr_t>(method) <= reinterpret_cast<uintptr_t>(member_address));
683 DCHECK(reinterpret_cast<uintptr_t>(member_address) + sizeof(T) <=
684 reinterpret_cast<uintptr_t>(method) + sizeof(ArtMethod));
685 size_t offset =
686 reinterpret_cast<uintptr_t>(member_address) - reinterpret_cast<uintptr_t>(method);
687 offset_to_name_size_.insert({offset, NameAndSize(sizeof(T), name)});
688 }
689
690 struct NameAndSize {
691 size_t size_;
692 std::string name_;
693 NameAndSize(size_t size, const std::string& name) : size_(size), name_(name) { }
694 NameAndSize() : size_(0), name_("INVALID") { }
695 };
696
697 std::map<size_t, NameAndSize> offset_to_name_size_;
698};
699
David Sehrb4005f02017-06-20 19:11:40 -0700700template<>
David Sehra49e0532017-08-25 08:05:29 -0700701class RegionSpecializedBase<ArtMethod> : public RegionCommon<ArtMethod> {
David Sehrb4005f02017-06-20 19:11:40 -0700702 public:
703 RegionSpecializedBase(std::ostream* os,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100704 ArrayRef<uint8_t> remote_contents,
705 ArrayRef<uint8_t> zygote_contents,
David Sehrb4005f02017-06-20 19:11:40 -0700706 const backtrace_map_t& boot_map,
David Sehra49e0532017-08-25 08:05:29 -0700707 const ImageHeader& image_header,
708 bool dump_dirty_objects ATTRIBUTE_UNUSED)
709 : RegionCommon<ArtMethod>(os, remote_contents, zygote_contents, boot_map, image_header),
710 os_(*os) {
711 // Prepare the table for offset to member lookups.
Vladimir Marko71d614f2019-04-01 15:19:40 +0100712 ArtMethod* art_method = reinterpret_cast<ArtMethod*>(&remote_contents[0]);
David Sehra49e0532017-08-25 08:05:29 -0700713 art_method->VisitMembers(member_info_);
714 // Prepare the table for address to symbolic entry point names.
715 BuildEntryPointNames();
716 class_linker_ = Runtime::Current()->GetClassLinker();
David Sehrb4005f02017-06-20 19:11:40 -0700717 }
718
David Sehra49e0532017-08-25 08:05:29 -0700719 // Define a common public type name for use by RegionData.
720 using VisitorClass = ImgArtMethodVisitor;
721
722 void VisitEntries(VisitorClass* visitor,
723 uint8_t* base,
724 PointerSize pointer_size)
David Sehrb4005f02017-06-20 19:11:40 -0700725 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier9d5956a2019-03-22 11:29:08 -0700726 RegionCommon<ArtMethod>::image_header_.VisitPackedArtMethods(*visitor, base, pointer_size);
David Sehrb4005f02017-06-20 19:11:40 -0700727 }
728
729 void VisitEntry(ArtMethod* method ATTRIBUTE_UNUSED)
730 REQUIRES_SHARED(Locks::mutator_lock_) {
731 }
732
David Sehra49e0532017-08-25 08:05:29 -0700733 void AddCleanEntry(ArtMethod* method ATTRIBUTE_UNUSED) {
734 }
735
David Sehrb4005f02017-06-20 19:11:40 -0700736 void AddFalseDirtyEntry(ArtMethod* method)
737 REQUIRES_SHARED(Locks::mutator_lock_) {
738 RegionCommon<ArtMethod>::AddFalseDirtyEntry(method);
739 }
740
David Sehrb4005f02017-06-20 19:11:40 -0700741 void AddDirtyEntry(ArtMethod* method, ArtMethod* method_remote)
742 REQUIRES_SHARED(Locks::mutator_lock_) {
743 size_t entry_size = EntrySize(method);
744 ++different_entries_;
745 dirty_entry_bytes_ += entry_size;
746 // Increment counts for the fields that are dirty
747 const uint8_t* current = reinterpret_cast<const uint8_t*>(method);
748 const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(method_remote);
749 // ArtMethods always log their dirty count and entries.
750 for (size_t i = 0; i < entry_size; ++i) {
751 if (current[i] != current_remote[i]) {
752 field_dirty_count_[i]++;
753 }
754 }
755 dirty_entries_.push_back(method);
756 }
757
David Sehra49e0532017-08-25 08:05:29 -0700758 void DiffEntryContents(ArtMethod* method,
759 uint8_t* remote_bytes,
760 const uint8_t* base_ptr,
761 bool log_dirty_objects ATTRIBUTE_UNUSED)
David Sehrb4005f02017-06-20 19:11:40 -0700762 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehra49e0532017-08-25 08:05:29 -0700763 const char* tabs = " ";
764 os_ << tabs << "ArtMethod " << ArtMethod::PrettyMethod(method) << "\n";
765
766 std::unordered_set<size_t> dirty_members;
767 // Examine the members comprising the ArtMethod, computing which members are dirty.
Andreas Gampeaad9d372018-09-18 15:58:47 -0700768 for (const std::pair<const size_t,
769 MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) {
David Sehra49e0532017-08-25 08:05:29 -0700770 const size_t offset = p.first;
771 if (memcmp(base_ptr + offset, remote_bytes + offset, p.second.size_) != 0) {
772 dirty_members.insert(p.first);
773 }
774 }
775 // Dump different fields.
776 if (!dirty_members.empty()) {
777 os_ << tabs << "Dirty members " << dirty_members.size() << "\n";
778 for (size_t offset : dirty_members) {
779 const MemberInfo::NameAndSize& member_info = member_info_.offset_to_name_size_[offset];
780 os_ << tabs << member_info.name_
781 << " original=" << StringFromBytes(base_ptr + offset, member_info.size_)
782 << " remote=" << StringFromBytes(remote_bytes + offset, member_info.size_)
783 << "\n";
784 }
785 }
786 os_ << "\n";
787 }
788
789 void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehrb4005f02017-06-20 19:11:40 -0700790 }
791
792 void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
793 DumpSamplesAndOffsetCount();
David Sehra49e0532017-08-25 08:05:29 -0700794 os_ << " offset to field map:\n";
Andreas Gampeaad9d372018-09-18 15:58:47 -0700795 for (const std::pair<const size_t,
796 MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) {
David Sehra49e0532017-08-25 08:05:29 -0700797 const size_t offset = p.first;
798 const size_t size = p.second.size_;
799 os_ << StringPrintf(" %zu-%zu: ", offset, offset + size - 1)
800 << p.second.name_
801 << std::endl;
802 }
803
David Sehrb4005f02017-06-20 19:11:40 -0700804 os_ << " field contents:\n";
805 for (ArtMethod* method : dirty_entries_) {
806 // remote method
807 auto art_method = reinterpret_cast<ArtMethod*>(method);
808 // remote class
Vladimir Markod93e3742018-07-18 10:58:13 +0100809 ObjPtr<mirror::Class> remote_declaring_class =
David Sehrb4005f02017-06-20 19:11:40 -0700810 FixUpRemotePointer(art_method->GetDeclaringClass(),
Vladimir Marko71d614f2019-04-01 15:19:40 +0100811 RegionCommon<ArtMethod>::remote_contents_,
David Sehrb4005f02017-06-20 19:11:40 -0700812 RegionCommon<ArtMethod>::boot_map_);
813 // local class
Vladimir Markod93e3742018-07-18 10:58:13 +0100814 ObjPtr<mirror::Class> declaring_class =
David Sehrb4005f02017-06-20 19:11:40 -0700815 RemoteContentsPointerToLocal(remote_declaring_class,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100816 RegionCommon<ArtMethod>::remote_contents_,
David Sehrb4005f02017-06-20 19:11:40 -0700817 RegionCommon<ArtMethod>::image_header_);
818 DumpOneArtMethod(art_method, declaring_class, remote_declaring_class);
819 }
820 }
821
822 void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehra49e0532017-08-25 08:05:29 -0700823 os_ << "\n" << " False-dirty ArtMethods\n";
David Sehrb4005f02017-06-20 19:11:40 -0700824 os_ << " field contents:\n";
825 for (ArtMethod* method : false_dirty_entries_) {
826 // local class
Vladimir Markod93e3742018-07-18 10:58:13 +0100827 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
David Sehrb4005f02017-06-20 19:11:40 -0700828 DumpOneArtMethod(method, declaring_class, nullptr);
829 }
830 }
831
832 void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
833 }
834
835 private:
836 std::ostream& os_;
David Sehra49e0532017-08-25 08:05:29 -0700837 MemberInfo member_info_;
838 std::map<const void*, std::string> entry_point_names_;
839 ClassLinker* class_linker_;
840
841 // Compute a map of addresses to names in the boot OAT file(s).
842 void BuildEntryPointNames() {
843 OatFileManager& oat_file_manager = Runtime::Current()->GetOatFileManager();
844 std::vector<const OatFile*> boot_oat_files = oat_file_manager.GetBootOatFiles();
845 for (const OatFile* oat_file : boot_oat_files) {
846 const OatHeader& oat_header = oat_file->GetOatHeader();
David Sehra49e0532017-08-25 08:05:29 -0700847 const void* jdl = oat_header.GetJniDlsymLookup();
848 if (jdl != nullptr) {
849 entry_point_names_[jdl] = "JniDlsymLookup (from boot oat file)";
850 }
851 const void* qgjt = oat_header.GetQuickGenericJniTrampoline();
852 if (qgjt != nullptr) {
853 entry_point_names_[qgjt] = "QuickGenericJniTrampoline (from boot oat file)";
854 }
855 const void* qrt = oat_header.GetQuickResolutionTrampoline();
856 if (qrt != nullptr) {
857 entry_point_names_[qrt] = "QuickResolutionTrampoline (from boot oat file)";
858 }
859 const void* qict = oat_header.GetQuickImtConflictTrampoline();
860 if (qict != nullptr) {
861 entry_point_names_[qict] = "QuickImtConflictTrampoline (from boot oat file)";
862 }
863 const void* q2ib = oat_header.GetQuickToInterpreterBridge();
864 if (q2ib != nullptr) {
865 entry_point_names_[q2ib] = "QuickToInterpreterBridge (from boot oat file)";
866 }
867 }
868 }
869
870 std::string StringFromBytes(const uint8_t* bytes, size_t size) {
871 switch (size) {
872 case 1:
873 return StringPrintf("%" PRIx8, *bytes);
874 case 2:
875 return StringPrintf("%" PRIx16, *reinterpret_cast<const uint16_t*>(bytes));
876 case 4:
877 case 8: {
878 // Compute an address if the bytes might contain one.
879 uint64_t intval;
880 if (size == 4) {
881 intval = *reinterpret_cast<const uint32_t*>(bytes);
882 } else {
883 intval = *reinterpret_cast<const uint64_t*>(bytes);
884 }
885 const void* addr = reinterpret_cast<const void*>(intval);
886 // Match the address against those that have Is* methods in the ClassLinker.
887 if (class_linker_->IsQuickToInterpreterBridge(addr)) {
888 return "QuickToInterpreterBridge";
889 } else if (class_linker_->IsQuickGenericJniStub(addr)) {
890 return "QuickGenericJniStub";
891 } else if (class_linker_->IsQuickResolutionStub(addr)) {
892 return "QuickResolutionStub";
893 } else if (class_linker_->IsJniDlsymLookupStub(addr)) {
894 return "JniDlsymLookupStub";
895 }
896 // Match the address against those that we saved from the boot OAT files.
897 if (entry_point_names_.find(addr) != entry_point_names_.end()) {
898 return entry_point_names_[addr];
899 }
900 return StringPrintf("%" PRIx64, intval);
901 }
902 default:
903 LOG(WARNING) << "Don't know how to convert " << size << " bytes to integer";
904 return "<UNKNOWN>";
905 }
906 }
David Sehrb4005f02017-06-20 19:11:40 -0700907
908 void DumpOneArtMethod(ArtMethod* art_method,
Vladimir Markod93e3742018-07-18 10:58:13 +0100909 ObjPtr<mirror::Class> declaring_class,
910 ObjPtr<mirror::Class> remote_declaring_class)
David Sehrb4005f02017-06-20 19:11:40 -0700911 REQUIRES_SHARED(Locks::mutator_lock_) {
912 PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet());
913 os_ << " " << reinterpret_cast<const void*>(art_method) << " ";
914 os_ << " entryPointFromJni: "
915 << reinterpret_cast<const void*>(art_method->GetDataPtrSize(pointer_size)) << ", ";
916 os_ << " entryPointFromQuickCompiledCode: "
917 << reinterpret_cast<const void*>(
918 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
919 << ", ";
920 os_ << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
David Sehra49e0532017-08-25 08:05:29 -0700921 // Null for runtime metionds.
922 if (declaring_class != nullptr) {
923 os_ << " class_status (local): " << declaring_class->GetStatus();
924 }
David Sehrb4005f02017-06-20 19:11:40 -0700925 if (remote_declaring_class != nullptr) {
926 os_ << ", class_status (remote): " << remote_declaring_class->GetStatus();
927 }
928 os_ << "\n";
929 }
930
931 DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase);
932};
933
934template <typename T>
935class RegionData : public RegionSpecializedBase<T> {
936 public:
937 RegionData(std::ostream* os,
Vladimir Marko71d614f2019-04-01 15:19:40 +0100938 ArrayRef<uint8_t> remote_contents,
939 ArrayRef<uint8_t> zygote_contents,
David Sehrb4005f02017-06-20 19:11:40 -0700940 const backtrace_map_t& boot_map,
Jeff Haoc23b0c02017-07-27 18:19:38 -0700941 const ImageHeader& image_header,
942 bool dump_dirty_objects)
943 : RegionSpecializedBase<T>(os,
944 remote_contents,
945 zygote_contents,
946 boot_map,
947 image_header,
948 dump_dirty_objects),
949 os_(*os) {
Vladimir Marko71d614f2019-04-01 15:19:40 +0100950 CHECK(!remote_contents.empty());
David Sehrb4005f02017-06-20 19:11:40 -0700951 }
952
953 // Walk over the type T entries in theregion between begin_image_ptr and end_image_ptr,
954 // collecting and reporting data regarding dirty, difference, etc.
955 void ProcessRegion(const MappingData& mapping_data,
956 RemoteProcesses remotes,
David Sehra49e0532017-08-25 08:05:29 -0700957 const uint8_t* begin_image_ptr)
David Sehrb4005f02017-06-20 19:11:40 -0700958 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehra49e0532017-08-25 08:05:29 -0700959 typename RegionSpecializedBase<T>::VisitorClass visitor(
960 [this](T* entry,
961 const uint8_t* begin_image_ptr,
962 const std::set<size_t>& dirty_page_set) REQUIRES_SHARED(Locks::mutator_lock_) {
963 this->ComputeEntryDirty(entry, begin_image_ptr, dirty_page_set);
964 },
965 begin_image_ptr,
966 mapping_data.dirty_page_set);
967 PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet());
968 RegionSpecializedBase<T>::VisitEntries(&visitor,
969 const_cast<uint8_t*>(begin_image_ptr),
970 pointer_size);
David Sehrb4005f02017-06-20 19:11:40 -0700971
972 // Looking at only dirty pages, figure out how many of those bytes belong to dirty entries.
973 // TODO: fix this now that there are multiple regions in a mapping.
974 float true_dirtied_percent =
975 RegionCommon<T>::GetDirtyEntryBytes() * 1.0f / (mapping_data.dirty_pages * kPageSize);
976
977 // Entry specific statistics.
978 os_ << RegionCommon<T>::GetDifferentEntryCount() << " different entries, \n "
979 << RegionCommon<T>::GetDirtyEntryBytes() << " different entry [bytes], \n "
980 << RegionCommon<T>::GetFalseDirtyEntryCount() << " false dirty entries,\n "
981 << RegionCommon<T>::GetFalseDirtyEntryBytes() << " false dirty entry [bytes], \n "
982 << true_dirtied_percent << " different entries-vs-total in a dirty page;\n "
Mathieu Chartier51e79652017-07-24 15:43:38 -0700983 << "\n";
David Sehrb4005f02017-06-20 19:11:40 -0700984
Mathieu Chartier51e79652017-07-24 15:43:38 -0700985 const uint8_t* base_ptr = begin_image_ptr;
David Sehrb4005f02017-06-20 19:11:40 -0700986 switch (remotes) {
987 case RemoteProcesses::kZygoteOnly:
988 os_ << " Zygote shared dirty entries: ";
989 break;
990 case RemoteProcesses::kImageAndZygote:
991 os_ << " Application dirty entries (private dirty): ";
Mathieu Chartier51e79652017-07-24 15:43:38 -0700992 // If we are dumping private dirty, diff against the zygote map to make it clearer what
993 // fields caused the page to be private dirty.
Vladimir Marko71d614f2019-04-01 15:19:40 +0100994 base_ptr = RegionCommon<T>::zygote_contents_.data();
David Sehrb4005f02017-06-20 19:11:40 -0700995 break;
996 case RemoteProcesses::kImageOnly:
997 os_ << " Application dirty entries (unknown whether private or shared dirty): ";
998 break;
999 }
Mathieu Chartier51e79652017-07-24 15:43:38 -07001000 DiffDirtyEntries(ProcessType::kRemote,
1001 begin_image_ptr,
1002 RegionCommon<T>::remote_contents_,
Jeff Haoc23b0c02017-07-27 18:19:38 -07001003 base_ptr,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001004 /*log_dirty_objects=*/true);
Mathieu Chartier51e79652017-07-24 15:43:38 -07001005 // Print shared dirty after since it's less important.
1006 if (RegionCommon<T>::GetZygoteDirtyEntryCount() != 0) {
1007 // We only reach this point if both pids were specified. Furthermore,
1008 // entries are only displayed here if they differed in both the image
1009 // and the zygote, so they are probably private dirty.
1010 CHECK(remotes == RemoteProcesses::kImageAndZygote);
1011 os_ << "\n" << " Zygote dirty entries (probably shared dirty): ";
1012 DiffDirtyEntries(ProcessType::kZygote,
1013 begin_image_ptr,
1014 RegionCommon<T>::zygote_contents_,
Jeff Haoc23b0c02017-07-27 18:19:38 -07001015 begin_image_ptr,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001016 /*log_dirty_objects=*/false);
Mathieu Chartier51e79652017-07-24 15:43:38 -07001017 }
Jeff Haoc23b0c02017-07-27 18:19:38 -07001018 RegionSpecializedBase<T>::DumpDirtyObjects();
David Sehrb4005f02017-06-20 19:11:40 -07001019 RegionSpecializedBase<T>::DumpDirtyEntries();
1020 RegionSpecializedBase<T>::DumpFalseDirtyEntries();
1021 RegionSpecializedBase<T>::DumpCleanEntries();
1022 }
1023
1024 private:
1025 std::ostream& os_;
1026
1027 void DiffDirtyEntries(ProcessType process_type,
1028 const uint8_t* begin_image_ptr,
Vladimir Marko71d614f2019-04-01 15:19:40 +01001029 ArrayRef<uint8_t> contents,
Jeff Haoc23b0c02017-07-27 18:19:38 -07001030 const uint8_t* base_ptr,
1031 bool log_dirty_objects)
David Sehrb4005f02017-06-20 19:11:40 -07001032 REQUIRES_SHARED(Locks::mutator_lock_) {
1033 os_ << RegionCommon<T>::dirty_entries_.size() << "\n";
1034 const std::set<T*>& entries =
1035 (process_type == ProcessType::kZygote) ?
1036 RegionCommon<T>::zygote_dirty_entries_:
1037 RegionCommon<T>::image_dirty_entries_;
1038 for (T* entry : entries) {
1039 uint8_t* entry_bytes = reinterpret_cast<uint8_t*>(entry);
1040 ptrdiff_t offset = entry_bytes - begin_image_ptr;
Vladimir Marko71d614f2019-04-01 15:19:40 +01001041 uint8_t* remote_bytes = &contents[offset];
Jeff Haoc23b0c02017-07-27 18:19:38 -07001042 RegionSpecializedBase<T>::DiffEntryContents(entry,
1043 remote_bytes,
1044 &base_ptr[offset],
1045 log_dirty_objects);
David Sehrb4005f02017-06-20 19:11:40 -07001046 }
1047 }
1048
1049 void ComputeEntryDirty(T* entry,
1050 const uint8_t* begin_image_ptr,
1051 const std::set<size_t>& dirty_pages)
1052 REQUIRES_SHARED(Locks::mutator_lock_) {
1053 // Set up pointers in the remote and the zygote for comparison.
1054 uint8_t* current = reinterpret_cast<uint8_t*>(entry);
1055 ptrdiff_t offset = current - begin_image_ptr;
1056 T* entry_remote =
Vladimir Marko71d614f2019-04-01 15:19:40 +01001057 reinterpret_cast<T*>(const_cast<uint8_t*>(&RegionCommon<T>::remote_contents_[offset]));
1058 const bool have_zygote = !RegionCommon<T>::zygote_contents_.empty();
David Sehrb4005f02017-06-20 19:11:40 -07001059 const uint8_t* current_zygote =
Vladimir Marko71d614f2019-04-01 15:19:40 +01001060 have_zygote ? &RegionCommon<T>::zygote_contents_[offset] : nullptr;
David Sehrb4005f02017-06-20 19:11:40 -07001061 T* entry_zygote = reinterpret_cast<T*>(const_cast<uint8_t*>(current_zygote));
1062 // Visit and classify entries at the current location.
1063 RegionSpecializedBase<T>::VisitEntry(entry);
Mathieu Chartier51e79652017-07-24 15:43:38 -07001064
1065 // Test private dirty first.
1066 bool is_dirty = false;
1067 if (have_zygote) {
1068 bool private_dirty = EntriesDiffer(entry_zygote, entry_remote);
1069 if (private_dirty) {
1070 // Private dirty, app vs zygote.
1071 is_dirty = true;
David Sehrb4005f02017-06-20 19:11:40 -07001072 RegionCommon<T>::AddImageDirtyEntry(entry);
David Sehrb4005f02017-06-20 19:11:40 -07001073 }
Mathieu Chartier51e79652017-07-24 15:43:38 -07001074 if (EntriesDiffer(entry_zygote, entry)) {
1075 // Shared dirty, zygote vs image.
1076 is_dirty = true;
1077 RegionCommon<T>::AddZygoteDirtyEntry(entry);
1078 }
1079 } else if (EntriesDiffer(entry_remote, entry)) {
1080 // Shared or private dirty, app vs image.
1081 is_dirty = true;
1082 RegionCommon<T>::AddImageDirtyEntry(entry);
1083 }
1084 if (is_dirty) {
1085 // TODO: Add support dirty entries in zygote and image.
1086 RegionSpecializedBase<T>::AddDirtyEntry(entry, entry_remote);
David Sehrb4005f02017-06-20 19:11:40 -07001087 } else {
1088 RegionSpecializedBase<T>::AddCleanEntry(entry);
Mathieu Chartier51e79652017-07-24 15:43:38 -07001089 if (RegionCommon<T>::IsEntryOnDirtyPage(entry, dirty_pages)) {
1090 // This entry was either never mutated or got mutated back to the same value.
1091 // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
1092 RegionSpecializedBase<T>::AddFalseDirtyEntry(entry);
1093 }
David Sehrb4005f02017-06-20 19:11:40 -07001094 }
1095 }
1096
1097 DISALLOW_COPY_AND_ASSIGN(RegionData);
1098};
1099
1100} // namespace
1101
1102
Igor Murashkin37743352014-11-13 14:38:00 -08001103class ImgDiagDumper {
1104 public:
1105 explicit ImgDiagDumper(std::ostream* os,
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001106 pid_t image_diff_pid,
Jeff Haoc23b0c02017-07-27 18:19:38 -07001107 pid_t zygote_diff_pid,
1108 bool dump_dirty_objects)
Igor Murashkin37743352014-11-13 14:38:00 -08001109 : os_(os),
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001110 image_diff_pid_(image_diff_pid),
David Sehr20e271a2017-06-14 13:02:14 -07001111 zygote_diff_pid_(zygote_diff_pid),
Jeff Haoc23b0c02017-07-27 18:19:38 -07001112 dump_dirty_objects_(dump_dirty_objects),
David Sehr20e271a2017-06-14 13:02:14 -07001113 zygote_pid_only_(false) {}
Igor Murashkin37743352014-11-13 14:38:00 -08001114
David Sehr50005a02017-06-21 13:24:21 -07001115 bool Init() {
Igor Murashkin37743352014-11-13 14:38:00 -08001116 std::ostream& os = *os_;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -07001117
David Sehr50005a02017-06-21 13:24:21 -07001118 if (image_diff_pid_ < 0 && zygote_diff_pid_ < 0) {
1119 os << "Either --image-diff-pid or --zygote-diff-pid (or both) must be specified.\n";
1120 return false;
Igor Murashkin37743352014-11-13 14:38:00 -08001121 }
1122
David Sehr50005a02017-06-21 13:24:21 -07001123 // To avoid the combinations of command-line argument use cases:
1124 // If the user invoked with only --zygote-diff-pid, shuffle that to
1125 // image_diff_pid_, invalidate zygote_diff_pid_, and remember that
1126 // image_diff_pid_ is now special.
1127 if (image_diff_pid_ < 0) {
1128 image_diff_pid_ = zygote_diff_pid_;
1129 zygote_diff_pid_ = -1;
1130 zygote_pid_only_ = true;
David Sehr45de57f2017-06-21 05:03:22 +00001131 }
Igor Murashkin37743352014-11-13 14:38:00 -08001132
David Sehr45de57f2017-06-21 05:03:22 +00001133 {
1134 struct stat sts;
1135 std::string proc_pid_str =
1136 StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int]
1137 if (stat(proc_pid_str.c_str(), &sts) == -1) {
1138 os << "Process does not exist";
1139 return false;
Igor Murashkin37743352014-11-13 14:38:00 -08001140 }
1141 }
1142
Vladimir Marko1f146b72019-03-08 16:28:08 +00001143 auto open_proc_maps = [&os](pid_t pid, /*out*/ std::unique_ptr<BacktraceMap>* proc_maps) {
1144 // Open /proc/<pid>/maps to view memory maps.
1145 proc_maps->reset(BacktraceMap::Create(pid));
1146 if (*proc_maps == nullptr) {
1147 os << "Could not read backtrace maps for " << pid;
1148 return false;
David Sehr0627be32017-06-16 13:50:02 -07001149 }
Vladimir Marko1f146b72019-03-08 16:28:08 +00001150 return true;
1151 };
1152 auto open_file = [&os] (const char* file_name, /*out*/ std::unique_ptr<File>* file) {
1153 file->reset(OS::OpenFileForReading(file_name));
1154 if (*file == nullptr) {
1155 os << "Failed to open " << file_name << " for reading";
1156 return false;
1157 }
1158 return true;
1159 };
1160 auto open_mem_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* mem_file) {
1161 // Open /proc/<pid>/mem and for reading remote contents.
1162 std::string mem_file_name =
1163 StringPrintf("/proc/%ld/mem", static_cast<long>(pid)); // NOLINT [runtime/int]
1164 return open_file(mem_file_name.c_str(), mem_file);
1165 };
1166 auto open_pagemap_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* pagemap_file) {
1167 // Open /proc/<pid>/pagemap.
1168 std::string pagemap_file_name = StringPrintf(
1169 "/proc/%ld/pagemap", static_cast<long>(pid)); // NOLINT [runtime/int]
1170 return open_file(pagemap_file_name.c_str(), pagemap_file);
1171 };
David Sehr0627be32017-06-16 13:50:02 -07001172
Vladimir Marko1f146b72019-03-08 16:28:08 +00001173 // Open files for inspecting image memory.
1174 std::unique_ptr<BacktraceMap> image_proc_maps;
1175 std::unique_ptr<File> image_mem_file;
1176 std::unique_ptr<File> image_pagemap_file;
1177 if (!open_proc_maps(image_diff_pid_, &image_proc_maps) ||
1178 !open_mem_file(image_diff_pid_, &image_mem_file) ||
1179 !open_pagemap_file(image_diff_pid_, &image_pagemap_file)) {
David Sehr50005a02017-06-21 13:24:21 -07001180 return false;
1181 }
1182
Vladimir Marko1f146b72019-03-08 16:28:08 +00001183 // If zygote_diff_pid_ != -1, open files for inspecting zygote memory.
1184 std::unique_ptr<BacktraceMap> zygote_proc_maps;
1185 std::unique_ptr<File> zygote_mem_file;
1186 std::unique_ptr<File> zygote_pagemap_file;
David Sehr50005a02017-06-21 13:24:21 -07001187 if (zygote_diff_pid_ != -1) {
Vladimir Marko1f146b72019-03-08 16:28:08 +00001188 if (!open_proc_maps(zygote_diff_pid_, &zygote_proc_maps) ||
1189 !open_mem_file(zygote_diff_pid_, &zygote_mem_file) ||
1190 !open_pagemap_file(zygote_diff_pid_, &zygote_pagemap_file)) {
David Sehr50005a02017-06-21 13:24:21 -07001191 return false;
1192 }
1193 }
1194
Vladimir Marko1f146b72019-03-08 16:28:08 +00001195 std::unique_ptr<File> clean_pagemap_file;
1196 std::unique_ptr<File> kpageflags_file;
1197 std::unique_ptr<File> kpagecount_file;
1198 if (!open_file("/proc/self/pagemap", &clean_pagemap_file) ||
1199 !open_file("/proc/kpageflags", &kpageflags_file) ||
1200 !open_file("/proc/kpagecount", &kpagecount_file)) {
David Sehr50005a02017-06-21 13:24:21 -07001201 return false;
1202 }
1203
Vladimir Markod0430bf2019-03-18 10:54:17 +00001204 // Note: the boot image is not really clean but close enough.
1205 // For now, log pages found to be dirty.
1206 // TODO: Rewrite imgdiag to load boot image without creating a runtime.
1207 // FIXME: The following does not reliably detect dirty pages.
Vladimir Marko1f146b72019-03-08 16:28:08 +00001208 Runtime* runtime = Runtime::Current();
1209 CHECK(!runtime->ShouldRelocate());
1210 size_t total_dirty_pages = 0u;
1211 for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
1212 const ImageHeader& image_header = space->GetImageHeader();
1213 const uint8_t* image_begin = image_header.GetImageBegin();
1214 const uint8_t* image_end = AlignUp(image_begin + image_header.GetImageSize(), kPageSize);
1215 size_t virtual_page_idx_begin = reinterpret_cast<uintptr_t>(image_begin) / kPageSize;
1216 size_t virtual_page_idx_end = reinterpret_cast<uintptr_t>(image_end) / kPageSize;
1217 size_t num_virtual_pages = virtual_page_idx_end - virtual_page_idx_begin;
1218
1219 std::string error_msg;
1220 std::vector<uint64_t> page_frame_numbers(num_virtual_pages);
1221 if (!GetPageFrameNumbers(clean_pagemap_file.get(),
1222 virtual_page_idx_begin,
1223 ArrayRef<uint64_t>(page_frame_numbers),
1224 &error_msg)) {
1225 os << "Failed to get page frame numbers for image space " << space->GetImageLocation()
1226 << ", error: " << error_msg;
1227 return false;
1228 }
1229
1230 std::vector<uint64_t> page_flags(num_virtual_pages);
1231 if (!GetPageFlagsOrCounts(kpageflags_file.get(),
1232 ArrayRef<const uint64_t>(page_frame_numbers),
1233 ArrayRef<uint64_t>(page_flags),
1234 &error_msg)) {
1235 os << "Failed to get page flags for image space " << space->GetImageLocation()
1236 << ", error: " << error_msg;
1237 return false;
1238 }
1239
1240 size_t num_dirty_pages = 0u;
1241 std::optional<size_t> first_dirty_page;
1242 for (size_t i = 0u, size = page_flags.size(); i != size; ++i) {
1243 if (UNLIKELY((page_flags[i] & kPageFlagsDirtyMask) != 0u)) {
1244 ++num_dirty_pages;
1245 if (!first_dirty_page.has_value()) {
1246 first_dirty_page = i;
1247 }
1248 }
1249 }
1250 if (num_dirty_pages != 0u) {
1251 DCHECK(first_dirty_page.has_value());
1252 os << "Found " << num_dirty_pages << " dirty pages for " << space->GetImageLocation()
1253 << ", first dirty page: " << first_dirty_page.value_or(0u);
1254 total_dirty_pages += num_dirty_pages;
1255 }
1256 }
David Sehr50005a02017-06-21 13:24:21 -07001257
Vladimir Marko1f146b72019-03-08 16:28:08 +00001258 // Commit the mappings and files.
1259 image_proc_maps_ = std::move(image_proc_maps);
1260 image_mem_file_ = std::move(*image_mem_file);
1261 image_pagemap_file_ = std::move(*image_pagemap_file);
1262 if (zygote_diff_pid_ != -1) {
1263 zygote_proc_maps_ = std::move(zygote_proc_maps);
1264 zygote_mem_file_ = std::move(*zygote_mem_file);
1265 zygote_pagemap_file_ = std::move(*zygote_pagemap_file);
David Sehr50005a02017-06-21 13:24:21 -07001266 }
Vladimir Marko1f146b72019-03-08 16:28:08 +00001267 clean_pagemap_file_ = std::move(*clean_pagemap_file);
1268 kpageflags_file_ = std::move(*kpageflags_file);
1269 kpagecount_file_ = std::move(*kpagecount_file);
David Sehr50005a02017-06-21 13:24:21 -07001270
1271 return true;
1272 }
1273
Vladimir Marko1f146b72019-03-08 16:28:08 +00001274 bool Dump(const ImageHeader& image_header, const std::string& image_location)
1275 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr50005a02017-06-21 13:24:21 -07001276 std::ostream& os = *os_;
Vladimir Marko1f146b72019-03-08 16:28:08 +00001277 os << "IMAGE LOCATION: " << image_location << "\n\n";
David Sehr50005a02017-06-21 13:24:21 -07001278
Vladimir Marko1f146b72019-03-08 16:28:08 +00001279 os << "MAGIC: " << image_header.GetMagic() << "\n\n";
David Sehr50005a02017-06-21 13:24:21 -07001280
Vladimir Marko1f146b72019-03-08 16:28:08 +00001281 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
David Sehr50005a02017-06-21 13:24:21 -07001282
1283 PrintPidLine("IMAGE", image_diff_pid_);
1284 os << "\n\n";
1285 PrintPidLine("ZYGOTE", zygote_diff_pid_);
1286 bool ret = true;
1287 if (image_diff_pid_ >= 0 || zygote_diff_pid_ >= 0) {
Vladimir Marko1f146b72019-03-08 16:28:08 +00001288 ret = DumpImageDiff(image_header, image_location);
David Sehr50005a02017-06-21 13:24:21 -07001289 os << "\n\n";
1290 }
1291
1292 os << std::flush;
1293
1294 return ret;
1295 }
1296
1297 private:
Vladimir Marko1f146b72019-03-08 16:28:08 +00001298 bool DumpImageDiff(const ImageHeader& image_header, const std::string& image_location)
David Sehr50005a02017-06-21 13:24:21 -07001299 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko1f146b72019-03-08 16:28:08 +00001300 return DumpImageDiffMap(image_header, image_location);
David Sehr50005a02017-06-21 13:24:21 -07001301 }
1302
Vladimir Marko1f146b72019-03-08 16:28:08 +00001303 bool ComputeDirtyBytes(const ImageHeader& image_header,
1304 const uint8_t* image_begin,
1305 const backtrace_map_t& boot_map,
Vladimir Marko71d614f2019-04-01 15:19:40 +01001306 ArrayRef<uint8_t> remote_contents,
Vladimir Marko1f146b72019-03-08 16:28:08 +00001307 MappingData* mapping_data /*out*/) {
David Sehr50005a02017-06-21 13:24:21 -07001308 std::ostream& os = *os_;
1309
1310 size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address)
1311 size_t page_idx = 0; // Page index relative to 0
1312 size_t previous_page_idx = 0; // Previous page index relative to 0
1313
1314
1315 // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
Vladimir Marko1f146b72019-03-08 16:28:08 +00001316 for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) {
1317 ptrdiff_t offset = begin - boot_map.start;
David Sehr50005a02017-06-21 13:24:21 -07001318
1319 // We treat the image header as part of the memory map for now
1320 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
1321 // But it might still be interesting to see if any of the ImageHeader data mutated
Vladimir Marko1f146b72019-03-08 16:28:08 +00001322 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset;
1323 const uint8_t* remote_ptr = &remote_contents[offset];
David Sehr50005a02017-06-21 13:24:21 -07001324
1325 if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
David Sehrb4005f02017-06-20 19:11:40 -07001326 mapping_data->different_pages++;
David Sehr50005a02017-06-21 13:24:21 -07001327
1328 // Count the number of 32-bit integers that are different.
1329 for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
Vladimir Marko1f146b72019-03-08 16:28:08 +00001330 const uint32_t* remote_ptr_int32 = reinterpret_cast<const uint32_t*>(remote_ptr);
David Sehr50005a02017-06-21 13:24:21 -07001331 const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
1332
1333 if (remote_ptr_int32[i] != local_ptr_int32[i]) {
David Sehrb4005f02017-06-20 19:11:40 -07001334 mapping_data->different_int32s++;
David Sehr50005a02017-06-21 13:24:21 -07001335 }
1336 }
1337 }
1338 }
1339
Mathieu Chartier728f8502017-07-28 17:35:30 -07001340 std::vector<size_t> private_dirty_pages_for_section(ImageHeader::kSectionCount, 0u);
1341
David Sehr50005a02017-06-21 13:24:21 -07001342 // Iterate through one byte at a time.
Vladimir Marko1f146b72019-03-08 16:28:08 +00001343 ptrdiff_t page_off_begin = image_header.GetImageBegin() - image_begin;
1344 for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) {
David Sehr50005a02017-06-21 13:24:21 -07001345 previous_page_idx = page_idx;
Vladimir Marko1f146b72019-03-08 16:28:08 +00001346 ptrdiff_t offset = begin - boot_map.start;
David Sehr50005a02017-06-21 13:24:21 -07001347
1348 // We treat the image header as part of the memory map for now
1349 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
1350 // But it might still be interesting to see if any of the ImageHeader data mutated
Vladimir Marko1f146b72019-03-08 16:28:08 +00001351 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset;
1352 const uint8_t* remote_ptr = &remote_contents[offset];
David Sehr50005a02017-06-21 13:24:21 -07001353
1354 virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
1355
1356 // Calculate the page index, relative to the 0th page where the image begins
1357 page_idx = (offset + page_off_begin) / kPageSize;
1358 if (*local_ptr != *remote_ptr) {
1359 // Track number of bytes that are different
David Sehrb4005f02017-06-20 19:11:40 -07001360 mapping_data->different_bytes++;
David Sehr50005a02017-06-21 13:24:21 -07001361 }
1362
1363 // Independently count the # of dirty pages on the remote side
1364 size_t remote_virtual_page_idx = begin / kPageSize;
1365 if (previous_page_idx != page_idx) {
1366 uint64_t page_count = 0xC0FFEE;
1367 // TODO: virtual_page_idx needs to be from the same process
1368 std::string error_msg;
Vladimir Marko1f146b72019-03-08 16:28:08 +00001369 int dirtiness = (IsPageDirty(&image_pagemap_file_, // Image-diff-pid procmap
David Sehr50005a02017-06-21 13:24:21 -07001370 &clean_pagemap_file_, // Self procmap
1371 &kpageflags_file_,
1372 &kpagecount_file_,
1373 remote_virtual_page_idx, // potentially "dirty" page
1374 virtual_page_idx, // true "clean" page
1375 &page_count,
1376 &error_msg));
1377 if (dirtiness < 0) {
1378 os << error_msg;
1379 return false;
1380 } else if (dirtiness > 0) {
David Sehrb4005f02017-06-20 19:11:40 -07001381 mapping_data->dirty_pages++;
1382 mapping_data->dirty_page_set.insert(mapping_data->dirty_page_set.end(), virtual_page_idx);
David Sehr50005a02017-06-21 13:24:21 -07001383 }
1384
1385 bool is_dirty = dirtiness > 0;
1386 bool is_private = page_count == 1;
1387
1388 if (page_count == 1) {
David Sehrb4005f02017-06-20 19:11:40 -07001389 mapping_data->private_pages++;
David Sehr50005a02017-06-21 13:24:21 -07001390 }
1391
1392 if (is_dirty && is_private) {
David Sehrb4005f02017-06-20 19:11:40 -07001393 mapping_data->private_dirty_pages++;
Mathieu Chartier728f8502017-07-28 17:35:30 -07001394 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1395 const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i);
Vladimir Marko1f146b72019-03-08 16:28:08 +00001396 if (image_header.GetImageSection(section).Contains(offset)) {
Mathieu Chartier728f8502017-07-28 17:35:30 -07001397 ++private_dirty_pages_for_section[i];
1398 }
1399 }
David Sehr50005a02017-06-21 13:24:21 -07001400 }
1401 }
1402 }
David Sehrb4005f02017-06-20 19:11:40 -07001403 mapping_data->false_dirty_pages = mapping_data->dirty_pages - mapping_data->different_pages;
1404 // Print low-level (bytes, int32s, pages) statistics.
1405 os << mapping_data->different_bytes << " differing bytes,\n "
1406 << mapping_data->different_int32s << " differing int32s,\n "
1407 << mapping_data->different_pages << " differing pages,\n "
1408 << mapping_data->dirty_pages << " pages are dirty;\n "
1409 << mapping_data->false_dirty_pages << " pages are false dirty;\n "
1410 << mapping_data->private_pages << " pages are private;\n "
Mathieu Chartier728f8502017-07-28 17:35:30 -07001411 << mapping_data->private_dirty_pages << " pages are Private_Dirty\n "
1412 << "\n";
1413
1414 size_t total_private_dirty_pages = std::accumulate(private_dirty_pages_for_section.begin(),
1415 private_dirty_pages_for_section.end(),
1416 0u);
1417 os << "Image sections (total private dirty pages " << total_private_dirty_pages << ")\n";
1418 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1419 const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i);
Vladimir Marko1f146b72019-03-08 16:28:08 +00001420 os << section << " " << image_header.GetImageSection(section)
Mathieu Chartier728f8502017-07-28 17:35:30 -07001421 << " private dirty pages=" << private_dirty_pages_for_section[i] << "\n";
1422 }
1423 os << "\n";
David Sehrb4005f02017-06-20 19:11:40 -07001424
David Sehr50005a02017-06-21 13:24:21 -07001425 return true;
1426 }
1427
David Sehr50005a02017-06-21 13:24:21 -07001428 // Look at /proc/$pid/mem and only diff the things from there
Vladimir Marko1f146b72019-03-08 16:28:08 +00001429 bool DumpImageDiffMap(const ImageHeader& image_header, const std::string& image_location)
David Sehrb4005f02017-06-20 19:11:40 -07001430 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr50005a02017-06-21 13:24:21 -07001431 std::ostream& os = *os_;
Igor Murashkin37743352014-11-13 14:38:00 -08001432 std::string error_msg;
1433
Vladimir Marko1f146b72019-03-08 16:28:08 +00001434 std::string image_location_base_name = GetImageLocationBaseName(image_location);
1435 // FIXME: BacktraceMap should provide a const_iterator so that we can take `maps` as const&.
1436 auto find_boot_map = [&os, &image_location_base_name](BacktraceMap& maps, const char* tag)
1437 -> std::optional<backtrace_map_t> {
1438 // Find the memory map for the current boot image component.
1439 for (const backtrace_map_t* map : maps) {
1440 if (EndsWith(map->name, image_location_base_name)) {
1441 if ((map->flags & PROT_WRITE) != 0) {
1442 return *map;
1443 }
1444 // In actuality there's more than 1 map, but the second one is read-only.
1445 // The one we care about is the write-able map.
1446 // The readonly maps are guaranteed to be identical, so its not interesting to compare
1447 // them.
1448 }
1449 }
1450 os << "Could not find map for " << image_location_base_name << " in " << tag;
1451 return std::nullopt;
1452 };
1453
1454 // Find the current boot image mapping.
1455 std::optional<backtrace_map_t> maybe_boot_map = find_boot_map(*image_proc_maps_, "image");
1456 if (maybe_boot_map == std::nullopt) {
1457 return false;
1458 }
1459 backtrace_map_t boot_map = maybe_boot_map.value_or(backtrace_map_t{});
1460 // Sanity check boot_map_.
1461 CHECK(boot_map.end >= boot_map.start);
1462 // The size of the boot image mapping.
1463 size_t boot_map_size = boot_map.end - boot_map.start;
1464
1465 // If zygote_diff_pid_ != -1, check that the zygote boot map is the same.
1466 if (zygote_diff_pid_ != -1) {
1467 std::optional<backtrace_map_t> maybe_zygote_boot_map =
1468 find_boot_map(*zygote_proc_maps_, "zygote");
1469 if (maybe_zygote_boot_map == std::nullopt) {
1470 return false;
1471 }
1472 backtrace_map_t zygote_boot_map = maybe_zygote_boot_map.value_or(backtrace_map_t{});
1473 if (zygote_boot_map.start != boot_map.start || zygote_boot_map.end != boot_map.end) {
1474 os << "Zygote boot map does not match image boot map: "
1475 << "zygote begin " << reinterpret_cast<const void*>(zygote_boot_map.start)
1476 << ", zygote end " << reinterpret_cast<const void*>(zygote_boot_map.end)
1477 << ", image begin " << reinterpret_cast<const void*>(boot_map.start)
1478 << ", image end " << reinterpret_cast<const void*>(boot_map.end);
1479 return false;
1480 }
1481 }
1482
Igor Murashkin37743352014-11-13 14:38:00 -08001483 // Walk the bytes and diff against our boot image
Igor Murashkin37743352014-11-13 14:38:00 -08001484 os << "\nObserving boot image header at address "
Vladimir Marko1f146b72019-03-08 16:28:08 +00001485 << reinterpret_cast<const void*>(&image_header)
Igor Murashkin37743352014-11-13 14:38:00 -08001486 << "\n\n";
1487
Vladimir Marko1f146b72019-03-08 16:28:08 +00001488 const uint8_t* image_begin_unaligned = image_header.GetImageBegin();
1489 const uint8_t* image_end_unaligned = image_begin_unaligned + image_header.GetImageSize();
Igor Murashkin37743352014-11-13 14:38:00 -08001490
1491 // Adjust range to nearest page
1492 const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
1493 const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
1494
Vladimir Marko1f146b72019-03-08 16:28:08 +00001495 size_t image_size = image_end - image_begin;
1496 if (image_size != boot_map_size) {
1497 os << "Remote boot map size does not match local boot map size: "
1498 << "local size " << image_size
1499 << ", remote size " << boot_map_size;
1500 return false;
1501 }
1502
Vladimir Marko71d614f2019-04-01 15:19:40 +01001503 auto read_contents = [&](File* mem_file,
1504 /*out*/ MemMap* map,
1505 /*out*/ ArrayRef<uint8_t>* contents) {
1506 DCHECK_ALIGNED(boot_map.start, kPageSize);
1507 DCHECK_ALIGNED(boot_map_size, kPageSize);
1508 std::string name = "Contents of " + mem_file->GetPath();
1509 std::string local_error_msg;
1510 // We need to use low 4 GiB memory so that we can walk the objects using standard
1511 // functions that use ObjPtr<> which is checking that it fits into lower 4 GiB.
1512 *map = MemMap::MapAnonymous(name.c_str(),
1513 boot_map_size,
1514 PROT_READ | PROT_WRITE,
1515 /* low_4gb= */ true,
1516 &local_error_msg);
1517 if (!map->IsValid()) {
1518 os << "Failed to allocate anonymous mapping for " << boot_map_size << " bytes.\n";
1519 return false;
1520 }
1521 if (!mem_file->PreadFully(map->Begin(), boot_map_size, boot_map.start)) {
1522 os << "Could not fully read file " << image_mem_file_.GetPath();
1523 return false;
1524 }
1525 *contents = ArrayRef<uint8_t>(map->Begin(), boot_map_size);
1526 return true;
1527 };
1528 // The contents of /proc/<image_diff_pid_>/mem.
1529 MemMap remote_contents_map;
1530 ArrayRef<uint8_t> remote_contents;
1531 if (!read_contents(&image_mem_file_, &remote_contents_map, &remote_contents)) {
Vladimir Marko1f146b72019-03-08 16:28:08 +00001532 return false;
1533 }
Vladimir Marko71d614f2019-04-01 15:19:40 +01001534 // The contents of /proc/<zygote_diff_pid_>/mem.
1535 MemMap zygote_contents_map;
1536 ArrayRef<uint8_t> zygote_contents;
Vladimir Marko1f146b72019-03-08 16:28:08 +00001537 if (zygote_diff_pid_ != -1) {
Vladimir Marko71d614f2019-04-01 15:19:40 +01001538 if (!read_contents(&zygote_mem_file_, &zygote_contents_map, &zygote_contents)) {
Vladimir Marko1f146b72019-03-08 16:28:08 +00001539 return false;
1540 }
1541 }
1542
1543 // FIXME: Because of ASLR, this check shall fail for most processes.
1544 // We need to update the entire diff to work with the ASLR. b/77856493
1545 if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start ||
1546 reinterpret_cast<uintptr_t>(image_end) < boot_map.end) {
Igor Murashkin37743352014-11-13 14:38:00 -08001547 // Sanity check that we aren't trying to read a completely different boot image
1548 os << "Remote boot map is out of range of local boot map: " <<
1549 "local begin " << reinterpret_cast<const void*>(image_begin) <<
1550 ", local end " << reinterpret_cast<const void*>(image_end) <<
Vladimir Marko1f146b72019-03-08 16:28:08 +00001551 ", remote begin " << reinterpret_cast<const void*>(boot_map.start) <<
1552 ", remote end " << reinterpret_cast<const void*>(boot_map.end);
Igor Murashkin37743352014-11-13 14:38:00 -08001553 return false;
1554 // If we wanted even more validation we could map the ImageHeader from the file
1555 }
1556
David Sehrb4005f02017-06-20 19:11:40 -07001557 MappingData mapping_data;
David Sehr45de57f2017-06-21 05:03:22 +00001558
Vladimir Marko1f146b72019-03-08 16:28:08 +00001559 os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", "
1560 << reinterpret_cast<void*>(boot_map.end) << ") had:\n ";
1561 if (!ComputeDirtyBytes(image_header, image_begin, boot_map, remote_contents, &mapping_data)) {
David Sehr50005a02017-06-21 13:24:21 -07001562 return false;
Igor Murashkin37743352014-11-13 14:38:00 -08001563 }
David Sehrb4005f02017-06-20 19:11:40 -07001564 RemoteProcesses remotes;
David Sehr20e271a2017-06-14 13:02:14 -07001565 if (zygote_pid_only_) {
David Sehrb4005f02017-06-20 19:11:40 -07001566 remotes = RemoteProcesses::kZygoteOnly;
1567 } else if (zygote_diff_pid_ > 0) {
1568 remotes = RemoteProcesses::kImageAndZygote;
David Sehr20e271a2017-06-14 13:02:14 -07001569 } else {
David Sehrb4005f02017-06-20 19:11:40 -07001570 remotes = RemoteProcesses::kImageOnly;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -07001571 }
1572
David Sehra49e0532017-08-25 08:05:29 -07001573 // Check all the mirror::Object entries in the image.
1574 RegionData<mirror::Object> object_region_data(os_,
Vladimir Marko71d614f2019-04-01 15:19:40 +01001575 remote_contents,
1576 zygote_contents,
Vladimir Marko1f146b72019-03-08 16:28:08 +00001577 boot_map,
1578 image_header,
David Sehra49e0532017-08-25 08:05:29 -07001579 dump_dirty_objects_);
David Sehrb4005f02017-06-20 19:11:40 -07001580 object_region_data.ProcessRegion(mapping_data,
1581 remotes,
David Sehra49e0532017-08-25 08:05:29 -07001582 image_begin_unaligned);
Igor Murashkin37743352014-11-13 14:38:00 -08001583
David Sehra49e0532017-08-25 08:05:29 -07001584 // Check all the ArtMethod entries in the image.
1585 RegionData<ArtMethod> artmethod_region_data(os_,
Vladimir Marko71d614f2019-04-01 15:19:40 +01001586 remote_contents,
1587 zygote_contents,
Vladimir Marko1f146b72019-03-08 16:28:08 +00001588 boot_map,
1589 image_header,
David Sehra49e0532017-08-25 08:05:29 -07001590 dump_dirty_objects_);
1591 artmethod_region_data.ProcessRegion(mapping_data,
1592 remotes,
1593 image_begin_unaligned);
Igor Murashkin37743352014-11-13 14:38:00 -08001594 return true;
1595 }
1596
Vladimir Marko1f146b72019-03-08 16:28:08 +00001597 // Note: On failure, `*page_frame_number` shall be clobbered.
Igor Murashkin37743352014-11-13 14:38:00 -08001598 static bool GetPageFrameNumber(File* page_map_file,
Vladimir Marko1f146b72019-03-08 16:28:08 +00001599 size_t virtual_page_index,
1600 /*out*/ uint64_t* page_frame_number,
1601 /*out*/ std::string* error_msg) {
Igor Murashkin37743352014-11-13 14:38:00 -08001602 CHECK(page_frame_number != nullptr);
Vladimir Marko1f146b72019-03-08 16:28:08 +00001603 return GetPageFrameNumbers(page_map_file,
1604 virtual_page_index,
1605 ArrayRef<uint64_t>(page_frame_number, 1u),
1606 error_msg);
1607 }
1608
1609 // Note: On failure, `page_frame_numbers[.]` shall be clobbered.
1610 static bool GetPageFrameNumbers(File* page_map_file,
1611 size_t virtual_page_index,
1612 /*out*/ ArrayRef<uint64_t> page_frame_numbers,
1613 /*out*/ std::string* error_msg) {
1614 CHECK(page_map_file != nullptr);
1615 CHECK_NE(page_frame_numbers.size(), 0u);
1616 CHECK(page_frame_numbers.data() != nullptr);
Igor Murashkin37743352014-11-13 14:38:00 -08001617 CHECK(error_msg != nullptr);
1618
Vladimir Marko1f146b72019-03-08 16:28:08 +00001619 // Read 64-bit entries from /proc/$pid/pagemap to get the physical page frame numbers.
1620 if (!page_map_file->PreadFully(page_frame_numbers.data(),
1621 page_frame_numbers.size() * kPageMapEntrySize,
1622 virtual_page_index * kPageMapEntrySize)) {
1623 *error_msg = StringPrintf("Failed to read the virtual page index entries from %s, error: %s",
1624 page_map_file->GetPath().c_str(),
1625 strerror(errno));
Igor Murashkin37743352014-11-13 14:38:00 -08001626 return false;
1627 }
1628
Vladimir Marko1f146b72019-03-08 16:28:08 +00001629 // Extract page frame numbers from pagemap entries.
1630 for (uint64_t& page_frame_number : page_frame_numbers) {
1631 page_frame_number &= kPageFrameNumberMask;
Igor Murashkin37743352014-11-13 14:38:00 -08001632 }
1633
Vladimir Marko1f146b72019-03-08 16:28:08 +00001634 return true;
1635 }
1636
1637 // Note: On failure, `page_flags_or_counts[.]` shall be clobbered.
1638 static bool GetPageFlagsOrCounts(File* kpage_file,
1639 ArrayRef<const uint64_t> page_frame_numbers,
1640 /*out*/ ArrayRef<uint64_t> page_flags_or_counts,
1641 /*out*/ std::string* error_msg) {
1642 static_assert(kPageFlagsEntrySize == kPageCountEntrySize, "entry size check");
1643 CHECK_NE(page_frame_numbers.size(), 0u);
1644 CHECK_EQ(page_flags_or_counts.size(), page_frame_numbers.size());
1645 CHECK(kpage_file != nullptr);
1646 CHECK(page_frame_numbers.data() != nullptr);
1647 CHECK(page_flags_or_counts.data() != nullptr);
1648 CHECK(error_msg != nullptr);
1649
1650 size_t size = page_frame_numbers.size();
1651 size_t i = 0;
1652 while (i != size) {
1653 size_t start = i;
1654 ++i;
1655 while (i != size && page_frame_numbers[i] - page_frame_numbers[start] == i - start) {
1656 ++i;
1657 }
1658 // Read 64-bit entries from /proc/kpageflags or /proc/kpagecount.
1659 if (!kpage_file->PreadFully(page_flags_or_counts.data() + start,
1660 (i - start) * kPageMapEntrySize,
1661 page_frame_numbers[start] * kPageFlagsEntrySize)) {
1662 *error_msg = StringPrintf("Failed to read the page flags or counts from %s, error: %s",
1663 kpage_file->GetPath().c_str(),
1664 strerror(errno));
1665 return false;
1666 }
1667 }
Igor Murashkin37743352014-11-13 14:38:00 -08001668
1669 return true;
1670 }
1671
1672 static int IsPageDirty(File* page_map_file,
David Sehr50005a02017-06-21 13:24:21 -07001673 File* clean_pagemap_file,
1674 File* kpageflags_file,
1675 File* kpagecount_file,
Igor Murashkin37743352014-11-13 14:38:00 -08001676 size_t virtual_page_idx,
1677 size_t clean_virtual_page_idx,
1678 // Out parameters:
1679 uint64_t* page_count, std::string* error_msg) {
1680 CHECK(page_map_file != nullptr);
David Sehr50005a02017-06-21 13:24:21 -07001681 CHECK(clean_pagemap_file != nullptr);
1682 CHECK_NE(page_map_file, clean_pagemap_file);
1683 CHECK(kpageflags_file != nullptr);
1684 CHECK(kpagecount_file != nullptr);
Igor Murashkin37743352014-11-13 14:38:00 -08001685 CHECK(page_count != nullptr);
1686 CHECK(error_msg != nullptr);
1687
1688 // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
1689
Igor Murashkin37743352014-11-13 14:38:00 -08001690 uint64_t page_frame_number = 0;
1691 if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
1692 return -1;
1693 }
1694
1695 uint64_t page_frame_number_clean = 0;
David Sehr50005a02017-06-21 13:24:21 -07001696 if (!GetPageFrameNumber(clean_pagemap_file, clean_virtual_page_idx, &page_frame_number_clean,
Igor Murashkin37743352014-11-13 14:38:00 -08001697 error_msg)) {
1698 return -1;
1699 }
1700
1701 // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
1702 uint64_t kpage_flags_entry = 0;
David Sehr50005a02017-06-21 13:24:21 -07001703 if (!kpageflags_file->PreadFully(&kpage_flags_entry,
Igor Murashkin37743352014-11-13 14:38:00 -08001704 kPageFlagsEntrySize,
1705 page_frame_number * kPageFlagsEntrySize)) {
1706 *error_msg = StringPrintf("Failed to read the page flags from %s",
David Sehr50005a02017-06-21 13:24:21 -07001707 kpageflags_file->GetPath().c_str());
Igor Murashkin37743352014-11-13 14:38:00 -08001708 return -1;
1709 }
1710
1711 // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
David Sehr50005a02017-06-21 13:24:21 -07001712 if (!kpagecount_file->PreadFully(page_count /*out*/,
Igor Murashkin37743352014-11-13 14:38:00 -08001713 kPageCountEntrySize,
1714 page_frame_number * kPageCountEntrySize)) {
1715 *error_msg = StringPrintf("Failed to read the page count from %s",
David Sehr50005a02017-06-21 13:24:21 -07001716 kpagecount_file->GetPath().c_str());
Igor Murashkin37743352014-11-13 14:38:00 -08001717 return -1;
1718 }
1719
1720 // There must be a page frame at the requested address.
1721 CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
1722 // The page frame must be memory mapped
1723 CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
1724
1725 // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
1726 bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
1727
1728 // page_frame_number_clean must come from the *same* process
1729 // but a *different* mmap than page_frame_number
1730 if (flags_dirty) {
Vladimir Markof421a902019-04-01 15:51:18 +01001731 // FIXME: This check sometimes fails and the reason is not understood. b/123852774
1732 if (page_frame_number != page_frame_number_clean) {
1733 LOG(ERROR) << "Check failed: page_frame_number != page_frame_number_clean "
1734 << "(page_frame_number=" << page_frame_number
1735 << ", page_frame_number_clean=" << page_frame_number_clean << ")"
1736 << " count: " << *page_count << " flags: 0x" << std::hex << kpage_flags_entry;
1737 }
Igor Murashkin37743352014-11-13 14:38:00 -08001738 }
1739
Andreas Gampe7c5acbb2018-09-20 13:54:52 -07001740 return (page_frame_number != page_frame_number_clean) ? 1 : 0;
Igor Murashkin37743352014-11-13 14:38:00 -08001741 }
1742
David Sehr50005a02017-06-21 13:24:21 -07001743 void PrintPidLine(const std::string& kind, pid_t pid) {
1744 if (pid < 0) {
1745 *os_ << kind << " DIFF PID: disabled\n\n";
1746 } else {
1747 *os_ << kind << " DIFF PID (" << pid << "): ";
1748 }
1749 }
1750
David Sehr50005a02017-06-21 13:24:21 -07001751 // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
1752 static std::string BaseName(const std::string& str) {
1753 size_t idx = str.rfind('/');
1754 if (idx == std::string::npos) {
1755 return str;
1756 }
1757
1758 return str.substr(idx + 1);
1759 }
1760
Igor Murashkin37743352014-11-13 14:38:00 -08001761 // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
Vladimir Marko1f146b72019-03-08 16:28:08 +00001762 static std::string GetImageLocationBaseName(const std::string& image_location) {
1763 return BaseName(std::string(image_location));
Igor Murashkin37743352014-11-13 14:38:00 -08001764 }
1765
Vladimir Marko1f146b72019-03-08 16:28:08 +00001766 static constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
1767 // bits 0-54 [in /proc/$pid/pagemap]
1768 static constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1;
1769
1770 static constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
1771 static constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
1772 static constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags
1773 static constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags
1774 static constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags
1775
1776
Igor Murashkin37743352014-11-13 14:38:00 -08001777 std::ostream* os_;
Igor Murashkin37743352014-11-13 14:38:00 -08001778 pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001779 pid_t zygote_diff_pid_; // Dump image diff against zygote boot.art if pid is non-negative
Jeff Haoc23b0c02017-07-27 18:19:38 -07001780 bool dump_dirty_objects_; // Adds dumping of objects that are dirty.
David Sehr20e271a2017-06-14 13:02:14 -07001781 bool zygote_pid_only_; // The user only specified a pid for the zygote.
Igor Murashkin37743352014-11-13 14:38:00 -08001782
David Sehr50005a02017-06-21 13:24:21 -07001783 // BacktraceMap used for finding the memory mapping of the image file.
Vladimir Marko1f146b72019-03-08 16:28:08 +00001784 std::unique_ptr<BacktraceMap> image_proc_maps_;
1785 // A File for reading /proc/<image_diff_pid_>/mem.
1786 File image_mem_file_;
1787 // A File for reading /proc/<image_diff_pid_>/pagemap.
1788 File image_pagemap_file_;
1789
1790 // BacktraceMap used for finding the memory mapping of the zygote image file.
1791 std::unique_ptr<BacktraceMap> zygote_proc_maps_;
1792 // A File for reading /proc/<zygote_diff_pid_>/mem.
1793 File zygote_mem_file_;
1794 // A File for reading /proc/<zygote_diff_pid_>/pagemap.
1795 File zygote_pagemap_file_;
1796
David Sehr50005a02017-06-21 13:24:21 -07001797 // A File for reading /proc/self/pagemap.
1798 File clean_pagemap_file_;
1799 // A File for reading /proc/kpageflags.
1800 File kpageflags_file_;
1801 // A File for reading /proc/kpagecount.
1802 File kpagecount_file_;
1803
Igor Murashkin37743352014-11-13 14:38:00 -08001804 DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
1805};
1806
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001807static int DumpImage(Runtime* runtime,
1808 std::ostream* os,
1809 pid_t image_diff_pid,
Jeff Haoc23b0c02017-07-27 18:19:38 -07001810 pid_t zygote_diff_pid,
1811 bool dump_dirty_objects) {
Igor Murashkin37743352014-11-13 14:38:00 -08001812 ScopedObjectAccess soa(Thread::Current());
1813 gc::Heap* heap = runtime->GetHeap();
Vladimir Marko1f146b72019-03-08 16:28:08 +00001814 const std::vector<gc::space::ImageSpace*>& image_spaces = heap->GetBootImageSpaces();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001815 CHECK(!image_spaces.empty());
Vladimir Marko1f146b72019-03-08 16:28:08 +00001816 ImgDiagDumper img_diag_dumper(os,
1817 image_diff_pid,
1818 zygote_diff_pid,
1819 dump_dirty_objects);
1820 if (!img_diag_dumper.Init()) {
1821 return EXIT_FAILURE;
1822 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001823 for (gc::space::ImageSpace* image_space : image_spaces) {
1824 const ImageHeader& image_header = image_space->GetImageHeader();
1825 if (!image_header.IsValid()) {
1826 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
1827 return EXIT_FAILURE;
1828 }
1829
Vladimir Marko1f146b72019-03-08 16:28:08 +00001830 if (!img_diag_dumper.Dump(image_header, image_space->GetImageLocation())) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001831 return EXIT_FAILURE;
1832 }
Igor Murashkin37743352014-11-13 14:38:00 -08001833 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001834 return EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -08001835}
1836
1837struct ImgDiagArgs : public CmdlineArgs {
1838 protected:
1839 using Base = CmdlineArgs;
1840
Vladimir Marko8581e2a2019-02-06 15:54:55 +00001841 ParseStatus ParseCustom(const char* raw_option,
1842 size_t raw_option_length,
1843 std::string* error_msg) override {
1844 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08001845 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00001846 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08001847 if (base_parse != kParseUnknownArgument) {
1848 return base_parse;
1849 }
1850 }
1851
Vladimir Marko8581e2a2019-02-06 15:54:55 +00001852 std::string_view option(raw_option, raw_option_length);
1853 if (StartsWith(option, "--image-diff-pid=")) {
1854 const char* image_diff_pid = raw_option + strlen("--image-diff-pid=");
Igor Murashkin37743352014-11-13 14:38:00 -08001855
Andreas Gampef9411702018-09-06 17:16:57 -07001856 if (!android::base::ParseInt(image_diff_pid, &image_diff_pid_)) {
Igor Murashkin37743352014-11-13 14:38:00 -08001857 *error_msg = "Image diff pid out of range";
1858 return kParseError;
1859 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00001860 } else if (StartsWith(option, "--zygote-diff-pid=")) {
1861 const char* zygote_diff_pid = raw_option + strlen("--zygote-diff-pid=");
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001862
Andreas Gampef9411702018-09-06 17:16:57 -07001863 if (!android::base::ParseInt(zygote_diff_pid, &zygote_diff_pid_)) {
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001864 *error_msg = "Zygote diff pid out of range";
1865 return kParseError;
1866 }
Jeff Haoc23b0c02017-07-27 18:19:38 -07001867 } else if (option == "--dump-dirty-objects") {
1868 dump_dirty_objects_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08001869 } else {
1870 return kParseUnknownArgument;
1871 }
1872
1873 return kParseOk;
1874 }
1875
Roland Levillainf73caca2018-08-24 17:19:07 +01001876 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08001877 // Perform the parent checks.
1878 ParseStatus parent_checks = Base::ParseChecks(error_msg);
1879 if (parent_checks != kParseOk) {
1880 return parent_checks;
1881 }
1882
1883 // Perform our own checks.
1884
1885 if (kill(image_diff_pid_,
1886 /*sig*/0) != 0) { // No signal is sent, perform error-checking only.
1887 // Check if the pid exists before proceeding.
1888 if (errno == ESRCH) {
1889 *error_msg = "Process specified does not exist";
1890 } else {
1891 *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
1892 }
1893 return kParseError;
Andreas Gampe8fae4b52017-09-27 20:04:47 -07001894 } else if (instruction_set_ != InstructionSet::kNone && instruction_set_ != kRuntimeISA) {
Igor Murashkin37743352014-11-13 14:38:00 -08001895 // Don't allow different ISAs since the images are ISA-specific.
1896 // Right now the code assumes both the runtime ISA and the remote ISA are identical.
1897 *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
1898 return kParseError;
1899 }
1900
1901 return kParseOk;
1902 }
1903
Andreas Gampefa6a1b02018-09-07 08:11:55 -07001904 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08001905 std::string usage;
1906
1907 usage +=
1908 "Usage: imgdiag [options] ...\n"
1909 " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
1910 " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
1911 "\n";
1912
1913 usage += Base::GetUsage();
1914
1915 usage += // Optional.
1916 " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
1917 " Example: --image-diff-pid=$(pid zygote)\n"
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001918 " --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff "
1919 "against.\n"
1920 " Example: --zygote-diff-pid=$(pid zygote)\n"
Jeff Haoc23b0c02017-07-27 18:19:38 -07001921 " --dump-dirty-objects: additionally output dirty objects of interest.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08001922 "\n";
1923
1924 return usage;
1925 }
1926
1927 public:
1928 pid_t image_diff_pid_ = -1;
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001929 pid_t zygote_diff_pid_ = -1;
Jeff Haoc23b0c02017-07-27 18:19:38 -07001930 bool dump_dirty_objects_ = false;
Igor Murashkin37743352014-11-13 14:38:00 -08001931};
1932
1933struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
Andreas Gampefa6a1b02018-09-07 08:11:55 -07001934 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08001935 CHECK(args_ != nullptr);
1936
1937 return DumpImage(runtime,
Igor Murashkin37743352014-11-13 14:38:00 -08001938 args_->os_,
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001939 args_->image_diff_pid_,
Jeff Haoc23b0c02017-07-27 18:19:38 -07001940 args_->zygote_diff_pid_,
1941 args_->dump_dirty_objects_) == EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -08001942 }
1943};
1944
1945} // namespace art
1946
1947int main(int argc, char** argv) {
1948 art::ImgDiagMain main;
1949 return main.Main(argc, argv);
1950}