Skip to content

Commit

Permalink
format thread-leak native code to google style
Browse files Browse the repository at this point in the history
  • Loading branch information
guanchu.shen committed Aug 20, 2021
1 parent 272248a commit 5e3251f
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 264 deletions.
2 changes: 1 addition & 1 deletion koom-thread-leak/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ project(${TARGET})
cmake_minimum_required(VERSION 3.4.1)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include/
${CMAKE_CURRENT_SOURCE_DIR}/src/include/
${KWAI_ANDROID_BASE_DIR}/src/main/cpp/include/
${KWAI_ANDROID_BASE_DIR}/src/main/cpp/liblog/include/
${THIRD_PARTY_DIR}/xhook/src/main/cpp/xhook/src/
Expand Down
56 changes: 28 additions & 28 deletions koom-thread-leak/src/main/cpp/src/common/callstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
*
*/

#include <kwai_linker/kwai_dlfcn.h>
#include <common/callstack.h>

#include <bionic/tls.h>
#include <bionic/tls_defines.h>
#include <dlfcn.h>
#include "callstack.h"
#include "bionic/tls_defines.h"
#include "bionic/tls.h"
#include <kwai_linker/kwai_dlfcn.h>

#include <string>

namespace koom {

const char *callstack_tag = "koom-callstack";

//静态变量初始化
pthread_key_t CallStack::pthread_key_self;
dump_java_stack_above_o_ptr CallStack::dump_java_stack_above_o;
dump_java_stack_ptr CallStack::dump_java_stack;
Expand All @@ -41,34 +43,36 @@ std::atomic<bool> CallStack::inSymbolize;
unwindstack::UnwinderFromPid *CallStack::unwinder;

void CallStack::Init() {

if (koom::Util::AndroidApi() < __ANDROID_API_L__) {
koom::Log::error(callstack_tag, "android api < __ANDROID_API_L__");
return;
}
void *handle = kwai::linker::DlFcn::dlopen("libart.so", RTLD_LAZY | RTLD_LOCAL);
void *handle =
kwai::linker::DlFcn::dlopen("libart.so", RTLD_LAZY | RTLD_LOCAL);
if (koom::Util::AndroidApi() >= __ANDROID_API_O__) {
dump_java_stack_above_o = reinterpret_cast<dump_java_stack_above_o_ptr>(
kwai::linker::DlFcn::dlsym(handle,
"_ZNK3art6Thread13DumpJavaStackERNSt3__113basic_ostreamIcNS1_11char_"
"traitsIcEEEEbb"));
dump_java_stack_above_o = reinterpret_cast<
dump_java_stack_above_o_ptr>(kwai::linker::DlFcn::dlsym(
handle,
"_ZNK3art6Thread13DumpJavaStackERNSt3__113basic_ostreamIcNS1_11char_"
"traitsIcEEEEbb"));
if (dump_java_stack_above_o == nullptr) {
koom::Log::error(callstack_tag, "dump_java_stack_above_o is null");
}
} else if (koom::Util::AndroidApi() >= __ANDROID_API_L__) {
dump_java_stack = reinterpret_cast<dump_java_stack_ptr>(
kwai::linker::DlFcn::dlsym(handle,
"_ZNK3art6Thread13DumpJavaStackERNSt3__113basic_ostreamIcNS1_11char_"
"traitsIcEEEE"));
dump_java_stack = reinterpret_cast<
dump_java_stack_ptr>(kwai::linker::DlFcn::dlsym(
handle,
"_ZNK3art6Thread13DumpJavaStackERNSt3__113basic_ostreamIcNS1_11char_"
"traitsIcEEEE"));
if (dump_java_stack == nullptr) {
koom::Log::error(callstack_tag, "dump_java_stack is null");
}
}

if (koom::Util::AndroidApi() < __ANDROID_API_N__) {
auto *pthread_key_self_art =
(pthread_key_t *) kwai::linker::DlFcn::dlsym(handle,
"_ZN3art6Thread17pthread_key_self_E");
reinterpret_cast<pthread_key_t *>(kwai::linker::DlFcn::dlsym(
handle, "_ZN3art6Thread17pthread_key_self_E"));
if (pthread_key_self_art != nullptr) {
pthread_key_self = reinterpret_cast<pthread_key_t>(*pthread_key_self_art);
} else {
Expand All @@ -95,9 +99,8 @@ void CallStack::JavaStackTrace(void *thread, std::ostream &os) {
os << "no java stack when dumping";
return;
}
if(dumpJavaLock.try_lock()) {
if (dumpJavaLock.try_lock()) {
if (koom::Util::AndroidApi() >= __ANDROID_API_O__) {
//不dump locks,有稳定性问题
dump_java_stack_above_o(thread, os, true, false);
} else if (koom::Util::AndroidApi() >= __ANDROID_API_L__) {
dump_java_stack(thread, os);
Expand All @@ -118,8 +121,9 @@ std::string CallStack::SymbolizePc(uintptr_t pc, int index) {
inSymbolize = true;

if (unwinder == nullptr) {
unwinder = new unwindstack::UnwinderFromPid(koom::Constant::kMaxCallStackDepth,
getpid(), unwindstack::Regs::CurrentArch());
unwinder = new unwindstack::UnwinderFromPid(
koom::Constant::kMaxCallStackDepth, getpid(),
unwindstack::Regs::CurrentArch());
unwinder->Init();
unwinder->SetDisplayBuildID(true);
unwinder->SetRegs(unwindstack::Regs::CreateFromLocal());
Expand All @@ -137,11 +141,7 @@ std::string CallStack::SymbolizePc(uintptr_t pc, int index) {
return format;
}

void CallStack::DisableJava() {
disableJava = true;
}
void CallStack::DisableJava() { disableJava = true; }

void CallStack::DisableNative() {
disableNative = true;
}
}
void CallStack::DisableNative() { disableNative = true; }
} // namespace koom
37 changes: 20 additions & 17 deletions koom-thread-leak/src/main/cpp/src/common/looper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@
*
*/

#include "looper.h"
#include "log.h"
#include <cassert>
#include <android/log.h>
#include <common/log.h>
#include <common/looper.h>
#include <fcntl.h>
#include <jni.h>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

#include <cassert>
#include <cerrno>
#include <climits>
#include <semaphore.h>
#include <android/log.h>
#include <sys/prctl.h>
#include <cstdio>
#include <cstring>

#define TAG "koom-looper"
#define LOGV(...) koom::Log::info(TAG, __VA_ARGS__);
Expand All @@ -44,8 +45,8 @@ struct LooperMessage {
bool quit;
};
void *looper::trampoline(void *p) {
prctl(PR_SET_NAME,"koom-looper");
((looper *) p)->loop();
prctl(PR_SET_NAME, "koom-looper");
reinterpret_cast<looper *>(p)->loop();
return nullptr;
}
looper::looper() {
Expand All @@ -58,7 +59,9 @@ looper::looper() {
}
looper::~looper() {
if (running) {
LOGV("Looper deleted while still running. Some messages will not be processed");
LOGV(
"Looper deleted while still running. Some messages will not be "
"processed");
quit();
}
}
Expand All @@ -68,12 +71,12 @@ void looper::post(int what, void *data, bool flush) {
msg->obj = data;
msg->next = nullptr;
msg->quit = false;
// LOGV("post msg %d build msg finish", msg->what);
// LOGV("post msg %d build msg finish", msg->what);
addMsg(msg, flush);
}
void looper::addMsg(LooperMessage *msg, bool flush) {
sem_wait(&headWriteProtect);
// LOGV("post msg %d start", msg->what);
// LOGV("post msg %d start", msg->what);
LooperMessage *h = head;
if (flush) {
while (h) {
Expand All @@ -90,7 +93,7 @@ void looper::addMsg(LooperMessage *msg, bool flush) {
head = msg;
tail = msg;
}
// LOGV("post msg %d end", msg->what);
// LOGV("post msg %d end", msg->what);
sem_post(&headWriteProtect);
sem_post(&headDataAvailable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@
*
*/

#ifndef APM_CALLSTACK_H
#define APM_CALLSTACK_H
#ifndef KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_CALLSTACK_H_
#define KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_CALLSTACK_H_

#include <ostream>
#include <sstream>
#include "util.h"
#include "constant.h"
#include <unistd.h>
#include <common/constant.h>
#include <common/util.h>
#include <fast_unwind/fast_unwind.h>
#include <unistd.h>
#include <unwindstack/Unwinder.h>

#include <ostream>
#include <sstream>
#include <string>

namespace koom {

using dump_java_stack_above_o_ptr = void (*)(void *, std::ostream &os, bool, bool);
using dump_java_stack_above_o_ptr = void (*)(void *, std::ostream &os, bool,
bool);
using dump_java_stack_ptr = void (*)(void *, std::ostream &os);

class CallStack {

enum Type {
java, native
};
enum Type { java, native };

private:
static dump_java_stack_above_o_ptr dump_java_stack_above_o;
Expand Down Expand Up @@ -67,6 +67,6 @@ class CallStack {
static void *GetCurrentThread();
};

}
} // namespace koom

#endif //APM_CALLSTACK_H
#endif // KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_CALLSTACK_H_
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*
*/

#ifndef APM_RESDETECTOR_CONSTANT_H
#define APM_RESDETECTOR_CONSTANT_H
#ifndef KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_CONSTANT_H_
#define KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_CONSTANT_H_

#include <string>
#include <vector>
Expand All @@ -28,8 +28,8 @@ namespace koom {
namespace Constant {
#define ALWAYS_INLINE __attribute__((always_inline))

const static int kMaxCallStackDepth = 18;
const static int kDlopenSourceInit = 0;
}
}
#endif //APM_RESDETECTOR_CONSTANT_H
static const int kMaxCallStackDepth = 18;
static const int kDlopenSourceInit = 0;
} // namespace Constant
} // namespace koom
#endif // KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_CONSTANT_H_
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*
*/

#ifndef APM_LOG_H
#define APM_LOG_H
#ifndef KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_LOG_H_
#define KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_LOG_H_

#include <stdio.h>
#include <android/log.h>
Expand Down Expand Up @@ -62,6 +62,6 @@ class Log {

static const int kMaxLogLine = 512;
};
}
} // namespace koom

#endif //APM_LOG_H
#endif // KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_LOG_H_
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* Created by shenvsv on 2021.
*
*/
#ifndef KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_LOOPER_H_
#define KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_LOOPER_H_

#include <pthread.h>
#include <semaphore.h>
Expand All @@ -37,4 +39,6 @@ class looper {
sem_t headWriteProtect;
sem_t headDataAvailable;
bool running;
};
};

#endif // KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_LOOPER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,38 @@
*
*/

#ifndef APM_UTIL_H
#define APM_UTIL_H
#ifndef KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_UTIL_H_
#define KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_UTIL_H_

#include <string>
#include <set>
#include "log.h"
#include <vector>
#include <map>
#include <fstream>
#include <streambuf>
#include <common/log.h>
#include <dirent.h>
#include <jni.h>

#include <fstream>
#include <map>
#include <set>
#include <streambuf>
#include <string>
#include <vector>

namespace koom {

class Util {

public:
static int android_api;

static void Init() {
android_api = android_get_device_api_level();
}
static void Init() { android_api = android_get_device_api_level(); }

static int AndroidApi() {
return android_api;
}
static int AndroidApi() { return android_api; }

static timespec CurrentClockTime() {
struct timespec now_time{};
struct timespec now_time {};
clock_gettime(CLOCK_MONOTONIC, &now_time);
return now_time;
}

static long long CurrentTimeNs() {
struct timespec now_time{};
static int64_t CurrentTimeNs() {
struct timespec now_time {};
clock_gettime(CLOCK_MONOTONIC, &now_time);
return now_time.tv_sec * 1000000000LL + now_time.tv_nsec;
}
Expand All @@ -67,10 +63,10 @@ class Util {
prev_pos = ++pos;
}

output.push_back(s.substr(prev_pos, pos - prev_pos)); // Last word
output.push_back(s.substr(prev_pos, pos - prev_pos)); // Last word

return output;
}
};
}
#endif //APM_UTIL_H
} // namespace koom
#endif // KOOM_THREAD_LEAK_SRC_MAIN_CPP_SRC_INCLUDE_COMMON_UTIL_H_
Loading

0 comments on commit 5e3251f

Please sign in to comment.