无效的值将传递给从共享库动态加载的函数
im从Android 4.4/System/lib/libgui.so上的共享库中动态函数
#include <jni.h>
#include <string>
#include <android/log.h>
#include <sys/types.h>
#include <iostream>
#include <link.h>
#include <dlfcn.h>
#define LOGI(...) \
((void)__android_log_print(ANDROID_LOG_INFO, "screencast::", __VA_ARGS__))
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, "screencast::", __VA_ARGS__))
typedef void* (*getBuiltInDisplay_t)(int32_t);
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_screencast_1lib_MainActivity_stringFromJNI(
JNIEnv* env,
jobject) {
LOGI("Loading libgui...");
void *handle = dlopen("/system/lib/libgui.so", RTLD_LAZY);
if (!handle) {
LOGE("Cannot load library: %s", dlerror());
return env->NewStringUTF("error while loading system library");
}
LOGI("Loading symbol getBuiltInDisplay...");
dlerror();
getBuiltInDisplay_t getBuiltInDisplay = (getBuiltInDisplay_t) dlsym(handle, "_ZN7android21SurfaceComposerClient17getBuiltInDisplayEi");
const char *dlsym_error = dlerror();
if (dlsym_error) {
LOGE("Cannot load symbol: %s", dlsym_error);
dlclose(handle);
return env->NewStringUTF("error while loading system function");
}
LOGI("Calling getBuiltInDisplay...");
getBuiltInDisplay(0);
LOGI("Closing library...");
dlclose(handle);
return env->NewStringUTF("success!!");
}
,并在日志中获得此
...
04-26 17:45:12.623 D/dalvikvm( 6659): VFY: replacing opcode 0x20 at 0x000c
04-26 17:45:12.633 I/screencast::( 6659): Loading libgui...
04-26 17:45:12.633 I/screencast::( 6659): Loading symbol getBuiltInDisplay...
04-26 17:45:12.633 I/screencast::( 6659): Calling getBuiltInDisplay...
04-26 17:45:12.633 E/SurfaceFlinger( 175): getDefaultDisplay: id=1076775671 is not a valid default display id
04-26 17:45:12.633 F/libc ( 6659): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 6659 (.screencast_lib)
04-26 17:45:12.693 F/libc ( 6739): Fatal signal 13 (SIGPIPE) at 0x00001a53 (code=0), thread 6739 (sh)
...
功能,我也发现了 getBuiltIndisplay静态方法的实现。
sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
return NULL;
}
return mBuiltinDisplays[id];
}
因此,正如我所理解的,出于某种原因,我传递给函数的0值显示为随机值(对于这种情况,1076775671,但几乎每次都是随机的)
Im getting function dynamically from shared library on android 4.4 /system/lib/libgui.so
#include <jni.h>
#include <string>
#include <android/log.h>
#include <sys/types.h>
#include <iostream>
#include <link.h>
#include <dlfcn.h>
#define LOGI(...) \
((void)__android_log_print(ANDROID_LOG_INFO, "screencast::", __VA_ARGS__))
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, "screencast::", __VA_ARGS__))
typedef void* (*getBuiltInDisplay_t)(int32_t);
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_screencast_1lib_MainActivity_stringFromJNI(
JNIEnv* env,
jobject) {
LOGI("Loading libgui...");
void *handle = dlopen("/system/lib/libgui.so", RTLD_LAZY);
if (!handle) {
LOGE("Cannot load library: %s", dlerror());
return env->NewStringUTF("error while loading system library");
}
LOGI("Loading symbol getBuiltInDisplay...");
dlerror();
getBuiltInDisplay_t getBuiltInDisplay = (getBuiltInDisplay_t) dlsym(handle, "_ZN7android21SurfaceComposerClient17getBuiltInDisplayEi");
const char *dlsym_error = dlerror();
if (dlsym_error) {
LOGE("Cannot load symbol: %s", dlsym_error);
dlclose(handle);
return env->NewStringUTF("error while loading system function");
}
LOGI("Calling getBuiltInDisplay...");
getBuiltInDisplay(0);
LOGI("Closing library...");
dlclose(handle);
return env->NewStringUTF("success!!");
}
and getting this in the logs
...
04-26 17:45:12.623 D/dalvikvm( 6659): VFY: replacing opcode 0x20 at 0x000c
04-26 17:45:12.633 I/screencast::( 6659): Loading libgui...
04-26 17:45:12.633 I/screencast::( 6659): Loading symbol getBuiltInDisplay...
04-26 17:45:12.633 I/screencast::( 6659): Calling getBuiltInDisplay...
04-26 17:45:12.633 E/SurfaceFlinger( 175): getDefaultDisplay: id=1076775671 is not a valid default display id
04-26 17:45:12.633 F/libc ( 6659): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 6659 (.screencast_lib)
04-26 17:45:12.693 F/libc ( 6739): Fatal signal 13 (SIGPIPE) at 0x00001a53 (code=0), thread 6739 (sh)
...
Also i found this implementation of getBuiltInDisplay static method.
sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
return NULL;
}
return mBuiltinDisplays[id];
}
So, as I understand it, for some reason, the 0 value that I pass to the function is shown as a random value (1076775671 for this case, but it's random almost every time)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了。正如@michael所说,这不是静态类方法,需要通过指针。因此,我分配了一些记忆,并将指针作为第一个参数。
而且有效!
I figured it out. As @Michael said, it's not a static class method and needs to be passed a pointer. So I allocate some memory and pass a pointer as the first argument.
And it works!