blob: 7f949f671a38756cf546753184331ea5b52b1c6c [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{
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070039 assert(visitor != NULL);
40 assert(table != NULL);
41 dvmHashTableLock(table);
Carl Shapirof9fa8c12011-04-08 16:40:54 -070042 for (int i = 0; i < table->tableSize; ++i) {
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070043 HashEntry *entry = &table->pEntries[i];
44 if (entry->data != NULL && entry->data != HASH_TOMBSTONE) {
Carl Shapiro07018e22010-10-26 21:07:41 -070045 (*visitor)(&entry->data, 0, type, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070046 }
47 }
48 dvmHashTableUnlock(table);
49}
50
51/*
52 * Visits all entries in the reference table.
53 */
Carl Shapiro07018e22010-10-26 21:07:41 -070054static void visitReferenceTable(RootVisitor *visitor, ReferenceTable *table,
55 u4 threadId, RootType type, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070056{
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070057 assert(visitor != NULL);
58 assert(table != NULL);
Carl Shapirof9fa8c12011-04-08 16:40:54 -070059 for (Object **entry = table->table; entry < table->nextEntry; ++entry) {
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070060 assert(entry != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -070061 (*visitor)(entry, threadId, type, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070062 }
63}
64
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -080065/*
66 * Visits all entries in the indirect reference table.
67 */
68static void visitIndirectRefTable(RootVisitor *visitor, IndirectRefTable *table,
69 u4 threadId, RootType type, void *arg)
70{
71 assert(visitor != NULL);
72 assert(table != NULL);
73 Object **entry = table->table;
74 int numEntries = dvmIndirectRefTableEntries(table);
Carl Shapirof9fa8c12011-04-08 16:40:54 -070075 for (int i = 0; i < numEntries; ++i) {
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -080076 (*visitor)(&entry[i], threadId, type, arg);
77 }
78}
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -080079
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070080/*
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -080081 * Visits all stack slots except those belonging to native method
82 * arguments.
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070083 */
Carl Shapiro07018e22010-10-26 21:07:41 -070084static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070085{
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070086 assert(visitor != NULL);
87 assert(thread != NULL);
Carl Shapirof9fa8c12011-04-08 16:40:54 -070088 u4 threadId = thread->threadId;
89 const StackSaveArea *saveArea;
buzbee30bc0d42011-04-22 10:27:14 -070090 for (u4 *fp = (u4 *)thread->interpSave.curFrame;
Carl Shapirof9fa8c12011-04-08 16:40:54 -070091 fp != NULL;
92 fp = (u4 *)saveArea->prevFrame) {
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070093 Method *method;
Carl Shapiro07018e22010-10-26 21:07:41 -070094 saveArea = SAVEAREA_FROM_FP(fp);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070095 method = (Method *)saveArea->method;
96 if (method != NULL && !dvmIsNativeMethod(method)) {
97 const RegisterMap* pMap = dvmGetExpandedRegisterMap(method);
98 const u1* regVector = NULL;
Carl Shapiro1fbfcab2010-07-22 17:29:23 -070099 if (pMap != NULL) {
100 /* found map, get registers for this address */
101 int addr = saveArea->xtra.currentPc - method->insns;
102 regVector = dvmRegisterMapGetLine(pMap, addr);
103 }
104 if (regVector == NULL) {
105 /*
106 * Either there was no register map or there is no
107 * info for the current PC. Perform a conservative
108 * scan.
109 */
Carl Shapirof9fa8c12011-04-08 16:40:54 -0700110 for (size_t i = 0; i < method->registersSize; ++i) {
Carl Shapiro07018e22010-10-26 21:07:41 -0700111 if (dvmIsValidObject((Object *)fp[i])) {
112 (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700113 }
114 }
115 } else {
116 /*
117 * Precise scan. v0 is at the lowest address on the
118 * interpreted stack, and is the first bit in the
119 * register vector, so we can walk through the
120 * register map and memory in the same direction.
121 *
122 * A '1' bit indicates a live reference.
123 */
124 u2 bits = 1 << 1;
Carl Shapirof9fa8c12011-04-08 16:40:54 -0700125 for (size_t i = 0; i < method->registersSize; ++i) {
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700126 bits >>= 1;
127 if (bits == 1) {
128 /* set bit 9 so we can tell when we're empty */
129 bits = *regVector++ | 0x0100;
130 }
131 if ((bits & 0x1) != 0) {
132 /*
133 * Register is marked as live, it's a valid root.
134 */
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800135#if WITH_EXTRA_GC_CHECKS
136 if (fp[i] != 0 && !dvmIsValidObject((Object *)fp[i])) {
137 /* this is very bad */
138 LOGE("PGC: invalid ref in reg %d: %#x",
139 method->registersSize - 1 - i, fp[i]);
140 LOGE("PGC: %s.%s addr %#x",
141 method->clazz->descriptor, method->name,
142 saveArea->xtra.currentPc - method->insns);
143 continue;
144 }
145#endif
Carl Shapiro07018e22010-10-26 21:07:41 -0700146 (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700147 }
148 }
149 dvmReleaseRegisterMapLine(pMap, regVector);
150 }
151 }
152 /*
153 * Don't fall into an infinite loop if things get corrupted.
154 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700155 assert((uintptr_t)saveArea->prevFrame > (uintptr_t)fp ||
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700156 saveArea->prevFrame == NULL);
157 }
158}
159
160/*
161 * Visits all roots associated with a thread.
162 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700163static void visitThread(RootVisitor *visitor, Thread *thread, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700164{
Carl Shapiro07018e22010-10-26 21:07:41 -0700165 u4 threadId;
166
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700167 assert(visitor != NULL);
168 assert(thread != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700169 threadId = thread->threadId;
170 (*visitor)(&thread->threadObj, threadId, ROOT_THREAD_OBJECT, arg);
171 (*visitor)(&thread->exception, threadId, ROOT_NATIVE_STACK, arg);
172 visitReferenceTable(visitor, &thread->internalLocalRefTable, threadId, ROOT_NATIVE_STACK, arg);
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -0800173 visitIndirectRefTable(visitor, &thread->jniLocalRefTable, threadId, ROOT_JNI_LOCAL, arg);
Carl Shapiro07018e22010-10-26 21:07:41 -0700174 if (thread->jniMonitorRefTable.table != NULL) {
175 visitReferenceTable(visitor, &thread->jniMonitorRefTable, threadId, ROOT_JNI_MONITOR, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700176 }
177 visitThreadStack(visitor, thread, arg);
178}
179
180/*
181 * Visits all threads on the thread list.
182 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700183static void visitThreads(RootVisitor *visitor, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700184{
185 Thread *thread;
186
187 assert(visitor != NULL);
188 dvmLockThreadList(dvmThreadSelf());
189 thread = gDvm.threadList;
190 while (thread) {
191 visitThread(visitor, thread, arg);
192 thread = thread->next;
193 }
194 dvmUnlockThreadList();
195}
196
Dan Bornsteina9c49df2011-03-11 12:58:05 -0800197static void visitPrimitiveTypes(RootVisitor *visitor, void *arg)
198{
Carl Shapiro19007bb2011-03-22 18:19:05 -0700199 (*visitor)(&gDvm.typeVoid, 0, ROOT_STICKY_CLASS, arg);
Dan Bornsteina9c49df2011-03-11 12:58:05 -0800200 (*visitor)(&gDvm.typeBoolean, 0, ROOT_STICKY_CLASS, arg);
Carl Shapiro19007bb2011-03-22 18:19:05 -0700201 (*visitor)(&gDvm.typeByte, 0, ROOT_STICKY_CLASS, arg);
202 (*visitor)(&gDvm.typeShort, 0, ROOT_STICKY_CLASS, arg);
203 (*visitor)(&gDvm.typeChar, 0, ROOT_STICKY_CLASS, arg);
204 (*visitor)(&gDvm.typeInt, 0, ROOT_STICKY_CLASS, arg);
205 (*visitor)(&gDvm.typeLong, 0, ROOT_STICKY_CLASS, arg);
206 (*visitor)(&gDvm.typeFloat, 0, ROOT_STICKY_CLASS, arg);
207 (*visitor)(&gDvm.typeDouble, 0, ROOT_STICKY_CLASS, arg);
Dan Bornsteina9c49df2011-03-11 12:58:05 -0800208}
209
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700210/*
Carl Shapiro07018e22010-10-26 21:07:41 -0700211 * Visits roots. TODO: visit cached global references.
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700212 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700213void dvmVisitRoots(RootVisitor *visitor, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700214{
215 assert(visitor != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700216 visitHashTable(visitor, gDvm.loadedClasses, ROOT_STICKY_CLASS, arg);
Dan Bornsteina9c49df2011-03-11 12:58:05 -0800217 visitPrimitiveTypes(visitor, arg);
Carl Shapiroca14ee32010-10-29 21:32:52 -0700218 if (gDvm.dbgRegistry != NULL) {
219 visitHashTable(visitor, gDvm.dbgRegistry, ROOT_DEBUGGER, arg);
220 }
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800221 if (gDvm.literalStrings != NULL) {
222 visitHashTable(visitor, gDvm.literalStrings, ROOT_INTERNED_STRING, arg);
223 }
224 dvmLockMutex(&gDvm.jniGlobalRefLock);
Carl Shapiroe4c3b5e2011-03-08 13:44:51 -0800225 visitIndirectRefTable(visitor, &gDvm.jniGlobalRefTable, 0, ROOT_JNI_GLOBAL, arg);
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800226 dvmUnlockMutex(&gDvm.jniGlobalRefLock);
227 dvmLockMutex(&gDvm.jniPinRefLock);
Carl Shapiro1ec987e2010-10-29 15:14:28 -0700228 visitReferenceTable(visitor, &gDvm.jniPinRefTable, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800229 dvmUnlockMutex(&gDvm.jniPinRefLock);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700230 visitThreads(visitor, arg);
Carl Shapiro07018e22010-10-26 21:07:41 -0700231 (*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
232 (*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
233 (*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700234}