Skip to content

Commit

Permalink
Changed initialization a bit, use extra logging tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rovo89 committed Apr 30, 2012
1 parent c63a3c5 commit 0892ac5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
13 changes: 3 additions & 10 deletions app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,9 @@ class AppRuntime : public AndroidRuntime
sp<ProcessState> proc = ProcessState::self();
LOGV("App process: starting thread pool.\n");
proc->startThreadPool();

xposedCallStaticVoidMethod(getJNIEnv(), "onStartedBeforeMain");

AndroidRuntime* ar = AndroidRuntime::getRuntime();
ar->callMain(mClassName, mClass, mArgC, mArgV);

xposedCallStaticVoidMethod(getJNIEnv(), "onStartedAfterMain");

IPCThreadState::self()->stopProcess();
}
Expand All @@ -110,8 +106,6 @@ class AppRuntime : public AndroidRuntime

virtual void onExit(int code)
{
xposedCallStaticVoidMethod(getJNIEnv(), "onExit");

if (mClassName == NULL) {
// if zygote
IPCThreadState::self()->stopProcess();
Expand Down Expand Up @@ -197,17 +191,17 @@ int main(int argc, const char* const argv[])

runtime.mParentDir = parentDir;

addXposedToClasspath(zygote);
keepLoadingXposed = !isXposedDisabled() && addXposedToClasspath(zygote);

if (zygote) {
runtime.start("com.android.internal.os.ZygoteInit",
runtime.start(keepLoadingXposed ? XPOSED_CLASS_DOTS : "com.android.internal.os.ZygoteInit",
startSystemServer ? "start-system-server" : "");
} else if (className) {
// Remainder of args get passed to startup class main()
runtime.mClassName = className;
runtime.mArgC = argc - i;
runtime.mArgV = argv + i;
runtime.start("com.android.internal.os.RuntimeInit",
runtime.start(keepLoadingXposed ? XPOSED_CLASS_DOTS : "com.android.internal.os.RuntimeInit",
application ? "application" : "tool");
} else {
fprintf(stderr, "Error: no class name or --zygote supplied.\n");
Expand All @@ -216,4 +210,3 @@ int main(int argc, const char* const argv[])
return 10;
}
}

44 changes: 21 additions & 23 deletions xposed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Xposed enables "god mode" for developers
*/

#define LOG_TAG "Xposed"

#include "xposed.h"

#include <utils/Log.h>
Expand Down Expand Up @@ -31,25 +33,30 @@ jclass xresourcesClass = NULL;
jmethodID xresourcesTranslateResId = NULL;
jmethodID xresourcesTranslateAttrId = NULL;
std::list<Method> xposedOriginalMethods;
const char* startClassName = NULL;


////////////////////////////////////////////////////////////
// called directoy by app_process
////////////////////////////////////////////////////////////

void addXposedToClasspath(bool zygote) {
bool isXposedDisabled() {
// is the blocker file present?
if (access(XPOSED_LOAD_BLOCKER, F_OK) == 0) {
LOGE("found %s, not loading Xposed\n", XPOSED_LOAD_BLOCKER);
return;
return true;
}
return false;
}

bool addXposedToClasspath(bool zygote) {
LOGI("-----------------\n");
// do we have a new version and are (re)starting zygote? Then load it!
if (zygote && access(XPOSED_JAR_NEWVERSION, R_OK) == 0) {
LOGI("Found new Xposed jar version, activating it\n");
if (rename(XPOSED_JAR_NEWVERSION, XPOSED_JAR) != 0) {
LOGE("could not move %s to %s\n", XPOSED_JAR_NEWVERSION, XPOSED_JAR);
return;
return false;
}
}
if (access(XPOSED_JAR, R_OK) == 0) {
Expand All @@ -61,18 +68,21 @@ void addXposedToClasspath(bool zygote) {
sprintf(classPath, "%s:%s", XPOSED_JAR, oldClassPath);
setenv("CLASSPATH", classPath, 1);
}
keepLoadingXposed = true;
LOGI("Added Xposed (%s) to CLASSPATH.\n", XPOSED_JAR);
return true;
} else {
LOGE("ERROR: could not access Xposed jar '%s'\n", XPOSED_JAR);
return false;
}
}


bool xposedOnVmCreated(JNIEnv* env, const char* loadedClassName) {
bool xposedOnVmCreated(JNIEnv* env, const char* className) {
if (!keepLoadingXposed)
return false;

startClassName = className;

// disable some access checks
char asmReturnTrue[] = { 0x01, 0x20, 0x70, 0x47 };
replaceAsm((void*) &dvmCheckClassAccess, asmReturnTrue, sizeof(asmReturnTrue));
Expand Down Expand Up @@ -130,24 +140,7 @@ bool xposedOnVmCreated(JNIEnv* env, const char* loadedClassName) {
return false;
}

LOGI("Calling onVmCreated in XposedBridge\n");
Method* methodId = (Method*)env->GetStaticMethodID(xposedClass, "onVmCreated", "(Ljava/lang/String;)Z");
if (methodId == NULL) {
LOGE("ERROR: could not find method %s.onVmCreated(String)\n", XPOSED_CLASS);
dvmLogExceptionStackTrace();
env->ExceptionClear();
return false;
}

jboolean result = env->CallStaticBooleanMethod(xposedClass, (jmethodID)methodId, env->NewStringUTF(loadedClassName));
if (env->ExceptionCheck()) {
LOGE("ERROR: uncaught exception in method %s.onVmCreated(String):\n", XPOSED_CLASS);
dvmLogExceptionStackTrace();
env->ExceptionClear();
return false;
}

return result;
return true;
}

void xposedCallStaticVoidMethod(JNIEnv* env, const char* methodName) {
Expand Down Expand Up @@ -458,10 +451,15 @@ static void android_content_res_XResources_rewriteXmlReferencesNative(JNIEnv* en
dvmChangeStatus(self, oldThreadStatus);
}

static jobject de_robv_android_xposed_XposedBridge_getStartClassName(JNIEnv* env, jclass clazz) {
return env->NewStringUTF(startClassName);
}


static const JNINativeMethod xposedMethods[] = {
{"hookMethodNative", "(Ljava/lang/reflect/Member;)V", (void*)de_robv_android_xposed_XposedBridge_hookMethodNative},
{"invokeOriginalMethodNative", "(Ljava/lang/reflect/Member;[Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", (void*)de_robv_android_xposed_XposedBridge_invokeOriginalMethodNative},
{"getStartClassName", "()Ljava/lang/String;", (void*)de_robv_android_xposed_XposedBridge_getStartClassName},
};

static const JNINativeMethod xresourcesMethods[] = {
Expand Down
8 changes: 5 additions & 3 deletions xposed.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ namespace android {
#define XPOSED_JAR_NEWVERSION XPOSED_JAR ".newversion"
#define XPOSED_LOAD_BLOCKER "/data/xposed/disabled"
#define XPOSED_CLASS "de/robv/android/xposed/XposedBridge"
#define XPOSED_CLASS_DOTS "de.robv.android.xposed.XposedBridge"
#define XRESOURCES_CLASS "android/content/res/XResources"
#define XPOSED_VERSION "1.5"
#define XPOSED_VERSION "1.6b1"

extern bool keepLoadingXposed;
typedef std::list<Method>::iterator XposedOriginalMethodsIt;

// called directoy by app_process
void addXposedToClasspath(bool zygote);
bool xposedOnVmCreated(JNIEnv* env, const char* loadedClassName);
bool isXposedDisabled();
bool addXposedToClasspath(bool zygote);
bool xposedOnVmCreated(JNIEnv* env, const char* className);
void xposedCallStaticVoidMethod(JNIEnv* env, const char* methodName);

// handling hooked methods / helpers
Expand Down

0 comments on commit 0892ac5

Please sign in to comment.