默認情況下,Android的core dump size是被設(shè)置為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();
當系統(tǒng)屬性persist.debug.trace的值為1時恩急,將rlim_cur設(shè)置為RLIM_INFINITY纪蜒。
2.修改Native進程的core dump size
打開system/core/init/property_service.cpp,添加隨系統(tǒng)屬性“persist.debug.trace”的處理纯续,當檢測到persist.debug.trace被置為1時,通過setrlimit設(shè)置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.設(shè)置coredump文件名稱格式及保存路徑
在system/core/rootdir/init.rc文件中加入,當檢測到系統(tǒng)屬性persist.debug.trace的值為1時逢唤,創(chuàng)建目錄“/data/core”,并在/proc/sys/kernel/core_pattern中寫入coredump的文件名稱格式為“%E.%p.%e”即“路徑.pid.可執(zhí)行程序名”
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.應(yīng)用
在adb shell中通過“setprop persist.debug.trace 1”將persist.debug.trace的值設(shè)置為1魔慷,然后重啟著恩,即可以開啟抓取coredump;在adb shell中通過“setprop persist.debug.trace 0”將persist.debug.trace的值置為0邀摆,關(guān)閉抓取coredump功能。生成的coredump文件將會保存在“/data/core/”目錄下隧熙。