| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 16 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Thread support. |
| 19 | */ |
| 20 | #include "Dalvik.h" |
| Bob Lee | 2fe146a | 2009-09-10 00:36:29 +0200 | [diff] [blame] | 21 | #include "native/SystemThread.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | |
| 23 | #include "utils/threads.h" // need Android thread priorities |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/time.h> |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 28 | #include <sys/types.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | #include <sys/resource.h> |
| 30 | #include <sys/mman.h> |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 31 | #include <signal.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | #include <errno.h> |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 33 | #include <fcntl.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | |
| 35 | #if defined(HAVE_PRCTL) |
| 36 | #include <sys/prctl.h> |
| 37 | #endif |
| 38 | |
| Ben Cheng | fe1be87 | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 39 | #if defined(WITH_SELF_VERIFICATION) |
| 40 | #include "interp/Jit.h" // need for self verification |
| 41 | #endif |
| 42 | |
| 43 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | /* desktop Linux needs a little help with gettid() */ |
| 45 | #if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS) |
| 46 | #define __KERNEL__ |
| 47 | # include <linux/unistd.h> |
| 48 | #ifdef _syscall0 |
| 49 | _syscall0(pid_t,gettid) |
| 50 | #else |
| 51 | pid_t gettid() { return syscall(__NR_gettid);} |
| 52 | #endif |
| 53 | #undef __KERNEL__ |
| 54 | #endif |
| 55 | |
| San Mehat | 256fc15 | 2009-04-21 14:03:06 -0700 | [diff] [blame] | 56 | // Change this to enable logging on cgroup errors |
| 57 | #define ENABLE_CGROUP_ERR_LOGGING 0 |
| 58 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | // change this to LOGV/LOGD to debug thread activity |
| 60 | #define LOG_THREAD LOGVV |
| 61 | |
| 62 | /* |
| 63 | Notes on Threading |
| 64 | |
| 65 | All threads are native pthreads. All threads, except the JDWP debugger |
| 66 | thread, are visible to code running in the VM and to the debugger. (We |
| 67 | don't want the debugger to try to manipulate the thread that listens for |
| 68 | instructions from the debugger.) Internal VM threads are in the "system" |
| 69 | ThreadGroup, all others are in the "main" ThreadGroup, per convention. |
| 70 | |
| 71 | The GC only runs when all threads have been suspended. Threads are |
| 72 | expected to suspend themselves, using a "safe point" mechanism. We check |
| 73 | for a suspend request at certain points in the main interpreter loop, |
| 74 | and on requests coming in from native code (e.g. all JNI functions). |
| 75 | Certain debugger events may inspire threads to self-suspend. |
| 76 | |
| 77 | Native methods must use JNI calls to modify object references to avoid |
| 78 | clashes with the GC. JNI doesn't provide a way for native code to access |
| 79 | arrays of objects as such -- code must always get/set individual entries -- |
| 80 | so it should be possible to fully control access through JNI. |
| 81 | |
| 82 | Internal native VM threads, such as the finalizer thread, must explicitly |
| 83 | check for suspension periodically. In most cases they will be sound |
| 84 | asleep on a condition variable, and won't notice the suspension anyway. |
| 85 | |
| 86 | Threads may be suspended by the GC, debugger, or the SIGQUIT listener |
| 87 | thread. The debugger may suspend or resume individual threads, while the |
| 88 | GC always suspends all threads. Each thread has a "suspend count" that |
| 89 | is incremented on suspend requests and decremented on resume requests. |
| 90 | When the count is zero, the thread is runnable. This allows us to fulfill |
| 91 | a debugger requirement: if the debugger suspends a thread, the thread is |
| 92 | not allowed to run again until the debugger resumes it (or disconnects, |
| 93 | in which case we must resume all debugger-suspended threads). |
| 94 | |
| 95 | Paused threads sleep on a condition variable, and are awoken en masse. |
| 96 | Certain "slow" VM operations, such as starting up a new thread, will be |
| 97 | done in a separate "VMWAIT" state, so that the rest of the VM doesn't |
| 98 | freeze up waiting for the operation to finish. Threads must check for |
| 99 | pending suspension when leaving VMWAIT. |
| 100 | |
| 101 | Because threads suspend themselves while interpreting code or when native |
| 102 | code makes JNI calls, there is no risk of suspending while holding internal |
| 103 | VM locks. All threads can enter a suspended (or native-code-only) state. |
| 104 | Also, we don't have to worry about object references existing solely |
| 105 | in hardware registers. |
| 106 | |
| 107 | We do, however, have to worry about objects that were allocated internally |
| 108 | and aren't yet visible to anything else in the VM. If we allocate an |
| 109 | object, and then go to sleep on a mutex after changing to a non-RUNNING |
| 110 | state (e.g. while trying to allocate a second object), the first object |
| 111 | could be garbage-collected out from under us while we sleep. To manage |
| 112 | this, we automatically add all allocated objects to an internal object |
| 113 | tracking list, and only remove them when we know we won't be suspended |
| 114 | before the object appears in the GC root set. |
| 115 | |
| 116 | The debugger may choose to suspend or resume a single thread, which can |
| 117 | lead to application-level deadlocks; this is expected behavior. The VM |
| 118 | will only check for suspension of single threads when the debugger is |
| 119 | active (the java.lang.Thread calls for this are deprecated and hence are |
| 120 | not supported). Resumption of a single thread is handled by decrementing |
| 121 | the thread's suspend count and sending a broadcast signal to the condition |
| 122 | variable. (This will cause all threads to wake up and immediately go back |
| 123 | to sleep, which isn't tremendously efficient, but neither is having the |
| 124 | debugger attached.) |
| 125 | |
| 126 | The debugger is not allowed to resume threads suspended by the GC. This |
| 127 | is trivially enforced by ignoring debugger requests while the GC is running |
| 128 | (the JDWP thread is suspended during GC). |
| 129 | |
| 130 | The VM maintains a Thread struct for every pthread known to the VM. There |
| 131 | is a java/lang/Thread object associated with every Thread. At present, |
| 132 | there is no safe way to go from a Thread object to a Thread struct except by |
| 133 | locking and scanning the list; this is necessary because the lifetimes of |
| 134 | the two are not closely coupled. We may want to change this behavior, |
| 135 | though at present the only performance impact is on the debugger (see |
| 136 | threadObjToThread()). See also notes about dvmDetachCurrentThread(). |
| 137 | */ |
| 138 | /* |
| 139 | Alternate implementation (signal-based): |
| 140 | |
| 141 | Threads run without safe points -- zero overhead. The VM uses a signal |
| 142 | (e.g. pthread_kill(SIGUSR1)) to notify threads of suspension or resumption. |
| 143 | |
| 144 | The trouble with using signals to suspend threads is that it means a thread |
| 145 | can be in the middle of an operation when garbage collection starts. |
| 146 | To prevent some sticky situations, we have to introduce critical sections |
| 147 | to the VM code. |
| 148 | |
| 149 | Critical sections temporarily block suspension for a given thread. |
| 150 | The thread must move to a non-blocked state (and self-suspend) after |
| 151 | finishing its current task. If the thread blocks on a resource held |
| 152 | by a suspended thread, we're hosed. |
| 153 | |
| 154 | One approach is to require that no blocking operations, notably |
| 155 | acquisition of mutexes, can be performed within a critical section. |
| 156 | This is too limiting. For example, if thread A gets suspended while |
| 157 | holding the thread list lock, it will prevent the GC or debugger from |
| 158 | being able to safely access the thread list. We need to wrap the critical |
| 159 | section around the entire operation (enter critical, get lock, do stuff, |
| 160 | release lock, exit critical). |
| 161 | |
| 162 | A better approach is to declare that certain resources can only be held |
| 163 | within critical sections. A thread that enters a critical section and |
| 164 | then gets blocked on the thread list lock knows that the thread it is |
| 165 | waiting for is also in a critical section, and will release the lock |
| 166 | before suspending itself. Eventually all threads will complete their |
| 167 | operations and self-suspend. For this to work, the VM must: |
| 168 | |
| 169 | (1) Determine the set of resources that may be accessed from the GC or |
| 170 | debugger threads. The mutexes guarding those go into the "critical |
| 171 | resource set" (CRS). |
| 172 | (2) Ensure that no resource in the CRS can be acquired outside of a |
| 173 | critical section. This can be verified with an assert(). |
| 174 | (3) Ensure that only resources in the CRS can be held while in a critical |
| 175 | section. This is harder to enforce. |
| 176 | |
| 177 | If any of these conditions are not met, deadlock can ensue when grabbing |
| 178 | resources in the GC or debugger (#1) or waiting for threads to suspend |
| 179 | (#2,#3). (You won't actually deadlock in the GC, because if the semantics |
| 180 | above are followed you don't need to lock anything in the GC. The risk is |
| 181 | rather that the GC will access data structures in an intermediate state.) |
| 182 | |
| 183 | This approach requires more care and awareness in the VM than |
| 184 | safe-pointing. Because the GC and debugger are fairly intrusive, there |
| 185 | really aren't any internal VM resources that aren't shared. Thus, the |
| 186 | enter/exit critical calls can be added to internal mutex wrappers, which |
| 187 | makes it easy to get #1 and #2 right. |
| 188 | |
| 189 | An ordering should be established for all locks to avoid deadlocks. |
| 190 | |
| 191 | Monitor locks, which are also implemented with pthread calls, should not |
| 192 | cause any problems here. Threads fighting over such locks will not be in |
| 193 | critical sections and can be suspended freely. |
| 194 | |
| 195 | This can get tricky if we ever need exclusive access to VM and non-VM |
| 196 | resources at the same time. It's not clear if this is a real concern. |
| 197 | |
| 198 | There are (at least) two ways to handle the incoming signals: |
| 199 | |
| 200 | (a) Always accept signals. If we're in a critical section, the signal |
| 201 | handler just returns without doing anything (the "suspend level" |
| 202 | should have been incremented before the signal was sent). Otherwise, |
| 203 | if the "suspend level" is nonzero, we go to sleep. |
| 204 | (b) Block signals in critical sections. This ensures that we can't be |
| 205 | interrupted in a critical section, but requires pthread_sigmask() |
| 206 | calls on entry and exit. |
| 207 | |
| 208 | This is a choice between blocking the message and blocking the messenger. |
| 209 | Because UNIX signals are unreliable (you can only know that you have been |
| 210 | signaled, not whether you were signaled once or 10 times), the choice is |
| 211 | not significant for correctness. The choice depends on the efficiency |
| 212 | of pthread_sigmask() and the desire to actually block signals. Either way, |
| 213 | it is best to ensure that there is only one indication of "blocked"; |
| 214 | having two (i.e. block signals and set a flag, then only send a signal |
| 215 | if the flag isn't set) can lead to race conditions. |
| 216 | |
| 217 | The signal handler must take care to copy registers onto the stack (via |
| 218 | setjmp), so that stack scans find all references. Because we have to scan |
| 219 | native stacks, "exact" GC is not possible with this approach. |
| 220 | |
| 221 | Some other concerns with flinging signals around: |
| 222 | - Odd interactions with some debuggers (e.g. gdb on the Mac) |
| 223 | - Restrictions on some standard library calls during GC (e.g. don't |
| 224 | use printf on stdout to print GC debug messages) |
| 225 | */ |
| 226 | |
| Carl Shapiro | 59a9312 | 2010-01-26 17:12:51 -0800 | [diff] [blame] | 227 | #define kMaxThreadId ((1 << 16) - 1) |
| 228 | #define kMainThreadId 1 |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 229 | |
| 230 | |
| 231 | static Thread* allocThread(int interpStackSize); |
| 232 | static bool prepareThread(Thread* thread); |
| 233 | static void setThreadSelf(Thread* thread); |
| 234 | static void unlinkThread(Thread* thread); |
| 235 | static void freeThread(Thread* thread); |
| 236 | static void assignThreadId(Thread* thread); |
| 237 | static bool createFakeEntryFrame(Thread* thread); |
| 238 | static bool createFakeRunFrame(Thread* thread); |
| 239 | static void* interpThreadStart(void* arg); |
| 240 | static void* internalThreadStart(void* arg); |
| 241 | static void threadExitUncaughtException(Thread* thread, Object* group); |
| 242 | static void threadExitCheck(void* arg); |
| 243 | static void waitForThreadSuspend(Thread* self, Thread* thread); |
| 244 | static int getThreadPriorityFromSystem(void); |
| 245 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 246 | /* |
| 247 | * The JIT needs to know if any thread is suspended. We do this by |
| 248 | * maintaining a global sum of all threads' suspend counts. All suspendCount |
| 249 | * updates should go through this after aquiring threadSuspendCountLock. |
| 250 | */ |
| 251 | static inline void dvmAddToThreadSuspendCount(int *pSuspendCount, int delta) |
| 252 | { |
| 253 | *pSuspendCount += delta; |
| 254 | gDvm.sumThreadSuspendCount += delta; |
| 255 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * Initialize thread list and main thread's environment. We need to set |
| 259 | * up some basic stuff so that dvmThreadSelf() will work when we start |
| 260 | * loading classes (e.g. to check for exceptions). |
| 261 | */ |
| 262 | bool dvmThreadStartup(void) |
| 263 | { |
| 264 | Thread* thread; |
| 265 | |
| 266 | /* allocate a TLS slot */ |
| 267 | if (pthread_key_create(&gDvm.pthreadKeySelf, threadExitCheck) != 0) { |
| 268 | LOGE("ERROR: pthread_key_create failed\n"); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | /* test our pthread lib */ |
| 273 | if (pthread_getspecific(gDvm.pthreadKeySelf) != NULL) |
| 274 | LOGW("WARNING: newly-created pthread TLS slot is not NULL\n"); |
| 275 | |
| 276 | /* prep thread-related locks and conditions */ |
| 277 | dvmInitMutex(&gDvm.threadListLock); |
| 278 | pthread_cond_init(&gDvm.threadStartCond, NULL); |
| 279 | //dvmInitMutex(&gDvm.vmExitLock); |
| 280 | pthread_cond_init(&gDvm.vmExitCond, NULL); |
| 281 | dvmInitMutex(&gDvm._threadSuspendLock); |
| 282 | dvmInitMutex(&gDvm.threadSuspendCountLock); |
| 283 | pthread_cond_init(&gDvm.threadSuspendCountCond, NULL); |
| 284 | #ifdef WITH_DEADLOCK_PREDICTION |
| 285 | dvmInitMutex(&gDvm.deadlockHistoryLock); |
| 286 | #endif |
| 287 | |
| 288 | /* |
| 289 | * Dedicated monitor for Thread.sleep(). |
| 290 | * TODO: change this to an Object* so we don't have to expose this |
| 291 | * call, and we interact better with JDWP monitor calls. Requires |
| 292 | * deferring the object creation to much later (e.g. final "main" |
| 293 | * thread prep) or until first use. |
| 294 | */ |
| 295 | gDvm.threadSleepMon = dvmCreateMonitor(NULL); |
| 296 | |
| 297 | gDvm.threadIdMap = dvmAllocBitVector(kMaxThreadId, false); |
| 298 | |
| 299 | thread = allocThread(gDvm.stackSize); |
| 300 | if (thread == NULL) |
| 301 | return false; |
| 302 | |
| 303 | /* switch mode for when we run initializers */ |
| 304 | thread->status = THREAD_RUNNING; |
| 305 | |
| 306 | /* |
| 307 | * We need to assign the threadId early so we can lock/notify |
| 308 | * object monitors. We'll set the "threadObj" field later. |
| 309 | */ |
| 310 | prepareThread(thread); |
| 311 | gDvm.threadList = thread; |
| 312 | |
| 313 | #ifdef COUNT_PRECISE_METHODS |
| 314 | gDvm.preciseMethods = dvmPointerSetAlloc(200); |
| 315 | #endif |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * We're a little farther up now, and can load some basic classes. |
| 322 | * |
| 323 | * We're far enough along that we can poke at java.lang.Thread and friends, |
| 324 | * but should not assume that static initializers have run (or cause them |
| 325 | * to do so). That means no object allocations yet. |
| 326 | */ |
| 327 | bool dvmThreadObjStartup(void) |
| 328 | { |
| 329 | /* |
| 330 | * Cache the locations of these classes. It's likely that we're the |
| 331 | * first to reference them, so they're being loaded now. |
| 332 | */ |
| 333 | gDvm.classJavaLangThread = |
| 334 | dvmFindSystemClassNoInit("Ljava/lang/Thread;"); |
| 335 | gDvm.classJavaLangVMThread = |
| 336 | dvmFindSystemClassNoInit("Ljava/lang/VMThread;"); |
| 337 | gDvm.classJavaLangThreadGroup = |
| 338 | dvmFindSystemClassNoInit("Ljava/lang/ThreadGroup;"); |
| 339 | if (gDvm.classJavaLangThread == NULL || |
| 340 | gDvm.classJavaLangThreadGroup == NULL || |
| 341 | gDvm.classJavaLangThreadGroup == NULL) |
| 342 | { |
| 343 | LOGE("Could not find one or more essential thread classes\n"); |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Cache field offsets. This makes things a little faster, at the |
| 349 | * expense of hard-coding non-public field names into the VM. |
| 350 | */ |
| 351 | gDvm.offJavaLangThread_vmThread = |
| 352 | dvmFindFieldOffset(gDvm.classJavaLangThread, |
| 353 | "vmThread", "Ljava/lang/VMThread;"); |
| 354 | gDvm.offJavaLangThread_group = |
| 355 | dvmFindFieldOffset(gDvm.classJavaLangThread, |
| 356 | "group", "Ljava/lang/ThreadGroup;"); |
| 357 | gDvm.offJavaLangThread_daemon = |
| 358 | dvmFindFieldOffset(gDvm.classJavaLangThread, "daemon", "Z"); |
| 359 | gDvm.offJavaLangThread_name = |
| 360 | dvmFindFieldOffset(gDvm.classJavaLangThread, |
| 361 | "name", "Ljava/lang/String;"); |
| 362 | gDvm.offJavaLangThread_priority = |
| 363 | dvmFindFieldOffset(gDvm.classJavaLangThread, "priority", "I"); |
| 364 | |
| 365 | if (gDvm.offJavaLangThread_vmThread < 0 || |
| 366 | gDvm.offJavaLangThread_group < 0 || |
| 367 | gDvm.offJavaLangThread_daemon < 0 || |
| 368 | gDvm.offJavaLangThread_name < 0 || |
| 369 | gDvm.offJavaLangThread_priority < 0) |
| 370 | { |
| 371 | LOGE("Unable to find all fields in java.lang.Thread\n"); |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | gDvm.offJavaLangVMThread_thread = |
| 376 | dvmFindFieldOffset(gDvm.classJavaLangVMThread, |
| 377 | "thread", "Ljava/lang/Thread;"); |
| 378 | gDvm.offJavaLangVMThread_vmData = |
| 379 | dvmFindFieldOffset(gDvm.classJavaLangVMThread, "vmData", "I"); |
| 380 | if (gDvm.offJavaLangVMThread_thread < 0 || |
| 381 | gDvm.offJavaLangVMThread_vmData < 0) |
| 382 | { |
| 383 | LOGE("Unable to find all fields in java.lang.VMThread\n"); |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Cache the vtable offset for "run()". |
| 389 | * |
| 390 | * We don't want to keep the Method* because then we won't find see |
| 391 | * methods defined in subclasses. |
| 392 | */ |
| 393 | Method* meth; |
| 394 | meth = dvmFindVirtualMethodByDescriptor(gDvm.classJavaLangThread, "run", "()V"); |
| 395 | if (meth == NULL) { |
| 396 | LOGE("Unable to find run() in java.lang.Thread\n"); |
| 397 | return false; |
| 398 | } |
| 399 | gDvm.voffJavaLangThread_run = meth->methodIndex; |
| 400 | |
| 401 | /* |
| 402 | * Cache vtable offsets for ThreadGroup methods. |
| 403 | */ |
| 404 | meth = dvmFindVirtualMethodByDescriptor(gDvm.classJavaLangThreadGroup, |
| 405 | "removeThread", "(Ljava/lang/Thread;)V"); |
| 406 | if (meth == NULL) { |
| 407 | LOGE("Unable to find removeThread(Thread) in java.lang.ThreadGroup\n"); |
| 408 | return false; |
| 409 | } |
| 410 | gDvm.voffJavaLangThreadGroup_removeThread = meth->methodIndex; |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * All threads should be stopped by now. Clean up some thread globals. |
| 417 | */ |
| 418 | void dvmThreadShutdown(void) |
| 419 | { |
| 420 | if (gDvm.threadList != NULL) { |
| Andy McFadden | f17638e | 2009-08-04 16:38:40 -0700 | [diff] [blame] | 421 | /* |
| 422 | * If we walk through the thread list and try to free the |
| 423 | * lingering thread structures (which should only be for daemon |
| 424 | * threads), the daemon threads may crash if they execute before |
| 425 | * the process dies. Let them leak. |
| 426 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 427 | freeThread(gDvm.threadList); |
| 428 | gDvm.threadList = NULL; |
| 429 | } |
| 430 | |
| 431 | dvmFreeBitVector(gDvm.threadIdMap); |
| 432 | |
| 433 | dvmFreeMonitorList(); |
| 434 | |
| 435 | pthread_key_delete(gDvm.pthreadKeySelf); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /* |
| 440 | * Grab the suspend count global lock. |
| 441 | */ |
| 442 | static inline void lockThreadSuspendCount(void) |
| 443 | { |
| 444 | /* |
| 445 | * Don't try to change to VMWAIT here. When we change back to RUNNING |
| 446 | * we have to check for a pending suspend, which results in grabbing |
| 447 | * this lock recursively. Doesn't work with "fast" pthread mutexes. |
| 448 | * |
| 449 | * This lock is always held for very brief periods, so as long as |
| 450 | * mutex ordering is respected we shouldn't stall. |
| 451 | */ |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 452 | dvmLockMutex(&gDvm.threadSuspendCountLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Release the suspend count global lock. |
| 457 | */ |
| 458 | static inline void unlockThreadSuspendCount(void) |
| 459 | { |
| 460 | dvmUnlockMutex(&gDvm.threadSuspendCountLock); |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Grab the thread list global lock. |
| 465 | * |
| 466 | * This is held while "suspend all" is trying to make everybody stop. If |
| 467 | * the shutdown is in progress, and somebody tries to grab the lock, they'll |
| 468 | * have to wait for the GC to finish. Therefore it's important that the |
| 469 | * thread not be in RUNNING mode. |
| 470 | * |
| 471 | * We don't have to check to see if we should be suspended once we have |
| 472 | * the lock. Nobody can suspend all threads without holding the thread list |
| 473 | * lock while they do it, so by definition there isn't a GC in progress. |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 474 | * |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 475 | * This function deliberately avoids the use of dvmChangeStatus(), |
| 476 | * which could grab threadSuspendCountLock. To avoid deadlock, threads |
| 477 | * are required to grab the thread list lock before the thread suspend |
| 478 | * count lock. (See comment in DvmGlobals.) |
| 479 | * |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 480 | * TODO: consider checking for suspend after acquiring the lock, and |
| 481 | * backing off if set. As stated above, it can't happen during normal |
| 482 | * execution, but it *can* happen during shutdown when daemon threads |
| 483 | * are being suspended. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 484 | */ |
| 485 | void dvmLockThreadList(Thread* self) |
| 486 | { |
| 487 | ThreadStatus oldStatus; |
| 488 | |
| 489 | if (self == NULL) /* try to get it from TLS */ |
| 490 | self = dvmThreadSelf(); |
| 491 | |
| 492 | if (self != NULL) { |
| 493 | oldStatus = self->status; |
| 494 | self->status = THREAD_VMWAIT; |
| 495 | } else { |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 496 | /* happens during VM shutdown */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 497 | //LOGW("NULL self in dvmLockThreadList\n"); |
| 498 | oldStatus = -1; // shut up gcc |
| 499 | } |
| 500 | |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 501 | dvmLockMutex(&gDvm.threadListLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 502 | |
| 503 | if (self != NULL) |
| 504 | self->status = oldStatus; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Release the thread list global lock. |
| 509 | */ |
| 510 | void dvmUnlockThreadList(void) |
| 511 | { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 512 | dvmUnlockMutex(&gDvm.threadListLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 515 | /* |
| 516 | * Convert SuspendCause to a string. |
| 517 | */ |
| 518 | static const char* getSuspendCauseStr(SuspendCause why) |
| 519 | { |
| 520 | switch (why) { |
| 521 | case SUSPEND_NOT: return "NOT?"; |
| 522 | case SUSPEND_FOR_GC: return "gc"; |
| 523 | case SUSPEND_FOR_DEBUG: return "debug"; |
| 524 | case SUSPEND_FOR_DEBUG_EVENT: return "debug-event"; |
| 525 | case SUSPEND_FOR_STACK_DUMP: return "stack-dump"; |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 526 | case SUSPEND_FOR_VERIFY: return "verify"; |
| Ben Cheng | a8e64a7 | 2009-10-20 13:01:36 -0700 | [diff] [blame] | 527 | #if defined(WITH_JIT) |
| 528 | case SUSPEND_FOR_TBL_RESIZE: return "table-resize"; |
| 529 | case SUSPEND_FOR_IC_PATCH: return "inline-cache-patch"; |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 530 | case SUSPEND_FOR_CC_RESET: return "reset-code-cache"; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 531 | case SUSPEND_FOR_REFRESH: return "refresh jit status"; |
| Ben Cheng | a8e64a7 | 2009-10-20 13:01:36 -0700 | [diff] [blame] | 532 | #endif |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 533 | default: return "UNKNOWN"; |
| 534 | } |
| 535 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Grab the "thread suspend" lock. This is required to prevent the |
| 539 | * GC and the debugger from simultaneously suspending all threads. |
| 540 | * |
| 541 | * If we fail to get the lock, somebody else is trying to suspend all |
| 542 | * threads -- including us. If we go to sleep on the lock we'll deadlock |
| 543 | * the VM. Loop until we get it or somebody puts us to sleep. |
| 544 | */ |
| 545 | static void lockThreadSuspend(const char* who, SuspendCause why) |
| 546 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 547 | const int kSpinSleepTime = 3*1000*1000; /* 3s */ |
| 548 | u8 startWhen = 0; // init req'd to placate gcc |
| 549 | int sleepIter = 0; |
| 550 | int cc; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 551 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 552 | do { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 553 | cc = dvmTryLockMutex(&gDvm._threadSuspendLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 554 | if (cc != 0) { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 555 | Thread* self = dvmThreadSelf(); |
| 556 | |
| 557 | if (!dvmCheckSuspendPending(self)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 558 | /* |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 559 | * Could be that a resume-all is in progress, and something |
| 560 | * grabbed the CPU when the wakeup was broadcast. The thread |
| 561 | * performing the resume hasn't had a chance to release the |
| Andy McFadden | e8059be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 562 | * thread suspend lock. (We release before the broadcast, |
| 563 | * so this should be a narrow window.) |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 564 | * |
| 565 | * Could be we hit the window as a suspend was started, |
| 566 | * and the lock has been grabbed but the suspend counts |
| 567 | * haven't been incremented yet. |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 568 | * |
| 569 | * Could be an unusual JNI thread-attach thing. |
| 570 | * |
| 571 | * Could be the debugger telling us to resume at roughly |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 572 | * the same time we're posting an event. |
| Ben Cheng | a8e64a7 | 2009-10-20 13:01:36 -0700 | [diff] [blame] | 573 | * |
| 574 | * Could be two app threads both want to patch predicted |
| 575 | * chaining cells around the same time. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 576 | */ |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 577 | LOGI("threadid=%d ODD: want thread-suspend lock (%s:%s)," |
| 578 | " it's held, no suspend pending\n", |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 579 | self->threadId, who, getSuspendCauseStr(why)); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 580 | } else { |
| 581 | /* we suspended; reset timeout */ |
| 582 | sleepIter = 0; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* give the lock-holder a chance to do some work */ |
| 586 | if (sleepIter == 0) |
| 587 | startWhen = dvmGetRelativeTimeUsec(); |
| 588 | if (!dvmIterativeSleep(sleepIter++, kSpinSleepTime, startWhen)) { |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 589 | LOGE("threadid=%d: couldn't get thread-suspend lock (%s:%s)," |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 590 | " bailing\n", |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 591 | self->threadId, who, getSuspendCauseStr(why)); |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 592 | /* threads are not suspended, thread dump could crash */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 593 | dvmDumpAllThreads(false); |
| 594 | dvmAbort(); |
| 595 | } |
| 596 | } |
| 597 | } while (cc != 0); |
| 598 | assert(cc == 0); |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Release the "thread suspend" lock. |
| 603 | */ |
| 604 | static inline void unlockThreadSuspend(void) |
| 605 | { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 606 | dvmUnlockMutex(&gDvm._threadSuspendLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | |
| 610 | /* |
| 611 | * Kill any daemon threads that still exist. All of ours should be |
| 612 | * stopped, so these should be Thread objects or JNI-attached threads |
| 613 | * started by the application. Actively-running threads are likely |
| 614 | * to crash the process if they continue to execute while the VM |
| 615 | * shuts down, so we really need to kill or suspend them. (If we want |
| 616 | * the VM to restart within this process, we need to kill them, but that |
| 617 | * leaves open the possibility of orphaned resources.) |
| 618 | * |
| 619 | * Waiting for the thread to suspend may be unwise at this point, but |
| 620 | * if one of these is wedged in a critical section then we probably |
| 621 | * would've locked up on the last GC attempt. |
| 622 | * |
| 623 | * It's possible for this function to get called after a failed |
| 624 | * initialization, so be careful with assumptions about the environment. |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 625 | * |
| 626 | * This will be called from whatever thread calls DestroyJavaVM, usually |
| 627 | * but not necessarily the main thread. It's likely, but not guaranteed, |
| 628 | * that the current thread has already been cleaned up. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 629 | */ |
| 630 | void dvmSlayDaemons(void) |
| 631 | { |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 632 | Thread* self = dvmThreadSelf(); // may be null |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 633 | Thread* target; |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 634 | int threadId = 0; |
| 635 | bool doWait = false; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 636 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 637 | dvmLockThreadList(self); |
| 638 | |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 639 | if (self != NULL) |
| 640 | threadId = self->threadId; |
| 641 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 642 | target = gDvm.threadList; |
| 643 | while (target != NULL) { |
| 644 | if (target == self) { |
| 645 | target = target->next; |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | if (!dvmGetFieldBoolean(target->threadObj, |
| 650 | gDvm.offJavaLangThread_daemon)) |
| 651 | { |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 652 | /* should never happen; suspend it with the rest */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 653 | LOGW("threadid=%d: non-daemon id=%d still running at shutdown?!\n", |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 654 | threadId, target->threadId); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 657 | char* threadName = dvmGetThreadName(target); |
| 658 | LOGD("threadid=%d: suspending daemon id=%d name='%s'\n", |
| 659 | threadId, target->threadId, threadName); |
| 660 | free(threadName); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 661 | |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 662 | /* mark as suspended */ |
| 663 | lockThreadSuspendCount(); |
| 664 | dvmAddToThreadSuspendCount(&target->suspendCount, 1); |
| 665 | unlockThreadSuspendCount(); |
| 666 | doWait = true; |
| 667 | |
| 668 | target = target->next; |
| 669 | } |
| 670 | |
| 671 | //dvmDumpAllThreads(false); |
| 672 | |
| 673 | /* |
| 674 | * Unlock the thread list, relocking it later if necessary. It's |
| 675 | * possible a thread is in VMWAIT after calling dvmLockThreadList, |
| 676 | * and that function *doesn't* check for pending suspend after |
| 677 | * acquiring the lock. We want to let them finish their business |
| 678 | * and see the pending suspend before we continue here. |
| 679 | * |
| 680 | * There's no guarantee of mutex fairness, so this might not work. |
| 681 | * (The alternative is to have dvmLockThreadList check for suspend |
| 682 | * after acquiring the lock and back off, something we should consider.) |
| 683 | */ |
| 684 | dvmUnlockThreadList(); |
| 685 | |
| 686 | if (doWait) { |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 687 | bool complained = false; |
| 688 | |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 689 | usleep(200 * 1000); |
| 690 | |
| 691 | dvmLockThreadList(self); |
| 692 | |
| 693 | /* |
| 694 | * Sleep for a bit until the threads have suspended. We're trying |
| 695 | * to exit, so don't wait for too long. |
| 696 | */ |
| 697 | int i; |
| 698 | for (i = 0; i < 10; i++) { |
| 699 | bool allSuspended = true; |
| 700 | |
| 701 | target = gDvm.threadList; |
| 702 | while (target != NULL) { |
| 703 | if (target == self) { |
| 704 | target = target->next; |
| 705 | continue; |
| 706 | } |
| 707 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 708 | if (target->status == THREAD_RUNNING) { |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 709 | if (!complained) |
| 710 | LOGD("threadid=%d not ready yet\n", target->threadId); |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 711 | allSuspended = false; |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 712 | /* keep going so we log each running daemon once */ |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | target = target->next; |
| 716 | } |
| 717 | |
| 718 | if (allSuspended) { |
| 719 | LOGD("threadid=%d: all daemons have suspended\n", threadId); |
| 720 | break; |
| 721 | } else { |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 722 | if (!complained) { |
| 723 | complained = true; |
| 724 | LOGD("threadid=%d: waiting briefly for daemon suspension\n", |
| 725 | threadId); |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 726 | } |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | usleep(200 * 1000); |
| 730 | } |
| 731 | dvmUnlockThreadList(); |
| 732 | } |
| 733 | |
| 734 | #if 0 /* bad things happen if they come out of JNI or "spuriously" wake up */ |
| 735 | /* |
| 736 | * Abandon the threads and recover their resources. |
| 737 | */ |
| 738 | target = gDvm.threadList; |
| 739 | while (target != NULL) { |
| 740 | Thread* nextTarget = target->next; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 741 | unlinkThread(target); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 742 | freeThread(target); |
| 743 | target = nextTarget; |
| 744 | } |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 745 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 746 | |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 747 | //dvmDumpAllThreads(true); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | |
| 751 | /* |
| 752 | * Finish preparing the parts of the Thread struct required to support |
| 753 | * JNI registration. |
| 754 | */ |
| 755 | bool dvmPrepMainForJni(JNIEnv* pEnv) |
| 756 | { |
| 757 | Thread* self; |
| 758 | |
| 759 | /* main thread is always first in list at this point */ |
| 760 | self = gDvm.threadList; |
| 761 | assert(self->threadId == kMainThreadId); |
| 762 | |
| 763 | /* create a "fake" JNI frame at the top of the main thread interp stack */ |
| 764 | if (!createFakeEntryFrame(self)) |
| 765 | return false; |
| 766 | |
| 767 | /* fill these in, since they weren't ready at dvmCreateJNIEnv time */ |
| 768 | dvmSetJniEnvThreadId(pEnv, self); |
| 769 | dvmSetThreadJNIEnv(self, (JNIEnv*) pEnv); |
| 770 | |
| 771 | return true; |
| 772 | } |
| 773 | |
| 774 | |
| 775 | /* |
| 776 | * Finish preparing the main thread, allocating some objects to represent |
| 777 | * it. As part of doing so, we finish initializing Thread and ThreadGroup. |
| Andy McFadden | a1a7a34 | 2009-05-04 13:29:30 -0700 | [diff] [blame] | 778 | * This will execute some interpreted code (e.g. class initializers). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 779 | */ |
| 780 | bool dvmPrepMainThread(void) |
| 781 | { |
| 782 | Thread* thread; |
| 783 | Object* groupObj; |
| 784 | Object* threadObj; |
| 785 | Object* vmThreadObj; |
| 786 | StringObject* threadNameStr; |
| 787 | Method* init; |
| 788 | JValue unused; |
| 789 | |
| 790 | LOGV("+++ finishing prep on main VM thread\n"); |
| 791 | |
| 792 | /* main thread is always first in list at this point */ |
| 793 | thread = gDvm.threadList; |
| 794 | assert(thread->threadId == kMainThreadId); |
| 795 | |
| 796 | /* |
| 797 | * Make sure the classes are initialized. We have to do this before |
| 798 | * we create an instance of them. |
| 799 | */ |
| 800 | if (!dvmInitClass(gDvm.classJavaLangClass)) { |
| 801 | LOGE("'Class' class failed to initialize\n"); |
| 802 | return false; |
| 803 | } |
| 804 | if (!dvmInitClass(gDvm.classJavaLangThreadGroup) || |
| 805 | !dvmInitClass(gDvm.classJavaLangThread) || |
| 806 | !dvmInitClass(gDvm.classJavaLangVMThread)) |
| 807 | { |
| 808 | LOGE("thread classes failed to initialize\n"); |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | groupObj = dvmGetMainThreadGroup(); |
| 813 | if (groupObj == NULL) |
| 814 | return false; |
| 815 | |
| 816 | /* |
| 817 | * Allocate and construct a Thread with the internal-creation |
| 818 | * constructor. |
| 819 | */ |
| 820 | threadObj = dvmAllocObject(gDvm.classJavaLangThread, ALLOC_DEFAULT); |
| 821 | if (threadObj == NULL) { |
| 822 | LOGE("unable to allocate main thread object\n"); |
| 823 | return false; |
| 824 | } |
| 825 | dvmReleaseTrackedAlloc(threadObj, NULL); |
| 826 | |
| Barry Hayes | 81f3ebe | 2010-06-15 16:17:37 -0700 | [diff] [blame] | 827 | threadNameStr = dvmCreateStringFromCstr("main"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 828 | if (threadNameStr == NULL) |
| 829 | return false; |
| 830 | dvmReleaseTrackedAlloc((Object*)threadNameStr, NULL); |
| 831 | |
| 832 | init = dvmFindDirectMethodByDescriptor(gDvm.classJavaLangThread, "<init>", |
| 833 | "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); |
| 834 | assert(init != NULL); |
| 835 | dvmCallMethod(thread, init, threadObj, &unused, groupObj, threadNameStr, |
| 836 | THREAD_NORM_PRIORITY, false); |
| 837 | if (dvmCheckException(thread)) { |
| 838 | LOGE("exception thrown while constructing main thread object\n"); |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | /* |
| 843 | * Allocate and construct a VMThread. |
| 844 | */ |
| 845 | vmThreadObj = dvmAllocObject(gDvm.classJavaLangVMThread, ALLOC_DEFAULT); |
| 846 | if (vmThreadObj == NULL) { |
| 847 | LOGE("unable to allocate main vmthread object\n"); |
| 848 | return false; |
| 849 | } |
| 850 | dvmReleaseTrackedAlloc(vmThreadObj, NULL); |
| 851 | |
| 852 | init = dvmFindDirectMethodByDescriptor(gDvm.classJavaLangVMThread, "<init>", |
| 853 | "(Ljava/lang/Thread;)V"); |
| 854 | dvmCallMethod(thread, init, vmThreadObj, &unused, threadObj); |
| 855 | if (dvmCheckException(thread)) { |
| 856 | LOGE("exception thrown while constructing main vmthread object\n"); |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | /* set the VMThread.vmData field to our Thread struct */ |
| 861 | assert(gDvm.offJavaLangVMThread_vmData != 0); |
| 862 | dvmSetFieldInt(vmThreadObj, gDvm.offJavaLangVMThread_vmData, (u4)thread); |
| 863 | |
| 864 | /* |
| 865 | * Stuff the VMThread back into the Thread. From this point on, other |
| Andy McFadden | a1a7a34 | 2009-05-04 13:29:30 -0700 | [diff] [blame] | 866 | * Threads will see that this Thread is running (at least, they would, |
| 867 | * if there were any). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 868 | */ |
| 869 | dvmSetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread, |
| 870 | vmThreadObj); |
| 871 | |
| 872 | thread->threadObj = threadObj; |
| 873 | |
| 874 | /* |
| Andy McFadden | a1a7a34 | 2009-05-04 13:29:30 -0700 | [diff] [blame] | 875 | * Set the context class loader. This invokes a ClassLoader method, |
| 876 | * which could conceivably call Thread.currentThread(), so we want the |
| 877 | * Thread to be fully configured before we do this. |
| 878 | */ |
| 879 | Object* systemLoader = dvmGetSystemClassLoader(); |
| 880 | if (systemLoader == NULL) { |
| 881 | LOGW("WARNING: system class loader is NULL (setting main ctxt)\n"); |
| 882 | /* keep going */ |
| 883 | } |
| 884 | int ctxtClassLoaderOffset = dvmFindFieldOffset(gDvm.classJavaLangThread, |
| 885 | "contextClassLoader", "Ljava/lang/ClassLoader;"); |
| 886 | if (ctxtClassLoaderOffset < 0) { |
| 887 | LOGE("Unable to find contextClassLoader field in Thread\n"); |
| 888 | return false; |
| 889 | } |
| 890 | dvmSetFieldObject(threadObj, ctxtClassLoaderOffset, systemLoader); |
| 891 | |
| 892 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 893 | * Finish our thread prep. |
| 894 | */ |
| 895 | |
| 896 | /* include self in non-daemon threads (mainly for AttachCurrentThread) */ |
| 897 | gDvm.nonDaemonThreadCount++; |
| 898 | |
| 899 | return true; |
| 900 | } |
| 901 | |
| 902 | |
| 903 | /* |
| 904 | * Alloc and initialize a Thread struct. |
| 905 | * |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 906 | * Does not create any objects, just stuff on the system (malloc) heap. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 907 | */ |
| 908 | static Thread* allocThread(int interpStackSize) |
| 909 | { |
| 910 | Thread* thread; |
| 911 | u1* stackBottom; |
| 912 | |
| 913 | thread = (Thread*) calloc(1, sizeof(Thread)); |
| 914 | if (thread == NULL) |
| 915 | return NULL; |
| 916 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 917 | #if defined(WITH_SELF_VERIFICATION) |
| 918 | if (dvmSelfVerificationShadowSpaceAlloc(thread) == NULL) |
| 919 | return NULL; |
| 920 | #endif |
| 921 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 922 | assert(interpStackSize >= kMinStackSize && interpStackSize <=kMaxStackSize); |
| 923 | |
| 924 | thread->status = THREAD_INITIALIZING; |
| 925 | thread->suspendCount = 0; |
| 926 | |
| 927 | #ifdef WITH_ALLOC_LIMITS |
| 928 | thread->allocLimit = -1; |
| 929 | #endif |
| 930 | |
| 931 | /* |
| 932 | * Allocate and initialize the interpreted code stack. We essentially |
| 933 | * "lose" the alloc pointer, which points at the bottom of the stack, |
| 934 | * but we can get it back later because we know how big the stack is. |
| 935 | * |
| 936 | * The stack must be aligned on a 4-byte boundary. |
| 937 | */ |
| 938 | #ifdef MALLOC_INTERP_STACK |
| 939 | stackBottom = (u1*) malloc(interpStackSize); |
| 940 | if (stackBottom == NULL) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 941 | #if defined(WITH_SELF_VERIFICATION) |
| 942 | dvmSelfVerificationShadowSpaceFree(thread); |
| 943 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 944 | free(thread); |
| 945 | return NULL; |
| 946 | } |
| 947 | memset(stackBottom, 0xc5, interpStackSize); // stop valgrind complaints |
| 948 | #else |
| 949 | stackBottom = mmap(NULL, interpStackSize, PROT_READ | PROT_WRITE, |
| 950 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 951 | if (stackBottom == MAP_FAILED) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 952 | #if defined(WITH_SELF_VERIFICATION) |
| 953 | dvmSelfVerificationShadowSpaceFree(thread); |
| 954 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 955 | free(thread); |
| 956 | return NULL; |
| 957 | } |
| 958 | #endif |
| 959 | |
| 960 | assert(((u4)stackBottom & 0x03) == 0); // looks like our malloc ensures this |
| 961 | thread->interpStackSize = interpStackSize; |
| 962 | thread->interpStackStart = stackBottom + interpStackSize; |
| 963 | thread->interpStackEnd = stackBottom + STACK_OVERFLOW_RESERVE; |
| 964 | |
| 965 | /* give the thread code a chance to set things up */ |
| 966 | dvmInitInterpStack(thread, interpStackSize); |
| 967 | |
| 968 | return thread; |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * Get a meaningful thread ID. At present this only has meaning under Linux, |
| 973 | * where getpid() and gettid() sometimes agree and sometimes don't depending |
| 974 | * on your thread model (try "export LD_ASSUME_KERNEL=2.4.19"). |
| 975 | */ |
| 976 | pid_t dvmGetSysThreadId(void) |
| 977 | { |
| 978 | #ifdef HAVE_GETTID |
| 979 | return gettid(); |
| 980 | #else |
| 981 | return getpid(); |
| 982 | #endif |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Finish initialization of a Thread struct. |
| 987 | * |
| 988 | * This must be called while executing in the new thread, but before the |
| 989 | * thread is added to the thread list. |
| 990 | * |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 991 | * NOTE: The threadListLock must be held by the caller (needed for |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 992 | * assignThreadId()). |
| 993 | */ |
| 994 | static bool prepareThread(Thread* thread) |
| 995 | { |
| 996 | assignThreadId(thread); |
| 997 | thread->handle = pthread_self(); |
| 998 | thread->systemTid = dvmGetSysThreadId(); |
| 999 | |
| 1000 | //LOGI("SYSTEM TID IS %d (pid is %d)\n", (int) thread->systemTid, |
| 1001 | // (int) getpid()); |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 1002 | /* |
| 1003 | * If we were called by dvmAttachCurrentThread, the self value is |
| 1004 | * already correctly established as "thread". |
| 1005 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1006 | setThreadSelf(thread); |
| 1007 | |
| 1008 | LOGV("threadid=%d: interp stack at %p\n", |
| 1009 | thread->threadId, thread->interpStackStart - thread->interpStackSize); |
| 1010 | |
| 1011 | /* |
| 1012 | * Initialize invokeReq. |
| 1013 | */ |
| Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 1014 | dvmInitMutex(&thread->invokeReq.lock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1015 | pthread_cond_init(&thread->invokeReq.cv, NULL); |
| 1016 | |
| 1017 | /* |
| 1018 | * Initialize our reference tracking tables. |
| 1019 | * |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1020 | * Most threads won't use jniMonitorRefTable, so we clear out the |
| 1021 | * structure but don't call the init function (which allocs storage). |
| 1022 | */ |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 1023 | #ifdef USE_INDIRECT_REF |
| 1024 | if (!dvmInitIndirectRefTable(&thread->jniLocalRefTable, |
| 1025 | kJniLocalRefMin, kJniLocalRefMax, kIndirectKindLocal)) |
| 1026 | return false; |
| 1027 | #else |
| 1028 | /* |
| 1029 | * The JNI local ref table *must* be fixed-size because we keep pointers |
| 1030 | * into the table in our stack frames. |
| 1031 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1032 | if (!dvmInitReferenceTable(&thread->jniLocalRefTable, |
| 1033 | kJniLocalRefMax, kJniLocalRefMax)) |
| 1034 | return false; |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 1035 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1036 | if (!dvmInitReferenceTable(&thread->internalLocalRefTable, |
| 1037 | kInternalRefDefault, kInternalRefMax)) |
| 1038 | return false; |
| 1039 | |
| 1040 | memset(&thread->jniMonitorRefTable, 0, sizeof(thread->jniMonitorRefTable)); |
| 1041 | |
| Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 1042 | pthread_cond_init(&thread->waitCond, NULL); |
| 1043 | dvmInitMutex(&thread->waitMutex); |
| 1044 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * Remove a thread from the internal list. |
| 1050 | * Clear out the links to make it obvious that the thread is |
| 1051 | * no longer on the list. Caller must hold gDvm.threadListLock. |
| 1052 | */ |
| 1053 | static void unlinkThread(Thread* thread) |
| 1054 | { |
| 1055 | LOG_THREAD("threadid=%d: removing from list\n", thread->threadId); |
| 1056 | if (thread == gDvm.threadList) { |
| 1057 | assert(thread->prev == NULL); |
| 1058 | gDvm.threadList = thread->next; |
| 1059 | } else { |
| 1060 | assert(thread->prev != NULL); |
| 1061 | thread->prev->next = thread->next; |
| 1062 | } |
| 1063 | if (thread->next != NULL) |
| 1064 | thread->next->prev = thread->prev; |
| 1065 | thread->prev = thread->next = NULL; |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * Free a Thread struct, and all the stuff allocated within. |
| 1070 | */ |
| 1071 | static void freeThread(Thread* thread) |
| 1072 | { |
| 1073 | if (thread == NULL) |
| 1074 | return; |
| 1075 | |
| 1076 | /* thread->threadId is zero at this point */ |
| 1077 | LOGVV("threadid=%d: freeing\n", thread->threadId); |
| 1078 | |
| 1079 | if (thread->interpStackStart != NULL) { |
| 1080 | u1* interpStackBottom; |
| 1081 | |
| 1082 | interpStackBottom = thread->interpStackStart; |
| 1083 | interpStackBottom -= thread->interpStackSize; |
| 1084 | #ifdef MALLOC_INTERP_STACK |
| 1085 | free(interpStackBottom); |
| 1086 | #else |
| 1087 | if (munmap(interpStackBottom, thread->interpStackSize) != 0) |
| 1088 | LOGW("munmap(thread stack) failed\n"); |
| 1089 | #endif |
| 1090 | } |
| 1091 | |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 1092 | #ifdef USE_INDIRECT_REF |
| 1093 | dvmClearIndirectRefTable(&thread->jniLocalRefTable); |
| 1094 | #else |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1095 | dvmClearReferenceTable(&thread->jniLocalRefTable); |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 1096 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1097 | dvmClearReferenceTable(&thread->internalLocalRefTable); |
| 1098 | if (&thread->jniMonitorRefTable.table != NULL) |
| 1099 | dvmClearReferenceTable(&thread->jniMonitorRefTable); |
| 1100 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 1101 | #if defined(WITH_SELF_VERIFICATION) |
| 1102 | dvmSelfVerificationShadowSpaceFree(thread); |
| 1103 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1104 | free(thread); |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Like pthread_self(), but on a Thread*. |
| 1109 | */ |
| 1110 | Thread* dvmThreadSelf(void) |
| 1111 | { |
| 1112 | return (Thread*) pthread_getspecific(gDvm.pthreadKeySelf); |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Explore our sense of self. Stuffs the thread pointer into TLS. |
| 1117 | */ |
| 1118 | static void setThreadSelf(Thread* thread) |
| 1119 | { |
| 1120 | int cc; |
| 1121 | |
| 1122 | cc = pthread_setspecific(gDvm.pthreadKeySelf, thread); |
| 1123 | if (cc != 0) { |
| 1124 | /* |
| 1125 | * Sometimes this fails under Bionic with EINVAL during shutdown. |
| 1126 | * This can happen if the timing is just right, e.g. a thread |
| 1127 | * fails to attach during shutdown, but the "fail" path calls |
| 1128 | * here to ensure we clean up after ourselves. |
| 1129 | */ |
| 1130 | if (thread != NULL) { |
| 1131 | LOGE("pthread_setspecific(%p) failed, err=%d\n", thread, cc); |
| 1132 | dvmAbort(); /* the world is fundamentally hosed */ |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * This is associated with the pthreadKeySelf key. It's called by the |
| 1139 | * pthread library when a thread is exiting and the "self" pointer in TLS |
| 1140 | * is non-NULL, meaning the VM hasn't had a chance to clean up. In normal |
| Andy McFadden | 909ce24 | 2009-12-10 16:38:30 -0800 | [diff] [blame] | 1141 | * operation this will not be called. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1142 | * |
| 1143 | * This is mainly of use to ensure that we don't leak resources if, for |
| 1144 | * example, a thread attaches itself to us with AttachCurrentThread and |
| 1145 | * then exits without notifying the VM. |
| Andy McFadden | 34e25bb | 2009-04-15 13:27:12 -0700 | [diff] [blame] | 1146 | * |
| 1147 | * We could do the detach here instead of aborting, but this will lead to |
| 1148 | * portability problems. Other implementations do not do this check and |
| 1149 | * will simply be unaware that the thread has exited, leading to resource |
| 1150 | * leaks (and, if this is a non-daemon thread, an infinite hang when the |
| 1151 | * VM tries to shut down). |
| Andy McFadden | 909ce24 | 2009-12-10 16:38:30 -0800 | [diff] [blame] | 1152 | * |
| 1153 | * Because some implementations may want to use the pthread destructor |
| 1154 | * to initiate the detach, and the ordering of destructors is not defined, |
| 1155 | * we want to iterate a couple of times to give those a chance to run. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1156 | */ |
| 1157 | static void threadExitCheck(void* arg) |
| 1158 | { |
| Andy McFadden | 909ce24 | 2009-12-10 16:38:30 -0800 | [diff] [blame] | 1159 | const int kMaxCount = 2; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1160 | |
| Andy McFadden | 909ce24 | 2009-12-10 16:38:30 -0800 | [diff] [blame] | 1161 | Thread* self = (Thread*) arg; |
| 1162 | assert(self != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1163 | |
| Andy McFadden | 909ce24 | 2009-12-10 16:38:30 -0800 | [diff] [blame] | 1164 | LOGV("threadid=%d: threadExitCheck(%p) count=%d\n", |
| 1165 | self->threadId, arg, self->threadExitCheckCount); |
| 1166 | |
| 1167 | if (self->status == THREAD_ZOMBIE) { |
| 1168 | LOGW("threadid=%d: Weird -- shouldn't be in threadExitCheck\n", |
| 1169 | self->threadId); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | if (self->threadExitCheckCount < kMaxCount) { |
| 1174 | /* |
| 1175 | * Spin a couple of times to let other destructors fire. |
| 1176 | */ |
| 1177 | LOGD("threadid=%d: thread exiting, not yet detached (count=%d)\n", |
| 1178 | self->threadId, self->threadExitCheckCount); |
| 1179 | self->threadExitCheckCount++; |
| 1180 | int cc = pthread_setspecific(gDvm.pthreadKeySelf, self); |
| 1181 | if (cc != 0) { |
| 1182 | LOGE("threadid=%d: unable to re-add thread to TLS\n", |
| 1183 | self->threadId); |
| 1184 | dvmAbort(); |
| 1185 | } |
| 1186 | } else { |
| 1187 | LOGE("threadid=%d: native thread exited without detaching\n", |
| 1188 | self->threadId); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1189 | dvmAbort(); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | /* |
| 1195 | * Assign the threadId. This needs to be a small integer so that our |
| 1196 | * "thin" locks fit in a small number of bits. |
| 1197 | * |
| 1198 | * We reserve zero for use as an invalid ID. |
| 1199 | * |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 1200 | * This must be called with threadListLock held. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1201 | */ |
| 1202 | static void assignThreadId(Thread* thread) |
| 1203 | { |
| Carl Shapiro | 59a9312 | 2010-01-26 17:12:51 -0800 | [diff] [blame] | 1204 | /* |
| 1205 | * Find a small unique integer. threadIdMap is a vector of |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1206 | * kMaxThreadId bits; dvmAllocBit() returns the index of a |
| 1207 | * bit, meaning that it will always be < kMaxThreadId. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1208 | */ |
| 1209 | int num = dvmAllocBit(gDvm.threadIdMap); |
| 1210 | if (num < 0) { |
| 1211 | LOGE("Ran out of thread IDs\n"); |
| 1212 | dvmAbort(); // TODO: make this a non-fatal error result |
| 1213 | } |
| 1214 | |
| Carl Shapiro | 59a9312 | 2010-01-26 17:12:51 -0800 | [diff] [blame] | 1215 | thread->threadId = num + 1; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1216 | |
| 1217 | assert(thread->threadId != 0); |
| 1218 | assert(thread->threadId != DVM_LOCK_INITIAL_THIN_VALUE); |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Give back the thread ID. |
| 1223 | */ |
| 1224 | static void releaseThreadId(Thread* thread) |
| 1225 | { |
| 1226 | assert(thread->threadId > 0); |
| Carl Shapiro | 7eed808 | 2010-01-28 16:12:44 -0800 | [diff] [blame] | 1227 | dvmClearBit(gDvm.threadIdMap, thread->threadId - 1); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1228 | thread->threadId = 0; |
| 1229 | } |
| 1230 | |
| 1231 | |
| 1232 | /* |
| 1233 | * Add a stack frame that makes it look like the native code in the main |
| 1234 | * thread was originally invoked from interpreted code. This gives us a |
| 1235 | * place to hang JNI local references. The VM spec says (v2 5.2) that the |
| 1236 | * VM begins by executing "main" in a class, so in a way this brings us |
| 1237 | * closer to the spec. |
| 1238 | */ |
| 1239 | static bool createFakeEntryFrame(Thread* thread) |
| 1240 | { |
| 1241 | assert(thread->threadId == kMainThreadId); // main thread only |
| 1242 | |
| 1243 | /* find the method on first use */ |
| 1244 | if (gDvm.methFakeNativeEntry == NULL) { |
| 1245 | ClassObject* nativeStart; |
| 1246 | Method* mainMeth; |
| 1247 | |
| 1248 | nativeStart = dvmFindSystemClassNoInit( |
| 1249 | "Ldalvik/system/NativeStart;"); |
| 1250 | if (nativeStart == NULL) { |
| 1251 | LOGE("Unable to find dalvik.system.NativeStart class\n"); |
| 1252 | return false; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Because we are creating a frame that represents application code, we |
| 1257 | * want to stuff the application class loader into the method's class |
| 1258 | * loader field, even though we're using the system class loader to |
| 1259 | * load it. This makes life easier over in JNI FindClass (though it |
| 1260 | * could bite us in other ways). |
| 1261 | * |
| 1262 | * Unfortunately this is occurring too early in the initialization, |
| 1263 | * of necessity coming before JNI is initialized, and we're not quite |
| 1264 | * ready to set up the application class loader. |
| 1265 | * |
| 1266 | * So we save a pointer to the method in gDvm.methFakeNativeEntry |
| 1267 | * and check it in FindClass. The method is private so nobody else |
| 1268 | * can call it. |
| 1269 | */ |
| 1270 | //nativeStart->classLoader = dvmGetSystemClassLoader(); |
| 1271 | |
| 1272 | mainMeth = dvmFindDirectMethodByDescriptor(nativeStart, |
| 1273 | "main", "([Ljava/lang/String;)V"); |
| 1274 | if (mainMeth == NULL) { |
| 1275 | LOGE("Unable to find 'main' in dalvik.system.NativeStart\n"); |
| 1276 | return false; |
| 1277 | } |
| 1278 | |
| 1279 | gDvm.methFakeNativeEntry = mainMeth; |
| 1280 | } |
| 1281 | |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 1282 | if (!dvmPushJNIFrame(thread, gDvm.methFakeNativeEntry)) |
| 1283 | return false; |
| 1284 | |
| 1285 | /* |
| 1286 | * Null out the "String[] args" argument. |
| 1287 | */ |
| 1288 | assert(gDvm.methFakeNativeEntry->registersSize == 1); |
| 1289 | u4* framePtr = (u4*) thread->curFrame; |
| 1290 | framePtr[0] = 0; |
| 1291 | |
| 1292 | return true; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | /* |
| 1297 | * Add a stack frame that makes it look like the native thread has been |
| 1298 | * executing interpreted code. This gives us a place to hang JNI local |
| 1299 | * references. |
| 1300 | */ |
| 1301 | static bool createFakeRunFrame(Thread* thread) |
| 1302 | { |
| 1303 | ClassObject* nativeStart; |
| 1304 | Method* runMeth; |
| 1305 | |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 1306 | /* |
| 1307 | * TODO: cache this result so we don't have to dig for it every time |
| 1308 | * somebody attaches a thread to the VM. Also consider changing this |
| 1309 | * to a static method so we don't have a null "this" pointer in the |
| 1310 | * "ins" on the stack. (Does it really need to look like a Runnable?) |
| 1311 | */ |
| 1312 | nativeStart = dvmFindSystemClassNoInit("Ldalvik/system/NativeStart;"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1313 | if (nativeStart == NULL) { |
| 1314 | LOGE("Unable to find dalvik.system.NativeStart class\n"); |
| 1315 | return false; |
| 1316 | } |
| 1317 | |
| 1318 | runMeth = dvmFindVirtualMethodByDescriptor(nativeStart, "run", "()V"); |
| 1319 | if (runMeth == NULL) { |
| 1320 | LOGE("Unable to find 'run' in dalvik.system.NativeStart\n"); |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 1324 | if (!dvmPushJNIFrame(thread, runMeth)) |
| 1325 | return false; |
| 1326 | |
| 1327 | /* |
| 1328 | * Provide a NULL 'this' argument. The method we've put at the top of |
| 1329 | * the stack looks like a virtual call to run() in a Runnable class. |
| 1330 | * (If we declared the method static, it wouldn't take any arguments |
| 1331 | * and we wouldn't have to do this.) |
| 1332 | */ |
| 1333 | assert(runMeth->registersSize == 1); |
| 1334 | u4* framePtr = (u4*) thread->curFrame; |
| 1335 | framePtr[0] = 0; |
| 1336 | |
| 1337 | return true; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | * Helper function to set the name of the current thread |
| 1342 | */ |
| 1343 | static void setThreadName(const char *threadName) |
| 1344 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1345 | int hasAt = 0; |
| 1346 | int hasDot = 0; |
| 1347 | const char *s = threadName; |
| 1348 | while (*s) { |
| 1349 | if (*s == '.') hasDot = 1; |
| 1350 | else if (*s == '@') hasAt = 1; |
| 1351 | s++; |
| 1352 | } |
| 1353 | int len = s - threadName; |
| 1354 | if (len < 15 || hasAt || !hasDot) { |
| 1355 | s = threadName; |
| 1356 | } else { |
| 1357 | s = threadName + len - 15; |
| 1358 | } |
| Andy McFadden | 22ec609 | 2010-07-01 11:23:15 -0700 | [diff] [blame] | 1359 | #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP) |
| Andy McFadden | b122c8b | 2010-07-08 15:43:19 -0700 | [diff] [blame] | 1360 | /* pthread_setname_np fails rather than truncating long strings */ |
| 1361 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 1362 | strncpy(buf, s, sizeof(buf)-1); |
| 1363 | buf[sizeof(buf)-1] = '\0'; |
| 1364 | int err = pthread_setname_np(pthread_self(), buf); |
| 1365 | if (err != 0) { |
| 1366 | LOGW("Unable to set the name of current thread to '%s': %s\n", |
| 1367 | buf, strerror(err)); |
| 1368 | } |
| André Goddard Rosa | bcd88cc | 2010-06-09 20:32:14 -0300 | [diff] [blame] | 1369 | #elif defined(HAVE_PRCTL) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1370 | prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0); |
| André Goddard Rosa | bcd88cc | 2010-06-09 20:32:14 -0300 | [diff] [blame] | 1371 | #else |
| Andy McFadden | b122c8b | 2010-07-08 15:43:19 -0700 | [diff] [blame] | 1372 | LOGD("No way to set current thread's name (%s)\n", s); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1373 | #endif |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Create a thread as a result of java.lang.Thread.start(). |
| 1378 | * |
| 1379 | * We do have to worry about some concurrency problems, e.g. programs |
| 1380 | * that try to call Thread.start() on the same object from multiple threads. |
| 1381 | * (This will fail for all but one, but we have to make sure that it succeeds |
| 1382 | * for exactly one.) |
| 1383 | * |
| 1384 | * Some of the complexity here arises from our desire to mimic the |
| 1385 | * Thread vs. VMThread class decomposition we inherited. We've been given |
| 1386 | * a Thread, and now we need to create a VMThread and then populate both |
| 1387 | * objects. We also need to create one of our internal Thread objects. |
| 1388 | * |
| 1389 | * Pass in a stack size of 0 to get the default. |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 1390 | * |
| 1391 | * The "threadObj" reference must be pinned by the caller to prevent the GC |
| 1392 | * from moving it around (e.g. added to the tracked allocation list). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1393 | */ |
| 1394 | bool dvmCreateInterpThread(Object* threadObj, int reqStackSize) |
| 1395 | { |
| 1396 | pthread_attr_t threadAttr; |
| 1397 | pthread_t threadHandle; |
| 1398 | Thread* self; |
| 1399 | Thread* newThread = NULL; |
| 1400 | Object* vmThreadObj = NULL; |
| 1401 | int stackSize; |
| 1402 | |
| 1403 | assert(threadObj != NULL); |
| 1404 | |
| 1405 | if(gDvm.zygote) { |
| Bob Lee | 9dc72a3 | 2009-09-04 18:28:16 -0700 | [diff] [blame] | 1406 | // Allow the sampling profiler thread. We shut it down before forking. |
| 1407 | StringObject* nameStr = (StringObject*) dvmGetFieldObject(threadObj, |
| 1408 | gDvm.offJavaLangThread_name); |
| 1409 | char* threadName = dvmCreateCstrFromString(nameStr); |
| 1410 | bool profilerThread = strcmp(threadName, "SamplingProfiler") == 0; |
| 1411 | free(threadName); |
| 1412 | if (!profilerThread) { |
| 1413 | dvmThrowException("Ljava/lang/IllegalStateException;", |
| 1414 | "No new threads in -Xzygote mode"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1415 | |
| Bob Lee | 9dc72a3 | 2009-09-04 18:28:16 -0700 | [diff] [blame] | 1416 | goto fail; |
| 1417 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | self = dvmThreadSelf(); |
| 1421 | if (reqStackSize == 0) |
| 1422 | stackSize = gDvm.stackSize; |
| 1423 | else if (reqStackSize < kMinStackSize) |
| 1424 | stackSize = kMinStackSize; |
| 1425 | else if (reqStackSize > kMaxStackSize) |
| 1426 | stackSize = kMaxStackSize; |
| 1427 | else |
| 1428 | stackSize = reqStackSize; |
| 1429 | |
| 1430 | pthread_attr_init(&threadAttr); |
| 1431 | pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); |
| 1432 | |
| 1433 | /* |
| 1434 | * To minimize the time spent in the critical section, we allocate the |
| 1435 | * vmThread object here. |
| 1436 | */ |
| 1437 | vmThreadObj = dvmAllocObject(gDvm.classJavaLangVMThread, ALLOC_DEFAULT); |
| 1438 | if (vmThreadObj == NULL) |
| 1439 | goto fail; |
| 1440 | |
| 1441 | newThread = allocThread(stackSize); |
| 1442 | if (newThread == NULL) |
| 1443 | goto fail; |
| 1444 | newThread->threadObj = threadObj; |
| 1445 | |
| 1446 | assert(newThread->status == THREAD_INITIALIZING); |
| 1447 | |
| 1448 | /* |
| 1449 | * We need to lock out other threads while we test and set the |
| 1450 | * "vmThread" field in java.lang.Thread, because we use that to determine |
| 1451 | * if this thread has been started before. We use the thread list lock |
| 1452 | * because it's handy and we're going to need to grab it again soon |
| 1453 | * anyway. |
| 1454 | */ |
| 1455 | dvmLockThreadList(self); |
| 1456 | |
| 1457 | if (dvmGetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread) != NULL) { |
| 1458 | dvmUnlockThreadList(); |
| 1459 | dvmThrowException("Ljava/lang/IllegalThreadStateException;", |
| 1460 | "thread has already been started"); |
| 1461 | goto fail; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * There are actually three data structures: Thread (object), VMThread |
| 1466 | * (object), and Thread (C struct). All of them point to at least one |
| 1467 | * other. |
| 1468 | * |
| 1469 | * As soon as "VMThread.vmData" is assigned, other threads can start |
| 1470 | * making calls into us (e.g. setPriority). |
| 1471 | */ |
| 1472 | dvmSetFieldInt(vmThreadObj, gDvm.offJavaLangVMThread_vmData, (u4)newThread); |
| 1473 | dvmSetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread, vmThreadObj); |
| 1474 | |
| 1475 | /* |
| 1476 | * Thread creation might take a while, so release the lock. |
| 1477 | */ |
| 1478 | dvmUnlockThreadList(); |
| 1479 | |
| Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 1480 | ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
| 1481 | int cc = pthread_create(&threadHandle, &threadAttr, interpThreadStart, |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 1482 | newThread); |
| 1483 | oldStatus = dvmChangeStatus(self, oldStatus); |
| 1484 | |
| 1485 | if (cc != 0) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1486 | /* |
| 1487 | * Failure generally indicates that we have exceeded system |
| 1488 | * resource limits. VirtualMachineError is probably too severe, |
| 1489 | * so use OutOfMemoryError. |
| 1490 | */ |
| 1491 | LOGE("Thread creation failed (err=%s)\n", strerror(errno)); |
| 1492 | |
| 1493 | dvmSetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread, NULL); |
| 1494 | |
| 1495 | dvmThrowException("Ljava/lang/OutOfMemoryError;", |
| 1496 | "thread creation failed"); |
| 1497 | goto fail; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * We need to wait for the thread to start. Otherwise, depending on |
| 1502 | * the whims of the OS scheduler, we could return and the code in our |
| 1503 | * thread could try to do operations on the new thread before it had |
| 1504 | * finished starting. |
| 1505 | * |
| 1506 | * The new thread will lock the thread list, change its state to |
| 1507 | * THREAD_STARTING, broadcast to gDvm.threadStartCond, and then sleep |
| 1508 | * on gDvm.threadStartCond (which uses the thread list lock). This |
| 1509 | * thread (the parent) will either see that the thread is already ready |
| 1510 | * after we grab the thread list lock, or will be awakened from the |
| 1511 | * condition variable on the broadcast. |
| 1512 | * |
| 1513 | * We don't want to stall the rest of the VM while the new thread |
| 1514 | * starts, which can happen if the GC wakes up at the wrong moment. |
| 1515 | * So, we change our own status to VMWAIT, and self-suspend if |
| 1516 | * necessary after we finish adding the new thread. |
| 1517 | * |
| 1518 | * |
| 1519 | * We have to deal with an odd race with the GC/debugger suspension |
| 1520 | * mechanism when creating a new thread. The information about whether |
| 1521 | * or not a thread should be suspended is contained entirely within |
| 1522 | * the Thread struct; this is usually cleaner to deal with than having |
| 1523 | * one or more globally-visible suspension flags. The trouble is that |
| 1524 | * we could create the thread while the VM is trying to suspend all |
| 1525 | * threads. The suspend-count won't be nonzero for the new thread, |
| 1526 | * so dvmChangeStatus(THREAD_RUNNING) won't cause a suspension. |
| 1527 | * |
| 1528 | * The easiest way to deal with this is to prevent the new thread from |
| 1529 | * running until the parent says it's okay. This results in the |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 1530 | * following (correct) sequence of events for a "badly timed" GC |
| 1531 | * (where '-' is us, 'o' is the child, and '+' is some other thread): |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1532 | * |
| 1533 | * - call pthread_create() |
| 1534 | * - lock thread list |
| 1535 | * - put self into THREAD_VMWAIT so GC doesn't wait for us |
| 1536 | * - sleep on condition var (mutex = thread list lock) until child starts |
| 1537 | * + GC triggered by another thread |
| 1538 | * + thread list locked; suspend counts updated; thread list unlocked |
| 1539 | * + loop waiting for all runnable threads to suspend |
| 1540 | * + success, start GC |
| 1541 | * o child thread wakes, signals condition var to wake parent |
| 1542 | * o child waits for parent ack on condition variable |
| 1543 | * - we wake up, locking thread list |
| 1544 | * - add child to thread list |
| 1545 | * - unlock thread list |
| 1546 | * - change our state back to THREAD_RUNNING; GC causes us to suspend |
| 1547 | * + GC finishes; all threads in thread list are resumed |
| 1548 | * - lock thread list |
| 1549 | * - set child to THREAD_VMWAIT, and signal it to start |
| 1550 | * - unlock thread list |
| 1551 | * o child resumes |
| 1552 | * o child changes state to THREAD_RUNNING |
| 1553 | * |
| 1554 | * The above shows the GC starting up during thread creation, but if |
| 1555 | * it starts anywhere after VMThread.create() is called it will |
| 1556 | * produce the same series of events. |
| 1557 | * |
| 1558 | * Once the child is in the thread list, it will be suspended and |
| 1559 | * resumed like any other thread. In the above scenario the resume-all |
| 1560 | * code will try to resume the new thread, which was never actually |
| 1561 | * suspended, and try to decrement the child's thread suspend count to -1. |
| 1562 | * We can catch this in the resume-all code. |
| 1563 | * |
| 1564 | * Bouncing back and forth between threads like this adds a small amount |
| 1565 | * of scheduler overhead to thread startup. |
| 1566 | * |
| 1567 | * One alternative to having the child wait for the parent would be |
| 1568 | * to have the child inherit the parents' suspension count. This |
| 1569 | * would work for a GC, since we can safely assume that the parent |
| 1570 | * thread didn't cause it, but we must only do so if the parent suspension |
| 1571 | * was caused by a suspend-all. If the parent was being asked to |
| 1572 | * suspend singly by the debugger, the child should not inherit the value. |
| 1573 | * |
| 1574 | * We could also have a global "new thread suspend count" that gets |
| 1575 | * picked up by new threads before changing state to THREAD_RUNNING. |
| 1576 | * This would be protected by the thread list lock and set by a |
| 1577 | * suspend-all. |
| 1578 | */ |
| 1579 | dvmLockThreadList(self); |
| 1580 | assert(self->status == THREAD_RUNNING); |
| 1581 | self->status = THREAD_VMWAIT; |
| 1582 | while (newThread->status != THREAD_STARTING) |
| 1583 | pthread_cond_wait(&gDvm.threadStartCond, &gDvm.threadListLock); |
| 1584 | |
| 1585 | LOG_THREAD("threadid=%d: adding to list\n", newThread->threadId); |
| 1586 | newThread->next = gDvm.threadList->next; |
| 1587 | if (newThread->next != NULL) |
| 1588 | newThread->next->prev = newThread; |
| 1589 | newThread->prev = gDvm.threadList; |
| 1590 | gDvm.threadList->next = newThread; |
| 1591 | |
| 1592 | if (!dvmGetFieldBoolean(threadObj, gDvm.offJavaLangThread_daemon)) |
| 1593 | gDvm.nonDaemonThreadCount++; // guarded by thread list lock |
| 1594 | |
| 1595 | dvmUnlockThreadList(); |
| 1596 | |
| 1597 | /* change status back to RUNNING, self-suspending if necessary */ |
| 1598 | dvmChangeStatus(self, THREAD_RUNNING); |
| 1599 | |
| 1600 | /* |
| 1601 | * Tell the new thread to start. |
| 1602 | * |
| 1603 | * We must hold the thread list lock before messing with another thread. |
| 1604 | * In the general case we would also need to verify that newThread was |
| 1605 | * still in the thread list, but in our case the thread has not started |
| 1606 | * executing user code and therefore has not had a chance to exit. |
| 1607 | * |
| 1608 | * We move it to VMWAIT, and it then shifts itself to RUNNING, which |
| 1609 | * comes with a suspend-pending check. |
| 1610 | */ |
| 1611 | dvmLockThreadList(self); |
| 1612 | |
| 1613 | assert(newThread->status == THREAD_STARTING); |
| 1614 | newThread->status = THREAD_VMWAIT; |
| 1615 | pthread_cond_broadcast(&gDvm.threadStartCond); |
| 1616 | |
| 1617 | dvmUnlockThreadList(); |
| 1618 | |
| 1619 | dvmReleaseTrackedAlloc(vmThreadObj, NULL); |
| 1620 | return true; |
| 1621 | |
| 1622 | fail: |
| 1623 | freeThread(newThread); |
| 1624 | dvmReleaseTrackedAlloc(vmThreadObj, NULL); |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * pthread entry function for threads started from interpreted code. |
| 1630 | */ |
| 1631 | static void* interpThreadStart(void* arg) |
| 1632 | { |
| 1633 | Thread* self = (Thread*) arg; |
| 1634 | |
| 1635 | char *threadName = dvmGetThreadName(self); |
| 1636 | setThreadName(threadName); |
| 1637 | free(threadName); |
| 1638 | |
| 1639 | /* |
| 1640 | * Finish initializing the Thread struct. |
| 1641 | */ |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 1642 | dvmLockThreadList(self); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1643 | prepareThread(self); |
| 1644 | |
| 1645 | LOG_THREAD("threadid=%d: created from interp\n", self->threadId); |
| 1646 | |
| 1647 | /* |
| 1648 | * Change our status and wake our parent, who will add us to the |
| 1649 | * thread list and advance our state to VMWAIT. |
| 1650 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1651 | self->status = THREAD_STARTING; |
| 1652 | pthread_cond_broadcast(&gDvm.threadStartCond); |
| 1653 | |
| 1654 | /* |
| 1655 | * Wait until the parent says we can go. Assuming there wasn't a |
| 1656 | * suspend pending, this will happen immediately. When it completes, |
| 1657 | * we're full-fledged citizens of the VM. |
| 1658 | * |
| 1659 | * We have to use THREAD_VMWAIT here rather than THREAD_RUNNING |
| 1660 | * because the pthread_cond_wait below needs to reacquire a lock that |
| 1661 | * suspend-all is also interested in. If we get unlucky, the parent could |
| 1662 | * change us to THREAD_RUNNING, then a GC could start before we get |
| 1663 | * signaled, and suspend-all will grab the thread list lock and then |
| 1664 | * wait for us to suspend. We'll be in the tail end of pthread_cond_wait |
| 1665 | * trying to get the lock. |
| 1666 | */ |
| 1667 | while (self->status != THREAD_VMWAIT) |
| 1668 | pthread_cond_wait(&gDvm.threadStartCond, &gDvm.threadListLock); |
| 1669 | |
| 1670 | dvmUnlockThreadList(); |
| 1671 | |
| 1672 | /* |
| 1673 | * Add a JNI context. |
| 1674 | */ |
| 1675 | self->jniEnv = dvmCreateJNIEnv(self); |
| 1676 | |
| 1677 | /* |
| 1678 | * Change our state so the GC will wait for us from now on. If a GC is |
| 1679 | * in progress this call will suspend us. |
| 1680 | */ |
| 1681 | dvmChangeStatus(self, THREAD_RUNNING); |
| 1682 | |
| 1683 | /* |
| 1684 | * Notify the debugger & DDM. The debugger notification may cause |
| 1685 | * us to suspend ourselves (and others). |
| 1686 | */ |
| 1687 | if (gDvm.debuggerConnected) |
| 1688 | dvmDbgPostThreadStart(self); |
| 1689 | |
| 1690 | /* |
| 1691 | * Set the system thread priority according to the Thread object's |
| 1692 | * priority level. We don't usually need to do this, because both the |
| 1693 | * Thread object and system thread priorities inherit from parents. The |
| 1694 | * tricky case is when somebody creates a Thread object, calls |
| 1695 | * setPriority(), and then starts the thread. We could manage this with |
| 1696 | * a "needs priority update" flag to avoid the redundant call. |
| 1697 | */ |
| Andy McFadden | 4879df9 | 2009-08-07 14:49:40 -0700 | [diff] [blame] | 1698 | int priority = dvmGetFieldInt(self->threadObj, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1699 | gDvm.offJavaLangThread_priority); |
| 1700 | dvmChangeThreadPriority(self, priority); |
| 1701 | |
| 1702 | /* |
| 1703 | * Execute the "run" method. |
| 1704 | * |
| 1705 | * At this point our stack is empty, so somebody who comes looking for |
| 1706 | * stack traces right now won't have much to look at. This is normal. |
| 1707 | */ |
| 1708 | Method* run = self->threadObj->clazz->vtable[gDvm.voffJavaLangThread_run]; |
| 1709 | JValue unused; |
| 1710 | |
| 1711 | LOGV("threadid=%d: calling run()\n", self->threadId); |
| 1712 | assert(strcmp(run->name, "run") == 0); |
| 1713 | dvmCallMethod(self, run, self->threadObj, &unused); |
| 1714 | LOGV("threadid=%d: exiting\n", self->threadId); |
| 1715 | |
| 1716 | /* |
| 1717 | * Remove the thread from various lists, report its death, and free |
| 1718 | * its resources. |
| 1719 | */ |
| 1720 | dvmDetachCurrentThread(); |
| 1721 | |
| 1722 | return NULL; |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * The current thread is exiting with an uncaught exception. The |
| 1727 | * Java programming language allows the application to provide a |
| 1728 | * thread-exit-uncaught-exception handler for the VM, for a specific |
| 1729 | * Thread, and for all threads in a ThreadGroup. |
| 1730 | * |
| 1731 | * Version 1.5 added the per-thread handler. We need to call |
| 1732 | * "uncaughtException" in the handler object, which is either the |
| 1733 | * ThreadGroup object or the Thread-specific handler. |
| 1734 | */ |
| 1735 | static void threadExitUncaughtException(Thread* self, Object* group) |
| 1736 | { |
| 1737 | Object* exception; |
| 1738 | Object* handlerObj; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1739 | Method* uncaughtHandler = NULL; |
| 1740 | InstField* threadHandler; |
| 1741 | |
| 1742 | LOGW("threadid=%d: thread exiting with uncaught exception (group=%p)\n", |
| 1743 | self->threadId, group); |
| 1744 | assert(group != NULL); |
| 1745 | |
| 1746 | /* |
| 1747 | * Get a pointer to the exception, then clear out the one in the |
| 1748 | * thread. We don't want to have it set when executing interpreted code. |
| 1749 | */ |
| 1750 | exception = dvmGetException(self); |
| 1751 | dvmAddTrackedAlloc(exception, self); |
| 1752 | dvmClearException(self); |
| 1753 | |
| 1754 | /* |
| 1755 | * Get the Thread's "uncaughtHandler" object. Use it if non-NULL; |
| 1756 | * else use "group" (which is an instance of UncaughtExceptionHandler). |
| 1757 | */ |
| 1758 | threadHandler = dvmFindInstanceField(gDvm.classJavaLangThread, |
| 1759 | "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;"); |
| 1760 | if (threadHandler == NULL) { |
| 1761 | LOGW("WARNING: no 'uncaughtHandler' field in java/lang/Thread\n"); |
| 1762 | goto bail; |
| 1763 | } |
| 1764 | handlerObj = dvmGetFieldObject(self->threadObj, threadHandler->byteOffset); |
| 1765 | if (handlerObj == NULL) |
| 1766 | handlerObj = group; |
| 1767 | |
| 1768 | /* |
| 1769 | * Find the "uncaughtHandler" field in this object. |
| 1770 | */ |
| 1771 | uncaughtHandler = dvmFindVirtualMethodHierByDescriptor(handlerObj->clazz, |
| 1772 | "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); |
| 1773 | |
| 1774 | if (uncaughtHandler != NULL) { |
| 1775 | //LOGI("+++ calling %s.uncaughtException\n", |
| 1776 | // handlerObj->clazz->descriptor); |
| 1777 | JValue unused; |
| 1778 | dvmCallMethod(self, uncaughtHandler, handlerObj, &unused, |
| 1779 | self->threadObj, exception); |
| 1780 | } else { |
| 1781 | /* restore it and dump a stack trace */ |
| 1782 | LOGW("WARNING: no 'uncaughtException' method in class %s\n", |
| 1783 | handlerObj->clazz->descriptor); |
| 1784 | dvmSetException(self, exception); |
| 1785 | dvmLogExceptionStackTrace(); |
| 1786 | } |
| 1787 | |
| 1788 | bail: |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 1789 | #if defined(WITH_JIT) |
| 1790 | /* Remove this thread's suspendCount from global suspendCount sum */ |
| 1791 | lockThreadSuspendCount(); |
| 1792 | dvmAddToThreadSuspendCount(&self->suspendCount, -self->suspendCount); |
| 1793 | unlockThreadSuspendCount(); |
| 1794 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1795 | dvmReleaseTrackedAlloc(exception, self); |
| 1796 | } |
| 1797 | |
| 1798 | |
| 1799 | /* |
| 1800 | * Create an internal VM thread, for things like JDWP and finalizers. |
| 1801 | * |
| 1802 | * The easiest way to do this is create a new thread and then use the |
| 1803 | * JNI AttachCurrentThread implementation. |
| 1804 | * |
| 1805 | * This does not return until after the new thread has begun executing. |
| 1806 | */ |
| 1807 | bool dvmCreateInternalThread(pthread_t* pHandle, const char* name, |
| 1808 | InternalThreadStart func, void* funcArg) |
| 1809 | { |
| 1810 | InternalStartArgs* pArgs; |
| 1811 | Object* systemGroup; |
| 1812 | pthread_attr_t threadAttr; |
| 1813 | volatile Thread* newThread = NULL; |
| 1814 | volatile int createStatus = 0; |
| 1815 | |
| 1816 | systemGroup = dvmGetSystemThreadGroup(); |
| 1817 | if (systemGroup == NULL) |
| 1818 | return false; |
| 1819 | |
| 1820 | pArgs = (InternalStartArgs*) malloc(sizeof(*pArgs)); |
| 1821 | pArgs->func = func; |
| 1822 | pArgs->funcArg = funcArg; |
| 1823 | pArgs->name = strdup(name); // storage will be owned by new thread |
| 1824 | pArgs->group = systemGroup; |
| 1825 | pArgs->isDaemon = true; |
| 1826 | pArgs->pThread = &newThread; |
| 1827 | pArgs->pCreateStatus = &createStatus; |
| 1828 | |
| 1829 | pthread_attr_init(&threadAttr); |
| 1830 | //pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); |
| 1831 | |
| 1832 | if (pthread_create(pHandle, &threadAttr, internalThreadStart, |
| 1833 | pArgs) != 0) |
| 1834 | { |
| 1835 | LOGE("internal thread creation failed\n"); |
| 1836 | free(pArgs->name); |
| 1837 | free(pArgs); |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
| 1841 | /* |
| 1842 | * Wait for the child to start. This gives us an opportunity to make |
| 1843 | * sure that the thread started correctly, and allows our caller to |
| 1844 | * assume that the thread has started running. |
| 1845 | * |
| 1846 | * Because we aren't holding a lock across the thread creation, it's |
| 1847 | * possible that the child will already have completed its |
| 1848 | * initialization. Because the child only adjusts "createStatus" while |
| 1849 | * holding the thread list lock, the initial condition on the "while" |
| 1850 | * loop will correctly avoid the wait if this occurs. |
| 1851 | * |
| 1852 | * It's also possible that we'll have to wait for the thread to finish |
| 1853 | * being created, and as part of allocating a Thread object it might |
| 1854 | * need to initiate a GC. We switch to VMWAIT while we pause. |
| 1855 | */ |
| 1856 | Thread* self = dvmThreadSelf(); |
| Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 1857 | ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1858 | dvmLockThreadList(self); |
| 1859 | while (createStatus == 0) |
| 1860 | pthread_cond_wait(&gDvm.threadStartCond, &gDvm.threadListLock); |
| 1861 | |
| 1862 | if (newThread == NULL) { |
| 1863 | LOGW("internal thread create failed (createStatus=%d)\n", createStatus); |
| 1864 | assert(createStatus < 0); |
| 1865 | /* don't free pArgs -- if pthread_create succeeded, child owns it */ |
| 1866 | dvmUnlockThreadList(); |
| 1867 | dvmChangeStatus(self, oldStatus); |
| 1868 | return false; |
| 1869 | } |
| 1870 | |
| 1871 | /* thread could be in any state now (except early init states) */ |
| 1872 | //assert(newThread->status == THREAD_RUNNING); |
| 1873 | |
| 1874 | dvmUnlockThreadList(); |
| 1875 | dvmChangeStatus(self, oldStatus); |
| 1876 | |
| 1877 | return true; |
| 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | * pthread entry function for internally-created threads. |
| 1882 | * |
| 1883 | * We are expected to free "arg" and its contents. If we're a daemon |
| 1884 | * thread, and we get cancelled abruptly when the VM shuts down, the |
| 1885 | * storage won't be freed. If this becomes a concern we can make a copy |
| 1886 | * on the stack. |
| 1887 | */ |
| 1888 | static void* internalThreadStart(void* arg) |
| 1889 | { |
| 1890 | InternalStartArgs* pArgs = (InternalStartArgs*) arg; |
| 1891 | JavaVMAttachArgs jniArgs; |
| 1892 | |
| 1893 | jniArgs.version = JNI_VERSION_1_2; |
| 1894 | jniArgs.name = pArgs->name; |
| 1895 | jniArgs.group = pArgs->group; |
| 1896 | |
| 1897 | setThreadName(pArgs->name); |
| 1898 | |
| 1899 | /* use local jniArgs as stack top */ |
| 1900 | if (dvmAttachCurrentThread(&jniArgs, pArgs->isDaemon)) { |
| 1901 | /* |
| 1902 | * Tell the parent of our success. |
| 1903 | * |
| 1904 | * threadListLock is the mutex for threadStartCond. |
| 1905 | */ |
| 1906 | dvmLockThreadList(dvmThreadSelf()); |
| 1907 | *pArgs->pCreateStatus = 1; |
| 1908 | *pArgs->pThread = dvmThreadSelf(); |
| 1909 | pthread_cond_broadcast(&gDvm.threadStartCond); |
| 1910 | dvmUnlockThreadList(); |
| 1911 | |
| 1912 | LOG_THREAD("threadid=%d: internal '%s'\n", |
| 1913 | dvmThreadSelf()->threadId, pArgs->name); |
| 1914 | |
| 1915 | /* execute */ |
| 1916 | (*pArgs->func)(pArgs->funcArg); |
| 1917 | |
| 1918 | /* detach ourselves */ |
| 1919 | dvmDetachCurrentThread(); |
| 1920 | } else { |
| 1921 | /* |
| 1922 | * Tell the parent of our failure. We don't have a Thread struct, |
| 1923 | * so we can't be suspended, so we don't need to enter a critical |
| 1924 | * section. |
| 1925 | */ |
| 1926 | dvmLockThreadList(dvmThreadSelf()); |
| 1927 | *pArgs->pCreateStatus = -1; |
| 1928 | assert(*pArgs->pThread == NULL); |
| 1929 | pthread_cond_broadcast(&gDvm.threadStartCond); |
| 1930 | dvmUnlockThreadList(); |
| 1931 | |
| 1932 | assert(*pArgs->pThread == NULL); |
| 1933 | } |
| 1934 | |
| 1935 | free(pArgs->name); |
| 1936 | free(pArgs); |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * Attach the current thread to the VM. |
| 1942 | * |
| 1943 | * Used for internally-created threads and JNI's AttachCurrentThread. |
| 1944 | */ |
| 1945 | bool dvmAttachCurrentThread(const JavaVMAttachArgs* pArgs, bool isDaemon) |
| 1946 | { |
| 1947 | Thread* self = NULL; |
| 1948 | Object* threadObj = NULL; |
| 1949 | Object* vmThreadObj = NULL; |
| 1950 | StringObject* threadNameStr = NULL; |
| 1951 | Method* init; |
| 1952 | bool ok, ret; |
| 1953 | |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 1954 | /* allocate thread struct, and establish a basic sense of self */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1955 | self = allocThread(gDvm.stackSize); |
| 1956 | if (self == NULL) |
| 1957 | goto fail; |
| 1958 | setThreadSelf(self); |
| 1959 | |
| 1960 | /* |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 1961 | * Finish our thread prep. We need to do this before adding ourselves |
| 1962 | * to the thread list or invoking any interpreted code. prepareThread() |
| 1963 | * requires that we hold the thread list lock. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1964 | */ |
| 1965 | dvmLockThreadList(self); |
| 1966 | ok = prepareThread(self); |
| 1967 | dvmUnlockThreadList(); |
| 1968 | if (!ok) |
| 1969 | goto fail; |
| 1970 | |
| 1971 | self->jniEnv = dvmCreateJNIEnv(self); |
| 1972 | if (self->jniEnv == NULL) |
| 1973 | goto fail; |
| 1974 | |
| 1975 | /* |
| 1976 | * Create a "fake" JNI frame at the top of the main thread interp stack. |
| 1977 | * It isn't really necessary for the internal threads, but it gives |
| 1978 | * the debugger something to show. It is essential for the JNI-attached |
| 1979 | * threads. |
| 1980 | */ |
| 1981 | if (!createFakeRunFrame(self)) |
| 1982 | goto fail; |
| 1983 | |
| 1984 | /* |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 1985 | * The native side of the thread is ready; add it to the list. Once |
| 1986 | * it's on the list the thread is visible to the JDWP code and the GC. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1987 | */ |
| 1988 | LOG_THREAD("threadid=%d: adding to list (attached)\n", self->threadId); |
| 1989 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1990 | dvmLockThreadList(self); |
| 1991 | |
| 1992 | self->next = gDvm.threadList->next; |
| 1993 | if (self->next != NULL) |
| 1994 | self->next->prev = self; |
| 1995 | self->prev = gDvm.threadList; |
| 1996 | gDvm.threadList->next = self; |
| 1997 | if (!isDaemon) |
| 1998 | gDvm.nonDaemonThreadCount++; |
| 1999 | |
| 2000 | dvmUnlockThreadList(); |
| 2001 | |
| 2002 | /* |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2003 | * Switch state from initializing to running. |
| 2004 | * |
| 2005 | * It's possible that a GC began right before we added ourselves |
| 2006 | * to the thread list, and is still going. That means our thread |
| 2007 | * suspend count won't reflect the fact that we should be suspended. |
| 2008 | * To deal with this, we transition to VMWAIT, pulse the heap lock, |
| 2009 | * and then advance to RUNNING. That will ensure that we stall until |
| 2010 | * the GC completes. |
| 2011 | * |
| 2012 | * Once we're in RUNNING, we're like any other thread in the VM (except |
| 2013 | * for the lack of an initialized threadObj). We're then free to |
| 2014 | * allocate and initialize objects. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2015 | */ |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2016 | assert(self->status == THREAD_INITIALIZING); |
| 2017 | dvmChangeStatus(self, THREAD_VMWAIT); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2018 | dvmLockMutex(&gDvm.gcHeapLock); |
| 2019 | dvmUnlockMutex(&gDvm.gcHeapLock); |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2020 | dvmChangeStatus(self, THREAD_RUNNING); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2021 | |
| 2022 | /* |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2023 | * Create Thread and VMThread objects. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2024 | */ |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2025 | threadObj = dvmAllocObject(gDvm.classJavaLangThread, ALLOC_DEFAULT); |
| 2026 | vmThreadObj = dvmAllocObject(gDvm.classJavaLangVMThread, ALLOC_DEFAULT); |
| 2027 | if (threadObj == NULL || vmThreadObj == NULL) |
| 2028 | goto fail_unlink; |
| 2029 | |
| 2030 | /* |
| 2031 | * This makes threadObj visible to the GC. We still have it in the |
| 2032 | * tracked allocation table, so it can't move around on us. |
| 2033 | */ |
| 2034 | self->threadObj = threadObj; |
| 2035 | dvmSetFieldInt(vmThreadObj, gDvm.offJavaLangVMThread_vmData, (u4)self); |
| 2036 | |
| 2037 | /* |
| 2038 | * Create a string for the thread name. |
| 2039 | */ |
| 2040 | if (pArgs->name != NULL) { |
| Barry Hayes | 81f3ebe | 2010-06-15 16:17:37 -0700 | [diff] [blame] | 2041 | threadNameStr = dvmCreateStringFromCstr(pArgs->name); |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2042 | if (threadNameStr == NULL) { |
| 2043 | assert(dvmCheckException(dvmThreadSelf())); |
| 2044 | goto fail_unlink; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | init = dvmFindDirectMethodByDescriptor(gDvm.classJavaLangThread, "<init>", |
| 2049 | "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); |
| 2050 | if (init == NULL) { |
| 2051 | assert(dvmCheckException(self)); |
| 2052 | goto fail_unlink; |
| 2053 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2054 | |
| 2055 | /* |
| 2056 | * Now we're ready to run some interpreted code. |
| 2057 | * |
| 2058 | * We need to construct the Thread object and set the VMThread field. |
| 2059 | * Setting VMThread tells interpreted code that we're alive. |
| 2060 | * |
| 2061 | * Call the (group, name, priority, daemon) constructor on the Thread. |
| 2062 | * This sets the thread's name and adds it to the specified group, and |
| 2063 | * provides values for priority and daemon (which are normally inherited |
| 2064 | * from the current thread). |
| 2065 | */ |
| 2066 | JValue unused; |
| 2067 | dvmCallMethod(self, init, threadObj, &unused, (Object*)pArgs->group, |
| 2068 | threadNameStr, getThreadPriorityFromSystem(), isDaemon); |
| 2069 | if (dvmCheckException(self)) { |
| 2070 | LOGE("exception thrown while constructing attached thread object\n"); |
| 2071 | goto fail_unlink; |
| 2072 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2073 | |
| 2074 | /* |
| 2075 | * Set the VMThread field, which tells interpreted code that we're alive. |
| 2076 | * |
| 2077 | * The risk of a thread start collision here is very low; somebody |
| 2078 | * would have to be deliberately polling the ThreadGroup list and |
| 2079 | * trying to start threads against anything it sees, which would |
| 2080 | * generally cause problems for all thread creation. However, for |
| 2081 | * correctness we test "vmThread" before setting it. |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2082 | * |
| 2083 | * TODO: this still has a race, it's just smaller. Not sure this is |
| 2084 | * worth putting effort into fixing. Need to hold a lock while |
| 2085 | * fiddling with the field, or maybe initialize the Thread object in a |
| 2086 | * way that ensures another thread can't call start() on it. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2087 | */ |
| 2088 | if (dvmGetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread) != NULL) { |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2089 | LOGW("WOW: thread start hijack\n"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2090 | dvmThrowException("Ljava/lang/IllegalThreadStateException;", |
| 2091 | "thread has already been started"); |
| 2092 | /* We don't want to free anything associated with the thread |
| 2093 | * because someone is obviously interested in it. Just let |
| 2094 | * it go and hope it will clean itself up when its finished. |
| 2095 | * This case should never happen anyway. |
| 2096 | * |
| 2097 | * Since we're letting it live, we need to finish setting it up. |
| 2098 | * We just have to let the caller know that the intended operation |
| 2099 | * has failed. |
| 2100 | * |
| 2101 | * [ This seems strange -- stepping on the vmThread object that's |
| 2102 | * already present seems like a bad idea. TODO: figure this out. ] |
| 2103 | */ |
| 2104 | ret = false; |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2105 | } else { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2106 | ret = true; |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2107 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2108 | dvmSetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread, vmThreadObj); |
| 2109 | |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2110 | /* we can now safely un-pin these */ |
| 2111 | dvmReleaseTrackedAlloc(threadObj, self); |
| 2112 | dvmReleaseTrackedAlloc(vmThreadObj, self); |
| 2113 | dvmReleaseTrackedAlloc((Object*)threadNameStr, self); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2114 | |
| 2115 | LOG_THREAD("threadid=%d: attached from native, name=%s\n", |
| 2116 | self->threadId, pArgs->name); |
| 2117 | |
| 2118 | /* tell the debugger & DDM */ |
| 2119 | if (gDvm.debuggerConnected) |
| 2120 | dvmDbgPostThreadStart(self); |
| 2121 | |
| 2122 | return ret; |
| 2123 | |
| 2124 | fail_unlink: |
| 2125 | dvmLockThreadList(self); |
| 2126 | unlinkThread(self); |
| 2127 | if (!isDaemon) |
| 2128 | gDvm.nonDaemonThreadCount--; |
| 2129 | dvmUnlockThreadList(); |
| 2130 | /* fall through to "fail" */ |
| 2131 | fail: |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 2132 | dvmReleaseTrackedAlloc(threadObj, self); |
| 2133 | dvmReleaseTrackedAlloc(vmThreadObj, self); |
| 2134 | dvmReleaseTrackedAlloc((Object*)threadNameStr, self); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2135 | if (self != NULL) { |
| 2136 | if (self->jniEnv != NULL) { |
| 2137 | dvmDestroyJNIEnv(self->jniEnv); |
| 2138 | self->jniEnv = NULL; |
| 2139 | } |
| 2140 | freeThread(self); |
| 2141 | } |
| 2142 | setThreadSelf(NULL); |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * Detach the thread from the various data structures, notify other threads |
| 2148 | * that are waiting to "join" it, and free up all heap-allocated storage. |
| 2149 | * |
| 2150 | * Used for all threads. |
| 2151 | * |
| 2152 | * When we get here the interpreted stack should be empty. The JNI 1.6 spec |
| 2153 | * requires us to enforce this for the DetachCurrentThread call, probably |
| 2154 | * because it also says that DetachCurrentThread causes all monitors |
| 2155 | * associated with the thread to be released. (Because the stack is empty, |
| 2156 | * we only have to worry about explicit JNI calls to MonitorEnter.) |
| 2157 | * |
| 2158 | * THOUGHT: |
| 2159 | * We might want to avoid freeing our internal Thread structure until the |
| 2160 | * associated Thread/VMThread objects get GCed. Our Thread is impossible to |
| 2161 | * get to once the thread shuts down, but there is a small possibility of |
| 2162 | * an operation starting in another thread before this thread halts, and |
| 2163 | * finishing much later (perhaps the thread got stalled by a weird OS bug). |
| 2164 | * We don't want something like Thread.isInterrupted() crawling through |
| 2165 | * freed storage. Can do with a Thread finalizer, or by creating a |
| 2166 | * dedicated ThreadObject class for java/lang/Thread and moving all of our |
| 2167 | * state into that. |
| 2168 | */ |
| 2169 | void dvmDetachCurrentThread(void) |
| 2170 | { |
| 2171 | Thread* self = dvmThreadSelf(); |
| 2172 | Object* vmThread; |
| 2173 | Object* group; |
| 2174 | |
| 2175 | /* |
| 2176 | * Make sure we're not detaching a thread that's still running. (This |
| 2177 | * could happen with an explicit JNI detach call.) |
| 2178 | * |
| 2179 | * A thread created by interpreted code will finish with a depth of |
| 2180 | * zero, while a JNI-attached thread will have the synthetic "stack |
| 2181 | * starter" native method at the top. |
| 2182 | */ |
| 2183 | int curDepth = dvmComputeExactFrameDepth(self->curFrame); |
| 2184 | if (curDepth != 0) { |
| 2185 | bool topIsNative = false; |
| 2186 | |
| 2187 | if (curDepth == 1) { |
| 2188 | /* not expecting a lingering break frame; just look at curFrame */ |
| 2189 | assert(!dvmIsBreakFrame(self->curFrame)); |
| 2190 | StackSaveArea* ssa = SAVEAREA_FROM_FP(self->curFrame); |
| 2191 | if (dvmIsNativeMethod(ssa->method)) |
| 2192 | topIsNative = true; |
| 2193 | } |
| 2194 | |
| 2195 | if (!topIsNative) { |
| 2196 | LOGE("ERROR: detaching thread with interp frames (count=%d)\n", |
| 2197 | curDepth); |
| 2198 | dvmDumpThread(self, false); |
| 2199 | dvmAbort(); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | group = dvmGetFieldObject(self->threadObj, gDvm.offJavaLangThread_group); |
| 2204 | LOG_THREAD("threadid=%d: detach (group=%p)\n", self->threadId, group); |
| 2205 | |
| 2206 | /* |
| 2207 | * Release any held monitors. Since there are no interpreted stack |
| 2208 | * frames, the only thing left are the monitors held by JNI MonitorEnter |
| 2209 | * calls. |
| 2210 | */ |
| 2211 | dvmReleaseJniMonitors(self); |
| 2212 | |
| 2213 | /* |
| 2214 | * Do some thread-exit uncaught exception processing if necessary. |
| 2215 | */ |
| 2216 | if (dvmCheckException(self)) |
| 2217 | threadExitUncaughtException(self, group); |
| 2218 | |
| 2219 | /* |
| 2220 | * Remove the thread from the thread group. |
| 2221 | */ |
| 2222 | if (group != NULL) { |
| 2223 | Method* removeThread = |
| 2224 | group->clazz->vtable[gDvm.voffJavaLangThreadGroup_removeThread]; |
| 2225 | JValue unused; |
| 2226 | dvmCallMethod(self, removeThread, group, &unused, self->threadObj); |
| 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * Clear the vmThread reference in the Thread object. Interpreted code |
| 2231 | * will now see that this Thread is not running. As this may be the |
| 2232 | * only reference to the VMThread object that the VM knows about, we |
| 2233 | * have to create an internal reference to it first. |
| 2234 | */ |
| 2235 | vmThread = dvmGetFieldObject(self->threadObj, |
| 2236 | gDvm.offJavaLangThread_vmThread); |
| 2237 | dvmAddTrackedAlloc(vmThread, self); |
| 2238 | dvmSetFieldObject(self->threadObj, gDvm.offJavaLangThread_vmThread, NULL); |
| 2239 | |
| 2240 | /* clear out our struct Thread pointer, since it's going away */ |
| 2241 | dvmSetFieldObject(vmThread, gDvm.offJavaLangVMThread_vmData, NULL); |
| 2242 | |
| 2243 | /* |
| 2244 | * Tell the debugger & DDM. This may cause the current thread or all |
| 2245 | * threads to suspend. |
| 2246 | * |
| 2247 | * The JDWP spec is somewhat vague about when this happens, other than |
| 2248 | * that it's issued by the dying thread, which may still appear in |
| 2249 | * an "all threads" listing. |
| 2250 | */ |
| 2251 | if (gDvm.debuggerConnected) |
| 2252 | dvmDbgPostThreadDeath(self); |
| 2253 | |
| 2254 | /* |
| 2255 | * Thread.join() is implemented as an Object.wait() on the VMThread |
| 2256 | * object. Signal anyone who is waiting. |
| 2257 | */ |
| 2258 | dvmLockObject(self, vmThread); |
| 2259 | dvmObjectNotifyAll(self, vmThread); |
| 2260 | dvmUnlockObject(self, vmThread); |
| 2261 | |
| 2262 | dvmReleaseTrackedAlloc(vmThread, self); |
| 2263 | vmThread = NULL; |
| 2264 | |
| 2265 | /* |
| 2266 | * We're done manipulating objects, so it's okay if the GC runs in |
| 2267 | * parallel with us from here out. It's important to do this if |
| 2268 | * profiling is enabled, since we can wait indefinitely. |
| 2269 | */ |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 2270 | android_atomic_release_store(THREAD_VMWAIT, &self->status); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2271 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2272 | /* |
| 2273 | * If we're doing method trace profiling, we don't want threads to exit, |
| 2274 | * because if they do we'll end up reusing thread IDs. This complicates |
| 2275 | * analysis and makes it impossible to have reasonable output in the |
| 2276 | * "threads" section of the "key" file. |
| 2277 | * |
| 2278 | * We need to do this after Thread.join() completes, or other threads |
| 2279 | * could get wedged. Since self->threadObj is still valid, the Thread |
| 2280 | * object will not get GCed even though we're no longer in the ThreadGroup |
| 2281 | * list (which is important since the profiling thread needs to get |
| 2282 | * the thread's name). |
| 2283 | */ |
| 2284 | MethodTraceState* traceState = &gDvm.methodTrace; |
| 2285 | |
| 2286 | dvmLockMutex(&traceState->startStopLock); |
| 2287 | if (traceState->traceEnabled) { |
| 2288 | LOGI("threadid=%d: waiting for method trace to finish\n", |
| 2289 | self->threadId); |
| 2290 | while (traceState->traceEnabled) { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 2291 | dvmWaitCond(&traceState->threadExitCond, |
| 2292 | &traceState->startStopLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | dvmUnlockMutex(&traceState->startStopLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2296 | |
| 2297 | dvmLockThreadList(self); |
| 2298 | |
| 2299 | /* |
| 2300 | * Lose the JNI context. |
| 2301 | */ |
| 2302 | dvmDestroyJNIEnv(self->jniEnv); |
| 2303 | self->jniEnv = NULL; |
| 2304 | |
| 2305 | self->status = THREAD_ZOMBIE; |
| 2306 | |
| 2307 | /* |
| 2308 | * Remove ourselves from the internal thread list. |
| 2309 | */ |
| 2310 | unlinkThread(self); |
| 2311 | |
| 2312 | /* |
| 2313 | * If we're the last one standing, signal anybody waiting in |
| 2314 | * DestroyJavaVM that it's okay to exit. |
| 2315 | */ |
| 2316 | if (!dvmGetFieldBoolean(self->threadObj, gDvm.offJavaLangThread_daemon)) { |
| 2317 | gDvm.nonDaemonThreadCount--; // guarded by thread list lock |
| 2318 | |
| 2319 | if (gDvm.nonDaemonThreadCount == 0) { |
| 2320 | int cc; |
| 2321 | |
| 2322 | LOGV("threadid=%d: last non-daemon thread\n", self->threadId); |
| 2323 | //dvmDumpAllThreads(false); |
| 2324 | // cond var guarded by threadListLock, which we already hold |
| 2325 | cc = pthread_cond_signal(&gDvm.vmExitCond); |
| 2326 | assert(cc == 0); |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | LOGV("threadid=%d: bye!\n", self->threadId); |
| 2331 | releaseThreadId(self); |
| 2332 | dvmUnlockThreadList(); |
| 2333 | |
| 2334 | setThreadSelf(NULL); |
| Bob Lee | 9dc72a3 | 2009-09-04 18:28:16 -0700 | [diff] [blame] | 2335 | |
| Bob Lee | 2fe146a | 2009-09-10 00:36:29 +0200 | [diff] [blame] | 2336 | dvmDetachSystemThread(self); |
| Bob Lee | 9dc72a3 | 2009-09-04 18:28:16 -0700 | [diff] [blame] | 2337 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2338 | freeThread(self); |
| 2339 | } |
| 2340 | |
| 2341 | |
| 2342 | /* |
| 2343 | * Suspend a single thread. Do not use to suspend yourself. |
| 2344 | * |
| 2345 | * This is used primarily for debugger/DDMS activity. Does not return |
| 2346 | * until the thread has suspended or is in a "safe" state (e.g. executing |
| 2347 | * native code outside the VM). |
| 2348 | * |
| 2349 | * The thread list lock should be held before calling here -- it's not |
| 2350 | * entirely safe to hang on to a Thread* from another thread otherwise. |
| 2351 | * (We'd need to grab it here anyway to avoid clashing with a suspend-all.) |
| 2352 | */ |
| 2353 | void dvmSuspendThread(Thread* thread) |
| 2354 | { |
| 2355 | assert(thread != NULL); |
| 2356 | assert(thread != dvmThreadSelf()); |
| 2357 | //assert(thread->handle != dvmJdwpGetDebugThread(gDvm.jdwpState)); |
| 2358 | |
| 2359 | lockThreadSuspendCount(); |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2360 | dvmAddToThreadSuspendCount(&thread->suspendCount, 1); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2361 | thread->dbgSuspendCount++; |
| 2362 | |
| 2363 | LOG_THREAD("threadid=%d: suspend++, now=%d\n", |
| 2364 | thread->threadId, thread->suspendCount); |
| 2365 | unlockThreadSuspendCount(); |
| 2366 | |
| 2367 | waitForThreadSuspend(dvmThreadSelf(), thread); |
| 2368 | } |
| 2369 | |
| 2370 | /* |
| 2371 | * Reduce the suspend count of a thread. If it hits zero, tell it to |
| 2372 | * resume. |
| 2373 | * |
| 2374 | * Used primarily for debugger/DDMS activity. The thread in question |
| 2375 | * might have been suspended singly or as part of a suspend-all operation. |
| 2376 | * |
| 2377 | * The thread list lock should be held before calling here -- it's not |
| 2378 | * entirely safe to hang on to a Thread* from another thread otherwise. |
| 2379 | * (We'd need to grab it here anyway to avoid clashing with a suspend-all.) |
| 2380 | */ |
| 2381 | void dvmResumeThread(Thread* thread) |
| 2382 | { |
| 2383 | assert(thread != NULL); |
| 2384 | assert(thread != dvmThreadSelf()); |
| 2385 | //assert(thread->handle != dvmJdwpGetDebugThread(gDvm.jdwpState)); |
| 2386 | |
| 2387 | lockThreadSuspendCount(); |
| 2388 | if (thread->suspendCount > 0) { |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2389 | dvmAddToThreadSuspendCount(&thread->suspendCount, -1); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2390 | thread->dbgSuspendCount--; |
| 2391 | } else { |
| 2392 | LOG_THREAD("threadid=%d: suspendCount already zero\n", |
| 2393 | thread->threadId); |
| 2394 | } |
| 2395 | |
| 2396 | LOG_THREAD("threadid=%d: suspend--, now=%d\n", |
| 2397 | thread->threadId, thread->suspendCount); |
| 2398 | |
| 2399 | if (thread->suspendCount == 0) { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 2400 | dvmBroadcastCond(&gDvm.threadSuspendCountCond); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | unlockThreadSuspendCount(); |
| 2404 | } |
| 2405 | |
| 2406 | /* |
| 2407 | * Suspend yourself, as a result of debugger activity. |
| 2408 | */ |
| 2409 | void dvmSuspendSelf(bool jdwpActivity) |
| 2410 | { |
| 2411 | Thread* self = dvmThreadSelf(); |
| 2412 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2413 | /* debugger thread must not suspend itself due to debugger activity! */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2414 | assert(gDvm.jdwpState != NULL); |
| 2415 | if (self->handle == dvmJdwpGetDebugThread(gDvm.jdwpState)) { |
| 2416 | assert(false); |
| 2417 | return; |
| 2418 | } |
| 2419 | |
| 2420 | /* |
| 2421 | * Collisions with other suspends aren't really interesting. We want |
| 2422 | * to ensure that we're the only one fiddling with the suspend count |
| 2423 | * though. |
| 2424 | */ |
| 2425 | lockThreadSuspendCount(); |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2426 | dvmAddToThreadSuspendCount(&self->suspendCount, 1); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2427 | self->dbgSuspendCount++; |
| 2428 | |
| 2429 | /* |
| 2430 | * Suspend ourselves. |
| 2431 | */ |
| 2432 | assert(self->suspendCount > 0); |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2433 | self->status = THREAD_SUSPENDED; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2434 | LOG_THREAD("threadid=%d: self-suspending (dbg)\n", self->threadId); |
| 2435 | |
| 2436 | /* |
| 2437 | * Tell JDWP that we've completed suspension. The JDWP thread can't |
| 2438 | * tell us to resume before we're fully asleep because we hold the |
| 2439 | * suspend count lock. |
| 2440 | * |
| 2441 | * If we got here via waitForDebugger(), don't do this part. |
| 2442 | */ |
| 2443 | if (jdwpActivity) { |
| 2444 | //LOGI("threadid=%d: clearing wait-for-event (my handle=%08x)\n", |
| 2445 | // self->threadId, (int) self->handle); |
| 2446 | dvmJdwpClearWaitForEventThread(gDvm.jdwpState); |
| 2447 | } |
| 2448 | |
| 2449 | while (self->suspendCount != 0) { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 2450 | dvmWaitCond(&gDvm.threadSuspendCountCond, |
| 2451 | &gDvm.threadSuspendCountLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2452 | if (self->suspendCount != 0) { |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2453 | /* |
| 2454 | * The condition was signaled but we're still suspended. This |
| 2455 | * can happen if the debugger lets go while a SIGQUIT thread |
| 2456 | * dump event is pending (assuming SignalCatcher was resumed for |
| 2457 | * just long enough to try to grab the thread-suspend lock). |
| 2458 | */ |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2459 | LOGD("threadid=%d: still suspended after undo (sc=%d dc=%d)\n", |
| 2460 | self->threadId, self->suspendCount, self->dbgSuspendCount); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | assert(self->suspendCount == 0 && self->dbgSuspendCount == 0); |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2464 | self->status = THREAD_RUNNING; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2465 | LOG_THREAD("threadid=%d: self-reviving (dbg), status=%d\n", |
| 2466 | self->threadId, self->status); |
| 2467 | |
| 2468 | unlockThreadSuspendCount(); |
| 2469 | } |
| 2470 | |
| 2471 | |
| 2472 | #ifdef HAVE_GLIBC |
| 2473 | # define NUM_FRAMES 20 |
| 2474 | # include <execinfo.h> |
| 2475 | /* |
| 2476 | * glibc-only stack dump function. Requires link with "--export-dynamic". |
| 2477 | * |
| 2478 | * TODO: move this into libs/cutils and make it work for all platforms. |
| 2479 | */ |
| 2480 | static void printBackTrace(void) |
| 2481 | { |
| 2482 | void* array[NUM_FRAMES]; |
| 2483 | size_t size; |
| 2484 | char** strings; |
| 2485 | size_t i; |
| 2486 | |
| 2487 | size = backtrace(array, NUM_FRAMES); |
| 2488 | strings = backtrace_symbols(array, size); |
| 2489 | |
| 2490 | LOGW("Obtained %zd stack frames.\n", size); |
| 2491 | |
| 2492 | for (i = 0; i < size; i++) |
| 2493 | LOGW("%s\n", strings[i]); |
| 2494 | |
| 2495 | free(strings); |
| 2496 | } |
| 2497 | #else |
| 2498 | static void printBackTrace(void) {} |
| 2499 | #endif |
| 2500 | |
| 2501 | /* |
| 2502 | * Dump the state of the current thread and that of another thread that |
| 2503 | * we think is wedged. |
| 2504 | */ |
| 2505 | static void dumpWedgedThread(Thread* thread) |
| 2506 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2507 | dvmDumpThread(dvmThreadSelf(), false); |
| 2508 | printBackTrace(); |
| 2509 | |
| 2510 | // dumping a running thread is risky, but could be useful |
| 2511 | dvmDumpThread(thread, true); |
| 2512 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2513 | // stop now and get a core dump |
| 2514 | //abort(); |
| 2515 | } |
| 2516 | |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2517 | /* |
| 2518 | * If the thread is running at below-normal priority, temporarily elevate |
| 2519 | * it to "normal". |
| 2520 | * |
| 2521 | * Returns zero if no changes were made. Otherwise, returns bit flags |
| 2522 | * indicating what was changed, storing the previous values in the |
| 2523 | * provided locations. |
| 2524 | */ |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 2525 | int dvmRaiseThreadPriorityIfNeeded(Thread* thread, int* pSavedThreadPrio, |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2526 | SchedPolicy* pSavedThreadPolicy) |
| 2527 | { |
| 2528 | errno = 0; |
| 2529 | *pSavedThreadPrio = getpriority(PRIO_PROCESS, thread->systemTid); |
| 2530 | if (errno != 0) { |
| 2531 | LOGW("Unable to get priority for threadid=%d sysTid=%d\n", |
| 2532 | thread->threadId, thread->systemTid); |
| 2533 | return 0; |
| 2534 | } |
| 2535 | if (get_sched_policy(thread->systemTid, pSavedThreadPolicy) != 0) { |
| 2536 | LOGW("Unable to get policy for threadid=%d sysTid=%d\n", |
| 2537 | thread->threadId, thread->systemTid); |
| 2538 | return 0; |
| 2539 | } |
| 2540 | |
| 2541 | int changeFlags = 0; |
| 2542 | |
| 2543 | /* |
| 2544 | * Change the priority if we're in the background group. |
| 2545 | */ |
| 2546 | if (*pSavedThreadPolicy == SP_BACKGROUND) { |
| 2547 | if (set_sched_policy(thread->systemTid, SP_FOREGROUND) != 0) { |
| 2548 | LOGW("Couldn't set fg policy on tid %d\n", thread->systemTid); |
| 2549 | } else { |
| 2550 | changeFlags |= kChangedPolicy; |
| 2551 | LOGD("Temporarily moving tid %d to fg (was %d)\n", |
| 2552 | thread->systemTid, *pSavedThreadPolicy); |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | * getpriority() returns the "nice" value, so larger numbers indicate |
| 2558 | * lower priority, with 0 being normal. |
| 2559 | */ |
| 2560 | if (*pSavedThreadPrio > 0) { |
| 2561 | const int kHigher = 0; |
| 2562 | if (setpriority(PRIO_PROCESS, thread->systemTid, kHigher) != 0) { |
| 2563 | LOGW("Couldn't raise priority on tid %d to %d\n", |
| 2564 | thread->systemTid, kHigher); |
| 2565 | } else { |
| 2566 | changeFlags |= kChangedPriority; |
| 2567 | LOGD("Temporarily raised priority on tid %d (%d -> %d)\n", |
| 2568 | thread->systemTid, *pSavedThreadPrio, kHigher); |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | return changeFlags; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
| 2576 | * Reset the priority values for the thread in question. |
| 2577 | */ |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 2578 | void dvmResetThreadPriority(Thread* thread, int changeFlags, |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2579 | int savedThreadPrio, SchedPolicy savedThreadPolicy) |
| 2580 | { |
| 2581 | if ((changeFlags & kChangedPolicy) != 0) { |
| 2582 | if (set_sched_policy(thread->systemTid, savedThreadPolicy) != 0) { |
| 2583 | LOGW("NOTE: couldn't reset tid %d to (%d)\n", |
| 2584 | thread->systemTid, savedThreadPolicy); |
| 2585 | } else { |
| 2586 | LOGD("Restored policy of %d to %d\n", |
| 2587 | thread->systemTid, savedThreadPolicy); |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | if ((changeFlags & kChangedPriority) != 0) { |
| 2592 | if (setpriority(PRIO_PROCESS, thread->systemTid, savedThreadPrio) != 0) |
| 2593 | { |
| 2594 | LOGW("NOTE: couldn't reset priority on thread %d to %d\n", |
| 2595 | thread->systemTid, savedThreadPrio); |
| 2596 | } else { |
| 2597 | LOGD("Restored priority on %d to %d\n", |
| 2598 | thread->systemTid, savedThreadPrio); |
| 2599 | } |
| 2600 | } |
| 2601 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2602 | |
| 2603 | /* |
| 2604 | * Wait for another thread to see the pending suspension and stop running. |
| 2605 | * It can either suspend itself or go into a non-running state such as |
| 2606 | * VMWAIT or NATIVE in which it cannot interact with the GC. |
| 2607 | * |
| 2608 | * If we're running at a higher priority, sched_yield() may not do anything, |
| 2609 | * so we need to sleep for "long enough" to guarantee that the other |
| 2610 | * thread has a chance to finish what it's doing. Sleeping for too short |
| 2611 | * a period (e.g. less than the resolution of the sleep clock) might cause |
| 2612 | * the scheduler to return immediately, so we want to start with a |
| 2613 | * "reasonable" value and expand. |
| 2614 | * |
| 2615 | * This does not return until the other thread has stopped running. |
| 2616 | * Eventually we time out and the VM aborts. |
| 2617 | * |
| 2618 | * This does not try to detect the situation where two threads are |
| 2619 | * waiting for each other to suspend. In normal use this is part of a |
| 2620 | * suspend-all, which implies that the suspend-all lock is held, or as |
| 2621 | * part of a debugger action in which the JDWP thread is always the one |
| 2622 | * doing the suspending. (We may need to re-evaluate this now that |
| 2623 | * getThreadStackTrace is implemented as suspend-snapshot-resume.) |
| 2624 | * |
| 2625 | * TODO: track basic stats about time required to suspend VM. |
| 2626 | */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2627 | #define FIRST_SLEEP (250*1000) /* 0.25s */ |
| 2628 | #define MORE_SLEEP (750*1000) /* 0.75s */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2629 | static void waitForThreadSuspend(Thread* self, Thread* thread) |
| 2630 | { |
| 2631 | const int kMaxRetries = 10; |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2632 | int spinSleepTime = FIRST_SLEEP; |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 2633 | bool complained = false; |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2634 | int priChangeFlags = 0; |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2635 | int savedThreadPrio = -500; |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2636 | SchedPolicy savedThreadPolicy = SP_FOREGROUND; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2637 | |
| 2638 | int sleepIter = 0; |
| 2639 | int retryCount = 0; |
| 2640 | u8 startWhen = 0; // init req'd to placate gcc |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2641 | u8 firstStartWhen = 0; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2642 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2643 | while (thread->status == THREAD_RUNNING) { |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2644 | if (sleepIter == 0) { // get current time on first iteration |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2645 | startWhen = dvmGetRelativeTimeUsec(); |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2646 | if (firstStartWhen == 0) // first iteration of first attempt |
| 2647 | firstStartWhen = startWhen; |
| 2648 | |
| 2649 | /* |
| 2650 | * After waiting for a bit, check to see if the target thread is |
| 2651 | * running at a reduced priority. If so, bump it up temporarily |
| 2652 | * to give it more CPU time. |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2653 | */ |
| 2654 | if (retryCount == 2) { |
| 2655 | assert(thread->systemTid != 0); |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 2656 | priChangeFlags = dvmRaiseThreadPriorityIfNeeded(thread, |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2657 | &savedThreadPrio, &savedThreadPolicy); |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2658 | } |
| 2659 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2660 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2661 | #if defined (WITH_JIT) |
| 2662 | /* |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 2663 | * If we're still waiting after the first timeout, unchain all |
| 2664 | * translations iff: |
| 2665 | * 1) There are new chains formed since the last unchain |
| 2666 | * 2) The top VM frame of the running thread is running JIT'ed code |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2667 | */ |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 2668 | if (gDvmJit.pJitEntryTable && retryCount > 0 && |
| 2669 | gDvmJit.hasNewChain && thread->inJitCodeCache) { |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2670 | LOGD("JIT unchain all for threadid=%d", thread->threadId); |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2671 | dvmJitUnchainAll(); |
| 2672 | } |
| 2673 | #endif |
| 2674 | |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2675 | /* |
| Andy McFadden | 1ede83b | 2009-12-02 17:03:41 -0800 | [diff] [blame] | 2676 | * Sleep briefly. The iterative sleep call returns false if we've |
| 2677 | * exceeded the total time limit for this round of sleeping. |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2678 | */ |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2679 | if (!dvmIterativeSleep(sleepIter++, spinSleepTime, startWhen)) { |
| Andy McFadden | 1ede83b | 2009-12-02 17:03:41 -0800 | [diff] [blame] | 2680 | if (spinSleepTime != FIRST_SLEEP) { |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2681 | LOGW("threadid=%d: spin on suspend #%d threadid=%d (pcf=%d)\n", |
| Andy McFadden | 1ede83b | 2009-12-02 17:03:41 -0800 | [diff] [blame] | 2682 | self->threadId, retryCount, |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2683 | thread->threadId, priChangeFlags); |
| 2684 | if (retryCount > 1) { |
| 2685 | /* stack trace logging is slow; skip on first iter */ |
| 2686 | dumpWedgedThread(thread); |
| 2687 | } |
| Andy McFadden | 1ede83b | 2009-12-02 17:03:41 -0800 | [diff] [blame] | 2688 | complained = true; |
| 2689 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2690 | |
| 2691 | // keep going; could be slow due to valgrind |
| 2692 | sleepIter = 0; |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2693 | spinSleepTime = MORE_SLEEP; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2694 | |
| 2695 | if (retryCount++ == kMaxRetries) { |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 2696 | LOGE("Fatal spin-on-suspend, dumping threads\n"); |
| 2697 | dvmDumpAllThreads(false); |
| 2698 | |
| 2699 | /* log this after -- long traces will scroll off log */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2700 | LOGE("threadid=%d: stuck on threadid=%d, giving up\n", |
| 2701 | self->threadId, thread->threadId); |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 2702 | |
| 2703 | /* try to get a debuggerd dump from the spinning thread */ |
| 2704 | dvmNukeThread(thread); |
| 2705 | /* abort the VM */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2706 | dvmAbort(); |
| 2707 | } |
| 2708 | } |
| 2709 | } |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 2710 | |
| 2711 | if (complained) { |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2712 | LOGW("threadid=%d: spin on suspend resolved in %lld msec\n", |
| 2713 | self->threadId, |
| 2714 | (dvmGetRelativeTimeUsec() - firstStartWhen) / 1000); |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 2715 | //dvmDumpThread(thread, false); /* suspended, so dump is safe */ |
| 2716 | } |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2717 | if (priChangeFlags != 0) { |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 2718 | dvmResetThreadPriority(thread, priChangeFlags, savedThreadPrio, |
| Andy McFadden | d2afbcf | 2010-03-02 14:23:04 -0800 | [diff] [blame] | 2719 | savedThreadPolicy); |
| Andy McFadden | 7ce9bd7 | 2009-08-07 11:41:35 -0700 | [diff] [blame] | 2720 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /* |
| 2724 | * Suspend all threads except the current one. This is used by the GC, |
| 2725 | * the debugger, and by any thread that hits a "suspend all threads" |
| 2726 | * debugger event (e.g. breakpoint or exception). |
| 2727 | * |
| 2728 | * If thread N hits a "suspend all threads" breakpoint, we don't want it |
| 2729 | * to suspend the JDWP thread. For the GC, we do, because the debugger can |
| 2730 | * create objects and even execute arbitrary code. The "why" argument |
| 2731 | * allows the caller to say why the suspension is taking place. |
| 2732 | * |
| 2733 | * This can be called when a global suspend has already happened, due to |
| 2734 | * various debugger gymnastics, so keeping an "everybody is suspended" flag |
| 2735 | * doesn't work. |
| 2736 | * |
| 2737 | * DO NOT grab any locks before calling here. We grab & release the thread |
| 2738 | * lock and suspend lock here (and we're not using recursive threads), and |
| 2739 | * we might have to self-suspend if somebody else beats us here. |
| 2740 | * |
| Andy McFadden | c650d2b | 2010-08-16 16:14:06 -0700 | [diff] [blame] | 2741 | * We know the current thread is in the thread list, because we attach the |
| 2742 | * thread before doing anything that could cause VM suspension (like object |
| 2743 | * allocation). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2744 | */ |
| 2745 | void dvmSuspendAllThreads(SuspendCause why) |
| 2746 | { |
| 2747 | Thread* self = dvmThreadSelf(); |
| 2748 | Thread* thread; |
| 2749 | |
| 2750 | assert(why != 0); |
| 2751 | |
| 2752 | /* |
| 2753 | * Start by grabbing the thread suspend lock. If we can't get it, most |
| 2754 | * likely somebody else is in the process of performing a suspend or |
| 2755 | * resume, so lockThreadSuspend() will cause us to self-suspend. |
| 2756 | * |
| 2757 | * We keep the lock until all other threads are suspended. |
| 2758 | */ |
| 2759 | lockThreadSuspend("susp-all", why); |
| 2760 | |
| 2761 | LOG_THREAD("threadid=%d: SuspendAll starting\n", self->threadId); |
| 2762 | |
| 2763 | /* |
| 2764 | * This is possible if the current thread was in VMWAIT mode when a |
| 2765 | * suspend-all happened, and then decided to do its own suspend-all. |
| 2766 | * This can happen when a couple of threads have simultaneous events |
| 2767 | * of interest to the debugger. |
| 2768 | */ |
| 2769 | //assert(self->suspendCount == 0); |
| 2770 | |
| 2771 | /* |
| 2772 | * Increment everybody's suspend count (except our own). |
| 2773 | */ |
| 2774 | dvmLockThreadList(self); |
| 2775 | |
| 2776 | lockThreadSuspendCount(); |
| 2777 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| 2778 | if (thread == self) |
| 2779 | continue; |
| 2780 | |
| 2781 | /* debugger events don't suspend JDWP thread */ |
| 2782 | if ((why == SUSPEND_FOR_DEBUG || why == SUSPEND_FOR_DEBUG_EVENT) && |
| 2783 | thread->handle == dvmJdwpGetDebugThread(gDvm.jdwpState)) |
| 2784 | continue; |
| 2785 | |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2786 | dvmAddToThreadSuspendCount(&thread->suspendCount, 1); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2787 | if (why == SUSPEND_FOR_DEBUG || why == SUSPEND_FOR_DEBUG_EVENT) |
| 2788 | thread->dbgSuspendCount++; |
| 2789 | } |
| 2790 | unlockThreadSuspendCount(); |
| 2791 | |
| 2792 | /* |
| 2793 | * Wait for everybody in THREAD_RUNNING state to stop. Other states |
| 2794 | * indicate the code is either running natively or sleeping quietly. |
| 2795 | * Any attempt to transition back to THREAD_RUNNING will cause a check |
| 2796 | * for suspension, so it should be impossible for anything to execute |
| 2797 | * interpreted code or modify objects (assuming native code plays nicely). |
| 2798 | * |
| 2799 | * It's also okay if the thread transitions to a non-RUNNING state. |
| 2800 | * |
| 2801 | * Note we released the threadSuspendCountLock before getting here, |
| 2802 | * so if another thread is fiddling with its suspend count (perhaps |
| 2803 | * self-suspending for the debugger) it won't block while we're waiting |
| 2804 | * in here. |
| 2805 | */ |
| 2806 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| 2807 | if (thread == self) |
| 2808 | continue; |
| 2809 | |
| 2810 | /* debugger events don't suspend JDWP thread */ |
| 2811 | if ((why == SUSPEND_FOR_DEBUG || why == SUSPEND_FOR_DEBUG_EVENT) && |
| 2812 | thread->handle == dvmJdwpGetDebugThread(gDvm.jdwpState)) |
| 2813 | continue; |
| 2814 | |
| 2815 | /* wait for the other thread to see the pending suspend */ |
| 2816 | waitForThreadSuspend(self, thread); |
| 2817 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2818 | LOG_THREAD("threadid=%d: threadid=%d status=%d sc=%d dc=%d\n", |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2819 | self->threadId, |
| 2820 | thread->threadId, thread->status, thread->suspendCount, |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2821 | thread->dbgSuspendCount); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2822 | } |
| 2823 | |
| 2824 | dvmUnlockThreadList(); |
| 2825 | unlockThreadSuspend(); |
| 2826 | |
| 2827 | LOG_THREAD("threadid=%d: SuspendAll complete\n", self->threadId); |
| 2828 | } |
| 2829 | |
| 2830 | /* |
| 2831 | * Resume all threads that are currently suspended. |
| 2832 | * |
| 2833 | * The "why" must match with the previous suspend. |
| 2834 | */ |
| 2835 | void dvmResumeAllThreads(SuspendCause why) |
| 2836 | { |
| 2837 | Thread* self = dvmThreadSelf(); |
| 2838 | Thread* thread; |
| 2839 | int cc; |
| 2840 | |
| 2841 | lockThreadSuspend("res-all", why); /* one suspend/resume at a time */ |
| 2842 | LOG_THREAD("threadid=%d: ResumeAll starting\n", self->threadId); |
| 2843 | |
| 2844 | /* |
| 2845 | * Decrement the suspend counts for all threads. No need for atomic |
| 2846 | * writes, since nobody should be moving until we decrement the count. |
| 2847 | * We do need to hold the thread list because of JNI attaches. |
| 2848 | */ |
| 2849 | dvmLockThreadList(self); |
| 2850 | lockThreadSuspendCount(); |
| 2851 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| 2852 | if (thread == self) |
| 2853 | continue; |
| 2854 | |
| 2855 | /* debugger events don't suspend JDWP thread */ |
| 2856 | if ((why == SUSPEND_FOR_DEBUG || why == SUSPEND_FOR_DEBUG_EVENT) && |
| 2857 | thread->handle == dvmJdwpGetDebugThread(gDvm.jdwpState)) |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 2858 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2859 | continue; |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 2860 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2861 | |
| 2862 | if (thread->suspendCount > 0) { |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2863 | dvmAddToThreadSuspendCount(&thread->suspendCount, -1); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2864 | if (why == SUSPEND_FOR_DEBUG || why == SUSPEND_FOR_DEBUG_EVENT) |
| 2865 | thread->dbgSuspendCount--; |
| 2866 | } else { |
| 2867 | LOG_THREAD("threadid=%d: suspendCount already zero\n", |
| 2868 | thread->threadId); |
| 2869 | } |
| 2870 | } |
| 2871 | unlockThreadSuspendCount(); |
| 2872 | dvmUnlockThreadList(); |
| 2873 | |
| 2874 | /* |
| Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 2875 | * In some ways it makes sense to continue to hold the thread-suspend |
| 2876 | * lock while we issue the wakeup broadcast. It allows us to complete |
| 2877 | * one operation before moving on to the next, which simplifies the |
| 2878 | * thread activity debug traces. |
| 2879 | * |
| 2880 | * This approach caused us some difficulty under Linux, because the |
| 2881 | * condition variable broadcast not only made the threads runnable, |
| 2882 | * but actually caused them to execute, and it was a while before |
| 2883 | * the thread performing the wakeup had an opportunity to release the |
| 2884 | * thread-suspend lock. |
| 2885 | * |
| 2886 | * This is a problem because, when a thread tries to acquire that |
| 2887 | * lock, it times out after 3 seconds. If at some point the thread |
| 2888 | * is told to suspend, the clock resets; but since the VM is still |
| 2889 | * theoretically mid-resume, there's no suspend pending. If, for |
| 2890 | * example, the GC was waking threads up while the SIGQUIT handler |
| 2891 | * was trying to acquire the lock, we would occasionally time out on |
| 2892 | * a busy system and SignalCatcher would abort. |
| 2893 | * |
| 2894 | * We now perform the unlock before the wakeup broadcast. The next |
| 2895 | * suspend can't actually start until the broadcast completes and |
| 2896 | * returns, because we're holding the thread-suspend-count lock, but the |
| 2897 | * suspending thread is now able to make progress and we avoid the abort. |
| 2898 | * |
| 2899 | * (Technically there is a narrow window between when we release |
| 2900 | * the thread-suspend lock and grab the thread-suspend-count lock. |
| 2901 | * This could cause us to send a broadcast to threads with nonzero |
| 2902 | * suspend counts, but this is expected and they'll all just fall |
| 2903 | * right back to sleep. It's probably safe to grab the suspend-count |
| 2904 | * lock before releasing thread-suspend, since we're still following |
| 2905 | * the correct order of acquisition, but it feels weird.) |
| 2906 | */ |
| 2907 | |
| 2908 | LOG_THREAD("threadid=%d: ResumeAll waking others\n", self->threadId); |
| 2909 | unlockThreadSuspend(); |
| 2910 | |
| 2911 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2912 | * Broadcast a notification to all suspended threads, some or all of |
| 2913 | * which may choose to wake up. No need to wait for them. |
| 2914 | */ |
| 2915 | lockThreadSuspendCount(); |
| 2916 | cc = pthread_cond_broadcast(&gDvm.threadSuspendCountCond); |
| 2917 | assert(cc == 0); |
| 2918 | unlockThreadSuspendCount(); |
| 2919 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2920 | LOG_THREAD("threadid=%d: ResumeAll complete\n", self->threadId); |
| 2921 | } |
| 2922 | |
| 2923 | /* |
| 2924 | * Undo any debugger suspensions. This is called when the debugger |
| 2925 | * disconnects. |
| 2926 | */ |
| 2927 | void dvmUndoDebuggerSuspensions(void) |
| 2928 | { |
| 2929 | Thread* self = dvmThreadSelf(); |
| 2930 | Thread* thread; |
| 2931 | int cc; |
| 2932 | |
| 2933 | lockThreadSuspend("undo", SUSPEND_FOR_DEBUG); |
| 2934 | LOG_THREAD("threadid=%d: UndoDebuggerSusp starting\n", self->threadId); |
| 2935 | |
| 2936 | /* |
| 2937 | * Decrement the suspend counts for all threads. No need for atomic |
| 2938 | * writes, since nobody should be moving until we decrement the count. |
| 2939 | * We do need to hold the thread list because of JNI attaches. |
| 2940 | */ |
| 2941 | dvmLockThreadList(self); |
| 2942 | lockThreadSuspendCount(); |
| 2943 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| 2944 | if (thread == self) |
| 2945 | continue; |
| 2946 | |
| 2947 | /* debugger events don't suspend JDWP thread */ |
| 2948 | if (thread->handle == dvmJdwpGetDebugThread(gDvm.jdwpState)) { |
| 2949 | assert(thread->dbgSuspendCount == 0); |
| 2950 | continue; |
| 2951 | } |
| 2952 | |
| 2953 | assert(thread->suspendCount >= thread->dbgSuspendCount); |
| Bill Buzbee | 46cd5b6 | 2009-06-05 15:36:06 -0700 | [diff] [blame] | 2954 | dvmAddToThreadSuspendCount(&thread->suspendCount, |
| 2955 | -thread->dbgSuspendCount); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2956 | thread->dbgSuspendCount = 0; |
| 2957 | } |
| 2958 | unlockThreadSuspendCount(); |
| 2959 | dvmUnlockThreadList(); |
| 2960 | |
| 2961 | /* |
| 2962 | * Broadcast a notification to all suspended threads, some or all of |
| 2963 | * which may choose to wake up. No need to wait for them. |
| 2964 | */ |
| 2965 | lockThreadSuspendCount(); |
| 2966 | cc = pthread_cond_broadcast(&gDvm.threadSuspendCountCond); |
| 2967 | assert(cc == 0); |
| 2968 | unlockThreadSuspendCount(); |
| 2969 | |
| 2970 | unlockThreadSuspend(); |
| 2971 | |
| 2972 | LOG_THREAD("threadid=%d: UndoDebuggerSusp complete\n", self->threadId); |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | * Determine if a thread is suspended. |
| 2977 | * |
| 2978 | * As with all operations on foreign threads, the caller should hold |
| 2979 | * the thread list lock before calling. |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 2980 | * |
| 2981 | * If the thread is suspending or waking, these fields could be changing |
| 2982 | * out from under us (or the thread could change state right after we |
| 2983 | * examine it), making this generally unreliable. This is chiefly |
| 2984 | * intended for use by the debugger. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2985 | */ |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 2986 | bool dvmIsSuspended(const Thread* thread) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2987 | { |
| 2988 | /* |
| 2989 | * The thread could be: |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2990 | * (1) Running happily. status is RUNNING, suspendCount is zero. |
| 2991 | * Return "false". |
| 2992 | * (2) Pending suspend. status is RUNNING, suspendCount is nonzero. |
| 2993 | * Return "false". |
| 2994 | * (3) Suspended. suspendCount is nonzero, and status is !RUNNING. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2995 | * Return "true". |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 2996 | * (4) Waking up. suspendCount is zero, status is SUSPENDED |
| 2997 | * Return "false" (since it could change out from under us, unless |
| 2998 | * we hold suspendCountLock). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2999 | */ |
| 3000 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3001 | return (thread->suspendCount != 0 && thread->status != THREAD_RUNNING); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /* |
| 3005 | * Wait until another thread self-suspends. This is specifically for |
| 3006 | * synchronization between the JDWP thread and a thread that has decided |
| 3007 | * to suspend itself after sending an event to the debugger. |
| 3008 | * |
| 3009 | * Threads that encounter "suspend all" events work as well -- the thread |
| 3010 | * in question suspends everybody else and then itself. |
| 3011 | * |
| 3012 | * We can't hold a thread lock here or in the caller, because we could |
| 3013 | * get here just before the to-be-waited-for-thread issues a "suspend all". |
| 3014 | * There's an opportunity for badness if the thread we're waiting for exits |
| 3015 | * and gets cleaned up, but since the thread in question is processing a |
| 3016 | * debugger event, that's not really a possibility. (To avoid deadlock, |
| 3017 | * it's important that we not be in THREAD_RUNNING while we wait.) |
| 3018 | */ |
| 3019 | void dvmWaitForSuspend(Thread* thread) |
| 3020 | { |
| 3021 | Thread* self = dvmThreadSelf(); |
| 3022 | |
| 3023 | LOG_THREAD("threadid=%d: waiting for threadid=%d to sleep\n", |
| 3024 | self->threadId, thread->threadId); |
| 3025 | |
| 3026 | assert(thread->handle != dvmJdwpGetDebugThread(gDvm.jdwpState)); |
| 3027 | assert(thread != self); |
| 3028 | assert(self->status != THREAD_RUNNING); |
| 3029 | |
| 3030 | waitForThreadSuspend(self, thread); |
| 3031 | |
| 3032 | LOG_THREAD("threadid=%d: threadid=%d is now asleep\n", |
| 3033 | self->threadId, thread->threadId); |
| 3034 | } |
| 3035 | |
| 3036 | /* |
| 3037 | * Check to see if we need to suspend ourselves. If so, go to sleep on |
| 3038 | * a condition variable. |
| 3039 | * |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3040 | * Returns "true" if we suspended ourselves. |
| 3041 | */ |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3042 | static bool fullSuspendCheck(Thread* self) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3043 | { |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 3044 | assert(self != NULL); |
| 3045 | assert(self->suspendCount >= 0); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3046 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3047 | /* |
| 3048 | * Grab gDvm.threadSuspendCountLock. This gives us exclusive write |
| 3049 | * access to self->suspendCount. |
| 3050 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3051 | lockThreadSuspendCount(); /* grab gDvm.threadSuspendCountLock */ |
| 3052 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3053 | bool needSuspend = (self->suspendCount != 0); |
| 3054 | if (needSuspend) { |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 3055 | LOG_THREAD("threadid=%d: self-suspending\n", self->threadId); |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3056 | ThreadStatus oldStatus = self->status; /* should be RUNNING */ |
| 3057 | self->status = THREAD_SUSPENDED; |
| 3058 | |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 3059 | while (self->suspendCount != 0) { |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3060 | /* |
| 3061 | * Wait for wakeup signal, releasing lock. The act of releasing |
| 3062 | * and re-acquiring the lock provides the memory barriers we |
| 3063 | * need for correct behavior on SMP. |
| 3064 | */ |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 3065 | dvmWaitCond(&gDvm.threadSuspendCountCond, |
| 3066 | &gDvm.threadSuspendCountLock); |
| 3067 | } |
| 3068 | assert(self->suspendCount == 0 && self->dbgSuspendCount == 0); |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3069 | self->status = oldStatus; |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 3070 | LOG_THREAD("threadid=%d: self-reviving, status=%d\n", |
| 3071 | self->threadId, self->status); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3072 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3073 | |
| 3074 | unlockThreadSuspendCount(); |
| 3075 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3076 | return needSuspend; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | /* |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3080 | * Check to see if a suspend is pending. If so, suspend the current |
| 3081 | * thread, and return "true" after we have been resumed. |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 3082 | */ |
| 3083 | bool dvmCheckSuspendPending(Thread* self) |
| 3084 | { |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3085 | assert(self != NULL); |
| 3086 | if (self->suspendCount == 0) { |
| 3087 | return false; |
| 3088 | } else { |
| 3089 | return fullSuspendCheck(self); |
| 3090 | } |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3094 | * Update our status. |
| 3095 | * |
| 3096 | * The "self" argument, which may be NULL, is accepted as an optimization. |
| 3097 | * |
| 3098 | * Returns the old status. |
| 3099 | */ |
| 3100 | ThreadStatus dvmChangeStatus(Thread* self, ThreadStatus newStatus) |
| 3101 | { |
| 3102 | ThreadStatus oldStatus; |
| 3103 | |
| 3104 | if (self == NULL) |
| 3105 | self = dvmThreadSelf(); |
| 3106 | |
| 3107 | LOGVV("threadid=%d: (status %d -> %d)\n", |
| 3108 | self->threadId, self->status, newStatus); |
| 3109 | |
| 3110 | oldStatus = self->status; |
| 3111 | |
| 3112 | if (newStatus == THREAD_RUNNING) { |
| 3113 | /* |
| 3114 | * Change our status to THREAD_RUNNING. The transition requires |
| 3115 | * that we check for pending suspension, because the VM considers |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 3116 | * us to be "asleep" in all other states, and another thread could |
| 3117 | * be performing a GC now. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3118 | * |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3119 | * The order of operations is very significant here. One way to |
| 3120 | * do this wrong is: |
| 3121 | * |
| 3122 | * GCing thread Our thread (in NATIVE) |
| 3123 | * ------------ ---------------------- |
| 3124 | * check suspend count (== 0) |
| 3125 | * dvmSuspendAllThreads() |
| 3126 | * grab suspend-count lock |
| 3127 | * increment all suspend counts |
| 3128 | * release suspend-count lock |
| 3129 | * check thread state (== NATIVE) |
| 3130 | * all are suspended, begin GC |
| 3131 | * set state to RUNNING |
| 3132 | * (continue executing) |
| 3133 | * |
| 3134 | * We can correct this by grabbing the suspend-count lock and |
| 3135 | * performing both of our operations (check suspend count, set |
| 3136 | * state) while holding it, now we need to grab a mutex on every |
| 3137 | * transition to RUNNING. |
| 3138 | * |
| 3139 | * What we do instead is change the order of operations so that |
| 3140 | * the transition to RUNNING happens first. If we then detect |
| 3141 | * that the suspend count is nonzero, we switch to SUSPENDED. |
| 3142 | * |
| 3143 | * Appropriate compiler and memory barriers are required to ensure |
| 3144 | * that the operations are observed in the expected order. |
| 3145 | * |
| 3146 | * This does create a small window of opportunity where a GC in |
| 3147 | * progress could observe what appears to be a running thread (if |
| 3148 | * it happens to look between when we set to RUNNING and when we |
| 3149 | * switch to SUSPENDED). At worst this only affects assertions |
| 3150 | * and thread logging. (We could work around it with some sort |
| 3151 | * of intermediate "pre-running" state that is generally treated |
| 3152 | * as equivalent to running, but that doesn't seem worthwhile.) |
| 3153 | * |
| 3154 | * We can also solve this by combining the "status" and "suspend |
| 3155 | * count" fields into a single 32-bit value. This trades the |
| 3156 | * store/load barrier on transition to RUNNING for an atomic RMW |
| 3157 | * op on all transitions and all suspend count updates (also, all |
| 3158 | * accesses to status or the thread count require bit-fiddling). |
| 3159 | * It also eliminates the brief transition through RUNNING when |
| 3160 | * the thread is supposed to be suspended. This is possibly faster |
| 3161 | * on SMP and slightly more correct, but less convenient. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3162 | */ |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3163 | assert(oldStatus != THREAD_RUNNING); |
| 3164 | android_atomic_acquire_store(newStatus, &self->status); |
| 3165 | if (self->suspendCount != 0) { |
| 3166 | fullSuspendCheck(self); |
| 3167 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3168 | } else { |
| 3169 | /* |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 3170 | * Not changing to THREAD_RUNNING. No additional work required. |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 3171 | * |
| 3172 | * We use a releasing store to ensure that, if we were RUNNING, |
| 3173 | * any updates we previously made to objects on the managed heap |
| 3174 | * will be observed before the state change. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3175 | */ |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3176 | assert(newStatus != THREAD_SUSPENDED); |
| Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 3177 | android_atomic_release_store(newStatus, &self->status); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | return oldStatus; |
| 3181 | } |
| 3182 | |
| 3183 | /* |
| 3184 | * Get a statically defined thread group from a field in the ThreadGroup |
| 3185 | * Class object. Expected arguments are "mMain" and "mSystem". |
| 3186 | */ |
| 3187 | static Object* getStaticThreadGroup(const char* fieldName) |
| 3188 | { |
| 3189 | StaticField* groupField; |
| 3190 | Object* groupObj; |
| 3191 | |
| 3192 | groupField = dvmFindStaticField(gDvm.classJavaLangThreadGroup, |
| 3193 | fieldName, "Ljava/lang/ThreadGroup;"); |
| 3194 | if (groupField == NULL) { |
| 3195 | LOGE("java.lang.ThreadGroup does not have an '%s' field\n", fieldName); |
| 3196 | dvmThrowException("Ljava/lang/IncompatibleClassChangeError;", NULL); |
| 3197 | return NULL; |
| 3198 | } |
| 3199 | groupObj = dvmGetStaticFieldObject(groupField); |
| 3200 | if (groupObj == NULL) { |
| 3201 | LOGE("java.lang.ThreadGroup.%s not initialized\n", fieldName); |
| 3202 | dvmThrowException("Ljava/lang/InternalError;", NULL); |
| 3203 | return NULL; |
| 3204 | } |
| 3205 | |
| 3206 | return groupObj; |
| 3207 | } |
| 3208 | Object* dvmGetSystemThreadGroup(void) |
| 3209 | { |
| 3210 | return getStaticThreadGroup("mSystem"); |
| 3211 | } |
| 3212 | Object* dvmGetMainThreadGroup(void) |
| 3213 | { |
| 3214 | return getStaticThreadGroup("mMain"); |
| 3215 | } |
| 3216 | |
| 3217 | /* |
| 3218 | * Given a VMThread object, return the associated Thread*. |
| 3219 | * |
| 3220 | * NOTE: if the thread detaches, the struct Thread will disappear, and |
| 3221 | * we will be touching invalid data. For safety, lock the thread list |
| 3222 | * before calling this. |
| 3223 | */ |
| 3224 | Thread* dvmGetThreadFromThreadObject(Object* vmThreadObj) |
| 3225 | { |
| 3226 | int vmData; |
| 3227 | |
| 3228 | vmData = dvmGetFieldInt(vmThreadObj, gDvm.offJavaLangVMThread_vmData); |
| Andy McFadden | 4486036 | 2009-08-06 17:56:14 -0700 | [diff] [blame] | 3229 | |
| 3230 | if (false) { |
| 3231 | Thread* thread = gDvm.threadList; |
| 3232 | while (thread != NULL) { |
| 3233 | if ((Thread*)vmData == thread) |
| 3234 | break; |
| 3235 | |
| 3236 | thread = thread->next; |
| 3237 | } |
| 3238 | |
| 3239 | if (thread == NULL) { |
| 3240 | LOGW("WARNING: vmThreadObj=%p has thread=%p, not in thread list\n", |
| 3241 | vmThreadObj, (Thread*)vmData); |
| 3242 | vmData = 0; |
| 3243 | } |
| 3244 | } |
| 3245 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3246 | return (Thread*) vmData; |
| 3247 | } |
| 3248 | |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 3249 | /* |
| 3250 | * Given a pthread handle, return the associated Thread*. |
| Andy McFadden | 0a24ef9 | 2010-03-12 13:39:59 -0800 | [diff] [blame] | 3251 | * Caller must hold the thread list lock. |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 3252 | * |
| 3253 | * Returns NULL if the thread was not found. |
| 3254 | */ |
| 3255 | Thread* dvmGetThreadByHandle(pthread_t handle) |
| 3256 | { |
| Andy McFadden | 0a24ef9 | 2010-03-12 13:39:59 -0800 | [diff] [blame] | 3257 | Thread* thread; |
| 3258 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 3259 | if (thread->handle == handle) |
| 3260 | break; |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 3261 | } |
| Andy McFadden | 0a24ef9 | 2010-03-12 13:39:59 -0800 | [diff] [blame] | 3262 | return thread; |
| 3263 | } |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 3264 | |
| Andy McFadden | 0a24ef9 | 2010-03-12 13:39:59 -0800 | [diff] [blame] | 3265 | /* |
| 3266 | * Given a threadId, return the associated Thread*. |
| 3267 | * Caller must hold the thread list lock. |
| 3268 | * |
| 3269 | * Returns NULL if the thread was not found. |
| 3270 | */ |
| 3271 | Thread* dvmGetThreadByThreadId(u4 threadId) |
| 3272 | { |
| 3273 | Thread* thread; |
| 3274 | for (thread = gDvm.threadList; thread != NULL; thread = thread->next) { |
| 3275 | if (thread->threadId == threadId) |
| 3276 | break; |
| 3277 | } |
| Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 3278 | return thread; |
| 3279 | } |
| 3280 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3281 | |
| 3282 | /* |
| 3283 | * Conversion map for "nice" values. |
| 3284 | * |
| 3285 | * We use Android thread priority constants to be consistent with the rest |
| 3286 | * of the system. In some cases adjacent entries may overlap. |
| 3287 | */ |
| 3288 | static const int kNiceValues[10] = { |
| 3289 | ANDROID_PRIORITY_LOWEST, /* 1 (MIN_PRIORITY) */ |
| 3290 | ANDROID_PRIORITY_BACKGROUND + 6, |
| 3291 | ANDROID_PRIORITY_BACKGROUND + 3, |
| 3292 | ANDROID_PRIORITY_BACKGROUND, |
| 3293 | ANDROID_PRIORITY_NORMAL, /* 5 (NORM_PRIORITY) */ |
| 3294 | ANDROID_PRIORITY_NORMAL - 2, |
| 3295 | ANDROID_PRIORITY_NORMAL - 4, |
| 3296 | ANDROID_PRIORITY_URGENT_DISPLAY + 3, |
| 3297 | ANDROID_PRIORITY_URGENT_DISPLAY + 2, |
| 3298 | ANDROID_PRIORITY_URGENT_DISPLAY /* 10 (MAX_PRIORITY) */ |
| 3299 | }; |
| 3300 | |
| 3301 | /* |
| 3302 | * Change the priority of a system thread to match that of the Thread object. |
| 3303 | * |
| 3304 | * We map a priority value from 1-10 to Linux "nice" values, where lower |
| 3305 | * numbers indicate higher priority. |
| 3306 | */ |
| 3307 | void dvmChangeThreadPriority(Thread* thread, int newPriority) |
| 3308 | { |
| 3309 | pid_t pid = thread->systemTid; |
| 3310 | int newNice; |
| 3311 | |
| 3312 | if (newPriority < 1 || newPriority > 10) { |
| 3313 | LOGW("bad priority %d\n", newPriority); |
| 3314 | newPriority = 5; |
| 3315 | } |
| 3316 | newNice = kNiceValues[newPriority-1]; |
| 3317 | |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3318 | if (newNice >= ANDROID_PRIORITY_BACKGROUND) { |
| San Mehat | 5a2056c | 2009-09-12 10:10:13 -0700 | [diff] [blame] | 3319 | set_sched_policy(dvmGetSysThreadId(), SP_BACKGROUND); |
| San Mehat | 3e371e2 | 2009-06-26 08:36:16 -0700 | [diff] [blame] | 3320 | } else if (getpriority(PRIO_PROCESS, pid) >= ANDROID_PRIORITY_BACKGROUND) { |
| San Mehat | 5a2056c | 2009-09-12 10:10:13 -0700 | [diff] [blame] | 3321 | set_sched_policy(dvmGetSysThreadId(), SP_FOREGROUND); |
| San Mehat | 256fc15 | 2009-04-21 14:03:06 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3324 | if (setpriority(PRIO_PROCESS, pid, newNice) != 0) { |
| 3325 | char* str = dvmGetThreadName(thread); |
| 3326 | LOGI("setPriority(%d) '%s' to prio=%d(n=%d) failed: %s\n", |
| 3327 | pid, str, newPriority, newNice, strerror(errno)); |
| 3328 | free(str); |
| 3329 | } else { |
| 3330 | LOGV("setPriority(%d) to prio=%d(n=%d)\n", |
| 3331 | pid, newPriority, newNice); |
| 3332 | } |
| 3333 | } |
| 3334 | |
| 3335 | /* |
| 3336 | * Get the thread priority for the current thread by querying the system. |
| 3337 | * This is useful when attaching a thread through JNI. |
| 3338 | * |
| 3339 | * Returns a value from 1 to 10 (compatible with java.lang.Thread values). |
| 3340 | */ |
| 3341 | static int getThreadPriorityFromSystem(void) |
| 3342 | { |
| 3343 | int i, sysprio, jprio; |
| 3344 | |
| 3345 | errno = 0; |
| 3346 | sysprio = getpriority(PRIO_PROCESS, 0); |
| 3347 | if (sysprio == -1 && errno != 0) { |
| 3348 | LOGW("getpriority() failed: %s\n", strerror(errno)); |
| 3349 | return THREAD_NORM_PRIORITY; |
| 3350 | } |
| 3351 | |
| 3352 | jprio = THREAD_MIN_PRIORITY; |
| 3353 | for (i = 0; i < NELEM(kNiceValues); i++) { |
| 3354 | if (sysprio >= kNiceValues[i]) |
| 3355 | break; |
| 3356 | jprio++; |
| 3357 | } |
| 3358 | if (jprio > THREAD_MAX_PRIORITY) |
| 3359 | jprio = THREAD_MAX_PRIORITY; |
| 3360 | |
| 3361 | return jprio; |
| 3362 | } |
| 3363 | |
| 3364 | |
| 3365 | /* |
| 3366 | * Return true if the thread is on gDvm.threadList. |
| 3367 | * Caller should not hold gDvm.threadListLock. |
| 3368 | */ |
| 3369 | bool dvmIsOnThreadList(const Thread* thread) |
| 3370 | { |
| 3371 | bool ret = false; |
| 3372 | |
| 3373 | dvmLockThreadList(NULL); |
| 3374 | if (thread == gDvm.threadList) { |
| 3375 | ret = true; |
| 3376 | } else { |
| 3377 | ret = thread->prev != NULL || thread->next != NULL; |
| 3378 | } |
| 3379 | dvmUnlockThreadList(); |
| 3380 | |
| 3381 | return ret; |
| 3382 | } |
| 3383 | |
| 3384 | /* |
| 3385 | * Dump a thread to the log file -- just calls dvmDumpThreadEx() with an |
| 3386 | * output target. |
| 3387 | */ |
| 3388 | void dvmDumpThread(Thread* thread, bool isRunning) |
| 3389 | { |
| 3390 | DebugOutputTarget target; |
| 3391 | |
| 3392 | dvmCreateLogOutputTarget(&target, ANDROID_LOG_INFO, LOG_TAG); |
| 3393 | dvmDumpThreadEx(&target, thread, isRunning); |
| 3394 | } |
| 3395 | |
| 3396 | /* |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3397 | * Try to get the scheduler group. |
| 3398 | * |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3399 | * The data from /proc/<pid>/cgroup looks (something) like: |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3400 | * 2:cpu:/bg_non_interactive |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3401 | * 1:cpuacct:/ |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3402 | * |
| 3403 | * We return the part after the "/", which will be an empty string for |
| 3404 | * the default cgroup. If the string is longer than "bufLen", the string |
| 3405 | * will be truncated. |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3406 | * |
| 3407 | * TODO: this is cloned from a static function in libcutils; expose that? |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3408 | */ |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3409 | static int getSchedulerGroup(int tid, char* buf, size_t bufLen) |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3410 | { |
| 3411 | #ifdef HAVE_ANDROID_OS |
| 3412 | char pathBuf[32]; |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3413 | char lineBuf[256]; |
| 3414 | FILE *fp; |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3415 | |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3416 | snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid); |
| 3417 | if (!(fp = fopen(pathBuf, "r"))) { |
| 3418 | return -1; |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3421 | while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) { |
| 3422 | char *next = lineBuf; |
| 3423 | char *subsys; |
| 3424 | char *grp; |
| 3425 | size_t len; |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3426 | |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3427 | /* Junk the first field */ |
| 3428 | if (!strsep(&next, ":")) { |
| 3429 | goto out_bad_data; |
| 3430 | } |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3431 | |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3432 | if (!(subsys = strsep(&next, ":"))) { |
| 3433 | goto out_bad_data; |
| 3434 | } |
| 3435 | |
| 3436 | if (strcmp(subsys, "cpu")) { |
| 3437 | /* Not the subsys we're looking for */ |
| 3438 | continue; |
| 3439 | } |
| 3440 | |
| 3441 | if (!(grp = strsep(&next, ":"))) { |
| 3442 | goto out_bad_data; |
| 3443 | } |
| 3444 | grp++; /* Drop the leading '/' */ |
| 3445 | len = strlen(grp); |
| 3446 | grp[len-1] = '\0'; /* Drop the trailing '\n' */ |
| 3447 | |
| 3448 | if (bufLen <= len) { |
| 3449 | len = bufLen - 1; |
| 3450 | } |
| 3451 | strncpy(buf, grp, len); |
| 3452 | buf[len] = '\0'; |
| 3453 | fclose(fp); |
| 3454 | return 0; |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3455 | } |
| 3456 | |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3457 | LOGE("Failed to find cpu subsys"); |
| 3458 | fclose(fp); |
| 3459 | return -1; |
| 3460 | out_bad_data: |
| 3461 | LOGE("Bad cgroup data {%s}", lineBuf); |
| 3462 | fclose(fp); |
| 3463 | return -1; |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3464 | #else |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3465 | errno = ENOSYS; |
| 3466 | return -1; |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3467 | #endif |
| 3468 | } |
| 3469 | |
| 3470 | /* |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 3471 | * Convert ThreadStatus to a string. |
| 3472 | */ |
| 3473 | const char* dvmGetThreadStatusStr(ThreadStatus status) |
| 3474 | { |
| 3475 | switch (status) { |
| 3476 | case THREAD_ZOMBIE: return "ZOMBIE"; |
| 3477 | case THREAD_RUNNING: return "RUNNABLE"; |
| 3478 | case THREAD_TIMED_WAIT: return "TIMED_WAIT"; |
| 3479 | case THREAD_MONITOR: return "MONITOR"; |
| 3480 | case THREAD_WAIT: return "WAIT"; |
| 3481 | case THREAD_INITIALIZING: return "INITIALIZING"; |
| 3482 | case THREAD_STARTING: return "STARTING"; |
| 3483 | case THREAD_NATIVE: return "NATIVE"; |
| 3484 | case THREAD_VMWAIT: return "VMWAIT"; |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3485 | case THREAD_SUSPENDED: return "SUSPENDED"; |
| Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 3486 | default: return "UNKNOWN"; |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3491 | * Print information about the specified thread. |
| 3492 | * |
| 3493 | * Works best when the thread in question is "self" or has been suspended. |
| 3494 | * When dumping a separate thread that's still running, set "isRunning" to |
| 3495 | * use a more cautious thread dump function. |
| 3496 | */ |
| 3497 | void dvmDumpThreadEx(const DebugOutputTarget* target, Thread* thread, |
| 3498 | bool isRunning) |
| 3499 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3500 | Object* threadObj; |
| 3501 | Object* groupObj; |
| 3502 | StringObject* nameStr; |
| 3503 | char* threadName = NULL; |
| 3504 | char* groupName = NULL; |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3505 | char schedulerGroupBuf[32]; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3506 | bool isDaemon; |
| 3507 | int priority; // java.lang.Thread priority |
| 3508 | int policy; // pthread policy |
| 3509 | struct sched_param sp; // pthread scheduling parameters |
| Christopher Tate | 962f896 | 2010-06-02 16:17:46 -0700 | [diff] [blame] | 3510 | char schedstatBuf[64]; // contents of /proc/[pid]/task/[tid]/schedstat |
| 3511 | int schedstatFd; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3512 | |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 3513 | /* |
| 3514 | * Get the java.lang.Thread object. This function gets called from |
| 3515 | * some weird debug contexts, so it's possible that there's a GC in |
| 3516 | * progress on some other thread. To decrease the chances of the |
| 3517 | * thread object being moved out from under us, we add the reference |
| 3518 | * to the tracked allocation list, which pins it in place. |
| 3519 | * |
| 3520 | * If threadObj is NULL, the thread is still in the process of being |
| 3521 | * attached to the VM, and there's really nothing interesting to |
| 3522 | * say about it yet. |
| 3523 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3524 | threadObj = thread->threadObj; |
| 3525 | if (threadObj == NULL) { |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 3526 | LOGI("Can't dump thread %d: threadObj not set\n", thread->threadId); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3527 | return; |
| 3528 | } |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 3529 | dvmAddTrackedAlloc(threadObj, NULL); |
| 3530 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3531 | nameStr = (StringObject*) dvmGetFieldObject(threadObj, |
| 3532 | gDvm.offJavaLangThread_name); |
| 3533 | threadName = dvmCreateCstrFromString(nameStr); |
| 3534 | |
| 3535 | priority = dvmGetFieldInt(threadObj, gDvm.offJavaLangThread_priority); |
| 3536 | isDaemon = dvmGetFieldBoolean(threadObj, gDvm.offJavaLangThread_daemon); |
| 3537 | |
| 3538 | if (pthread_getschedparam(pthread_self(), &policy, &sp) != 0) { |
| 3539 | LOGW("Warning: pthread_getschedparam failed\n"); |
| 3540 | policy = -1; |
| 3541 | sp.sched_priority = -1; |
| 3542 | } |
| Andy McFadden | 7f64ede | 2010-03-03 15:37:10 -0800 | [diff] [blame] | 3543 | if (getSchedulerGroup(thread->systemTid, schedulerGroupBuf, |
| 3544 | sizeof(schedulerGroupBuf)) != 0) |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3545 | { |
| 3546 | strcpy(schedulerGroupBuf, "unknown"); |
| 3547 | } else if (schedulerGroupBuf[0] == '\0') { |
| 3548 | strcpy(schedulerGroupBuf, "default"); |
| 3549 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3550 | |
| 3551 | /* a null value for group is not expected, but deal with it anyway */ |
| 3552 | groupObj = (Object*) dvmGetFieldObject(threadObj, |
| 3553 | gDvm.offJavaLangThread_group); |
| 3554 | if (groupObj != NULL) { |
| 3555 | int offset = dvmFindFieldOffset(gDvm.classJavaLangThreadGroup, |
| 3556 | "name", "Ljava/lang/String;"); |
| 3557 | if (offset < 0) { |
| 3558 | LOGW("Unable to find 'name' field in ThreadGroup\n"); |
| 3559 | } else { |
| 3560 | nameStr = (StringObject*) dvmGetFieldObject(groupObj, offset); |
| 3561 | groupName = dvmCreateCstrFromString(nameStr); |
| 3562 | } |
| 3563 | } |
| 3564 | if (groupName == NULL) |
| Andy McFadden | 40607dd | 2010-06-28 16:57:24 -0700 | [diff] [blame] | 3565 | groupName = strdup("(null; initializing?)"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3566 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3567 | dvmPrintDebugMessage(target, |
| Ben Cheng | dc4a928 | 2010-02-24 17:27:01 -0800 | [diff] [blame] | 3568 | "\"%s\"%s prio=%d tid=%d %s%s\n", |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3569 | threadName, isDaemon ? " daemon" : "", |
| Ben Cheng | dc4a928 | 2010-02-24 17:27:01 -0800 | [diff] [blame] | 3570 | priority, thread->threadId, dvmGetThreadStatusStr(thread->status), |
| 3571 | #if defined(WITH_JIT) |
| 3572 | thread->inJitCodeCache ? " JIT" : "" |
| 3573 | #else |
| 3574 | "" |
| 3575 | #endif |
| 3576 | ); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3577 | dvmPrintDebugMessage(target, |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3578 | " | group=\"%s\" sCount=%d dsCount=%d obj=%p self=%p\n", |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3579 | groupName, thread->suspendCount, thread->dbgSuspendCount, |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 3580 | thread->threadObj, thread); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3581 | dvmPrintDebugMessage(target, |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3582 | " | sysTid=%d nice=%d sched=%d/%d cgrp=%s handle=%d\n", |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3583 | thread->systemTid, getpriority(PRIO_PROCESS, thread->systemTid), |
| Andy McFadden | d62c0b5 | 2009-08-04 15:02:12 -0700 | [diff] [blame] | 3584 | policy, sp.sched_priority, schedulerGroupBuf, (int)thread->handle); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3585 | |
| Christopher Tate | 962f896 | 2010-06-02 16:17:46 -0700 | [diff] [blame] | 3586 | snprintf(schedstatBuf, sizeof(schedstatBuf), "/proc/%d/task/%d/schedstat", |
| 3587 | getpid(), thread->systemTid); |
| 3588 | schedstatFd = open(schedstatBuf, O_RDONLY); |
| 3589 | if (schedstatFd >= 0) { |
| 3590 | int bytes; |
| 3591 | bytes = read(schedstatFd, schedstatBuf, sizeof(schedstatBuf) - 1); |
| 3592 | close(schedstatFd); |
| 3593 | if (bytes > 1) { |
| 3594 | schedstatBuf[bytes-1] = 0; // trailing newline |
| 3595 | dvmPrintDebugMessage(target, " | schedstat=( %s )\n", schedstatBuf); |
| 3596 | } |
| 3597 | } |
| 3598 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3599 | #ifdef WITH_MONITOR_TRACKING |
| 3600 | if (!isRunning) { |
| 3601 | LockedObjectData* lod = thread->pLockedObjects; |
| 3602 | if (lod != NULL) |
| 3603 | dvmPrintDebugMessage(target, " | monitors held:\n"); |
| 3604 | else |
| 3605 | dvmPrintDebugMessage(target, " | monitors held: <none>\n"); |
| 3606 | while (lod != NULL) { |
| Elliott Hughes | beea0b7 | 2009-11-13 11:20:15 -0800 | [diff] [blame] | 3607 | Object* obj = lod->obj; |
| 3608 | if (obj->clazz == gDvm.classJavaLangClass) { |
| 3609 | ClassObject* clazz = (ClassObject*) obj; |
| 3610 | dvmPrintDebugMessage(target, " > %p[%d] (%s object for class %s)\n", |
| 3611 | obj, lod->recursionCount, obj->clazz->descriptor, |
| 3612 | clazz->descriptor); |
| 3613 | } else { |
| 3614 | dvmPrintDebugMessage(target, " > %p[%d] (%s)\n", |
| 3615 | obj, lod->recursionCount, obj->clazz->descriptor); |
| 3616 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3617 | lod = lod->next; |
| 3618 | } |
| 3619 | } |
| 3620 | #endif |
| 3621 | |
| 3622 | if (isRunning) |
| 3623 | dvmDumpRunningThreadStack(target, thread); |
| 3624 | else |
| 3625 | dvmDumpThreadStack(target, thread); |
| 3626 | |
| Andy McFadden | e3346d8 | 2010-06-02 15:37:21 -0700 | [diff] [blame] | 3627 | dvmReleaseTrackedAlloc(threadObj, NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3628 | free(threadName); |
| 3629 | free(groupName); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | /* |
| 3633 | * Get the name of a thread. |
| 3634 | * |
| 3635 | * For correctness, the caller should hold the thread list lock to ensure |
| 3636 | * that the thread doesn't go away mid-call. |
| 3637 | * |
| 3638 | * Returns a newly-allocated string, or NULL if the Thread doesn't have a name. |
| 3639 | */ |
| 3640 | char* dvmGetThreadName(Thread* thread) |
| 3641 | { |
| 3642 | StringObject* nameObj; |
| 3643 | |
| 3644 | if (thread->threadObj == NULL) { |
| 3645 | LOGW("threadObj is NULL, name not available\n"); |
| 3646 | return strdup("-unknown-"); |
| 3647 | } |
| 3648 | |
| 3649 | nameObj = (StringObject*) |
| 3650 | dvmGetFieldObject(thread->threadObj, gDvm.offJavaLangThread_name); |
| 3651 | return dvmCreateCstrFromString(nameObj); |
| 3652 | } |
| 3653 | |
| 3654 | /* |
| 3655 | * Dump all threads to the log file -- just calls dvmDumpAllThreadsEx() with |
| 3656 | * an output target. |
| 3657 | */ |
| 3658 | void dvmDumpAllThreads(bool grabLock) |
| 3659 | { |
| 3660 | DebugOutputTarget target; |
| 3661 | |
| 3662 | dvmCreateLogOutputTarget(&target, ANDROID_LOG_INFO, LOG_TAG); |
| 3663 | dvmDumpAllThreadsEx(&target, grabLock); |
| 3664 | } |
| 3665 | |
| 3666 | /* |
| 3667 | * Print information about all known threads. Assumes they have been |
| 3668 | * suspended (or are in a non-interpreting state, e.g. WAIT or NATIVE). |
| 3669 | * |
| 3670 | * If "grabLock" is true, we grab the thread lock list. This is important |
| 3671 | * to do unless the caller already holds the lock. |
| 3672 | */ |
| 3673 | void dvmDumpAllThreadsEx(const DebugOutputTarget* target, bool grabLock) |
| 3674 | { |
| 3675 | Thread* thread; |
| 3676 | |
| 3677 | dvmPrintDebugMessage(target, "DALVIK THREADS:\n"); |
| 3678 | |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 3679 | #ifdef HAVE_ANDROID_OS |
| 3680 | dvmPrintDebugMessage(target, |
| 3681 | "(mutexes: tll=%x tsl=%x tscl=%x ghl=%x hwl=%x hwll=%x)\n", |
| 3682 | gDvm.threadListLock.value, |
| 3683 | gDvm._threadSuspendLock.value, |
| 3684 | gDvm.threadSuspendCountLock.value, |
| 3685 | gDvm.gcHeapLock.value, |
| 3686 | gDvm.heapWorkerLock.value, |
| 3687 | gDvm.heapWorkerListLock.value); |
| 3688 | #endif |
| 3689 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3690 | if (grabLock) |
| 3691 | dvmLockThreadList(dvmThreadSelf()); |
| 3692 | |
| 3693 | thread = gDvm.threadList; |
| 3694 | while (thread != NULL) { |
| 3695 | dvmDumpThreadEx(target, thread, false); |
| 3696 | |
| 3697 | /* verify link */ |
| 3698 | assert(thread->next == NULL || thread->next->prev == thread); |
| 3699 | |
| 3700 | thread = thread->next; |
| 3701 | } |
| 3702 | |
| 3703 | if (grabLock) |
| 3704 | dvmUnlockThreadList(); |
| 3705 | } |
| 3706 | |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 3707 | /* |
| 3708 | * Nuke the target thread from orbit. |
| 3709 | * |
| 3710 | * The idea is to send a "crash" signal to the target thread so that |
| 3711 | * debuggerd will take notice and dump an appropriate stack trace. |
| 3712 | * Because of the way debuggerd works, we have to throw the same signal |
| 3713 | * at it twice. |
| 3714 | * |
| 3715 | * This does not necessarily cause the entire process to stop, but once a |
| 3716 | * thread has been nuked the rest of the system is likely to be unstable. |
| 3717 | * This returns so that some limited set of additional operations may be |
| Andy McFadden | d4e0952 | 2010-03-23 12:34:43 -0700 | [diff] [blame] | 3718 | * performed, but it's advisable (and expected) to call dvmAbort soon. |
| 3719 | * (This is NOT a way to simply cancel a thread.) |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 3720 | */ |
| 3721 | void dvmNukeThread(Thread* thread) |
| 3722 | { |
| Andy McFadden | a388a16 | 2010-03-18 16:27:14 -0700 | [diff] [blame] | 3723 | /* suppress the heapworker watchdog to assist anyone using a debugger */ |
| 3724 | gDvm.nativeDebuggerActive = true; |
| 3725 | |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 3726 | /* |
| Andy McFadden | d4e0952 | 2010-03-23 12:34:43 -0700 | [diff] [blame] | 3727 | * Send the signals, separated by a brief interval to allow debuggerd |
| 3728 | * to work its magic. An uncommon signal like SIGFPE or SIGSTKFLT |
| 3729 | * can be used instead of SIGSEGV to avoid making it look like the |
| 3730 | * code actually crashed at the current point of execution. |
| 3731 | * |
| 3732 | * (Observed behavior: with SIGFPE, debuggerd will dump the target |
| 3733 | * thread and then the thread that calls dvmAbort. With SIGSEGV, |
| 3734 | * you don't get the second stack trace; possibly something in the |
| 3735 | * kernel decides that a signal has already been sent and it's time |
| 3736 | * to just kill the process. The position in the current thread is |
| 3737 | * generally known, so the second dump is not useful.) |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 3738 | * |
| Andy McFadden | a388a16 | 2010-03-18 16:27:14 -0700 | [diff] [blame] | 3739 | * The target thread can continue to execute between the two signals. |
| 3740 | * (The first just causes debuggerd to attach to it.) |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 3741 | */ |
| Andy McFadden | d4e0952 | 2010-03-23 12:34:43 -0700 | [diff] [blame] | 3742 | LOGD("threadid=%d: sending two SIGSTKFLTs to threadid=%d (tid=%d) to" |
| 3743 | " cause debuggerd dump\n", |
| 3744 | dvmThreadSelf()->threadId, thread->threadId, thread->systemTid); |
| 3745 | pthread_kill(thread->handle, SIGSTKFLT); |
| Andy McFadden | a388a16 | 2010-03-18 16:27:14 -0700 | [diff] [blame] | 3746 | usleep(2 * 1000 * 1000); // TODO: timed-wait until debuggerd attaches |
| Andy McFadden | d4e0952 | 2010-03-23 12:34:43 -0700 | [diff] [blame] | 3747 | pthread_kill(thread->handle, SIGSTKFLT); |
| Andy McFadden | 7122d86 | 2010-03-19 15:18:57 -0700 | [diff] [blame] | 3748 | LOGD("Sent, pausing to let debuggerd run\n"); |
| Andy McFadden | a388a16 | 2010-03-18 16:27:14 -0700 | [diff] [blame] | 3749 | usleep(8 * 1000 * 1000); // TODO: timed-wait until debuggerd finishes |
| Andy McFadden | d4e0952 | 2010-03-23 12:34:43 -0700 | [diff] [blame] | 3750 | |
| 3751 | /* ignore SIGSEGV so the eventual dmvAbort() doesn't notify debuggerd */ |
| 3752 | signal(SIGSEGV, SIG_IGN); |
| Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 3753 | LOGD("Continuing\n"); |
| 3754 | } |
| 3755 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3756 | #ifdef WITH_MONITOR_TRACKING |
| 3757 | /* |
| 3758 | * Count up the #of locked objects in the current thread. |
| 3759 | */ |
| 3760 | static int getThreadObjectCount(const Thread* self) |
| 3761 | { |
| 3762 | LockedObjectData* lod; |
| 3763 | int count = 0; |
| 3764 | |
| 3765 | lod = self->pLockedObjects; |
| 3766 | while (lod != NULL) { |
| 3767 | count++; |
| 3768 | lod = lod->next; |
| 3769 | } |
| 3770 | return count; |
| 3771 | } |
| 3772 | |
| 3773 | /* |
| 3774 | * Add the object to the thread's locked object list if it doesn't already |
| 3775 | * exist. The most recently added object is the most likely to be released |
| 3776 | * next, so we insert at the head of the list. |
| 3777 | * |
| 3778 | * If it already exists, we increase the recursive lock count. |
| 3779 | * |
| 3780 | * The object's lock may be thin or fat. |
| 3781 | */ |
| 3782 | void dvmAddToMonitorList(Thread* self, Object* obj, bool withTrace) |
| 3783 | { |
| 3784 | LockedObjectData* newLod; |
| 3785 | LockedObjectData* lod; |
| 3786 | int* trace; |
| 3787 | int depth; |
| 3788 | |
| 3789 | lod = self->pLockedObjects; |
| 3790 | while (lod != NULL) { |
| 3791 | if (lod->obj == obj) { |
| 3792 | lod->recursionCount++; |
| 3793 | LOGV("+++ +recursive lock %p -> %d\n", obj, lod->recursionCount); |
| 3794 | return; |
| 3795 | } |
| 3796 | lod = lod->next; |
| 3797 | } |
| 3798 | |
| 3799 | newLod = (LockedObjectData*) calloc(1, sizeof(LockedObjectData)); |
| 3800 | if (newLod == NULL) { |
| 3801 | LOGE("malloc failed on %d bytes\n", sizeof(LockedObjectData)); |
| 3802 | return; |
| 3803 | } |
| 3804 | newLod->obj = obj; |
| 3805 | newLod->recursionCount = 0; |
| 3806 | |
| 3807 | if (withTrace) { |
| 3808 | trace = dvmFillInStackTraceRaw(self, &depth); |
| 3809 | newLod->rawStackTrace = trace; |
| 3810 | newLod->stackDepth = depth; |
| 3811 | } |
| 3812 | |
| 3813 | newLod->next = self->pLockedObjects; |
| 3814 | self->pLockedObjects = newLod; |
| 3815 | |
| 3816 | LOGV("+++ threadid=%d: added %p, now %d\n", |
| 3817 | self->threadId, newLod, getThreadObjectCount(self)); |
| 3818 | } |
| 3819 | |
| 3820 | /* |
| 3821 | * Remove the object from the thread's locked object list. If the entry |
| 3822 | * has a nonzero recursion count, we just decrement the count instead. |
| 3823 | */ |
| 3824 | void dvmRemoveFromMonitorList(Thread* self, Object* obj) |
| 3825 | { |
| 3826 | LockedObjectData* lod; |
| 3827 | LockedObjectData* prevLod; |
| 3828 | |
| 3829 | lod = self->pLockedObjects; |
| 3830 | prevLod = NULL; |
| 3831 | while (lod != NULL) { |
| 3832 | if (lod->obj == obj) { |
| 3833 | if (lod->recursionCount > 0) { |
| 3834 | lod->recursionCount--; |
| 3835 | LOGV("+++ -recursive lock %p -> %d\n", |
| 3836 | obj, lod->recursionCount); |
| 3837 | return; |
| 3838 | } else { |
| 3839 | break; |
| 3840 | } |
| 3841 | } |
| 3842 | prevLod = lod; |
| 3843 | lod = lod->next; |
| 3844 | } |
| 3845 | |
| 3846 | if (lod == NULL) { |
| 3847 | LOGW("BUG: object %p not found in thread's lock list\n", obj); |
| 3848 | return; |
| 3849 | } |
| 3850 | if (prevLod == NULL) { |
| 3851 | /* first item in list */ |
| 3852 | assert(self->pLockedObjects == lod); |
| 3853 | self->pLockedObjects = lod->next; |
| 3854 | } else { |
| 3855 | /* middle/end of list */ |
| 3856 | prevLod->next = lod->next; |
| 3857 | } |
| 3858 | |
| 3859 | LOGV("+++ threadid=%d: removed %p, now %d\n", |
| 3860 | self->threadId, lod, getThreadObjectCount(self)); |
| 3861 | free(lod->rawStackTrace); |
| 3862 | free(lod); |
| 3863 | } |
| 3864 | |
| 3865 | /* |
| 3866 | * If the specified object is already in the thread's locked object list, |
| 3867 | * return the LockedObjectData struct. Otherwise return NULL. |
| 3868 | */ |
| 3869 | LockedObjectData* dvmFindInMonitorList(const Thread* self, const Object* obj) |
| 3870 | { |
| 3871 | LockedObjectData* lod; |
| 3872 | |
| 3873 | lod = self->pLockedObjects; |
| 3874 | while (lod != NULL) { |
| 3875 | if (lod->obj == obj) |
| 3876 | return lod; |
| 3877 | lod = lod->next; |
| 3878 | } |
| 3879 | return NULL; |
| 3880 | } |
| 3881 | #endif /*WITH_MONITOR_TRACKING*/ |
| 3882 | |
| 3883 | |
| 3884 | /* |
| 3885 | * GC helper functions |
| 3886 | */ |
| 3887 | |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3888 | /* |
| 3889 | * Add the contents of the registers from the interpreted call stack. |
| 3890 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3891 | static void gcScanInterpStackReferences(Thread *thread) |
| 3892 | { |
| 3893 | const u4 *framePtr; |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3894 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 3895 | bool first = true; |
| 3896 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3897 | |
| 3898 | framePtr = (const u4 *)thread->curFrame; |
| 3899 | while (framePtr != NULL) { |
| 3900 | const StackSaveArea *saveArea; |
| 3901 | const Method *method; |
| 3902 | |
| 3903 | saveArea = SAVEAREA_FROM_FP(framePtr); |
| 3904 | method = saveArea->method; |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 3905 | if (method != NULL) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3906 | #ifdef COUNT_PRECISE_METHODS |
| 3907 | /* the GC is running, so no lock required */ |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3908 | if (dvmPointerSetAddEntry(gDvm.preciseMethods, method)) |
| 3909 | LOGI("PGC: added %s.%s %p\n", |
| 3910 | method->clazz->descriptor, method->name, method); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3911 | #endif |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3912 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 3913 | /* |
| 3914 | * May also want to enable the memset() in the "invokeMethod" |
| 3915 | * goto target in the portable interpreter. That sets the stack |
| 3916 | * to a pattern that makes referring to uninitialized data |
| 3917 | * very obvious. |
| 3918 | */ |
| 3919 | |
| 3920 | if (first) { |
| 3921 | /* |
| 3922 | * First frame, isn't native, check the "alternate" saved PC |
| 3923 | * as a sanity check. |
| 3924 | * |
| 3925 | * It seems like we could check the second frame if the first |
| 3926 | * is native, since the PCs should be the same. It turns out |
| 3927 | * this doesn't always work. The problem is that we could |
| 3928 | * have calls in the sequence: |
| 3929 | * interp method #2 |
| 3930 | * native method |
| 3931 | * interp method #1 |
| 3932 | * |
| 3933 | * and then GC while in the native method after returning |
| 3934 | * from interp method #2. The currentPc on the stack is |
| 3935 | * for interp method #1, but thread->currentPc2 is still |
| 3936 | * set for the last thing interp method #2 did. |
| 3937 | * |
| 3938 | * This can also happen in normal execution: |
| 3939 | * - sget-object on not-yet-loaded class |
| 3940 | * - class init updates currentPc2 |
| 3941 | * - static field init is handled by parsing annotations; |
| 3942 | * static String init requires creation of a String object, |
| 3943 | * which can cause a GC |
| 3944 | * |
| 3945 | * Essentially, any pattern that involves executing |
| 3946 | * interpreted code and then causes an allocation without |
| 3947 | * executing instructions in the original method will hit |
| 3948 | * this. These are rare enough that the test still has |
| 3949 | * some value. |
| 3950 | */ |
| 3951 | if (saveArea->xtra.currentPc != thread->currentPc2) { |
| 3952 | LOGW("PGC: savedPC(%p) != current PC(%p), %s.%s ins=%p\n", |
| 3953 | saveArea->xtra.currentPc, thread->currentPc2, |
| 3954 | method->clazz->descriptor, method->name, method->insns); |
| 3955 | if (saveArea->xtra.currentPc != NULL) |
| 3956 | LOGE(" pc inst = 0x%04x\n", *saveArea->xtra.currentPc); |
| 3957 | if (thread->currentPc2 != NULL) |
| 3958 | LOGE(" pc2 inst = 0x%04x\n", *thread->currentPc2); |
| 3959 | dvmDumpThread(thread, false); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3960 | } |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3961 | } else { |
| 3962 | /* |
| 3963 | * It's unusual, but not impossible, for a non-first frame |
| 3964 | * to be at something other than a method invocation. For |
| 3965 | * example, if we do a new-instance on a nonexistent class, |
| 3966 | * we'll have a lot of class loader activity on the stack |
| 3967 | * above the frame with the "new" operation. Could also |
| 3968 | * happen while we initialize a Throwable when an instruction |
| 3969 | * fails. |
| 3970 | * |
| 3971 | * So there's not much we can do here to verify the PC, |
| 3972 | * except to verify that it's a GC point. |
| 3973 | */ |
| 3974 | } |
| 3975 | assert(saveArea->xtra.currentPc != NULL); |
| 3976 | #endif |
| 3977 | |
| 3978 | const RegisterMap* pMap; |
| 3979 | const u1* regVector; |
| 3980 | int i; |
| 3981 | |
| Andy McFadden | cf8b55c | 2009-04-13 15:26:03 -0700 | [diff] [blame] | 3982 | Method* nonConstMethod = (Method*) method; // quiet gcc |
| 3983 | pMap = dvmGetExpandedRegisterMap(nonConstMethod); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3984 | if (pMap != NULL) { |
| 3985 | /* found map, get registers for this address */ |
| 3986 | int addr = saveArea->xtra.currentPc - method->insns; |
| Andy McFadden | d45a887 | 2009-03-24 20:41:52 -0700 | [diff] [blame] | 3987 | regVector = dvmRegisterMapGetLine(pMap, addr); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3988 | if (regVector == NULL) { |
| 3989 | LOGW("PGC: map but no entry for %s.%s addr=0x%04x\n", |
| 3990 | method->clazz->descriptor, method->name, addr); |
| 3991 | } else { |
| 3992 | LOGV("PGC: found map for %s.%s 0x%04x (t=%d)\n", |
| 3993 | method->clazz->descriptor, method->name, addr, |
| 3994 | thread->threadId); |
| 3995 | } |
| 3996 | } else { |
| 3997 | /* |
| 3998 | * No map found. If precise GC is disabled this is |
| 3999 | * expected -- we don't create pointers to the map data even |
| 4000 | * if it's present -- but if it's enabled it means we're |
| 4001 | * unexpectedly falling back on a conservative scan, so it's |
| 4002 | * worth yelling a little. |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 4003 | */ |
| 4004 | if (gDvm.preciseGc) { |
| Andy McFadden | a66a01a | 2009-08-18 15:11:35 -0700 | [diff] [blame] | 4005 | LOGVV("PGC: no map for %s.%s\n", |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 4006 | method->clazz->descriptor, method->name); |
| 4007 | } |
| 4008 | regVector = NULL; |
| 4009 | } |
| 4010 | |
| 4011 | if (regVector == NULL) { |
| 4012 | /* conservative scan */ |
| 4013 | for (i = method->registersSize - 1; i >= 0; i--) { |
| 4014 | u4 rval = *framePtr++; |
| 4015 | if (rval != 0 && (rval & 0x3) == 0) { |
| 4016 | dvmMarkIfObject((Object *)rval); |
| 4017 | } |
| 4018 | } |
| 4019 | } else { |
| 4020 | /* |
| 4021 | * Precise scan. v0 is at the lowest address on the |
| 4022 | * interpreted stack, and is the first bit in the register |
| 4023 | * vector, so we can walk through the register map and |
| 4024 | * memory in the same direction. |
| 4025 | * |
| 4026 | * A '1' bit indicates a live reference. |
| 4027 | */ |
| 4028 | u2 bits = 1 << 1; |
| 4029 | for (i = method->registersSize - 1; i >= 0; i--) { |
| 4030 | u4 rval = *framePtr++; |
| 4031 | |
| 4032 | bits >>= 1; |
| 4033 | if (bits == 1) { |
| 4034 | /* set bit 9 so we can tell when we're empty */ |
| 4035 | bits = *regVector++ | 0x0100; |
| 4036 | LOGVV("loaded bits: 0x%02x\n", bits & 0xff); |
| 4037 | } |
| 4038 | |
| 4039 | if (rval != 0 && (bits & 0x01) != 0) { |
| 4040 | /* |
| 4041 | * Non-null, register marked as live reference. This |
| 4042 | * should always be a valid object. |
| 4043 | */ |
| 4044 | #if WITH_EXTRA_GC_CHECKS > 0 |
| 4045 | if ((rval & 0x3) != 0 || |
| 4046 | !dvmIsValidObject((Object*) rval)) |
| 4047 | { |
| 4048 | /* this is very bad */ |
| 4049 | LOGE("PGC: invalid ref in reg %d: 0x%08x\n", |
| 4050 | method->registersSize-1 - i, rval); |
| 4051 | } else |
| 4052 | #endif |
| 4053 | { |
| 4054 | dvmMarkObjectNonNull((Object *)rval); |
| 4055 | } |
| 4056 | } else { |
| 4057 | /* |
| 4058 | * Null or non-reference, do nothing at all. |
| 4059 | */ |
| 4060 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 4061 | if (dvmIsValidObject((Object*) rval)) { |
| 4062 | /* this is normal, but we feel chatty */ |
| 4063 | LOGD("PGC: ignoring valid ref in reg %d: 0x%08x\n", |
| 4064 | method->registersSize-1 - i, rval); |
| 4065 | } |
| 4066 | #endif |
| 4067 | } |
| 4068 | } |
| 4069 | dvmReleaseRegisterMapLine(pMap, regVector); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4070 | } |
| 4071 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4072 | |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 4073 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 4074 | first = false; |
| 4075 | #endif |
| 4076 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4077 | /* Don't fall into an infinite loop if things get corrupted. |
| 4078 | */ |
| 4079 | assert((uintptr_t)saveArea->prevFrame > (uintptr_t)framePtr || |
| 4080 | saveArea->prevFrame == NULL); |
| 4081 | framePtr = saveArea->prevFrame; |
| 4082 | } |
| 4083 | } |
| 4084 | |
| 4085 | static void gcScanReferenceTable(ReferenceTable *refTable) |
| 4086 | { |
| 4087 | Object **op; |
| 4088 | |
| 4089 | //TODO: these asserts are overkill; turn them off when things stablize. |
| 4090 | assert(refTable != NULL); |
| 4091 | assert(refTable->table != NULL); |
| 4092 | assert(refTable->nextEntry != NULL); |
| 4093 | assert((uintptr_t)refTable->nextEntry >= (uintptr_t)refTable->table); |
| 4094 | assert(refTable->nextEntry - refTable->table <= refTable->maxEntries); |
| 4095 | |
| 4096 | op = refTable->table; |
| 4097 | while ((uintptr_t)op < (uintptr_t)refTable->nextEntry) { |
| 4098 | dvmMarkObjectNonNull(*(op++)); |
| 4099 | } |
| 4100 | } |
| 4101 | |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 4102 | #ifdef USE_INDIRECT_REF |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 4103 | static void gcScanIndirectRefTable(IndirectRefTable* pRefTable) |
| 4104 | { |
| 4105 | Object** op = pRefTable->table; |
| 4106 | int numEntries = dvmIndirectRefTableEntries(pRefTable); |
| 4107 | int i; |
| 4108 | |
| 4109 | for (i = 0; i < numEntries; i++) { |
| 4110 | Object* obj = *op; |
| 4111 | if (obj != NULL) |
| 4112 | dvmMarkObjectNonNull(obj); |
| 4113 | op++; |
| 4114 | } |
| 4115 | } |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 4116 | #endif |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 4117 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4118 | /* |
| 4119 | * Scan a Thread and mark any objects it references. |
| 4120 | */ |
| 4121 | static void gcScanThread(Thread *thread) |
| 4122 | { |
| 4123 | assert(thread != NULL); |
| 4124 | |
| 4125 | /* |
| 4126 | * The target thread must be suspended or in a state where it can't do |
| 4127 | * any harm (e.g. in Object.wait()). The only exception is the current |
| 4128 | * thread, which will still be active and in the "running" state. |
| 4129 | * |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 4130 | * It's possible to encounter a false-positive here because a thread |
| 4131 | * transitioning to running from (say) vmwait or native will briefly |
| 4132 | * set their status to running before switching to suspended. This |
| 4133 | * is highly unlikely, but does mean that we don't want to abort if |
| 4134 | * the situation arises. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4135 | */ |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 4136 | if (thread->status == THREAD_RUNNING && thread != dvmThreadSelf()) { |
| Andy McFadden | d40223e | 2009-12-07 15:35:51 -0800 | [diff] [blame] | 4137 | Thread* self = dvmThreadSelf(); |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 4138 | LOGW("threadid=%d: Warning: GC scanning a running thread (%d)\n", |
| Andy McFadden | d40223e | 2009-12-07 15:35:51 -0800 | [diff] [blame] | 4139 | self->threadId, thread->threadId); |
| 4140 | dvmDumpThread(thread, true); |
| 4141 | LOGW("Found by:\n"); |
| 4142 | dvmDumpThread(self, false); |
| 4143 | |
| Andy McFadden | 6dce996 | 2010-08-23 16:45:24 -0700 | [diff] [blame^] | 4144 | /* continue anyway */ |
| Andy McFadden | d40223e | 2009-12-07 15:35:51 -0800 | [diff] [blame] | 4145 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4146 | |
| 4147 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_THREAD_OBJECT, thread->threadId); |
| 4148 | |
| 4149 | dvmMarkObject(thread->threadObj); // could be NULL, when constructing |
| 4150 | |
| 4151 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_NATIVE_STACK, thread->threadId); |
| 4152 | |
| 4153 | dvmMarkObject(thread->exception); // usually NULL |
| 4154 | gcScanReferenceTable(&thread->internalLocalRefTable); |
| 4155 | |
| 4156 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_JNI_LOCAL, thread->threadId); |
| 4157 | |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 4158 | #ifdef USE_INDIRECT_REF |
| 4159 | gcScanIndirectRefTable(&thread->jniLocalRefTable); |
| 4160 | #else |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4161 | gcScanReferenceTable(&thread->jniLocalRefTable); |
| Andy McFadden | d5ab726 | 2009-08-25 07:19:34 -0700 | [diff] [blame] | 4162 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4163 | |
| 4164 | if (thread->jniMonitorRefTable.table != NULL) { |
| 4165 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_JNI_MONITOR, thread->threadId); |
| 4166 | |
| 4167 | gcScanReferenceTable(&thread->jniMonitorRefTable); |
| 4168 | } |
| 4169 | |
| 4170 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_JAVA_FRAME, thread->threadId); |
| 4171 | |
| 4172 | gcScanInterpStackReferences(thread); |
| 4173 | |
| 4174 | HPROF_CLEAR_GC_SCAN_STATE(); |
| 4175 | } |
| 4176 | |
| 4177 | static void gcScanAllThreads() |
| 4178 | { |
| 4179 | Thread *thread; |
| 4180 | |
| 4181 | /* Lock the thread list so we can safely use the |
| 4182 | * next/prev pointers. |
| 4183 | */ |
| 4184 | dvmLockThreadList(dvmThreadSelf()); |
| 4185 | |
| 4186 | for (thread = gDvm.threadList; thread != NULL; |
| 4187 | thread = thread->next) |
| 4188 | { |
| 4189 | /* We need to scan our own stack, so don't special-case |
| 4190 | * the current thread. |
| 4191 | */ |
| 4192 | gcScanThread(thread); |
| 4193 | } |
| 4194 | |
| 4195 | dvmUnlockThreadList(); |
| 4196 | } |
| 4197 | |
| 4198 | void dvmGcScanRootThreadGroups() |
| 4199 | { |
| 4200 | /* We scan the VM's list of threads instead of going |
| 4201 | * through the actual ThreadGroups, but it should be |
| 4202 | * equivalent. |
| 4203 | * |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 4204 | * This assumes that the ThreadGroup class object is in |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4205 | * the root set, which should always be true; it's |
| 4206 | * loaded by the built-in class loader, which is part |
| 4207 | * of the root set. |
| 4208 | */ |
| 4209 | gcScanAllThreads(); |
| 4210 | } |