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