首页 > 其他分享 >Android 11 SystemServer启动流程

Android 11 SystemServer启动流程

时间:2024-03-15 12:31:03浏览次数:26  
标签:11 Runnable ex RuntimeInit new Android main android SystemServer

Android 11 Zygote启动流程有提到,Zygote通过forkSystemServer,fork出SystemServer进程,并在SystemServer进程中调用handleSystemServerProcess 返回一个 Runnable

	//......
		/* For child process */
        if (pid == 0) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }
            zygoteServer.closeServerSocket();
            //这里返回一个Runnable ,然后在ZygoteInit的main函数中调用此Runnable 的run方法,启动systemserver
            return handleSystemServerProcess(parsedArgs);
        }

然后调用Runnable 的run方法

	//......
		if (startSystemServer) {
                Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);

                // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
                // child (system_server) process.
                if (r != null) {
                    r.run();
                    return;
                }
            }
	//......

那就由handleSystemServerProcess开始分析,看看返回的Runnable是什么类型

/*env |grep SYSTEMSERVERCLASSPATH
SYSTEMSERVERCLASSPATH=/system/framework/com.android.location.provider.jar:/system/framework/services.jar:/system/framework/ethernet-service.jar:/apex/com.android.permission/javalib/service-permission.jar:/apex/com.android.ipsec/javalib/android.net.ipsec.ike.jar*/
private static Runnable handleSystemServerProcess(ZygoteArguments parsedArgs) {
       //......
        final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH");
        
        if (systemServerClasspath != null) {
        	
            performSystemServerDexOpt(systemServerClasspath);//做odex
            
            //......
        }
	if (parsedArgs.mInvokeWith != null) {
	
            //......

            throw new IllegalStateException("Unexpected return from WrapperInit.execApplication");
        } else {
            ClassLoader cl = null;
            if (systemServerClasspath != null) {
                cl = createPathClassLoader(systemServerClasspath, parsedArgs.mTargetSdkVersion);

                Thread.currentThread().setContextClassLoader(cl);
            }

            /*
             * Pass the remaining arguments to SystemServer.
             */
            return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
                    parsedArgs.mDisabledCompatChanges,
                    parsedArgs.mRemainingArgs, cl);
        }

先对相关文件夹下的包做odex,然后创建ClassLoader,执行zygoteInit方法

public static final Runnable zygoteInit(int targetSdkVersion, long[] disabledCompatChanges,
            String[] argv, ClassLoader classLoader) {
        if (RuntimeInit.DEBUG) {
            Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");
        }

        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
        RuntimeInit.redirectLogStreams();

        RuntimeInit.commonInit(); //主要是设置默认的异常捕获方法
        ZygoteInit.nativeZygoteInit();//开启binder线程池
        return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,
                classLoader);
    }

在zygoteInit方法中,主演完成三个工作:

  1. 调用RuntimeInit的commonInit方法
  2. 调用ZygoteInit的nativeZygoteInit方法
  3. 调用RuntimeInit的applicationInit方法

RuntimeInit.commonInit

@UnsupportedAppUsage
    protected static final void commonInit() {
        if (DEBUG) Slog.d(TAG, "Entered RuntimeInit!");

        /*
         * set handlers; these apply to all threads in the VM. Apps can replace
         * the default handler, but not the pre handler.
         */
        LoggingHandler loggingHandler = new LoggingHandler();
        RuntimeHooks.setUncaughtExceptionPreHandler(loggingHandler);
        Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler));

	//......

通过调用setDefaultUncaughtExceptionHandler,设置线程默认的异常捕获方法

ZygoteInit.nativeZygoteInit
nativeZygoteInit对应的JNI函数为com_android_internal_os_ZygoteInit_nativeZygoteInit

//frameworks\base\core\jni\AndroidRuntime.cpp
int register_com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env)
{
    const JNINativeMethod methods[] = {
        { "nativeZygoteInit", "()V",
            (void*) com_android_internal_os_ZygoteInit_nativeZygoteInit },
    };
    return jniRegisterNativeMethods(env, "com/android/internal/os/ZygoteInit",
        methods, NELEM(methods));
}
static void com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
{
    gCurRuntime->onZygoteInit();
}

调用gCurRuntime的onZygoteInit函数,gCurRuntime是AppRuntime对象(AppRuntime继承自AndroidRuntime,在app_main.cpp的main函数中初始化),在onZygoteInit 函数中开启线程池

virtual void onZygoteInit()
    {
        sp<ProcessState> proc = ProcessState::self();
        ALOGV("App process: starting thread pool.\n");
        proc->startThreadPool();
    }

RuntimeInit.applicationInit

protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,
            String[] argv, ClassLoader classLoader) {
        //......

        final Arguments args = new Arguments(argv);

        // The end of of the RuntimeInit event (see #zygoteInit).
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

        // Remaining arguments are passed to the start class's static main
        return findStaticMain(args.startClass, args.startArgs, classLoader);
    }

通过findStaticMain查找 startClass中的main函数。追溯函数调用,这个startClass是 com.android.server.SystemServer

protected static Runnable findStaticMain(String className, String[] argv,
            ClassLoader classLoader) {
        Class<?> cl;

        try {
            cl = Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    "Missing class when invoking static main " + className,
                    ex);
        }

        Method m;
        try {
            m = cl.getMethod("main", new Class[] { String[].class });//查找main方法
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(
                    "Missing static main on " + className, ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(
                    "Problem getting static main on " + className, ex);
        }

      	//......
        return new MethodAndArgsCaller(m, argv);
    }

封装成MethodAndArgsCaller 对象返回。所以前面提的返回的Runnable是什么类型,这里就知道了,是MethodAndArgsCaller类型

public MethodAndArgsCaller(Method method, String[] args) {
            mMethod = method;
            mArgs = args;
        }

调用 Runnable的run方法即调用MethodAndArgsCaller的run方法

public void run() {
            try {
                mMethod.invoke(null, new Object[] { mArgs });
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            } catch (InvocationTargetException ex) {
                Throwable cause = ex.getCause();
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                } else if (cause instanceof Error) {
                    throw (Error) cause;
                }
                throw new RuntimeException(ex);
            }
        }

然后就执行到了 com.android.server.SystemServer的 main方法

//frameworks\base\services\java\com\android\server\SystemServer.java
public static void main(String[] args) {
        new SystemServer().run();
   }
private void run() {
	try {
     	t.traceBegin("InitBeforeStartServices");
		//......
		Looper.prepareMainLooper();
		Looper.getMainLooper().setSlowLogThresholdMs(
                    SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);

        SystemServiceRegistry.sEnableServiceNotFoundWtf = true;

        // Initialize native services.
        System.loadLibrary("android_servers");

		//......
		// Initialize the system context.
        createSystemContext();
		
		//......

	} finally {
            t.traceEnd();  // InitBeforeStartServices
    }

	// Start services.
        try {
            t.traceBegin("StartServices");
            startBootstrapServices(t);
            startCoreServices(t);
            startOtherServices(t);
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            t.traceEnd(); // StartServices
        }

	// Loop forever.
    Looper.loop();
    throw new RuntimeException("Main thread loop unexpectedly exited");
}

run方法中主要完成以下工作

  1. 开启消息队列
  2. 加载libandroid_servers库
  3. 创建上下文对象
  4. 开启服务

总结

在SystemServer的启动过程中,主要完成以下工作

  1. 对特定目录下的包做odex
  2. 设置默认的异常捕获器
  3. 开启binder线程池
  4. 启动服务
try {
            t.traceBegin("StartServices");
            startBootstrapServices(t);
            startCoreServices(t);
            startOtherServices(t);
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            t.traceEnd(); // StartServices
        }

标签:11,Runnable,ex,RuntimeInit,new,Android,main,android,SystemServer
From: https://blog.csdn.net/littleyards/article/details/136658830

相关文章

  • Android 11 Zygote启动流程
    Zygote进程由init进程启动,是systemserver进程和APP进程的父进程先看一下rc文件,以init.zygote32.rc为例servicezygote/system/bin/app_process-Xzygote/system/bin--zygote--start-system-serverclassmainpriority-20userrootgrouprootr......
  • 浮木云学习日志(11)---表格设计(一)
    ​表格作为展现数据最为清晰、高效的形式之一,同时又具有复用度高,拓展性强优势,是最常见的信息展示形式,也是对数据进行浏览和操作最便捷的设计方式,合理的表格设计能给用户带来高效的信息获取率。今天我就利用浮木云软件设计开发平台https://www.fumucloud.com/设计一款简洁美观的......
  • Android NDK入门:在应用中加入C和C++的力量
    目录​编辑引NDK的设计目的与Java/Kotlin的结合使用场景开发流程设置项目以支持NDK编写本地代码使用JNI连接本地代码和Java/Kotlin代码编译和运行你的应用附 引自诩方向是android方向的移动端开发工程师,却从来没有真正仔细了解过NDK,这里就详细的整理了解一下n......
  • 在 Android 上恢复已删除文件的 5 种简单方法
    您可能会因为意外删除、未完成的Android更新、手机意外关机等原因而丢失Android上的重要数据。新技术的发展使许多手机功能或程序能够从内部恢复丢失的数据。在Android上恢复已删除文件的5种简单方法然而恢复成功率的不确定性也成为人们克服数据丢失困境的重要考虑......
  • Android中的NFC操作
    目录1.申请权限2.设置意图过滤器3.判断NFC功能是否可用(NfcAdapter)4.NFC感应(NfcAdapter)启用与禁用5.NFC数据解码6.案例代码一览NFC的全称是“NearFieldCommunication”,意思是近场通信、与邻近的区域通信。该功能由NfcAdapter(NFC适配器)控制。1.申请权限<!--N......
  • 信息学奥赛一本通:1146:判断字符串是否为回文
    【题目描述】输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。【输入】输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。【输出】如果字符串是回文,输出yes;否则,输出no。【输入样例】abcdedcba【输出样例】yes【参考程序......
  • QT6.6下android编译及调用自定义so库方法
    一.问题概述   最近需要把QT程序从5.12的桌面环境移植到QT6.6的android下,项目中有很多DLL库要重新编译为so文件,移植到android环境然后调用,中间有很多坑,虽然大体和桌面系统用法相同,但细节上还是有区别的,尤其是so库的调用方法上,过程中也是debug了两天才解决,如果你也遇到了同......
  • powerbuilder11.5解析XML
    //定义变量OLEObjectlole_xmlhttpOLEObjectlole_xml//创建MSXML2.XMLHTTP对象lole_xmlhttp=CREATEOLEObjectlole_xmlhttp.ConnectToNewObject("MSXML2.XMLHTTP")//创建MSXML2.DOMDocument对象lole_xml=CREATEOLEObjectlole_xml.ConnectToNewObject("......
  • 代码随想录算法训练营第day17|110.平衡二叉树 、 257. 二叉树的所有路径 、404.左叶子
    目录a.110.平衡二叉树b.257.二叉树的所有路径 c.404.左叶子之和a.110.平衡二叉树力扣题目链接(opensnewwindow)给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。示例1......
  • android项目运行流程
    android项目运行流程三步 第一步:查清单AndroidManifest.xmlandroid项目核心在app的src的main里java是代码,res是资源有很多xml,然后AndroidManifest.xml是清单这是AndroidManifest.xml,在里面找到category.LAUNCHER-->里面的activity的名称就是我们要访问的第二步:跳到Main......