源码位置:frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
https://blog.csdn.net/yiranfeng/article/details/103550262
目录
main方法
823 @UnsupportedAppUsage
824 public static void main(String argv[]) {
825 ZygoteServer zygoteServer = null;
826
827 // Mark zygote start. This ensures that thread creation will throw
828 // an error.
829 ZygoteHooks.startZygoteNoThreadCreation();
830
831 // Zygote goes into its own process group.
832 try {
833 Os.setpgid(0, 0);
834 } catch (ErrnoException ex) {
835 throw new RuntimeException("Failed to setpgid(0,0)", ex);
836 }
837
838 Runnable caller;
839 try {
840 // Report Zygote start time to tron unless it is a runtime restart
841 if (!"1".equals(SystemProperties.get("sys.boot_completed"))) {
842 MetricsLogger.histogram(null, "boot_zygote_init",
843 (int) SystemClock.elapsedRealtime());
844 }
845
846 String bootTimeTag = Process.is64Bit() ? "Zygote64Timing" : "Zygote32Timing";
847 TimingsTraceLog bootTimingsTraceLog = new TimingsTraceLog(bootTimeTag,
848 Trace.TRACE_TAG_DALVIK);
849 bootTimingsTraceLog.traceBegin("ZygoteInit");
850 RuntimeInit.enableDdms();
851
852 boolean startSystemServer = false;
853 String zygoteSocketName = "zygote";
854 String abiList = null;
855 boolean enableLazyPreload = false;
856 for (int i = 1; i < argv.length; i++) {
857 if ("start-system-server".equals(argv[i])) {
858 startSystemServer = true;
859 } else if ("--enable-lazy-preload".equals(argv[i])) {
860 enableLazyPreload = true;
861 } else if (argv[i].startsWith(ABI_LIST_ARG)) {
862 abiList = argv[i].substring(ABI_LIST_ARG.length());
863 } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
864 zygoteSocketName = argv[i].substring(SOCKET_NAME_ARG.length());
865 } else {
866 throw new RuntimeException("Unknown command line argument: " + argv[i]);
867 }
868 }
869
870 final boolean isPrimaryZygote = zygoteSocketName.equals(Zygote.PRIMARY_SOCKET_NAME);
871
872 if (abiList == null) {
873 throw new RuntimeException("No ABI list supplied.");
874 }
875
876 // In some configurations, we avoid preloading resources and classes eagerly.
877 // In such cases, we will preload things prior to our first fork.
878 if (!enableLazyPreload) {
879 bootTimingsTraceLog.traceBegin("ZygotePreload");
880 EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
881 SystemClock.uptimeMillis());
882 preload(bootTimingsTraceLog);
883 EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
884 SystemClock.uptimeMillis());
885 bootTimingsTraceLog.traceEnd(); // ZygotePreload
886 } else {
887 Zygote.resetNicePriority();
888 }
890 // Do an initial gc to clean up after startup
891 bootTimingsTraceLog.traceBegin("PostZygoteInitGC");
892 gcAndFinalize();
893 bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC
894
895 bootTimingsTraceLog.traceEnd(); // ZygoteInit
896 // Disable tracing so that forked processes do not inherit stale tracing tags from
897 // Zygote.
898 Trace.setTracingEnabled(false, 0);
899
900
901 Zygote.initNativeState(isPrimaryZygote);
902
903 ZygoteHooks.stopZygoteNoThreadCreation();
904
905 zygoteServer = new ZygoteServer(isPrimaryZygote);
906
907 if (startSystemServer) {
908 Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);
909
910 // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
911 // child (system_server) process.
912 if (r != null) {
913 r.run();
914 return;
915 }
916 }
917
918 Log.i(TAG, "Accepting command socket connections");
919
920 // The select loop returns early in the child process after a fork and
921 // loops forever in the zygote.
922 caller = zygoteServer.runSelectLoop(abiList);
923 } catch (Throwable ex) {
924 Log.e(TAG, "System zygote died with exception", ex);
925 throw ex;
926 } finally {
927 if (zygoteServer != null) {
928 zygoteServer.closeServerSocket();
929 }
930 }
931
932 // We're in the child process and have exited the select loop. Proceed to execute the
933 // command.
934 if (caller != null) {
935 caller.run();
936 }
937 }
核心代码截取:
1)创建ZygoteServer实例
2)forkSystemServer
3)zygoteServer.runSelectLoop
Runnable caller;
try{
...
zygoteServer = new ZygoteServer(isPrimaryZygote);
906
907 if (startSystemServer) {
908 Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);
909
910 // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
911 // child (system_server) process.
912 if (r != null) {
//调用SystemServer的main方法
913 r.run();
914 return;
915 }
916 }
917
918 Log.i(TAG, "Accepting command socket connections");
919
920 // The select loop returns early in the child process after a fork and
921 // loops forever in the zygote.
922 caller = zygoteServer.runSelectLoop(abiList);
923 } catch (Throwable ex) {
924 Log.e(TAG, "System zygote died with exception", ex);
925 throw ex;
926 } finally {
927 if (zygoteServer != null) {
928 zygoteServer.closeServerSocket();
929 }
930 }
931
932 // We're in the child process and have exited the select loop. Proceed to execute the
933 // command.
934 if (caller != null) {
935 caller.run();
936 }
1.forkSystemServer方法
方法的一开始就定义了args数组,其中最后为"com.android.server.SystemServer",即SystemServer全类名,会一直往后面传递。
→Zygote.forkSystemServer(创建SystemServer子进程)
http://xinyiworld.top/wordpress_it/?p=13969
→handleSystemServerProcess方法(反射SystemServer类的main方法)
534 createSystemServerClassLoader();
535 ClassLoader cl = sCachedSystemServerClassLoader;
536 if (cl != null) {
537 Thread.currentThread().setContextClassLoader(cl);
538 }
539
540 /*
541 * Pass the remaining arguments to SystemServer.
542 */
543 return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
544 parsedArgs.mRemainingArgs, cl);
→→createSystemServerClassLoader
556 private static void createSystemServerClassLoader() {
557 if (sCachedSystemServerClassLoader != null) {
558 return;
559 }
560 final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH");
561 // TODO: Should we run optimization here?
562 if (systemServerClasspath != null) {
563 sCachedSystemServerClassLoader = createPathClassLoader(systemServerClasspath,
564 VMRuntime.SDK_VERSION_CUR_DEVELOPMENT);
565 }
566 }
打印环境变量SYSTEMSERVERCLASSPATH
kona:/ # set | grep SYSTEMSERVERCLASSPATH
SYSTEMSERVERCLASSPATH=/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar:/system/framework/com.android
.location.provider.jar
我们知道SystemServer.java就是编译到/system/framework/services.jar里的。
→→zygoteInit
977 public static final Runnable zygoteInit(int targetSdkVersion, String[] argv,
978 ClassLoader classLoader) {
979 if (RuntimeInit.DEBUG) {
980 Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");
981 }
982
983 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
984 RuntimeInit.redirectLogStreams();
985
986 RuntimeInit.commonInit();
987 ZygoteInit.nativeZygoteInit();
988 return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);
989 }
→→→RuntimeInit.commonInit()
commonInit()的可见性修饰符为protected,ZygoteInit.java与RuntimeInit.java在同一包名下,所以能直接调用。
见:http://xinyiworld.top/wordpress_it/?p=13980
崩溃性的异常捕获
67 /**
68 * Logs a message when a thread encounters an uncaught exception. By
69 * default, {@link KillApplicationHandler} will terminate this process later,
70 * but apps can override that behavior.
71 */
72 private static class LoggingHandler implements Thread.UncaughtExceptionHandler {
73 public volatile boolean mTriggered = false;
74
75 @Override
76 public void uncaughtException(Thread t, Throwable e) {
77 mTriggered = true;
78
79 // Don't re-enter if KillApplicationHandler has already run
80 if (mCrashing) return;
81
82 // mApplicationObject is null for non-zygote java programs (e.g. "am")
83 // There are also apps running with the system UID. We don't want the
84 // first clause in either of these two cases, only for system_server.
85 if (mApplicationObject == null && (Process.SYSTEM_UID == Process.myUid())) {
86 Clog_e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e);
87 } else {
88 StringBuilder message = new StringBuilder();
89 // The "FATAL EXCEPTION" string is still used on Android even though
90 // apps can set a custom UncaughtExceptionHandler that renders uncaught
91 // exceptions non-fatal.
92 message.append("FATAL EXCEPTION: ").append(t.getName()).append("\n");
93 final String processName = ActivityThread.currentProcessName();
94 if (processName != null) {
95 message.append("Process: ").append(processName).append(", ");
96 }
97 message.append("PID: ").append(Process.myPid());
98 Clog_e(TAG, message.toString(), e);
99 }
100 }
101 }
→→→RuntimeInit.applicationInit
RuntimeInit.applicationInit方法见:http://xinyiworld.top/wordpress_it/?p=13980
0 条评论