blob: c2a812ee0981283e97be26233b297d4a94bf37e1 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "art_method-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080030#include "base/unix_file/fd_file.h"
31#include "base/stringprintf.h"
32#include "gc/space/image_space.h"
33#include "gc/heap.h"
34#include "mirror/class-inl.h"
35#include "mirror/object-inl.h"
Igor Murashkin37743352014-11-13 14:38:00 -080036#include "image.h"
37#include "scoped_thread_state_change.h"
38#include "os.h"
Igor Murashkin37743352014-11-13 14:38:00 -080039
40#include "cmdline.h"
41#include "backtrace/BacktraceMap.h"
42
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <signal.h>
46
47namespace art {
48
49class ImgDiagDumper {
50 public:
51 explicit ImgDiagDumper(std::ostream* os,
Mathieu Chartiercb044bc2016-04-01 13:56:41 -070052 const ImageHeader& image_header,
53 const std::string& image_location,
54 pid_t image_diff_pid)
Igor Murashkin37743352014-11-13 14:38:00 -080055 : os_(os),
56 image_header_(image_header),
57 image_location_(image_location),
58 image_diff_pid_(image_diff_pid) {}
59
Mathieu Chartier90443472015-07-16 20:32:27 -070060 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -080061 std::ostream& os = *os_;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -070062 os << "IMAGE LOCATION: " << image_location_ << "\n\n";
63
Igor Murashkin37743352014-11-13 14:38:00 -080064 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
65
66 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
67
68 bool ret = true;
69 if (image_diff_pid_ >= 0) {
70 os << "IMAGE DIFF PID (" << image_diff_pid_ << "): ";
71 ret = DumpImageDiff(image_diff_pid_);
72 os << "\n\n";
73 } else {
74 os << "IMAGE DIFF PID: disabled\n\n";
75 }
76
77 os << std::flush;
78
79 return ret;
80 }
81
82 private:
83 static bool EndsWith(const std::string& str, const std::string& suffix) {
84 return str.size() >= suffix.size() &&
85 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
86 }
87
88 // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
89 static std::string BaseName(const std::string& str) {
90 size_t idx = str.rfind("/");
91 if (idx == std::string::npos) {
92 return str;
93 }
94
95 return str.substr(idx + 1);
96 }
97
Mathieu Chartier90443472015-07-16 20:32:27 -070098 bool DumpImageDiff(pid_t image_diff_pid) SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -080099 std::ostream& os = *os_;
100
101 {
102 struct stat sts;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700103 std::string proc_pid_str =
104 StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int]
Igor Murashkin37743352014-11-13 14:38:00 -0800105 if (stat(proc_pid_str.c_str(), &sts) == -1) {
106 os << "Process does not exist";
107 return false;
108 }
109 }
110
111 // Open /proc/$pid/maps to view memory maps
112 auto proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid));
113 if (proc_maps == nullptr) {
114 os << "Could not read backtrace maps";
115 return false;
116 }
117
118 bool found_boot_map = false;
119 backtrace_map_t boot_map = backtrace_map_t();
120 // Find the memory map only for boot.art
121 for (const backtrace_map_t& map : *proc_maps) {
122 if (EndsWith(map.name, GetImageLocationBaseName())) {
123 if ((map.flags & PROT_WRITE) != 0) {
124 boot_map = map;
125 found_boot_map = true;
126 break;
127 }
128 // In actuality there's more than 1 map, but the second one is read-only.
129 // The one we care about is the write-able map.
130 // The readonly maps are guaranteed to be identical, so its not interesting to compare
131 // them.
132 }
133 }
134
135 if (!found_boot_map) {
136 os << "Could not find map for " << GetImageLocationBaseName();
137 return false;
138 }
139
140 // Future idea: diff against zygote so we can ignore the shared dirty pages.
141 return DumpImageDiffMap(image_diff_pid, boot_map);
142 }
143
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700144 static std::string PrettyFieldValue(ArtField* field, mirror::Object* obj)
145 SHARED_REQUIRES(Locks::mutator_lock_) {
146 std::ostringstream oss;
147 switch (field->GetTypeAsPrimitiveType()) {
148 case Primitive::kPrimNot: {
149 oss << obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
150 field->GetOffset());
151 break;
152 }
153 case Primitive::kPrimBoolean: {
154 oss << static_cast<bool>(obj->GetFieldBoolean<kVerifyNone>(field->GetOffset()));
155 break;
156 }
157 case Primitive::kPrimByte: {
158 oss << static_cast<int32_t>(obj->GetFieldByte<kVerifyNone>(field->GetOffset()));
159 break;
160 }
161 case Primitive::kPrimChar: {
162 oss << obj->GetFieldChar<kVerifyNone>(field->GetOffset());
163 break;
164 }
165 case Primitive::kPrimShort: {
166 oss << obj->GetFieldShort<kVerifyNone>(field->GetOffset());
167 break;
168 }
169 case Primitive::kPrimInt: {
170 oss << obj->GetField32<kVerifyNone>(field->GetOffset());
171 break;
172 }
173 case Primitive::kPrimLong: {
174 oss << obj->GetField64<kVerifyNone>(field->GetOffset());
175 break;
176 }
177 case Primitive::kPrimFloat: {
178 oss << obj->GetField32<kVerifyNone>(field->GetOffset());
179 break;
180 }
181 case Primitive::kPrimDouble: {
182 oss << obj->GetField64<kVerifyNone>(field->GetOffset());
183 break;
184 }
185 case Primitive::kPrimVoid: {
186 oss << "void";
187 break;
188 }
189 }
190 return oss.str();
191 }
192
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700193 // Aggregate and detail class data from an image diff.
194 struct ClassData {
195 int dirty_object_count = 0;
196
197 // Track only the byte-per-byte dirtiness (in bytes)
198 int dirty_object_byte_count = 0;
199
200 // Track the object-by-object dirtiness (in bytes)
201 int dirty_object_size_in_bytes = 0;
202
203 int clean_object_count = 0;
204
205 std::string descriptor;
206
207 int false_dirty_byte_count = 0;
208 int false_dirty_object_count = 0;
209 std::vector<mirror::Object*> false_dirty_objects;
210
211 // Remote pointers to dirty objects
212 std::vector<mirror::Object*> dirty_objects;
213 };
214
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700215 // Look at /proc/$pid/mem and only diff the things from there
Igor Murashkin37743352014-11-13 14:38:00 -0800216 bool DumpImageDiffMap(pid_t image_diff_pid, const backtrace_map_t& boot_map)
Mathieu Chartier90443472015-07-16 20:32:27 -0700217 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -0800218 std::ostream& os = *os_;
219 const size_t pointer_size = InstructionSetPointerSize(
220 Runtime::Current()->GetInstructionSet());
221
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700222 std::string file_name =
223 StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int]
Igor Murashkin37743352014-11-13 14:38:00 -0800224
225 size_t boot_map_size = boot_map.end - boot_map.start;
226
227 // Open /proc/$pid/mem as a file
228 auto map_file = std::unique_ptr<File>(OS::OpenFileForReading(file_name.c_str()));
229 if (map_file == nullptr) {
230 os << "Failed to open " << file_name << " for reading";
231 return false;
232 }
233
234 // Memory-map /proc/$pid/mem subset from the boot map
235 CHECK(boot_map.end >= boot_map.start);
236
237 std::string error_msg;
238
239 // Walk the bytes and diff against our boot image
Andreas Gampe8994a042015-12-30 19:03:17 +0000240 const ImageHeader& boot_image_header = image_header_;
Igor Murashkin37743352014-11-13 14:38:00 -0800241
242 os << "\nObserving boot image header at address "
243 << reinterpret_cast<const void*>(&boot_image_header)
244 << "\n\n";
245
246 const uint8_t* image_begin_unaligned = boot_image_header.GetImageBegin();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247 const uint8_t* image_mirror_end_unaligned = image_begin_unaligned +
Mathieu Chartiere401d142015-04-22 13:56:20 -0700248 boot_image_header.GetImageSection(ImageHeader::kSectionObjects).Size();
249 const uint8_t* image_end_unaligned = image_begin_unaligned + boot_image_header.GetImageSize();
Igor Murashkin37743352014-11-13 14:38:00 -0800250
251 // Adjust range to nearest page
252 const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
253 const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
254
255 ptrdiff_t page_off_begin = boot_image_header.GetImageBegin() - image_begin;
256
257 if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start ||
258 reinterpret_cast<uintptr_t>(image_end) < boot_map.end) {
259 // Sanity check that we aren't trying to read a completely different boot image
260 os << "Remote boot map is out of range of local boot map: " <<
261 "local begin " << reinterpret_cast<const void*>(image_begin) <<
262 ", local end " << reinterpret_cast<const void*>(image_end) <<
263 ", remote begin " << reinterpret_cast<const void*>(boot_map.start) <<
264 ", remote end " << reinterpret_cast<const void*>(boot_map.end);
265 return false;
266 // If we wanted even more validation we could map the ImageHeader from the file
267 }
268
269 std::vector<uint8_t> remote_contents(boot_map_size);
270 if (!map_file->PreadFully(&remote_contents[0], boot_map_size, boot_map.start)) {
271 os << "Could not fully read file " << file_name;
272 return false;
273 }
274
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700275 std::string page_map_file_name = StringPrintf(
276 "/proc/%ld/pagemap", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int]
Igor Murashkin37743352014-11-13 14:38:00 -0800277 auto page_map_file = std::unique_ptr<File>(OS::OpenFileForReading(page_map_file_name.c_str()));
278 if (page_map_file == nullptr) {
279 os << "Failed to open " << page_map_file_name << " for reading: " << strerror(errno);
280 return false;
281 }
282
283 // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough
284 const char* clean_page_map_file_name = "/proc/self/pagemap";
285 auto clean_page_map_file = std::unique_ptr<File>(
286 OS::OpenFileForReading(clean_page_map_file_name));
287 if (clean_page_map_file == nullptr) {
288 os << "Failed to open " << clean_page_map_file_name << " for reading: " << strerror(errno);
289 return false;
290 }
291
292 auto kpage_flags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags"));
293 if (kpage_flags_file == nullptr) {
294 os << "Failed to open /proc/kpageflags for reading: " << strerror(errno);
295 return false;
296 }
297
298 auto kpage_count_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount"));
299 if (kpage_count_file == nullptr) {
300 os << "Failed to open /proc/kpagecount for reading:" << strerror(errno);
301 return false;
302 }
303
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700304 // Set of the remote virtual page indices that are dirty
305 std::set<size_t> dirty_page_set_remote;
306 // Set of the local virtual page indices that are dirty
307 std::set<size_t> dirty_page_set_local;
Igor Murashkin37743352014-11-13 14:38:00 -0800308
309 size_t different_int32s = 0;
310 size_t different_bytes = 0;
311 size_t different_pages = 0;
312 size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address)
313 size_t page_idx = 0; // Page index relative to 0
314 size_t previous_page_idx = 0; // Previous page index relative to 0
315 size_t dirty_pages = 0;
316 size_t private_pages = 0;
317 size_t private_dirty_pages = 0;
318
319 // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
320 for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) {
321 ptrdiff_t offset = begin - boot_map.start;
322
323 // We treat the image header as part of the memory map for now
324 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
325 // But it might still be interesting to see if any of the ImageHeader data mutated
326 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
327 uint8_t* remote_ptr = &remote_contents[offset];
328
329 if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
330 different_pages++;
331
332 // Count the number of 32-bit integers that are different.
333 for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
334 uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr);
335 const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
336
337 if (remote_ptr_int32[i] != local_ptr_int32[i]) {
338 different_int32s++;
339 }
340 }
341 }
342 }
343
344 // Iterate through one byte at a time.
345 for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) {
346 previous_page_idx = page_idx;
347 ptrdiff_t offset = begin - boot_map.start;
348
349 // We treat the image header as part of the memory map for now
350 // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
351 // But it might still be interesting to see if any of the ImageHeader data mutated
352 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
353 uint8_t* remote_ptr = &remote_contents[offset];
354
355 virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
356
357 // Calculate the page index, relative to the 0th page where the image begins
358 page_idx = (offset + page_off_begin) / kPageSize;
359 if (*local_ptr != *remote_ptr) {
360 // Track number of bytes that are different
361 different_bytes++;
362 }
363
364 // Independently count the # of dirty pages on the remote side
365 size_t remote_virtual_page_idx = begin / kPageSize;
366 if (previous_page_idx != page_idx) {
367 uint64_t page_count = 0xC0FFEE;
368 // TODO: virtual_page_idx needs to be from the same process
369 int dirtiness = (IsPageDirty(page_map_file.get(), // Image-diff-pid procmap
370 clean_page_map_file.get(), // Self procmap
371 kpage_flags_file.get(),
372 kpage_count_file.get(),
373 remote_virtual_page_idx, // potentially "dirty" page
374 virtual_page_idx, // true "clean" page
375 &page_count,
376 &error_msg));
377 if (dirtiness < 0) {
378 os << error_msg;
379 return false;
380 } else if (dirtiness > 0) {
381 dirty_pages++;
382 dirty_page_set_remote.insert(dirty_page_set_remote.end(), remote_virtual_page_idx);
383 dirty_page_set_local.insert(dirty_page_set_local.end(), virtual_page_idx);
384 }
385
386 bool is_dirty = dirtiness > 0;
387 bool is_private = page_count == 1;
388
389 if (page_count == 1) {
390 private_pages++;
391 }
392
393 if (is_dirty && is_private) {
394 private_dirty_pages++;
395 }
396 }
397 }
398
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700399 std::map<mirror::Class*, ClassData> class_data;
400
Igor Murashkin37743352014-11-13 14:38:00 -0800401 // Walk each object in the remote image space and compare it against ours
402 size_t different_objects = 0;
Igor Murashkin37743352014-11-13 14:38:00 -0800403
404 std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 std::vector<ArtMethod*> art_method_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800406
407 std::map<off_t /* field offset */, int /* count */> class_field_dirty_count;
408 std::vector<mirror::Class*> class_dirty_objects;
409
410 // List of local objects that are clean, but located on dirty pages.
411 std::vector<mirror::Object*> false_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800412 size_t false_dirty_object_bytes = 0;
413
Igor Murashkin37743352014-11-13 14:38:00 -0800414 // Look up remote classes by their descriptor
415 std::map<std::string, mirror::Class*> remote_class_map;
416 // Look up local classes by their descriptor
417 std::map<std::string, mirror::Class*> local_class_map;
418
Mathieu Chartierd464fa12016-04-08 18:54:36 -0700419 std::unordered_set<mirror::Object*> dirty_objects;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700420
Igor Murashkin37743352014-11-13 14:38:00 -0800421 size_t dirty_object_bytes = 0;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700422 const uint8_t* begin_image_ptr = image_begin_unaligned;
423 const uint8_t* end_image_ptr = image_mirror_end_unaligned;
Igor Murashkin37743352014-11-13 14:38:00 -0800424
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700425 const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment);
426 while (reinterpret_cast<uintptr_t>(current) < reinterpret_cast<uintptr_t>(end_image_ptr)) {
427 CHECK_ALIGNED(current, kObjectAlignment);
428 mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current));
Igor Murashkin37743352014-11-13 14:38:00 -0800429
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700430 // Sanity check that we are reading a real object
431 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
432 if (kUseBakerOrBrooksReadBarrier) {
433 obj->AssertReadBarrierPointer();
434 }
435
436 // Iterate every page this object belongs to
437 bool on_dirty_page = false;
438 size_t page_off = 0;
439 size_t current_page_idx;
440 uintptr_t object_address;
441 do {
442 object_address = reinterpret_cast<uintptr_t>(current);
443 current_page_idx = object_address / kPageSize + page_off;
444
445 if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) {
446 // This object is on a dirty page
447 on_dirty_page = true;
Igor Murashkin37743352014-11-13 14:38:00 -0800448 }
449
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700450 page_off++;
451 } while ((current_page_idx * kPageSize) <
452 RoundUp(object_address + obj->SizeOf(), kObjectAlignment));
Igor Murashkin37743352014-11-13 14:38:00 -0800453
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700454 mirror::Class* klass = obj->GetClass();
455
456 bool different_object = false;
457
458 // Check against the other object and see if they are different
459 ptrdiff_t offset = current - begin_image_ptr;
460 const uint8_t* current_remote = &remote_contents[offset];
461 mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(
462 const_cast<uint8_t*>(current_remote));
463 if (memcmp(current, current_remote, obj->SizeOf()) != 0) {
464 different_objects++;
465 dirty_object_bytes += obj->SizeOf();
466 dirty_objects.insert(obj);
467
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700468 ++class_data[klass].dirty_object_count;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700469
470 // Go byte-by-byte and figure out what exactly got dirtied
471 size_t dirty_byte_count_per_object = 0;
472 for (size_t i = 0; i < obj->SizeOf(); ++i) {
473 if (current[i] != current_remote[i]) {
474 dirty_byte_count_per_object++;
Igor Murashkin37743352014-11-13 14:38:00 -0800475 }
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700476 }
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700477 class_data[klass].dirty_object_byte_count += dirty_byte_count_per_object;
478 class_data[klass].dirty_object_size_in_bytes += obj->SizeOf();
Igor Murashkin37743352014-11-13 14:38:00 -0800479
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700480 different_object = true;
Igor Murashkin37743352014-11-13 14:38:00 -0800481
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700482 class_data[klass].dirty_objects.push_back(remote_obj);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700483 } else {
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700484 ++class_data[klass].clean_object_count;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700485 }
Igor Murashkin37743352014-11-13 14:38:00 -0800486
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700487 std::string descriptor = GetClassDescriptor(klass);
488 if (different_object) {
489 if (klass->IsClassClass()) {
490 // this is a "Class"
491 mirror::Class* obj_as_class = reinterpret_cast<mirror::Class*>(remote_obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800492
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700493 // print the fields that are dirty
Igor Murashkin37743352014-11-13 14:38:00 -0800494 for (size_t i = 0; i < obj->SizeOf(); ++i) {
495 if (current[i] != current_remote[i]) {
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700496 class_field_dirty_count[i]++;
Igor Murashkin37743352014-11-13 14:38:00 -0800497 }
498 }
Igor Murashkin37743352014-11-13 14:38:00 -0800499
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700500 class_dirty_objects.push_back(obj_as_class);
501 } else if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
502 // this is an ArtMethod
503 ArtMethod* art_method = reinterpret_cast<ArtMethod*>(remote_obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800504
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700505 // print the fields that are dirty
506 for (size_t i = 0; i < obj->SizeOf(); ++i) {
507 if (current[i] != current_remote[i]) {
508 art_method_field_dirty_count[i]++;
Igor Murashkin37743352014-11-13 14:38:00 -0800509 }
Igor Murashkin37743352014-11-13 14:38:00 -0800510 }
Igor Murashkin37743352014-11-13 14:38:00 -0800511
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700512 art_method_dirty_objects.push_back(art_method);
Igor Murashkin37743352014-11-13 14:38:00 -0800513 }
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700514 } else if (on_dirty_page) {
515 // This object was either never mutated or got mutated back to the same value.
516 // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
517 false_dirty_objects.push_back(obj);
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700518 class_data[klass].false_dirty_objects.push_back(obj);
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700519 false_dirty_object_bytes += obj->SizeOf();
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700520 class_data[obj->GetClass()].false_dirty_byte_count += obj->SizeOf();
521 class_data[obj->GetClass()].false_dirty_object_count += 1;
Igor Murashkin37743352014-11-13 14:38:00 -0800522 }
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700523
524 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
525 local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj);
526 remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj);
527 }
528
529 // Unconditionally store the class descriptor in case we need it later
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700530 class_data[klass].descriptor = descriptor;
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700531 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Igor Murashkin37743352014-11-13 14:38:00 -0800532 }
533
534 // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects.
535 float true_dirtied_percent = dirty_object_bytes * 1.0f / (dirty_pages * kPageSize);
536 size_t false_dirty_pages = dirty_pages - different_pages;
537
538 os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", "
539 << reinterpret_cast<void*>(boot_map.end) << ") had: \n "
540 << different_bytes << " differing bytes, \n "
541 << different_int32s << " differing int32s, \n "
542 << different_objects << " different objects, \n "
543 << dirty_object_bytes << " different object [bytes], \n "
544 << false_dirty_objects.size() << " false dirty objects,\n "
545 << false_dirty_object_bytes << " false dirty object [bytes], \n "
546 << true_dirtied_percent << " different objects-vs-total in a dirty page;\n "
547 << different_pages << " different pages; \n "
548 << dirty_pages << " pages are dirty; \n "
549 << false_dirty_pages << " pages are false dirty; \n "
550 << private_pages << " pages are private; \n "
551 << private_dirty_pages << " pages are Private_Dirty\n "
552 << "";
553
554 // vector of pairs (int count, Class*)
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700555 auto dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
556 class_data, [](const ClassData& d) { return d.dirty_object_count; });
557 auto clean_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
558 class_data, [](const ClassData& d) { return d.clean_object_count; });
Igor Murashkin37743352014-11-13 14:38:00 -0800559
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700560 os << "\n" << " Dirty objects: " << dirty_objects.size() << "\n";
561 for (mirror::Object* obj : dirty_objects) {
562 const char* tabs = " ";
563 // Attempt to find fields for all dirty bytes.
564 mirror::Class* klass = obj->GetClass();
565 if (obj->IsClass()) {
566 os << tabs << "Class " << PrettyClass(obj->AsClass()) << " " << obj << "\n";
567 } else {
568 os << tabs << "Instance of " << PrettyClass(klass) << " " << obj << "\n";
569 }
570
571 std::unordered_set<ArtField*> dirty_instance_fields;
572 std::unordered_set<ArtField*> dirty_static_fields;
573 const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj);
574 ptrdiff_t offset = obj_bytes - begin_image_ptr;
575 uint8_t* remote_bytes = &remote_contents[offset];
576 mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(remote_bytes);
577 for (size_t i = 0, count = obj->SizeOf(); i < count; ++i) {
578 if (obj_bytes[i] != remote_bytes[i]) {
579 ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i);
580 if (field != nullptr) {
581 dirty_instance_fields.insert(field);
582 } else if (obj->IsClass()) {
583 field = ArtField::FindStaticFieldWithOffset</*exact*/false>(obj->AsClass(), i);
584 if (field != nullptr) {
585 dirty_static_fields.insert(field);
586 }
587 }
588 if (field == nullptr) {
589 if (klass->IsArrayClass()) {
590 mirror::Class* component_type = klass->GetComponentType();
591 Primitive::Type primitive_type = component_type->GetPrimitiveType();
592 size_t component_size = Primitive::ComponentSize(primitive_type);
593 size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
594 if (i >= data_offset) {
595 os << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n";
596 // Skip to next element to prevent spam.
597 i += component_size - 1;
598 continue;
599 }
600 }
601 os << tabs << "No field for byte offset " << i << "\n";
602 }
603 }
604 }
605 // Dump different fields. TODO: Dump field contents.
606 if (!dirty_instance_fields.empty()) {
607 os << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n";
608 for (ArtField* field : dirty_instance_fields) {
609 os << tabs << PrettyField(field)
610 << " original=" << PrettyFieldValue(field, obj)
611 << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
612 }
613 }
614 if (!dirty_static_fields.empty()) {
615 os << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n";
616 for (ArtField* field : dirty_static_fields) {
617 os << tabs << PrettyField(field)
618 << " original=" << PrettyFieldValue(field, obj)
619 << " remote=" << PrettyFieldValue(field, remote_obj) << "\n";
620 }
621 }
622 os << "\n";
623 }
624
Igor Murashkin37743352014-11-13 14:38:00 -0800625 os << "\n" << " Dirty object count by class:\n";
626 for (const auto& vk_pair : dirty_object_class_values) {
627 int dirty_object_count = vk_pair.first;
628 mirror::Class* klass = vk_pair.second;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700629 int object_sizes = class_data[klass].dirty_object_size_in_bytes;
630 float avg_dirty_bytes_per_class =
631 class_data[klass].dirty_object_byte_count * 1.0f / object_sizes;
Igor Murashkin37743352014-11-13 14:38:00 -0800632 float avg_object_size = object_sizes * 1.0f / dirty_object_count;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700633 const std::string& descriptor = class_data[klass].descriptor;
Igor Murashkin37743352014-11-13 14:38:00 -0800634 os << " " << PrettyClass(klass) << " ("
635 << "objects: " << dirty_object_count << ", "
636 << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
637 << "avg object size: " << avg_object_size << ", "
638 << "class descriptor: '" << descriptor << "'"
639 << ")\n";
640
641 constexpr size_t kMaxAddressPrint = 5;
642 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
643 os << " sample object addresses: ";
644 for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
645 auto art_method = art_method_dirty_objects[i];
646
647 os << reinterpret_cast<void*>(art_method) << ", ";
648 }
649 os << "\n";
650
651 os << " dirty byte +offset:count list = ";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700652 auto art_method_field_dirty_count_sorted =
653 SortByValueDesc<off_t, int, int>(art_method_field_dirty_count);
Igor Murashkin37743352014-11-13 14:38:00 -0800654 for (auto pair : art_method_field_dirty_count_sorted) {
655 off_t offset = pair.second;
656 int count = pair.first;
657
658 os << "+" << offset << ":" << count << ", ";
659 }
660
661 os << "\n";
662
663 os << " field contents:\n";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700664 const auto& dirty_objects_list = class_data[klass].dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800665 for (mirror::Object* obj : dirty_objects_list) {
666 // remote method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700667 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800668
669 // remote class
670 mirror::Class* remote_declaring_class =
671 FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents, boot_map);
672
673 // local class
674 mirror::Class* declaring_class =
675 RemoteContentsPointerToLocal(remote_declaring_class,
676 remote_contents,
677 boot_image_header);
678
679 os << " " << reinterpret_cast<void*>(obj) << " ";
680 os << " entryPointFromJni: "
681 << reinterpret_cast<const void*>(
682 art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800683 os << " entryPointFromQuickCompiledCode: "
684 << reinterpret_cast<const void*>(
685 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
686 << ", ";
687 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
688 os << " class_status (local): " << declaring_class->GetStatus();
689 os << " class_status (remote): " << remote_declaring_class->GetStatus();
690 os << "\n";
691 }
692 }
693 if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
694 os << " sample object addresses: ";
695 for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
696 auto class_ptr = class_dirty_objects[i];
697
698 os << reinterpret_cast<void*>(class_ptr) << ", ";
699 }
700 os << "\n";
701
702 os << " dirty byte +offset:count list = ";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700703 auto class_field_dirty_count_sorted =
704 SortByValueDesc<off_t, int, int>(class_field_dirty_count);
Igor Murashkin37743352014-11-13 14:38:00 -0800705 for (auto pair : class_field_dirty_count_sorted) {
706 off_t offset = pair.second;
707 int count = pair.first;
708
709 os << "+" << offset << ":" << count << ", ";
710 }
711 os << "\n";
712
713 os << " field contents:\n";
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700714 const auto& dirty_objects_list = class_data[klass].dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800715 for (mirror::Object* obj : dirty_objects_list) {
716 // remote class object
717 auto remote_klass = reinterpret_cast<mirror::Class*>(obj);
718
719 // local class object
720 auto local_klass = RemoteContentsPointerToLocal(remote_klass,
721 remote_contents,
722 boot_image_header);
723
724 os << " " << reinterpret_cast<void*>(obj) << " ";
725 os << " class_status (remote): " << remote_klass->GetStatus() << ", ";
726 os << " class_status (local): " << local_klass->GetStatus();
727 os << "\n";
728 }
729 }
730 }
731
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700732 auto false_dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>(
733 class_data, [](const ClassData& d) { return d.false_dirty_object_count; });
Igor Murashkin37743352014-11-13 14:38:00 -0800734
735 os << "\n" << " False-dirty object count by class:\n";
736 for (const auto& vk_pair : false_dirty_object_class_values) {
737 int object_count = vk_pair.first;
738 mirror::Class* klass = vk_pair.second;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700739 int object_sizes = class_data[klass].false_dirty_byte_count;
Igor Murashkin37743352014-11-13 14:38:00 -0800740 float avg_object_size = object_sizes * 1.0f / object_count;
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700741 const std::string& descriptor = class_data[klass].descriptor;
Igor Murashkin37743352014-11-13 14:38:00 -0800742 os << " " << PrettyClass(klass) << " ("
743 << "objects: " << object_count << ", "
744 << "avg object size: " << avg_object_size << ", "
745 << "total bytes: " << object_sizes << ", "
746 << "class descriptor: '" << descriptor << "'"
747 << ")\n";
748
749 if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700750 auto& art_method_false_dirty_objects = class_data[klass].false_dirty_objects;
Igor Murashkin37743352014-11-13 14:38:00 -0800751
752 os << " field contents:\n";
753 for (mirror::Object* obj : art_method_false_dirty_objects) {
754 // local method
Mathieu Chartiere401d142015-04-22 13:56:20 -0700755 auto art_method = reinterpret_cast<ArtMethod*>(obj);
Igor Murashkin37743352014-11-13 14:38:00 -0800756
757 // local class
758 mirror::Class* declaring_class = art_method->GetDeclaringClass();
759
760 os << " " << reinterpret_cast<void*>(obj) << " ";
761 os << " entryPointFromJni: "
762 << reinterpret_cast<const void*>(
763 art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
Igor Murashkin37743352014-11-13 14:38:00 -0800764 os << " entryPointFromQuickCompiledCode: "
765 << reinterpret_cast<const void*>(
766 art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
767 << ", ";
768 os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
769 os << " class_status (local): " << declaring_class->GetStatus();
770 os << "\n";
771 }
772 }
773 }
774
775 os << "\n" << " Clean object count by class:\n";
776 for (const auto& vk_pair : clean_object_class_values) {
777 os << " " << PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
778 }
779
780 return true;
781 }
782
783 // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
784 // Returned pointer will point to inside of remote_contents.
785 template <typename T>
786 static T* FixUpRemotePointer(T* remote_ptr,
787 std::vector<uint8_t>& remote_contents,
788 const backtrace_map_t& boot_map) {
789 if (remote_ptr == nullptr) {
790 return nullptr;
791 }
792
793 uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr);
794
795 CHECK_LE(boot_map.start, remote);
796 CHECK_GT(boot_map.end, remote);
797
798 off_t boot_offset = remote - boot_map.start;
799
800 return reinterpret_cast<T*>(&remote_contents[boot_offset]);
801 }
802
803 template <typename T>
804 static T* RemoteContentsPointerToLocal(T* remote_ptr,
805 std::vector<uint8_t>& remote_contents,
806 const ImageHeader& image_header) {
807 if (remote_ptr == nullptr) {
808 return nullptr;
809 }
810
811 uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr);
812 ptrdiff_t boot_offset = remote - &remote_contents[0];
813
814 const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
815
816 return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
817 }
818
819 static std::string GetClassDescriptor(mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700820 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin37743352014-11-13 14:38:00 -0800821 CHECK(klass != nullptr);
822
823 std::string descriptor;
824 const char* descriptor_str = klass->GetDescriptor(&descriptor);
825
826 return std::string(descriptor_str);
827 }
828
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700829 template <typename K, typename V, typename D>
830 static std::vector<std::pair<V, K>> SortByValueDesc(
831 const std::map<K, D> map,
832 std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) {
Igor Murashkin37743352014-11-13 14:38:00 -0800833 // Store value->key so that we can use the default sort from pair which
834 // sorts by value first and then key
835 std::vector<std::pair<V, K>> value_key_vector;
836
837 for (const auto& kv_pair : map) {
Andreas Gampe7ad71d02016-04-04 13:49:18 -0700838 value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first));
Igor Murashkin37743352014-11-13 14:38:00 -0800839 }
840
841 // Sort in reverse (descending order)
842 std::sort(value_key_vector.rbegin(), value_key_vector.rend());
843 return value_key_vector;
844 }
845
846 static bool GetPageFrameNumber(File* page_map_file,
847 size_t virtual_page_index,
848 uint64_t* page_frame_number,
849 std::string* error_msg) {
850 CHECK(page_map_file != nullptr);
851 CHECK(page_frame_number != nullptr);
852 CHECK(error_msg != nullptr);
853
854 constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
855 constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; // bits 0-54 [in /proc/$pid/pagemap]
856 constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55); // bit 55 [in /proc/$pid/pagemap]
857
858 uint64_t page_map_entry = 0;
859
860 // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number
861 if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize,
862 virtual_page_index * kPageMapEntrySize)) {
863 *error_msg = StringPrintf("Failed to read the virtual page index entry from %s",
864 page_map_file->GetPath().c_str());
865 return false;
866 }
867
868 // TODO: seems useless, remove this.
869 bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0;
870 if ((false)) {
871 LOG(VERBOSE) << soft_dirty; // Suppress unused warning
872 UNREACHABLE();
873 }
874
875 *page_frame_number = page_map_entry & kPageFrameNumberMask;
876
877 return true;
878 }
879
880 static int IsPageDirty(File* page_map_file,
881 File* clean_page_map_file,
882 File* kpage_flags_file,
883 File* kpage_count_file,
884 size_t virtual_page_idx,
885 size_t clean_virtual_page_idx,
886 // Out parameters:
887 uint64_t* page_count, std::string* error_msg) {
888 CHECK(page_map_file != nullptr);
889 CHECK(clean_page_map_file != nullptr);
890 CHECK_NE(page_map_file, clean_page_map_file);
891 CHECK(kpage_flags_file != nullptr);
892 CHECK(kpage_count_file != nullptr);
893 CHECK(page_count != nullptr);
894 CHECK(error_msg != nullptr);
895
896 // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
897
898 constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
899 constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
900 constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags
901 constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags
902 constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags
903
904 uint64_t page_frame_number = 0;
905 if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
906 return -1;
907 }
908
909 uint64_t page_frame_number_clean = 0;
910 if (!GetPageFrameNumber(clean_page_map_file, clean_virtual_page_idx, &page_frame_number_clean,
911 error_msg)) {
912 return -1;
913 }
914
915 // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
916 uint64_t kpage_flags_entry = 0;
917 if (!kpage_flags_file->PreadFully(&kpage_flags_entry,
918 kPageFlagsEntrySize,
919 page_frame_number * kPageFlagsEntrySize)) {
920 *error_msg = StringPrintf("Failed to read the page flags from %s",
921 kpage_flags_file->GetPath().c_str());
922 return -1;
923 }
924
925 // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
926 if (!kpage_count_file->PreadFully(page_count /*out*/,
927 kPageCountEntrySize,
928 page_frame_number * kPageCountEntrySize)) {
929 *error_msg = StringPrintf("Failed to read the page count from %s",
930 kpage_count_file->GetPath().c_str());
931 return -1;
932 }
933
934 // There must be a page frame at the requested address.
935 CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
936 // The page frame must be memory mapped
937 CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
938
939 // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
940 bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
941
942 // page_frame_number_clean must come from the *same* process
943 // but a *different* mmap than page_frame_number
944 if (flags_dirty) {
945 CHECK_NE(page_frame_number, page_frame_number_clean);
946 }
947
948 return page_frame_number != page_frame_number_clean;
949 }
950
Igor Murashkin37743352014-11-13 14:38:00 -0800951 private:
952 // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
953 std::string GetImageLocationBaseName() const {
954 return BaseName(std::string(image_location_));
955 }
956
957 std::ostream* os_;
958 const ImageHeader& image_header_;
Andreas Gampe8994a042015-12-30 19:03:17 +0000959 const std::string image_location_;
Igor Murashkin37743352014-11-13 14:38:00 -0800960 pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative
961
962 DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
963};
964
Jeff Haodcdc85b2015-12-04 14:06:18 -0800965static int DumpImage(Runtime* runtime, std::ostream* os, pid_t image_diff_pid) {
Igor Murashkin37743352014-11-13 14:38:00 -0800966 ScopedObjectAccess soa(Thread::Current());
967 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800968 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
969 CHECK(!image_spaces.empty());
970 for (gc::space::ImageSpace* image_space : image_spaces) {
971 const ImageHeader& image_header = image_space->GetImageHeader();
972 if (!image_header.IsValid()) {
973 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
974 return EXIT_FAILURE;
975 }
976
977 ImgDiagDumper img_diag_dumper(
Andreas Gampe8994a042015-12-30 19:03:17 +0000978 os, image_header, image_space->GetImageLocation(), image_diff_pid);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800979 if (!img_diag_dumper.Dump()) {
980 return EXIT_FAILURE;
981 }
Igor Murashkin37743352014-11-13 14:38:00 -0800982 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800983 return EXIT_SUCCESS;
Igor Murashkin37743352014-11-13 14:38:00 -0800984}
985
986struct ImgDiagArgs : public CmdlineArgs {
987 protected:
988 using Base = CmdlineArgs;
989
990 virtual ParseStatus ParseCustom(const StringPiece& option,
991 std::string* error_msg) OVERRIDE {
992 {
993 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
994 if (base_parse != kParseUnknownArgument) {
995 return base_parse;
996 }
997 }
998
999 if (option.starts_with("--image-diff-pid=")) {
1000 const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data();
1001
1002 if (!ParseInt(image_diff_pid, &image_diff_pid_)) {
1003 *error_msg = "Image diff pid out of range";
1004 return kParseError;
1005 }
1006 } else {
1007 return kParseUnknownArgument;
1008 }
1009
1010 return kParseOk;
1011 }
1012
1013 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
1014 // Perform the parent checks.
1015 ParseStatus parent_checks = Base::ParseChecks(error_msg);
1016 if (parent_checks != kParseOk) {
1017 return parent_checks;
1018 }
1019
1020 // Perform our own checks.
1021
1022 if (kill(image_diff_pid_,
1023 /*sig*/0) != 0) { // No signal is sent, perform error-checking only.
1024 // Check if the pid exists before proceeding.
1025 if (errno == ESRCH) {
1026 *error_msg = "Process specified does not exist";
1027 } else {
1028 *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
1029 }
1030 return kParseError;
1031 } else if (instruction_set_ != kRuntimeISA) {
1032 // Don't allow different ISAs since the images are ISA-specific.
1033 // Right now the code assumes both the runtime ISA and the remote ISA are identical.
1034 *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
1035 return kParseError;
1036 }
1037
1038 return kParseOk;
1039 }
1040
1041 virtual std::string GetUsage() const {
1042 std::string usage;
1043
1044 usage +=
1045 "Usage: imgdiag [options] ...\n"
1046 " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
1047 " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
1048 "\n";
1049
1050 usage += Base::GetUsage();
1051
1052 usage += // Optional.
1053 " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
1054 " Example: --image-diff-pid=$(pid zygote)\n"
1055 "\n";
1056
1057 return usage;
1058 }
1059
1060 public:
1061 pid_t image_diff_pid_ = -1;
1062};
1063
1064struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
1065 virtual bool ExecuteWithRuntime(Runtime* runtime) {
1066 CHECK(args_ != nullptr);
1067
1068 return DumpImage(runtime,
Igor Murashkin37743352014-11-13 14:38:00 -08001069 args_->os_,
1070 args_->image_diff_pid_) == EXIT_SUCCESS;
1071 }
1072};
1073
1074} // namespace art
1075
1076int main(int argc, char** argv) {
1077 art::ImgDiagMain main;
1078 return main.Main(argc, argv);
1079}