| 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 | /* |
| 100 | * Visits all stack slots. TODO: visit native methods. |
| 101 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 102 | static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 103 | { |
| 104 | const StackSaveArea *saveArea; |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 105 | u4 *fp; |
| 106 | u4 threadId; |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 107 | |
| 108 | assert(visitor != NULL); |
| 109 | assert(thread != NULL); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 110 | threadId = thread->threadId; |
| 111 | fp = (u4 *)thread->curFrame; |
| 112 | for (; fp != NULL; fp = saveArea->prevFrame) { |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 113 | Method *method; |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 114 | saveArea = SAVEAREA_FROM_FP(fp); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 115 | 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 133 | if (dvmIsValidObject((Object *)fp[i])) { |
| 134 | (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 135 | } |
| 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 157 | (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | dvmReleaseRegisterMapLine(pMap, regVector); |
| 161 | } |
| 162 | } |
| 163 | /* |
| 164 | * Don't fall into an infinite loop if things get corrupted. |
| 165 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 166 | assert((uintptr_t)saveArea->prevFrame > (uintptr_t)fp || |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 167 | saveArea->prevFrame == NULL); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Visits all roots associated with a thread. |
| 173 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 174 | static void visitThread(RootVisitor *visitor, Thread *thread, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 175 | { |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 176 | u4 threadId; |
| 177 | |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 178 | assert(visitor != NULL); |
| 179 | assert(thread != NULL); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 180 | 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 Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 187 | } |
| 188 | visitThreadStack(visitor, thread, arg); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Visits all threads on the thread list. |
| 193 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 194 | static void visitThreads(RootVisitor *visitor, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 195 | { |
| 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 Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 209 | * Visits roots. TODO: visit cached global references. |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 210 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 211 | void dvmVisitRoots(RootVisitor *visitor, void *arg) |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 212 | { |
| 213 | assert(visitor != NULL); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 214 | visitHashTable(visitor, gDvm.loadedClasses, ROOT_STICKY_CLASS, arg); |
| 215 | visitArray(visitor, (Object **)gDvm.primitiveClass, NELEM(gDvm.primitiveClass), ROOT_STICKY_CLASS, arg); |
| 216 | visitHashTable(visitor, gDvm.dbgRegistry, ROOT_DEBUGGER, arg); |
| 217 | visitHashTable(visitor, gDvm.internedStrings, ROOT_INTERNED_STRING, arg); |
| 218 | visitHashTable(visitor, gDvm.literalStrings, ROOT_INTERNED_STRING, arg); |
| 219 | visitReferenceTable(visitor, &gDvm.jniGlobalRefTable, 0, ROOT_JNI_GLOBAL, arg); |
| 220 | visitReferenceTable(visitor, &gDvm.jniPinRefTable, 0, ROOT_NATIVE_STACK, arg); |
| 221 | visitLargeHeapRefTable(visitor, gDvm.gcHeap->referenceOperations, ROOT_REFERENCE_CLEANUP, arg); |
| 222 | visitLargeHeapRefTable(visitor, gDvm.gcHeap->pendingFinalizationRefs, ROOT_FINALIZING, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 223 | visitThreads(visitor, arg); |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame^] | 224 | (*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg); |
| 225 | (*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 226 | (*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 227 | } |