| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 19 | #include "alloc/HeapInternal.h" |
| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 20 | #include "alloc/Visit.h" |
| Carl Shapiro | ddb0c1c | 2010-07-13 17:36:41 -0700 | [diff] [blame] | 21 | #include "alloc/VisitInlines.h" |
| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| Carl Shapiro | ddb0c1c | 2010-07-13 17:36:41 -0700 | [diff] [blame] | 24 | * Visits all of the reference locations in an object. |
| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 25 | */ |
| Carl Shapiro | 056b966 | 2010-06-15 14:40:20 -0700 | [diff] [blame] | 26 | void dvmVisitObject(Visitor *visitor, Object *obj, void *arg) |
| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 27 | { |
| 28 | assert(visitor != NULL); |
| 29 | assert(obj != NULL); |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 30 | assert(obj->clazz != NULL); |
| Carl Shapiro | ddb0c1c | 2010-07-13 17:36:41 -0700 | [diff] [blame] | 31 | visitObject(visitor, obj, arg); |
| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 32 | } |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Applies a verification function to all present values in the hash table. |
| 36 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 37 | static void visitHashTable(RootVisitor *visitor, HashTable *table, |
| 38 | RootType type, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 39 | { |
| 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 48 | (*visitor)(&entry->data, 0, type, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | dvmHashTableUnlock(table); |
| 52 | } |
| 53 | |
| 54 | /* |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 55 | * Applies a verification function to all elements in the array. |
| 56 | */ |
| 57 | static 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 Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 70 | * Visits all entries in the reference table. |
| 71 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 72 | static void visitReferenceTable(RootVisitor *visitor, ReferenceTable *table, |
| 73 | u4 threadId, RootType type, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 74 | { |
| 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 81 | (*visitor)(entry, threadId, type, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 82 | } |
| 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 89 | static void visitLargeHeapRefTable(RootVisitor *visitor, |
| 90 | LargeHeapRefTable *table, |
| 91 | RootType type, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 92 | { |
| 93 | assert(visitor != NULL); |
| 94 | for (; table != NULL; table = table->next) { |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 95 | visitReferenceTable(visitor, &table->refs, 0, type, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame^] | 100 | * Visits all stack slots except those belonging to native method |
| 101 | * arguments. |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 102 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 103 | static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 104 | { |
| 105 | const StackSaveArea *saveArea; |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 106 | u4 *fp; |
| 107 | u4 threadId; |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 108 | |
| 109 | assert(visitor != NULL); |
| 110 | assert(thread != NULL); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 111 | threadId = thread->threadId; |
| 112 | fp = (u4 *)thread->curFrame; |
| 113 | for (; fp != NULL; fp = saveArea->prevFrame) { |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 114 | Method *method; |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 115 | saveArea = SAVEAREA_FROM_FP(fp); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 116 | 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 134 | if (dvmIsValidObject((Object *)fp[i])) { |
| 135 | (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 136 | } |
| 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 Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame^] | 158 | #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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 169 | (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | dvmReleaseRegisterMapLine(pMap, regVector); |
| 173 | } |
| 174 | } |
| 175 | /* |
| 176 | * Don't fall into an infinite loop if things get corrupted. |
| 177 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 178 | assert((uintptr_t)saveArea->prevFrame > (uintptr_t)fp || |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 179 | saveArea->prevFrame == NULL); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Visits all roots associated with a thread. |
| 185 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 186 | static void visitThread(RootVisitor *visitor, Thread *thread, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 187 | { |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 188 | u4 threadId; |
| 189 | |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 190 | assert(visitor != NULL); |
| 191 | assert(thread != NULL); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 192 | 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 Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 199 | } |
| 200 | visitThreadStack(visitor, thread, arg); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Visits all threads on the thread list. |
| 205 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 206 | static void visitThreads(RootVisitor *visitor, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 207 | { |
| 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 221 | * Visits roots. TODO: visit cached global references. |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 222 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 223 | void dvmVisitRoots(RootVisitor *visitor, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 224 | { |
| 225 | assert(visitor != NULL); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 226 | visitHashTable(visitor, gDvm.loadedClasses, ROOT_STICKY_CLASS, arg); |
| 227 | visitArray(visitor, (Object **)gDvm.primitiveClass, NELEM(gDvm.primitiveClass), ROOT_STICKY_CLASS, arg); |
| Carl Shapiro | ca14ee3 | 2010-10-29 21:32:52 -0700 | [diff] [blame] | 228 | if (gDvm.dbgRegistry != NULL) { |
| 229 | visitHashTable(visitor, gDvm.dbgRegistry, ROOT_DEBUGGER, arg); |
| 230 | } |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame^] | 231 | if (gDvm.literalStrings != NULL) { |
| 232 | visitHashTable(visitor, gDvm.literalStrings, ROOT_INTERNED_STRING, arg); |
| 233 | } |
| 234 | dvmLockMutex(&gDvm.jniGlobalRefLock); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 235 | visitReferenceTable(visitor, &gDvm.jniGlobalRefTable, 0, ROOT_JNI_GLOBAL, arg); |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame^] | 236 | dvmUnlockMutex(&gDvm.jniGlobalRefLock); |
| 237 | dvmLockMutex(&gDvm.jniPinRefLock); |
| Carl Shapiro | 1ec987e | 2010-10-29 15:14:28 -0700 | [diff] [blame] | 238 | visitReferenceTable(visitor, &gDvm.jniPinRefTable, 0, ROOT_VM_INTERNAL, arg); |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame^] | 239 | dvmUnlockMutex(&gDvm.jniPinRefLock); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 240 | visitLargeHeapRefTable(visitor, gDvm.gcHeap->referenceOperations, ROOT_REFERENCE_CLEANUP, arg); |
| 241 | visitLargeHeapRefTable(visitor, gDvm.gcHeap->pendingFinalizationRefs, ROOT_FINALIZING, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 242 | visitThreads(visitor, arg); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 243 | (*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 Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 246 | } |