Android 核心使用 kgdb

  • android source code
https://android.googlesource.com/platform/manifest/
mkdir android-5.1.1_r9
cd android-5.1.1_r9
repo init -u https://android.googlesource.com/platform/manifest -b android-5.1.1_r9
repo sync -j8
  • checkout goldfish 源碼: 模擬器使用的kernel 是 goldfish
git clone https://android.googlesource.com/kernel/goldfish.git kernel
cd kernel
git checkout -t origin/android-goldfish-3.4 -b goldfish3.4
CONFIG_HIGHMEM=y 打開這個選項後,啟動模擬器時 emulator -memory
參數才能發揮作用,否則模擬器的內存總是700多M
CONFIG_DEBUG_KERNEL=y 打開這個選項後,vmlinux 才有符號
CONFIG_KGDB=y 開啟kgdb
CONFIG_DEBUG_INFO=y
關閉Linux內核優化比較麻煩.我通過和朋友討論,以及網絡搜索還沒有找到很好的解決辦法,原因是默認的Linux內核
編譯是開啟-O2優化的,這種模式之下會造成gdb和實際的源碼對不上,相信使用過windbg調試-O2的朋友都有這個經歷
,所以我們需要關閉Linux的-O2,不過目前還沒有很好的解決辦法下面這篇文章討論的解決辦法是.針對文件進行關閉
優化.下面這兩篇文章的討論都非常有意義:

http://www.lenky.info/archives/2013/03/2238

http://www.ibm.com/developerworks/cn/linux/l-kdb/

這邊我們將-Os 和-O2都調成-O.針對具體文件關閉優化,這邊就不搞了.具體到自己的調試任務的時候再看.
ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
KBUILD_CFLAGS    += -Os
else
KBUILD_CFLAGS    += -O2
endif

ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
KBUILD_CFLAGS    += -O
else
KBUILD_CFLAGS    += -O
endif
#!/bin/bash

TOP=`pwd`

usage() {
cat <<USAGE

Usage:
    bash $0 <TARGET_PRODUCT> [OPTIONS]

Description:
    Builds Android tree for given TARGET_PRODUCT

OPTIONS:
    -c, --clean_build
        Clean build - build from scratch by removing entire out dir

    -d, --debug
        Enable debugging - captures all commands while doing the build

    -h, --help
        Display this help message

    -i, --image
        Specify image to be build/re-build (bootimg/sysimg/usrimg)

    -j, --jobs
        Specifies the number of jobs to run simultaneously (Default: 8)

    -k, --kernel_defconf
        Specify defconf file to be used for compiling Kernel

    -l, --log_file
        Log file to store build logs (Default: <TARGET_PRODUCT>.log)

    -m, --module
        Module to be build

    -p, --project
        Project to be build

    -s, --setup_ccache
        Set CCACHE for faster incremental builds (true/false - Default: true)

    -u, --update-api
        Update APIs

    -v, --build_variant
        Build variant (Default: userdebug)

USAGE
}

buid_kernel() {
    export PATH=$TOP/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin:$PATH
    export ARCH=arm
    export SUBARCH=arm
    export CROSS_COMPILE=arm-eabi-
    make -C kernel goldfish_armv7_defconfig
    sed -i 's/.*CONFIG_HIGHMEM.*/CONFIG_HIGHMEM=y/' kernel/.config
    sed -i 's/.*CONFIG_DEBUG_KERNEL.*/CONFIG_DEBUG_KERNEL=y/' kernel/.config
    sed -i 's/.*CONFIG_KGDB.*/CONFIG_KGDB=y/' kernel/.config
    sed -i 's/.*CONFIG_DEBUG_INFO.*/CONFIG_DEBUG_INFO=y/' kernel/.config
    yes '' | make -C kernel oldconfig
    make -C kernel clean
    time  make -C kernel  V=1 -k -j8 2>&1 | tee kernel_build.log
}

build_env() {
    source $TOP/build/envsetup.sh;
    lunch aosp_arm-eng;
}

debug_emulator() {
    build_env
    emulator  -no-audio -verbose -show-kernel -kernel kernel/arch/arm/boot/zImage -memory 2048 -qemu -s -S
}

run_emulator() {
    build_env
    emulator -kernel kernel/arch/arm/boot/zImage
}

while true; do
    case "$1" in
        -h|--help) usage; exit 0;;
        -k|--kernel) buid_kernel; exit 0;;
        -e|--env) build_env; exit 0;;
        -r|--run) run_emulator; exit 0;;
        -d|--debug) debug_emulator; exit 0;;
        --) shift; break;;
    esac
    shift
done
  • build.env
source build/envsetup.sh;
lunch aosp_arm-eng;
source build.env
  • gdbinit
target remote localhost:1234
b *start_kernel
arm-linux-androideabi-gdb kernel/vmlinux
source gdbinit