Skip to content

Commit df2e2ef

Browse files
Christopher TateAndroid (Google) Code Review
authored andcommitted
Merge "Watchdog now records kernel stacks when it fires" into froyo
2 parents 5474902 + ecaa7b4 commit df2e2ef

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

core/jni/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ LOCAL_SRC_FILES:= \
120120
android_server_BluetoothService.cpp \
121121
android_server_BluetoothEventLoop.cpp \
122122
android_server_BluetoothA2dpService.cpp \
123+
android_server_Watchdog.cpp \
123124
android_message_digest_sha1.cpp \
124125
android_ddm_DdmHandleNativeHeap.cpp \
125126
android_location_GpsLocationProvider.cpp \

core/jni/AndroidRuntime.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ extern int register_android_bluetooth_ScoSocket(JNIEnv *env);
153153
extern int register_android_server_BluetoothService(JNIEnv* env);
154154
extern int register_android_server_BluetoothEventLoop(JNIEnv *env);
155155
extern int register_android_server_BluetoothA2dpService(JNIEnv* env);
156+
extern int register_android_server_Watchdog(JNIEnv* env);
156157
extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
157158
extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
158159
extern int register_android_location_GpsLocationProvider(JNIEnv* env);
@@ -1276,6 +1277,7 @@ static const RegJNIRec gRegJNI[] = {
12761277
REG_JNI(register_android_server_BluetoothService),
12771278
REG_JNI(register_android_server_BluetoothEventLoop),
12781279
REG_JNI(register_android_server_BluetoothA2dpService),
1280+
REG_JNI(register_android_server_Watchdog),
12791281
REG_JNI(register_android_message_digest_sha1),
12801282
REG_JNI(register_android_ddm_DdmHandleNativeHeap),
12811283
REG_JNI(register_android_location_GpsLocationProvider),
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
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+
20+
#include <sys/types.h>
21+
#include <fcntl.h>
22+
#include <dirent.h>
23+
#include <string.h>
24+
#include <errno.h>
25+
26+
#include "jni.h"
27+
#include "JNIHelp.h"
28+
#include <android_runtime/AndroidRuntime.h>
29+
30+
static void dumpOneStack(int tid, int outFd) {
31+
char buf[64];
32+
33+
snprintf(buf, sizeof(buf), "/proc/%d/stack", tid);
34+
int stackFd = open(buf, O_RDONLY);
35+
if (stackFd >= 0) {
36+
// header for readability
37+
strncat(buf, ":\n", sizeof(buf) - strlen(buf) - 1);
38+
write(outFd, buf, strlen(buf));
39+
40+
// copy the stack dump text
41+
int nBytes;
42+
while ((nBytes = read(stackFd, buf, sizeof(buf))) > 0) {
43+
write(outFd, buf, nBytes);
44+
}
45+
46+
// footer and done
47+
write(outFd, "\n", 1);
48+
close(stackFd);
49+
} else {
50+
LOGE("Unable to open stack of tid %d : %d (%s)", tid, errno, strerror(errno));
51+
}
52+
}
53+
54+
static void dumpKernelStacks(JNIEnv* env, jobject clazz, jstring pathStr) {
55+
char buf[128];
56+
DIR* taskdir;
57+
58+
LOGI("dumpKernelStacks");
59+
if (!pathStr) {
60+
jniThrowException(env, "java/lang/IllegalArgumentException", "Null path");
61+
return;
62+
}
63+
64+
const char *path = env->GetStringUTFChars(pathStr, NULL);
65+
66+
int outFd = open(path, O_WRONLY | O_APPEND | O_CREAT);
67+
if (outFd < 0) {
68+
LOGE("Unable to open stack dump file: %d (%s)", errno, strerror(errno));
69+
goto done;
70+
}
71+
72+
snprintf(buf, sizeof(buf), "\n----- begin pid %d kernel stacks -----\n", getpid());
73+
write(outFd, buf, strlen(buf));
74+
75+
// look up the list of all threads in this process
76+
snprintf(buf, sizeof(buf), "/proc/%d/task", getpid());
77+
taskdir = opendir(buf);
78+
if (taskdir != NULL) {
79+
struct dirent * ent;
80+
while ((ent = readdir(taskdir)) != NULL) {
81+
int tid = atoi(ent->d_name);
82+
if (tid > 0 && tid <= 65535) {
83+
// dump each stack trace
84+
dumpOneStack(tid, outFd);
85+
}
86+
}
87+
closedir(taskdir);
88+
}
89+
90+
snprintf(buf, sizeof(buf), "----- end pid %d kernel stacks -----\n", getpid());
91+
write(outFd, buf, strlen(buf));
92+
93+
close(outFd);
94+
done:
95+
env->ReleaseStringUTFChars(pathStr, path);
96+
}
97+
98+
// ----------------------------------------
99+
100+
namespace android {
101+
102+
static const JNINativeMethod g_methods[] = {
103+
{ "native_dumpKernelStacks", "(Ljava/lang/String;)V", (void*)dumpKernelStacks },
104+
};
105+
106+
int register_android_server_Watchdog(JNIEnv* env) {
107+
return AndroidRuntime::registerNativeMethods(env, "com/android/server/Watchdog",
108+
g_methods, NELEM(g_methods));
109+
}
110+
111+
}

services/java/com/android/server/Watchdog.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import android.util.Slog;
4040

4141
import java.io.File;
42+
import java.io.FileInputStream;
43+
import java.io.FileOutputStream;
4244
import java.io.IOException;
4345
import java.util.ArrayList;
4446
import java.util.Calendar;
@@ -51,6 +53,9 @@ public class Watchdog extends Thread {
5153
// Set this to true to use debug default values.
5254
static final boolean DB = false;
5355

56+
// Set this to true to have the watchdog record kernel thread stacks when it fires
57+
static final boolean RECORD_KERNEL_THREADS = true;
58+
5459
static final int MONITOR = 2718;
5560
static final int GLOBAL_PSS = 2719;
5661

@@ -850,6 +855,11 @@ public void run() {
850855
// The system's been hanging for a minute, another second or two won't hurt much.
851856
SystemClock.sleep(2000);
852857

858+
// Pull our own kernel thread stacks as well if we're configured for that
859+
if (RECORD_KERNEL_THREADS) {
860+
dumpKernelStackTraces();
861+
}
862+
853863
mActivity.addErrorToDropBox("watchdog", null, null, null, name, null, stack, null);
854864

855865
// Only kill the process if the debugger is not attached.
@@ -864,4 +874,16 @@ public void run() {
864874
waitedHalf = false;
865875
}
866876
}
877+
878+
private File dumpKernelStackTraces() {
879+
String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
880+
if (tracesPath == null || tracesPath.length() == 0) {
881+
return null;
882+
}
883+
884+
native_dumpKernelStacks(tracesPath);
885+
return new File(tracesPath);
886+
}
887+
888+
private native void native_dumpKernelStacks(String tracesPath);
867889
}

0 commit comments

Comments
 (0)