之前留下了一个大坑,现在慢慢填上。
app_process是安卓系统启动Java虚拟机的程序。首先来回忆下Zygote进程,按照邓凡平老师的书上说的, java世界由Zygote进程创建,通过fork一个一个产生。那么, Zygote又是怎么来的呢?
我们知道,linux系统第一个进程就是init,安卓也不例外。在kernel层,init起来之后,安卓会通过一个脚本 激活Zygote进程。
init的具体实现位于 system/core/init 下, 如果详细介绍init的情况,这篇文章会偏离主旨的,会另外开一篇介绍 这个事情。以system/core/rootdir/init.zygote32.rc为例:
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
class main
priority -20
user root
group root readproc reserved_disk
socket zygote stream 660 root system
socket usap_pool_primary stream 660 root system
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart audioserver
onrestart restart cameraserver
onrestart restart media
onrestart restart netd
onrestart restart wificond
writepid /dev/cpuset/foreground/tasks
app_process会接受三类启动参数,分别代表不同的系统服务。在 frameworks/base/cmds/app_process/app_main.cpp文件中
if (zygote) {
runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
} else if (className) {
runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
} else {
fprintf(stderr, "Error: no class name or --zygote supplied.\n");
app_usage();
LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
}
最终会以具体的形式调用runtime. runtime实际上是AppRuntime的实例,而Appruntime是AndroidRuntime的派生类。AppRuntime也是定义在app_main.cpp中, 只是重写了setClassNameAndArgs、onVmCreated()、onStarted()、onZygoteInit()、onExit()这几个方法。
我们本文的重点函数AndroidRuntime是一个重点钉子户,涉及的东西比较多,源文件在 frameworks/base/core/jni/AndroidRuntime.cpp 。这个文件处理了很多jni注册的事情。
/** Start the Android runtime,This invokes VM and calling the "static void main(String[], args)" method in the class named
* by "className"
*/
void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote){
...
// 加载一些环境变量
const char* rootDir = getenv("ANDROID_ROOT"); // getenv.c 在bionic/libc/upstream-openbsd/lib/libc/stdlib/getenv.c中定义
// 这个文件理解起来还不是那么友好的
const char* runtimeRootdir = getenv("ANDROID_RUNTIME_ROOT");
const char* tzdataRootDir = getenv("ANDROID_TZDATA_ROOT");
...
JniInvocation jni_invocation; // 这两个是重中之重
jni_invocation.Init(NULL);
}
在我写这篇笔记的时候,Android已经诞生了12年之多,想想自己当初在刚上大学那会(12-13)还在用Nokia的 N51,其实那个时候安卓系统已经很厉害了,之所以自己这么顽固不化主要还是因为自己的经济状况。导致自己发誓入手一台智能机的原因是: 驾考科目四被挂了,这才不得已使用智能机了。那时候已经是大三了,用的是华为麦芒。
研究生毕业后来到这家公司后,直接上手了Android源码,真的很奇怪,我好想学什么都是先底层然后再上层,这次的Android也是。现在也别管 Android以后的发展怎么样,因为它就在这里,自己必须在最短的时间内去熟悉它。
前言部分一般是无用的,可以直接跳过。
这里,我就是简单地回忆下自己思考的,根本没有从代码 其他书籍去考证这些东西的正确性,当然,后面随着熟悉的深入,也需要进行一些修改。
众所周知,我们安卓使用的应用的打包形式为APK文件,当launch 安装这个应用是,AOSP(Android Open Source Platfrom,用来简称安卓)会维持一个名为installd的守护进程,他会自动监听应用程序包的安装情况。
在这一通道上,有一个名为PackageManagerService的服务进行包安装。这是AOSP中一个较为核心的服务。
在aosp的源码中,external/avb目录下包含了这个工具,特指 Android Verified Boot 2.0,该目录
包含有tools和相关的库函数。
avb
vimer@host:~/src/aosp$ avbtool info_image --image out/target/product/generic_x86_64/vbmeta.img
Minimum libavb version: 1.0
Header Block: 256 bytes
Authentication Block: 576 bytes
Auxiliary Block: 1984 bytes
Algorithm: SHA256_RSA4096
Rollback Index: 0
Flags: 1
Release String: 'avbtool 1.1.0'
Descriptors:
Chain Partition descriptor:
Partition Name: system
Rollback Index Location: 1
Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d
Prop: com.android.build.vendor.os_version -> '10'
Hashtree descriptor:
Version of dm-verity: 1
Image Size: 117182464 bytes
Tree Offset: 117182464
Tree Size: 929792 bytes
Data Block Size: 4096 bytes
Hash Block Size: 4096 bytes
FEC num roots: 2
FEC offset: 118112256
FEC size: 933888 bytes
Hash Algorithm: sha1
Partition Name: vendor
Salt: 500a5d4b28c5b94e8053a56a25f0a4a4455937e0
Root Digest: 3d0fbf077fa15a1a04600e835153516fbcb9e3fc
Flags: 0
如果想弄清楚一个文件的作用,要知道三个方面的内容:
就我目前接触到的aosp的soong系统,这三个对我而言并不是特别的清晰。当然,如果
真要说这块的大体位置,我可以绝对保证,相关的中间文件位于out/.相关的目录。
soong有一个自带的logger,可以看一下下面这个目录:
vimer@host:~/src/aosp$ ls build/soong/ui/
build logger metrics status terminal tracer
看一下 build\soong\cmd\soong_ui\main.go 这个文件中如何使用logger.
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"android/soong/ui/build"
"android/soong/ui/logger"
"android/soong/ui/metrics"
"android/soong/ui/status"
"android/soong/ui/terminal"
"android/soong/ui/tracer"
)
这里先铺垫一下go的基本使用方法。 关键词 package main是指定了
由这个main.go文件生成了一个main的包,这样,在其他go文件中,只要
使用import命令,就比如android/soong/ui/logger的引用方式,我们
就可以直接使用main.xx()的方式使用main文件定义的方法。
很明显,main.go把logger模块(感觉包不如模块熟悉些)引入到main.go 中,比如:
...
writer := terminal.NewWriter(stdio)
defer writer.Finish()
log := logger.New(writer)
defer log.Cleanup()
.....
if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
if !strings.HasSuffix(start, "N") {
if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
log.Verbosef("Took %dms to start up.",
time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
}
}
...
我们可以看到log.verbosef()方法已经生效,那么这个log文件位于什么地方呢?具体应该可以在代
码中定位到的,不过我现在没有去查找,据我了解的是在out/soong.log文件下。
但是这个方法对于某些文件并不能凑效,比如,我想调试下build/soong/cc/cmakelists.go的某些具体量,但是不知道为什么根本加不进去logger模块,报错的:
build/soong/cc/cmakelists.go:25:2: can't find import: "android/soong/ui/logger"
非常简答,只能是作为过渡方法:
...
f_1, _ := os.Create("out/yubo_soong_include.log")
defer f_1.Close()
f_1.WriteString("yubo\n")
f_1.WriteString(fmt.Sprintf("project(%s)\n", ccModule.ModuleBase.Name()))
...
这样自定义输出log文件的好处是: 清晰 只打印自己想要的内容,但缺点是输出的内容难以控制, 需要适配,比如:某个模块的某个方法到底能返回什么样类型的值,还需要看代码去确定。
但是,先这样凑合着使用吧。
c++的虚函数与面向对象的多态的思想紧密结合,这篇文章
简单介绍了多态的一些概念,这个值得下面进行总结。
多态主要有两种:
目前,本博客已经就编译时多态进行了一些总结,这篇文 章主要总结运行时多态的用法,也就是虚函数的用法。
举个简单的例子热身.
using namespace std;
class B {
public:
virtual void s() = 0; // pure Virtual Function
};
class D:public B{
public:
void s(){
cout <<"Virtual function in Derived class";
}
};
int main(){
B *a;
D obj;
a = &obj;
a->s();
}
为了更好的说明虚函数的用法,先看一个最基本的例子:
#include <iostream>
using namespace std;
class base {
public:
virtual void print() // keyword virtual
{
cout << "print the base class" << endl;
}
void show(){
cout << "show the base class" << endl;
}
};
class derived : public base {
public:
void print()
{
cout << "print derived class " << endl;
}
void show()
{
cout << "show derived class" << endl;
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
// vitual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
}
输出结果为:
print derived class
show the base class
这里最显著的一个地方在于,在同一个派生类(有地址引用)调用有无virtual修饰的方法是不一样的。
如果用我自己的话说就是,虚函数改变了基类的成员函数,这还是在不修改
基类代码的情况下。那么,在已有继承的方法前提下,这个特点还有意义吗?
或者说,虚函数到底是做什么的?这不仅仅对我这样的初学者是一种疑惑,
同样的,stackoverflow
也有类似的疑惑。Task is cheap,show me the code.
example 1:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "I' m eating generic food" << endl;
}
};
class Cat : public Animal{
public:
void eat() { cout << "I am eating a rat " << endl; }
};
int main()
{
Animal *animal = new Animal;
Cat *cat = new Cat;
animal->eat(); // I am eating generic food
cat->eat(); // I am eating a rat
}
都没问题吧,但是,如果利用一个中间函数去调用情况又不一样了,比如:
void func(Animal *xyz) {
xyz->eat();
}
int main()
{
Animal *animal = new Animal;
Cat *cat = new Cat;
func(animal);
//I' m eating generic food
func(cat);
//I' m eating generic food ''
}
这个时候就出现问题了,全部输出了基类的方法。如果还有dog pig monkey
啥的都能乱死了,那怎么办?
关键就是在基类的方法加上virtual就可以了。
其实,在我看来,加上这个关键词并不能有助于真正理解这个问题:问题的 关键在于怎么使用基类和派生类才会导致这个问题的产生。
如果在func(Animal *xyz)使用 cat 去定义函数参数类型则是失败的。
这是一个编程题目大体的意思是:
一个基类: Person, 内含 name age两个成员变量,getdata()和putdata()两个方法,负责分别得到和输出两个成员变量。
派生类Professor多加一个publications的变量,两个成员方法不变,只不过在putdata方法中 还需要自动展示一个id号。
派生类Student需要一个marks[6]去存储6门课程成绩,最后输出。
代码如下:
/*
* File Name: print_test.cpp
* Author: Bo Yu
* Mail: [email protected]
* Created Time: 2020年04月26日 星期日 09时00分43秒
*/
#include <iostream>
#include <cstring>
using namespace std;
class Person {
public:
string name;
int age;
virtual void getdata() {
cin >> this->name >> this->age;
}
virtual void putdata() {
cout << this->name << " " << this->age << endl;
}
};
class Professor : public Person {
public:
Professor() {
this->cur_id = ++id;
}
int publications;
static int id;
int cur_id;
void getdata() {
cin >> this->name >> this->age >> this->publications;
}
void putdata() {
cout << this->name << " "
<< this->age << " "
<< this->publications << " "
<< this->cur_id << endl;
}
};
int Professor::id = 0;
class Student : public Person {
#define NUM_OF_MARKS 6
public:
Student() {
this->cur_id = ++id;
}
int marks[NUM_OF_MARKS];
static int id;
int cur_id;
void getdata() {
cin >> this->name >> this->age;
for (int i=0; i<NUM_OF_MARKS; i++) {
cin >> marks[i];
}
}
void putdata() {
int marksSum = 0;
for (int i=0; i<NUM_OF_MARKS; i++) {
marksSum += marks[i];
}
cout << this->name << " "
<< this->age << " "
<< marksSum << " "
<< this->cur_id << endl;
}
};
int Student::id = 0;
int main(){
int n, val;
cin>>n; //The number of objects that is going to be created.
Person *per[n];
for(int i = 0;i < n;i++){
cin>>val;
if(val == 1){
// If val is 1 current object is of type Professor
per[i] = new Professor;
}
else per[i] = new Student; // Else the current object is of type Student
per[i]->getdata(); // Get the data from the user.
}
for(int i=0;i<n;i++)
per[i]->putdata(); // Print the required output for each object.
return 0;
}
需要说明的一点是:
类中的静态成员变量必须在类外定义,这一点是由于static变量的生存周期决定的。
我自己的代码与这份代码已经具有85%的相似性了,但是就是输出不对。
首先提前反思下,这个操作很有必要吗?正常来说,我们都应该是在开发分支上开发, 然后验证 最后合入到 master,最后再git push.
我用到这个场景的原因是:我的博客在好几个平台上都有,自己的云主机、公司的工作站或者 还有自己的笔记本上偶尔也有git push。这东西一多的问题就是:master分支经常忘记git pull. 这样导致如果直接在master分支上开发,下载下来的代码经常冲突。
git checkout -b dev_br
git push origin dev_br:master