2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * This is a thread that catches signals and does something useful. For
18 * example, when a SIGQUIT (Ctrl-\) arrives, suspend the VM and dump the
19 * status of all threads.
32 static void* signalCatcherThreadStart(void* arg);
35 * Crank up the signal catcher thread.
37 * Returns immediately.
39 bool dvmSignalCatcherStartup(void)
41 gDvm.haltSignalCatcher = false;
43 if (!dvmCreateInternalThread(&gDvm.signalCatcherHandle,
44 "Signal Catcher", signalCatcherThreadStart, NULL))
51 * Shut down the signal catcher thread if it was started.
53 * Since we know the thread is just sitting around waiting for signals
54 * to arrive, send it one.
56 void dvmSignalCatcherShutdown(void)
58 gDvm.haltSignalCatcher = true;
59 if (gDvm.signalCatcherHandle == 0) // not started yet
62 pthread_kill(gDvm.signalCatcherHandle, SIGQUIT);
64 pthread_join(gDvm.signalCatcherHandle, NULL);
65 LOGV("signal catcher has shut down\n");
70 * Print the name of the current process, if we can get it.
72 static void printProcessName(const DebugOutputTarget* target)
76 fd = open("/proc/self/cmdline", O_RDONLY, 0);
83 actual = read(fd, tmpBuf, sizeof(tmpBuf)-1);
87 tmpBuf[actual] = '\0';
88 dvmPrintDebugMessage(target, "Cmd line: %s\n", tmpBuf);
96 * Dump the stack traces for all threads to the log or to a file. If it's
97 * to a file we have a little setup to do.
99 static void logThreadStacks(void)
101 DebugOutputTarget target;
103 if (gDvm.stackTraceFile == NULL) {
104 /* just dump to log file */
105 dvmCreateLogOutputTarget(&target, ANDROID_LOG_INFO, LOG_TAG);
106 dvmDumpAllThreadsEx(&target, true);
112 * Open the stack trace output file, creating it if necessary. It
113 * needs to be world-writable so other processes can write to it.
115 fd = open(gDvm.stackTraceFile, O_WRONLY | O_APPEND | O_CREAT, 0666);
117 LOGE("Unable to open stack trace file '%s': %s\n",
118 gDvm.stackTraceFile, strerror(errno));
122 /* gain exclusive access to the file */
123 cc = flock(fd, LOCK_EX | LOCK_UN);
125 LOGV("Sleeping on flock(%s)\n", gDvm.stackTraceFile);
126 cc = flock(fd, LOCK_EX);
129 LOGE("Unable to lock stack trace file '%s': %s\n",
130 gDvm.stackTraceFile, strerror(errno));
135 fp = fdopen(fd, "a");
137 LOGE("Unable to fdopen '%s' (%d): %s\n",
138 gDvm.stackTraceFile, fd, strerror(errno));
144 dvmCreateFileOutputTarget(&target, fp);
146 pid_t pid = getpid();
147 time_t now = time(NULL);
149 #ifdef HAVE_LOCALTIME_R
151 ptm = localtime_r(&now, &tmbuf);
153 ptm = localtime(&now);
155 dvmPrintDebugMessage(&target,
156 "\n\n----- pid %d at %04d-%02d-%02d %02d:%02d:%02d -----\n",
157 pid, ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday,
158 ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
159 printProcessName(&target);
160 dvmPrintDebugMessage(&target, "\n");
161 fflush(fp); /* emit at least the header if we crash during dump */
162 dvmDumpAllThreadsEx(&target, true);
163 fprintf(fp, "----- end %d -----\n", pid);
166 * Unlock and close the file, flushing pending data before we unlock
167 * it. The fclose() will close the underyling fd.
173 LOGI("Wrote stack trace to '%s'\n", gDvm.stackTraceFile);
179 * Sleep in sigwait() until a signal arrives.
181 static void* signalCatcherThreadStart(void* arg)
183 Thread* self = dvmThreadSelf();
187 UNUSED_PARAMETER(arg);
189 LOGV("Signal catcher thread started (threadid=%d)\n", self->threadId);
191 /* set up mask with signals we want to handle */
193 sigaddset(&mask, SIGQUIT);
194 sigaddset(&mask, SIGUSR1);
199 dvmChangeStatus(self, THREAD_VMWAIT);
202 * Signals for sigwait() must be blocked but not ignored. We
203 * block signals like SIGQUIT for all threads, so the condition
204 * is met. When the signal hits, we wake up, without any signal
205 * handlers being invoked.
207 * We want to suspend all other threads, so that it's safe to
208 * traverse their stacks.
210 * When running under GDB we occasionally return with EINTR (e.g.
211 * when other threads exit).
214 cc = sigwait(&mask, &rcvd);
217 //LOGV("sigwait: EINTR\n");
220 assert(!"bad result from sigwait");
223 if (!gDvm.haltSignalCatcher) {
224 LOGI("threadid=%d: reacting to signal %d\n",
225 dvmThreadSelf()->threadId, rcvd);
228 /* set our status to RUNNING, self-suspending if GC in progress */
229 dvmChangeStatus(self, THREAD_RUNNING);
231 if (gDvm.haltSignalCatcher)
234 if (rcvd == SIGQUIT) {
235 dvmSuspendAllThreads(SUSPEND_FOR_STACK_DUMP);
236 dvmDumpLoaderStats("sig");
241 dvmLockMutex(&gDvm.jniGlobalRefLock);
242 dvmDumpReferenceTable(&gDvm.jniGlobalRefTable, "JNI global");
243 dvmUnlockMutex(&gDvm.jniGlobalRefLock);
246 //dvmDumpTrackedAllocations(true);
247 dvmResumeAllThreads(SUSPEND_FOR_STACK_DUMP);
248 } else if (rcvd == SIGUSR1) {
250 LOGI("SIGUSR1 forcing GC and HPROF dump\n");
253 LOGI("SIGUSR1 forcing GC (no HPROF)\n");
254 dvmCollectGarbage(false);
257 LOGE("unexpected signal %d\n", rcvd);