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