[android-kernel環境搭建]emulator_kernel


概述

編譯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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM