blob: 7fa7cc79b4f6a4c7ed673e83dc57af9d22d60e27 [file] [log] [blame]
Christopher Tateecaa7b42010-06-04 14:55:02 -07001/*
2 ** Copyright 2010, 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 LOG_TAG "Watchdog_N"
18#include <utils/Log.h>
19
Marco Nelissend8689132011-05-09 12:46:56 -070020#include <sys/stat.h>
Christopher Tateecaa7b42010-06-04 14:55:02 -070021#include <sys/types.h>
22#include <fcntl.h>
23#include <dirent.h>
24#include <string.h>
25#include <errno.h>
26
27#include "jni.h"
28#include "JNIHelp.h"
29#include <android_runtime/AndroidRuntime.h>
30
31static void dumpOneStack(int tid, int outFd) {
32 char buf[64];
33
34 snprintf(buf, sizeof(buf), "/proc/%d/stack", tid);
35 int stackFd = open(buf, O_RDONLY);
36 if (stackFd >= 0) {
37 // header for readability
38 strncat(buf, ":\n", sizeof(buf) - strlen(buf) - 1);
39 write(outFd, buf, strlen(buf));
40
41 // copy the stack dump text
42 int nBytes;
43 while ((nBytes = read(stackFd, buf, sizeof(buf))) > 0) {
44 write(outFd, buf, nBytes);
45 }
46
47 // footer and done
48 write(outFd, "\n", 1);
49 close(stackFd);
50 } else {
51 LOGE("Unable to open stack of tid %d : %d (%s)", tid, errno, strerror(errno));
52 }
53}
54
55static void dumpKernelStacks(JNIEnv* env, jobject clazz, jstring pathStr) {
56 char buf[128];
57 DIR* taskdir;
58
59 LOGI("dumpKernelStacks");
60 if (!pathStr) {
61 jniThrowException(env, "java/lang/IllegalArgumentException", "Null path");
62 return;
63 }
64
65 const char *path = env->GetStringUTFChars(pathStr, NULL);
66
Marco Nelissend8689132011-05-09 12:46:56 -070067 int outFd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IWUSR);
Christopher Tateecaa7b42010-06-04 14:55:02 -070068 if (outFd < 0) {
69 LOGE("Unable to open stack dump file: %d (%s)", errno, strerror(errno));
70 goto done;
71 }
72
73 snprintf(buf, sizeof(buf), "\n----- begin pid %d kernel stacks -----\n", getpid());
74 write(outFd, buf, strlen(buf));
75
76 // look up the list of all threads in this process
77 snprintf(buf, sizeof(buf), "/proc/%d/task", getpid());
78 taskdir = opendir(buf);
79 if (taskdir != NULL) {
80 struct dirent * ent;
81 while ((ent = readdir(taskdir)) != NULL) {
82 int tid = atoi(ent->d_name);
83 if (tid > 0 && tid <= 65535) {
84 // dump each stack trace
85 dumpOneStack(tid, outFd);
86 }
87 }
88 closedir(taskdir);
89 }
90
91 snprintf(buf, sizeof(buf), "----- end pid %d kernel stacks -----\n", getpid());
92 write(outFd, buf, strlen(buf));
93
94 close(outFd);
95done:
96 env->ReleaseStringUTFChars(pathStr, path);
97}
98
99// ----------------------------------------
100
101namespace android {
102
103static const JNINativeMethod g_methods[] = {
104 { "native_dumpKernelStacks", "(Ljava/lang/String;)V", (void*)dumpKernelStacks },
105};
106
107int register_android_server_Watchdog(JNIEnv* env) {
108 return AndroidRuntime::registerNativeMethods(env, "com/android/server/Watchdog",
109 g_methods, NELEM(g_methods));
110}
111
112}