blob: 01d565b26ddb7a4f25c0310761627d511a1b5a17 [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"
Steven Moreland2279b252017-07-19 09:50:45 -070028#include <nativehelper/JNIHelp.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080029#include "core_jni_helpers.h"
Christopher Tateecaa7b42010-06-04 14:55:02 -070030
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 {
Steve Block3762c312012-01-06 19:20:56 +000051 ALOGE("Unable to open stack of tid %d : %d (%s)", tid, errno, strerror(errno));
Christopher Tateecaa7b42010-06-04 14:55:02 -070052 }
53}
54
55static void dumpKernelStacks(JNIEnv* env, jobject clazz, jstring pathStr) {
56 char buf[128];
57 DIR* taskdir;
58
Steve Block6215d3f2012-01-04 20:05:49 +000059 ALOGI("dumpKernelStacks");
Christopher Tateecaa7b42010-06-04 14:55:02 -070060 if (!pathStr) {
61 jniThrowException(env, "java/lang/IllegalArgumentException", "Null path");
62 return;
63 }
64
65 const char *path = env->GetStringUTFChars(pathStr, NULL);
66
Marco Nelissen19399f42011-05-09 14:06:57 -070067 int outFd = open(path, O_WRONLY | O_APPEND | O_CREAT,
68 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
Christopher Tateecaa7b42010-06-04 14:55:02 -070069 if (outFd < 0) {
Steve Block3762c312012-01-06 19:20:56 +000070 ALOGE("Unable to open stack dump file: %d (%s)", errno, strerror(errno));
Christopher Tateecaa7b42010-06-04 14:55:02 -070071 goto done;
72 }
73
74 snprintf(buf, sizeof(buf), "\n----- begin pid %d kernel stacks -----\n", getpid());
75 write(outFd, buf, strlen(buf));
76
77 // look up the list of all threads in this process
78 snprintf(buf, sizeof(buf), "/proc/%d/task", getpid());
79 taskdir = opendir(buf);
80 if (taskdir != NULL) {
81 struct dirent * ent;
82 while ((ent = readdir(taskdir)) != NULL) {
83 int tid = atoi(ent->d_name);
84 if (tid > 0 && tid <= 65535) {
85 // dump each stack trace
86 dumpOneStack(tid, outFd);
87 }
88 }
89 closedir(taskdir);
90 }
91
92 snprintf(buf, sizeof(buf), "----- end pid %d kernel stacks -----\n", getpid());
93 write(outFd, buf, strlen(buf));
94
95 close(outFd);
96done:
97 env->ReleaseStringUTFChars(pathStr, path);
98}
99
100// ----------------------------------------
101
102namespace android {
103
104static const JNINativeMethod g_methods[] = {
105 { "native_dumpKernelStacks", "(Ljava/lang/String;)V", (void*)dumpKernelStacks },
106};
107
108int register_android_server_Watchdog(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800109 return RegisterMethodsOrDie(env, "com/android/server/Watchdog", g_methods, NELEM(g_methods));
Christopher Tateecaa7b42010-06-04 14:55:02 -0700110}
111
112}