Skip to content

Commit

Permalink
Optimize code in core and map. Optimize interface API.
Browse files Browse the repository at this point in the history
  • Loading branch information
caikelun committed Apr 10, 2018
1 parent 83efaf4 commit bddf327
Show file tree
Hide file tree
Showing 16 changed files with 951 additions and 705 deletions.
30 changes: 6 additions & 24 deletions libbiz/jni/biz.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <pthread.h>
#include <jni.h>
#include <android/log.h>
#include "xh_core.h"
#include "xhook.h"

static int my_common_log_print(int prio, const char* tag, const char* fmt, ...)
static int my_system_log_print(int prio, const char* tag, const char* fmt, ...)
{
va_list ap;
char buf[1024];
Expand All @@ -14,7 +14,7 @@ static int my_common_log_print(int prio, const char* tag, const char* fmt, ...)
snprintf(buf, sizeof(buf), "[%s] %s", (NULL == tag ? "" : tag), (NULL == fmt ? "" : fmt));

va_start(ap, fmt);
r = __android_log_vprint(prio, "xhook_common", buf, ap);
r = __android_log_vprint(prio, "xhook_system", buf, ap);
va_end(ap);
return r;
}
Expand All @@ -33,30 +33,12 @@ static int my_libtest_log_print(int prio, const char* tag, const char* fmt, ...)
return r;
}

static void *new_thread_func(void *arg)
{
(void)arg;
unsigned int i = 0;

while(1)
{
__android_log_print(ANDROID_LOG_DEBUG, "mybiz", "call directly. %u (unhook)\n", i);
i++;
sleep(1);
}

return NULL;
}

void Java_com_qiyi_biz_NativeHandler_start(JNIEnv* env, jobject obj)
{
(void)env;
(void)obj;

pthread_t tid;
pthread_create(&tid, NULL, &new_thread_func, NULL);

xh_core_hook(NULL, "__android_log_print", my_common_log_print, NULL);
xh_core_hook("libtest.so", "__android_log_print", my_libtest_log_print, NULL);
xh_core_unhook("libbiz.so", "__android_log_print");
xhook_register("^/system/.*\\.so$", "__android_log_print", my_system_log_print, NULL);
xhook_register("^/vendor/.*\\.so$", "__android_log_print", my_system_log_print, NULL);
xhook_register("^/.*/libtest\\.so$", "__android_log_print", my_libtest_log_print, NULL);
}
8 changes: 4 additions & 4 deletions libtest/jni/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ my_log_t my_global_log_ptr = (my_log_t)__android_log_print;
static void *new_thread_func(void *arg)
{
(void)arg;
my_log_t my_local_log_ptr = (my_log_t)__android_log_print;
my_log_t my_local_log_ptr2 = (my_log_t)__android_log_print;
unsigned int i = 0;

while(1)
{
my_log_t my_local_log_ptr2 = (my_log_t)__android_log_print;
my_log_t my_local_log_ptr = (my_log_t)__android_log_print;
__android_log_print(ANDROID_LOG_DEBUG, "mytest", "call directly. %u\n", i);
my_global_log_ptr(ANDROID_LOG_DEBUG, "mytest", "call from global ptr. %u\n", i);
my_local_log_ptr(ANDROID_LOG_DEBUG, "mytest", "call from local ptr. %u (hooking maybe failed)\n", i);
my_local_log_ptr2(ANDROID_LOG_DEBUG, "mytest", "call from local ptr2. %u\n", i);
my_local_log_ptr(ANDROID_LOG_DEBUG, "mytest", "call from local ptr. %u\n", i);
my_local_log_ptr2(ANDROID_LOG_DEBUG, "mytest", "call from local ptr2. %u (definitely failed when compiled with -O0)\n", i);
i++;
sleep(1);
}
Expand Down
4 changes: 2 additions & 2 deletions libxhook/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := xhook
LOCAL_SRC_FILES := xh_core.c \
xh_map.c \
LOCAL_SRC_FILES := xhook.c \
xh_core.c \
xh_elf.c \
xh_jni.c \
xh_log.c \
Expand Down
Loading

0 comments on commit bddf327

Please sign in to comment.