blob: d3ab838ce7e1777a241a0b14735b00b30a2cf17f [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 Sehr50005a02017-06-21 13:24:21 -070067 bool Init() {
Igor Murashkin37743352014-11-13 14:38:00 -080068 std::ostream& os = *os_;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -070069
David Sehr50005a02017-06-21 13:24:21 -070070 if (image_diff_pid_ < 0 && zygote_diff_pid_ < 0) {
71 os << "Either --image-diff-pid or --zygote-diff-pid (or both) must be specified.\n";
72 return false;
Igor Murashkin37743352014-11-13 14:38:00 -080073 }
74
David Sehr50005a02017-06-21 13:24:21 -070075 // To avoid the combinations of command-line argument use cases:
76 // If the user invoked with only --zygote-diff-pid, shuffle that to
77 // image_diff_pid_, invalidate zygote_diff_pid_, and remember that
78 // image_diff_pid_ is now special.
79 if (image_diff_pid_ < 0) {
80 image_diff_pid_ = zygote_diff_pid_;
81 zygote_diff_pid_ = -1;
82 zygote_pid_only_ = true;
David Sehr45de57f2017-06-21 05:03:22 +000083 }
Igor Murashkin37743352014-11-13 14:38:00 -080084
David Sehr45de57f2017-06-21 05:03:22 +000085 {
86 struct stat sts;
87 std::string proc_pid_str =
88 StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int]
89 if (stat(proc_pid_str.c_str(), &sts) == -1) {
90 os << "Process does not exist";
91 return false;
Igor Murashkin37743352014-11-13 14:38:00 -080092 }
93 }
94
David Sehr45de57f2017-06-21 05:03:22 +000095 // Open /proc/$pid/maps to view memory maps
David Sehr50005a02017-06-21 13:24:21 -070096 auto tmp_proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid_));
97 if (tmp_proc_maps == nullptr) {
David Sehr45de57f2017-06-21 05:03:22 +000098 os << "Could not read backtrace maps";
99 return false;
100 }
Igor Murashkin37743352014-11-13 14:38:00 -0800101
David Sehr45de57f2017-06-21 05:03:22 +0000102 bool found_boot_map = false;
David Sehr45de57f2017-06-21 05:03:22 +0000103 // Find the memory map only for boot.art
David Sehr50005a02017-06-21 13:24:21 -0700104 for (const backtrace_map_t& map : *tmp_proc_maps) {
David Sehr45de57f2017-06-21 05:03:22 +0000105 if (EndsWith(map.name, GetImageLocationBaseName())) {
106 if ((map.flags & PROT_WRITE) != 0) {
David Sehr50005a02017-06-21 13:24:21 -0700107 boot_map_ = map;
David Sehr45de57f2017-06-21 05:03:22 +0000108 found_boot_map = true;
109 break;
David Sehr0627be32017-06-16 13:50:02 -0700110 }
David Sehr45de57f2017-06-21 05:03:22 +0000111 // In actuality there's more than 1 map, but the second one is read-only.
112 // The one we care about is the write-able map.
113 // The readonly maps are guaranteed to be identical, so its not interesting to compare
114 // them.
David Sehr0627be32017-06-16 13:50:02 -0700115 }
116 }
David Sehr0627be32017-06-16 13:50:02 -0700117
David Sehr45de57f2017-06-21 05:03:22 +0000118 if (!found_boot_map) {
119 os << "Could not find map for " << GetImageLocationBaseName();
120 return false;
121 }
David Sehr50005a02017-06-21 13:24:21 -0700122 // Sanity check boot_map_.
123 CHECK(boot_map_.end >= boot_map_.start);
124 boot_map_size_ = boot_map_.end - boot_map_.start;
David Sehr0627be32017-06-16 13:50:02 -0700125
David Sehr50005a02017-06-21 13:24:21 -0700126 pointer_size_ = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet());
127
128 // Open /proc/<image_diff_pid_>/mem and read as remote_contents_.
129 std::string image_file_name =
130 StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int]
131 auto image_map_file = std::unique_ptr<File>(OS::OpenFileForReading(image_file_name.c_str()));
132 if (image_map_file == nullptr) {
133 os << "Failed to open " << image_file_name << " for reading";
134 return false;
135 }
136 std::vector<uint8_t> tmp_remote_contents(boot_map_size_);
137 if (!image_map_file->PreadFully(&tmp_remote_contents[0], boot_map_size_, boot_map_.start)) {
138 os << "Could not fully read file " << image_file_name;
139 return false;
140 }
141
142 // If zygote_diff_pid_ != -1, open /proc/<zygote_diff_pid_>/mem and read as zygote_contents_.
143 std::vector<uint8_t> tmp_zygote_contents;
144 if (zygote_diff_pid_ != -1) {
145 std::string zygote_file_name =
146 StringPrintf("/proc/%ld/mem", static_cast<long>(zygote_diff_pid_)); // NOLINT [runtime/int]
147 std::unique_ptr<File> zygote_map_file(OS::OpenFileForReading(zygote_file_name.c_str()));
148 if (zygote_map_file == nullptr) {
149 os << "Failed to open " << zygote_file_name << " for reading";
150 return false;
151 }
152 // The boot map should be at the same address.
153 tmp_zygote_contents.reserve(boot_map_size_);
154 if (!zygote_map_file->PreadFully(&tmp_zygote_contents[0], boot_map_size_, boot_map_.start)) {
155 LOG(WARNING) << "Could not fully read zygote file " << zygote_file_name;
156 return false;
157 }
158 }
159
160 // Open /proc/<image_diff_pid_>/pagemap.
161 std::string pagemap_file_name = StringPrintf(
162 "/proc/%ld/pagemap", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int]
163 auto tmp_pagemap_file =
164 std::unique_ptr<File>(OS::OpenFileForReading(pagemap_file_name.c_str()));
165 if (tmp_pagemap_file == nullptr) {
166 os << "Failed to open " << pagemap_file_name << " for reading: " << strerror(errno);
167 return false;
168 }
169
170 // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough
171 const char* clean_pagemap_file_name = "/proc/self/pagemap";
172 auto tmp_clean_pagemap_file = std::unique_ptr<File>(
173 OS::OpenFileForReading(clean_pagemap_file_name));
174 if (tmp_clean_pagemap_file == nullptr) {
175 os << "Failed to open " << clean_pagemap_file_name << " for reading: " << strerror(errno);
176 return false;
177 }
178
179 auto tmp_kpageflags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags"));
180 if (tmp_kpageflags_file == nullptr) {
181 os << "Failed to open /proc/kpageflags for reading: " << strerror(errno);
182 return false;
183 }
184
185 auto tmp_kpagecount_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount"));
186 if (tmp_kpagecount_file == nullptr) {
187 os << "Failed to open /proc/kpagecount for reading:" << strerror(errno);
188 return false;
189 }
190
191 // Commit the mappings, etc., to the object state.
192 proc_maps_ = std::move(tmp_proc_maps);
193 remote_contents_ = std::move(tmp_remote_contents);
194 zygote_contents_ = std::move(tmp_zygote_contents);
195 pagemap_file_ = std::move(*tmp_pagemap_file.release());
196 clean_pagemap_file_ = std::move(*tmp_clean_pagemap_file.release());
197 kpageflags_file_ = std::move(*tmp_kpageflags_file.release());
198 kpagecount_file_ = std::move(*tmp_kpagecount_file.release());
199
200 return true;
201 }
202
203 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
204 std::ostream& os = *os_;
205 os << "IMAGE LOCATION: " << image_location_ << "\n\n";
206
207 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
208
209 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
210
211 PrintPidLine("IMAGE", image_diff_pid_);
212 os << "\n\n";
213 PrintPidLine("ZYGOTE", zygote_diff_pid_);
214 bool ret = true;
215 if (image_diff_pid_ >= 0 || zygote_diff_pid_ >= 0) {
216 ret = DumpImageDiff();
217 os << "\n\n";
218 }
219
220 os << std::flush;
221
222 return ret;
223 }
224
225 private:
226 bool DumpImageDiff()
227 REQUIRES_SHARED(Locks::mutator_lock_) {
228 return DumpImageDiffMap();
229 }
230
231 bool ComputeDirtyBytes(const uint8_t* image_begin,
232 size_t* dirty_pages /*out*/,
233 size_t* different_pages /*out*/,
234 size_t* different_bytes /*out*/,
235 size_t* different_int32s /*out*/,
236 size_t* private_pages /*out*/,
237 size_t* private_dirty_pages /*out*/,
238 std::set<size_t>* dirty_page_set_local) {
239 std::ostream& os = *os_;
240
241 size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address)
242 size_t page_idx = 0; // Page index relative to 0
243 size_t previous_page_idx = 0; // Previous page index relative to 0
244
245
246 // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
247 for (uintptr_t begin = boot_map_.start; begin != boot_map_.end; begin += kPageSize) {
248 ptrdiff_t offset = begin - boot_map_.start;
249
250 // We treat the image header as part of the memory map for now
251 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
252 // But it might still be interesting to see if any of the ImageHeader data mutated
253 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header_) + offset;
254 uint8_t* remote_ptr = &remote_contents_[offset];
255
256 if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
257 different_pages++;
258
259 // Count the number of 32-bit integers that are different.
260 for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
261 uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr);
262 const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
263
264 if (remote_ptr_int32[i] != local_ptr_int32[i]) {
265 different_int32s++;
266 }
267 }
268 }
269 }
270
271 // Iterate through one byte at a time.
272 ptrdiff_t page_off_begin = image_header_.GetImageBegin() - image_begin;
273 for (uintptr_t begin = boot_map_.start; begin != boot_map_.end; ++begin) {
274 previous_page_idx = page_idx;
275 ptrdiff_t offset = begin - boot_map_.start;
276
277 // We treat the image header as part of the memory map for now
278 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
279 // But it might still be interesting to see if any of the ImageHeader data mutated
280 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header_) + offset;
281 uint8_t* remote_ptr = &remote_contents_[offset];
282
283 virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
284
285 // Calculate the page index, relative to the 0th page where the image begins
286 page_idx = (offset + page_off_begin) / kPageSize;
287 if (*local_ptr != *remote_ptr) {
288 // Track number of bytes that are different
289 different_bytes++;
290 }
291
292 // Independently count the # of dirty pages on the remote side
293 size_t remote_virtual_page_idx = begin / kPageSize;
294 if (previous_page_idx != page_idx) {
295 uint64_t page_count = 0xC0FFEE;
296 // TODO: virtual_page_idx needs to be from the same process
297 std::string error_msg;
298 int dirtiness = (IsPageDirty(&pagemap_file_, // Image-diff-pid procmap
299 &clean_pagemap_file_, // Self procmap
300 &kpageflags_file_,
301 &kpagecount_file_,
302 remote_virtual_page_idx, // potentially "dirty" page
303 virtual_page_idx, // true "clean" page
304 &page_count,
305 &error_msg));
306 if (dirtiness < 0) {
307 os << error_msg;
308 return false;
309 } else if (dirtiness > 0) {
310 (*dirty_pages)++;
311 dirty_page_set_local->insert(dirty_page_set_local->end(), virtual_page_idx);
312 }
313
314 bool is_dirty = dirtiness > 0;
315 bool is_private = page_count == 1;
316
317 if (page_count == 1) {
318 (*private_pages)++;
319 }
320
321 if (is_dirty && is_private) {
322 (*private_dirty_pages)++;
323 }
324 }
325 }
326 return true;
327 }
328
329 bool ObjectIsOnDirtyPage(const uint8_t* item,
330 size_t size,
331 const std::set<size_t>& dirty_page_set_local) {
332 size_t page_off = 0;
333 size_t current_page_idx;
334 uintptr_t object_address = reinterpret_cast<uintptr_t>(item);
335 // Iterate every page this object belongs to
336 do {
337 current_page_idx = object_address / kPageSize + page_off;
338
339 if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) {
340 // This object is on a dirty page
341 return true;
342 }
343
344 page_off++;
345 } while ((current_page_idx * kPageSize) < RoundUp(object_address + size, kObjectAlignment));
346
347 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800348 }
349
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700350 static std::string PrettyFieldValue(ArtField* field, mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700351 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700352 std::ostringstream oss;
353 switch (field->GetTypeAsPrimitiveType()) {
354 case Primitive::kPrimNot: {
355 oss << obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
356 field->GetOffset());
357 break;
358 }
359 case Primitive::kPrimBoolean: {
360 oss << static_cast<bool>(obj->GetFieldBoolean<kVerifyNone>(field->GetOffset()));
361 break;
362 }
363 case Primitive::kPrimByte: {
364 oss << static_cast<int32_t>(obj->GetFieldByte<kVerifyNone>(field->GetOffset()));
365 break;
366 }
367 case Primitive::kPrimChar: {
368 oss << obj->GetFieldChar<kVerifyNone>(field->GetOffset());
369 break;
370 }
371 case Primitive::kPrimShort: {
372 oss << obj->GetFieldShort<kVerifyNone>(field->GetOffset());
373 break;
374 }
375 case Primitive::kPrimInt: {
376 oss << obj->GetField32<kVerifyNone>(field->GetOffset());
377 break;
378 }
379 case Primitive::kPrimLong: {
380 oss << obj->GetField64<kVerifyNone>(field->GetOffset());
381 break;
382 }
383 case Primitive::kPrimFloat: {
384 oss << obj->GetField32<kVerifyNone>(field->GetOffset());
385 break;
386 }
387 case Primitive::kPrimDouble: {
388 oss << obj->GetField64<kVerifyNone>(field->GetOffset());
389 break;
390 }
391 case Primitive::kPrimVoid: {
392 oss << "void";
393 break;
394 }
395 }
396 return oss.str();
397 }
398
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700399 // Aggregate and detail class data from an image diff.
400 struct ClassData {
David Sehr50005a02017-06-21 13:24:21 -0700401 size_t dirty_object_count = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700402
403 // Track only the byte-per-byte dirtiness (in bytes)
David Sehr50005a02017-06-21 13:24:21 -0700404 size_t dirty_object_byte_count = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700405
406 // Track the object-by-object dirtiness (in bytes)
David Sehr50005a02017-06-21 13:24:21 -0700407 size_t dirty_object_size_in_bytes = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700408
David Sehr50005a02017-06-21 13:24:21 -0700409 size_t clean_object_count = 0;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700410
411 std::string descriptor;
412
David Sehr50005a02017-06-21 13:24:21 -0700413 size_t false_dirty_byte_count = 0;
414 size_t false_dirty_object_count = 0;
415 std::vector<const uint8_t*> false_dirty_objects;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700416
417 // Remote pointers to dirty objects
David Sehr50005a02017-06-21 13:24:21 -0700418 std::vector<const uint8_t*> dirty_objects;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700419 };
420
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700421 void DiffObjectContents(mirror::Object* obj,
422 uint8_t* remote_bytes,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700423 std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700424 const char* tabs = " ";
425 // Attempt to find fields for all dirty bytes.
426 mirror::Class* klass = obj->GetClass();
427 if (obj->IsClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700428 os << tabs << "Class " << mirror::Class::PrettyClass(obj->AsClass()) << " " << obj << "\n";
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700429 } else {
David Sehr709b0702016-10-13 09:12:37 -0700430 os << tabs << "Instance of " << mirror::Class::PrettyClass(klass) << " " << obj << "\n";
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700431 }
432
433 std::unordered_set<ArtField*> dirty_instance_fields;
434 std::unordered_set<ArtField*> dirty_static_fields;
435 const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj);
436 mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(remote_bytes);
437 for (size_t i = 0, count = obj->SizeOf(); i < count; ++i) {
438 if (obj_bytes[i] != remote_bytes[i]) {
439 ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i);
440 if (field != nullptr) {
441 dirty_instance_fields.insert(field);
442 } else if (obj->IsClass()) {
443 field = ArtField::FindStaticFieldWithOffset</*exact*/false>(obj->AsClass(), i);
444 if (field != nullptr) {
445 dirty_static_fields.insert(field);
446 }
447 }
448 if (field == nullptr) {
449 if (klass->IsArrayClass()) {
450 mirror::Class* component_type = klass->GetComponentType();
451 Primitive::Type primitive_type = component_type->GetPrimitiveType();
452 size_t component_size = Primitive::ComponentSize(primitive_type);
453 size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
454 if (i >= data_offset) {
455 os << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n";
456 // Skip to next element to prevent spam.
457 i += component_size - 1;
458 continue;
459 }
460 }
461 os << tabs << "No field for byte offset " << i << "\n";
462 }
463 }
464 }
465 // Dump different fields. TODO: Dump field contents.
466 if (!dirty_instance_fields.empty()) {
467 os << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n";
468 for (ArtField* field : dirty_instance_fields) {
David Sehr709b0702016-10-13 09:12:37 -0700469 os << tabs << ArtField::PrettyField(field)
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700470 << " original=" << PrettyFieldValue(field, obj)
471 << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
472 }
473 }
474 if (!dirty_static_fields.empty()) {
475 os << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n";
476 for (ArtField* field : dirty_static_fields) {
David Sehr709b0702016-10-13 09:12:37 -0700477 os << tabs << ArtField::PrettyField(field)
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700478 << " original=" << PrettyFieldValue(field, obj)
479 << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
480 }
481 }
482 os << "\n";
483 }
484
David Sehr50005a02017-06-21 13:24:21 -0700485 struct ObjectRegionData {
486 // Count of objects that are different.
487 size_t different_objects = 0;
David Sehr45de57f2017-06-21 05:03:22 +0000488
David Sehr50005a02017-06-21 13:24:21 -0700489 // Local objects that are dirty (differ in at least one byte).
490 size_t dirty_object_bytes = 0;
491 std::vector<const uint8_t*>* dirty_objects;
David Sehr45de57f2017-06-21 05:03:22 +0000492
David Sehr50005a02017-06-21 13:24:21 -0700493 // Local objects that are clean, but located on dirty pages.
494 size_t false_dirty_object_bytes = 0;
495 std::vector<const uint8_t*> false_dirty_objects;
David Sehr45de57f2017-06-21 05:03:22 +0000496
David Sehr50005a02017-06-21 13:24:21 -0700497 // Image dirty objects
498 // If zygote_pid_only_ == true, these are shared dirty objects in the zygote.
499 // If zygote_pid_only_ == false, these are private dirty objects in the application.
500 std::set<const uint8_t*> image_dirty_objects;
501
502 // Zygote dirty objects (probably private dirty).
503 // We only add objects here if they differed in both the image and the zygote, so
504 // they are probably private dirty.
505 std::set<const uint8_t*> zygote_dirty_objects;
506
507 std::map<off_t /* field offset */, size_t /* count */>* field_dirty_count;
508 };
509
510 void ComputeObjectDirty(const uint8_t* current,
511 const uint8_t* current_remote,
512 const uint8_t* current_zygote,
513 ClassData* obj_class_data,
514 size_t obj_size,
515 const std::set<size_t>& dirty_page_set_local,
516 ObjectRegionData* region_data /*out*/) {
517 bool different_image_object = memcmp(current, current_remote, obj_size) != 0;
518 if (different_image_object) {
519 bool different_zygote_object = false;
520 if (!zygote_contents_.empty()) {
521 different_zygote_object = memcmp(current, current_zygote, obj_size) != 0;
522 }
523 if (different_zygote_object) {
524 // Different from zygote.
525 region_data->zygote_dirty_objects.insert(current);
526 } else {
527 // Just different from image.
528 region_data->image_dirty_objects.insert(current);
529 }
530
531 ++region_data->different_objects;
532 region_data->dirty_object_bytes += obj_size;
533
534 ++obj_class_data->dirty_object_count;
535
536 // Go byte-by-byte and figure out what exactly got dirtied
537 size_t dirty_byte_count_per_object = 0;
538 for (size_t i = 0; i < obj_size; ++i) {
539 if (current[i] != current_remote[i]) {
540 dirty_byte_count_per_object++;
541 }
542 }
543 obj_class_data->dirty_object_byte_count += dirty_byte_count_per_object;
544 obj_class_data->dirty_object_size_in_bytes += obj_size;
545 obj_class_data->dirty_objects.push_back(current_remote);
546 } else {
547 ++obj_class_data->clean_object_count;
David Sehr45de57f2017-06-21 05:03:22 +0000548 }
549
David Sehr50005a02017-06-21 13:24:21 -0700550 if (different_image_object) {
551 if (region_data->dirty_objects != nullptr) {
552 // print the fields that are dirty
553 for (size_t i = 0; i < obj_size; ++i) {
554 if (current[i] != current_remote[i]) {
555 size_t dirty_count = 0;
556 if (region_data->field_dirty_count->find(i) != region_data->field_dirty_count->end()) {
557 dirty_count = (*region_data->field_dirty_count)[i];
558 }
559 (*region_data->field_dirty_count)[i] = dirty_count;
560 }
561 }
David Sehr45de57f2017-06-21 05:03:22 +0000562
David Sehr50005a02017-06-21 13:24:21 -0700563 region_data->dirty_objects->push_back(current);
564 }
565 /*
566 * TODO: Resurrect this stuff in the client when we add ArtMethod iterator.
567 } else {
568 std::string descriptor = GetClassDescriptor(klass);
569 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
570 // this is an ArtMethod
571 ArtMethod* art_method = reinterpret_cast<ArtMethod*>(remote_obj);
572
573 // print the fields that are dirty
574 for (size_t i = 0; i < obj_size; ++i) {
575 if (current[i] != current_remote[i]) {
576 art_method_field_dirty_count[i]++;
577 }
578 }
579
580 art_method_dirty_objects.push_back(art_method);
581 }
582 }
583 */
584 } else if (ObjectIsOnDirtyPage(current, obj_size, dirty_page_set_local)) {
585 // This object was either never mutated or got mutated back to the same value.
586 // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
587 region_data->false_dirty_objects.push_back(current);
588 obj_class_data->false_dirty_objects.push_back(current);
589 region_data->false_dirty_object_bytes += obj_size;
590 obj_class_data->false_dirty_byte_count += obj_size;
591 obj_class_data->false_dirty_object_count += 1;
592 }
593 }
594
595 // Look at /proc/$pid/mem and only diff the things from there
596 bool DumpImageDiffMap()
597 REQUIRES_SHARED(Locks::mutator_lock_) {
598 std::ostream& os = *os_;
Igor Murashkin37743352014-11-13 14:38:00 -0800599 std::string error_msg;
600
601 // Walk the bytes and diff against our boot image
Igor Murashkin37743352014-11-13 14:38:00 -0800602 os << "\nObserving boot image header at address "
David Sehr50005a02017-06-21 13:24:21 -0700603 << reinterpret_cast<const void*>(&image_header_)
Igor Murashkin37743352014-11-13 14:38:00 -0800604 << "\n\n";
605
David Sehr50005a02017-06-21 13:24:21 -0700606 const uint8_t* image_begin_unaligned = image_header_.GetImageBegin();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700607 const uint8_t* image_mirror_end_unaligned = image_begin_unaligned +
David Sehr50005a02017-06-21 13:24:21 -0700608 image_header_.GetImageSection(ImageHeader::kSectionObjects).Size();
609 const uint8_t* image_end_unaligned = image_begin_unaligned + image_header_.GetImageSize();
Igor Murashkin37743352014-11-13 14:38:00 -0800610
611 // Adjust range to nearest page
612 const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
613 const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
614
David Sehr50005a02017-06-21 13:24:21 -0700615 if (reinterpret_cast<uintptr_t>(image_begin) > boot_map_.start ||
616 reinterpret_cast<uintptr_t>(image_end) < boot_map_.end) {
Igor Murashkin37743352014-11-13 14:38:00 -0800617 // Sanity check that we aren't trying to read a completely different boot image
618 os << "Remote boot map is out of range of local boot map: " <<
619 "local begin " << reinterpret_cast<const void*>(image_begin) <<
620 ", local end " << reinterpret_cast<const void*>(image_end) <<
David Sehr50005a02017-06-21 13:24:21 -0700621 ", remote begin " << reinterpret_cast<const void*>(boot_map_.start) <<
622 ", remote end " << reinterpret_cast<const void*>(boot_map_.end);
Igor Murashkin37743352014-11-13 14:38:00 -0800623 return false;
624 // If we wanted even more validation we could map the ImageHeader from the file
625 }
626
David Sehr45de57f2017-06-21 05:03:22 +0000627 size_t dirty_pages = 0;
David Sehr50005a02017-06-21 13:24:21 -0700628 size_t different_pages = 0;
629 size_t different_bytes = 0;
630 size_t different_int32s = 0;
David Sehr45de57f2017-06-21 05:03:22 +0000631 size_t private_pages = 0;
632 size_t private_dirty_pages = 0;
633
David Sehr50005a02017-06-21 13:24:21 -0700634 // Set of the local virtual page indices that are dirty
635 std::set<size_t> dirty_page_set_local;
David Sehr45de57f2017-06-21 05:03:22 +0000636
David Sehr50005a02017-06-21 13:24:21 -0700637 if (!ComputeDirtyBytes(image_begin,
638 &dirty_pages,
639 &different_pages,
640 &different_bytes,
641 &different_int32s,
642 &private_pages,
643 &private_dirty_pages,
644 &dirty_page_set_local)) {
645 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800646 }
647
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700648 std::map<mirror::Class*, ClassData> class_data;
649
Igor Murashkin37743352014-11-13 14:38:00 -0800650 // Walk each object in the remote image space and compare it against ours
Igor Murashkin37743352014-11-13 14:38:00 -0800651 std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700652 std::vector<ArtMethod*> art_method_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800653
David Sehr50005a02017-06-21 13:24:21 -0700654 std::map<off_t /* field offset */, size_t /* count */> class_field_dirty_count;
655 std::vector<const uint8_t*> class_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800656
Igor Murashkin37743352014-11-13 14:38:00 -0800657
Igor Murashkin37743352014-11-13 14:38:00 -0800658 // Look up remote classes by their descriptor
659 std::map<std::string, mirror::Class*> remote_class_map;
660 // Look up local classes by their descriptor
661 std::map<std::string, mirror::Class*> local_class_map;
662
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700663 const uint8_t* begin_image_ptr = image_begin_unaligned;
664 const uint8_t* end_image_ptr = image_mirror_end_unaligned;
Igor Murashkin37743352014-11-13 14:38:00 -0800665
David Sehr50005a02017-06-21 13:24:21 -0700666 ObjectRegionData region_data;
667
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700668 const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment);
669 while (reinterpret_cast<uintptr_t>(current) < reinterpret_cast<uintptr_t>(end_image_ptr)) {
670 CHECK_ALIGNED(current, kObjectAlignment);
671 mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current));
Igor Murashkin37743352014-11-13 14:38:00 -0800672
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700673 // Sanity check that we are reading a real object
674 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700675 if (kUseBakerReadBarrier) {
676 obj->AssertReadBarrierState();
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700677 }
678
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700679 mirror::Class* klass = obj->GetClass();
David Sehr50005a02017-06-21 13:24:21 -0700680 size_t obj_size = obj->SizeOf();
681 ClassData& obj_class_data = class_data[klass];
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700682
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700683 // Check against the other object and see if they are different
684 ptrdiff_t offset = current - begin_image_ptr;
David Sehr50005a02017-06-21 13:24:21 -0700685 const uint8_t* current_remote = &remote_contents_[offset];
686 const uint8_t* current_zygote =
687 zygote_contents_.empty() ? nullptr : &zygote_contents_[offset];
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700688
David Sehr50005a02017-06-21 13:24:21 -0700689 if (klass->IsClassClass()) {
690 region_data.field_dirty_count = &class_field_dirty_count;
691 region_data.dirty_objects = &class_dirty_objects;
David Sehr45de57f2017-06-21 05:03:22 +0000692 } else {
David Sehr50005a02017-06-21 13:24:21 -0700693 region_data.field_dirty_count = nullptr;
694 region_data.dirty_objects = nullptr;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700695 }
Igor Murashkin37743352014-11-13 14:38:00 -0800696
David Sehr50005a02017-06-21 13:24:21 -0700697
698 ComputeObjectDirty(current,
699 current_remote,
700 current_zygote,
701 &obj_class_data,
702 obj_size,
703 dirty_page_set_local,
704 &region_data);
705
706 // Object specific stuff.
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700707 std::string descriptor = GetClassDescriptor(klass);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700708 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
709 local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj);
David Sehr50005a02017-06-21 13:24:21 -0700710 mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(
711 const_cast<uint8_t*>(current_remote));
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700712 remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj);
713 }
714
715 // Unconditionally store the class descriptor in case we need it later
David Sehr50005a02017-06-21 13:24:21 -0700716 obj_class_data.descriptor = descriptor;
717
718 current += RoundUp(obj_size, kObjectAlignment);
Igor Murashkin37743352014-11-13 14:38:00 -0800719 }
720
721 // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects.
David Sehr50005a02017-06-21 13:24:21 -0700722 float true_dirtied_percent = region_data.dirty_object_bytes * 1.0f / (dirty_pages * kPageSize);
Igor Murashkin37743352014-11-13 14:38:00 -0800723 size_t false_dirty_pages = dirty_pages - different_pages;
724
David Sehr50005a02017-06-21 13:24:21 -0700725 os << "Mapping at [" << reinterpret_cast<void*>(boot_map_.start) << ", "
726 << reinterpret_cast<void*>(boot_map_.end) << ") had: \n "
Igor Murashkin37743352014-11-13 14:38:00 -0800727 << different_bytes << " differing bytes, \n "
728 << different_int32s << " differing int32s, \n "
David Sehr50005a02017-06-21 13:24:21 -0700729 << region_data.different_objects << " different objects, \n "
730 << region_data.dirty_object_bytes << " different object [bytes], \n "
731 << region_data.false_dirty_objects.size() << " false dirty objects,\n "
732 << region_data.false_dirty_object_bytes << " false dirty object [bytes], \n "
Igor Murashkin37743352014-11-13 14:38:00 -0800733 << true_dirtied_percent << " different objects-vs-total in a dirty page;\n "
734 << different_pages << " different pages; \n "
735 << dirty_pages << " pages are dirty; \n "
736 << false_dirty_pages << " pages are false dirty; \n "
737 << private_pages << " pages are private; \n "
738 << private_dirty_pages << " pages are Private_Dirty\n "
739 << "";
740
741 // vector of pairs (int count, Class*)
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700742 auto dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
743 class_data, [](const ClassData& d) { return d.dirty_object_count; });
744 auto clean_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
745 class_data, [](const ClassData& d) { return d.clean_object_count; });
Igor Murashkin37743352014-11-13 14:38:00 -0800746
David Sehr50005a02017-06-21 13:24:21 -0700747 if (!region_data.zygote_dirty_objects.empty()) {
David Sehr20e271a2017-06-14 13:02:14 -0700748 // We only reach this point if both pids were specified. Furthermore,
749 // objects are only displayed here if they differed in both the image
750 // and the zygote, so they are probably private dirty.
751 CHECK(image_diff_pid_ > 0 && zygote_diff_pid_ > 0);
752 os << "\n" << " Zygote dirty objects (probably shared dirty): "
David Sehr50005a02017-06-21 13:24:21 -0700753 << region_data.zygote_dirty_objects.size() << "\n";
754 for (const uint8_t* obj_bytes : region_data.zygote_dirty_objects) {
755 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(obj_bytes));
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700756 ptrdiff_t offset = obj_bytes - begin_image_ptr;
David Sehr50005a02017-06-21 13:24:21 -0700757 uint8_t* remote_bytes = &zygote_contents_[offset];
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700758 DiffObjectContents(obj, remote_bytes, os);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700759 }
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700760 }
David Sehr20e271a2017-06-14 13:02:14 -0700761 os << "\n";
762 if (zygote_pid_only_) {
David Sehr41d8eee2017-06-15 11:11:32 -0700763 // image_diff_pid_ is the zygote process.
764 os << " Zygote shared dirty objects: ";
David Sehr20e271a2017-06-14 13:02:14 -0700765 } else {
David Sehr41d8eee2017-06-15 11:11:32 -0700766 // image_diff_pid_ is actually the image (application) process.
767 if (zygote_diff_pid_ > 0) {
768 os << " Application dirty objects (private dirty): ";
769 } else {
770 os << " Application dirty objects (unknown whether private or shared dirty): ";
771 }
David Sehr20e271a2017-06-14 13:02:14 -0700772 }
David Sehr50005a02017-06-21 13:24:21 -0700773 os << region_data.image_dirty_objects.size() << "\n";
774 for (const uint8_t* obj_bytes : region_data.image_dirty_objects) {
775 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(obj_bytes));
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700776 ptrdiff_t offset = obj_bytes - begin_image_ptr;
David Sehr50005a02017-06-21 13:24:21 -0700777 uint8_t* remote_bytes = &remote_contents_[offset];
Mathieu Chartierc5196cd2016-04-08 14:08:37 -0700778 DiffObjectContents(obj, remote_bytes, os);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700779 }
780
Igor Murashkin37743352014-11-13 14:38:00 -0800781 os << "\n" << " Dirty object count by class:\n";
782 for (const auto& vk_pair : dirty_object_class_values) {
783 int dirty_object_count = vk_pair.first;
784 mirror::Class* klass = vk_pair.second;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700785 int object_sizes = class_data[klass].dirty_object_size_in_bytes;
786 float avg_dirty_bytes_per_class =
787 class_data[klass].dirty_object_byte_count * 1.0f / object_sizes;
Igor Murashkin37743352014-11-13 14:38:00 -0800788 float avg_object_size = object_sizes * 1.0f / dirty_object_count;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700789 const std::string& descriptor = class_data[klass].descriptor;
David Sehr709b0702016-10-13 09:12:37 -0700790 os << " " << mirror::Class::PrettyClass(klass) << " ("
Igor Murashkin37743352014-11-13 14:38:00 -0800791 << "objects: " << dirty_object_count << ", "
792 << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
793 << "avg object size: " << avg_object_size << ", "
794 << "class descriptor: '" << descriptor << "'"
795 << ")\n";
796
797 constexpr size_t kMaxAddressPrint = 5;
798 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
799 os << " sample object addresses: ";
800 for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
801 auto art_method = art_method_dirty_objects[i];
802
803 os << reinterpret_cast<void*>(art_method) << ", ";
804 }
805 os << "\n";
806
807 os << " dirty byte +offset:count list = ";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700808 auto art_method_field_dirty_count_sorted =
809 SortByValueDesc<off_t, int, int>(art_method_field_dirty_count);
Igor Murashkin37743352014-11-13 14:38:00 -0800810 for (auto pair : art_method_field_dirty_count_sorted) {
811 off_t offset = pair.second;
812 int count = pair.first;
813
814 os << "+" << offset << ":" << count << ", ";
815 }
816
817 os << "\n";
818
819 os << " field contents:\n";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700820 const auto& dirty_objects_list = class_data[klass].dirty_objects;
David Sehr50005a02017-06-21 13:24:21 -0700821 for (const uint8_t* uobj : dirty_objects_list) {
822 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(uobj));
Igor Murashkin37743352014-11-13 14:38:00 -0800823 // remote method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700824 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800825
826 // remote class
827 mirror::Class* remote_declaring_class =
David Sehr50005a02017-06-21 13:24:21 -0700828 FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents_, boot_map_);
Igor Murashkin37743352014-11-13 14:38:00 -0800829
830 // local class
831 mirror::Class* declaring_class =
David Sehr50005a02017-06-21 13:24:21 -0700832 RemoteContentsPointerToLocal(remote_declaring_class, remote_contents_, image_header_);
Igor Murashkin37743352014-11-13 14:38:00 -0800833
834 os << " " << reinterpret_cast<void*>(obj) << " ";
835 os << " entryPointFromJni: "
836 << reinterpret_cast<const void*>(
David Sehr50005a02017-06-21 13:24:21 -0700837 art_method->GetDataPtrSize(pointer_size_)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800838 os << " entryPointFromQuickCompiledCode: "
839 << reinterpret_cast<const void*>(
David Sehr50005a02017-06-21 13:24:21 -0700840 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_))
Igor Murashkin37743352014-11-13 14:38:00 -0800841 << ", ";
842 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
843 os << " class_status (local): " << declaring_class->GetStatus();
844 os << " class_status (remote): " << remote_declaring_class->GetStatus();
845 os << "\n";
846 }
847 }
848 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
849 os << " sample object addresses: ";
850 for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
851 auto class_ptr = class_dirty_objects[i];
852
David Sehr50005a02017-06-21 13:24:21 -0700853 os << reinterpret_cast<const void*>(class_ptr) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800854 }
855 os << "\n";
856
857 os << " dirty byte +offset:count list = ";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700858 auto class_field_dirty_count_sorted =
David Sehr50005a02017-06-21 13:24:21 -0700859 SortByValueDesc<off_t, int, size_t>(class_field_dirty_count);
Igor Murashkin37743352014-11-13 14:38:00 -0800860 for (auto pair : class_field_dirty_count_sorted) {
861 off_t offset = pair.second;
862 int count = pair.first;
863
864 os << "+" << offset << ":" << count << ", ";
865 }
866 os << "\n";
867
868 os << " field contents:\n";
David Sehr50005a02017-06-21 13:24:21 -0700869 // TODO: templatize this to avoid the awful casts down to uint8_t* and back.
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700870 const auto& dirty_objects_list = class_data[klass].dirty_objects;
David Sehr50005a02017-06-21 13:24:21 -0700871 for (const uint8_t* uobj : dirty_objects_list) {
872 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(uobj));
Igor Murashkin37743352014-11-13 14:38:00 -0800873 // remote class object
874 auto remote_klass = reinterpret_cast<mirror::Class*>(obj);
875
876 // local class object
877 auto local_klass = RemoteContentsPointerToLocal(remote_klass,
David Sehr50005a02017-06-21 13:24:21 -0700878 remote_contents_,
879 image_header_);
Igor Murashkin37743352014-11-13 14:38:00 -0800880
David Sehr50005a02017-06-21 13:24:21 -0700881 os << " " << reinterpret_cast<const void*>(obj) << " ";
Igor Murashkin37743352014-11-13 14:38:00 -0800882 os << " class_status (remote): " << remote_klass->GetStatus() << ", ";
883 os << " class_status (local): " << local_klass->GetStatus();
884 os << "\n";
885 }
886 }
887 }
888
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700889 auto false_dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
890 class_data, [](const ClassData& d) { return d.false_dirty_object_count; });
Igor Murashkin37743352014-11-13 14:38:00 -0800891
892 os << "\n" << " False-dirty object count by class:\n";
893 for (const auto& vk_pair : false_dirty_object_class_values) {
894 int object_count = vk_pair.first;
895 mirror::Class* klass = vk_pair.second;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700896 int object_sizes = class_data[klass].false_dirty_byte_count;
Igor Murashkin37743352014-11-13 14:38:00 -0800897 float avg_object_size = object_sizes * 1.0f / object_count;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700898 const std::string& descriptor = class_data[klass].descriptor;
David Sehr709b0702016-10-13 09:12:37 -0700899 os << " " << mirror::Class::PrettyClass(klass) << " ("
Igor Murashkin37743352014-11-13 14:38:00 -0800900 << "objects: " << object_count << ", "
901 << "avg object size: " << avg_object_size << ", "
902 << "total bytes: " << object_sizes << ", "
903 << "class descriptor: '" << descriptor << "'"
904 << ")\n";
905
906 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
David Sehr50005a02017-06-21 13:24:21 -0700907 // TODO: templatize this to avoid the awful casts down to uint8_t* and back.
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700908 auto& art_method_false_dirty_objects = class_data[klass].false_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800909
910 os << " field contents:\n";
David Sehr50005a02017-06-21 13:24:21 -0700911 for (const uint8_t* uobj : art_method_false_dirty_objects) {
912 auto obj = const_cast<mirror::Object*>(reinterpret_cast<const mirror::Object*>(uobj));
Igor Murashkin37743352014-11-13 14:38:00 -0800913 // local method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700914 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800915
916 // local class
917 mirror::Class* declaring_class = art_method->GetDeclaringClass();
918
David Sehr50005a02017-06-21 13:24:21 -0700919 os << " " << reinterpret_cast<const void*>(obj) << " ";
Igor Murashkin37743352014-11-13 14:38:00 -0800920 os << " entryPointFromJni: "
921 << reinterpret_cast<const void*>(
David Sehr50005a02017-06-21 13:24:21 -0700922 art_method->GetDataPtrSize(pointer_size_)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800923 os << " entryPointFromQuickCompiledCode: "
924 << reinterpret_cast<const void*>(
David Sehr50005a02017-06-21 13:24:21 -0700925 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_))
Igor Murashkin37743352014-11-13 14:38:00 -0800926 << ", ";
927 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
928 os << " class_status (local): " << declaring_class->GetStatus();
929 os << "\n";
930 }
931 }
932 }
933
934 os << "\n" << " Clean object count by class:\n";
935 for (const auto& vk_pair : clean_object_class_values) {
David Sehr709b0702016-10-13 09:12:37 -0700936 os << " " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
Igor Murashkin37743352014-11-13 14:38:00 -0800937 }
938
939 return true;
940 }
941
942 // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
943 // Returned pointer will point to inside of remote_contents.
944 template <typename T>
945 static T* FixUpRemotePointer(T* remote_ptr,
946 std::vector<uint8_t>& remote_contents,
947 const backtrace_map_t& boot_map) {
948 if (remote_ptr == nullptr) {
949 return nullptr;
950 }
951
952 uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr);
953
954 CHECK_LE(boot_map.start, remote);
955 CHECK_GT(boot_map.end, remote);
956
957 off_t boot_offset = remote - boot_map.start;
958
959 return reinterpret_cast<T*>(&remote_contents[boot_offset]);
960 }
961
962 template <typename T>
963 static T* RemoteContentsPointerToLocal(T* remote_ptr,
964 std::vector<uint8_t>& remote_contents,
965 const ImageHeader& image_header) {
966 if (remote_ptr == nullptr) {
967 return nullptr;
968 }
969
970 uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr);
971 ptrdiff_t boot_offset = remote - &remote_contents[0];
972
973 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
974
975 return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
976 }
977
978 static std::string GetClassDescriptor(mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700979 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -0800980 CHECK(klass != nullptr);
981
982 std::string descriptor;
983 const char* descriptor_str = klass->GetDescriptor(&descriptor);
984
985 return std::string(descriptor_str);
986 }
987
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700988 template <typename K, typename V, typename D>
989 static std::vector<std::pair<V, K>> SortByValueDesc(
990 const std::map<K, D> map,
991 std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) {
Igor Murashkin37743352014-11-13 14:38:00 -0800992 // Store value->key so that we can use the default sort from pair which
993 // sorts by value first and then key
994 std::vector<std::pair<V, K>> value_key_vector;
995
996 for (const auto& kv_pair : map) {
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700997 value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first));
Igor Murashkin37743352014-11-13 14:38:00 -0800998 }
999
1000 // Sort in reverse (descending order)
1001 std::sort(value_key_vector.rbegin(), value_key_vector.rend());
1002 return value_key_vector;
1003 }
1004
1005 static bool GetPageFrameNumber(File* page_map_file,
1006 size_t virtual_page_index,
1007 uint64_t* page_frame_number,
1008 std::string* error_msg) {
1009 CHECK(page_map_file != nullptr);
1010 CHECK(page_frame_number != nullptr);
1011 CHECK(error_msg != nullptr);
1012
1013 constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
1014 constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; // bits 0-54 [in /proc/$pid/pagemap]
1015 constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55); // bit 55 [in /proc/$pid/pagemap]
1016
1017 uint64_t page_map_entry = 0;
1018
1019 // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number
1020 if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize,
1021 virtual_page_index * kPageMapEntrySize)) {
1022 *error_msg = StringPrintf("Failed to read the virtual page index entry from %s",
1023 page_map_file->GetPath().c_str());
1024 return false;
1025 }
1026
1027 // TODO: seems useless, remove this.
1028 bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0;
1029 if ((false)) {
1030 LOG(VERBOSE) << soft_dirty; // Suppress unused warning
1031 UNREACHABLE();
1032 }
1033
1034 *page_frame_number = page_map_entry & kPageFrameNumberMask;
1035
1036 return true;
1037 }
1038
1039 static int IsPageDirty(File* page_map_file,
David Sehr50005a02017-06-21 13:24:21 -07001040 File* clean_pagemap_file,
1041 File* kpageflags_file,
1042 File* kpagecount_file,
Igor Murashkin37743352014-11-13 14:38:00 -08001043 size_t virtual_page_idx,
1044 size_t clean_virtual_page_idx,
1045 // Out parameters:
1046 uint64_t* page_count, std::string* error_msg) {
1047 CHECK(page_map_file != nullptr);
David Sehr50005a02017-06-21 13:24:21 -07001048 CHECK(clean_pagemap_file != nullptr);
1049 CHECK_NE(page_map_file, clean_pagemap_file);
1050 CHECK(kpageflags_file != nullptr);
1051 CHECK(kpagecount_file != nullptr);
Igor Murashkin37743352014-11-13 14:38:00 -08001052 CHECK(page_count != nullptr);
1053 CHECK(error_msg != nullptr);
1054
1055 // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
1056
1057 constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
1058 constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
1059 constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags
1060 constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags
1061 constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags
1062
1063 uint64_t page_frame_number = 0;
1064 if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
1065 return -1;
1066 }
1067
1068 uint64_t page_frame_number_clean = 0;
David Sehr50005a02017-06-21 13:24:21 -07001069 if (!GetPageFrameNumber(clean_pagemap_file, clean_virtual_page_idx, &page_frame_number_clean,
Igor Murashkin37743352014-11-13 14:38:00 -08001070 error_msg)) {
1071 return -1;
1072 }
1073
1074 // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
1075 uint64_t kpage_flags_entry = 0;
David Sehr50005a02017-06-21 13:24:21 -07001076 if (!kpageflags_file->PreadFully(&kpage_flags_entry,
Igor Murashkin37743352014-11-13 14:38:00 -08001077 kPageFlagsEntrySize,
1078 page_frame_number * kPageFlagsEntrySize)) {
1079 *error_msg = StringPrintf("Failed to read the page flags from %s",
David Sehr50005a02017-06-21 13:24:21 -07001080 kpageflags_file->GetPath().c_str());
Igor Murashkin37743352014-11-13 14:38:00 -08001081 return -1;
1082 }
1083
1084 // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
David Sehr50005a02017-06-21 13:24:21 -07001085 if (!kpagecount_file->PreadFully(page_count /*out*/,
Igor Murashkin37743352014-11-13 14:38:00 -08001086 kPageCountEntrySize,
1087 page_frame_number * kPageCountEntrySize)) {
1088 *error_msg = StringPrintf("Failed to read the page count from %s",
David Sehr50005a02017-06-21 13:24:21 -07001089 kpagecount_file->GetPath().c_str());
Igor Murashkin37743352014-11-13 14:38:00 -08001090 return -1;
1091 }
1092
1093 // There must be a page frame at the requested address.
1094 CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
1095 // The page frame must be memory mapped
1096 CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
1097
1098 // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
1099 bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
1100
1101 // page_frame_number_clean must come from the *same* process
1102 // but a *different* mmap than page_frame_number
1103 if (flags_dirty) {
1104 CHECK_NE(page_frame_number, page_frame_number_clean);
1105 }
1106
1107 return page_frame_number != page_frame_number_clean;
1108 }
1109
David Sehr50005a02017-06-21 13:24:21 -07001110 void PrintPidLine(const std::string& kind, pid_t pid) {
1111 if (pid < 0) {
1112 *os_ << kind << " DIFF PID: disabled\n\n";
1113 } else {
1114 *os_ << kind << " DIFF PID (" << pid << "): ";
1115 }
1116 }
1117
1118 static bool EndsWith(const std::string& str, const std::string& suffix) {
1119 return str.size() >= suffix.size() &&
1120 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
1121 }
1122
1123 // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
1124 static std::string BaseName(const std::string& str) {
1125 size_t idx = str.rfind('/');
1126 if (idx == std::string::npos) {
1127 return str;
1128 }
1129
1130 return str.substr(idx + 1);
1131 }
1132
Igor Murashkin37743352014-11-13 14:38:00 -08001133 // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
1134 std::string GetImageLocationBaseName() const {
1135 return BaseName(std::string(image_location_));
1136 }
1137
1138 std::ostream* os_;
1139 const ImageHeader& image_header_;
Andreas Gampe8994a042015-12-30 19:03:17 +00001140 const std::string image_location_;
Igor Murashkin37743352014-11-13 14:38:00 -08001141 pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001142 pid_t zygote_diff_pid_; // Dump image diff against zygote boot.art if pid is non-negative
David Sehr20e271a2017-06-14 13:02:14 -07001143 bool zygote_pid_only_; // The user only specified a pid for the zygote.
Igor Murashkin37743352014-11-13 14:38:00 -08001144
David Sehr50005a02017-06-21 13:24:21 -07001145 // Pointer size constant for object fields, etc.
1146 PointerSize pointer_size_;
1147 // BacktraceMap used for finding the memory mapping of the image file.
1148 std::unique_ptr<BacktraceMap> proc_maps_;
1149 // Boot image mapping.
1150 backtrace_map_t boot_map_{}; // NOLINT
1151 // The size of the boot image mapping.
1152 size_t boot_map_size_;
1153 // The contents of /proc/<image_diff_pid_>/maps.
1154 std::vector<uint8_t> remote_contents_;
1155 // The contents of /proc/<zygote_diff_pid_>/maps.
1156 std::vector<uint8_t> zygote_contents_;
1157 // A File for reading /proc/<zygote_diff_pid_>/maps.
1158 File pagemap_file_;
1159 // A File for reading /proc/self/pagemap.
1160 File clean_pagemap_file_;
1161 // A File for reading /proc/kpageflags.
1162 File kpageflags_file_;
1163 // A File for reading /proc/kpagecount.
1164 File kpagecount_file_;
1165
Igor Murashkin37743352014-11-13 14:38:00 -08001166 DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
1167};
1168
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001169static int DumpImage(Runtime* runtime,
1170 std::ostream* os,
1171 pid_t image_diff_pid,
1172 pid_t zygote_diff_pid) {
Igor Murashkin37743352014-11-13 14:38:00 -08001173 ScopedObjectAccess soa(Thread::Current());
1174 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001175 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
1176 CHECK(!image_spaces.empty());
1177 for (gc::space::ImageSpace* image_space : image_spaces) {
1178 const ImageHeader& image_header = image_space->GetImageHeader();
1179 if (!image_header.IsValid()) {
1180 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
1181 return EXIT_FAILURE;
1182 }
1183
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001184 ImgDiagDumper img_diag_dumper(os,
1185 image_header,
1186 image_space->GetImageLocation(),
1187 image_diff_pid,
1188 zygote_diff_pid);
David Sehr50005a02017-06-21 13:24:21 -07001189 if (!img_diag_dumper.Init()) {
1190 return EXIT_FAILURE;
1191 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001192 if (!img_diag_dumper.Dump()) {
1193 return EXIT_FAILURE;
1194 }
Igor Murashkin37743352014-11-13 14:38:00 -08001195 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001196 return EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -08001197}
1198
1199struct ImgDiagArgs : public CmdlineArgs {
1200 protected:
1201 using Base = CmdlineArgs;
1202
1203 virtual ParseStatus ParseCustom(const StringPiece& option,
1204 std::string* error_msg) OVERRIDE {
1205 {
1206 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
1207 if (base_parse != kParseUnknownArgument) {
1208 return base_parse;
1209 }
1210 }
1211
1212 if (option.starts_with("--image-diff-pid=")) {
1213 const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data();
1214
1215 if (!ParseInt(image_diff_pid, &image_diff_pid_)) {
1216 *error_msg = "Image diff pid out of range";
1217 return kParseError;
1218 }
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001219 } else if (option.starts_with("--zygote-diff-pid=")) {
1220 const char* zygote_diff_pid = option.substr(strlen("--zygote-diff-pid=")).data();
1221
1222 if (!ParseInt(zygote_diff_pid, &zygote_diff_pid_)) {
1223 *error_msg = "Zygote diff pid out of range";
1224 return kParseError;
1225 }
Igor Murashkin37743352014-11-13 14:38:00 -08001226 } else {
1227 return kParseUnknownArgument;
1228 }
1229
1230 return kParseOk;
1231 }
1232
1233 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
1234 // Perform the parent checks.
1235 ParseStatus parent_checks = Base::ParseChecks(error_msg);
1236 if (parent_checks != kParseOk) {
1237 return parent_checks;
1238 }
1239
1240 // Perform our own checks.
1241
1242 if (kill(image_diff_pid_,
1243 /*sig*/0) != 0) { // No signal is sent, perform error-checking only.
1244 // Check if the pid exists before proceeding.
1245 if (errno == ESRCH) {
1246 *error_msg = "Process specified does not exist";
1247 } else {
1248 *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
1249 }
1250 return kParseError;
1251 } else if (instruction_set_ != kRuntimeISA) {
1252 // Don't allow different ISAs since the images are ISA-specific.
1253 // Right now the code assumes both the runtime ISA and the remote ISA are identical.
1254 *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
1255 return kParseError;
1256 }
1257
1258 return kParseOk;
1259 }
1260
1261 virtual std::string GetUsage() const {
1262 std::string usage;
1263
1264 usage +=
1265 "Usage: imgdiag [options] ...\n"
1266 " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
1267 " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
1268 "\n";
1269
1270 usage += Base::GetUsage();
1271
1272 usage += // Optional.
1273 " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
1274 " Example: --image-diff-pid=$(pid zygote)\n"
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001275 " --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff "
1276 "against.\n"
1277 " Example: --zygote-diff-pid=$(pid zygote)\n"
Igor Murashkin37743352014-11-13 14:38:00 -08001278 "\n";
1279
1280 return usage;
1281 }
1282
1283 public:
1284 pid_t image_diff_pid_ = -1;
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001285 pid_t zygote_diff_pid_ = -1;
Igor Murashkin37743352014-11-13 14:38:00 -08001286};
1287
1288struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
1289 virtual bool ExecuteWithRuntime(Runtime* runtime) {
1290 CHECK(args_ != nullptr);
1291
1292 return DumpImage(runtime,
Igor Murashkin37743352014-11-13 14:38:00 -08001293 args_->os_,
Mathieu Chartierc5196cd2016-04-08 14:08:37 -07001294 args_->image_diff_pid_,
1295 args_->zygote_diff_pid_) == EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -08001296 }
1297};
1298
1299} // namespace art
1300
1301int main(int argc, char** argv) {
1302 art::ImgDiagMain main;
1303 return main.Main(argc, argv);
1304}