Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix一些问题 #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 61 additions & 33 deletions java-oom/src/main/cpp/hprof_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,24 +434,31 @@ int processHeap(const void *buf, int firstIndex, int maxLen, int heapSerialNo, i

//裁剪掉基本类型数组,无论是否在system space都进行裁剪
//区别是数组左坐标,app space时带数组元信息(类型、长度)方便回填
if (isCurrentSystemHeap) {
stripIndexListPair[stripIndex * 2] = firstIndex;
} else {
stripIndexListPair[stripIndex * 2] =
primitiveArrayDumpIndex + BASIC_TYPE_BYTE_SIZE /*value type*/;
}
arraySerialNo++;
// if (isCurrentSystemHeap) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方为什么要删掉?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

裁剪模式会导致分析的时候出现问题,并且也不需要这块的分析

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是为了裁剪掉更多的heap,如果注释掉了,会导致裁剪的内容变少。具体出现的问题是什么?

// stripIndexListPair[stripIndex * 2] = firstIndex;
// } else {
// stripIndexListPair[stripIndex * 2] =
// primitiveArrayDumpIndex + BASIC_TYPE_BYTE_SIZE /*value type*/;
// }
// arraySerialNo++;
//
// int valueSize = getByteSizeFromType(((unsigned char *)buf)[primitiveArrayDumpIndex]);
// primitiveArrayDumpIndex += BASIC_TYPE_BYTE_SIZE /*value type*/ + valueSize * length;
//
// //数组右坐标
// stripIndexListPair[stripIndex * 2 + 1] = primitiveArrayDumpIndex;
//
// // app space时,不修改长度因为回填数组时会补齐
// if (isCurrentSystemHeap) {
// stripBytesSum += primitiveArrayDumpIndex - firstIndex;
// }

stripIndexListPair[stripIndex * 2] = firstIndex;
int valueSize = getByteSizeFromType(((unsigned char *)buf)[primitiveArrayDumpIndex]);
primitiveArrayDumpIndex += BASIC_TYPE_BYTE_SIZE /*value type*/ + valueSize * length;

//数组右坐标
stripIndexListPair[stripIndex * 2 + 1] = primitiveArrayDumpIndex;
stripBytesSum += primitiveArrayDumpIndex - firstIndex;

// app space时,不修改长度因为回填数组时会补齐
if (isCurrentSystemHeap) {
stripBytesSum += primitiveArrayDumpIndex - firstIndex;
}
stripIndex++;

arraySerialNo = processHeap(buf, primitiveArrayDumpIndex, maxLen, heapSerialNo, arraySerialNo);
Expand Down Expand Up @@ -594,7 +601,8 @@ extern "C" {
#endif

JNIEXPORT void JNICALL
Java_com_kwai_koom_javaoom_dump_StripHprofHeapDumper_initStripDump(JNIEnv *env, jobject jObject) {
Java_com_kwai_koom_javaoom_dump_StripHprofHeapDumper_initStripDump(
JNIEnv *env, jobject jObject, int sdk_version) {
hprofFd = -1;
hprofName = nullptr;
isDumpHookSucc = false;
Expand All @@ -611,14 +619,20 @@ Java_com_kwai_koom_javaoom_dump_StripHprofHeapDumper_initStripDump(JNIEnv *env,
* android 7-10版本,open方法都在libart.so中
* libbase.so与libartbase.so,为保险操作
*/
xhook_register("libart.so", "open", (void *)hook_open, nullptr);
xhook_register("libbase.so", "open", (void *)hook_open, nullptr);
xhook_register("libartbase.so", "open", (void *)hook_open, nullptr);

xhook_register("libc.so", "write", (void *)hook_write, nullptr);
xhook_register("libart.so", "write", (void *)hook_write, nullptr);
xhook_register("libbase.so", "write", (void *)hook_write, nullptr);
xhook_register("libartbase.so", "write", (void *)hook_write, nullptr);
if (sdk_version == 30) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要特判30呢,这两个函数原本不是也hook了吗

Copy link
Author

@liminwanqing liminwanqing Jan 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

30 不需要hook其他的,只要hook 2个就可以了,(我们还有其他的hook尽量少吧,也可以不判断,对程序结果不影响)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果这里特判了,其他版本感觉也要特判。但是可能会导致有些不遵守规范的rom版本用不了有问题,比如是6.0的android版本,但是实际上是5.0的虚拟机so

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好其他也判断下,我们这边5.0的没上这个

xhook_register("libart.so", "open", (void *)hook_open, nullptr);
xhook_register("libc.so", "write", (void *)hook_write, nullptr);
} else {
xhook_register("libart.so", "open", (void *)hook_open, nullptr);
xhook_register("libbase.so", "open", (void *)hook_open, nullptr);
xhook_register("libartbase.so", "open", (void *)hook_open, nullptr);

xhook_register("libc.so", "write", (void *)hook_write, nullptr);
xhook_register("libart.so", "write", (void *)hook_write, nullptr);
xhook_register("libbase.so", "write", (void *)hook_write, nullptr);
xhook_register("libartbase.so", "write", (void *)hook_write, nullptr);
}

xhook_refresh(0);
xhook_clear();
Expand All @@ -638,10 +652,12 @@ static void initDumpHprofSymbols();
static pthread_once_t once_control = PTHREAD_ONCE_INIT;

JNIEXPORT void JNICALL
Java_com_kwai_koom_javaoom_dump_ForkJvmHeapDumper_initForkDump(JNIEnv *env, jobject jObject) {
if (!initForkVMSymbols()) {
// Above android 11
Java_com_kwai_koom_javaoom_dump_ForkJvmHeapDumper_initForkDump(
JNIEnv *env, jobject jObject, jint sdk_version) {
if (sdk_version == 30) {
pthread_once(&once_control, initDumpHprofSymbols);
} else {
initForkVMSymbols();
}
}

Expand Down Expand Up @@ -709,8 +725,6 @@ JNIEXPORT void JNICALL Java_com_kwai_koom_javaoom_dump_ForkJvmHeapDumper_waitPid
__asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__val)); \
__val; \
})
#else
#error unsupported architecture
#endif

// What caused the GC?
Expand Down Expand Up @@ -881,16 +895,36 @@ JNIEXPORT jboolean JNICALL Java_com_kwai_koom_javaoom_dump_ForkJvmHeapDumper_dum
HprofConstructor == nullptr || HprofDestructor == nullptr || Dump == nullptr) {
return JNI_FALSE;
}
#ifdef defined(__arm__) || defined(__aarch64__)
ScopedGCCriticalSectionConstructor(gSGCSHandle, __get_tls()[TLS_SLOT_ART_THREAD_SELF],
kGcCauseHprof, kCollectorTypeHprof);
#else
static pthread_key_t pthread_key_self_;
void* thread = pthread_getspecific(pthread_key_self_);
ScopedGCCriticalSectionConstructor(gSGCSHandle, thread, kGcCauseHprof, kCollectorTypeHprof);
#endif
ScopedSuspendAllConstructor(gSSAHandle, LOG_TAG, true);
pid_t pid = fork();
if (pid == -1) {
// Fork error.
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "failed to fork!");
return JNI_FALSE;
}
if (pid != 0) {
if (pid == 0) {
if (nullptr == hprofName) {
const char *filename = env->GetStringUTFChars(file_name, nullptr);
HprofConstructor(gHprofHandle, filename, -1, false);
Dump(gHprofHandle);
HprofDestructor(gHprofHandle);
env->ReleaseStringUTFChars(file_name, filename);
} else {
HprofConstructor(gHprofHandle, hprofName, -1, false);
Dump(gHprofHandle);
HprofDestructor(gHprofHandle);
}
isDumpHookSucc = true;
_exit(0);
} else {
// Parent
ScopedGCCriticalSectionDestructor(gSGCSHandle);
ScopedSuspendAllDestructor(gSSAHandle);
Expand All @@ -901,14 +935,8 @@ JNIEXPORT jboolean JNICALL Java_com_kwai_koom_javaoom_dump_ForkJvmHeapDumper_dum
break;
}
}
return JNI_TRUE;
}

const char *filename = env->GetStringUTFChars(file_name, nullptr);
HprofConstructor(gHprofHandle, filename, -1, false);
Dump(gHprofHandle);
HprofDestructor(gHprofHandle);
env->ReleaseStringUTFChars(file_name, filename);
return JNI_TRUE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.kwai.koom.javaoom.common.KGlobalConfig;
import com.kwai.koom.javaoom.common.KLog;

import java.io.File;

/**
* Copyright 2020 Kwai, Inc. All rights reserved.
Expand Down Expand Up @@ -38,7 +39,7 @@ public class ForkJvmHeapDumper implements HeapDumper {
public ForkJvmHeapDumper() {
soLoaded = KGlobalConfig.getSoLoader().loadLib("koom-java");
if (soLoaded) {
initForkDump();
initForkDump(Build.VERSION.SDK_INT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用传参,native也能判断

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,这个可以修改下

}
}

Expand All @@ -60,6 +61,19 @@ public boolean dump(String path) {
return false;
}

//modify file permission, adapt to some rom
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里能说下具体哪些rom有哪些不兼容的现象吗

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meizu 16T 和 oppo r11 2台手机我们这边都不行

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有什么解决方案吗小老哥

File file = new File(path);
if (file.exists()) {
file.setReadable(true, false);
} else {
try {
file.createNewFile();
file.setReadable(true, false);
} catch (IOException e) {
e.printStackTrace();
}
}

// Compatible with Android 11
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
return dumpHprofDataNative(path);
Expand Down Expand Up @@ -96,7 +110,7 @@ private boolean waitDumping(int pid) {
*
* @return init result
*/
private native void initForkDump();
private native void initForkDump(int sdk_version);

/**
* First do suspend vm, then do fork.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;

import android.os.Build;
import android.os.Debug;
import android.util.Log;

Expand Down Expand Up @@ -38,7 +39,7 @@ public class StripHprofHeapDumper implements HeapDumper {
public StripHprofHeapDumper() {
soLoaded = KGlobalConfig.getSoLoader().loadLib("koom-java");
if (soLoaded) {
initStripDump();
initStripDump(Build.VERSION.SDK_INT);
}
}

Expand Down Expand Up @@ -67,7 +68,11 @@ public boolean dump(String path) {
return dumpRes;
}

public native void initStripDump();
public void hprofNameNative(String name) {
hprofName(name);
}

public native void initStripDump(int sdk_version);

public native void hprofName(String name);

Expand Down
2 changes: 1 addition & 1 deletion kwai-linker/src/main/cpp/kwai_dlfcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ KWAI_EXPORT void *DlFcn::dlopen(const char *lib_name, int flags) {
return ::dlopen(lib_name, flags);
}
if (android_api_ > __ANDROID_API_N__) {
void *handle = ::dlopen("libdl.so", RTLD_NOW);
void *handle = ::dlopen("libdl.so", flags);
CHECKP(handle)
auto __loader_dlopen = reinterpret_cast<__loader_dlopen_fn>(::dlsym(handle, "__loader_dlopen"));
CHECKP(__loader_dlopen)
Expand Down