rovo89 / XposedBridge

The Java part of the Xposed framework.
3.91k stars 1.1k forks source link

How to identify caller in hooks #104

Closed mingwayzhang closed 8 years ago

mingwayzhang commented 8 years ago

This is just a question. So I am wondering whether Xposed could provide the mode that it only instrument functions in apk instead of functions in platform code (e.g., /system/framework/telecom.jar).

For instance, getSimOperator is a function I would like to hook. However, in many cases, it is the platform code that invokes this function instead of the apk. I would like to skip those invocations from platform but record those from apk.

The reason for that is that I am doing a lightweight instrumentation and I hope I could just instrument all function from apk (either direct or through reflection from java or jni) to make the instrumentation more efficient.

So, the question really is that "is there a way to force ART to compile compiling Xposed into apk only?". Please let me know. Sorry if it is wrong question. Thanks for any feedback.

rovo89 commented 8 years ago

is there a way to force ART to compile compiling Xposed into apk only?

Xposed isn't compiled into anything. Hooks work in a way that the entrypoint for the hooked method is redirected to Xposed-specific code, which will then call the hooks and the original method. As the entrypoint information exists only once per method, it's not possible to differentiate between callers. One could try to let Xposed do as little as possible under certain circumstances, but there isn't really that much that could be optimized away. You could obviously try to lock at the stack trace, but doing that for each method call can also be expensive.

Just to make that clear: You should place all your hooks in handleLoadPackage() for a specific package, not in initZygote() if you want to look only at a certain APK.

You could consider building your own version of ART with added instrumentation coding, depending on how deep you want to dive.

lilicoding commented 7 years ago

Hi all,

Interesting discussion here. I am very new to Xposed and would also like to get the caller method of a hooked method. For example, for the simple snippet shown below, if the invoke method is hooked, can I somehow get to know the caller method (i.e., testXYZ).

public void testXYZ(Method m) {
    m.invoke();
}

Actually, expensiveness is not a problem for me as I only need to target a specific APK, within handleLoadPackage() method of course. @rovo89 has mentioned this: "You could obviously try to lock at the stack trace, but doing that for each method call can also be expensive.", is it possible to provide an example code showing how to achieve that? Again, it would be also very much appreciated if this functionality can be integrated into the XposedBridge framework.

rovo89 commented 7 years ago

See: https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java I do have some plans for adding stack analysis in the future, but those plans are currently on hold due to other priorities.