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> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 23 | #include <map> |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 24 | #include <optional> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include <set> |
| 26 | #include <string> |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 27 | #include <unordered_set> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include <vector> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 29 | |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 30 | #include <android-base/parseint.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 31 | #include "android-base/stringprintf.h" |
| 32 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 33 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | #include "art_method-inl.h" |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 35 | #include "base/array_ref.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 36 | #include "base/os.h" |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 37 | #include "base/string_view_cpp20.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 38 | #include "base/unix_file/fd_file.h" |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 39 | #include "class_linker.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 40 | #include "gc/heap.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 41 | #include "gc/space/image_space.h" |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 42 | #include "image-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 43 | #include "mirror/class-inl.h" |
| 44 | #include "mirror/object-inl.h" |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 45 | #include "oat.h" |
| 46 | #include "oat_file.h" |
| 47 | #include "oat_file_manager.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 48 | #include "scoped_thread_state_change-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 49 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 50 | #include "backtrace/BacktraceMap.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 51 | #include "cmdline.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 52 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 53 | #include <signal.h> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 54 | #include <sys/stat.h> |
| 55 | #include <sys/types.h> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 56 | |
| 57 | namespace art { |
| 58 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 59 | using android::base::StringPrintf; |
| 60 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | constexpr size_t kMaxAddressPrint = 5; |
| 64 | |
| 65 | enum class ProcessType { |
| 66 | kZygote, |
| 67 | kRemote |
| 68 | }; |
| 69 | |
| 70 | enum class RemoteProcesses { |
| 71 | kImageOnly, |
| 72 | kZygoteOnly, |
| 73 | kImageAndZygote |
| 74 | }; |
| 75 | |
| 76 | struct MappingData { |
| 77 | // The count of pages that are considered dirty by the OS. |
| 78 | size_t dirty_pages = 0; |
| 79 | // The count of pages that differ by at least one byte. |
| 80 | size_t different_pages = 0; |
| 81 | // The count of differing bytes. |
| 82 | size_t different_bytes = 0; |
| 83 | // The count of differing four-byte units. |
| 84 | size_t different_int32s = 0; |
| 85 | // The count of pages that have mapping count == 1. |
| 86 | size_t private_pages = 0; |
| 87 | // The count of private pages that are also dirty. |
| 88 | size_t private_dirty_pages = 0; |
| 89 | // The count of pages that are marked dirty but do not differ. |
| 90 | size_t false_dirty_pages = 0; |
| 91 | // Set of the local virtual page indices that are dirty. |
| 92 | std::set<size_t> dirty_page_set; |
| 93 | }; |
| 94 | |
| 95 | static std::string GetClassDescriptor(mirror::Class* klass) |
| 96 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 97 | CHECK(klass != nullptr); |
| 98 | |
| 99 | std::string descriptor; |
| 100 | const char* descriptor_str = klass->GetDescriptor(&descriptor /*out*/); |
| 101 | |
| 102 | return std::string(descriptor_str); |
| 103 | } |
| 104 | |
| 105 | static std::string PrettyFieldValue(ArtField* field, mirror::Object* object) |
| 106 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 107 | std::ostringstream oss; |
| 108 | switch (field->GetTypeAsPrimitiveType()) { |
| 109 | case Primitive::kPrimNot: { |
| 110 | oss << object->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 111 | field->GetOffset()); |
| 112 | break; |
| 113 | } |
| 114 | case Primitive::kPrimBoolean: { |
| 115 | oss << static_cast<bool>(object->GetFieldBoolean<kVerifyNone>(field->GetOffset())); |
| 116 | break; |
| 117 | } |
| 118 | case Primitive::kPrimByte: { |
| 119 | oss << static_cast<int32_t>(object->GetFieldByte<kVerifyNone>(field->GetOffset())); |
| 120 | break; |
| 121 | } |
| 122 | case Primitive::kPrimChar: { |
| 123 | oss << object->GetFieldChar<kVerifyNone>(field->GetOffset()); |
| 124 | break; |
| 125 | } |
| 126 | case Primitive::kPrimShort: { |
| 127 | oss << object->GetFieldShort<kVerifyNone>(field->GetOffset()); |
| 128 | break; |
| 129 | } |
| 130 | case Primitive::kPrimInt: { |
| 131 | oss << object->GetField32<kVerifyNone>(field->GetOffset()); |
| 132 | break; |
| 133 | } |
| 134 | case Primitive::kPrimLong: { |
| 135 | oss << object->GetField64<kVerifyNone>(field->GetOffset()); |
| 136 | break; |
| 137 | } |
| 138 | case Primitive::kPrimFloat: { |
| 139 | oss << object->GetField32<kVerifyNone>(field->GetOffset()); |
| 140 | break; |
| 141 | } |
| 142 | case Primitive::kPrimDouble: { |
| 143 | oss << object->GetField64<kVerifyNone>(field->GetOffset()); |
| 144 | break; |
| 145 | } |
| 146 | case Primitive::kPrimVoid: { |
| 147 | oss << "void"; |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | return oss.str(); |
| 152 | } |
| 153 | |
| 154 | template <typename K, typename V, typename D> |
| 155 | static std::vector<std::pair<V, K>> SortByValueDesc( |
| 156 | const std::map<K, D> map, |
| 157 | std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) { |
| 158 | // Store value->key so that we can use the default sort from pair which |
| 159 | // sorts by value first and then key |
| 160 | std::vector<std::pair<V, K>> value_key_vector; |
| 161 | |
| 162 | for (const auto& kv_pair : map) { |
| 163 | value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first)); |
| 164 | } |
| 165 | |
| 166 | // Sort in reverse (descending order) |
| 167 | std::sort(value_key_vector.rbegin(), value_key_vector.rend()); |
| 168 | return value_key_vector; |
| 169 | } |
| 170 | |
| 171 | // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory. |
| 172 | // Returned pointer will point to inside of remote_contents. |
| 173 | template <typename T> |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 174 | static ObjPtr<T> FixUpRemotePointer(ObjPtr<T> remote_ptr, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 175 | ArrayRef<uint8_t> remote_contents, |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 176 | const backtrace_map_t& boot_map) |
| 177 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 178 | if (remote_ptr == nullptr) { |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 182 | uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr.Ptr()); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 183 | |
Mathieu Chartier | 21f7ac1 | 2018-07-09 16:18:27 -0700 | [diff] [blame] | 184 | // In the case the remote pointer is out of range, it probably belongs to another image. |
| 185 | // Just return null for this case. |
| 186 | if (remote < boot_map.start || remote >= boot_map.end) { |
| 187 | return nullptr; |
| 188 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 189 | |
| 190 | off_t boot_offset = remote - boot_map.start; |
| 191 | |
| 192 | return reinterpret_cast<T*>(&remote_contents[boot_offset]); |
| 193 | } |
| 194 | |
| 195 | template <typename T> |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 196 | static ObjPtr<T> RemoteContentsPointerToLocal(ObjPtr<T> remote_ptr, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 197 | ArrayRef<uint8_t> remote_contents, |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 198 | const ImageHeader& image_header) |
| 199 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 200 | if (remote_ptr == nullptr) { |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 204 | uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr.Ptr()); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 205 | ptrdiff_t boot_offset = remote - &remote_contents[0]; |
| 206 | |
| 207 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset; |
| 208 | |
| 209 | return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr)); |
| 210 | } |
| 211 | |
| 212 | template <typename T> size_t EntrySize(T* entry); |
| 213 | template<> size_t EntrySize(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 214 | return object->SizeOf(); |
| 215 | } |
| 216 | template<> size_t EntrySize(ArtMethod* art_method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 217 | return sizeof(*art_method); |
| 218 | } |
| 219 | |
Mathieu Chartier | d3b6664 | 2019-06-06 08:11:55 -0700 | [diff] [blame] | 220 | // entry1 and entry2 might be relocated, this means we must use the runtime image's entry |
| 221 | // (image_entry) to avoid crashes. |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 222 | template <typename T> |
Mathieu Chartier | d3b6664 | 2019-06-06 08:11:55 -0700 | [diff] [blame] | 223 | static bool EntriesDiffer(T* image_entry, |
| 224 | T* entry1, |
| 225 | T* entry2) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 226 | // Use the image entry since entry1 and entry2 might both be remote and relocated. |
| 227 | return memcmp(entry1, entry2, EntrySize(image_entry)) != 0; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | template <typename T> |
| 231 | struct RegionCommon { |
| 232 | public: |
| 233 | RegionCommon(std::ostream* os, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 234 | ArrayRef<uint8_t> remote_contents, |
| 235 | ArrayRef<uint8_t> zygote_contents, |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 236 | const backtrace_map_t& boot_map, |
| 237 | const ImageHeader& image_header) : |
| 238 | os_(*os), |
| 239 | remote_contents_(remote_contents), |
| 240 | zygote_contents_(zygote_contents), |
| 241 | boot_map_(boot_map), |
| 242 | image_header_(image_header), |
| 243 | different_entries_(0), |
| 244 | dirty_entry_bytes_(0), |
| 245 | false_dirty_entry_bytes_(0) { |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 246 | CHECK(!remote_contents.empty()); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void DumpSamplesAndOffsetCount() { |
| 250 | os_ << " sample object addresses: "; |
| 251 | for (size_t i = 0; i < dirty_entries_.size() && i < kMaxAddressPrint; ++i) { |
| 252 | T* entry = dirty_entries_[i]; |
| 253 | os_ << reinterpret_cast<void*>(entry) << ", "; |
| 254 | } |
| 255 | os_ << "\n"; |
| 256 | os_ << " dirty byte +offset:count list = "; |
| 257 | std::vector<std::pair<size_t, off_t>> field_dirty_count_sorted = |
| 258 | SortByValueDesc<off_t, size_t, size_t>(field_dirty_count_); |
| 259 | for (const std::pair<size_t, off_t>& pair : field_dirty_count_sorted) { |
| 260 | off_t offset = pair.second; |
| 261 | size_t count = pair.first; |
| 262 | os_ << "+" << offset << ":" << count << ", "; |
| 263 | } |
| 264 | os_ << "\n"; |
| 265 | } |
| 266 | |
| 267 | size_t GetDifferentEntryCount() const { return different_entries_; } |
| 268 | size_t GetDirtyEntryBytes() const { return dirty_entry_bytes_; } |
| 269 | size_t GetFalseDirtyEntryCount() const { return false_dirty_entries_.size(); } |
| 270 | size_t GetFalseDirtyEntryBytes() const { return false_dirty_entry_bytes_; } |
| 271 | size_t GetZygoteDirtyEntryCount() const { return zygote_dirty_entries_.size(); } |
| 272 | |
| 273 | protected: |
| 274 | bool IsEntryOnDirtyPage(T* entry, const std::set<size_t>& dirty_pages) const |
| 275 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 276 | size_t size = EntrySize(entry); |
| 277 | size_t page_off = 0; |
| 278 | size_t current_page_idx; |
| 279 | uintptr_t entry_address = reinterpret_cast<uintptr_t>(entry); |
| 280 | // Iterate every page this entry belongs to |
| 281 | do { |
| 282 | current_page_idx = entry_address / kPageSize + page_off; |
| 283 | if (dirty_pages.find(current_page_idx) != dirty_pages.end()) { |
| 284 | // This entry is on a dirty page |
| 285 | return true; |
| 286 | } |
| 287 | page_off++; |
| 288 | } while ((current_page_idx * kPageSize) < RoundUp(entry_address + size, kObjectAlignment)); |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | void AddZygoteDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 293 | zygote_dirty_entries_.insert(entry); |
| 294 | } |
| 295 | |
| 296 | void AddImageDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 297 | image_dirty_entries_.insert(entry); |
| 298 | } |
| 299 | |
| 300 | void AddFalseDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 301 | false_dirty_entries_.push_back(entry); |
| 302 | false_dirty_entry_bytes_ += EntrySize(entry); |
| 303 | } |
| 304 | |
| 305 | // The output stream to write to. |
| 306 | std::ostream& os_; |
| 307 | // The byte contents of the remote (image) process' image. |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 308 | ArrayRef<uint8_t> remote_contents_; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 309 | // The byte contents of the zygote process' image. |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 310 | ArrayRef<uint8_t> zygote_contents_; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 311 | const backtrace_map_t& boot_map_; |
| 312 | const ImageHeader& image_header_; |
| 313 | |
| 314 | // Count of entries that are different. |
| 315 | size_t different_entries_; |
| 316 | |
| 317 | // Local entries that are dirty (differ in at least one byte). |
| 318 | size_t dirty_entry_bytes_; |
| 319 | std::vector<T*> dirty_entries_; |
| 320 | |
| 321 | // Local entries that are clean, but located on dirty pages. |
| 322 | size_t false_dirty_entry_bytes_; |
| 323 | std::vector<T*> false_dirty_entries_; |
| 324 | |
| 325 | // Image dirty entries |
| 326 | // If zygote_pid_only_ == true, these are shared dirty entries in the zygote. |
| 327 | // If zygote_pid_only_ == false, these are private dirty entries in the application. |
| 328 | std::set<T*> image_dirty_entries_; |
| 329 | |
| 330 | // Zygote dirty entries (probably private dirty). |
| 331 | // We only add entries here if they differed in both the image and the zygote, so |
| 332 | // they are probably private dirty. |
| 333 | std::set<T*> zygote_dirty_entries_; |
| 334 | |
| 335 | std::map<off_t /* field offset */, size_t /* count */> field_dirty_count_; |
| 336 | |
| 337 | private: |
| 338 | DISALLOW_COPY_AND_ASSIGN(RegionCommon); |
| 339 | }; |
| 340 | |
| 341 | template <typename T> |
| 342 | class RegionSpecializedBase : public RegionCommon<T> { |
| 343 | }; |
| 344 | |
| 345 | // Region analysis for mirror::Objects |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 346 | class ImgObjectVisitor : public ObjectVisitor { |
| 347 | public: |
| 348 | using ComputeDirtyFunc = std::function<void(mirror::Object* object, |
| 349 | const uint8_t* begin_image_ptr, |
| 350 | const std::set<size_t>& dirty_pages)>; |
Andreas Gampe | 6856214 | 2018-06-20 21:49:11 +0000 | [diff] [blame] | 351 | ImgObjectVisitor(ComputeDirtyFunc dirty_func, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 352 | const uint8_t* begin_image_ptr, |
| 353 | const std::set<size_t>& dirty_pages) : |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 354 | dirty_func_(std::move(dirty_func)), |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 355 | begin_image_ptr_(begin_image_ptr), |
| 356 | dirty_pages_(dirty_pages) { } |
| 357 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 358 | ~ImgObjectVisitor() override { } |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 359 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 360 | void Visit(mirror::Object* object) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Pedowitz | 2d53643 | 2020-07-22 14:33:00 -0700 | [diff] [blame] | 361 | // Check that we are reading a real mirror::Object |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 362 | CHECK(object->GetClass() != nullptr) << "Image object at address " |
| 363 | << object |
| 364 | << " has null class"; |
| 365 | if (kUseBakerReadBarrier) { |
| 366 | object->AssertReadBarrierState(); |
| 367 | } |
| 368 | dirty_func_(object, begin_image_ptr_, dirty_pages_); |
| 369 | } |
| 370 | |
| 371 | private: |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 372 | const ComputeDirtyFunc dirty_func_; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 373 | const uint8_t* begin_image_ptr_; |
| 374 | const std::set<size_t>& dirty_pages_; |
| 375 | }; |
| 376 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 377 | template<> |
| 378 | class RegionSpecializedBase<mirror::Object> : public RegionCommon<mirror::Object> { |
| 379 | public: |
| 380 | RegionSpecializedBase(std::ostream* os, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 381 | ArrayRef<uint8_t> remote_contents, |
| 382 | ArrayRef<uint8_t> zygote_contents, |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 383 | const backtrace_map_t& boot_map, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 384 | const ImageHeader& image_header, |
| 385 | bool dump_dirty_objects) |
| 386 | : RegionCommon<mirror::Object>(os, remote_contents, zygote_contents, boot_map, image_header), |
| 387 | os_(*os), |
| 388 | dump_dirty_objects_(dump_dirty_objects) { } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 389 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 390 | // Define a common public type name for use by RegionData. |
| 391 | using VisitorClass = ImgObjectVisitor; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 392 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 393 | void VisitEntries(VisitorClass* visitor, |
| 394 | uint8_t* base, |
| 395 | PointerSize pointer_size) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 396 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 397 | RegionCommon<mirror::Object>::image_header_.VisitObjects(visitor, base, pointer_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void VisitEntry(mirror::Object* entry) |
| 401 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 402 | // Unconditionally store the class descriptor in case we need it later |
| 403 | mirror::Class* klass = entry->GetClass(); |
| 404 | class_data_[klass].descriptor = GetClassDescriptor(klass); |
| 405 | } |
| 406 | |
| 407 | void AddCleanEntry(mirror::Object* entry) |
| 408 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 409 | class_data_[entry->GetClass()].AddCleanObject(); |
| 410 | } |
| 411 | |
| 412 | void AddFalseDirtyEntry(mirror::Object* entry) |
| 413 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 414 | RegionCommon<mirror::Object>::AddFalseDirtyEntry(entry); |
| 415 | class_data_[entry->GetClass()].AddFalseDirtyObject(entry); |
| 416 | } |
| 417 | |
| 418 | void AddDirtyEntry(mirror::Object* entry, mirror::Object* entry_remote) |
| 419 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 420 | size_t entry_size = EntrySize(entry); |
| 421 | ++different_entries_; |
| 422 | dirty_entry_bytes_ += entry_size; |
| 423 | // Log dirty count and objects for class objects only. |
| 424 | mirror::Class* klass = entry->GetClass(); |
| 425 | if (klass->IsClassClass()) { |
| 426 | // Increment counts for the fields that are dirty |
| 427 | const uint8_t* current = reinterpret_cast<const uint8_t*>(entry); |
| 428 | const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(entry_remote); |
| 429 | for (size_t i = 0; i < entry_size; ++i) { |
| 430 | if (current[i] != current_remote[i]) { |
| 431 | field_dirty_count_[i]++; |
| 432 | } |
| 433 | } |
| 434 | dirty_entries_.push_back(entry); |
| 435 | } |
| 436 | class_data_[klass].AddDirtyObject(entry, entry_remote); |
| 437 | } |
| 438 | |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 439 | void DiffEntryContents(mirror::Object* entry, |
| 440 | uint8_t* remote_bytes, |
| 441 | const uint8_t* base_ptr, |
| 442 | bool log_dirty_objects) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 443 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 444 | const char* tabs = " "; |
| 445 | // Attempt to find fields for all dirty bytes. |
| 446 | mirror::Class* klass = entry->GetClass(); |
| 447 | if (entry->IsClass()) { |
| 448 | os_ << tabs |
| 449 | << "Class " << mirror::Class::PrettyClass(entry->AsClass()) << " " << entry << "\n"; |
| 450 | } else { |
| 451 | os_ << tabs |
| 452 | << "Instance of " << mirror::Class::PrettyClass(klass) << " " << entry << "\n"; |
| 453 | } |
| 454 | |
| 455 | std::unordered_set<ArtField*> dirty_instance_fields; |
| 456 | std::unordered_set<ArtField*> dirty_static_fields; |
| 457 | // Examine the bytes comprising the Object, computing which fields are dirty |
| 458 | // and recording them for later display. If the Object is an array object, |
| 459 | // compute the dirty entries. |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 460 | mirror::Object* remote_entry = reinterpret_cast<mirror::Object*>(remote_bytes); |
| 461 | for (size_t i = 0, count = entry->SizeOf(); i < count; ++i) { |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 462 | if (base_ptr[i] != remote_bytes[i]) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 463 | ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i); |
| 464 | if (field != nullptr) { |
| 465 | dirty_instance_fields.insert(field); |
| 466 | } else if (entry->IsClass()) { |
| 467 | field = ArtField::FindStaticFieldWithOffset</*exact*/false>(entry->AsClass(), i); |
| 468 | if (field != nullptr) { |
| 469 | dirty_static_fields.insert(field); |
| 470 | } |
| 471 | } |
| 472 | if (field == nullptr) { |
| 473 | if (klass->IsArrayClass()) { |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 474 | ObjPtr<mirror::Class> component_type = klass->GetComponentType(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 475 | Primitive::Type primitive_type = component_type->GetPrimitiveType(); |
| 476 | size_t component_size = Primitive::ComponentSize(primitive_type); |
| 477 | size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value(); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 478 | DCHECK_ALIGNED_PARAM(data_offset, component_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 479 | if (i >= data_offset) { |
| 480 | os_ << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n"; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 481 | // Skip the remaining bytes of this element to prevent spam. |
| 482 | DCHECK(IsPowerOfTwo(component_size)); |
| 483 | i |= component_size - 1; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 484 | continue; |
| 485 | } |
| 486 | } |
| 487 | os_ << tabs << "No field for byte offset " << i << "\n"; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | // Dump different fields. |
| 492 | if (!dirty_instance_fields.empty()) { |
| 493 | os_ << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n"; |
| 494 | for (ArtField* field : dirty_instance_fields) { |
| 495 | os_ << tabs << ArtField::PrettyField(field) |
| 496 | << " original=" << PrettyFieldValue(field, entry) |
| 497 | << " remote=" << PrettyFieldValue(field, remote_entry) << "\n"; |
| 498 | } |
| 499 | } |
| 500 | if (!dirty_static_fields.empty()) { |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 501 | if (dump_dirty_objects_ && log_dirty_objects) { |
| 502 | dirty_objects_.insert(entry); |
| 503 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 504 | os_ << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n"; |
| 505 | for (ArtField* field : dirty_static_fields) { |
| 506 | os_ << tabs << ArtField::PrettyField(field) |
| 507 | << " original=" << PrettyFieldValue(field, entry) |
| 508 | << " remote=" << PrettyFieldValue(field, remote_entry) << "\n"; |
| 509 | } |
| 510 | } |
| 511 | os_ << "\n"; |
| 512 | } |
| 513 | |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 514 | void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 515 | for (mirror::Object* obj : dirty_objects_) { |
| 516 | if (obj->IsClass()) { |
Vladimir Marko | 001bb57 | 2021-02-25 11:54:38 +0000 | [diff] [blame] | 517 | std::string temp; |
| 518 | os_ << "Private dirty object: " << obj->AsClass()->GetDescriptor(&temp) << "\n"; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 523 | void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 524 | // vector of pairs (size_t count, Class*) |
| 525 | auto dirty_object_class_values = |
| 526 | SortByValueDesc<mirror::Class*, size_t, ClassData>( |
| 527 | class_data_, |
| 528 | [](const ClassData& d) { return d.dirty_object_count; }); |
| 529 | os_ << "\n" << " Dirty object count by class:\n"; |
| 530 | for (const auto& vk_pair : dirty_object_class_values) { |
| 531 | size_t dirty_object_count = vk_pair.first; |
| 532 | mirror::Class* klass = vk_pair.second; |
| 533 | ClassData& class_data = class_data_[klass]; |
| 534 | size_t object_sizes = class_data.dirty_object_size_in_bytes; |
| 535 | float avg_dirty_bytes_per_class = |
| 536 | class_data.dirty_object_byte_count * 1.0f / object_sizes; |
| 537 | float avg_object_size = object_sizes * 1.0f / dirty_object_count; |
| 538 | const std::string& descriptor = class_data.descriptor; |
| 539 | os_ << " " << mirror::Class::PrettyClass(klass) << " (" |
| 540 | << "objects: " << dirty_object_count << ", " |
| 541 | << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", " |
| 542 | << "avg object size: " << avg_object_size << ", " |
| 543 | << "class descriptor: '" << descriptor << "'" |
| 544 | << ")\n"; |
| 545 | if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) { |
| 546 | DumpSamplesAndOffsetCount(); |
| 547 | os_ << " field contents:\n"; |
| 548 | for (mirror::Object* object : class_data.dirty_objects) { |
| 549 | // remote class object |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 550 | ObjPtr<mirror::Class> remote_klass = |
| 551 | ObjPtr<mirror::Class>::DownCast<mirror::Object>(object); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 552 | // local class object |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 553 | ObjPtr<mirror::Class> local_klass = |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 554 | RemoteContentsPointerToLocal(remote_klass, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 555 | RegionCommon<mirror::Object>::remote_contents_, |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 556 | RegionCommon<mirror::Object>::image_header_); |
| 557 | os_ << " " << reinterpret_cast<const void*>(object) << " "; |
| 558 | os_ << " class_status (remote): " << remote_klass->GetStatus() << ", "; |
| 559 | os_ << " class_status (local): " << local_klass->GetStatus(); |
| 560 | os_ << "\n"; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 567 | // vector of pairs (size_t count, Class*) |
| 568 | auto false_dirty_object_class_values = |
| 569 | SortByValueDesc<mirror::Class*, size_t, ClassData>( |
| 570 | class_data_, |
| 571 | [](const ClassData& d) { return d.false_dirty_object_count; }); |
| 572 | os_ << "\n" << " False-dirty object count by class:\n"; |
| 573 | for (const auto& vk_pair : false_dirty_object_class_values) { |
| 574 | size_t object_count = vk_pair.first; |
| 575 | mirror::Class* klass = vk_pair.second; |
| 576 | ClassData& class_data = class_data_[klass]; |
| 577 | size_t object_sizes = class_data.false_dirty_byte_count; |
| 578 | float avg_object_size = object_sizes * 1.0f / object_count; |
| 579 | const std::string& descriptor = class_data.descriptor; |
| 580 | os_ << " " << mirror::Class::PrettyClass(klass) << " (" |
| 581 | << "objects: " << object_count << ", " |
| 582 | << "avg object size: " << avg_object_size << ", " |
| 583 | << "total bytes: " << object_sizes << ", " |
| 584 | << "class descriptor: '" << descriptor << "'" |
| 585 | << ")\n"; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 590 | // vector of pairs (size_t count, Class*) |
| 591 | auto clean_object_class_values = |
| 592 | SortByValueDesc<mirror::Class*, size_t, ClassData>( |
| 593 | class_data_, |
| 594 | [](const ClassData& d) { return d.clean_object_count; }); |
| 595 | os_ << "\n" << " Clean object count by class:\n"; |
| 596 | for (const auto& vk_pair : clean_object_class_values) { |
| 597 | os_ << " " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n"; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | private: |
| 602 | // Aggregate and detail class data from an image diff. |
| 603 | struct ClassData { |
| 604 | size_t dirty_object_count = 0; |
| 605 | // Track only the byte-per-byte dirtiness (in bytes) |
| 606 | size_t dirty_object_byte_count = 0; |
| 607 | // Track the object-by-object dirtiness (in bytes) |
| 608 | size_t dirty_object_size_in_bytes = 0; |
| 609 | size_t clean_object_count = 0; |
| 610 | std::string descriptor; |
| 611 | size_t false_dirty_byte_count = 0; |
| 612 | size_t false_dirty_object_count = 0; |
| 613 | std::vector<mirror::Object*> false_dirty_objects; |
| 614 | // Remote pointers to dirty objects |
| 615 | std::vector<mirror::Object*> dirty_objects; |
| 616 | |
| 617 | void AddCleanObject() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 618 | ++clean_object_count; |
| 619 | } |
| 620 | |
| 621 | void AddDirtyObject(mirror::Object* object, mirror::Object* object_remote) |
| 622 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 623 | ++dirty_object_count; |
| 624 | dirty_object_byte_count += CountDirtyBytes(object, object_remote); |
| 625 | dirty_object_size_in_bytes += EntrySize(object); |
| 626 | dirty_objects.push_back(object_remote); |
| 627 | } |
| 628 | |
| 629 | void AddFalseDirtyObject(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 630 | ++false_dirty_object_count; |
| 631 | false_dirty_objects.push_back(object); |
| 632 | false_dirty_byte_count += EntrySize(object); |
| 633 | } |
| 634 | |
| 635 | private: |
| 636 | // Go byte-by-byte and figure out what exactly got dirtied |
| 637 | static size_t CountDirtyBytes(mirror::Object* object1, mirror::Object* object2) |
| 638 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 639 | const uint8_t* cur1 = reinterpret_cast<const uint8_t*>(object1); |
| 640 | const uint8_t* cur2 = reinterpret_cast<const uint8_t*>(object2); |
| 641 | size_t dirty_bytes = 0; |
| 642 | size_t object_size = EntrySize(object1); |
| 643 | for (size_t i = 0; i < object_size; ++i) { |
| 644 | if (cur1[i] != cur2[i]) { |
| 645 | dirty_bytes++; |
| 646 | } |
| 647 | } |
| 648 | return dirty_bytes; |
| 649 | } |
| 650 | }; |
| 651 | |
| 652 | std::ostream& os_; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 653 | bool dump_dirty_objects_; |
| 654 | std::unordered_set<mirror::Object*> dirty_objects_; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 655 | std::map<mirror::Class*, ClassData> class_data_; |
| 656 | |
| 657 | DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase); |
| 658 | }; |
| 659 | |
| 660 | // Region analysis for ArtMethods. |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 661 | class ImgArtMethodVisitor { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 662 | public: |
| 663 | using ComputeDirtyFunc = std::function<void(ArtMethod*, |
| 664 | const uint8_t*, |
| 665 | const std::set<size_t>&)>; |
Andreas Gampe | 6856214 | 2018-06-20 21:49:11 +0000 | [diff] [blame] | 666 | ImgArtMethodVisitor(ComputeDirtyFunc dirty_func, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 667 | const uint8_t* begin_image_ptr, |
| 668 | const std::set<size_t>& dirty_pages) : |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 669 | dirty_func_(std::move(dirty_func)), |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 670 | begin_image_ptr_(begin_image_ptr), |
| 671 | dirty_pages_(dirty_pages) { } |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 672 | void operator()(ArtMethod& method) const { |
| 673 | dirty_func_(&method, begin_image_ptr_, dirty_pages_); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | private: |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 677 | const ComputeDirtyFunc dirty_func_; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 678 | const uint8_t* begin_image_ptr_; |
| 679 | const std::set<size_t>& dirty_pages_; |
| 680 | }; |
| 681 | |
| 682 | // Struct and functor for computing offsets of members of ArtMethods. |
| 683 | // template <typename RegionType> |
| 684 | struct MemberInfo { |
| 685 | template <typename T> |
| 686 | void operator() (const ArtMethod* method, const T* member_address, const std::string& name) { |
| 687 | // Check that member_address is a pointer inside *method. |
| 688 | DCHECK(reinterpret_cast<uintptr_t>(method) <= reinterpret_cast<uintptr_t>(member_address)); |
| 689 | DCHECK(reinterpret_cast<uintptr_t>(member_address) + sizeof(T) <= |
| 690 | reinterpret_cast<uintptr_t>(method) + sizeof(ArtMethod)); |
| 691 | size_t offset = |
| 692 | reinterpret_cast<uintptr_t>(member_address) - reinterpret_cast<uintptr_t>(method); |
| 693 | offset_to_name_size_.insert({offset, NameAndSize(sizeof(T), name)}); |
| 694 | } |
| 695 | |
| 696 | struct NameAndSize { |
| 697 | size_t size_; |
| 698 | std::string name_; |
| 699 | NameAndSize(size_t size, const std::string& name) : size_(size), name_(name) { } |
| 700 | NameAndSize() : size_(0), name_("INVALID") { } |
| 701 | }; |
| 702 | |
| 703 | std::map<size_t, NameAndSize> offset_to_name_size_; |
| 704 | }; |
| 705 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 706 | template<> |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 707 | class RegionSpecializedBase<ArtMethod> : public RegionCommon<ArtMethod> { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 708 | public: |
| 709 | RegionSpecializedBase(std::ostream* os, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 710 | ArrayRef<uint8_t> remote_contents, |
| 711 | ArrayRef<uint8_t> zygote_contents, |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 712 | const backtrace_map_t& boot_map, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 713 | const ImageHeader& image_header, |
| 714 | bool dump_dirty_objects ATTRIBUTE_UNUSED) |
| 715 | : RegionCommon<ArtMethod>(os, remote_contents, zygote_contents, boot_map, image_header), |
| 716 | os_(*os) { |
| 717 | // Prepare the table for offset to member lookups. |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 718 | ArtMethod* art_method = reinterpret_cast<ArtMethod*>(&remote_contents[0]); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 719 | art_method->VisitMembers(member_info_); |
| 720 | // Prepare the table for address to symbolic entry point names. |
| 721 | BuildEntryPointNames(); |
| 722 | class_linker_ = Runtime::Current()->GetClassLinker(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 723 | } |
| 724 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 725 | // Define a common public type name for use by RegionData. |
| 726 | using VisitorClass = ImgArtMethodVisitor; |
| 727 | |
| 728 | void VisitEntries(VisitorClass* visitor, |
| 729 | uint8_t* base, |
| 730 | PointerSize pointer_size) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 731 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 732 | RegionCommon<ArtMethod>::image_header_.VisitPackedArtMethods(*visitor, base, pointer_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | void VisitEntry(ArtMethod* method ATTRIBUTE_UNUSED) |
| 736 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 737 | } |
| 738 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 739 | void AddCleanEntry(ArtMethod* method ATTRIBUTE_UNUSED) { |
| 740 | } |
| 741 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 742 | void AddFalseDirtyEntry(ArtMethod* method) |
| 743 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 744 | RegionCommon<ArtMethod>::AddFalseDirtyEntry(method); |
| 745 | } |
| 746 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 747 | void AddDirtyEntry(ArtMethod* method, ArtMethod* method_remote) |
| 748 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 749 | size_t entry_size = EntrySize(method); |
| 750 | ++different_entries_; |
| 751 | dirty_entry_bytes_ += entry_size; |
| 752 | // Increment counts for the fields that are dirty |
| 753 | const uint8_t* current = reinterpret_cast<const uint8_t*>(method); |
| 754 | const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(method_remote); |
| 755 | // ArtMethods always log their dirty count and entries. |
| 756 | for (size_t i = 0; i < entry_size; ++i) { |
| 757 | if (current[i] != current_remote[i]) { |
| 758 | field_dirty_count_[i]++; |
| 759 | } |
| 760 | } |
| 761 | dirty_entries_.push_back(method); |
| 762 | } |
| 763 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 764 | void DiffEntryContents(ArtMethod* method, |
| 765 | uint8_t* remote_bytes, |
| 766 | const uint8_t* base_ptr, |
| 767 | bool log_dirty_objects ATTRIBUTE_UNUSED) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 768 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 769 | const char* tabs = " "; |
| 770 | os_ << tabs << "ArtMethod " << ArtMethod::PrettyMethod(method) << "\n"; |
| 771 | |
| 772 | std::unordered_set<size_t> dirty_members; |
| 773 | // Examine the members comprising the ArtMethod, computing which members are dirty. |
Andreas Gampe | aad9d37 | 2018-09-18 15:58:47 -0700 | [diff] [blame] | 774 | for (const std::pair<const size_t, |
| 775 | MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 776 | const size_t offset = p.first; |
| 777 | if (memcmp(base_ptr + offset, remote_bytes + offset, p.second.size_) != 0) { |
| 778 | dirty_members.insert(p.first); |
| 779 | } |
| 780 | } |
| 781 | // Dump different fields. |
| 782 | if (!dirty_members.empty()) { |
| 783 | os_ << tabs << "Dirty members " << dirty_members.size() << "\n"; |
| 784 | for (size_t offset : dirty_members) { |
| 785 | const MemberInfo::NameAndSize& member_info = member_info_.offset_to_name_size_[offset]; |
| 786 | os_ << tabs << member_info.name_ |
| 787 | << " original=" << StringFromBytes(base_ptr + offset, member_info.size_) |
| 788 | << " remote=" << StringFromBytes(remote_bytes + offset, member_info.size_) |
| 789 | << "\n"; |
| 790 | } |
| 791 | } |
| 792 | os_ << "\n"; |
| 793 | } |
| 794 | |
| 795 | void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 799 | DumpSamplesAndOffsetCount(); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 800 | os_ << " offset to field map:\n"; |
Andreas Gampe | aad9d37 | 2018-09-18 15:58:47 -0700 | [diff] [blame] | 801 | for (const std::pair<const size_t, |
| 802 | MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 803 | const size_t offset = p.first; |
| 804 | const size_t size = p.second.size_; |
| 805 | os_ << StringPrintf(" %zu-%zu: ", offset, offset + size - 1) |
| 806 | << p.second.name_ |
| 807 | << std::endl; |
| 808 | } |
| 809 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 810 | os_ << " field contents:\n"; |
| 811 | for (ArtMethod* method : dirty_entries_) { |
| 812 | // remote method |
| 813 | auto art_method = reinterpret_cast<ArtMethod*>(method); |
| 814 | // remote class |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 815 | ObjPtr<mirror::Class> remote_declaring_class = |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 816 | FixUpRemotePointer(art_method->GetDeclaringClass(), |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 817 | RegionCommon<ArtMethod>::remote_contents_, |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 818 | RegionCommon<ArtMethod>::boot_map_); |
| 819 | // local class |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 820 | ObjPtr<mirror::Class> declaring_class = |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 821 | RemoteContentsPointerToLocal(remote_declaring_class, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 822 | RegionCommon<ArtMethod>::remote_contents_, |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 823 | RegionCommon<ArtMethod>::image_header_); |
| 824 | DumpOneArtMethod(art_method, declaring_class, remote_declaring_class); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 829 | os_ << "\n" << " False-dirty ArtMethods\n"; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 830 | os_ << " field contents:\n"; |
| 831 | for (ArtMethod* method : false_dirty_entries_) { |
| 832 | // local class |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 833 | ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 834 | DumpOneArtMethod(method, declaring_class, nullptr); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 839 | } |
| 840 | |
| 841 | private: |
| 842 | std::ostream& os_; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 843 | MemberInfo member_info_; |
| 844 | std::map<const void*, std::string> entry_point_names_; |
| 845 | ClassLinker* class_linker_; |
| 846 | |
| 847 | // Compute a map of addresses to names in the boot OAT file(s). |
| 848 | void BuildEntryPointNames() { |
| 849 | OatFileManager& oat_file_manager = Runtime::Current()->GetOatFileManager(); |
| 850 | std::vector<const OatFile*> boot_oat_files = oat_file_manager.GetBootOatFiles(); |
| 851 | for (const OatFile* oat_file : boot_oat_files) { |
| 852 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 853 | const void* jdl = oat_header.GetJniDlsymLookupTrampoline(); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 854 | if (jdl != nullptr) { |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 855 | entry_point_names_[jdl] = "JniDlsymLookupTrampoline (from boot oat file)"; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 856 | } |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 857 | const void* jdlc = oat_header.GetJniDlsymLookupCriticalTrampoline(); |
| 858 | if (jdlc != nullptr) { |
| 859 | entry_point_names_[jdlc] = "JniDlsymLookupCriticalTrampoline (from boot oat file)"; |
| 860 | } |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 861 | const void* qgjt = oat_header.GetQuickGenericJniTrampoline(); |
| 862 | if (qgjt != nullptr) { |
| 863 | entry_point_names_[qgjt] = "QuickGenericJniTrampoline (from boot oat file)"; |
| 864 | } |
| 865 | const void* qrt = oat_header.GetQuickResolutionTrampoline(); |
| 866 | if (qrt != nullptr) { |
| 867 | entry_point_names_[qrt] = "QuickResolutionTrampoline (from boot oat file)"; |
| 868 | } |
| 869 | const void* qict = oat_header.GetQuickImtConflictTrampoline(); |
| 870 | if (qict != nullptr) { |
| 871 | entry_point_names_[qict] = "QuickImtConflictTrampoline (from boot oat file)"; |
| 872 | } |
| 873 | const void* q2ib = oat_header.GetQuickToInterpreterBridge(); |
| 874 | if (q2ib != nullptr) { |
| 875 | entry_point_names_[q2ib] = "QuickToInterpreterBridge (from boot oat file)"; |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | std::string StringFromBytes(const uint8_t* bytes, size_t size) { |
| 881 | switch (size) { |
| 882 | case 1: |
| 883 | return StringPrintf("%" PRIx8, *bytes); |
| 884 | case 2: |
| 885 | return StringPrintf("%" PRIx16, *reinterpret_cast<const uint16_t*>(bytes)); |
| 886 | case 4: |
| 887 | case 8: { |
| 888 | // Compute an address if the bytes might contain one. |
| 889 | uint64_t intval; |
| 890 | if (size == 4) { |
| 891 | intval = *reinterpret_cast<const uint32_t*>(bytes); |
| 892 | } else { |
| 893 | intval = *reinterpret_cast<const uint64_t*>(bytes); |
| 894 | } |
| 895 | const void* addr = reinterpret_cast<const void*>(intval); |
| 896 | // Match the address against those that have Is* methods in the ClassLinker. |
| 897 | if (class_linker_->IsQuickToInterpreterBridge(addr)) { |
| 898 | return "QuickToInterpreterBridge"; |
| 899 | } else if (class_linker_->IsQuickGenericJniStub(addr)) { |
| 900 | return "QuickGenericJniStub"; |
| 901 | } else if (class_linker_->IsQuickResolutionStub(addr)) { |
| 902 | return "QuickResolutionStub"; |
| 903 | } else if (class_linker_->IsJniDlsymLookupStub(addr)) { |
| 904 | return "JniDlsymLookupStub"; |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 905 | } else if (class_linker_->IsJniDlsymLookupCriticalStub(addr)) { |
| 906 | return "JniDlsymLookupCriticalStub"; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 907 | } |
| 908 | // Match the address against those that we saved from the boot OAT files. |
| 909 | if (entry_point_names_.find(addr) != entry_point_names_.end()) { |
| 910 | return entry_point_names_[addr]; |
| 911 | } |
| 912 | return StringPrintf("%" PRIx64, intval); |
| 913 | } |
| 914 | default: |
| 915 | LOG(WARNING) << "Don't know how to convert " << size << " bytes to integer"; |
| 916 | return "<UNKNOWN>"; |
| 917 | } |
| 918 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 919 | |
| 920 | void DumpOneArtMethod(ArtMethod* art_method, |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 921 | ObjPtr<mirror::Class> declaring_class, |
| 922 | ObjPtr<mirror::Class> remote_declaring_class) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 923 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 924 | PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 925 | os_ << " " << reinterpret_cast<const void*>(art_method) << " "; |
| 926 | os_ << " entryPointFromJni: " |
| 927 | << reinterpret_cast<const void*>(art_method->GetDataPtrSize(pointer_size)) << ", "; |
| 928 | os_ << " entryPointFromQuickCompiledCode: " |
| 929 | << reinterpret_cast<const void*>( |
| 930 | art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)) |
| 931 | << ", "; |
| 932 | os_ << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", "; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 933 | // Null for runtime metionds. |
| 934 | if (declaring_class != nullptr) { |
| 935 | os_ << " class_status (local): " << declaring_class->GetStatus(); |
| 936 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 937 | if (remote_declaring_class != nullptr) { |
| 938 | os_ << ", class_status (remote): " << remote_declaring_class->GetStatus(); |
| 939 | } |
| 940 | os_ << "\n"; |
| 941 | } |
| 942 | |
| 943 | DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase); |
| 944 | }; |
| 945 | |
| 946 | template <typename T> |
| 947 | class RegionData : public RegionSpecializedBase<T> { |
| 948 | public: |
| 949 | RegionData(std::ostream* os, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 950 | ArrayRef<uint8_t> remote_contents, |
| 951 | ArrayRef<uint8_t> zygote_contents, |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 952 | const backtrace_map_t& boot_map, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 953 | const ImageHeader& image_header, |
| 954 | bool dump_dirty_objects) |
| 955 | : RegionSpecializedBase<T>(os, |
| 956 | remote_contents, |
| 957 | zygote_contents, |
| 958 | boot_map, |
| 959 | image_header, |
| 960 | dump_dirty_objects), |
| 961 | os_(*os) { |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 962 | CHECK(!remote_contents.empty()); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | // Walk over the type T entries in theregion between begin_image_ptr and end_image_ptr, |
| 966 | // collecting and reporting data regarding dirty, difference, etc. |
| 967 | void ProcessRegion(const MappingData& mapping_data, |
| 968 | RemoteProcesses remotes, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 969 | const uint8_t* begin_image_ptr) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 970 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 971 | typename RegionSpecializedBase<T>::VisitorClass visitor( |
| 972 | [this](T* entry, |
| 973 | const uint8_t* begin_image_ptr, |
| 974 | const std::set<size_t>& dirty_page_set) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 975 | this->ComputeEntryDirty(entry, begin_image_ptr, dirty_page_set); |
| 976 | }, |
| 977 | begin_image_ptr, |
| 978 | mapping_data.dirty_page_set); |
| 979 | PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 980 | RegionSpecializedBase<T>::VisitEntries(&visitor, |
| 981 | const_cast<uint8_t*>(begin_image_ptr), |
| 982 | pointer_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 983 | |
| 984 | // Looking at only dirty pages, figure out how many of those bytes belong to dirty entries. |
| 985 | // TODO: fix this now that there are multiple regions in a mapping. |
| 986 | float true_dirtied_percent = |
| 987 | RegionCommon<T>::GetDirtyEntryBytes() * 1.0f / (mapping_data.dirty_pages * kPageSize); |
| 988 | |
| 989 | // Entry specific statistics. |
| 990 | os_ << RegionCommon<T>::GetDifferentEntryCount() << " different entries, \n " |
| 991 | << RegionCommon<T>::GetDirtyEntryBytes() << " different entry [bytes], \n " |
| 992 | << RegionCommon<T>::GetFalseDirtyEntryCount() << " false dirty entries,\n " |
| 993 | << RegionCommon<T>::GetFalseDirtyEntryBytes() << " false dirty entry [bytes], \n " |
| 994 | << true_dirtied_percent << " different entries-vs-total in a dirty page;\n " |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 995 | << "\n"; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 996 | |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 997 | const uint8_t* base_ptr = begin_image_ptr; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 998 | switch (remotes) { |
| 999 | case RemoteProcesses::kZygoteOnly: |
| 1000 | os_ << " Zygote shared dirty entries: "; |
| 1001 | break; |
| 1002 | case RemoteProcesses::kImageAndZygote: |
| 1003 | os_ << " Application dirty entries (private dirty): "; |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1004 | // If we are dumping private dirty, diff against the zygote map to make it clearer what |
| 1005 | // fields caused the page to be private dirty. |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1006 | base_ptr = RegionCommon<T>::zygote_contents_.data(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1007 | break; |
| 1008 | case RemoteProcesses::kImageOnly: |
| 1009 | os_ << " Application dirty entries (unknown whether private or shared dirty): "; |
| 1010 | break; |
| 1011 | } |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1012 | DiffDirtyEntries(ProcessType::kRemote, |
| 1013 | begin_image_ptr, |
| 1014 | RegionCommon<T>::remote_contents_, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1015 | base_ptr, |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 1016 | /*log_dirty_objects=*/true); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1017 | // Print shared dirty after since it's less important. |
| 1018 | if (RegionCommon<T>::GetZygoteDirtyEntryCount() != 0) { |
| 1019 | // We only reach this point if both pids were specified. Furthermore, |
| 1020 | // entries are only displayed here if they differed in both the image |
| 1021 | // and the zygote, so they are probably private dirty. |
| 1022 | CHECK(remotes == RemoteProcesses::kImageAndZygote); |
| 1023 | os_ << "\n" << " Zygote dirty entries (probably shared dirty): "; |
| 1024 | DiffDirtyEntries(ProcessType::kZygote, |
| 1025 | begin_image_ptr, |
| 1026 | RegionCommon<T>::zygote_contents_, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1027 | begin_image_ptr, |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 1028 | /*log_dirty_objects=*/false); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1029 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1030 | RegionSpecializedBase<T>::DumpDirtyObjects(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1031 | RegionSpecializedBase<T>::DumpDirtyEntries(); |
| 1032 | RegionSpecializedBase<T>::DumpFalseDirtyEntries(); |
| 1033 | RegionSpecializedBase<T>::DumpCleanEntries(); |
| 1034 | } |
| 1035 | |
| 1036 | private: |
| 1037 | std::ostream& os_; |
| 1038 | |
| 1039 | void DiffDirtyEntries(ProcessType process_type, |
| 1040 | const uint8_t* begin_image_ptr, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1041 | ArrayRef<uint8_t> contents, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1042 | const uint8_t* base_ptr, |
| 1043 | bool log_dirty_objects) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1044 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1045 | os_ << RegionCommon<T>::dirty_entries_.size() << "\n"; |
| 1046 | const std::set<T*>& entries = |
| 1047 | (process_type == ProcessType::kZygote) ? |
| 1048 | RegionCommon<T>::zygote_dirty_entries_: |
| 1049 | RegionCommon<T>::image_dirty_entries_; |
| 1050 | for (T* entry : entries) { |
| 1051 | uint8_t* entry_bytes = reinterpret_cast<uint8_t*>(entry); |
| 1052 | ptrdiff_t offset = entry_bytes - begin_image_ptr; |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1053 | uint8_t* remote_bytes = &contents[offset]; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1054 | RegionSpecializedBase<T>::DiffEntryContents(entry, |
| 1055 | remote_bytes, |
| 1056 | &base_ptr[offset], |
| 1057 | log_dirty_objects); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | void ComputeEntryDirty(T* entry, |
| 1062 | const uint8_t* begin_image_ptr, |
| 1063 | const std::set<size_t>& dirty_pages) |
| 1064 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1065 | // Set up pointers in the remote and the zygote for comparison. |
| 1066 | uint8_t* current = reinterpret_cast<uint8_t*>(entry); |
| 1067 | ptrdiff_t offset = current - begin_image_ptr; |
| 1068 | T* entry_remote = |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1069 | reinterpret_cast<T*>(const_cast<uint8_t*>(&RegionCommon<T>::remote_contents_[offset])); |
| 1070 | const bool have_zygote = !RegionCommon<T>::zygote_contents_.empty(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1071 | const uint8_t* current_zygote = |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1072 | have_zygote ? &RegionCommon<T>::zygote_contents_[offset] : nullptr; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1073 | T* entry_zygote = reinterpret_cast<T*>(const_cast<uint8_t*>(current_zygote)); |
| 1074 | // Visit and classify entries at the current location. |
| 1075 | RegionSpecializedBase<T>::VisitEntry(entry); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1076 | |
| 1077 | // Test private dirty first. |
| 1078 | bool is_dirty = false; |
| 1079 | if (have_zygote) { |
Mathieu Chartier | d3b6664 | 2019-06-06 08:11:55 -0700 | [diff] [blame] | 1080 | bool private_dirty = EntriesDiffer(entry, entry_zygote, entry_remote); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1081 | if (private_dirty) { |
| 1082 | // Private dirty, app vs zygote. |
| 1083 | is_dirty = true; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1084 | RegionCommon<T>::AddImageDirtyEntry(entry); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1085 | } |
Mathieu Chartier | d3b6664 | 2019-06-06 08:11:55 -0700 | [diff] [blame] | 1086 | if (EntriesDiffer(entry, entry_zygote, entry)) { |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1087 | // Shared dirty, zygote vs image. |
| 1088 | is_dirty = true; |
| 1089 | RegionCommon<T>::AddZygoteDirtyEntry(entry); |
| 1090 | } |
Mathieu Chartier | d3b6664 | 2019-06-06 08:11:55 -0700 | [diff] [blame] | 1091 | } else if (EntriesDiffer(entry, entry_remote, entry)) { |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1092 | // Shared or private dirty, app vs image. |
| 1093 | is_dirty = true; |
| 1094 | RegionCommon<T>::AddImageDirtyEntry(entry); |
| 1095 | } |
| 1096 | if (is_dirty) { |
| 1097 | // TODO: Add support dirty entries in zygote and image. |
| 1098 | RegionSpecializedBase<T>::AddDirtyEntry(entry, entry_remote); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1099 | } else { |
| 1100 | RegionSpecializedBase<T>::AddCleanEntry(entry); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1101 | if (RegionCommon<T>::IsEntryOnDirtyPage(entry, dirty_pages)) { |
| 1102 | // This entry was either never mutated or got mutated back to the same value. |
| 1103 | // TODO: Do I want to distinguish a "different" vs a "dirty" page here? |
| 1104 | RegionSpecializedBase<T>::AddFalseDirtyEntry(entry); |
| 1105 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | DISALLOW_COPY_AND_ASSIGN(RegionData); |
| 1110 | }; |
| 1111 | |
| 1112 | } // namespace |
| 1113 | |
| 1114 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1115 | class ImgDiagDumper { |
| 1116 | public: |
| 1117 | explicit ImgDiagDumper(std::ostream* os, |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1118 | pid_t image_diff_pid, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1119 | pid_t zygote_diff_pid, |
| 1120 | bool dump_dirty_objects) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1121 | : os_(os), |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1122 | image_diff_pid_(image_diff_pid), |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1123 | zygote_diff_pid_(zygote_diff_pid), |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1124 | dump_dirty_objects_(dump_dirty_objects), |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1125 | zygote_pid_only_(false) {} |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1126 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1127 | bool Init() { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1128 | std::ostream& os = *os_; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 1129 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1130 | if (image_diff_pid_ < 0 && zygote_diff_pid_ < 0) { |
| 1131 | os << "Either --image-diff-pid or --zygote-diff-pid (or both) must be specified.\n"; |
| 1132 | return false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1135 | // To avoid the combinations of command-line argument use cases: |
| 1136 | // If the user invoked with only --zygote-diff-pid, shuffle that to |
| 1137 | // image_diff_pid_, invalidate zygote_diff_pid_, and remember that |
| 1138 | // image_diff_pid_ is now special. |
| 1139 | if (image_diff_pid_ < 0) { |
| 1140 | image_diff_pid_ = zygote_diff_pid_; |
| 1141 | zygote_diff_pid_ = -1; |
| 1142 | zygote_pid_only_ = true; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame] | 1143 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1144 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame] | 1145 | { |
| 1146 | struct stat sts; |
| 1147 | std::string proc_pid_str = |
| 1148 | StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int] |
| 1149 | if (stat(proc_pid_str.c_str(), &sts) == -1) { |
| 1150 | os << "Process does not exist"; |
| 1151 | return false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1155 | auto open_proc_maps = [&os](pid_t pid, /*out*/ std::unique_ptr<BacktraceMap>* proc_maps) { |
| 1156 | // Open /proc/<pid>/maps to view memory maps. |
| 1157 | proc_maps->reset(BacktraceMap::Create(pid)); |
| 1158 | if (*proc_maps == nullptr) { |
| 1159 | os << "Could not read backtrace maps for " << pid; |
| 1160 | return false; |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 1161 | } |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1162 | return true; |
| 1163 | }; |
| 1164 | auto open_file = [&os] (const char* file_name, /*out*/ std::unique_ptr<File>* file) { |
| 1165 | file->reset(OS::OpenFileForReading(file_name)); |
| 1166 | if (*file == nullptr) { |
| 1167 | os << "Failed to open " << file_name << " for reading"; |
| 1168 | return false; |
| 1169 | } |
| 1170 | return true; |
| 1171 | }; |
| 1172 | auto open_mem_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* mem_file) { |
| 1173 | // Open /proc/<pid>/mem and for reading remote contents. |
| 1174 | std::string mem_file_name = |
| 1175 | StringPrintf("/proc/%ld/mem", static_cast<long>(pid)); // NOLINT [runtime/int] |
| 1176 | return open_file(mem_file_name.c_str(), mem_file); |
| 1177 | }; |
| 1178 | auto open_pagemap_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* pagemap_file) { |
| 1179 | // Open /proc/<pid>/pagemap. |
| 1180 | std::string pagemap_file_name = StringPrintf( |
| 1181 | "/proc/%ld/pagemap", static_cast<long>(pid)); // NOLINT [runtime/int] |
| 1182 | return open_file(pagemap_file_name.c_str(), pagemap_file); |
| 1183 | }; |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 1184 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1185 | // Open files for inspecting image memory. |
| 1186 | std::unique_ptr<BacktraceMap> image_proc_maps; |
| 1187 | std::unique_ptr<File> image_mem_file; |
| 1188 | std::unique_ptr<File> image_pagemap_file; |
| 1189 | if (!open_proc_maps(image_diff_pid_, &image_proc_maps) || |
| 1190 | !open_mem_file(image_diff_pid_, &image_mem_file) || |
| 1191 | !open_pagemap_file(image_diff_pid_, &image_pagemap_file)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1192 | return false; |
| 1193 | } |
| 1194 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1195 | // If zygote_diff_pid_ != -1, open files for inspecting zygote memory. |
| 1196 | std::unique_ptr<BacktraceMap> zygote_proc_maps; |
| 1197 | std::unique_ptr<File> zygote_mem_file; |
| 1198 | std::unique_ptr<File> zygote_pagemap_file; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1199 | if (zygote_diff_pid_ != -1) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1200 | if (!open_proc_maps(zygote_diff_pid_, &zygote_proc_maps) || |
| 1201 | !open_mem_file(zygote_diff_pid_, &zygote_mem_file) || |
| 1202 | !open_pagemap_file(zygote_diff_pid_, &zygote_pagemap_file)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1203 | return false; |
| 1204 | } |
| 1205 | } |
| 1206 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1207 | std::unique_ptr<File> clean_pagemap_file; |
| 1208 | std::unique_ptr<File> kpageflags_file; |
| 1209 | std::unique_ptr<File> kpagecount_file; |
| 1210 | if (!open_file("/proc/self/pagemap", &clean_pagemap_file) || |
| 1211 | !open_file("/proc/kpageflags", &kpageflags_file) || |
| 1212 | !open_file("/proc/kpagecount", &kpagecount_file)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1213 | return false; |
| 1214 | } |
| 1215 | |
Vladimir Marko | d0430bf | 2019-03-18 10:54:17 +0000 | [diff] [blame] | 1216 | // Note: the boot image is not really clean but close enough. |
| 1217 | // For now, log pages found to be dirty. |
| 1218 | // TODO: Rewrite imgdiag to load boot image without creating a runtime. |
| 1219 | // FIXME: The following does not reliably detect dirty pages. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1220 | Runtime* runtime = Runtime::Current(); |
| 1221 | CHECK(!runtime->ShouldRelocate()); |
| 1222 | size_t total_dirty_pages = 0u; |
| 1223 | for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) { |
| 1224 | const ImageHeader& image_header = space->GetImageHeader(); |
| 1225 | const uint8_t* image_begin = image_header.GetImageBegin(); |
| 1226 | const uint8_t* image_end = AlignUp(image_begin + image_header.GetImageSize(), kPageSize); |
| 1227 | size_t virtual_page_idx_begin = reinterpret_cast<uintptr_t>(image_begin) / kPageSize; |
| 1228 | size_t virtual_page_idx_end = reinterpret_cast<uintptr_t>(image_end) / kPageSize; |
| 1229 | size_t num_virtual_pages = virtual_page_idx_end - virtual_page_idx_begin; |
| 1230 | |
| 1231 | std::string error_msg; |
| 1232 | std::vector<uint64_t> page_frame_numbers(num_virtual_pages); |
| 1233 | if (!GetPageFrameNumbers(clean_pagemap_file.get(), |
| 1234 | virtual_page_idx_begin, |
| 1235 | ArrayRef<uint64_t>(page_frame_numbers), |
| 1236 | &error_msg)) { |
| 1237 | os << "Failed to get page frame numbers for image space " << space->GetImageLocation() |
| 1238 | << ", error: " << error_msg; |
| 1239 | return false; |
| 1240 | } |
| 1241 | |
| 1242 | std::vector<uint64_t> page_flags(num_virtual_pages); |
| 1243 | if (!GetPageFlagsOrCounts(kpageflags_file.get(), |
| 1244 | ArrayRef<const uint64_t>(page_frame_numbers), |
| 1245 | ArrayRef<uint64_t>(page_flags), |
| 1246 | &error_msg)) { |
| 1247 | os << "Failed to get page flags for image space " << space->GetImageLocation() |
| 1248 | << ", error: " << error_msg; |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | size_t num_dirty_pages = 0u; |
| 1253 | std::optional<size_t> first_dirty_page; |
| 1254 | for (size_t i = 0u, size = page_flags.size(); i != size; ++i) { |
| 1255 | if (UNLIKELY((page_flags[i] & kPageFlagsDirtyMask) != 0u)) { |
| 1256 | ++num_dirty_pages; |
| 1257 | if (!first_dirty_page.has_value()) { |
| 1258 | first_dirty_page = i; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | if (num_dirty_pages != 0u) { |
| 1263 | DCHECK(first_dirty_page.has_value()); |
| 1264 | os << "Found " << num_dirty_pages << " dirty pages for " << space->GetImageLocation() |
| 1265 | << ", first dirty page: " << first_dirty_page.value_or(0u); |
| 1266 | total_dirty_pages += num_dirty_pages; |
| 1267 | } |
| 1268 | } |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1269 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1270 | // Commit the mappings and files. |
| 1271 | image_proc_maps_ = std::move(image_proc_maps); |
| 1272 | image_mem_file_ = std::move(*image_mem_file); |
| 1273 | image_pagemap_file_ = std::move(*image_pagemap_file); |
| 1274 | if (zygote_diff_pid_ != -1) { |
| 1275 | zygote_proc_maps_ = std::move(zygote_proc_maps); |
| 1276 | zygote_mem_file_ = std::move(*zygote_mem_file); |
| 1277 | zygote_pagemap_file_ = std::move(*zygote_pagemap_file); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1278 | } |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1279 | clean_pagemap_file_ = std::move(*clean_pagemap_file); |
| 1280 | kpageflags_file_ = std::move(*kpageflags_file); |
| 1281 | kpagecount_file_ = std::move(*kpagecount_file); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1282 | |
| 1283 | return true; |
| 1284 | } |
| 1285 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1286 | bool Dump(const ImageHeader& image_header, const std::string& image_location) |
| 1287 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1288 | std::ostream& os = *os_; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1289 | os << "IMAGE LOCATION: " << image_location << "\n\n"; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1290 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1291 | os << "MAGIC: " << image_header.GetMagic() << "\n\n"; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1292 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1293 | os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n"; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1294 | |
| 1295 | PrintPidLine("IMAGE", image_diff_pid_); |
| 1296 | os << "\n\n"; |
| 1297 | PrintPidLine("ZYGOTE", zygote_diff_pid_); |
| 1298 | bool ret = true; |
| 1299 | if (image_diff_pid_ >= 0 || zygote_diff_pid_ >= 0) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1300 | ret = DumpImageDiff(image_header, image_location); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1301 | os << "\n\n"; |
| 1302 | } |
| 1303 | |
| 1304 | os << std::flush; |
| 1305 | |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | private: |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1310 | bool DumpImageDiff(const ImageHeader& image_header, const std::string& image_location) |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1311 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1312 | return DumpImageDiffMap(image_header, image_location); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1315 | bool ComputeDirtyBytes(const ImageHeader& image_header, |
| 1316 | const uint8_t* image_begin, |
| 1317 | const backtrace_map_t& boot_map, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1318 | ArrayRef<uint8_t> remote_contents, |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1319 | MappingData* mapping_data /*out*/) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1320 | std::ostream& os = *os_; |
| 1321 | |
| 1322 | size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address) |
| 1323 | size_t page_idx = 0; // Page index relative to 0 |
| 1324 | size_t previous_page_idx = 0; // Previous page index relative to 0 |
| 1325 | |
| 1326 | |
| 1327 | // Iterate through one page at a time. Boot map begin/end already implicitly aligned. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1328 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) { |
| 1329 | ptrdiff_t offset = begin - boot_map.start; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1330 | |
| 1331 | // We treat the image header as part of the memory map for now |
| 1332 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 1333 | // But it might still be interesting to see if any of the ImageHeader data mutated |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1334 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset; |
| 1335 | const uint8_t* remote_ptr = &remote_contents[offset]; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1336 | |
| 1337 | if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1338 | mapping_data->different_pages++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1339 | |
| 1340 | // Count the number of 32-bit integers that are different. |
| 1341 | for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1342 | const uint32_t* remote_ptr_int32 = reinterpret_cast<const uint32_t*>(remote_ptr); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1343 | const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr); |
| 1344 | |
| 1345 | if (remote_ptr_int32[i] != local_ptr_int32[i]) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1346 | mapping_data->different_int32s++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1352 | std::vector<size_t> private_dirty_pages_for_section(ImageHeader::kSectionCount, 0u); |
| 1353 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1354 | // Iterate through one byte at a time. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1355 | ptrdiff_t page_off_begin = image_header.GetImageBegin() - image_begin; |
| 1356 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1357 | previous_page_idx = page_idx; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1358 | ptrdiff_t offset = begin - boot_map.start; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1359 | |
| 1360 | // We treat the image header as part of the memory map for now |
| 1361 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 1362 | // But it might still be interesting to see if any of the ImageHeader data mutated |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1363 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset; |
| 1364 | const uint8_t* remote_ptr = &remote_contents[offset]; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1365 | |
| 1366 | virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize; |
| 1367 | |
| 1368 | // Calculate the page index, relative to the 0th page where the image begins |
| 1369 | page_idx = (offset + page_off_begin) / kPageSize; |
| 1370 | if (*local_ptr != *remote_ptr) { |
| 1371 | // Track number of bytes that are different |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1372 | mapping_data->different_bytes++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | // Independently count the # of dirty pages on the remote side |
| 1376 | size_t remote_virtual_page_idx = begin / kPageSize; |
| 1377 | if (previous_page_idx != page_idx) { |
| 1378 | uint64_t page_count = 0xC0FFEE; |
| 1379 | // TODO: virtual_page_idx needs to be from the same process |
| 1380 | std::string error_msg; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1381 | int dirtiness = (IsPageDirty(&image_pagemap_file_, // Image-diff-pid procmap |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1382 | &clean_pagemap_file_, // Self procmap |
| 1383 | &kpageflags_file_, |
| 1384 | &kpagecount_file_, |
| 1385 | remote_virtual_page_idx, // potentially "dirty" page |
| 1386 | virtual_page_idx, // true "clean" page |
| 1387 | &page_count, |
| 1388 | &error_msg)); |
| 1389 | if (dirtiness < 0) { |
| 1390 | os << error_msg; |
| 1391 | return false; |
| 1392 | } else if (dirtiness > 0) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1393 | mapping_data->dirty_pages++; |
| 1394 | mapping_data->dirty_page_set.insert(mapping_data->dirty_page_set.end(), virtual_page_idx); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | bool is_dirty = dirtiness > 0; |
| 1398 | bool is_private = page_count == 1; |
| 1399 | |
| 1400 | if (page_count == 1) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1401 | mapping_data->private_pages++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | if (is_dirty && is_private) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1405 | mapping_data->private_dirty_pages++; |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1406 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1407 | const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1408 | if (image_header.GetImageSection(section).Contains(offset)) { |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1409 | ++private_dirty_pages_for_section[i]; |
| 1410 | } |
| 1411 | } |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1415 | mapping_data->false_dirty_pages = mapping_data->dirty_pages - mapping_data->different_pages; |
| 1416 | // Print low-level (bytes, int32s, pages) statistics. |
| 1417 | os << mapping_data->different_bytes << " differing bytes,\n " |
| 1418 | << mapping_data->different_int32s << " differing int32s,\n " |
| 1419 | << mapping_data->different_pages << " differing pages,\n " |
| 1420 | << mapping_data->dirty_pages << " pages are dirty;\n " |
| 1421 | << mapping_data->false_dirty_pages << " pages are false dirty;\n " |
| 1422 | << mapping_data->private_pages << " pages are private;\n " |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1423 | << mapping_data->private_dirty_pages << " pages are Private_Dirty\n " |
| 1424 | << "\n"; |
| 1425 | |
| 1426 | size_t total_private_dirty_pages = std::accumulate(private_dirty_pages_for_section.begin(), |
| 1427 | private_dirty_pages_for_section.end(), |
| 1428 | 0u); |
| 1429 | os << "Image sections (total private dirty pages " << total_private_dirty_pages << ")\n"; |
| 1430 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1431 | const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1432 | os << section << " " << image_header.GetImageSection(section) |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1433 | << " private dirty pages=" << private_dirty_pages_for_section[i] << "\n"; |
| 1434 | } |
| 1435 | os << "\n"; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1436 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1437 | return true; |
| 1438 | } |
| 1439 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1440 | // Look at /proc/$pid/mem and only diff the things from there |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1441 | bool DumpImageDiffMap(const ImageHeader& image_header, const std::string& image_location) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1442 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1443 | std::ostream& os = *os_; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1444 | std::string error_msg; |
| 1445 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1446 | std::string image_location_base_name = GetImageLocationBaseName(image_location); |
| 1447 | // FIXME: BacktraceMap should provide a const_iterator so that we can take `maps` as const&. |
| 1448 | auto find_boot_map = [&os, &image_location_base_name](BacktraceMap& maps, const char* tag) |
| 1449 | -> std::optional<backtrace_map_t> { |
| 1450 | // Find the memory map for the current boot image component. |
| 1451 | for (const backtrace_map_t* map : maps) { |
Mathieu Chartier | 30c0094 | 2019-09-03 14:23:58 -0700 | [diff] [blame] | 1452 | // The map name ends with ']' if it's an anonymous memmap. We need to special case that |
| 1453 | // to find the boot image map in some cases. |
| 1454 | if (EndsWith(map->name, image_location_base_name) || |
| 1455 | EndsWith(map->name, image_location_base_name + "]")) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1456 | if ((map->flags & PROT_WRITE) != 0) { |
| 1457 | return *map; |
| 1458 | } |
| 1459 | // In actuality there's more than 1 map, but the second one is read-only. |
| 1460 | // The one we care about is the write-able map. |
| 1461 | // The readonly maps are guaranteed to be identical, so its not interesting to compare |
| 1462 | // them. |
| 1463 | } |
| 1464 | } |
| 1465 | os << "Could not find map for " << image_location_base_name << " in " << tag; |
| 1466 | return std::nullopt; |
| 1467 | }; |
| 1468 | |
| 1469 | // Find the current boot image mapping. |
| 1470 | std::optional<backtrace_map_t> maybe_boot_map = find_boot_map(*image_proc_maps_, "image"); |
| 1471 | if (maybe_boot_map == std::nullopt) { |
| 1472 | return false; |
| 1473 | } |
| 1474 | backtrace_map_t boot_map = maybe_boot_map.value_or(backtrace_map_t{}); |
Ian Pedowitz | 2d53643 | 2020-07-22 14:33:00 -0700 | [diff] [blame] | 1475 | // Check the validity of the boot_map_. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1476 | CHECK(boot_map.end >= boot_map.start); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1477 | |
| 1478 | // Adjust the `end` of the mapping. Some other mappings may have been |
| 1479 | // inserted within the image. |
| 1480 | boot_map.end = RoundUp(boot_map.start + image_header.GetImageSize(), kPageSize); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1481 | // The size of the boot image mapping. |
| 1482 | size_t boot_map_size = boot_map.end - boot_map.start; |
| 1483 | |
| 1484 | // If zygote_diff_pid_ != -1, check that the zygote boot map is the same. |
| 1485 | if (zygote_diff_pid_ != -1) { |
| 1486 | std::optional<backtrace_map_t> maybe_zygote_boot_map = |
| 1487 | find_boot_map(*zygote_proc_maps_, "zygote"); |
| 1488 | if (maybe_zygote_boot_map == std::nullopt) { |
| 1489 | return false; |
| 1490 | } |
| 1491 | backtrace_map_t zygote_boot_map = maybe_zygote_boot_map.value_or(backtrace_map_t{}); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1492 | // Adjust the `end` of the mapping. Some other mappings may have been |
| 1493 | // inserted within the image. |
| 1494 | zygote_boot_map.end = RoundUp(zygote_boot_map.start + image_header.GetImageSize(), kPageSize); |
| 1495 | if (zygote_boot_map.start != boot_map.start) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1496 | os << "Zygote boot map does not match image boot map: " |
| 1497 | << "zygote begin " << reinterpret_cast<const void*>(zygote_boot_map.start) |
| 1498 | << ", zygote end " << reinterpret_cast<const void*>(zygote_boot_map.end) |
| 1499 | << ", image begin " << reinterpret_cast<const void*>(boot_map.start) |
| 1500 | << ", image end " << reinterpret_cast<const void*>(boot_map.end); |
| 1501 | return false; |
| 1502 | } |
| 1503 | } |
| 1504 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1505 | // Walk the bytes and diff against our boot image |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1506 | os << "\nObserving boot image header at address " |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1507 | << reinterpret_cast<const void*>(&image_header) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1508 | << "\n\n"; |
| 1509 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1510 | const uint8_t* image_begin_unaligned = image_header.GetImageBegin(); |
| 1511 | const uint8_t* image_end_unaligned = image_begin_unaligned + image_header.GetImageSize(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1512 | |
| 1513 | // Adjust range to nearest page |
| 1514 | const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize); |
| 1515 | const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize); |
| 1516 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1517 | size_t image_size = image_end - image_begin; |
| 1518 | if (image_size != boot_map_size) { |
| 1519 | os << "Remote boot map size does not match local boot map size: " |
| 1520 | << "local size " << image_size |
| 1521 | << ", remote size " << boot_map_size; |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1525 | auto read_contents = [&](File* mem_file, |
| 1526 | /*out*/ MemMap* map, |
| 1527 | /*out*/ ArrayRef<uint8_t>* contents) { |
| 1528 | DCHECK_ALIGNED(boot_map.start, kPageSize); |
| 1529 | DCHECK_ALIGNED(boot_map_size, kPageSize); |
| 1530 | std::string name = "Contents of " + mem_file->GetPath(); |
| 1531 | std::string local_error_msg; |
| 1532 | // We need to use low 4 GiB memory so that we can walk the objects using standard |
| 1533 | // functions that use ObjPtr<> which is checking that it fits into lower 4 GiB. |
| 1534 | *map = MemMap::MapAnonymous(name.c_str(), |
| 1535 | boot_map_size, |
| 1536 | PROT_READ | PROT_WRITE, |
| 1537 | /* low_4gb= */ true, |
| 1538 | &local_error_msg); |
| 1539 | if (!map->IsValid()) { |
| 1540 | os << "Failed to allocate anonymous mapping for " << boot_map_size << " bytes.\n"; |
| 1541 | return false; |
| 1542 | } |
| 1543 | if (!mem_file->PreadFully(map->Begin(), boot_map_size, boot_map.start)) { |
| 1544 | os << "Could not fully read file " << image_mem_file_.GetPath(); |
| 1545 | return false; |
| 1546 | } |
| 1547 | *contents = ArrayRef<uint8_t>(map->Begin(), boot_map_size); |
| 1548 | return true; |
| 1549 | }; |
| 1550 | // The contents of /proc/<image_diff_pid_>/mem. |
| 1551 | MemMap remote_contents_map; |
| 1552 | ArrayRef<uint8_t> remote_contents; |
| 1553 | if (!read_contents(&image_mem_file_, &remote_contents_map, &remote_contents)) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1554 | return false; |
| 1555 | } |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1556 | // The contents of /proc/<zygote_diff_pid_>/mem. |
| 1557 | MemMap zygote_contents_map; |
| 1558 | ArrayRef<uint8_t> zygote_contents; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1559 | if (zygote_diff_pid_ != -1) { |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1560 | if (!read_contents(&zygote_mem_file_, &zygote_contents_map, &zygote_contents)) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1561 | return false; |
| 1562 | } |
| 1563 | } |
| 1564 | |
Mathieu Chartier | d3b6664 | 2019-06-06 08:11:55 -0700 | [diff] [blame] | 1565 | // TODO: We need to update the entire diff to work with the ASLR. b/77856493 |
| 1566 | // Since the images may be relocated, just check the sizes. |
| 1567 | if (static_cast<uintptr_t>(image_end - image_begin) != boot_map.end - boot_map.start) { |
| 1568 | os << "Remote boot map is a different size than local boot map: " << |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1569 | "local begin " << reinterpret_cast<const void*>(image_begin) << |
| 1570 | ", local end " << reinterpret_cast<const void*>(image_end) << |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1571 | ", remote begin " << reinterpret_cast<const void*>(boot_map.start) << |
| 1572 | ", remote end " << reinterpret_cast<const void*>(boot_map.end); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1573 | return false; |
Mathieu Chartier | d3b6664 | 2019-06-06 08:11:55 -0700 | [diff] [blame] | 1574 | // For more validation should also check the ImageHeader from the file |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1577 | MappingData mapping_data; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame] | 1578 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1579 | os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", " |
| 1580 | << reinterpret_cast<void*>(boot_map.end) << ") had:\n "; |
| 1581 | if (!ComputeDirtyBytes(image_header, image_begin, boot_map, remote_contents, &mapping_data)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1582 | return false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1583 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1584 | RemoteProcesses remotes; |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1585 | if (zygote_pid_only_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1586 | remotes = RemoteProcesses::kZygoteOnly; |
| 1587 | } else if (zygote_diff_pid_ > 0) { |
| 1588 | remotes = RemoteProcesses::kImageAndZygote; |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1589 | } else { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1590 | remotes = RemoteProcesses::kImageOnly; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1593 | // Check all the mirror::Object entries in the image. |
| 1594 | RegionData<mirror::Object> object_region_data(os_, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1595 | remote_contents, |
| 1596 | zygote_contents, |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1597 | boot_map, |
| 1598 | image_header, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1599 | dump_dirty_objects_); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1600 | object_region_data.ProcessRegion(mapping_data, |
| 1601 | remotes, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1602 | image_begin_unaligned); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1603 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1604 | // Check all the ArtMethod entries in the image. |
| 1605 | RegionData<ArtMethod> artmethod_region_data(os_, |
Vladimir Marko | 71d614f | 2019-04-01 15:19:40 +0100 | [diff] [blame] | 1606 | remote_contents, |
| 1607 | zygote_contents, |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1608 | boot_map, |
| 1609 | image_header, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1610 | dump_dirty_objects_); |
| 1611 | artmethod_region_data.ProcessRegion(mapping_data, |
| 1612 | remotes, |
| 1613 | image_begin_unaligned); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1614 | return true; |
| 1615 | } |
| 1616 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1617 | // Note: On failure, `*page_frame_number` shall be clobbered. |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1618 | static bool GetPageFrameNumber(File* page_map_file, |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1619 | size_t virtual_page_index, |
| 1620 | /*out*/ uint64_t* page_frame_number, |
| 1621 | /*out*/ std::string* error_msg) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1622 | CHECK(page_frame_number != nullptr); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1623 | return GetPageFrameNumbers(page_map_file, |
| 1624 | virtual_page_index, |
| 1625 | ArrayRef<uint64_t>(page_frame_number, 1u), |
| 1626 | error_msg); |
| 1627 | } |
| 1628 | |
| 1629 | // Note: On failure, `page_frame_numbers[.]` shall be clobbered. |
| 1630 | static bool GetPageFrameNumbers(File* page_map_file, |
| 1631 | size_t virtual_page_index, |
| 1632 | /*out*/ ArrayRef<uint64_t> page_frame_numbers, |
| 1633 | /*out*/ std::string* error_msg) { |
| 1634 | CHECK(page_map_file != nullptr); |
| 1635 | CHECK_NE(page_frame_numbers.size(), 0u); |
| 1636 | CHECK(page_frame_numbers.data() != nullptr); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1637 | CHECK(error_msg != nullptr); |
| 1638 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1639 | // Read 64-bit entries from /proc/$pid/pagemap to get the physical page frame numbers. |
| 1640 | if (!page_map_file->PreadFully(page_frame_numbers.data(), |
| 1641 | page_frame_numbers.size() * kPageMapEntrySize, |
| 1642 | virtual_page_index * kPageMapEntrySize)) { |
| 1643 | *error_msg = StringPrintf("Failed to read the virtual page index entries from %s, error: %s", |
| 1644 | page_map_file->GetPath().c_str(), |
| 1645 | strerror(errno)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1646 | return false; |
| 1647 | } |
| 1648 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1649 | // Extract page frame numbers from pagemap entries. |
| 1650 | for (uint64_t& page_frame_number : page_frame_numbers) { |
| 1651 | page_frame_number &= kPageFrameNumberMask; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1652 | } |
| 1653 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1654 | return true; |
| 1655 | } |
| 1656 | |
| 1657 | // Note: On failure, `page_flags_or_counts[.]` shall be clobbered. |
| 1658 | static bool GetPageFlagsOrCounts(File* kpage_file, |
| 1659 | ArrayRef<const uint64_t> page_frame_numbers, |
| 1660 | /*out*/ ArrayRef<uint64_t> page_flags_or_counts, |
| 1661 | /*out*/ std::string* error_msg) { |
| 1662 | static_assert(kPageFlagsEntrySize == kPageCountEntrySize, "entry size check"); |
| 1663 | CHECK_NE(page_frame_numbers.size(), 0u); |
| 1664 | CHECK_EQ(page_flags_or_counts.size(), page_frame_numbers.size()); |
| 1665 | CHECK(kpage_file != nullptr); |
| 1666 | CHECK(page_frame_numbers.data() != nullptr); |
| 1667 | CHECK(page_flags_or_counts.data() != nullptr); |
| 1668 | CHECK(error_msg != nullptr); |
| 1669 | |
| 1670 | size_t size = page_frame_numbers.size(); |
| 1671 | size_t i = 0; |
| 1672 | while (i != size) { |
| 1673 | size_t start = i; |
| 1674 | ++i; |
| 1675 | while (i != size && page_frame_numbers[i] - page_frame_numbers[start] == i - start) { |
| 1676 | ++i; |
| 1677 | } |
| 1678 | // Read 64-bit entries from /proc/kpageflags or /proc/kpagecount. |
| 1679 | if (!kpage_file->PreadFully(page_flags_or_counts.data() + start, |
| 1680 | (i - start) * kPageMapEntrySize, |
| 1681 | page_frame_numbers[start] * kPageFlagsEntrySize)) { |
| 1682 | *error_msg = StringPrintf("Failed to read the page flags or counts from %s, error: %s", |
| 1683 | kpage_file->GetPath().c_str(), |
| 1684 | strerror(errno)); |
| 1685 | return false; |
| 1686 | } |
| 1687 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1688 | |
| 1689 | return true; |
| 1690 | } |
| 1691 | |
| 1692 | static int IsPageDirty(File* page_map_file, |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1693 | File* clean_pagemap_file, |
| 1694 | File* kpageflags_file, |
| 1695 | File* kpagecount_file, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1696 | size_t virtual_page_idx, |
| 1697 | size_t clean_virtual_page_idx, |
| 1698 | // Out parameters: |
| 1699 | uint64_t* page_count, std::string* error_msg) { |
| 1700 | CHECK(page_map_file != nullptr); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1701 | CHECK(clean_pagemap_file != nullptr); |
| 1702 | CHECK_NE(page_map_file, clean_pagemap_file); |
| 1703 | CHECK(kpageflags_file != nullptr); |
| 1704 | CHECK(kpagecount_file != nullptr); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1705 | CHECK(page_count != nullptr); |
| 1706 | CHECK(error_msg != nullptr); |
| 1707 | |
| 1708 | // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt |
| 1709 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1710 | uint64_t page_frame_number = 0; |
| 1711 | if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) { |
| 1712 | return -1; |
| 1713 | } |
| 1714 | |
| 1715 | uint64_t page_frame_number_clean = 0; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1716 | if (!GetPageFrameNumber(clean_pagemap_file, clean_virtual_page_idx, &page_frame_number_clean, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1717 | error_msg)) { |
| 1718 | return -1; |
| 1719 | } |
| 1720 | |
| 1721 | // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page |
| 1722 | uint64_t kpage_flags_entry = 0; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1723 | if (!kpageflags_file->PreadFully(&kpage_flags_entry, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1724 | kPageFlagsEntrySize, |
| 1725 | page_frame_number * kPageFlagsEntrySize)) { |
| 1726 | *error_msg = StringPrintf("Failed to read the page flags from %s", |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1727 | kpageflags_file->GetPath().c_str()); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1728 | return -1; |
| 1729 | } |
| 1730 | |
| 1731 | // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1732 | if (!kpagecount_file->PreadFully(page_count /*out*/, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1733 | kPageCountEntrySize, |
| 1734 | page_frame_number * kPageCountEntrySize)) { |
| 1735 | *error_msg = StringPrintf("Failed to read the page count from %s", |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1736 | kpagecount_file->GetPath().c_str()); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1737 | return -1; |
| 1738 | } |
| 1739 | |
| 1740 | // There must be a page frame at the requested address. |
| 1741 | CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u); |
| 1742 | // The page frame must be memory mapped |
| 1743 | CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u); |
| 1744 | |
| 1745 | // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1 |
| 1746 | bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0; |
| 1747 | |
| 1748 | // page_frame_number_clean must come from the *same* process |
| 1749 | // but a *different* mmap than page_frame_number |
| 1750 | if (flags_dirty) { |
Vladimir Marko | f421a90 | 2019-04-01 15:51:18 +0100 | [diff] [blame] | 1751 | // FIXME: This check sometimes fails and the reason is not understood. b/123852774 |
| 1752 | if (page_frame_number != page_frame_number_clean) { |
| 1753 | LOG(ERROR) << "Check failed: page_frame_number != page_frame_number_clean " |
| 1754 | << "(page_frame_number=" << page_frame_number |
| 1755 | << ", page_frame_number_clean=" << page_frame_number_clean << ")" |
| 1756 | << " count: " << *page_count << " flags: 0x" << std::hex << kpage_flags_entry; |
| 1757 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1758 | } |
| 1759 | |
Andreas Gampe | 7c5acbb | 2018-09-20 13:54:52 -0700 | [diff] [blame] | 1760 | return (page_frame_number != page_frame_number_clean) ? 1 : 0; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1763 | void PrintPidLine(const std::string& kind, pid_t pid) { |
| 1764 | if (pid < 0) { |
| 1765 | *os_ << kind << " DIFF PID: disabled\n\n"; |
| 1766 | } else { |
| 1767 | *os_ << kind << " DIFF PID (" << pid << "): "; |
| 1768 | } |
| 1769 | } |
| 1770 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1771 | // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar) |
| 1772 | static std::string BaseName(const std::string& str) { |
| 1773 | size_t idx = str.rfind('/'); |
| 1774 | if (idx == std::string::npos) { |
| 1775 | return str; |
| 1776 | } |
| 1777 | |
| 1778 | return str.substr(idx + 1); |
| 1779 | } |
| 1780 | |
David Srbecky | 6355d69 | 2020-03-26 14:10:26 +0000 | [diff] [blame] | 1781 | // Return the image location, stripped of any directories, e.g. "boot.art" |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1782 | static std::string GetImageLocationBaseName(const std::string& image_location) { |
| 1783 | return BaseName(std::string(image_location)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1784 | } |
| 1785 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1786 | static constexpr size_t kPageMapEntrySize = sizeof(uint64_t); |
| 1787 | // bits 0-54 [in /proc/$pid/pagemap] |
| 1788 | static constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; |
| 1789 | |
| 1790 | static constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t); |
| 1791 | static constexpr size_t kPageCountEntrySize = sizeof(uint64_t); |
| 1792 | static constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags |
| 1793 | static constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags |
| 1794 | static constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags |
| 1795 | |
| 1796 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1797 | std::ostream* os_; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1798 | 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] | 1799 | pid_t zygote_diff_pid_; // Dump image diff against zygote boot.art if pid is non-negative |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1800 | bool dump_dirty_objects_; // Adds dumping of objects that are dirty. |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1801 | 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] | 1802 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1803 | // BacktraceMap used for finding the memory mapping of the image file. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1804 | std::unique_ptr<BacktraceMap> image_proc_maps_; |
| 1805 | // A File for reading /proc/<image_diff_pid_>/mem. |
| 1806 | File image_mem_file_; |
| 1807 | // A File for reading /proc/<image_diff_pid_>/pagemap. |
| 1808 | File image_pagemap_file_; |
| 1809 | |
| 1810 | // BacktraceMap used for finding the memory mapping of the zygote image file. |
| 1811 | std::unique_ptr<BacktraceMap> zygote_proc_maps_; |
| 1812 | // A File for reading /proc/<zygote_diff_pid_>/mem. |
| 1813 | File zygote_mem_file_; |
| 1814 | // A File for reading /proc/<zygote_diff_pid_>/pagemap. |
| 1815 | File zygote_pagemap_file_; |
| 1816 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1817 | // A File for reading /proc/self/pagemap. |
| 1818 | File clean_pagemap_file_; |
| 1819 | // A File for reading /proc/kpageflags. |
| 1820 | File kpageflags_file_; |
| 1821 | // A File for reading /proc/kpagecount. |
| 1822 | File kpagecount_file_; |
| 1823 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1824 | DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper); |
| 1825 | }; |
| 1826 | |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1827 | static int DumpImage(Runtime* runtime, |
| 1828 | std::ostream* os, |
| 1829 | pid_t image_diff_pid, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1830 | pid_t zygote_diff_pid, |
| 1831 | bool dump_dirty_objects) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1832 | ScopedObjectAccess soa(Thread::Current()); |
| 1833 | gc::Heap* heap = runtime->GetHeap(); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1834 | const std::vector<gc::space::ImageSpace*>& image_spaces = heap->GetBootImageSpaces(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1835 | CHECK(!image_spaces.empty()); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1836 | ImgDiagDumper img_diag_dumper(os, |
| 1837 | image_diff_pid, |
| 1838 | zygote_diff_pid, |
| 1839 | dump_dirty_objects); |
| 1840 | if (!img_diag_dumper.Init()) { |
| 1841 | return EXIT_FAILURE; |
| 1842 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1843 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 1844 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1845 | if (!image_header.IsValid()) { |
| 1846 | fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str()); |
| 1847 | return EXIT_FAILURE; |
| 1848 | } |
| 1849 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1850 | if (!img_diag_dumper.Dump(image_header, image_space->GetImageLocation())) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1851 | return EXIT_FAILURE; |
| 1852 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1853 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1854 | return EXIT_SUCCESS; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | struct ImgDiagArgs : public CmdlineArgs { |
| 1858 | protected: |
| 1859 | using Base = CmdlineArgs; |
| 1860 | |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1861 | ParseStatus ParseCustom(const char* raw_option, |
| 1862 | size_t raw_option_length, |
| 1863 | std::string* error_msg) override { |
| 1864 | DCHECK_EQ(strlen(raw_option), raw_option_length); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1865 | { |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1866 | ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1867 | if (base_parse != kParseUnknownArgument) { |
| 1868 | return base_parse; |
| 1869 | } |
| 1870 | } |
| 1871 | |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1872 | std::string_view option(raw_option, raw_option_length); |
| 1873 | if (StartsWith(option, "--image-diff-pid=")) { |
| 1874 | const char* image_diff_pid = raw_option + strlen("--image-diff-pid="); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1875 | |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 1876 | if (!android::base::ParseInt(image_diff_pid, &image_diff_pid_)) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1877 | *error_msg = "Image diff pid out of range"; |
| 1878 | return kParseError; |
| 1879 | } |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1880 | } else if (StartsWith(option, "--zygote-diff-pid=")) { |
| 1881 | const char* zygote_diff_pid = raw_option + strlen("--zygote-diff-pid="); |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1882 | |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 1883 | if (!android::base::ParseInt(zygote_diff_pid, &zygote_diff_pid_)) { |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1884 | *error_msg = "Zygote diff pid out of range"; |
| 1885 | return kParseError; |
| 1886 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1887 | } else if (option == "--dump-dirty-objects") { |
| 1888 | dump_dirty_objects_ = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1889 | } else { |
| 1890 | return kParseUnknownArgument; |
| 1891 | } |
| 1892 | |
| 1893 | return kParseOk; |
| 1894 | } |
| 1895 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 1896 | ParseStatus ParseChecks(std::string* error_msg) override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1897 | // Perform the parent checks. |
| 1898 | ParseStatus parent_checks = Base::ParseChecks(error_msg); |
| 1899 | if (parent_checks != kParseOk) { |
| 1900 | return parent_checks; |
| 1901 | } |
| 1902 | |
| 1903 | // Perform our own checks. |
| 1904 | |
| 1905 | if (kill(image_diff_pid_, |
| 1906 | /*sig*/0) != 0) { // No signal is sent, perform error-checking only. |
| 1907 | // Check if the pid exists before proceeding. |
| 1908 | if (errno == ESRCH) { |
| 1909 | *error_msg = "Process specified does not exist"; |
| 1910 | } else { |
| 1911 | *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno)); |
| 1912 | } |
| 1913 | return kParseError; |
Andreas Gampe | 8fae4b5 | 2017-09-27 20:04:47 -0700 | [diff] [blame] | 1914 | } else if (instruction_set_ != InstructionSet::kNone && instruction_set_ != kRuntimeISA) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1915 | // Don't allow different ISAs since the images are ISA-specific. |
| 1916 | // Right now the code assumes both the runtime ISA and the remote ISA are identical. |
| 1917 | *error_msg = "Must use the default runtime ISA; changing ISA is not supported."; |
| 1918 | return kParseError; |
| 1919 | } |
| 1920 | |
| 1921 | return kParseOk; |
| 1922 | } |
| 1923 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1924 | std::string GetUsage() const override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1925 | std::string usage; |
| 1926 | |
| 1927 | usage += |
| 1928 | "Usage: imgdiag [options] ...\n" |
| 1929 | " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n" |
| 1930 | " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n" |
| 1931 | "\n"; |
| 1932 | |
| 1933 | usage += Base::GetUsage(); |
| 1934 | |
| 1935 | usage += // Optional. |
| 1936 | " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n" |
| 1937 | " Example: --image-diff-pid=$(pid zygote)\n" |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1938 | " --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff " |
| 1939 | "against.\n" |
| 1940 | " Example: --zygote-diff-pid=$(pid zygote)\n" |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1941 | " --dump-dirty-objects: additionally output dirty objects of interest.\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1942 | "\n"; |
| 1943 | |
| 1944 | return usage; |
| 1945 | } |
| 1946 | |
| 1947 | public: |
| 1948 | pid_t image_diff_pid_ = -1; |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1949 | pid_t zygote_diff_pid_ = -1; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1950 | bool dump_dirty_objects_ = false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1951 | }; |
| 1952 | |
| 1953 | struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> { |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1954 | bool ExecuteWithRuntime(Runtime* runtime) override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1955 | CHECK(args_ != nullptr); |
| 1956 | |
| 1957 | return DumpImage(runtime, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1958 | args_->os_, |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1959 | args_->image_diff_pid_, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1960 | args_->zygote_diff_pid_, |
| 1961 | args_->dump_dirty_objects_) == EXIT_SUCCESS; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1962 | } |
| 1963 | }; |
| 1964 | |
| 1965 | } // namespace art |
| 1966 | |
| 1967 | int main(int argc, char** argv) { |
| 1968 | art::ImgDiagMain main; |
| 1969 | return main.Main(argc, argv); |
| 1970 | } |