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