Customize logger for efficiency

This commit is contained in:
LoveSy 2021-02-06 13:11:57 +08:00 committed by LoveSy
parent 3f87a49fc7
commit 33f2e15737
20 changed files with 152 additions and 53 deletions

View File

@ -26,8 +26,8 @@ namespace lspd {
CHECK_EQ(JNI_OK, jni_result);
}
#define REGISTER_EDXP_NATIVE_METHODS(class_name) \
RegisterNativeMethodsInternal(env, (class_name), gMethods, arraysize(gMethods))
#define REGISTER_LSP_NATIVE_METHODS(class_name) \
RegisterNativeMethodsInternal(env, "io.github.lsposed.lspd.nativebridge." #class_name, gMethods, arraysize(gMethods))
} // namespace lspd

View File

@ -238,6 +238,23 @@ namespace lspd {
fs::create_directories(GetPrefsPath(""));
fs::permissions(GetPrefsPath(""),
fs::perms::owner_all | fs::perms::group_all | fs::perms::others_exec);
auto log_path = GetLogPath();
auto modules_log_path = GetModulesLogPath();
if (!fs::is_directory(log_path)) {
fs::remove(log_path);
}
if (!path_exists(log_path)) {
fs::create_directories(modules_log_path);
}
if (!path_exists<true>(modules_log_path)) {
std::ofstream(modules_log_path, std::ios::out);
}
fs::permissions(log_path,
fs::perms::owner_all | fs::perms::group_all | fs::perms::others_all);
fs::permissions(modules_log_path,
fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read |
fs::perms::group_write | fs::perms::others_read |
fs::perms::others_write);
} catch (const fs::filesystem_error &e) {
LOGE("init: %s", e.what());
return false;
@ -271,17 +288,10 @@ namespace lspd {
if (!path_exists<true>(conf_path)) {
fs::create_directories(conf_path);
}
auto log_path = GetLogPath();
if (!path_exists<true>(log_path)) {
fs::create_directories(log_path);
}
recursive_permissions(conf_path, fs::perms::owner_all | fs::perms::group_all |
fs::perms::set_gid);
recursive_permissions(log_path, fs::perms::owner_all | fs::perms::group_all |
fs::perms::set_gid);
if (pkg_name == "android") uid = -1;
path_chown(conf_path, uid, 1000u, true);
path_chown(log_path, uid, 1000u, true);
if (current_user_ == 0) {
auto variant = GetVariantPath();
fs::permissions(variant, fs::perms::owner_all | fs::perms::group_all);

View File

@ -67,8 +67,8 @@ namespace lspd {
return base_config_path_ / "conf" / suffix;
}
inline auto GetLogPath(const std::string &suffix = {}) const {
return base_config_path_ / "log" / suffix;
inline static auto GetLogPath(const std::string &suffix = {}) {
return misc_path_ / "log" / suffix;
}
inline const auto &GetBaseConfigPath() const { return base_config_path_; }
@ -85,6 +85,10 @@ namespace lspd {
return "/sys/fs/selinux/enforce";
}
inline static auto GetModulesLogPath() {
return GetLogPath("modules.log");
}
std::vector<std::string> GetAppModuleList(const std::string &pkg_name) const;
bool NeedUpdateConfig() const {

View File

@ -2,17 +2,17 @@
#include <android-base/macros.h>
#include <JNIHelper.h>
#include <android-base/logging.h>
#include <jni/config_manager.h>
#include <jni/art_class_linker.h>
#include <jni/art_heap.h>
#include <jni/yahfa.h>
#include <jni/resources_hook.h>
#include "jni/config_manager.h"
#include "jni/art_class_linker.h"
#include "jni/art_heap.h"
#include "jni/yahfa.h"
#include "jni/resources_hook.h"
#include <dl_util.h>
#include <art/runtime/jni_env_ext.h>
#include <art/runtime/mirror/class.h>
#include <android-base/strings.h>
#include <nativehelper/scoped_local_ref.h>
#include <jni/pending_hooks.h>
#include "jni/pending_hooks.h"
#include <sandhook.h>
#include <fstream>
#include <sstream>
@ -21,6 +21,7 @@
#include "art/runtime/runtime.h"
#include "art/runtime/gc/heap.h"
#include "native_hook.h"
#include "jni/logger.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
@ -113,6 +114,7 @@ namespace lspd {
entry_class_ = (jclass) (env->NewGlobalRef(
FindClassFromLoader(env, GetCurrentClassLoader(), kEntryClassName)));
RegisterLogger(env);
RegisterEdxpResourcesHook(env);
RegisterConfigManagerMethods(env);
RegisterArtClassLinker(env);

View File

@ -29,7 +29,7 @@ namespace lspd {
};
void RegisterArtClassLinker(JNIEnv *env) {
REGISTER_EDXP_NATIVE_METHODS(kClassLinkerClassName.c_str());
REGISTER_LSP_NATIVE_METHODS(ClassLinker);
}
}

View File

@ -20,7 +20,7 @@ namespace lspd {
};
void RegisterArtHeap(JNIEnv *env) {
REGISTER_EDXP_NATIVE_METHODS("io.github.lsposed.lspd.nativebridge.Heap");
REGISTER_LSP_NATIVE_METHODS(Heap);
}
}

View File

@ -83,7 +83,7 @@ namespace lspd {
};
void RegisterConfigManagerMethods(JNIEnv *env) {
REGISTER_EDXP_NATIVE_METHODS("io.github.lsposed.lspd.nativebridge.ConfigManager");
REGISTER_LSP_NATIVE_METHODS(ConfigManager);
}
}

View File

@ -0,0 +1,34 @@
//
// Created by loves on 2/6/2021.
//
#include "logger.h"
#include "nativehelper/jni_macros.h"
#include "native_util.h"
#include "JNIHelper.h"
#include "../config_manager.h"
#include <fstream>
#include <fcntl.h>
namespace lspd {
LSP_DEF_NATIVE_METHOD(void, Logger, nativeLog, jstring jstr) {
static int fd = open(ConfigManager::GetModulesLogPath().c_str(), O_APPEND | O_WRONLY);
if (fd < 0) {
LOGD("Logger fail: %s", strerror(errno));
return;
}
JUTFString str(env, jstr);
int res = write(fd, str.get(), std::strlen(str.get()));
if (res < 0) {
LOGD("Logger fail: %s", strerror(errno));
}
}
static JNINativeMethod gMethods[] = {
LSP_NATIVE_METHOD(Logger, nativeLog, "(Ljava/lang/String;)V")
};
void RegisterLogger(JNIEnv *env) {
REGISTER_LSP_NATIVE_METHODS(Logger);
}
}

View File

@ -0,0 +1,10 @@
//
// Created by loves on 2/6/2021.
//
#pragma once
#include <jni.h>
namespace lspd {
void RegisterLogger(JNIEnv*);
}

View File

@ -45,8 +45,7 @@ namespace lspd {
pending_classes_.erase(clazz);
}
static void
PendingHooks_recordPendingMethodNative(JNI_START, jobject method_ref, jclass class_ref) {
LSP_DEF_NATIVE_METHOD(void, PendingHooks, recordPendingMethodNative, jobject method_ref, jclass class_ref){
auto *class_ptr = art::Thread::Current().DecodeJObject(class_ref);
auto *method = getArtMethodYahfa(env, method_ref);
art::mirror::Class mirror_class(class_ptr);
@ -69,12 +68,12 @@ namespace lspd {
}
static JNINativeMethod gMethods[] = {
NATIVE_METHOD(PendingHooks, recordPendingMethodNative,
LSP_NATIVE_METHOD(PendingHooks, recordPendingMethodNative,
"(Ljava/lang/reflect/Method;Ljava/lang/Class;)V"),
};
void RegisterPendingHooks(JNIEnv *env) {
REGISTER_EDXP_NATIVE_METHODS("de.robv.android.xposed.PendingHooks");
REGISTER_LSP_NATIVE_METHODS(PendingHooks);
}
bool isHooked(void *art_method) {

View File

@ -1,18 +1,18 @@
#include <jni.h>
#include <native_util.h>
#include <nativehelper/jni_macros.h>
#include <resource_hook.h>
#include "native_util.h"
#include "nativehelper/jni_macros.h"
#include "resources_hook.h"
namespace lspd {
static jboolean ResourcesHook_initXResourcesNative(JNI_START) {
LSP_DEF_NATIVE_METHOD(jboolean, ResourcesHook, initXResourcesNative) {
return XposedBridge_initXResourcesNative(env, clazz);
}
// @ApiSensitive(Level.MIDDLE)
static jboolean ResourcesHook_removeFinalFlagNative(JNI_START, jclass target_class) {
LSP_DEF_NATIVE_METHOD(jboolean, ResourcesHook, removeFinalFlagNative, jclass target_class) {
if (target_class) {
jclass class_clazz = JNI_FindClass(env, "java/lang/Class");
jfieldID java_lang_Class_accessFlags = JNI_GetFieldID(
@ -25,12 +25,12 @@ namespace lspd {
}
static JNINativeMethod gMethods[] = {
NATIVE_METHOD(ResourcesHook, initXResourcesNative, "()Z"),
NATIVE_METHOD(ResourcesHook, removeFinalFlagNative, "(Ljava/lang/Class;)Z"),
LSP_NATIVE_METHOD(ResourcesHook, initXResourcesNative, "()Z"),
LSP_NATIVE_METHOD(ResourcesHook, removeFinalFlagNative, "(Ljava/lang/Class;)Z"),
};
void RegisterEdxpResourcesHook(JNIEnv *env) {
REGISTER_EDXP_NATIVE_METHODS("io.github.lsposed.lspd.core.ResourcesHook");
REGISTER_LSP_NATIVE_METHODS(ResourcesHook);
}
}

View File

@ -1,9 +1,8 @@
#include "HookMain.h"
#include <nativehelper/jni_macros.h>
#include "jni.h"
#include "native_util.h"
#include "yahfa.h"
#include "HookMain.h"
#include "nativehelper/jni_macros.h"
#include "native_util.h"
#include "pending_hooks.h"
#include "art/runtime/class_linker.h"
@ -43,7 +42,7 @@ namespace lspd {
};
void RegisterEdxpYahfa(JNIEnv *env) {
REGISTER_EDXP_NATIVE_METHODS("io.github.lsposed.lspd.nativebridge.Yahfa");
REGISTER_LSP_NATIVE_METHODS(Yahfa);
}
}

View File

@ -7,6 +7,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import static de.robv.android.xposed.XposedBridge.hookMethodNative;
import static io.github.lsposed.lspd.nativebridge.PendingHooks.recordPendingMethodNative;
public final class PendingHooks {
@ -42,5 +43,4 @@ public final class PendingHooks {
sPendingHooks.clear();
}
private static native void recordPendingMethodNative(Method hookMethod, Class clazz);
}

View File

@ -31,6 +31,7 @@ import de.robv.android.xposed.callbacks.XC_LoadPackage;
import de.robv.android.xposed.callbacks.XCallback;
import external.com.android.dx.DexMaker;
import external.com.android.dx.TypeId;
import io.github.lsposed.lspd.nativebridge.Logger;
import static de.robv.android.xposed.XposedHelpers.getIntField;
import static de.robv.android.xposed.XposedHelpers.setObjectField;
@ -159,10 +160,8 @@ public final class XposedBridge {
* @param text The log message.
*/
public synchronized static void log(String text) {
if (ConfigManager.isNoModuleLogEnabled()) {
return;
}
Log.i(TAG, text);
Logger.log(text);
}
/**
@ -174,7 +173,9 @@ public final class XposedBridge {
* @param t The Throwable object for the stack trace.
*/
public synchronized static void log(Throwable t) {
Log.e(TAG, Log.getStackTraceString(t));
String logStr = Log.getStackTraceString(t);
Log.e(TAG, logStr);
Logger.log(logStr);
}
/**

View File

@ -0,0 +1,35 @@
package io.github.lsposed.lspd.nativebridge;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.AndroidAppHelper;
import android.os.Process;
public class Logger {
static SimpleDateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss", Locale.getDefault());
public static native void nativeLog(String str);
public static void log(String str) {
StringBuilder sb = new StringBuilder();
sb.append(logDateFormat.format(new Date()));
sb.append(' ');
sb.append(Process.myPid());
sb.append('-');
sb.append(Process.myTid());
sb.append('/');
try {
sb.append((String) Class.forName("android.app.ActivityThread").getDeclaredMethod("currentProcessName").invoke(null));
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) {
sb.append("?");
}
sb.append(' ');
sb.append("LSPosedBridge: ");
sb.append(str);
sb.append('\n');
nativeLog(sb.toString());
}
}

View File

@ -0,0 +1,7 @@
package io.github.lsposed.lspd.nativebridge;
import java.lang.reflect.Method;
public class PendingHooks {
public static native void recordPendingMethodNative(Method hookMethod, Class clazz);
}

View File

@ -1,4 +1,4 @@
package io.github.lsposed.lspd.core;
package io.github.lsposed.lspd.nativebridge;
public class ResourcesHook {

View File

@ -4,7 +4,7 @@ import android.util.Log;
import io.github.lsposed.lspd.nativebridge.ClassLinker;
import io.github.lsposed.lspd.config.BaseHookProvider;
import io.github.lsposed.lspd.core.ResourcesHook;
import io.github.lsposed.lspd.nativebridge.ResourcesHook;
import io.github.lsposed.lspd.nativebridge.Yahfa;
import com.swift.sandhook.xposedcompat.XposedCompat;
import com.swift.sandhook.xposedcompat.methodgen.SandHookXposedBridge;

View File

@ -2,7 +2,7 @@ package io.github.lsposed.lspd.yahfa.config;
import io.github.lsposed.lspd.nativebridge.ClassLinker;
import io.github.lsposed.lspd.config.BaseHookProvider;
import io.github.lsposed.lspd.core.ResourcesHook;
import io.github.lsposed.lspd.nativebridge.ResourcesHook;
import io.github.lsposed.lspd.nativebridge.Yahfa;
import io.github.lsposed.lspd.yahfa.dexmaker.DynamicBridge;

View File

@ -45,9 +45,8 @@ livePatch() {
MISC_PATH=$(cat /data/adb/lspd/misc_path)
BASE_PATH="/data/misc/$MISC_PATH"
LOG_PATH="${BASE_PATH}/0/log"
CONF_PATH="${BASE_PATH}/0/conf"
DISABLE_VERBOSE_LOG_FILE="${CONF_PATH}/disable_verbose_log"
LOG_PATH="${BASE_PATH}/log"
DISABLE_VERBOSE_LOG_FILE="${BASE_PATH}/disable_verbose_log"
LOG_VERBOSE=true
OLD_PATH=${PATH}
PATH=${PATH#*:}
@ -117,12 +116,6 @@ start_log_cather () {
# execute live patch if rule not found
[[ -f "${MODDIR}/sepolicy.rule" ]] || livePatch
# start_verbose_log_catcher
start_log_cather all "LSPosed:V XSharedPreferences:V LSPosed-Bridge:V LSPosedManager:V *:F" true ${LOG_VERBOSE}
# start_bridge_log_catcher
start_log_cather error "XSharedPreferences:V LSPosed-Bridge:V" true true
if [[ -f "/data/adb/riru/modules/lspd.prop" ]]; then
CONFIG=$(cat "/data/adb/riru/modules/lspd.prop")
[[ -d "${TARGET}/${CONFIG}" ]] || mkdir -p "${TARGET}/${CONFIG}"
@ -139,5 +132,10 @@ if [[ ! -z "${MISC_PATH}" ]]; then
chcon -R u:object_r:magisk_file:s0 "${BASE_PATH}"
chmod 771 "${BASE_PATH}"
chmod 777 "${BASE_PATH}/cache"
rm -rf ${LOG_PATH}.old
mv ${LOG_PATH} ${LOG_PATH}.old
mkdir -p ${LOG_PATH}
# start_verbose_log_catcher
start_log_cather all "LSPosed:V XSharedPreferences:V LSPosed-Bridge:V LSPosedManager:V *:F" true ${LOG_VERBOSE}
fi
rm -f /data/adb/lspd/new_install