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