blob: e11d583788c90fd6105cfd4ab4590681590e31a9 [file] [log] [blame]
Carl Shapiroadc346f2010-06-03 23:01:39 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Dalvik.h"
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070018#include "alloc/HeapInternal.h"
Carl Shapiroadc346f2010-06-03 23:01:39 -070019#include "alloc/Visit.h"
Carl Shapiroddb0c1c2010-07-13 17:36:41 -070020#include "alloc/VisitInlines.h"
Carl Shapiroadc346f2010-06-03 23:01:39 -070021
22/*
Carl Shapiroddb0c1c2010-07-13 17:36:41 -070023 * Visits all of the reference locations in an object.
Carl Shapiroadc346f2010-06-03 23:01:39 -070024 */
Carl Shapiro056b9662010-06-15 14:40:20 -070025void dvmVisitObject(Visitor *visitor, Object *obj, void *arg)
Carl Shapiroadc346f2010-06-03 23:01:39 -070026{
27 assert(visitor != NULL);
28 assert(obj != NULL);
Barry Hayes899cdb72010-06-08 09:59:12 -070029 assert(obj->clazz != NULL);
Carl Shapiroddb0c1c2010-07-13 17:36:41 -070030 visitObject(visitor, obj, arg);
Carl Shapiroadc346f2010-06-03 23:01:39 -070031}
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070032
33/*
34 * Applies a verification function to all present values in the hash table.
35 */
Carl Shapiro07018e22010-10-26 21:07:41 -070036static void visitHashTable(RootVisitor *visitor, HashTable *table,
37 RootType type, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070038{
39 int i;
40
41 assert(visitor != NULL);
42 assert(table != NULL);
43 dvmHashTableLock(table);
44 for (i = 0; i < table->tableSize; ++i) {
45 HashEntry *entry = &table->pEntries[i];
46 if (entry->data != NULL && entry->data != HASH_TOMBSTONE) {
Carl Shapiro07018e22010-10-26 21:07:41 -070047 (*visitor)(&entry->data, 0, type, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070048 }
49 }
50 dvmHashTableUnlock(table);
51}
52
53/*
54 * Visits all entries in the reference table.
55 */
Carl Shapiro07018e22010-10-26 21:07:41 -070056static void visitReferenceTable(RootVisitor *visitor, ReferenceTable *table,
57 u4 threadId, RootType type, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070058{
59 Object **entry;
60
61 assert(visitor != NULL);
62 assert(table != NULL);
63 for (entry = table->table; entry < table->nextEntry; ++entry) {
64 assert(entry != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -070065 (*visitor)(entry, threadId, type, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070066 }
67}
68
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -080069/*
70 * Visits all entries in the indirect reference table.
71 */
72static void visitIndirectRefTable(RootVisitor *visitor, IndirectRefTable *table,
73 u4 threadId, RootType type, void *arg)
74{
75 assert(visitor != NULL);
76 assert(table != NULL);
77 Object **entry = table->table;
78 int numEntries = dvmIndirectRefTableEntries(table);
79 int i;
80 for (i = 0; i < numEntries; ++i) {
81 (*visitor)(&entry[i], threadId, type, arg);
82 }
83}
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -080084
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070085/*
86 * Visits a large heap reference table. These objects are list heads.
87 * As such, it is valid for table to be NULL.
88 */
Carl Shapiro07018e22010-10-26 21:07:41 -070089static void visitLargeHeapRefTable(RootVisitor *visitor,
90 LargeHeapRefTable *table,
91 RootType type, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070092{
93 assert(visitor != NULL);
94 for (; table != NULL; table = table->next) {
Carl Shapiro07018e22010-10-26 21:07:41 -070095 visitReferenceTable(visitor, &table->refs, 0, type, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070096 }
97}
98
99/*
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800100 * Visits all stack slots except those belonging to native method
101 * arguments.
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700102 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700103static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700104{
105 const StackSaveArea *saveArea;
Carl Shapiro07018e22010-10-26 21:07:41 -0700106 u4 *fp;
107 u4 threadId;
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700108
109 assert(visitor != NULL);
110 assert(thread != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700111 threadId = thread->threadId;
112 fp = (u4 *)thread->curFrame;
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800113 for (; fp != NULL; fp = (u4 *)saveArea->prevFrame) {
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700114 Method *method;
Carl Shapiro07018e22010-10-26 21:07:41 -0700115 saveArea = SAVEAREA_FROM_FP(fp);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700116 method = (Method *)saveArea->method;
117 if (method != NULL && !dvmIsNativeMethod(method)) {
118 const RegisterMap* pMap = dvmGetExpandedRegisterMap(method);
119 const u1* regVector = NULL;
120 size_t i;
121
122 if (pMap != NULL) {
123 /* found map, get registers for this address */
124 int addr = saveArea->xtra.currentPc - method->insns;
125 regVector = dvmRegisterMapGetLine(pMap, addr);
126 }
127 if (regVector == NULL) {
128 /*
129 * Either there was no register map or there is no
130 * info for the current PC. Perform a conservative
131 * scan.
132 */
133 for (i = 0; i < method->registersSize; ++i) {
Carl Shapiro07018e22010-10-26 21:07:41 -0700134 if (dvmIsValidObject((Object *)fp[i])) {
135 (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700136 }
137 }
138 } else {
139 /*
140 * Precise scan. v0 is at the lowest address on the
141 * interpreted stack, and is the first bit in the
142 * register vector, so we can walk through the
143 * register map and memory in the same direction.
144 *
145 * A '1' bit indicates a live reference.
146 */
147 u2 bits = 1 << 1;
148 for (i = 0; i < method->registersSize; ++i) {
149 bits >>= 1;
150 if (bits == 1) {
151 /* set bit 9 so we can tell when we're empty */
152 bits = *regVector++ | 0x0100;
153 }
154 if ((bits & 0x1) != 0) {
155 /*
156 * Register is marked as live, it's a valid root.
157 */
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800158#if WITH_EXTRA_GC_CHECKS
159 if (fp[i] != 0 && !dvmIsValidObject((Object *)fp[i])) {
160 /* this is very bad */
161 LOGE("PGC: invalid ref in reg %d: %#x",
162 method->registersSize - 1 - i, fp[i]);
163 LOGE("PGC: %s.%s addr %#x",
164 method->clazz->descriptor, method->name,
165 saveArea->xtra.currentPc - method->insns);
166 continue;
167 }
168#endif
Carl Shapiro07018e22010-10-26 21:07:41 -0700169 (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700170 }
171 }
172 dvmReleaseRegisterMapLine(pMap, regVector);
173 }
174 }
175 /*
176 * Don't fall into an infinite loop if things get corrupted.
177 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700178 assert((uintptr_t)saveArea->prevFrame > (uintptr_t)fp ||
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700179 saveArea->prevFrame == NULL);
180 }
181}
182
183/*
184 * Visits all roots associated with a thread.
185 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700186static void visitThread(RootVisitor *visitor, Thread *thread, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700187{
Carl Shapiro07018e22010-10-26 21:07:41 -0700188 u4 threadId;
189
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700190 assert(visitor != NULL);
191 assert(thread != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700192 threadId = thread->threadId;
193 (*visitor)(&thread->threadObj, threadId, ROOT_THREAD_OBJECT, arg);
194 (*visitor)(&thread->exception, threadId, ROOT_NATIVE_STACK, arg);
195 visitReferenceTable(visitor, &thread->internalLocalRefTable, threadId, ROOT_NATIVE_STACK, arg);
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -0800196 visitIndirectRefTable(visitor, &thread->jniLocalRefTable, threadId, ROOT_JNI_LOCAL, arg);
Carl Shapiro07018e22010-10-26 21:07:41 -0700197 if (thread->jniMonitorRefTable.table != NULL) {
198 visitReferenceTable(visitor, &thread->jniMonitorRefTable, threadId, ROOT_JNI_MONITOR, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700199 }
200 visitThreadStack(visitor, thread, arg);
201}
202
203/*
204 * Visits all threads on the thread list.
205 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700206static void visitThreads(RootVisitor *visitor, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700207{
208 Thread *thread;
209
210 assert(visitor != NULL);
211 dvmLockThreadList(dvmThreadSelf());
212 thread = gDvm.threadList;
213 while (thread) {
214 visitThread(visitor, thread, arg);
215 thread = thread->next;
216 }
217 dvmUnlockThreadList();
218}
219
Dan Bornsteina9c49df2011-03-11 12:58:05 -0800220static void visitPrimitiveTypes(RootVisitor *visitor, void *arg)
221{
222 (*visitor)(&gDvm.typeVoid, 0, ROOT_STICKY_CLASS, arg);
223 (*visitor)(&gDvm.typeBoolean, 0, ROOT_STICKY_CLASS, arg);
224 (*visitor)(&gDvm.typeByte, 0, ROOT_STICKY_CLASS, arg);
225 (*visitor)(&gDvm.typeShort, 0, ROOT_STICKY_CLASS, arg);
226 (*visitor)(&gDvm.typeChar, 0, ROOT_STICKY_CLASS, arg);
227 (*visitor)(&gDvm.typeInt, 0, ROOT_STICKY_CLASS, arg);
228 (*visitor)(&gDvm.typeLong, 0, ROOT_STICKY_CLASS, arg);
229 (*visitor)(&gDvm.typeFloat, 0, ROOT_STICKY_CLASS, arg);
230 (*visitor)(&gDvm.typeDouble, 0, ROOT_STICKY_CLASS, arg);
231}
232
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700233/*
Carl Shapiro07018e22010-10-26 21:07:41 -0700234 * Visits roots. TODO: visit cached global references.
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700235 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700236void dvmVisitRoots(RootVisitor *visitor, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700237{
238 assert(visitor != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700239 visitHashTable(visitor, gDvm.loadedClasses, ROOT_STICKY_CLASS, arg);
Dan Bornsteina9c49df2011-03-11 12:58:05 -0800240 visitPrimitiveTypes(visitor, arg);
Carl Shapiroca14ee32010-10-29 21:32:52 -0700241 if (gDvm.dbgRegistry != NULL) {
242 visitHashTable(visitor, gDvm.dbgRegistry, ROOT_DEBUGGER, arg);
243 }
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800244 if (gDvm.literalStrings != NULL) {
245 visitHashTable(visitor, gDvm.literalStrings, ROOT_INTERNED_STRING, arg);
246 }
247 dvmLockMutex(&gDvm.jniGlobalRefLock);
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -0800248 visitIndirectRefTable(visitor, &gDvm.jniGlobalRefTable, 0, ROOT_JNI_GLOBAL, arg);
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800249 dvmUnlockMutex(&gDvm.jniGlobalRefLock);
250 dvmLockMutex(&gDvm.jniPinRefLock);
Carl Shapiro1ec987e2010-10-29 15:14:28 -0700251 visitReferenceTable(visitor, &gDvm.jniPinRefTable, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800252 dvmUnlockMutex(&gDvm.jniPinRefLock);
Carl Shapiro07018e22010-10-26 21:07:41 -0700253 visitLargeHeapRefTable(visitor, gDvm.gcHeap->referenceOperations, ROOT_REFERENCE_CLEANUP, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700254 visitThreads(visitor, arg);
Carl Shapiro07018e22010-10-26 21:07:41 -0700255 (*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
256 (*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
257 (*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700258}