简体   繁体   English

是否可以在不分叉 Android 的进程的情况下捕获退出代码?

[英]Is it possible to capture the exit code without forking the process on Android?

We are writing an Android application, with mixed C and Java parts, mainly used to contain and control an external Java VM. We are writing an Android application, with mixed C and Java parts, mainly used to contain and control an external Java VM. And we need to somehow catch the moment when the VM decides to call exit() without modifying the VM or forking the process (because we also need some way to acess an OpenGL ES context).我们需要以某种方式捕捉 VM 决定在不修改 VM 或分叉进程的情况下调用 exit() 的时刻(因为我们还需要某种方式来访问 OpenGL ES 上下文)。 atexit call can't give us a response with the error code, which is not good. atexit 调用不能给我们一个错误代码的响应,这是不好的。 Is there any other way (maybe bionic-exclusive) to accomplish this?有没有其他方法(可能是仿生独有的)来实现这一点?

You could hook libc's exit() function to call yours instead (assuming exit() and not another variant is called).您可以挂钩 libc 的 exit() function 来调用您的(假设 exit() 而不是调用另一个变体)。

Here is an example using funchook :这是一个使用funchook的示例:

#include <funchook.h>
#include <stdio.h>
#include <stdlib.h>

void exit(int status);
static void (*exit_func)(int status);

static void exit_hook(int status) {
    printf("status is %d\n", status);

    exit_func(status);
}

int main() {
    funchook_t *funchook = funchook_create();

    exit_func = exit;
    // Check for errors
    funchook_prepare(funchook, (void**)&exit_func, exit_hook);
    funchook_install(funchook, 0);

    return 123;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio 模拟器和“进程已完成,退出代码为 0” - Android Studio Emulator and “Process finished with exit code 0” Android Emulator:流程结束,退出代码为-529697949 - Android Emulator: Process finished with exit code -529697949 用户退出应用程序/终止进程时,是否可以在android中执行脚本? - Is it possible to execute a script in android when the user exit the app / kill the process? Android Studio模拟器:流程结束,退出代码为139 Android Studio - Android Studio Emulator: Process finished with exit code 139 Android Studio Android Studio 3.0.1显示模拟器:进程已完成,退出代码为1 - Android Studio 3.0.1 Shows Emulator: Process finished with exit code 1 Android studio 无法运行模拟器:进程已完成,退出代码为 2 - Android studio unable to run emulator: Process finished with exit code 2 Android Studio AVD - 模拟器:进程已完成,退出代码为 1 - Android Studio AVD - Emulator: Process finished with exit code 1 Android Studio 调试器进程完成,退出代码为 127 - Android Studio Debugger process finished with exit code 127 Android模拟器:进程完成,退出代码为-1073741819(0xC0000005) - Android Emulator: Process finished with exit code -1073741819 (0xC0000005) Android模拟器错误“模拟器:进程以退出代码1完成” - Android Emulator Error “Emulator: Process finished with exit code 1”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM