blob: a3179553cc008b582f7a84973b76d96414429312 [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"
18#include "alloc/clz.h"
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070019#include "alloc/HeapInternal.h"
Carl Shapiroadc346f2010-06-03 23:01:39 -070020#include "alloc/Visit.h"
Carl Shapiroddb0c1c2010-07-13 17:36:41 -070021#include "alloc/VisitInlines.h"
Carl Shapiroadc346f2010-06-03 23:01:39 -070022
23/*
Carl Shapiroddb0c1c2010-07-13 17:36:41 -070024 * Visits all of the reference locations in an object.
Carl Shapiroadc346f2010-06-03 23:01:39 -070025 */
Carl Shapiro056b9662010-06-15 14:40:20 -070026void dvmVisitObject(Visitor *visitor, Object *obj, void *arg)
Carl Shapiroadc346f2010-06-03 23:01:39 -070027{
28 assert(visitor != NULL);
29 assert(obj != NULL);
Barry Hayes899cdb72010-06-08 09:59:12 -070030 assert(obj->clazz != NULL);
Carl Shapiroddb0c1c2010-07-13 17:36:41 -070031 visitObject(visitor, obj, arg);
Carl Shapiroadc346f2010-06-03 23:01:39 -070032}
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070033
34/*
35 * Applies a verification function to all present values in the hash table.
36 */
Carl Shapiro07018e22010-10-26 21:07:41 -070037static void visitHashTable(RootVisitor *visitor, HashTable *table,
38 RootType type, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070039{
40 int i;
41
42 assert(visitor != NULL);
43 assert(table != NULL);
44 dvmHashTableLock(table);
45 for (i = 0; i < table->tableSize; ++i) {
46 HashEntry *entry = &table->pEntries[i];
47 if (entry->data != NULL && entry->data != HASH_TOMBSTONE) {
Carl Shapiro07018e22010-10-26 21:07:41 -070048 (*visitor)(&entry->data, 0, type, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070049 }
50 }
51 dvmHashTableUnlock(table);
52}
53
54/*
Carl Shapiro07018e22010-10-26 21:07:41 -070055 * Applies a verification function to all elements in the array.
56 */
57static void visitArray(RootVisitor *visitor, Object **array, size_t length,
58 RootType type, void *arg)
59{
60 size_t i;
61
62 assert(visitor != NULL);
63 assert(array != NULL);
64 for (i = 0; i < length; ++i) {
65 (*visitor)(&array[i], 0, type, arg);
66 }
67}
68
69/*
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070070 * Visits all entries in the reference table.
71 */
Carl Shapiro07018e22010-10-26 21:07:41 -070072static void visitReferenceTable(RootVisitor *visitor, ReferenceTable *table,
73 u4 threadId, RootType type, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070074{
75 Object **entry;
76
77 assert(visitor != NULL);
78 assert(table != NULL);
79 for (entry = table->table; entry < table->nextEntry; ++entry) {
80 assert(entry != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -070081 (*visitor)(entry, threadId, type, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070082 }
83}
84
85/*
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;
113 for (; fp != NULL; fp = 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);
196 visitReferenceTable(visitor, &thread->jniLocalRefTable, threadId, ROOT_JNI_LOCAL, arg);
197 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
220/*
Carl Shapiro07018e22010-10-26 21:07:41 -0700221 * Visits roots. TODO: visit cached global references.
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700222 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700223void dvmVisitRoots(RootVisitor *visitor, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700224{
225 assert(visitor != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700226 visitHashTable(visitor, gDvm.loadedClasses, ROOT_STICKY_CLASS, arg);
227 visitArray(visitor, (Object **)gDvm.primitiveClass, NELEM(gDvm.primitiveClass), ROOT_STICKY_CLASS, arg);
Carl Shapiroca14ee32010-10-29 21:32:52 -0700228 if (gDvm.dbgRegistry != NULL) {
229 visitHashTable(visitor, gDvm.dbgRegistry, ROOT_DEBUGGER, arg);
230 }
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800231 if (gDvm.literalStrings != NULL) {
232 visitHashTable(visitor, gDvm.literalStrings, ROOT_INTERNED_STRING, arg);
233 }
234 dvmLockMutex(&gDvm.jniGlobalRefLock);
Carl Shapiro07018e22010-10-26 21:07:41 -0700235 visitReferenceTable(visitor, &gDvm.jniGlobalRefTable, 0, ROOT_JNI_GLOBAL, arg);
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800236 dvmUnlockMutex(&gDvm.jniGlobalRefLock);
237 dvmLockMutex(&gDvm.jniPinRefLock);
Carl Shapiro1ec987e2010-10-29 15:14:28 -0700238 visitReferenceTable(visitor, &gDvm.jniPinRefTable, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800239 dvmUnlockMutex(&gDvm.jniPinRefLock);
Carl Shapiro07018e22010-10-26 21:07:41 -0700240 visitLargeHeapRefTable(visitor, gDvm.gcHeap->referenceOperations, ROOT_REFERENCE_CLEANUP, arg);
241 visitLargeHeapRefTable(visitor, gDvm.gcHeap->pendingFinalizationRefs, ROOT_FINALIZING, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700242 visitThreads(visitor, arg);
Carl Shapiro07018e22010-10-26 21:07:41 -0700243 (*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
244 (*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
245 (*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700246}