blob: 697cc3fe58b78399c6c3080fc518a9ff2985b1e2 [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/*
100 * Visits all stack slots. TODO: visit native methods.
101 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700102static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700103{
104 const StackSaveArea *saveArea;
Carl Shapiro07018e22010-10-26 21:07:41 -0700105 u4 *fp;
106 u4 threadId;
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700107
108 assert(visitor != NULL);
109 assert(thread != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700110 threadId = thread->threadId;
111 fp = (u4 *)thread->curFrame;
112 for (; fp != NULL; fp = saveArea->prevFrame) {
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700113 Method *method;
Carl Shapiro07018e22010-10-26 21:07:41 -0700114 saveArea = SAVEAREA_FROM_FP(fp);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700115 method = (Method *)saveArea->method;
116 if (method != NULL && !dvmIsNativeMethod(method)) {
117 const RegisterMap* pMap = dvmGetExpandedRegisterMap(method);
118 const u1* regVector = NULL;
119 size_t i;
120
121 if (pMap != NULL) {
122 /* found map, get registers for this address */
123 int addr = saveArea->xtra.currentPc - method->insns;
124 regVector = dvmRegisterMapGetLine(pMap, addr);
125 }
126 if (regVector == NULL) {
127 /*
128 * Either there was no register map or there is no
129 * info for the current PC. Perform a conservative
130 * scan.
131 */
132 for (i = 0; i < method->registersSize; ++i) {
Carl Shapiro07018e22010-10-26 21:07:41 -0700133 if (dvmIsValidObject((Object *)fp[i])) {
134 (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700135 }
136 }
137 } else {
138 /*
139 * Precise scan. v0 is at the lowest address on the
140 * interpreted stack, and is the first bit in the
141 * register vector, so we can walk through the
142 * register map and memory in the same direction.
143 *
144 * A '1' bit indicates a live reference.
145 */
146 u2 bits = 1 << 1;
147 for (i = 0; i < method->registersSize; ++i) {
148 bits >>= 1;
149 if (bits == 1) {
150 /* set bit 9 so we can tell when we're empty */
151 bits = *regVector++ | 0x0100;
152 }
153 if ((bits & 0x1) != 0) {
154 /*
155 * Register is marked as live, it's a valid root.
156 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700157 (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700158 }
159 }
160 dvmReleaseRegisterMapLine(pMap, regVector);
161 }
162 }
163 /*
164 * Don't fall into an infinite loop if things get corrupted.
165 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700166 assert((uintptr_t)saveArea->prevFrame > (uintptr_t)fp ||
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700167 saveArea->prevFrame == NULL);
168 }
169}
170
171/*
172 * Visits all roots associated with a thread.
173 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700174static void visitThread(RootVisitor *visitor, Thread *thread, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700175{
Carl Shapiro07018e22010-10-26 21:07:41 -0700176 u4 threadId;
177
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700178 assert(visitor != NULL);
179 assert(thread != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700180 threadId = thread->threadId;
181 (*visitor)(&thread->threadObj, threadId, ROOT_THREAD_OBJECT, arg);
182 (*visitor)(&thread->exception, threadId, ROOT_NATIVE_STACK, arg);
183 visitReferenceTable(visitor, &thread->internalLocalRefTable, threadId, ROOT_NATIVE_STACK, arg);
184 visitReferenceTable(visitor, &thread->jniLocalRefTable, threadId, ROOT_JNI_LOCAL, arg);
185 if (thread->jniMonitorRefTable.table != NULL) {
186 visitReferenceTable(visitor, &thread->jniMonitorRefTable, threadId, ROOT_JNI_MONITOR, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700187 }
188 visitThreadStack(visitor, thread, arg);
189}
190
191/*
192 * Visits all threads on the thread list.
193 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700194static void visitThreads(RootVisitor *visitor, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700195{
196 Thread *thread;
197
198 assert(visitor != NULL);
199 dvmLockThreadList(dvmThreadSelf());
200 thread = gDvm.threadList;
201 while (thread) {
202 visitThread(visitor, thread, arg);
203 thread = thread->next;
204 }
205 dvmUnlockThreadList();
206}
207
208/*
Carl Shapiro07018e22010-10-26 21:07:41 -0700209 * Visits roots. TODO: visit cached global references.
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700210 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700211void dvmVisitRoots(RootVisitor *visitor, void *arg)
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700212{
213 assert(visitor != NULL);
Carl Shapiro07018e22010-10-26 21:07:41 -0700214 visitHashTable(visitor, gDvm.loadedClasses, ROOT_STICKY_CLASS, arg);
215 visitArray(visitor, (Object **)gDvm.primitiveClass, NELEM(gDvm.primitiveClass), ROOT_STICKY_CLASS, arg);
Carl Shapiroca14ee32010-10-29 21:32:52 -0700216 if (gDvm.dbgRegistry != NULL) {
217 visitHashTable(visitor, gDvm.dbgRegistry, ROOT_DEBUGGER, arg);
218 }
Carl Shapiro07018e22010-10-26 21:07:41 -0700219 visitHashTable(visitor, gDvm.internedStrings, ROOT_INTERNED_STRING, arg);
220 visitHashTable(visitor, gDvm.literalStrings, ROOT_INTERNED_STRING, arg);
221 visitReferenceTable(visitor, &gDvm.jniGlobalRefTable, 0, ROOT_JNI_GLOBAL, arg);
Carl Shapiro1ec987e2010-10-29 15:14:28 -0700222 visitReferenceTable(visitor, &gDvm.jniPinRefTable, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro07018e22010-10-26 21:07:41 -0700223 visitLargeHeapRefTable(visitor, gDvm.gcHeap->referenceOperations, ROOT_REFERENCE_CLEANUP, arg);
224 visitLargeHeapRefTable(visitor, gDvm.gcHeap->pendingFinalizationRefs, ROOT_FINALIZING, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700225 visitThreads(visitor, arg);
Carl Shapiro07018e22010-10-26 21:07:41 -0700226 (*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
227 (*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
228 (*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
Carl Shapiro1fbfcab2010-07-22 17:29:23 -0700229}