默认情况下,Android的core dump size是被设置为0的,所以在进程crash时不会生成coredump。为了抓取coredump,需要开启该功能。下面介绍开启方法。
1.修改Zygote子进程的core dump size
打开art/runtime/native/dalvik_system_ZygoteHooks.cc,加入以下代码:
diff --git a/runtime/native/dalvik_system_ZygoteHooks.cc b/runtime/native/dalvik_system_ZygoteHooks.cc
index 891cdfa..caea8c0 100644
--- a/[runtime/native/dalvik_system_ZygoteHooks.cc]
+++ b/[runtime/native/dalvik_system_ZygoteHooks.cc]
@@ -46,7 +46,9 @@
#if defined(__linux__)
#include <sys/prctl.h>
#endif
-
+#ifdef __ANDROID__
+#include <cutils/properties.h>
+#endif
#include <sys/resource.h>
namespace art {
@@ -78,7 +80,18 @@ static void EnableDebugger() {
#endif
// We don't want core dumps, though, so set the core dump size to 0.
rlimit rl;
+#ifdef __ANDROID__
+ char prop_value[PROPERTY_VALUE_MAX];
+ property_get("persist.debug.trace", prop_value, "0");
+ if (prop_value[0] == '1') {
+ LOG(INFO) << "setting RLIM to infinity for process " << getpid();
+ rl.rlim_cur = RLIM_INFINITY;
+ } else {
+ rl.rlim_cur = 0;
+ }
+#else
rl.rlim_cur = 0;
+#endif
rl.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &rl) == -1) {
PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
当系统属性persist.debug.trace的值为1时,将rlim_cur设置为RLIM_INFINITY。
2.修改Native进程的core dump size
打开system/core/init/property_service.cpp,添加随系统属性“persist.debug.trace”的处理,当检测到persist.debug.trace被置为1时,通过setrlimit设置rlim_cur和rlim_max为RLIM_INFINITY,即不对资源限制。
diff --git a/init/property_service.cpp b/init/property_service.cpp
index 4172ba7..cdc5998 100644
--- a/[init/property_service.cpp]
+++ b/[init/property_service.cpp]
@@ -698,6 +698,20 @@ static void load_override_properties() {
}
}
+static int check_rlim_action() {
+ struct rlimit rl;
+ std::string value = android::base::GetProperty("persist.debug.trace", "");
+
+ if(value == "1") {
+ rl.rlim_cur = RLIM_INFINITY;
+ rl.rlim_max = RLIM_INFINITY;
+ if (setrlimit(RLIMIT_CORE, &rl) < 0) {
+ PLOG(ERROR) << "could not enable core file generation";
+ }
+ }
+ return 0;
+}
+
/* When booting an encrypted system, /data is not mounted when the
* property service is started, so any properties stored there are
* not loaded. Vold triggers init to load these properties once it
@@ -723,6 +737,8 @@ void load_persist_props(void) {
}
persistent_properties_loaded = true;
property_set("ro.persistent_properties.ready", "true");
+ /*check for coredump*/
+ check_rlim_action();
}
void load_recovery_id_prop() {
3.设置coredump文件名称格式及保存路径
在system/core/rootdir/init.rc文件中加入,当检测到系统属性persist.debug.trace的值为1时,创建目录“/data/core”,并在/proc/sys/kernel/core_pattern中写入coredump的文件名称格式为“%E.%p.%e”即“路径.pid.可执行程序名”
diff --git a/rootdir/init.rc b/rootdir/init.rc
index f95d58e..7d0fdfb 100644
--- a/[rootdir/init.rc]
+++ b/[rootdir/init.rc]
@@ -700,6 +700,11 @@ on property:vold.decrypt=trigger_load_persist_props
start logd
start logd-reinit
+# corefile limit
+on property:persist.debug.trace=1
+ mkdir /data/core 0777 root root
+ write /proc/sys/kernel/core_pattern "/data/core/%E.%p.%e"
+
on property:vold.decrypt=trigger_post_fs_data
trigger post-fs-data
trigger zygote-start
4.应用
在adb shell中通过“setprop persist.debug.trace 1”将persist.debug.trace的值设置为1,然后重启,即可以开启抓取coredump;在adb shell中通过“setprop persist.debug.trace 0”将persist.debug.trace的值置为0,关闭抓取coredump功能。生成的coredump文件将会保存在“/data/core/”目录下。
网友评论