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.
18 * This is a thread that catches signals and does something useful. For
19 * example, when a SIGQUIT (Ctrl-\) arrives, suspend the VM and dump the
20 * status of all threads.
33 #include <cutils/open_memstream.h>
35 static void* signalCatcherThreadStart(void* arg);
38 * Crank up the signal catcher thread.
40 * Returns immediately.
42 bool dvmSignalCatcherStartup()
44 gDvm.haltSignalCatcher = false;
46 if (!dvmCreateInternalThread(&gDvm.signalCatcherHandle,
47 "Signal Catcher", signalCatcherThreadStart, NULL))
54 * Shut down the signal catcher thread if it was started.
56 * Since we know the thread is just sitting around waiting for signals
57 * to arrive, send it one.
59 void dvmSignalCatcherShutdown()
61 gDvm.haltSignalCatcher = true;
62 if (gDvm.signalCatcherHandle == 0) // not started yet
65 pthread_kill(gDvm.signalCatcherHandle, SIGQUIT);
67 pthread_join(gDvm.signalCatcherHandle, NULL);
68 ALOGV("signal catcher has shut down");
73 * Print the name of the current process, if we can get it.
75 static void printProcessName(const DebugOutputTarget* target)
79 fd = open("/proc/self/cmdline", O_RDONLY, 0);
86 actual = read(fd, tmpBuf, sizeof(tmpBuf)-1);
90 tmpBuf[actual] = '\0';
91 dvmPrintDebugMessage(target, "Cmd line: %s\n", tmpBuf);
99 * Dump the stack traces for all threads to the supplied file, putting
100 * a timestamp header on it.
102 static void logThreadStacks(FILE* fp)
104 DebugOutputTarget target;
106 dvmCreateFileOutputTarget(&target, fp);
108 pid_t pid = getpid();
109 time_t now = time(NULL);
111 #ifdef HAVE_LOCALTIME_R
113 ptm = localtime_r(&now, &tmbuf);
115 ptm = localtime(&now);
117 dvmPrintDebugMessage(&target,
118 "\n\n----- pid %d at %04d-%02d-%02d %02d:%02d:%02d -----\n",
119 pid, ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday,
120 ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
121 printProcessName(&target);
122 dvmPrintDebugMessage(&target, "\n");
123 dvmDumpJniStats(&target);
124 dvmDumpAllThreadsEx(&target, true);
125 fprintf(fp, "----- end %d -----\n", pid);
130 * Respond to a SIGQUIT by dumping the thread stacks. Optionally dump
131 * a few other things while we're at it.
133 * Thread stacks can either go to the log or to a file designated for holding
134 * ANR traces. If we're writing to a file, we want to do it in one shot,
135 * so we can use a single O_APPEND write instead of contending for exclusive
136 * access with flock(). There may be an advantage in resuming the VM
137 * before doing the file write, so we don't stall the VM if disk I/O is
140 * If JIT tuning is compiled in, dump compiler stats as well.
142 static void handleSigQuit()
144 char* traceBuf = NULL;
147 dvmSuspendAllThreads(SUSPEND_FOR_STACK_DUMP);
149 dvmDumpLoaderStats("sig");
151 if (gDvm.stackTraceFile == NULL) {
152 /* just dump to log */
153 DebugOutputTarget target;
154 dvmCreateLogOutputTarget(&target, ANDROID_LOG_INFO, LOG_TAG);
155 dvmDumpJniStats(&target);
156 dvmDumpAllThreadsEx(&target, true);
158 /* write to memory buffer */
159 FILE* memfp = open_memstream(&traceBuf, &traceLen);
161 ALOGE("Unable to create memstream for stack traces");
162 traceBuf = NULL; /* make sure it didn't touch this */
165 logThreadStacks(memfp);
170 #if defined(WITH_JIT) && defined(WITH_JIT_TUNING)
171 dvmCompilerDumpStats();
174 if (false) dvmDumpTrackedAllocations(true);
176 dvmResumeAllThreads(SUSPEND_FOR_STACK_DUMP);
178 if (traceBuf != NULL) {
180 * We don't know how long it will take to do the disk I/O, so put us
181 * into VMWAIT for the duration.
183 ThreadStatus oldStatus = dvmChangeStatus(dvmThreadSelf(), THREAD_VMWAIT);
186 * Open the stack trace output file, creating it if necessary. It
187 * needs to be world-writable so other processes can write to it.
189 int fd = open(gDvm.stackTraceFile, O_WRONLY | O_APPEND | O_CREAT, 0666);
191 ALOGE("Unable to open stack trace file '%s': %s",
192 gDvm.stackTraceFile, strerror(errno));
194 ssize_t actual = TEMP_FAILURE_RETRY(write(fd, traceBuf, traceLen));
195 if (actual != (ssize_t) traceLen) {
196 ALOGE("Failed to write stack traces to %s (%d of %zd): %s",
197 gDvm.stackTraceFile, (int) actual, traceLen,
200 ALOGI("Wrote stack traces to '%s'", gDvm.stackTraceFile);
206 dvmChangeStatus(dvmThreadSelf(), oldStatus);
211 * Respond to a SIGUSR1 by forcing a GC.
213 static void handleSigUsr1()
215 ALOGI("SIGUSR1 forcing GC (no HPROF)");
219 #if defined(WITH_JIT) && defined(WITH_JIT_TUNING)
220 /* Sample callback function for dvmJitScanAllClassPointers */
221 void printAllClass(void *ptr)
223 ClassObject **classPP = (ClassObject **) ptr;
224 ALOGE("class %s", (*classPP)->descriptor);
229 * Respond to a SIGUSR2 by dumping some JIT stats and possibly resetting
232 static void handleSigUsr2()
234 static int codeCacheResetCount = 0;
235 gDvmJit.receivedSIGUSR2 ^= true;
236 if ((--codeCacheResetCount & 7) == 0) {
237 /* Dump all class pointers in the traces */
238 dvmJitScanAllClassPointers(printAllClass);
239 gDvmJit.codeCacheFull = true;
241 dvmCompilerDumpStats();
242 /* Stress-test unchain all */
244 ALOGD("Send %d more signals to reset the code cache",
245 codeCacheResetCount & 7);
247 dvmCheckInterpStateConsistency();
252 * Sleep in sigwait() until a signal arrives.
254 static void* signalCatcherThreadStart(void* arg)
256 Thread* self = dvmThreadSelf();
260 UNUSED_PARAMETER(arg);
262 ALOGV("Signal catcher thread started (threadid=%d)", self->threadId);
264 /* set up mask with signals we want to handle */
266 sigaddset(&mask, SIGQUIT);
267 sigaddset(&mask, SIGUSR1);
268 #if defined(WITH_JIT) && defined(WITH_JIT_TUNING)
269 sigaddset(&mask, SIGUSR2);
275 dvmChangeStatus(self, THREAD_VMWAIT);
278 * Signals for sigwait() must be blocked but not ignored. We
279 * block signals like SIGQUIT for all threads, so the condition
280 * is met. When the signal hits, we wake up, without any signal
281 * handlers being invoked.
283 * When running under GDB we occasionally return from sigwait()
284 * with EINTR (e.g. when other threads exit).
287 cc = sigwait(&mask, &rcvd);
290 //ALOGV("sigwait: EINTR");
293 assert(!"bad result from sigwait");
296 if (!gDvm.haltSignalCatcher) {
297 ALOGI("threadid=%d: reacting to signal %d",
298 dvmThreadSelf()->threadId, rcvd);
301 /* set our status to RUNNING, self-suspending if GC in progress */
302 dvmChangeStatus(self, THREAD_RUNNING);
304 if (gDvm.haltSignalCatcher)
314 #if defined(WITH_JIT) && defined(WITH_JIT_TUNING)
320 ALOGE("unexpected signal %d", rcvd);