System Server 分析

SystemServer 是 Android Java 层的系统服务模块,这个模块主要功能就是管理供 Android 应用开发的 system service.
一.SystemServer 类是如何启动的

让我们从 Android 的启动过程看起,查看 init.rc 文件,就会发现下面这一行service zygote /system/bin/app_process -Xzygote /system/bin –zygote –start-system-server

我们知道 zygote 进程是整个 Android 的孵化器进程,所有的 Activity 进程均是通过它来生成的。我们发现在 zygote 进程启动过程中指定了这么一个参数“– start-system-server” ,这个参数就是在 zygote 进程启动的同时启动 SystemServer 。

那么 SystemServer 是以什么样的形式启动的呢?是单独的一个进程还是线程,分析一下 zygote 进程的启动过程就明白了。在 Android 中 zygote 进程启动其实就是启动 /system/bin/app_process 这个进程,这个进程的源代码在 frameworks/base/cmds/app_process/app_main.cpp 中。

if (0 == strcmp("--zygote", arg)) {
bool startSystemServer = (i < argc) ?
strcmp(argv[i], "--start-system-server") == 0 : false;
setArgv0(argv0, "zygote");
set_process_name("zygote");
runtime.start("com.android.internal.os.ZygoteInit",
startSystemServer);
} else {
set_process_name(argv0);

runtime.mClassName = arg;

// Remainder of args get passed to startup class main()
runtime.mArgC = argc-i;
runtime.mArgV = argv+i;

LOGV("App process is starting with pid=%d, class=%s.\n",
getpid(), runtime.getClassName());
runtime.start();
}

由于 zygote 进程启动过程有“ –zygote” 这个参数,所以走的是下面这步

runtime.start("com.android.internal.os.ZygoteInit",startSystemServer);

查看对象 runtime 的类型的定义,

class AppRuntime : public AndroidRuntime

因此查看 AndroidRuntime 的 start 方法中的一段代码 (frameworks/base/core/jni/AndroidRuntime.cpp)

startClass = env->FindClass(slashClassName);
if (startClass == NULL) {
LOGE("JavaVM unable to locate class '%s'\n", slashClassName);
/* keep going */
} else {
startMeth = env->GetStaticMethodID(startClass, "main",
"([Ljava/lang/String;)V");
if (startMeth == NULL) {
LOGE("JavaVM unable to find main() in '%s'\n", className);
/* keep going */
} else {
env->CallStaticVoidMethod(startClass, startMeth, strArray);

#if 0
if (env->ExceptionCheck())
threadExitUncaughtException(env);
#endif

其中 startClass 即为 “com.android.internal.os.ZygoteInit” ,这段代码调用了 com.android.internal.os.ZygoteInit 的 main 函数。

那么再往下看 ZygoteInit 类的 main 函数,其中的一段 MethodAndArgsCaller 代码为

if (argv[1].equals("true")) {
startSystemServer();
} else if (!argv[1].equals("false")) {
throw new RuntimeException(argv[0] + USAGE_STRING);
}

调用了 startSystemServer () ,这个函数启动了一个子进程来作为 SystemServer 的载体,

1. 它首先指定 SystemServer 进程的参数 ;

2. 根据指定的参数来创建 SystemServer 进程;

3. 调用 handleSystemServerProcess 启动第一步指定进程参数过程中指定的类,此时为“ com.android.server.SystemServer ” ,启动的这个进程在 ps 查看后显示为” system_server ” 。

private static boolean startSystemServer()
throws MethodAndArgsCaller, RuntimeException {
/* Hardcoded command line to start the system server */
String args[];
String ashmem_size = System.getProperty("gralloc.ashmem_size");
if ((null != ashmem_size) && (0 != ashmem_size.length())) {
args = new String[] {
"--setuid=1000",
"--setgid=1000",
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003,3006",
"--capabilities=130104352,130104352",
"--rlimit=8,",
"--runtime-init",
"--nice-name=system_server",
"com.android.server.SystemServer",
};
args[4] = args[4].concat(ashmem_size);
args[4] = args[4].concat(",");
args[4] = args[4].concat(ashmem_size);
} else {
args = new String[] {
"--setuid=1000",
"--setgid=1000",
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003,3006",
"--capabilities=130104352,130104352",
"--runtime-init",
"--nice-name=system_server",
"com.android.server.SystemServer",
};
}
ZygoteConnection.Arguments parsedArgs = null;

int pid;

try {
parsedArgs = new ZygoteConnection.Arguments(args);

/*
* Enable debugging of the system process if *either* the command line flags
* indicate it should be debuggable or the ro.debuggable system property
* is set to "1"
*/
int debugFlags = parsedArgs.debugFlags;
if ("1".equals(SystemProperties.get("ro.debuggable")))
debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;

int[][] rlimits = new int[0][0];

if (parsedArgs.rlimits != null) {
rlimits = parsedArgs.rlimits.toArray(rlimits);
}

/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids, debugFlags, rlimits,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}

/* For child process */
if (pid == 0) {
handleSystemServerProcess(parsedArgs);
}

return true;
}

通过以上过程就会调用到 SystemServer 类,且是运行在一个名为“ system_server” 的进程中,这个进程为 zygote 的子进程。

虽然 SystemServer 类是运行在 system_server 中的,但是它并不运行在 system_server 的主线程中。

/**
* Finish remaining work for the newly forked system server process.
*/
private static void handleSystemServerProcess(
ZygoteConnection.Arguments parsedArgs)
throws ZygoteInit.MethodAndArgsCaller {

closeServerSocket();

/*
* Pass the remaining arguments to SystemServer.
* "--nice-name=system_server com.android.server.SystemServer"
*/
RuntimeInit.zygoteInit(parsedArgs.remainingArgs);
/* should never reach here */
}

/**
* The main function called when started through the zygote process. This
* could be unified with main(), if the native code in finishInit()
* were rationalized with Zygote startup.<p>
*
*/
public static final void zygoteInit(String[] argv)
throws ZygoteInit.MethodAndArgsCaller {
// TODO: Doing this here works, but it seems kind of arbitrary. Find
// a better place. The goal is to set it up for applications, but not
// tools like am.
System.setOut(new AndroidPrintStream(Log.INFO, "System.out"));
System.setErr(new AndroidPrintStream(Log.WARN, "System.err"));

commonInit();
zygoteInitNative();

int curArg = 0;
for ( /* curArg */ ; curArg < argv.length; curArg++) {
String arg = argv[curArg];

if (arg.equals("--")) {
curArg++;
break;
} else if (!arg.startsWith("--")) {
break;
} else if (arg.startsWith("--nice-name=")) {
String niceName = arg.substring(arg.indexOf('=') + 1);
Process.setArgV0(niceName);
}
}

if (curArg == argv.length) {
Slog.e(TAG, "Missing classname argument to RuntimeInit!");
// let the process exit
return;
}

// Remaining arguments are passed to the start class's static main

String startClass = argv[curArg++];
String[] startArgs = new String[argv.length - curArg];

System.arraycopy(argv, curArg, startArgs, 0, startArgs.length);
invokeStaticMain(startClass, startArgs);
}

/**
* Invokes a static "main(argv[]) method on class "className".
* Converts various failing exceptions into RuntimeExceptions, with
* the assumption that they will then cause the VM instance to exit.
*
* @param className Fully-qualified class name
* @param argv Argument vector for main()
*/
private static void invokeStaticMain(String className, String[] argv)
throws ZygoteInit.MethodAndArgsCaller {

// We want to be fairly aggressive about heap utilization, to avoid
// holding on to a lot of memory that isn't needed.
VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);

Class<?> cl;

try {
cl = Class.forName(className);
} 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 });
} 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);
}

int modifiers = m.getModifiers();
if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
throw new RuntimeException(
"Main method is not public and static on " + className);
}

/*
* This throw gets caught in ZygoteInit.main(), which responds
* by invoking the exception's run() method. This arrangement
* clears up all the stack frames that were required in setting
* up the process.
*/
throw new ZygoteInit.MethodAndArgsCaller(m, argv);
}

由代码可以看出,在调用函数 invokeStaticMain 的最后会抛出一个异常。

public static class MethodAndArgsCaller extends Exception implements Runnable

回过头再看一下 ZygoteInit 类的 main 函数中的一段代码

catch (MethodAndArgsCaller caller) {
caller.run();
}

其实就是在这个抛出的异常中启动了 SystemServer 类,且这个异常为一个 Runnable 类型,因此 SystemServer 类就运行在 system_server 进程中的一个新的线程里。
二.SystemServer 执行内容

1. 执行 frameworks/base/cmds/system_server/library/system_init.cpp 中的 system_init 函数,启动当前进程,也就是进程 system_server 的 pool thread ,以便执行 Binder IPC 操作。

2. 向 SM(Service Manager) 添加系统服务。
Socket阻塞导致的system_server无法正常启动分析 这篇文章主要记录分析monkey出现的一个system server无法正常启动问题 从中可以了解如何分析strace,锁占用等内容 … 注:文章分析过程比较长,可以先参考文章末尾的问题主线图 问题现象   设备跑monkey过程中system server重启,但是没有成功启动,system server被block,出现卡白米的现象。 问题分析   首先看看s... 阅读详情

相关推荐

Android系统跑monkey测试发生SWT system_server问题log简单分析

首先从手机导出DB文件,使用MediatekLogView解析db.fatal.00.SWT.dbg,打开__exp_main.txt文件,可以看到Fatal的详细信息。 Exception Class: SWT Exception Type: system_server_watchdog Current Executing Process: system_server Trigger ti...

吁吁小玉的博客 1208

SystemServer分析记录

Zygote进程forkSystemServer进程后,调用了SystemServer的main方法,具体流程请参考其他资料,这里记录的是SystemServer关键代码的分析。

JohanMan的博客 321

MTK system_server 卡死导致手机重启案例分析

和你一起终身学习,这里是程序员Android经典好文推荐,通过阅读本文,您将收获以下知识点:一、MTK AEE Log分析工具二、AEE Log分析流程三、system_server 卡死案例分析及解决本文主要针对Exception Type: system_server_watchdog,system_server卡死找出的分析以及解决方案。一、MTK AEE Log分析工具MTK AEE...

程序员Android 2327

java输出的时候出现[Ljava.lang.String是什么意思+解决方法

例如:` public static void main(String[] args) { String a1 = "123.sdggs.1234df"; String[] a2 = a1.split("\\."); System.out.println(a2); }`

实用ai技巧 6万+

gitlab远程子仓库配置SSH - 保姆级教程

在网上搜索了一些方法,但是不够详细,走了好多弯路,所以记录下配置SSH的详细过程吧。

是菠萝的博客 1万+

酰基转移酶工程:从理性设计到合成生物学应用的深度解析

酰基转移酶是一类催化酰基转移反应的关键生物催化剂,其核心机制涉及将酰基从供体分子特异性地转移至受体分子。这一过程依赖于酶活性中心精密的微环境与底物之间的相互作用,决定了反应的特异性与效率。通过理性设计与定向进化等蛋白质工程策略,可以系统性地改造酶的底物结合口袋与催化特性,从而突破其天然底物谱限制,实现非天然底物的高效转化。这类工程化酶在合成生物学中展现出巨大技术价值,能够作为核心模块用于构建微生物细胞工厂或多酶级联反应体系,实现药物中间体、高价值香料及生物基材料的高效、绿色生物制造。本文聚焦于酰基转移酶的工

weixin_30516429的博客 306

android SystemServer进程的作用

SystemServer进程是android中一个很重要的进程由Zygote进程启动SystemServer进程主要用于启动系统中的服务SystemServer进程启动服务的启动函数为main函数SystemServer在执行过程中首先会初始化一些系统变量,加载类库,创建Context对象,创建SystemServiceManager对象等之后才开始启动系统服务SystemServer进程将系统服

wang_you189959的专栏 1473

【论文阅读】LIME:Low-light Image Enhancement via Illumination Map Estimation(笔记最全篇)

文章目录AbstractIntroductionContributionMethodSpeed-up Method(1):ALM子问题 T子问题 G子问题 Z和μ\muμSpeed-up Method(2):权重变量实验conclusion 今天要整理的一篇论文是使用传统的方法解决低照度的问题,Guo等人于2016年发表在IEEE上,论文《LIME:Low-light Image Enhance...

florrie 2万+

SystemServer的含义和作用

1.SystemServer是一个进程,并且是由zygote进程fork出来的 [zygote进程] 2.Android Framework里面两大非常重要的进程: 1.SystemServer进程 2.zygote进程 3.SystemServer比较重要的原因为: 1.系统级的重要服务都是在这个进程里开启的 比如: ActivityManageService、 PackgeManageService、WindowManageService等...

冯旭的博客 2909

自动化测试软件设计:C# + VISA 库实现多接口(GPIB/USB)测试仪器控制

本文介绍了如何利用C#和VISA库开发支持多接口的自动化测试软件。VISA库作为标准化API,能够统一控制GPIB、USB等不同接口的测试设备。文章详细阐述了软件设计目标,包括多接口支持、统一控制、自动化脚本执行等功能。重点讲解了C#中VISA库的安装配置,并提供了设备控制类的核心代码实现,涵盖设备连接、命令发送、响应读取等关键操作。通过具体示例展示了GPIB和USB设备的连接方式,并扩展了自动化测试脚本功能。该方案具有高扩展性和维护性,适用于电子设备测试、生产线检测等多种场景,为自动化测试系统开发提供了实

这里只有干货:从 Modbus 到 MES,从 YOLO 到工控机部署;65 篇现场排障案例,每篇都写到改哪一行。 1735

【QT+VS】如何在现有VS项目中添加Qt界面?【全网最详细】

VS项目有了,就缺个QT界面,怎么办呢?这篇文章帮你一键搞定!

深耕CV 5008

system_server进程分析

 一、system_server的创建 1 1.1、forkSystemServer()函数 2 1.2、nativeForkSystemServer() 的JNI层实现 3 1.3、forkAndSpecializeCommon( ) 4 1.4、SetSigChldHandler() 4 1.5、SigChldHandler 5 二、handleSys

Toc_SunWinner的博客 2333

幻尔串口总线舵机控制Python SDK

幻尔串口总线舵机控制Python SDK,可以直接在PC端,不需要借助单片机对舵机进行控制。

daishabby2486的博客 4148

system_server ANR分析之误报

工作中遇到system_server出现ANR,实际分析了日志后,发现问题并不是出在system_server,而是由于其它进程出现了ANR导致的system_server误报(运行环境Android 11)。至此,问题结案,是系统在收集ANR日志信息的时候,真正出现ANR的应用由于已经被kill了,导致没有采集到真正出现ANR的应用的堆栈信息,而把锅甩给了system_server这个“倒霉蛋”。与anr_2023-06-23-09-05-04-876文件中的pid信息一致,

borise123456的专栏 441

疾风气象大模型4.0白皮书

《疾风气象大模型白皮书摘要》 疾风气象大模型(J-AFM)是国内首个基于3D建模的气象智能平台,突破传统二维气象分析的局限,采用体素化技术构建"x-y-z-t"四维统一模型,实现从分钟级短临到30天跨时效的连续预测。核心技术包括:1)多源数据三维同化,融合雷达、卫星、风廓线等异构数据;2)物理约束AI解码器,嵌入能量守恒、湍流封闭等气象原理;3)行业适配插件,服务风电、光伏、航空等领域,如山地风场尾流校正、云影短临预测。平台支持批流一体推理与边缘部署,提供概率化输出和误差溯源能力,显著降

专注AI大模型,软件混淆,授权 558

在VINS-FUSION上处理KITTI数据集(双目+GPS)

VINS-Fusion 是一个开源的实时多传感器状态估计库,主要由香港科技大学的沈邵劼教授领导的研究团队开发。它是 VINS-Mono(单目视觉惯性系统)的扩展,支持多种传感器组合,如双目、立体相机和IMU。

如果想成为中心,那么就到中心去吧。 1216

Android System Server进程源码分析 上

一 System Server System Server是Zygote启动的第一个进程,它的核心功能是启动和管理Android系统的各类服务。 1.0 startSystemServer private static boolean startSystemServer(String abiList, String socketName) // abiList

Android 6.0 源码学习 1811

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Android源码分析--system_server进程分析

在上一篇博文中我们进行了有关Zygote进程的分析,我们知道Zygote进程创建了一个重要的进程–system_server进程后就进入了无限循环中,之后Android系统中的重要任务就交给了system_server进程,作为zygote的嫡长子进程,system_server进程的意义非凡,今天我们来分析一下system_server进程。

Joe_c 的专栏 4407

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

kernel空指针引起的system_server watchdog问题分析

一. 问题描述1.1 现象  手机kernel启动时狂点屏幕会导致手机卡死重启.1.3 结论  因为surfaceflinger主线程在等中断处理函数执行执行结束,而中断处理函数一直没有执行完,所以导致surfaceflinger的主线程卡住.这时候system_server通过binder跨进程去调用 surfaceflinger中的方法时,由于surfaceflinger卡住了,所以syste...

aa787282301的博客 4980

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

system_server NE问题分析

有台手机出现频繁上层重启问题,开机后不操作,放在桌子上不动,过一会就会发生FW重启,然后重复,查看log,发现是发生了system_server的NE问题,从Log看如下:[log analysis]01-27 11:41:19.553  5376  5376 F DEBUG   : pid: 1404, tid: 2453, name: Binder:1404_7  &gt;&gt;&gt; s...

bmj的专栏 1977

Android情景分析之深入解析system_server

system_server进程作为zygote的嫡长子,其重要性是不言而喻的。下面我们通过代码来深入分析下system_server的实现。 system_server的诞生 在深入解析zygote的时候,我们看过system_server的启动过程,这里我们再来回顾下: /* Hardcoded command line to start the system server */

莫灰灰的专栏 2万+

android System Server 分析

SystemServer 是 Android JAVA 层的系统服务模块,这个模块主要功能就是管理供 Android 应用开发的 system service. 1.SystemServer 类是如何启动的 让我们从 Android 的启动过程看起,查看 init.rc 文件,就会发现下面这一行 service zygote /syst

小明的专栏 1274

图解Android - Zygote, System Server 启动分析

而能够访问Java端的thread需要跟JVM进行绑定,下面是具体的实现函数//将entryFunc 暂存在args[0]//entryFunc变成javaThreadShell./* 跟 VM 绑定 */= JNI_OK)return -1;/* 运行真正的’entryFunc’ */attachVM() 到底做什么事情?

erhtre的博客 553
上一篇: DHCP之简要分析
下一篇: Android系统属性SystemProperties分析
sharp2wing
博客等级 码龄19年 5粉丝 89原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址