Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <fstream> |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 21 | #include <functional> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | #include <set> |
| 26 | #include <map> |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 27 | #include <unordered_set> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 28 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 29 | #include "android-base/stringprintf.h" |
| 30 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 31 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 32 | #include "art_method-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 33 | #include "base/unix_file/fd_file.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 34 | #include "gc/space/image_space.h" |
| 35 | #include "gc/heap.h" |
| 36 | #include "mirror/class-inl.h" |
| 37 | #include "mirror/object-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 38 | #include "image.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 39 | #include "scoped_thread_state_change-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 40 | #include "os.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 41 | |
| 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 | |
| 49 | namespace art { |
| 50 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 51 | using android::base::StringPrintf; |
| 52 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 53 | class ImgDiagDumper { |
| 54 | public: |
| 55 | explicit ImgDiagDumper(std::ostream* os, |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 56 | const ImageHeader& image_header, |
| 57 | const std::string& image_location, |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 58 | pid_t image_diff_pid, |
| 59 | pid_t zygote_diff_pid) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 60 | : os_(os), |
| 61 | image_header_(image_header), |
| 62 | image_location_(image_location), |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 63 | image_diff_pid_(image_diff_pid), |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 64 | zygote_diff_pid_(zygote_diff_pid), |
| 65 | zygote_pid_only_(false) {} |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 66 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 67 | bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 68 | std::ostream& os = *os_; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 69 | os << "IMAGE LOCATION: " << image_location_ << "\n\n"; |
| 70 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 71 | os << "MAGIC: " << image_header_.GetMagic() << "\n\n"; |
| 72 | |
| 73 | os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; |
| 74 | |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 75 | PrintPidLine("IMAGE", image_diff_pid_); |
| 76 | os << "\n\n"; |
| 77 | PrintPidLine("ZYGOTE", zygote_diff_pid_); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 78 | bool ret = true; |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 79 | if (image_diff_pid_ >= 0 || zygote_diff_pid_ >= 0) { |
| 80 | if (image_diff_pid_ < 0) { |
| 81 | image_diff_pid_ = zygote_diff_pid_; |
| 82 | zygote_diff_pid_ = -1; |
| 83 | zygote_pid_only_ = true; |
| 84 | } |
| 85 | ret = DumpImageDiff(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 86 | os << "\n\n"; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | os << std::flush; |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | private: |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 95 | void PrintPidLine(const std::string& kind, pid_t pid) { |
| 96 | if (pid < 0) { |
| 97 | *os_ << kind << " DIFF PID: disabled\n\n"; |
| 98 | } else { |
| 99 | *os_ << kind << " DIFF PID (" << pid << "): "; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static bool EndsWith(const std::string& str, const std::string& suffix) { |
| 104 | return str.size() >= suffix.size() && |
| 105 | str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; |
| 106 | } |
| 107 | |
| 108 | // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar) |
| 109 | static std::string BaseName(const std::string& str) { |
| 110 | size_t idx = str.rfind('/'); |
| 111 | if (idx == std::string::npos) { |
| 112 | return str; |
| 113 | } |
| 114 | |
| 115 | return str.substr(idx + 1); |
| 116 | } |
| 117 | |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 118 | bool DumpImageDiff() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 119 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 120 | std::ostream& os = *os_; |
| 121 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 122 | { |
| 123 | struct stat sts; |
| 124 | std::string proc_pid_str = |
| 125 | StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int] |
| 126 | if (stat(proc_pid_str.c_str(), &sts) == -1) { |
| 127 | os << "Process does not exist"; |
| 128 | return false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 132 | // Open /proc/$pid/maps to view memory maps |
| 133 | auto proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid_)); |
| 134 | if (proc_maps == nullptr) { |
| 135 | os << "Could not read backtrace maps"; |
| 136 | return false; |
| 137 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 138 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 139 | bool found_boot_map = false; |
| 140 | backtrace_map_t boot_map = backtrace_map_t(); |
| 141 | // Find the memory map only for boot.art |
| 142 | for (const backtrace_map_t& map : *proc_maps) { |
| 143 | if (EndsWith(map.name, GetImageLocationBaseName())) { |
| 144 | if ((map.flags & PROT_WRITE) != 0) { |
| 145 | boot_map = map; |
| 146 | found_boot_map = true; |
| 147 | break; |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 148 | } |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 149 | // In actuality there's more than 1 map, but the second one is read-only. |
| 150 | // The one we care about is the write-able map. |
| 151 | // The readonly maps are guaranteed to be identical, so its not interesting to compare |
| 152 | // them. |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 155 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 156 | if (!found_boot_map) { |
| 157 | os << "Could not find map for " << GetImageLocationBaseName(); |
| 158 | return false; |
| 159 | } |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 160 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 161 | // Future idea: diff against zygote so we can ignore the shared dirty pages. |
| 162 | return DumpImageDiffMap(boot_map); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 165 | static std::string PrettyFieldValue(ArtField* field, mirror::Object* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 166 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 167 | std::ostringstream oss; |
| 168 | switch (field->GetTypeAsPrimitiveType()) { |
| 169 | case Primitive::kPrimNot: { |
| 170 | oss << obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 171 | field->GetOffset()); |
| 172 | break; |
| 173 | } |
| 174 | case Primitive::kPrimBoolean: { |
| 175 | oss << static_cast<bool>(obj->GetFieldBoolean<kVerifyNone>(field->GetOffset())); |
| 176 | break; |
| 177 | } |
| 178 | case Primitive::kPrimByte: { |
| 179 | oss << static_cast<int32_t>(obj->GetFieldByte<kVerifyNone>(field->GetOffset())); |
| 180 | break; |
| 181 | } |
| 182 | case Primitive::kPrimChar: { |
| 183 | oss << obj->GetFieldChar<kVerifyNone>(field->GetOffset()); |
| 184 | break; |
| 185 | } |
| 186 | case Primitive::kPrimShort: { |
| 187 | oss << obj->GetFieldShort<kVerifyNone>(field->GetOffset()); |
| 188 | break; |
| 189 | } |
| 190 | case Primitive::kPrimInt: { |
| 191 | oss << obj->GetField32<kVerifyNone>(field->GetOffset()); |
| 192 | break; |
| 193 | } |
| 194 | case Primitive::kPrimLong: { |
| 195 | oss << obj->GetField64<kVerifyNone>(field->GetOffset()); |
| 196 | break; |
| 197 | } |
| 198 | case Primitive::kPrimFloat: { |
| 199 | oss << obj->GetField32<kVerifyNone>(field->GetOffset()); |
| 200 | break; |
| 201 | } |
| 202 | case Primitive::kPrimDouble: { |
| 203 | oss << obj->GetField64<kVerifyNone>(field->GetOffset()); |
| 204 | break; |
| 205 | } |
| 206 | case Primitive::kPrimVoid: { |
| 207 | oss << "void"; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | return oss.str(); |
| 212 | } |
| 213 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 214 | // Aggregate and detail class data from an image diff. |
| 215 | struct ClassData { |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 216 | int dirty_object_count = 0; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 217 | |
| 218 | // Track only the byte-per-byte dirtiness (in bytes) |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 219 | int dirty_object_byte_count = 0; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 220 | |
| 221 | // Track the object-by-object dirtiness (in bytes) |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 222 | int dirty_object_size_in_bytes = 0; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 223 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 224 | int clean_object_count = 0; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 225 | |
| 226 | std::string descriptor; |
| 227 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 228 | int false_dirty_byte_count = 0; |
| 229 | int false_dirty_object_count = 0; |
| 230 | std::vector<mirror::Object*> false_dirty_objects; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 231 | |
| 232 | // Remote pointers to dirty objects |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 233 | std::vector<mirror::Object*> dirty_objects; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 236 | void DiffObjectContents(mirror::Object* obj, |
| 237 | uint8_t* remote_bytes, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 238 | std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 239 | const char* tabs = " "; |
| 240 | // Attempt to find fields for all dirty bytes. |
| 241 | mirror::Class* klass = obj->GetClass(); |
| 242 | if (obj->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 243 | os << tabs << "Class " << mirror::Class::PrettyClass(obj->AsClass()) << " " << obj << "\n"; |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 244 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 245 | os << tabs << "Instance of " << mirror::Class::PrettyClass(klass) << " " << obj << "\n"; |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | std::unordered_set<ArtField*> dirty_instance_fields; |
| 249 | std::unordered_set<ArtField*> dirty_static_fields; |
| 250 | const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj); |
| 251 | mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(remote_bytes); |
| 252 | for (size_t i = 0, count = obj->SizeOf(); i < count; ++i) { |
| 253 | if (obj_bytes[i] != remote_bytes[i]) { |
| 254 | ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i); |
| 255 | if (field != nullptr) { |
| 256 | dirty_instance_fields.insert(field); |
| 257 | } else if (obj->IsClass()) { |
| 258 | field = ArtField::FindStaticFieldWithOffset</*exact*/false>(obj->AsClass(), i); |
| 259 | if (field != nullptr) { |
| 260 | dirty_static_fields.insert(field); |
| 261 | } |
| 262 | } |
| 263 | if (field == nullptr) { |
| 264 | if (klass->IsArrayClass()) { |
| 265 | mirror::Class* component_type = klass->GetComponentType(); |
| 266 | Primitive::Type primitive_type = component_type->GetPrimitiveType(); |
| 267 | size_t component_size = Primitive::ComponentSize(primitive_type); |
| 268 | size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value(); |
| 269 | if (i >= data_offset) { |
| 270 | os << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n"; |
| 271 | // Skip to next element to prevent spam. |
| 272 | i += component_size - 1; |
| 273 | continue; |
| 274 | } |
| 275 | } |
| 276 | os << tabs << "No field for byte offset " << i << "\n"; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | // Dump different fields. TODO: Dump field contents. |
| 281 | if (!dirty_instance_fields.empty()) { |
| 282 | os << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n"; |
| 283 | for (ArtField* field : dirty_instance_fields) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 284 | os << tabs << ArtField::PrettyField(field) |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 285 | << " original=" << PrettyFieldValue(field, obj) |
| 286 | << " remote=" << PrettyFieldValue(field, remote_obj) << "\n"; |
| 287 | } |
| 288 | } |
| 289 | if (!dirty_static_fields.empty()) { |
| 290 | os << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n"; |
| 291 | for (ArtField* field : dirty_static_fields) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 292 | os << tabs << ArtField::PrettyField(field) |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 293 | << " original=" << PrettyFieldValue(field, obj) |
| 294 | << " remote=" << PrettyFieldValue(field, remote_obj) << "\n"; |
| 295 | } |
| 296 | } |
| 297 | os << "\n"; |
| 298 | } |
| 299 | |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 300 | // Look at /proc/$pid/mem and only diff the things from there |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 301 | bool DumpImageDiffMap(const backtrace_map_t& boot_map) |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 302 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 303 | std::ostream& os = *os_; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 304 | const PointerSize pointer_size = InstructionSetPointerSize( |
| 305 | Runtime::Current()->GetInstructionSet()); |
| 306 | |
| 307 | std::string file_name = |
| 308 | StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int] |
| 309 | |
| 310 | size_t boot_map_size = boot_map.end - boot_map.start; |
| 311 | |
| 312 | // Open /proc/$pid/mem as a file |
| 313 | auto map_file = std::unique_ptr<File>(OS::OpenFileForReading(file_name.c_str())); |
| 314 | if (map_file == nullptr) { |
| 315 | os << "Failed to open " << file_name << " for reading"; |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | // Memory-map /proc/$pid/mem subset from the boot map |
| 320 | CHECK(boot_map.end >= boot_map.start); |
| 321 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 322 | std::string error_msg; |
| 323 | |
| 324 | // Walk the bytes and diff against our boot image |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 325 | const ImageHeader& boot_image_header = image_header_; |
| 326 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 327 | os << "\nObserving boot image header at address " |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 328 | << reinterpret_cast<const void*>(&boot_image_header) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 329 | << "\n\n"; |
| 330 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 331 | const uint8_t* image_begin_unaligned = boot_image_header.GetImageBegin(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 332 | const uint8_t* image_mirror_end_unaligned = image_begin_unaligned + |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 333 | boot_image_header.GetImageSection(ImageHeader::kSectionObjects).Size(); |
| 334 | const uint8_t* image_end_unaligned = image_begin_unaligned + boot_image_header.GetImageSize(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 335 | |
| 336 | // Adjust range to nearest page |
| 337 | const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize); |
| 338 | const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize); |
| 339 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 340 | ptrdiff_t page_off_begin = boot_image_header.GetImageBegin() - image_begin; |
| 341 | |
| 342 | if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start || |
| 343 | reinterpret_cast<uintptr_t>(image_end) < boot_map.end) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 344 | // Sanity check that we aren't trying to read a completely different boot image |
| 345 | os << "Remote boot map is out of range of local boot map: " << |
| 346 | "local begin " << reinterpret_cast<const void*>(image_begin) << |
| 347 | ", local end " << reinterpret_cast<const void*>(image_end) << |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 348 | ", remote begin " << reinterpret_cast<const void*>(boot_map.start) << |
| 349 | ", remote end " << reinterpret_cast<const void*>(boot_map.end); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 350 | return false; |
| 351 | // If we wanted even more validation we could map the ImageHeader from the file |
| 352 | } |
| 353 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 354 | std::vector<uint8_t> remote_contents(boot_map_size); |
| 355 | if (!map_file->PreadFully(&remote_contents[0], boot_map_size, boot_map.start)) { |
| 356 | os << "Could not fully read file " << file_name; |
| 357 | return false; |
| 358 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 359 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 360 | std::vector<uint8_t> zygote_contents; |
| 361 | std::unique_ptr<File> zygote_map_file; |
| 362 | if (zygote_diff_pid_ != -1) { |
| 363 | std::string zygote_file_name = |
| 364 | StringPrintf("/proc/%ld/mem", static_cast<long>(zygote_diff_pid_)); // NOLINT [runtime/int] |
| 365 | zygote_map_file.reset(OS::OpenFileForReading(zygote_file_name.c_str())); |
| 366 | // The boot map should be at the same address. |
| 367 | zygote_contents.resize(boot_map_size); |
| 368 | if (!zygote_map_file->PreadFully(&zygote_contents[0], boot_map_size, boot_map.start)) { |
| 369 | LOG(WARNING) << "Could not fully read zygote file " << zygote_file_name; |
| 370 | zygote_contents.clear(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | std::string page_map_file_name = StringPrintf( |
| 375 | "/proc/%ld/pagemap", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int] |
| 376 | auto page_map_file = std::unique_ptr<File>(OS::OpenFileForReading(page_map_file_name.c_str())); |
| 377 | if (page_map_file == nullptr) { |
| 378 | os << "Failed to open " << page_map_file_name << " for reading: " << strerror(errno); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough |
| 383 | const char* clean_page_map_file_name = "/proc/self/pagemap"; |
| 384 | auto clean_page_map_file = std::unique_ptr<File>( |
| 385 | OS::OpenFileForReading(clean_page_map_file_name)); |
| 386 | if (clean_page_map_file == nullptr) { |
| 387 | os << "Failed to open " << clean_page_map_file_name << " for reading: " << strerror(errno); |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | auto kpage_flags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags")); |
| 392 | if (kpage_flags_file == nullptr) { |
| 393 | os << "Failed to open /proc/kpageflags for reading: " << strerror(errno); |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | auto kpage_count_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount")); |
| 398 | if (kpage_count_file == nullptr) { |
| 399 | os << "Failed to open /proc/kpagecount for reading:" << strerror(errno); |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | // Set of the remote virtual page indices that are dirty |
| 404 | std::set<size_t> dirty_page_set_remote; |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 405 | // Set of the local virtual page indices that are dirty |
| 406 | std::set<size_t> dirty_page_set_local; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 407 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 408 | size_t different_int32s = 0; |
| 409 | size_t different_bytes = 0; |
| 410 | size_t different_pages = 0; |
| 411 | size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address) |
| 412 | size_t page_idx = 0; // Page index relative to 0 |
| 413 | size_t previous_page_idx = 0; // Previous page index relative to 0 |
| 414 | size_t dirty_pages = 0; |
| 415 | size_t private_pages = 0; |
| 416 | size_t private_dirty_pages = 0; |
| 417 | |
| 418 | // Iterate through one page at a time. Boot map begin/end already implicitly aligned. |
| 419 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) { |
| 420 | ptrdiff_t offset = begin - boot_map.start; |
| 421 | |
| 422 | // We treat the image header as part of the memory map for now |
| 423 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 424 | // But it might still be interesting to see if any of the ImageHeader data mutated |
| 425 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset; |
| 426 | uint8_t* remote_ptr = &remote_contents[offset]; |
| 427 | |
| 428 | if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) { |
| 429 | different_pages++; |
| 430 | |
| 431 | // Count the number of 32-bit integers that are different. |
| 432 | for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) { |
| 433 | uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr); |
| 434 | const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr); |
| 435 | |
| 436 | if (remote_ptr_int32[i] != local_ptr_int32[i]) { |
| 437 | different_int32s++; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Iterate through one byte at a time. |
| 444 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) { |
| 445 | previous_page_idx = page_idx; |
| 446 | ptrdiff_t offset = begin - boot_map.start; |
| 447 | |
| 448 | // We treat the image header as part of the memory map for now |
| 449 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 450 | // But it might still be interesting to see if any of the ImageHeader data mutated |
| 451 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset; |
| 452 | uint8_t* remote_ptr = &remote_contents[offset]; |
| 453 | |
| 454 | virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize; |
| 455 | |
| 456 | // Calculate the page index, relative to the 0th page where the image begins |
| 457 | page_idx = (offset + page_off_begin) / kPageSize; |
| 458 | if (*local_ptr != *remote_ptr) { |
| 459 | // Track number of bytes that are different |
| 460 | different_bytes++; |
| 461 | } |
| 462 | |
| 463 | // Independently count the # of dirty pages on the remote side |
| 464 | size_t remote_virtual_page_idx = begin / kPageSize; |
| 465 | if (previous_page_idx != page_idx) { |
| 466 | uint64_t page_count = 0xC0FFEE; |
| 467 | // TODO: virtual_page_idx needs to be from the same process |
| 468 | int dirtiness = (IsPageDirty(page_map_file.get(), // Image-diff-pid procmap |
| 469 | clean_page_map_file.get(), // Self procmap |
| 470 | kpage_flags_file.get(), |
| 471 | kpage_count_file.get(), |
| 472 | remote_virtual_page_idx, // potentially "dirty" page |
| 473 | virtual_page_idx, // true "clean" page |
| 474 | &page_count, |
| 475 | &error_msg)); |
| 476 | if (dirtiness < 0) { |
| 477 | os << error_msg; |
| 478 | return false; |
| 479 | } else if (dirtiness > 0) { |
| 480 | dirty_pages++; |
| 481 | dirty_page_set_remote.insert(dirty_page_set_remote.end(), remote_virtual_page_idx); |
| 482 | dirty_page_set_local.insert(dirty_page_set_local.end(), virtual_page_idx); |
| 483 | } |
| 484 | |
| 485 | bool is_dirty = dirtiness > 0; |
| 486 | bool is_private = page_count == 1; |
| 487 | |
| 488 | if (page_count == 1) { |
| 489 | private_pages++; |
| 490 | } |
| 491 | |
| 492 | if (is_dirty && is_private) { |
| 493 | private_dirty_pages++; |
| 494 | } |
| 495 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 498 | std::map<mirror::Class*, ClassData> class_data; |
| 499 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 500 | // Walk each object in the remote image space and compare it against ours |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 501 | size_t different_objects = 0; |
| 502 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 503 | std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 504 | std::vector<ArtMethod*> art_method_dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 505 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 506 | std::map<off_t /* field offset */, int /* count */> class_field_dirty_count; |
| 507 | std::vector<mirror::Class*> class_dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 508 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 509 | // List of local objects that are clean, but located on dirty pages. |
| 510 | std::vector<mirror::Object*> false_dirty_objects; |
| 511 | size_t false_dirty_object_bytes = 0; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 512 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 513 | // Look up remote classes by their descriptor |
| 514 | std::map<std::string, mirror::Class*> remote_class_map; |
| 515 | // Look up local classes by their descriptor |
| 516 | std::map<std::string, mirror::Class*> local_class_map; |
| 517 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 518 | // Image dirty objects |
| 519 | // If zygote_pid_only_ == true, these are shared dirty objects in the zygote. |
| 520 | // If zygote_pid_only_ == false, these are private dirty objects in the application. |
| 521 | std::set<mirror::Object*> image_dirty_objects; |
| 522 | |
| 523 | // Zygote dirty objects (probably private dirty). |
| 524 | // We only add objects here if they differed in both the image and the zygote, so |
| 525 | // they are probably private dirty. |
| 526 | std::set<mirror::Object*> zygote_dirty_objects; |
| 527 | |
| 528 | size_t dirty_object_bytes = 0; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 529 | const uint8_t* begin_image_ptr = image_begin_unaligned; |
| 530 | const uint8_t* end_image_ptr = image_mirror_end_unaligned; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 531 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 532 | const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 533 | while (reinterpret_cast<uintptr_t>(current) < reinterpret_cast<uintptr_t>(end_image_ptr)) { |
| 534 | CHECK_ALIGNED(current, kObjectAlignment); |
| 535 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 536 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 537 | // Sanity check that we are reading a real object |
| 538 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 539 | if (kUseBakerReadBarrier) { |
| 540 | obj->AssertReadBarrierState(); |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 541 | } |
| 542 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 543 | // Iterate every page this object belongs to |
| 544 | bool on_dirty_page = false; |
| 545 | size_t page_off = 0; |
| 546 | size_t current_page_idx; |
| 547 | uintptr_t object_address; |
| 548 | do { |
| 549 | object_address = reinterpret_cast<uintptr_t>(current); |
| 550 | current_page_idx = object_address / kPageSize + page_off; |
| 551 | |
| 552 | if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) { |
| 553 | // This object is on a dirty page |
| 554 | on_dirty_page = true; |
| 555 | } |
| 556 | |
| 557 | page_off++; |
| 558 | } while ((current_page_idx * kPageSize) < |
| 559 | RoundUp(object_address + obj->SizeOf(), kObjectAlignment)); |
| 560 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 561 | mirror::Class* klass = obj->GetClass(); |
| 562 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 563 | // Check against the other object and see if they are different |
| 564 | ptrdiff_t offset = current - begin_image_ptr; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 565 | const uint8_t* current_remote = &remote_contents[offset]; |
| 566 | mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>( |
| 567 | const_cast<uint8_t*>(current_remote)); |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 568 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 569 | bool different_image_object = memcmp(current, current_remote, obj->SizeOf()) != 0; |
| 570 | if (different_image_object) { |
| 571 | bool different_zygote_object = false; |
| 572 | if (!zygote_contents.empty()) { |
| 573 | const uint8_t* zygote_ptr = &zygote_contents[offset]; |
| 574 | different_zygote_object = memcmp(current, zygote_ptr, obj->SizeOf()) != 0; |
| 575 | } |
| 576 | if (different_zygote_object) { |
| 577 | // Different from zygote. |
| 578 | zygote_dirty_objects.insert(obj); |
| 579 | } else { |
| 580 | // Just different from image. |
| 581 | image_dirty_objects.insert(obj); |
| 582 | } |
| 583 | |
| 584 | different_objects++; |
| 585 | dirty_object_bytes += obj->SizeOf(); |
| 586 | |
| 587 | ++class_data[klass].dirty_object_count; |
| 588 | |
| 589 | // Go byte-by-byte and figure out what exactly got dirtied |
| 590 | size_t dirty_byte_count_per_object = 0; |
| 591 | for (size_t i = 0; i < obj->SizeOf(); ++i) { |
| 592 | if (current[i] != current_remote[i]) { |
| 593 | dirty_byte_count_per_object++; |
| 594 | } |
| 595 | } |
| 596 | class_data[klass].dirty_object_byte_count += dirty_byte_count_per_object; |
| 597 | class_data[klass].dirty_object_size_in_bytes += obj->SizeOf(); |
| 598 | class_data[klass].dirty_objects.push_back(remote_obj); |
| 599 | } else { |
| 600 | ++class_data[klass].clean_object_count; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 601 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 602 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 603 | std::string descriptor = GetClassDescriptor(klass); |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 604 | if (different_image_object) { |
| 605 | if (klass->IsClassClass()) { |
| 606 | // this is a "Class" |
| 607 | mirror::Class* obj_as_class = reinterpret_cast<mirror::Class*>(remote_obj); |
| 608 | |
| 609 | // print the fields that are dirty |
| 610 | for (size_t i = 0; i < obj->SizeOf(); ++i) { |
| 611 | if (current[i] != current_remote[i]) { |
| 612 | class_field_dirty_count[i]++; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | class_dirty_objects.push_back(obj_as_class); |
| 617 | } else if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) { |
| 618 | // this is an ArtMethod |
| 619 | ArtMethod* art_method = reinterpret_cast<ArtMethod*>(remote_obj); |
| 620 | |
| 621 | // print the fields that are dirty |
| 622 | for (size_t i = 0; i < obj->SizeOf(); ++i) { |
| 623 | if (current[i] != current_remote[i]) { |
| 624 | art_method_field_dirty_count[i]++; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | art_method_dirty_objects.push_back(art_method); |
| 629 | } |
| 630 | } else if (on_dirty_page) { |
| 631 | // This object was either never mutated or got mutated back to the same value. |
| 632 | // TODO: Do I want to distinguish a "different" vs a "dirty" page here? |
| 633 | false_dirty_objects.push_back(obj); |
| 634 | class_data[klass].false_dirty_objects.push_back(obj); |
| 635 | false_dirty_object_bytes += obj->SizeOf(); |
| 636 | class_data[obj->GetClass()].false_dirty_byte_count += obj->SizeOf(); |
| 637 | class_data[obj->GetClass()].false_dirty_object_count += 1; |
| 638 | } |
| 639 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 640 | if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) { |
| 641 | local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj); |
| 642 | remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj); |
| 643 | } |
| 644 | |
| 645 | // Unconditionally store the class descriptor in case we need it later |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 646 | class_data[klass].descriptor = descriptor; |
| 647 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects. |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 651 | float true_dirtied_percent = dirty_object_bytes * 1.0f / (dirty_pages * kPageSize); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 652 | size_t false_dirty_pages = dirty_pages - different_pages; |
| 653 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 654 | os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", " |
| 655 | << reinterpret_cast<void*>(boot_map.end) << ") had: \n " |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 656 | << different_bytes << " differing bytes, \n " |
| 657 | << different_int32s << " differing int32s, \n " |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 658 | << different_objects << " different objects, \n " |
| 659 | << dirty_object_bytes << " different object [bytes], \n " |
| 660 | << false_dirty_objects.size() << " false dirty objects,\n " |
| 661 | << false_dirty_object_bytes << " false dirty object [bytes], \n " |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 662 | << true_dirtied_percent << " different objects-vs-total in a dirty page;\n " |
| 663 | << different_pages << " different pages; \n " |
| 664 | << dirty_pages << " pages are dirty; \n " |
| 665 | << false_dirty_pages << " pages are false dirty; \n " |
| 666 | << private_pages << " pages are private; \n " |
| 667 | << private_dirty_pages << " pages are Private_Dirty\n " |
| 668 | << ""; |
| 669 | |
| 670 | // vector of pairs (int count, Class*) |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 671 | auto dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>( |
| 672 | class_data, [](const ClassData& d) { return d.dirty_object_count; }); |
| 673 | auto clean_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>( |
| 674 | class_data, [](const ClassData& d) { return d.clean_object_count; }); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 675 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 676 | if (!zygote_dirty_objects.empty()) { |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 677 | // We only reach this point if both pids were specified. Furthermore, |
| 678 | // objects are only displayed here if they differed in both the image |
| 679 | // and the zygote, so they are probably private dirty. |
| 680 | CHECK(image_diff_pid_ > 0 && zygote_diff_pid_ > 0); |
| 681 | os << "\n" << " Zygote dirty objects (probably shared dirty): " |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 682 | << zygote_dirty_objects.size() << "\n"; |
| 683 | for (mirror::Object* obj : zygote_dirty_objects) { |
| 684 | const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 685 | ptrdiff_t offset = obj_bytes - begin_image_ptr; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 686 | uint8_t* remote_bytes = &zygote_contents[offset]; |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 687 | DiffObjectContents(obj, remote_bytes, os); |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 688 | } |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 689 | } |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 690 | os << "\n"; |
| 691 | if (zygote_pid_only_) { |
David Sehr | 41d8eee | 2017-06-15 11:11:32 -0700 | [diff] [blame] | 692 | // image_diff_pid_ is the zygote process. |
| 693 | os << " Zygote shared dirty objects: "; |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 694 | } else { |
David Sehr | 41d8eee | 2017-06-15 11:11:32 -0700 | [diff] [blame] | 695 | // image_diff_pid_ is actually the image (application) process. |
| 696 | if (zygote_diff_pid_ > 0) { |
| 697 | os << " Application dirty objects (private dirty): "; |
| 698 | } else { |
| 699 | os << " Application dirty objects (unknown whether private or shared dirty): "; |
| 700 | } |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 701 | } |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 702 | os << image_dirty_objects.size() << "\n"; |
| 703 | for (mirror::Object* obj : image_dirty_objects) { |
| 704 | const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 705 | ptrdiff_t offset = obj_bytes - begin_image_ptr; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 706 | uint8_t* remote_bytes = &remote_contents[offset]; |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 707 | DiffObjectContents(obj, remote_bytes, os); |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 710 | os << "\n" << " Dirty object count by class:\n"; |
| 711 | for (const auto& vk_pair : dirty_object_class_values) { |
| 712 | int dirty_object_count = vk_pair.first; |
| 713 | mirror::Class* klass = vk_pair.second; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 714 | int object_sizes = class_data[klass].dirty_object_size_in_bytes; |
| 715 | float avg_dirty_bytes_per_class = |
| 716 | class_data[klass].dirty_object_byte_count * 1.0f / object_sizes; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 717 | float avg_object_size = object_sizes * 1.0f / dirty_object_count; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 718 | const std::string& descriptor = class_data[klass].descriptor; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 719 | os << " " << mirror::Class::PrettyClass(klass) << " (" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 720 | << "objects: " << dirty_object_count << ", " |
| 721 | << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", " |
| 722 | << "avg object size: " << avg_object_size << ", " |
| 723 | << "class descriptor: '" << descriptor << "'" |
| 724 | << ")\n"; |
| 725 | |
| 726 | constexpr size_t kMaxAddressPrint = 5; |
| 727 | if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) { |
| 728 | os << " sample object addresses: "; |
| 729 | for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) { |
| 730 | auto art_method = art_method_dirty_objects[i]; |
| 731 | |
| 732 | os << reinterpret_cast<void*>(art_method) << ", "; |
| 733 | } |
| 734 | os << "\n"; |
| 735 | |
| 736 | os << " dirty byte +offset:count list = "; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 737 | auto art_method_field_dirty_count_sorted = |
| 738 | SortByValueDesc<off_t, int, int>(art_method_field_dirty_count); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 739 | for (auto pair : art_method_field_dirty_count_sorted) { |
| 740 | off_t offset = pair.second; |
| 741 | int count = pair.first; |
| 742 | |
| 743 | os << "+" << offset << ":" << count << ", "; |
| 744 | } |
| 745 | |
| 746 | os << "\n"; |
| 747 | |
| 748 | os << " field contents:\n"; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 749 | const auto& dirty_objects_list = class_data[klass].dirty_objects; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 750 | for (mirror::Object* obj : dirty_objects_list) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 751 | // remote method |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 752 | auto art_method = reinterpret_cast<ArtMethod*>(obj); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 753 | |
| 754 | // remote class |
| 755 | mirror::Class* remote_declaring_class = |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 756 | FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents, boot_map); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 757 | |
| 758 | // local class |
| 759 | mirror::Class* declaring_class = |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 760 | RemoteContentsPointerToLocal(remote_declaring_class, |
| 761 | remote_contents, |
| 762 | boot_image_header); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 763 | |
| 764 | os << " " << reinterpret_cast<void*>(obj) << " "; |
| 765 | os << " entryPointFromJni: " |
| 766 | << reinterpret_cast<const void*>( |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 767 | art_method->GetDataPtrSize(pointer_size)) << ", "; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 768 | os << " entryPointFromQuickCompiledCode: " |
| 769 | << reinterpret_cast<const void*>( |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 770 | art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 771 | << ", "; |
| 772 | os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", "; |
| 773 | os << " class_status (local): " << declaring_class->GetStatus(); |
| 774 | os << " class_status (remote): " << remote_declaring_class->GetStatus(); |
| 775 | os << "\n"; |
| 776 | } |
| 777 | } |
| 778 | if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) { |
| 779 | os << " sample object addresses: "; |
| 780 | for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) { |
| 781 | auto class_ptr = class_dirty_objects[i]; |
| 782 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 783 | os << reinterpret_cast<void*>(class_ptr) << ", "; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 784 | } |
| 785 | os << "\n"; |
| 786 | |
| 787 | os << " dirty byte +offset:count list = "; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 788 | auto class_field_dirty_count_sorted = |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 789 | SortByValueDesc<off_t, int, int>(class_field_dirty_count); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 790 | for (auto pair : class_field_dirty_count_sorted) { |
| 791 | off_t offset = pair.second; |
| 792 | int count = pair.first; |
| 793 | |
| 794 | os << "+" << offset << ":" << count << ", "; |
| 795 | } |
| 796 | os << "\n"; |
| 797 | |
| 798 | os << " field contents:\n"; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 799 | const auto& dirty_objects_list = class_data[klass].dirty_objects; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 800 | for (mirror::Object* obj : dirty_objects_list) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 801 | // remote class object |
| 802 | auto remote_klass = reinterpret_cast<mirror::Class*>(obj); |
| 803 | |
| 804 | // local class object |
| 805 | auto local_klass = RemoteContentsPointerToLocal(remote_klass, |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 806 | remote_contents, |
| 807 | boot_image_header); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 808 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 809 | os << " " << reinterpret_cast<void*>(obj) << " "; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 810 | os << " class_status (remote): " << remote_klass->GetStatus() << ", "; |
| 811 | os << " class_status (local): " << local_klass->GetStatus(); |
| 812 | os << "\n"; |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 817 | auto false_dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>( |
| 818 | class_data, [](const ClassData& d) { return d.false_dirty_object_count; }); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 819 | |
| 820 | os << "\n" << " False-dirty object count by class:\n"; |
| 821 | for (const auto& vk_pair : false_dirty_object_class_values) { |
| 822 | int object_count = vk_pair.first; |
| 823 | mirror::Class* klass = vk_pair.second; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 824 | int object_sizes = class_data[klass].false_dirty_byte_count; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 825 | float avg_object_size = object_sizes * 1.0f / object_count; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 826 | const std::string& descriptor = class_data[klass].descriptor; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 827 | os << " " << mirror::Class::PrettyClass(klass) << " (" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 828 | << "objects: " << object_count << ", " |
| 829 | << "avg object size: " << avg_object_size << ", " |
| 830 | << "total bytes: " << object_sizes << ", " |
| 831 | << "class descriptor: '" << descriptor << "'" |
| 832 | << ")\n"; |
| 833 | |
| 834 | if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) { |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 835 | auto& art_method_false_dirty_objects = class_data[klass].false_dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 836 | |
| 837 | os << " field contents:\n"; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 838 | for (mirror::Object* obj : art_method_false_dirty_objects) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 839 | // local method |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 840 | auto art_method = reinterpret_cast<ArtMethod*>(obj); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 841 | |
| 842 | // local class |
| 843 | mirror::Class* declaring_class = art_method->GetDeclaringClass(); |
| 844 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 845 | os << " " << reinterpret_cast<void*>(obj) << " "; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 846 | os << " entryPointFromJni: " |
| 847 | << reinterpret_cast<const void*>( |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 848 | art_method->GetDataPtrSize(pointer_size)) << ", "; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 849 | os << " entryPointFromQuickCompiledCode: " |
| 850 | << reinterpret_cast<const void*>( |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 851 | art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 852 | << ", "; |
| 853 | os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", "; |
| 854 | os << " class_status (local): " << declaring_class->GetStatus(); |
| 855 | os << "\n"; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | os << "\n" << " Clean object count by class:\n"; |
| 861 | for (const auto& vk_pair : clean_object_class_values) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 862 | os << " " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n"; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | return true; |
| 866 | } |
| 867 | |
| 868 | // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory. |
| 869 | // Returned pointer will point to inside of remote_contents. |
| 870 | template <typename T> |
| 871 | static T* FixUpRemotePointer(T* remote_ptr, |
| 872 | std::vector<uint8_t>& remote_contents, |
| 873 | const backtrace_map_t& boot_map) { |
| 874 | if (remote_ptr == nullptr) { |
| 875 | return nullptr; |
| 876 | } |
| 877 | |
| 878 | uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr); |
| 879 | |
| 880 | CHECK_LE(boot_map.start, remote); |
| 881 | CHECK_GT(boot_map.end, remote); |
| 882 | |
| 883 | off_t boot_offset = remote - boot_map.start; |
| 884 | |
| 885 | return reinterpret_cast<T*>(&remote_contents[boot_offset]); |
| 886 | } |
| 887 | |
| 888 | template <typename T> |
| 889 | static T* RemoteContentsPointerToLocal(T* remote_ptr, |
| 890 | std::vector<uint8_t>& remote_contents, |
| 891 | const ImageHeader& image_header) { |
| 892 | if (remote_ptr == nullptr) { |
| 893 | return nullptr; |
| 894 | } |
| 895 | |
| 896 | uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr); |
| 897 | ptrdiff_t boot_offset = remote - &remote_contents[0]; |
| 898 | |
| 899 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset; |
| 900 | |
| 901 | return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr)); |
| 902 | } |
| 903 | |
| 904 | static std::string GetClassDescriptor(mirror::Class* klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 905 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 906 | CHECK(klass != nullptr); |
| 907 | |
| 908 | std::string descriptor; |
| 909 | const char* descriptor_str = klass->GetDescriptor(&descriptor); |
| 910 | |
| 911 | return std::string(descriptor_str); |
| 912 | } |
| 913 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 914 | template <typename K, typename V, typename D> |
| 915 | static std::vector<std::pair<V, K>> SortByValueDesc( |
| 916 | const std::map<K, D> map, |
| 917 | std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 918 | // Store value->key so that we can use the default sort from pair which |
| 919 | // sorts by value first and then key |
| 920 | std::vector<std::pair<V, K>> value_key_vector; |
| 921 | |
| 922 | for (const auto& kv_pair : map) { |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame] | 923 | value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | // Sort in reverse (descending order) |
| 927 | std::sort(value_key_vector.rbegin(), value_key_vector.rend()); |
| 928 | return value_key_vector; |
| 929 | } |
| 930 | |
| 931 | static bool GetPageFrameNumber(File* page_map_file, |
| 932 | size_t virtual_page_index, |
| 933 | uint64_t* page_frame_number, |
| 934 | std::string* error_msg) { |
| 935 | CHECK(page_map_file != nullptr); |
| 936 | CHECK(page_frame_number != nullptr); |
| 937 | CHECK(error_msg != nullptr); |
| 938 | |
| 939 | constexpr size_t kPageMapEntrySize = sizeof(uint64_t); |
| 940 | constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; // bits 0-54 [in /proc/$pid/pagemap] |
| 941 | constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55); // bit 55 [in /proc/$pid/pagemap] |
| 942 | |
| 943 | uint64_t page_map_entry = 0; |
| 944 | |
| 945 | // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number |
| 946 | if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize, |
| 947 | virtual_page_index * kPageMapEntrySize)) { |
| 948 | *error_msg = StringPrintf("Failed to read the virtual page index entry from %s", |
| 949 | page_map_file->GetPath().c_str()); |
| 950 | return false; |
| 951 | } |
| 952 | |
| 953 | // TODO: seems useless, remove this. |
| 954 | bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0; |
| 955 | if ((false)) { |
| 956 | LOG(VERBOSE) << soft_dirty; // Suppress unused warning |
| 957 | UNREACHABLE(); |
| 958 | } |
| 959 | |
| 960 | *page_frame_number = page_map_entry & kPageFrameNumberMask; |
| 961 | |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | static int IsPageDirty(File* page_map_file, |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 966 | File* clean_page_map_file, |
| 967 | File* kpage_flags_file, |
| 968 | File* kpage_count_file, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 969 | size_t virtual_page_idx, |
| 970 | size_t clean_virtual_page_idx, |
| 971 | // Out parameters: |
| 972 | uint64_t* page_count, std::string* error_msg) { |
| 973 | CHECK(page_map_file != nullptr); |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 974 | CHECK(clean_page_map_file != nullptr); |
| 975 | CHECK_NE(page_map_file, clean_page_map_file); |
| 976 | CHECK(kpage_flags_file != nullptr); |
| 977 | CHECK(kpage_count_file != nullptr); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 978 | CHECK(page_count != nullptr); |
| 979 | CHECK(error_msg != nullptr); |
| 980 | |
| 981 | // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt |
| 982 | |
| 983 | constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t); |
| 984 | constexpr size_t kPageCountEntrySize = sizeof(uint64_t); |
| 985 | constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags |
| 986 | constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags |
| 987 | constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags |
| 988 | |
| 989 | uint64_t page_frame_number = 0; |
| 990 | if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) { |
| 991 | return -1; |
| 992 | } |
| 993 | |
| 994 | uint64_t page_frame_number_clean = 0; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 995 | if (!GetPageFrameNumber(clean_page_map_file, clean_virtual_page_idx, &page_frame_number_clean, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 996 | error_msg)) { |
| 997 | return -1; |
| 998 | } |
| 999 | |
| 1000 | // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page |
| 1001 | uint64_t kpage_flags_entry = 0; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 1002 | if (!kpage_flags_file->PreadFully(&kpage_flags_entry, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1003 | kPageFlagsEntrySize, |
| 1004 | page_frame_number * kPageFlagsEntrySize)) { |
| 1005 | *error_msg = StringPrintf("Failed to read the page flags from %s", |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 1006 | kpage_flags_file->GetPath().c_str()); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1007 | return -1; |
| 1008 | } |
| 1009 | |
| 1010 | // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 1011 | if (!kpage_count_file->PreadFully(page_count /*out*/, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1012 | kPageCountEntrySize, |
| 1013 | page_frame_number * kPageCountEntrySize)) { |
| 1014 | *error_msg = StringPrintf("Failed to read the page count from %s", |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 1015 | kpage_count_file->GetPath().c_str()); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1016 | return -1; |
| 1017 | } |
| 1018 | |
| 1019 | // There must be a page frame at the requested address. |
| 1020 | CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u); |
| 1021 | // The page frame must be memory mapped |
| 1022 | CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u); |
| 1023 | |
| 1024 | // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1 |
| 1025 | bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0; |
| 1026 | |
| 1027 | // page_frame_number_clean must come from the *same* process |
| 1028 | // but a *different* mmap than page_frame_number |
| 1029 | if (flags_dirty) { |
| 1030 | CHECK_NE(page_frame_number, page_frame_number_clean); |
| 1031 | } |
| 1032 | |
| 1033 | return page_frame_number != page_frame_number_clean; |
| 1034 | } |
| 1035 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame^] | 1036 | private: |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1037 | // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art" |
| 1038 | std::string GetImageLocationBaseName() const { |
| 1039 | return BaseName(std::string(image_location_)); |
| 1040 | } |
| 1041 | |
| 1042 | std::ostream* os_; |
| 1043 | const ImageHeader& image_header_; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1044 | const std::string image_location_; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1045 | pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1046 | pid_t zygote_diff_pid_; // Dump image diff against zygote boot.art if pid is non-negative |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1047 | bool zygote_pid_only_; // The user only specified a pid for the zygote. |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1048 | |
| 1049 | DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper); |
| 1050 | }; |
| 1051 | |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1052 | static int DumpImage(Runtime* runtime, |
| 1053 | std::ostream* os, |
| 1054 | pid_t image_diff_pid, |
| 1055 | pid_t zygote_diff_pid) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1056 | ScopedObjectAccess soa(Thread::Current()); |
| 1057 | gc::Heap* heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1058 | std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces(); |
| 1059 | CHECK(!image_spaces.empty()); |
| 1060 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 1061 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1062 | if (!image_header.IsValid()) { |
| 1063 | fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str()); |
| 1064 | return EXIT_FAILURE; |
| 1065 | } |
| 1066 | |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1067 | ImgDiagDumper img_diag_dumper(os, |
| 1068 | image_header, |
| 1069 | image_space->GetImageLocation(), |
| 1070 | image_diff_pid, |
| 1071 | zygote_diff_pid); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1072 | if (!img_diag_dumper.Dump()) { |
| 1073 | return EXIT_FAILURE; |
| 1074 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1075 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1076 | return EXIT_SUCCESS; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | struct ImgDiagArgs : public CmdlineArgs { |
| 1080 | protected: |
| 1081 | using Base = CmdlineArgs; |
| 1082 | |
| 1083 | virtual ParseStatus ParseCustom(const StringPiece& option, |
| 1084 | std::string* error_msg) OVERRIDE { |
| 1085 | { |
| 1086 | ParseStatus base_parse = Base::ParseCustom(option, error_msg); |
| 1087 | if (base_parse != kParseUnknownArgument) { |
| 1088 | return base_parse; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | if (option.starts_with("--image-diff-pid=")) { |
| 1093 | const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data(); |
| 1094 | |
| 1095 | if (!ParseInt(image_diff_pid, &image_diff_pid_)) { |
| 1096 | *error_msg = "Image diff pid out of range"; |
| 1097 | return kParseError; |
| 1098 | } |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1099 | } else if (option.starts_with("--zygote-diff-pid=")) { |
| 1100 | const char* zygote_diff_pid = option.substr(strlen("--zygote-diff-pid=")).data(); |
| 1101 | |
| 1102 | if (!ParseInt(zygote_diff_pid, &zygote_diff_pid_)) { |
| 1103 | *error_msg = "Zygote diff pid out of range"; |
| 1104 | return kParseError; |
| 1105 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1106 | } else { |
| 1107 | return kParseUnknownArgument; |
| 1108 | } |
| 1109 | |
| 1110 | return kParseOk; |
| 1111 | } |
| 1112 | |
| 1113 | virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE { |
| 1114 | // Perform the parent checks. |
| 1115 | ParseStatus parent_checks = Base::ParseChecks(error_msg); |
| 1116 | if (parent_checks != kParseOk) { |
| 1117 | return parent_checks; |
| 1118 | } |
| 1119 | |
| 1120 | // Perform our own checks. |
| 1121 | |
| 1122 | if (kill(image_diff_pid_, |
| 1123 | /*sig*/0) != 0) { // No signal is sent, perform error-checking only. |
| 1124 | // Check if the pid exists before proceeding. |
| 1125 | if (errno == ESRCH) { |
| 1126 | *error_msg = "Process specified does not exist"; |
| 1127 | } else { |
| 1128 | *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno)); |
| 1129 | } |
| 1130 | return kParseError; |
| 1131 | } else if (instruction_set_ != kRuntimeISA) { |
| 1132 | // Don't allow different ISAs since the images are ISA-specific. |
| 1133 | // Right now the code assumes both the runtime ISA and the remote ISA are identical. |
| 1134 | *error_msg = "Must use the default runtime ISA; changing ISA is not supported."; |
| 1135 | return kParseError; |
| 1136 | } |
| 1137 | |
| 1138 | return kParseOk; |
| 1139 | } |
| 1140 | |
| 1141 | virtual std::string GetUsage() const { |
| 1142 | std::string usage; |
| 1143 | |
| 1144 | usage += |
| 1145 | "Usage: imgdiag [options] ...\n" |
| 1146 | " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n" |
| 1147 | " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n" |
| 1148 | "\n"; |
| 1149 | |
| 1150 | usage += Base::GetUsage(); |
| 1151 | |
| 1152 | usage += // Optional. |
| 1153 | " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n" |
| 1154 | " Example: --image-diff-pid=$(pid zygote)\n" |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1155 | " --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff " |
| 1156 | "against.\n" |
| 1157 | " Example: --zygote-diff-pid=$(pid zygote)\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1158 | "\n"; |
| 1159 | |
| 1160 | return usage; |
| 1161 | } |
| 1162 | |
| 1163 | public: |
| 1164 | pid_t image_diff_pid_ = -1; |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1165 | pid_t zygote_diff_pid_ = -1; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1166 | }; |
| 1167 | |
| 1168 | struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> { |
| 1169 | virtual bool ExecuteWithRuntime(Runtime* runtime) { |
| 1170 | CHECK(args_ != nullptr); |
| 1171 | |
| 1172 | return DumpImage(runtime, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1173 | args_->os_, |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1174 | args_->image_diff_pid_, |
| 1175 | args_->zygote_diff_pid_) == EXIT_SUCCESS; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1176 | } |
| 1177 | }; |
| 1178 | |
| 1179 | } // namespace art |
| 1180 | |
| 1181 | int main(int argc, char** argv) { |
| 1182 | art::ImgDiagMain main; |
| 1183 | return main.Main(argc, argv); |
| 1184 | } |