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