[android-kernel环境搭建]emulator_kernel

2021/7/10 6:06:02

本文主要是介绍[android-kernel环境搭建]emulator_kernel,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 概述
    • 1. emulator内核编译
    • 2. 内核gdb调试
    • 3. vscode的gdb调试
  • 源码解析
    • 1. 编译mk文件
      • 1.1 sdk_phone_x86文件
      • 1.2 aosp_x86文件
      • 1.3 x86-vendor文件
  • 补充
  • 问题
    • 1. 编译的kernel,替换之后Android系统起不来
    • 2. #error New address family defined, please update secclass_map.
  • 参考

概述

编译emulator使用的qemu的kernel,调试内核

1. emulator内核编译

# 下载内核
git clone https://android.googlesource.com/kernel/goldfish.git

# 切换分支,android-10.0.0_r13 tag对应4.14.112内核
git checkout -b android-goldfish-4.14-gchips remotes/origin/android-goldfish-4.14-gchips

# 拷贝编译脚本到goldfish目录中
cp ~/android_aosp/aosp/prebuilts/qemu-kernel/build-kernel.sh build-kernel.sh

# 修改编译脚本的CROSS_COMPILE和CONFIG宏
CROSS_COMPILE=/home/jetson/android_aosp/aosp/prebuilts/qemu-kernel/kernel-toolchain/android-kernel-toolchain-
CONFIG=x86_64_ranchu

# 编译内核
./build-kernel.sh --arch=x86_64

# 拷贝内核
cp /tmp/kernel-qemu/x86_64-4.14.112/kernel-qemu /home/jetson/android_aosp/aosp/out/target/product/generic_x86/kernel-ranchu-64

2. 内核gdb调试

# 修改defconfig
make ARCH=x86_64 menuconfig

# 确认配置打开
CONFIG_DEBUG_KERNEL=y
CONFIG_DEBUG_INFO=y
CONFIG_FRAME_POINTER=y
CONFIG_KGDB=y 
CONFIG_DEBUG_RODATA=n
CONFIG_RANDOMIZE_BASE=n
CONFIG_GDB_SCRIPTS=y
CONFIG_DEBUG_INFO_REDUCED=n

# 保存配置
make savedefconfig
mv -f defconfig arch/x86/configs/x86_64_ranchu_defconfig

# 编译
./build-kernel.sh --arch=x86_64

# 添加 emulator 命令
-qemu -s	打开gdbserver
-s              shorthand for -gdb tcp::1234
emulator -verbose -show-kernel -qemu -s

# 修改/etc/profile文件,添加环境变量
export PATH=/home/jetson/android_aosp/aosp/prebuilts/gdb/linux-x86/bin:$PATH

# gdb调试
gdb vmlinux

3. vscode的gdb调试

添加/home/jetson/android_aosp/goldfish/.vscode/launch.json文件

运行gdb调试

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "kernel-debug",
            "type": "cppdbg",
            "request": "launch",
            "miDebuggerServerAddress": "127.0.0.1:1234",
            "program": "/home/jetson/android_aosp/goldfish/vmlinux",
            "args": [],
            "stopAtEntry": false,
            "cwd": "/home/jetson/android_aosp/goldfish/",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

源码解析

从mk文件中可以知道sdk_phone_x86使用的kernel是prebuilts/qemu-kernel/x86_64/4.14/kernel-qemu2

1. 编译mk文件

aosp/build/make/target/product/sdk_phone_x86.mk

1.1 sdk_phone_x86文件

# 这里包含kernel相关
$(call inherit-product, $(SRC_TARGET_DIR)/product/aosp_x86.mk)

# Define the host tools and libs that are parts of the SDK.
-include sdk/build/product_sdk.mk
-include development/build/product_sdk.mk

# Overrides
PRODUCT_BRAND := Android
PRODUCT_NAME := sdk_phone_x86
PRODUCT_DEVICE := generic_x86
PRODUCT_MODEL := Android SDK built for x86

1.2 aosp_x86文件

aosp/build/make/target/product/aosp_x86.mk

PRODUCT_USE_DYNAMIC_PARTITIONS := true

# The system image of aosp_x86-userdebug is a GSI for the devices with:
# - x86 32 bits user space
# - 64 bits binder interface
# - system-as-root
# - VNDK enforcement
# - compatible property override enabled

# GSI for system/product
$(call inherit-product, $(SRC_TARGET_DIR)/product/gsi_common.mk)

# Emulator for vendor
# 这里包含kernel文件
$(call inherit-product-if-exists, device/generic/goldfish/x86-vendor.mk)
$(call inherit-product, $(SRC_TARGET_DIR)/product/emulator_vendor.mk)
$(call inherit-product, $(SRC_TARGET_DIR)/board/generic_x86/device.mk)

# Enable mainline checking for excat this product name
ifeq (aosp_x86,$(TARGET_PRODUCT))
PRODUCT_ENFORCE_ARTIFACT_PATH_REQUIREMENTS := relaxed
endif

PRODUCT_NAME := aosp_x86
PRODUCT_DEVICE := generic_x86
PRODUCT_BRAND := Android
PRODUCT_MODEL := AOSP on x86

1.3 x86-vendor文件

aosp/device/generic/goldfish/x86-vendor.mk

PRODUCT_KERNEL_VERSION := 4.14

PRODUCT_PROPERTY_OVERRIDES += \
       vendor.rild.libpath=/vendor/lib/libgoldfish-ril.so

# This is a build configuration for a full-featured build of the
# Open-Source part of the tree. It's geared toward a US-centric
# build quite specifically for the emulator, and might not be
# entirely appropriate to inherit from for on-device configurations.
PRODUCT_COPY_FILES += \
    device/generic/goldfish/data/etc/advancedFeatures.ini:advancedFeatures.ini \
    device/generic/goldfish/data/etc/encryptionkey.img:encryptionkey.img \
    # 就是这个kernel了
    prebuilts/qemu-kernel/x86_64/$(PRODUCT_KERNEL_VERSION)/kernel-qemu2:kernel-ranchu-64
PRODUCT_SDK_ADDON_COPY_FILES += \
    device/generic/goldfish/data/etc/advancedFeatures.ini:images/x86/advancedFeatures.ini \
    device/generic/goldfish/data/etc/encryptionkey.img:images/x86/encryptionkey.img \
    prebuilts/qemu-kernel/x86_64/$(PRODUCT_KERNEL_VERSION)/kernel-qemu2:images/x86/kernel-ranchu-64

PRODUCT_SHIPPING_API_LEVEL := 28
TARGET_USES_MKE2FS := true

补充

问题

1. 编译的kernel,替换之后Android系统起不来

切换分支:git checkout -b android-goldfish-4.14-gchips remotes/origin/android-goldfish-4.14-gchips

2. #error New address family defined, please update secclass_map.

In file included from scripts/selinux/mdp/mdp.c:49:
./security/selinux/include/classmap.h:247:2: error: #error New address family defined, please update secclass_map.
  247 | #error New address family defined, please update secclass_map.
      |  ^~~~~
In file included from scripts/selinux/genheaders/genheaders.c:19:
./security/selinux/include/classmap.h:247:2: error: #error New address family defined, please update secclass_map.
  247 | #error New address family defined, please update secclass_map.
      |  ^~~~~
make[3]: *** [scripts/Makefile.host:102: scripts/selinux/mdp/mdp] Error 1
make[3]: *** [scripts/Makefile.host:102: scripts/selinux/genheaders/genheaders] Error 1
make[2]: *** [scripts/Makefile.build:671: scripts/selinux/mdp] Error 2

diff --git a/scripts/selinux/genheaders/genheaders.c b/scripts/selinux/genheaders/genheaders.c
index fa48fabcb330..3cc4893d98cc 100644
--- a/scripts/selinux/genheaders/genheaders.c
+++ b/scripts/selinux/genheaders/genheaders.c
@@ -9,7 +9,6 @@
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
-#include <sys/socket.h>
 
 struct security_class_mapping {
        const char *name;
diff --git a/scripts/selinux/mdp/mdp.c b/scripts/selinux/mdp/mdp.c
index ffe8179f5d41..c29fa4a6228d 100644
--- a/scripts/selinux/mdp/mdp.c
+++ b/scripts/selinux/mdp/mdp.c
@@ -32,7 +32,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <sys/socket.h>
 
 static void usage(char *name)
 {
diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
index acdee7795297..5ae315ab060b 100644
--- a/security/selinux/include/classmap.h
+++ b/security/selinux/include/classmap.h
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #include <linux/capability.h>
+#include <linux/socket.h>
 
 #define COMMON_FILE_SOCK_PERMS "ioctl", "read", "write", "create", \
     "getattr", "setattr", "lock", "relabelfrom", "relabelto", "append", "map"

参考

1. 使用Android模拟器调试linux内核
https://blog.csdn.net/zhangjg_blog/article/details/84291663
2. Debugging kernel and modules via gdb
https://android.googlesource.com/kernel/hikey-linaro/+/master/Documentation/gdb-kernel-debugging.txt


这篇关于[android-kernel环境搭建]emulator_kernel的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程