blob: 99a438ed546c89a6766ad25d27a10b58782d2685 [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>
23#include <string>
24#include <vector>
25#include <set>
26#include <map>
Mathieu Chartiercb044bc2016-04-01 13:56:41 -070027#include <unordered_set>
Igor Murashkin37743352014-11-13 14:38:00 -080028
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
30
Andreas Gampea1d2f952017-04-20 22:53:58 -070031#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "art_method-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080033#include "base/unix_file/fd_file.h"
Igor Murashkin37743352014-11-13 14:38:00 -080034#include "gc/space/image_space.h"
35#include "gc/heap.h"
36#include "mirror/class-inl.h"
37#include "mirror/object-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080038#include "image.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080040#include "os.h"
Igor Murashkin37743352014-11-13 14:38:00 -080041
42#include "cmdline.h"
43#include "backtrace/BacktraceMap.h"
44
45#include <sys/stat.h>
46#include <sys/types.h>
47#include <signal.h>
48
49namespace art {
50
Andreas Gampe46ee31b2016-12-14 10:11:49 -080051using android::base::StringPrintf;
52
Igor Murashkin37743352014-11-13 14:38:00 -080053class ImgDiagDumper {
54 public:
55 explicit ImgDiagDumper(std::ostream* os,
Mathieu Chartiercb044bc2016-04-01 13:56:41 -070056 const ImageHeader& image_header,
57 const std::string& image_location,
Mathieu Chartierc5196cd2016-04-08 14:08:37 -070058 pid_t image_diff_pid,
59 pid_t zygote_diff_pid)
Igor Murashkin37743352014-11-13 14:38:00 -080060 : os_(os),
61 image_header_(image_header),
62 image_location_(image_location),
Mathieu Chartierc5196cd2016-04-08 14:08:37 -070063 image_diff_pid_(image_diff_pid),
David Sehr20e271a2017-06-14 13:02:14 -070064 zygote_diff_pid_(zygote_diff_pid),
65 zygote_pid_only_(false) {}
Igor Murashkin37743352014-11-13 14:38:00 -080066
David Sehr0627be32017-06-16 13:50:02 -070067 bool Init() {
68 std::ostream& os = *os_;
69
70 {
71 struct stat sts;
72 std::string proc_pid_str =
73 StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int]
74 if (stat(proc_pid_str.c_str(), &sts) == -1) {
75 os << "Process does not exist";
76 return false;
77 }
78 }
79
80 // Open /proc/$pid/maps to view memory maps
81 auto tmp_proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid_));
82 if (tmp_proc_maps == nullptr) {
83 os << "Could not read backtrace maps";
84 return false;
85 }
86
87 bool found_boot_map = false;
88 // Find the memory map only for boot.art
89 for (const backtrace_map_t& map : *tmp_proc_maps) {
90 if (EndsWith(map.name, GetImageLocationBaseName())) {
91 if ((map.flags & PROT_WRITE) != 0) {
92 boot_map_ = map;
93 found_boot_map = true;
94 break;
95 }
96 // In actuality there's more than 1 map, but the second one is read-only.
97 // The one we care about is the write-able map.
98 // The readonly maps are guaranteed to be identical, so its not interesting to compare
99 // them.
100 }
101 }
102
103 if (!found_boot_map) {
104 os << "Could not find map for " << GetImageLocationBaseName();
105 return false;
106 }
107 // Sanity check boot_map_.
108 CHECK(boot_map_.end >= boot_map_.start);
109 boot_map_size_ = boot_map_.end - boot_map_.start;
110
111 pointer_size_ = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet());
112
113 // Open /proc/<image_diff_pid_>/mem and read as remote_contents_.
114 std::string image_file_name =
115 StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int]
116 auto image_map_file = std::unique_ptr<File>(OS::OpenFileForReading(image_file_name.c_str()));
117 if (image_map_file == nullptr) {
118 os << "Failed to open " << image_file_name << " for reading";
119 return false;
120 }
121 std::vector<uint8_t> tmp_remote_contents(boot_map_size_);
122 if (!image_map_file->PreadFully(&tmp_remote_contents[0], boot_map_size_, boot_map_.start)) {
123 os << "Could not fully read file " << image_file_name;
124 return false;
125 }
126
127 // If zygote_diff_pid_ != -1, open /proc/<zygote_diff_pid_>/mem and read as zygote_contents_.
128 std::vector<uint8_t> tmp_zygote_contents;
129 if (zygote_diff_pid_ != -1) {
130 std::string zygote_file_name =
131 StringPrintf("/proc/%ld/mem", static_cast<long>(zygote_diff_pid_)); // NOLINT [runtime/int]
132 std::unique_ptr<File> zygote_map_file(OS::OpenFileForReading(zygote_file_name.c_str()));
133 if (zygote_map_file == nullptr) {
134 os << "Failed to open " << zygote_file_name << " for reading";
135 return false;
136 }
137 // The boot map should be at the same address.
138 tmp_zygote_contents.reserve(boot_map_size_);
139 if (!zygote_map_file->PreadFully(&tmp_zygote_contents[0], boot_map_size_, boot_map_.start)) {
140 LOG(WARNING) << "Could not fully read zygote file " << zygote_file_name;
141 return false;
142 }
143 }
144
145 // Open /proc/<image_diff_pid_>/pagemap.
146 std::string pagemap_file_name = StringPrintf(
147 "/proc/%ld/pagemap", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int]
148 auto tmp_pagemap_file =
149 std::unique_ptr<File>(OS::OpenFileForReading(pagemap_file_name.c_str()));
150 if (tmp_pagemap_file == nullptr) {
151 os << "Failed to open " << pagemap_file_name << " for reading: " << strerror(errno);
152 return false;
153 }
154
155 // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough
156 const char* clean_pagemap_file_name = "/proc/self/pagemap";
157 auto tmp_clean_pagemap_file = std::unique_ptr<File>(
158 OS::OpenFileForReading(clean_pagemap_file_name));
159 if (tmp_clean_pagemap_file == nullptr) {
160 os << "Failed to open " << clean_pagemap_file_name << " for reading: " << strerror(errno);
161 return false;
162 }
163
164 auto tmp_kpageflags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags"));
165 if (tmp_kpageflags_file == nullptr) {
166 os << "Failed to open /proc/kpageflags for reading: " << strerror(errno);
167 return false;
168 }
169
170 auto tmp_kpagecount_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount"));
171 if (tmp_kpagecount_file == nullptr) {
172 os << "Failed to open /proc/kpagecount for reading:" << strerror(errno);
173 return false;
174 }
175
176 // Commit the mappings, etc., to the object state.
177 proc_maps_ = std::move(tmp_proc_maps);
178 remote_contents_ = std::move(tmp_remote_contents);
179 zygote_contents_ = std::move(tmp_zygote_contents);
180 pagemap_file_ = std::move(*tmp_pagemap_file.release());
181 clean_pagemap_file_ = std::move(*tmp_clean_pagemap_file.release());
182 kpageflags_file_ = std::move(*tmp_kpageflags_file.release());
183 kpagecount_file_ = std::move(*tmp_kpagecount_file.release());
184
185 return true;
186 }
187
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700188 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -0800189 std::ostream& os = *os_;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700190 os << "IMAGE LOCATION: " << image_location_ << "\n\n";
191
Igor Murashkin37743352014-11-13 14:38:00 -0800192 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
193
194 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
195
David Sehr20e271a2017-06-14 13:02:14 -0700196 PrintPidLine("IMAGE", image_diff_pid_);
197 os << "\n\n";
198 PrintPidLine("ZYGOTE", zygote_diff_pid_);
Igor Murashkin37743352014-11-13 14:38:00 -0800199 bool ret = true;
David Sehr20e271a2017-06-14 13:02:14 -0700200 if (image_diff_pid_ >= 0 || zygote_diff_pid_ >= 0) {
201 if (image_diff_pid_ < 0) {
202 image_diff_pid_ = zygote_diff_pid_;
203 zygote_diff_pid_ = -1;
204 zygote_pid_only_ = true;
205 }
206 ret = DumpImageDiff();
Igor Murashkin37743352014-11-13 14:38:00 -0800207 os << "\n\n";
Igor Murashkin37743352014-11-13 14:38:00 -0800208 }
209
210 os << std::flush;
211
212 return ret;
213 }
214
215 private:
David Sehr20e271a2017-06-14 13:02:14 -0700216 bool DumpImageDiff()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr0627be32017-06-16 13:50:02 -0700218 return DumpImageDiffMap();
219 }
220
221 bool ComputeDirtyBytes(const uint8_t* image_begin,
222 size_t* dirty_pages /*out*/,
223 size_t* different_pages /*out*/,
224 size_t* different_bytes /*out*/,
225 size_t* different_int32s /*out*/,
226 size_t* private_pages /*out*/,
227 size_t* private_dirty_pages /*out*/,
228 std::set<size_t>* dirty_page_set_local) {
Igor Murashkin37743352014-11-13 14:38:00 -0800229 std::ostream& os = *os_;
230
David Sehr0627be32017-06-16 13:50:02 -0700231 size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address)
232 size_t page_idx = 0; // Page index relative to 0
233 size_t previous_page_idx = 0; // Previous page index relative to 0
Igor Murashkin37743352014-11-13 14:38:00 -0800234
Igor Murashkin37743352014-11-13 14:38:00 -0800235
David Sehr0627be32017-06-16 13:50:02 -0700236 // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
237 for (uintptr_t begin = boot_map_.start; begin != boot_map_.end; begin += kPageSize) {
238 ptrdiff_t offset = begin - boot_map_.start;
239
240 // We treat the image header as part of the memory map for now
241 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
242 // But it might still be interesting to see if any of the ImageHeader data mutated
243 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header_) + offset;
244 uint8_t* remote_ptr = &remote_contents_[offset];
245
246 if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
247 different_pages++;
248
249 // Count the number of 32-bit integers that are different.
250 for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
251 uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr);
252 const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
253
254 if (remote_ptr_int32[i] != local_ptr_int32[i]) {
255 different_int32s++;
256 }
Igor Murashkin37743352014-11-13 14:38:00 -0800257 }
Igor Murashkin37743352014-11-13 14:38:00 -0800258 }
259 }
260
David Sehr0627be32017-06-16 13:50:02 -0700261 // Iterate through one byte at a time.
262 ptrdiff_t page_off_begin = image_header_.GetImageBegin() - image_begin;
263 for (uintptr_t begin = boot_map_.start; begin != boot_map_.end; ++begin) {
264 previous_page_idx = page_idx;
265 ptrdiff_t offset = begin - boot_map_.start;
Igor Murashkin37743352014-11-13 14:38:00 -0800266
David Sehr0627be32017-06-16 13:50:02 -0700267 // We treat the image header as part of the memory map for now
268 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
269 // But it might still be interesting to see if any of the ImageHeader data mutated
270 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header_) + offset;
271 uint8_t* remote_ptr = &remote_contents_[offset];
272
273 virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
274
275 // Calculate the page index, relative to the 0th page where the image begins
276 page_idx = (offset + page_off_begin) / kPageSize;
277 if (*local_ptr != *remote_ptr) {
278 // Track number of bytes that are different
279 different_bytes++;
280 }
281
282 // Independently count the # of dirty pages on the remote side
283 size_t remote_virtual_page_idx = begin / kPageSize;
284 if (previous_page_idx != page_idx) {
285 uint64_t page_count = 0xC0FFEE;
286 // TODO: virtual_page_idx needs to be from the same process
287 std::string error_msg;
288 int dirtiness = (IsPageDirty(&pagemap_file_, // Image-diff-pid procmap
289 &clean_pagemap_file_, // Self procmap
290 &kpageflags_file_,
291 &kpagecount_file_,
292 remote_virtual_page_idx, // potentially "dirty" page
293 virtual_page_idx, // true "clean" page
294 &page_count,
295 &error_msg));
296 if (dirtiness < 0) {
297 os << error_msg;
298 return false;
299 } else if (dirtiness > 0) {
300 (*dirty_pages)++;
301 dirty_page_set_local->insert(dirty_page_set_local->end(), virtual_page_idx);
302 }
303
304 bool is_dirty = dirtiness > 0;
305 bool is_private = page_count == 1;
306
307 if (page_count == 1) {
308 (*private_pages)++;
309 }
310
311 if (is_dirty && is_private) {
312 (*private_dirty_pages)++;
313 }
314 }
315 }
316 return true;
317 }
318
319 bool ObjectIsOnDirtyPage(const uint8_t* item,
320 size_t size,
321 const std::set<size_t>& dirty_page_set_local) {
322 size_t page_off = 0;
323 size_t current_page_idx;
324 uintptr_t object_address = reinterpret_cast<uintptr_t>(item);
325 // Iterate every page this object belongs to
326 do {
327 current_page_idx = object_address / kPageSize + page_off;
328
329 if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) {
330 // This object is on a dirty page
331 return true;
332 }
333
334 page_off++;
335 } while ((current_page_idx * kPageSize) < RoundUp(object_address + size, kObjectAlignment));
336
337 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800338 }
339
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700340 static std::string PrettyFieldValue(ArtField* field, mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700341 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700342 std::ostringstream oss;
343 switch (field->GetTypeAsPrimitiveType()) {
344 case Primitive::kPrimNot: {
345 oss << obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
346 field->GetOffset());
347 break;
348 }
349 case Primitive::kPrimBoolean: {
350 oss << static_cast<bool>(obj->GetFieldBoolean<kVerifyNone>(field->GetOffset()));
351 break;
352 }
353 case Primitive::kPrimByte: {
354 oss << static_cast<int32_t>(obj->GetFieldByte<kVerifyNone>(field->GetOffset()));
355 break;
356 }
357 case Primitive::kPrimChar: {
358 oss << obj->GetFieldChar<kVerifyNone>(field->GetOffset());
359 break;
360 }
361 case Primitive::kPrimShort: {
362 oss << obj->GetFieldShort<kVerifyNone>(field->GetOffset());
363 break;
364 }
365 case Primitive::kPrimInt: {
366 oss << obj->GetField32<kVerifyNone>(field->GetOffset());
367 break;
368 }
369 case Primitive::kPrimLong: {
370 oss << obj->GetField64<kVerifyNone>(field->GetOffset());
371 break;
372 }
373 case Primitive::kPrimFloat: {
374 oss << obj->GetField32<kVerifyNone>(field->GetOffset());
375 break;
376 }
377 case Primitive::kPrimDouble: {
378 oss << obj->GetField64<kVerifyNone>(field->GetOffset());
379 break;
380 }
381 case Primitive::kPrimVoid: {
382 oss << "void";
383 break;
384 }
385 }
386 return oss.str();
387 }
388
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700389 // Aggregate and detail class data from an image diff.
390 struct ClassData {
David Sehr0627be32017-06-16 13:50:02 -0700391 size_t dirty_object_count = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700392
393 // Track only the byte-per-byte dirtiness (in bytes)
David Sehr0627be32017-06-16 13:50:02 -0700394 size_t dirty_object_byte_count = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700395
396 // Track the object-by-object dirtiness (in bytes)
David Sehr0627be32017-06-16 13:50:02 -0700397 size_t dirty_object_size_in_bytes = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700398
David Sehr0627be32017-06-16 13:50:02 -0700399 size_t clean_object_count = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700400
401 std::string descriptor;
402
David Sehr0627be32017-06-16 13:50:02 -0700403 size_t false_dirty_byte_count = 0;
404 size_t false_dirty_object_count = 0;
405 std::vector<const uint8_t*> false_dirty_objects;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700406
407 // Remote pointers to dirty objects
David Sehr0627be32017-06-16 13:50:02 -0700408 std::vector<const uint8_t*> dirty_objects;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700409 };
410
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700411 void DiffObjectContents(mirror::Object* obj,
412 uint8_t* remote_bytes,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700413 std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700414 const char* tabs = " ";
415 // Attempt to find fields for all dirty bytes.
416 mirror::Class* klass = obj->GetClass();
417 if (obj->IsClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700418 os << tabs << "Class " << mirror::Class::PrettyClass(obj->AsClass()) << " " << obj << "\n";
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700419 } else {
David Sehr709b0702016-10-13 09:12:37 -0700420 os << tabs << "Instance of " << mirror::Class::PrettyClass(klass) << " " << obj << "\n";
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700421 }
422
423 std::unordered_set<ArtField*> dirty_instance_fields;
424 std::unordered_set<ArtField*> dirty_static_fields;
425 const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj);
426 mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(remote_bytes);
427 for (size_t i = 0, count = obj->SizeOf(); i < count; ++i) {
428 if (obj_bytes[i] != remote_bytes[i]) {
429 ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i);
430 if (field != nullptr) {
431 dirty_instance_fields.insert(field);
432 } else if (obj->IsClass()) {
433 field = ArtField::FindStaticFieldWithOffset</*exact*/false>(obj->AsClass(), i);
434 if (field != nullptr) {
435 dirty_static_fields.insert(field);
436 }
437 }
438 if (field == nullptr) {
439 if (klass->IsArrayClass()) {
440 mirror::Class* component_type = klass->GetComponentType();
441 Primitive::Type primitive_type = component_type->GetPrimitiveType();
442 size_t component_size = Primitive::ComponentSize(primitive_type);
443 size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
444 if (i >= data_offset) {
445 os << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n";
446 // Skip to next element to prevent spam.
447 i += component_size - 1;
448 continue;
449 }
450 }
451 os << tabs << "No field for byte offset " << i << "\n";
452 }
453 }
454 }
455 // Dump different fields. TODO: Dump field contents.
456 if (!dirty_instance_fields.empty()) {
457 os << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n";
458 for (ArtField* field : dirty_instance_fields) {
David Sehr709b0702016-10-13 09:12:37 -0700459 os << tabs << ArtField::PrettyField(field)
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700460 << " original=" << PrettyFieldValue(field, obj)
461 << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
462 }
463 }
464 if (!dirty_static_fields.empty()) {
465 os << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n";
466 for (ArtField* field : dirty_static_fields) {
David Sehr709b0702016-10-13 09:12:37 -0700467 os << tabs << ArtField::PrettyField(field)
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700468 << " original=" << PrettyFieldValue(field, obj)
469 << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
470 }
471 }
472 os << "\n";
473 }
474
David Sehr0627be32017-06-16 13:50:02 -0700475 struct ObjectRegionData {
476 // Count of objects that are different.
477 size_t different_objects = 0;
Igor Murashkin37743352014-11-13 14:38:00 -0800478
David Sehr0627be32017-06-16 13:50:02 -0700479 // Local objects that are dirty (differ in at least one byte).
480 size_t dirty_object_bytes = 0;
481 std::vector<const uint8_t*>* dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800482
David Sehr0627be32017-06-16 13:50:02 -0700483 // Local objects that are clean, but located on dirty pages.
484 size_t false_dirty_object_bytes = 0;
485 std::vector<const uint8_t*> false_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800486
David Sehr0627be32017-06-16 13:50:02 -0700487 // Image dirty objects
488 // If zygote_pid_only_ == true, these are shared dirty objects in the zygote.
489 // If zygote_pid_only_ == false, these are private dirty objects in the application.
490 std::set<const uint8_t*> image_dirty_objects;
491
492 // Zygote dirty objects (probably private dirty).
493 // We only add objects here if they differed in both the image and the zygote, so
494 // they are probably private dirty.
495 std::set<const uint8_t*> zygote_dirty_objects;
496
497 std::map<off_t /* field offset */, size_t /* count */> field_dirty_count;
498 };
499
500 void ComputeObjectDirty(const uint8_t* current,
501 const uint8_t* current_remote,
502 const uint8_t* current_zygote,
503 ClassData* obj_class_data,
504 size_t obj_size,
505 const std::set<size_t>& dirty_page_set_local,
506 ObjectRegionData* region_data /*out*/) {
507 bool different_image_object = memcmp(current, current_remote, obj_size) != 0;
508 if (different_image_object) {
509 bool different_zygote_object = false;
510 if (!zygote_contents_.empty()) {
511 different_zygote_object = memcmp(current, current_zygote, obj_size) != 0;
512 }
513 if (different_zygote_object) {
514 // Different from zygote.
515 region_data->zygote_dirty_objects.insert(current);
516 } else {
517 // Just different from image.
518 region_data->image_dirty_objects.insert(current);
519 }
520
521 ++region_data->different_objects;
522 region_data->dirty_object_bytes += obj_size;
523
524 ++obj_class_data->dirty_object_count;
525
526 // Go byte-by-byte and figure out what exactly got dirtied
527 size_t dirty_byte_count_per_object = 0;
528 for (size_t i = 0; i < obj_size; ++i) {
529 if (current[i] != current_remote[i]) {
530 dirty_byte_count_per_object++;
531 }
532 }
533 obj_class_data->dirty_object_byte_count += dirty_byte_count_per_object;
534 obj_class_data->dirty_object_size_in_bytes += obj_size;
535 obj_class_data->dirty_objects.push_back(current_remote);
536 } else {
537 ++obj_class_data->clean_object_count;
Igor Murashkin37743352014-11-13 14:38:00 -0800538 }
539
David Sehr0627be32017-06-16 13:50:02 -0700540 if (different_image_object) {
541 if (region_data->dirty_objects != nullptr) {
542 // print the fields that are dirty
543 for (size_t i = 0; i < obj_size; ++i) {
544 if (current[i] != current_remote[i]) {
545 region_data->field_dirty_count[i]++;
546 }
547 }
Igor Murashkin37743352014-11-13 14:38:00 -0800548
David Sehr0627be32017-06-16 13:50:02 -0700549 region_data->dirty_objects->push_back(current);
550 }
551 /*
552 * TODO: Resurrect this stuff in the client when we add ArtMethod iterator.
553 } else {
554 std::string descriptor = GetClassDescriptor(klass);
555 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
556 // this is an ArtMethod
557 ArtMethod* art_method = reinterpret_cast<ArtMethod*>(remote_obj);
558
559 // print the fields that are dirty
560 for (size_t i = 0; i < obj_size; ++i) {
561 if (current[i] != current_remote[i]) {
562 art_method_field_dirty_count[i]++;
563 }
564 }
565
566 art_method_dirty_objects.push_back(art_method);
567 }
568 }
569 */
570 } else if (ObjectIsOnDirtyPage(current, obj_size, dirty_page_set_local)) {
571 // This object was either never mutated or got mutated back to the same value.
572 // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
573 region_data->false_dirty_objects.push_back(current);
574 obj_class_data->false_dirty_objects.push_back(current);
575 region_data->false_dirty_object_bytes += obj_size;
576 obj_class_data->false_dirty_byte_count += obj_size;
577 obj_class_data->false_dirty_object_count += 1;
578 }
579 }
580
581 // Look at /proc/$pid/mem and only diff the things from there
582 bool DumpImageDiffMap()
583 REQUIRES_SHARED(Locks::mutator_lock_) {
584 std::ostream& os = *os_;
Igor Murashkin37743352014-11-13 14:38:00 -0800585 std::string error_msg;
586
587 // Walk the bytes and diff against our boot image
Igor Murashkin37743352014-11-13 14:38:00 -0800588 os << "\nObserving boot image header at address "
David Sehr0627be32017-06-16 13:50:02 -0700589 << reinterpret_cast<const void*>(&image_header_)
Igor Murashkin37743352014-11-13 14:38:00 -0800590 << "\n\n";
591
David Sehr0627be32017-06-16 13:50:02 -0700592 const uint8_t* image_begin_unaligned = image_header_.GetImageBegin();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700593 const uint8_t* image_mirror_end_unaligned = image_begin_unaligned +
David Sehr0627be32017-06-16 13:50:02 -0700594 image_header_.GetImageSection(ImageHeader::kSectionObjects).Size();
595 const uint8_t* image_end_unaligned = image_begin_unaligned + image_header_.GetImageSize();
Igor Murashkin37743352014-11-13 14:38:00 -0800596
597 // Adjust range to nearest page
598 const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
599 const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
600
David Sehr0627be32017-06-16 13:50:02 -0700601 if (reinterpret_cast<uintptr_t>(image_begin) > boot_map_.start ||
602 reinterpret_cast<uintptr_t>(image_end) < boot_map_.end) {
Igor Murashkin37743352014-11-13 14:38:00 -0800603 // Sanity check that we aren't trying to read a completely different boot image
604 os << "Remote boot map is out of range of local boot map: " <<
605 "local begin " << reinterpret_cast<const void*>(image_begin) <<
606 ", local end " << reinterpret_cast<const void*>(image_end) <<
David Sehr0627be32017-06-16 13:50:02 -0700607 ", remote begin " << reinterpret_cast<const void*>(boot_map_.start) <<
608 ", remote end " << reinterpret_cast<const void*>(boot_map_.end);
Igor Murashkin37743352014-11-13 14:38:00 -0800609 return false;
610 // If we wanted even more validation we could map the ImageHeader from the file
611 }
612
Igor Murashkin37743352014-11-13 14:38:00 -0800613 size_t dirty_pages = 0;
David Sehr0627be32017-06-16 13:50:02 -0700614 size_t different_pages = 0;
615 size_t different_bytes = 0;
616 size_t different_int32s = 0;
Igor Murashkin37743352014-11-13 14:38:00 -0800617 size_t private_pages = 0;
618 size_t private_dirty_pages = 0;
619
David Sehr0627be32017-06-16 13:50:02 -0700620 // Set of the local virtual page indices that are dirty
621 std::set<size_t> dirty_page_set_local;
Igor Murashkin37743352014-11-13 14:38:00 -0800622
David Sehr0627be32017-06-16 13:50:02 -0700623 if (!ComputeDirtyBytes(image_begin,
624 &dirty_pages,
625 &different_pages,
626 &different_bytes,
627 &different_int32s,
628 &private_pages,
629 &private_dirty_pages,
630 &dirty_page_set_local)) {
631 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800632 }
633
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700634 std::map<mirror::Class*, ClassData> class_data;
635
Igor Murashkin37743352014-11-13 14:38:00 -0800636 // Walk each object in the remote image space and compare it against ours
Igor Murashkin37743352014-11-13 14:38:00 -0800637 std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 std::vector<ArtMethod*> art_method_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800639
David Sehr0627be32017-06-16 13:50:02 -0700640 std::map<off_t /* field offset */, size_t /* count */> class_field_dirty_count;
641 std::vector<const uint8_t*> class_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800642
Igor Murashkin37743352014-11-13 14:38:00 -0800643
Igor Murashkin37743352014-11-13 14:38:00 -0800644 // Look up remote classes by their descriptor
645 std::map<std::string, mirror::Class*> remote_class_map;
646 // Look up local classes by their descriptor
647 std::map<std::string, mirror::Class*> local_class_map;
648
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700649 const uint8_t* begin_image_ptr = image_begin_unaligned;
650 const uint8_t* end_image_ptr = image_mirror_end_unaligned;
Igor Murashkin37743352014-11-13 14:38:00 -0800651
David Sehr0627be32017-06-16 13:50:02 -0700652 ObjectRegionData region_data;
653
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700654 const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment);
655 while (reinterpret_cast<uintptr_t>(current) < reinterpret_cast<uintptr_t>(end_image_ptr)) {
656 CHECK_ALIGNED(current, kObjectAlignment);
657 mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current));
Igor Murashkin37743352014-11-13 14:38:00 -0800658
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700659 // Sanity check that we are reading a real object
660 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700661 if (kUseBakerReadBarrier) {
662 obj->AssertReadBarrierState();
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700663 }
664
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700665 mirror::Class* klass = obj->GetClass();
David Sehr0627be32017-06-16 13:50:02 -0700666 size_t obj_size = obj->SizeOf();
667 ClassData& obj_class_data = class_data[klass];
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700668
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700669 // Check against the other object and see if they are different
670 ptrdiff_t offset = current - begin_image_ptr;
David Sehr0627be32017-06-16 13:50:02 -0700671 const uint8_t* current_remote = &remote_contents_[offset];
672 const uint8_t* current_zygote =
673 zygote_contents_.empty() ? nullptr : &zygote_contents_[offset];
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700674
David Sehr0627be32017-06-16 13:50:02 -0700675 std::map<off_t /* field offset */, size_t /* count */>* field_dirty_count = nullptr;
676 if (klass->IsClassClass()) {
677 field_dirty_count = &class_field_dirty_count;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700678 }
Igor Murashkin37743352014-11-13 14:38:00 -0800679
David Sehr0627be32017-06-16 13:50:02 -0700680 ComputeObjectDirty(current,
681 current_remote,
682 current_zygote,
683 &obj_class_data,
684 obj_size,
685 dirty_page_set_local,
686 &region_data);
687
688 // Object specific stuff.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700689 std::string descriptor = GetClassDescriptor(klass);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700690 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
691 local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj);
David Sehr0627be32017-06-16 13:50:02 -0700692 mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(
693 const_cast<uint8_t*>(current_remote));
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700694 remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj);
695 }
696
697 // Unconditionally store the class descriptor in case we need it later
David Sehr0627be32017-06-16 13:50:02 -0700698 obj_class_data.descriptor = descriptor;
699
700 current += RoundUp(obj_size, kObjectAlignment);
Igor Murashkin37743352014-11-13 14:38:00 -0800701 }
702
703 // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects.
David Sehr0627be32017-06-16 13:50:02 -0700704 float true_dirtied_percent = region_data.dirty_object_bytes * 1.0f / (dirty_pages * kPageSize);
Igor Murashkin37743352014-11-13 14:38:00 -0800705 size_t false_dirty_pages = dirty_pages - different_pages;
706
David Sehr0627be32017-06-16 13:50:02 -0700707 os << "Mapping at [" << reinterpret_cast<void*>(boot_map_.start) << ", "
708 << reinterpret_cast<void*>(boot_map_.end) << ") had: \n "
Igor Murashkin37743352014-11-13 14:38:00 -0800709 << different_bytes << " differing bytes, \n "
710 << different_int32s << " differing int32s, \n "
David Sehr0627be32017-06-16 13:50:02 -0700711 << region_data.different_objects << " different objects, \n "
712 << region_data.dirty_object_bytes << " different object [bytes], \n "
713 << region_data.false_dirty_objects.size() << " false dirty objects,\n "
714 << region_data.false_dirty_object_bytes << " false dirty object [bytes], \n "
Igor Murashkin37743352014-11-13 14:38:00 -0800715 << true_dirtied_percent << " different objects-vs-total in a dirty page;\n "
716 << different_pages << " different pages; \n "
717 << dirty_pages << " pages are dirty; \n "
718 << false_dirty_pages << " pages are false dirty; \n "
719 << private_pages << " pages are private; \n "
720 << private_dirty_pages << " pages are Private_Dirty\n "
721 << "";
722
723 // vector of pairs (int count, Class*)
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700724 auto dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
725 class_data, [](const ClassData& d) { return d.dirty_object_count; });
726 auto clean_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
727 class_data, [](const ClassData& d) { return d.clean_object_count; });
Igor Murashkin37743352014-11-13 14:38:00 -0800728
David Sehr0627be32017-06-16 13:50:02 -0700729 if (!region_data.zygote_dirty_objects.empty()) {
David Sehr20e271a2017-06-14 13:02:14 -0700730 // We only reach this point if both pids were specified. Furthermore,
731 // objects are only displayed here if they differed in both the image
732 // and the zygote, so they are probably private dirty.
733 CHECK(image_diff_pid_ > 0 && zygote_diff_pid_ > 0);
734 os << "\n" << " Zygote dirty objects (probably shared dirty): "
David Sehr0627be32017-06-16 13:50:02 -0700735 << region_data.zygote_dirty_objects.size() << "\n";
736 for (const uint8_t* obj_bytes : region_data.zygote_dirty_objects) {
737 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(obj_bytes));
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700738 ptrdiff_t offset = obj_bytes - begin_image_ptr;
David Sehr0627be32017-06-16 13:50:02 -0700739 uint8_t* remote_bytes = &zygote_contents_[offset];
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700740 DiffObjectContents(obj, remote_bytes, os);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700741 }
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700742 }
David Sehr20e271a2017-06-14 13:02:14 -0700743 os << "\n";
744 if (zygote_pid_only_) {
David Sehr41d8eee2017-06-15 11:11:32 -0700745 // image_diff_pid_ is the zygote process.
746 os << " Zygote shared dirty objects: ";
David Sehr20e271a2017-06-14 13:02:14 -0700747 } else {
David Sehr41d8eee2017-06-15 11:11:32 -0700748 // image_diff_pid_ is actually the image (application) process.
749 if (zygote_diff_pid_ > 0) {
750 os << " Application dirty objects (private dirty): ";
751 } else {
752 os << " Application dirty objects (unknown whether private or shared dirty): ";
753 }
David Sehr20e271a2017-06-14 13:02:14 -0700754 }
David Sehr0627be32017-06-16 13:50:02 -0700755 os << region_data.image_dirty_objects.size() << "\n";
756 for (const uint8_t* obj_bytes : region_data.image_dirty_objects) {
757 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(obj_bytes));
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700758 ptrdiff_t offset = obj_bytes - begin_image_ptr;
David Sehr0627be32017-06-16 13:50:02 -0700759 uint8_t* remote_bytes = &remote_contents_[offset];
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700760 DiffObjectContents(obj, remote_bytes, os);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700761 }
762
Igor Murashkin37743352014-11-13 14:38:00 -0800763 os << "\n" << " Dirty object count by class:\n";
764 for (const auto& vk_pair : dirty_object_class_values) {
765 int dirty_object_count = vk_pair.first;
766 mirror::Class* klass = vk_pair.second;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700767 int object_sizes = class_data[klass].dirty_object_size_in_bytes;
768 float avg_dirty_bytes_per_class =
769 class_data[klass].dirty_object_byte_count * 1.0f / object_sizes;
Igor Murashkin37743352014-11-13 14:38:00 -0800770 float avg_object_size = object_sizes * 1.0f / dirty_object_count;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700771 const std::string& descriptor = class_data[klass].descriptor;
David Sehr709b0702016-10-13 09:12:37 -0700772 os << " " << mirror::Class::PrettyClass(klass) << " ("
Igor Murashkin37743352014-11-13 14:38:00 -0800773 << "objects: " << dirty_object_count << ", "
774 << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
775 << "avg object size: " << avg_object_size << ", "
776 << "class descriptor: '" << descriptor << "'"
777 << ")\n";
778
779 constexpr size_t kMaxAddressPrint = 5;
780 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
781 os << " sample object addresses: ";
782 for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
783 auto art_method = art_method_dirty_objects[i];
784
785 os << reinterpret_cast<void*>(art_method) << ", ";
786 }
787 os << "\n";
788
789 os << " dirty byte +offset:count list = ";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700790 auto art_method_field_dirty_count_sorted =
791 SortByValueDesc<off_t, int, int>(art_method_field_dirty_count);
Igor Murashkin37743352014-11-13 14:38:00 -0800792 for (auto pair : art_method_field_dirty_count_sorted) {
793 off_t offset = pair.second;
794 int count = pair.first;
795
796 os << "+" << offset << ":" << count << ", ";
797 }
798
799 os << "\n";
800
801 os << " field contents:\n";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700802 const auto& dirty_objects_list = class_data[klass].dirty_objects;
David Sehr0627be32017-06-16 13:50:02 -0700803 for (const uint8_t* uobj : dirty_objects_list) {
804 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(uobj));
Igor Murashkin37743352014-11-13 14:38:00 -0800805 // remote method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700806 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800807
808 // remote class
809 mirror::Class* remote_declaring_class =
David Sehr0627be32017-06-16 13:50:02 -0700810 FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents_, boot_map_);
Igor Murashkin37743352014-11-13 14:38:00 -0800811
812 // local class
813 mirror::Class* declaring_class =
David Sehr0627be32017-06-16 13:50:02 -0700814 RemoteContentsPointerToLocal(remote_declaring_class, remote_contents_, image_header_);
Igor Murashkin37743352014-11-13 14:38:00 -0800815
816 os << " " << reinterpret_cast<void*>(obj) << " ";
817 os << " entryPointFromJni: "
818 << reinterpret_cast<const void*>(
David Sehr0627be32017-06-16 13:50:02 -0700819 art_method->GetDataPtrSize(pointer_size_)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800820 os << " entryPointFromQuickCompiledCode: "
821 << reinterpret_cast<const void*>(
David Sehr0627be32017-06-16 13:50:02 -0700822 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_))
Igor Murashkin37743352014-11-13 14:38:00 -0800823 << ", ";
824 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
825 os << " class_status (local): " << declaring_class->GetStatus();
826 os << " class_status (remote): " << remote_declaring_class->GetStatus();
827 os << "\n";
828 }
829 }
830 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
831 os << " sample object addresses: ";
832 for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
833 auto class_ptr = class_dirty_objects[i];
834
David Sehr0627be32017-06-16 13:50:02 -0700835 os << reinterpret_cast<const void*>(class_ptr) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800836 }
837 os << "\n";
838
839 os << " dirty byte +offset:count list = ";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700840 auto class_field_dirty_count_sorted =
David Sehr0627be32017-06-16 13:50:02 -0700841 SortByValueDesc<off_t, int, size_t>(class_field_dirty_count);
Igor Murashkin37743352014-11-13 14:38:00 -0800842 for (auto pair : class_field_dirty_count_sorted) {
843 off_t offset = pair.second;
844 int count = pair.first;
845
846 os << "+" << offset << ":" << count << ", ";
847 }
848 os << "\n";
849
850 os << " field contents:\n";
David Sehr0627be32017-06-16 13:50:02 -0700851 // TODO: templatize this to avoid the awful casts down to uint8_t* and back.
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700852 const auto& dirty_objects_list = class_data[klass].dirty_objects;
David Sehr0627be32017-06-16 13:50:02 -0700853 for (const uint8_t* uobj : dirty_objects_list) {
854 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(uobj));
Igor Murashkin37743352014-11-13 14:38:00 -0800855 // remote class object
856 auto remote_klass = reinterpret_cast<mirror::Class*>(obj);
857
858 // local class object
859 auto local_klass = RemoteContentsPointerToLocal(remote_klass,
David Sehr0627be32017-06-16 13:50:02 -0700860 remote_contents_,
861 image_header_);
Igor Murashkin37743352014-11-13 14:38:00 -0800862
David Sehr0627be32017-06-16 13:50:02 -0700863 os << " " << reinterpret_cast<const void*>(obj) << " ";
Igor Murashkin37743352014-11-13 14:38:00 -0800864 os << " class_status (remote): " << remote_klass->GetStatus() << ", ";
865 os << " class_status (local): " << local_klass->GetStatus();
866 os << "\n";
867 }
868 }
869 }
870
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700871 auto false_dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
872 class_data, [](const ClassData& d) { return d.false_dirty_object_count; });
Igor Murashkin37743352014-11-13 14:38:00 -0800873
874 os << "\n" << " False-dirty object count by class:\n";
875 for (const auto& vk_pair : false_dirty_object_class_values) {
876 int object_count = vk_pair.first;
877 mirror::Class* klass = vk_pair.second;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700878 int object_sizes = class_data[klass].false_dirty_byte_count;
Igor Murashkin37743352014-11-13 14:38:00 -0800879 float avg_object_size = object_sizes * 1.0f / object_count;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700880 const std::string& descriptor = class_data[klass].descriptor;
David Sehr709b0702016-10-13 09:12:37 -0700881 os << " " << mirror::Class::PrettyClass(klass) << " ("
Igor Murashkin37743352014-11-13 14:38:00 -0800882 << "objects: " << object_count << ", "
883 << "avg object size: " << avg_object_size << ", "
884 << "total bytes: " << object_sizes << ", "
885 << "class descriptor: '" << descriptor << "'"
886 << ")\n";
887
888 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
David Sehr0627be32017-06-16 13:50:02 -0700889 // TODO: templatize this to avoid the awful casts down to uint8_t* and back.
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700890 auto& art_method_false_dirty_objects = class_data[klass].false_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800891
892 os << " field contents:\n";
David Sehr0627be32017-06-16 13:50:02 -0700893 for (const uint8_t* uobj : art_method_false_dirty_objects) {
894 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(uobj));
Igor Murashkin37743352014-11-13 14:38:00 -0800895 // local method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700896 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800897
898 // local class
899 mirror::Class* declaring_class = art_method->GetDeclaringClass();
900
David Sehr0627be32017-06-16 13:50:02 -0700901 os << " " << reinterpret_cast<const void*>(obj) << " ";
Igor Murashkin37743352014-11-13 14:38:00 -0800902 os << " entryPointFromJni: "
903 << reinterpret_cast<const void*>(
David Sehr0627be32017-06-16 13:50:02 -0700904 art_method->GetDataPtrSize(pointer_size_)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800905 os << " entryPointFromQuickCompiledCode: "
906 << reinterpret_cast<const void*>(
David Sehr0627be32017-06-16 13:50:02 -0700907 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_))
Igor Murashkin37743352014-11-13 14:38:00 -0800908 << ", ";
909 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
910 os << " class_status (local): " << declaring_class->GetStatus();
911 os << "\n";
912 }
913 }
914 }
915
916 os << "\n" << " Clean object count by class:\n";
917 for (const auto& vk_pair : clean_object_class_values) {
David Sehr709b0702016-10-13 09:12:37 -0700918 os << " " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
Igor Murashkin37743352014-11-13 14:38:00 -0800919 }
920
921 return true;
922 }
923
924 // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
925 // Returned pointer will point to inside of remote_contents.
926 template <typename T>
927 static T* FixUpRemotePointer(T* remote_ptr,
928 std::vector<uint8_t>& remote_contents,
929 const backtrace_map_t& boot_map) {
930 if (remote_ptr == nullptr) {
931 return nullptr;
932 }
933
934 uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr);
935
936 CHECK_LE(boot_map.start, remote);
937 CHECK_GT(boot_map.end, remote);
938
939 off_t boot_offset = remote - boot_map.start;
940
941 return reinterpret_cast<T*>(&remote_contents[boot_offset]);
942 }
943
944 template <typename T>
945 static T* RemoteContentsPointerToLocal(T* remote_ptr,
946 std::vector<uint8_t>& remote_contents,
947 const ImageHeader& image_header) {
948 if (remote_ptr == nullptr) {
949 return nullptr;
950 }
951
952 uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr);
953 ptrdiff_t boot_offset = remote - &remote_contents[0];
954
955 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
956
957 return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
958 }
959
960 static std::string GetClassDescriptor(mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700961 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -0800962 CHECK(klass != nullptr);
963
964 std::string descriptor;
965 const char* descriptor_str = klass->GetDescriptor(&descriptor);
966
967 return std::string(descriptor_str);
968 }
969
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700970 template <typename K, typename V, typename D>
971 static std::vector<std::pair<V, K>> SortByValueDesc(
972 const std::map<K, D> map,
973 std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) {
Igor Murashkin37743352014-11-13 14:38:00 -0800974 // Store value->key so that we can use the default sort from pair which
975 // sorts by value first and then key
976 std::vector<std::pair<V, K>> value_key_vector;
977
978 for (const auto& kv_pair : map) {
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700979 value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first));
Igor Murashkin37743352014-11-13 14:38:00 -0800980 }
981
982 // Sort in reverse (descending order)
983 std::sort(value_key_vector.rbegin(), value_key_vector.rend());
984 return value_key_vector;
985 }
986
987 static bool GetPageFrameNumber(File* page_map_file,
988 size_t virtual_page_index,
989 uint64_t* page_frame_number,
990 std::string* error_msg) {
991 CHECK(page_map_file != nullptr);
992 CHECK(page_frame_number != nullptr);
993 CHECK(error_msg != nullptr);
994
995 constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
996 constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; // bits 0-54 [in /proc/$pid/pagemap]
997 constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55); // bit 55 [in /proc/$pid/pagemap]
998
999 uint64_t page_map_entry = 0;
1000
1001 // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number
1002 if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize,
1003 virtual_page_index * kPageMapEntrySize)) {
1004 *error_msg = StringPrintf("Failed to read the virtual page index entry from %s",
1005 page_map_file->GetPath().c_str());
1006 return false;
1007 }
1008
1009 // TODO: seems useless, remove this.
1010 bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0;
1011 if ((false)) {
1012 LOG(VERBOSE) << soft_dirty; // Suppress unused warning
1013 UNREACHABLE();
1014 }
1015
1016 *page_frame_number = page_map_entry & kPageFrameNumberMask;
1017
1018 return true;
1019 }
1020
1021 static int IsPageDirty(File* page_map_file,
David Sehr0627be32017-06-16 13:50:02 -07001022 File* clean_pagemap_file,
1023 File* kpageflags_file,
1024 File* kpagecount_file,
Igor Murashkin37743352014-11-13 14:38:00 -08001025 size_t virtual_page_idx,
1026 size_t clean_virtual_page_idx,
1027 // Out parameters:
1028 uint64_t* page_count, std::string* error_msg) {
1029 CHECK(page_map_file != nullptr);
David Sehr0627be32017-06-16 13:50:02 -07001030 CHECK(clean_pagemap_file != nullptr);
1031 CHECK_NE(page_map_file, clean_pagemap_file);
1032 CHECK(kpageflags_file != nullptr);
1033 CHECK(kpagecount_file != nullptr);
Igor Murashkin37743352014-11-13 14:38:00 -08001034 CHECK(page_count != nullptr);
1035 CHECK(error_msg != nullptr);
1036
1037 // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
1038
1039 constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
1040 constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
1041 constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags
1042 constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags
1043 constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags
1044
1045 uint64_t page_frame_number = 0;
1046 if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
1047 return -1;
1048 }
1049
1050 uint64_t page_frame_number_clean = 0;
David Sehr0627be32017-06-16 13:50:02 -07001051 if (!GetPageFrameNumber(clean_pagemap_file, clean_virtual_page_idx, &page_frame_number_clean,
Igor Murashkin37743352014-11-13 14:38:00 -08001052 error_msg)) {
1053 return -1;
1054 }
1055
1056 // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
1057 uint64_t kpage_flags_entry = 0;
David Sehr0627be32017-06-16 13:50:02 -07001058 if (!kpageflags_file->PreadFully(&kpage_flags_entry,
Igor Murashkin37743352014-11-13 14:38:00 -08001059 kPageFlagsEntrySize,
1060 page_frame_number * kPageFlagsEntrySize)) {
1061 *error_msg = StringPrintf("Failed to read the page flags from %s",
David Sehr0627be32017-06-16 13:50:02 -07001062 kpageflags_file->GetPath().c_str());
Igor Murashkin37743352014-11-13 14:38:00 -08001063 return -1;
1064 }
1065
1066 // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
David Sehr0627be32017-06-16 13:50:02 -07001067 if (!kpagecount_file->PreadFully(page_count /*out*/,
Igor Murashkin37743352014-11-13 14:38:00 -08001068 kPageCountEntrySize,
1069 page_frame_number * kPageCountEntrySize)) {
1070 *error_msg = StringPrintf("Failed to read the page count from %s",
David Sehr0627be32017-06-16 13:50:02 -07001071 kpagecount_file->GetPath().c_str());
Igor Murashkin37743352014-11-13 14:38:00 -08001072 return -1;
1073 }
1074
1075 // There must be a page frame at the requested address.
1076 CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
1077 // The page frame must be memory mapped
1078 CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
1079
1080 // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
1081 bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
1082
1083 // page_frame_number_clean must come from the *same* process
1084 // but a *different* mmap than page_frame_number
1085 if (flags_dirty) {
1086 CHECK_NE(page_frame_number, page_frame_number_clean);
1087 }
1088
1089 return page_frame_number != page_frame_number_clean;
1090 }
1091
David Sehr0627be32017-06-16 13:50:02 -07001092 void PrintPidLine(const std::string& kind, pid_t pid) {
1093 if (pid < 0) {
1094 *os_ << kind << " DIFF PID: disabled\n\n";
1095 } else {
1096 *os_ << kind << " DIFF PID (" << pid << "): ";
1097 }
1098 }
1099
1100 static bool EndsWith(const std::string& str, const std::string& suffix) {
1101 return str.size() >= suffix.size() &&
1102 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
1103 }
1104
1105 // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
1106 static std::string BaseName(const std::string& str) {
1107 size_t idx = str.rfind('/');
1108 if (idx == std::string::npos) {
1109 return str;
1110 }
1111
1112 return str.substr(idx + 1);
1113 }
1114
Igor Murashkin37743352014-11-13 14:38:00 -08001115 // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
1116 std::string GetImageLocationBaseName() const {
1117 return BaseName(std::string(image_location_));
1118 }
1119
1120 std::ostream* os_;
1121 const ImageHeader& image_header_;
Andreas Gampe8994a042015-12-30 19:03:17 +00001122 const std::string image_location_;
Igor Murashkin37743352014-11-13 14:38:00 -08001123 pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001124 pid_t zygote_diff_pid_; // Dump image diff against zygote boot.art if pid is non-negative
David Sehr20e271a2017-06-14 13:02:14 -07001125 bool zygote_pid_only_; // The user only specified a pid for the zygote.
Igor Murashkin37743352014-11-13 14:38:00 -08001126
David Sehr0627be32017-06-16 13:50:02 -07001127 // Pointer size constant for object fields, etc.
1128 PointerSize pointer_size_;
1129 // BacktraceMap used for finding the memory mapping of the image file.
1130 std::unique_ptr<BacktraceMap> proc_maps_;
1131 // Boot image mapping.
1132 backtrace_map_t boot_map_{}; // NOLINT
1133 // The size of the boot image mapping.
1134 size_t boot_map_size_;
1135 // The contents of /proc/<image_diff_pid_>/maps.
1136 std::vector<uint8_t> remote_contents_;
1137 // The contents of /proc/<zygote_diff_pid_>/maps.
1138 std::vector<uint8_t> zygote_contents_;
1139 // A File for reading /proc/<zygote_diff_pid_>/maps.
1140 File pagemap_file_;
1141 // A File for reading /proc/self/pagemap.
1142 File clean_pagemap_file_;
1143 // A File for reading /proc/kpageflags.
1144 File kpageflags_file_;
1145 // A File for reading /proc/kpagecount.
1146 File kpagecount_file_;
1147
Igor Murashkin37743352014-11-13 14:38:00 -08001148 DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
1149};
1150
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001151static int DumpImage(Runtime* runtime,
1152 std::ostream* os,
1153 pid_t image_diff_pid,
1154 pid_t zygote_diff_pid) {
Igor Murashkin37743352014-11-13 14:38:00 -08001155 ScopedObjectAccess soa(Thread::Current());
1156 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001157 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
1158 CHECK(!image_spaces.empty());
1159 for (gc::space::ImageSpace* image_space : image_spaces) {
1160 const ImageHeader& image_header = image_space->GetImageHeader();
1161 if (!image_header.IsValid()) {
1162 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
1163 return EXIT_FAILURE;
1164 }
1165
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001166 ImgDiagDumper img_diag_dumper(os,
1167 image_header,
1168 image_space->GetImageLocation(),
1169 image_diff_pid,
1170 zygote_diff_pid);
David Sehr0627be32017-06-16 13:50:02 -07001171 if (!img_diag_dumper.Init()) {
1172 return EXIT_FAILURE;
1173 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001174 if (!img_diag_dumper.Dump()) {
1175 return EXIT_FAILURE;
1176 }
Igor Murashkin37743352014-11-13 14:38:00 -08001177 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001178 return EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -08001179}
1180
1181struct ImgDiagArgs : public CmdlineArgs {
1182 protected:
1183 using Base = CmdlineArgs;
1184
1185 virtual ParseStatus ParseCustom(const StringPiece& option,
1186 std::string* error_msg) OVERRIDE {
1187 {
1188 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
1189 if (base_parse != kParseUnknownArgument) {
1190 return base_parse;
1191 }
1192 }
1193
1194 if (option.starts_with("--image-diff-pid=")) {
1195 const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data();
1196
1197 if (!ParseInt(image_diff_pid, &image_diff_pid_)) {
1198 *error_msg = "Image diff pid out of range";
1199 return kParseError;
1200 }
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001201 } else if (option.starts_with("--zygote-diff-pid=")) {
1202 const char* zygote_diff_pid = option.substr(strlen("--zygote-diff-pid=")).data();
1203
1204 if (!ParseInt(zygote_diff_pid, &zygote_diff_pid_)) {
1205 *error_msg = "Zygote diff pid out of range";
1206 return kParseError;
1207 }
Igor Murashkin37743352014-11-13 14:38:00 -08001208 } else {
1209 return kParseUnknownArgument;
1210 }
1211
1212 return kParseOk;
1213 }
1214
1215 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
1216 // Perform the parent checks.
1217 ParseStatus parent_checks = Base::ParseChecks(error_msg);
1218 if (parent_checks != kParseOk) {
1219 return parent_checks;
1220 }
1221
1222 // Perform our own checks.
1223
1224 if (kill(image_diff_pid_,
1225 /*sig*/0) != 0) { // No signal is sent, perform error-checking only.
1226 // Check if the pid exists before proceeding.
1227 if (errno == ESRCH) {
1228 *error_msg = "Process specified does not exist";
1229 } else {
1230 *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
1231 }
1232 return kParseError;
1233 } else if (instruction_set_ != kRuntimeISA) {
1234 // Don't allow different ISAs since the images are ISA-specific.
1235 // Right now the code assumes both the runtime ISA and the remote ISA are identical.
1236 *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
1237 return kParseError;
1238 }
1239
1240 return kParseOk;
1241 }
1242
1243 virtual std::string GetUsage() const {
1244 std::string usage;
1245
1246 usage +=
1247 "Usage: imgdiag [options] ...\n"
1248 " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
1249 " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
1250 "\n";
1251
1252 usage += Base::GetUsage();
1253
1254 usage += // Optional.
1255 " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
1256 " Example: --image-diff-pid=$(pid zygote)\n"
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001257 " --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff "
1258 "against.\n"
1259 " Example: --zygote-diff-pid=$(pid zygote)\n"
Igor Murashkin37743352014-11-13 14:38:00 -08001260 "\n";
1261
1262 return usage;
1263 }
1264
1265 public:
1266 pid_t image_diff_pid_ = -1;
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001267 pid_t zygote_diff_pid_ = -1;
Igor Murashkin37743352014-11-13 14:38:00 -08001268};
1269
1270struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
1271 virtual bool ExecuteWithRuntime(Runtime* runtime) {
1272 CHECK(args_ != nullptr);
1273
1274 return DumpImage(runtime,
Igor Murashkin37743352014-11-13 14:38:00 -08001275 args_->os_,
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001276 args_->image_diff_pid_,
1277 args_->zygote_diff_pid_) == EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -08001278 }
1279};
1280
1281} // namespace art
1282
1283int main(int argc, char** argv) {
1284 art::ImgDiagMain main;
1285 return main.Main(argc, argv);
1286}