diff --git a/core/src/main/cpp/main/include/native_util.h b/core/src/main/cpp/main/include/native_util.h index c2671e83..753d85ff 100644 --- a/core/src/main/cpp/main/include/native_util.h +++ b/core/src/main/cpp/main/include/native_util.h @@ -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 diff --git a/core/src/main/cpp/main/src/config_manager.cpp b/core/src/main/cpp/main/src/config_manager.cpp index fa6db3e5..3fb6f313 100644 --- a/core/src/main/cpp/main/src/config_manager.cpp +++ b/core/src/main/cpp/main/src/config_manager.cpp @@ -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(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(conf_path)) { fs::create_directories(conf_path); } - auto log_path = GetLogPath(); - if (!path_exists(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); diff --git a/core/src/main/cpp/main/src/config_manager.h b/core/src/main/cpp/main/src/config_manager.h index 25d37ff6..6ec603cf 100644 --- a/core/src/main/cpp/main/src/config_manager.h +++ b/core/src/main/cpp/main/src/config_manager.h @@ -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 GetAppModuleList(const std::string &pkg_name) const; bool NeedUpdateConfig() const { diff --git a/core/src/main/cpp/main/src/context.cpp b/core/src/main/cpp/main/src/context.cpp index b3fff427..75ae277f 100644 --- a/core/src/main/cpp/main/src/context.cpp +++ b/core/src/main/cpp/main/src/context.cpp @@ -2,17 +2,17 @@ #include #include #include -#include -#include -#include -#include -#include +#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 #include #include #include #include -#include +#include "jni/pending_hooks.h" #include #include #include @@ -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); diff --git a/core/src/main/cpp/main/src/jni/art_class_linker.cpp b/core/src/main/cpp/main/src/jni/art_class_linker.cpp index f94a7cd8..359d84bd 100644 --- a/core/src/main/cpp/main/src/jni/art_class_linker.cpp +++ b/core/src/main/cpp/main/src/jni/art_class_linker.cpp @@ -29,7 +29,7 @@ namespace lspd { }; void RegisterArtClassLinker(JNIEnv *env) { - REGISTER_EDXP_NATIVE_METHODS(kClassLinkerClassName.c_str()); + REGISTER_LSP_NATIVE_METHODS(ClassLinker); } } \ No newline at end of file diff --git a/core/src/main/cpp/main/src/jni/art_heap.cpp b/core/src/main/cpp/main/src/jni/art_heap.cpp index 719d8ceb..e39a78d9 100644 --- a/core/src/main/cpp/main/src/jni/art_heap.cpp +++ b/core/src/main/cpp/main/src/jni/art_heap.cpp @@ -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); } } \ No newline at end of file diff --git a/core/src/main/cpp/main/src/jni/config_manager.cpp b/core/src/main/cpp/main/src/jni/config_manager.cpp index fef3684c..7179ebe2 100644 --- a/core/src/main/cpp/main/src/jni/config_manager.cpp +++ b/core/src/main/cpp/main/src/jni/config_manager.cpp @@ -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); } } \ No newline at end of file diff --git a/core/src/main/cpp/main/src/jni/logger.cpp b/core/src/main/cpp/main/src/jni/logger.cpp new file mode 100644 index 00000000..3e16eec7 --- /dev/null +++ b/core/src/main/cpp/main/src/jni/logger.cpp @@ -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 +#include + +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); + } +} diff --git a/core/src/main/cpp/main/src/jni/logger.h b/core/src/main/cpp/main/src/jni/logger.h new file mode 100644 index 00000000..b566b62a --- /dev/null +++ b/core/src/main/cpp/main/src/jni/logger.h @@ -0,0 +1,10 @@ +// +// Created by loves on 2/6/2021. +// +#pragma once + +#include + +namespace lspd { + void RegisterLogger(JNIEnv*); +} diff --git a/core/src/main/cpp/main/src/jni/pending_hooks.cpp b/core/src/main/cpp/main/src/jni/pending_hooks.cpp index 2101463f..4496aedc 100644 --- a/core/src/main/cpp/main/src/jni/pending_hooks.cpp +++ b/core/src/main/cpp/main/src/jni/pending_hooks.cpp @@ -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) { diff --git a/core/src/main/cpp/main/src/jni/resources_hook.cpp b/core/src/main/cpp/main/src/jni/resources_hook.cpp index 6f8ab75f..8565d123 100644 --- a/core/src/main/cpp/main/src/jni/resources_hook.cpp +++ b/core/src/main/cpp/main/src/jni/resources_hook.cpp @@ -1,18 +1,18 @@ #include -#include -#include #include +#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); } } \ No newline at end of file diff --git a/core/src/main/cpp/main/src/jni/yahfa.cpp b/core/src/main/cpp/main/src/jni/yahfa.cpp index ff4ebbb9..74420895 100644 --- a/core/src/main/cpp/main/src/jni/yahfa.cpp +++ b/core/src/main/cpp/main/src/jni/yahfa.cpp @@ -1,9 +1,8 @@ -#include "HookMain.h" -#include -#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); } } \ No newline at end of file diff --git a/core/src/main/java/de/robv/android/xposed/PendingHooks.java b/core/src/main/java/de/robv/android/xposed/PendingHooks.java index a49dfdb5..5328aa94 100644 --- a/core/src/main/java/de/robv/android/xposed/PendingHooks.java +++ b/core/src/main/java/de/robv/android/xposed/PendingHooks.java @@ -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); } diff --git a/core/src/main/java/de/robv/android/xposed/XposedBridge.java b/core/src/main/java/de/robv/android/xposed/XposedBridge.java index fe8cbcb3..023d989a 100644 --- a/core/src/main/java/de/robv/android/xposed/XposedBridge.java +++ b/core/src/main/java/de/robv/android/xposed/XposedBridge.java @@ -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); } /** diff --git a/core/src/main/java/io/github/lsposed/lspd/nativebridge/Logger.java b/core/src/main/java/io/github/lsposed/lspd/nativebridge/Logger.java new file mode 100644 index 00000000..2b78a06f --- /dev/null +++ b/core/src/main/java/io/github/lsposed/lspd/nativebridge/Logger.java @@ -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()); + } +} diff --git a/core/src/main/java/io/github/lsposed/lspd/nativebridge/PendingHooks.java b/core/src/main/java/io/github/lsposed/lspd/nativebridge/PendingHooks.java new file mode 100644 index 00000000..47db896a --- /dev/null +++ b/core/src/main/java/io/github/lsposed/lspd/nativebridge/PendingHooks.java @@ -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); +} diff --git a/core/src/main/java/io/github/lsposed/lspd/core/ResourcesHook.java b/core/src/main/java/io/github/lsposed/lspd/nativebridge/ResourcesHook.java similarity index 78% rename from core/src/main/java/io/github/lsposed/lspd/core/ResourcesHook.java rename to core/src/main/java/io/github/lsposed/lspd/nativebridge/ResourcesHook.java index 8a11312b..02161754 100644 --- a/core/src/main/java/io/github/lsposed/lspd/core/ResourcesHook.java +++ b/core/src/main/java/io/github/lsposed/lspd/nativebridge/ResourcesHook.java @@ -1,4 +1,4 @@ -package io.github.lsposed.lspd.core; +package io.github.lsposed.lspd.nativebridge; public class ResourcesHook { diff --git a/core/src/main/java/io/github/lsposed/lspd/sandhook/config/SandHookProvider.java b/core/src/main/java/io/github/lsposed/lspd/sandhook/config/SandHookProvider.java index e9324d10..d8c7cada 100644 --- a/core/src/main/java/io/github/lsposed/lspd/sandhook/config/SandHookProvider.java +++ b/core/src/main/java/io/github/lsposed/lspd/sandhook/config/SandHookProvider.java @@ -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; diff --git a/core/src/main/java/io/github/lsposed/lspd/yahfa/config/YahfaHookProvider.java b/core/src/main/java/io/github/lsposed/lspd/yahfa/config/YahfaHookProvider.java index 90e8d522..2257f144 100644 --- a/core/src/main/java/io/github/lsposed/lspd/yahfa/config/YahfaHookProvider.java +++ b/core/src/main/java/io/github/lsposed/lspd/yahfa/config/YahfaHookProvider.java @@ -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; diff --git a/core/template_override/post-fs-data.sh b/core/template_override/post-fs-data.sh index 1b0d059b..29481350 100644 --- a/core/template_override/post-fs-data.sh +++ b/core/template_override/post-fs-data.sh @@ -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