Search code examples
androidlinux-kernelkernelandroid-source

android version with kernel debug symbols


I'm trying to generate a custom android image, to run under the emulator, using a kernel version compiled by myself (the idea is to include debug symbols). I found a lot of information on google about it so I checked out the branch android-goldfish-4.14-dev, and tried to compile it a couple of times, but this crash happens any time I run the compiled kernel.

[    0.851597]  ? generic_make_request+0x123/0x300
[    0.852314]  submit_bio+0x73/0x140
[    0.852854]  ? bio_alloc_bioset+0xcc/0x1e0
[    0.853504]  ? submit_bio+0x73/0x140
[    0.854052]  ? guard_bio_eod+0x2c/0xf0
[    0.854622]  submit_bh_wbc+0x180/0x1b0
[    0.855195]  __bread_gfp+0x54/0xe0
[    0.855744]  ext4_fill_super+0x1f6/0x3a10
[    0.856377]  ? vsnprintf+0x24f/0x4e0
[    0.856943]  ? down_write+0x12/0x40
[    0.857497]  ? snprintf+0x45/0x70
[    0.858021]  mount_bdev+0x17f/0x1b0
[    0.858572]  ? mount_bdev+0x17f/0x1b0
[    0.859149]  ? ext4_calculate_overhead+0x490/0x490
[    0.859896]  ext4_mount+0x15/0x20
[    0.860420]  mount_fs+0x155/0x180
[    0.860942]  ? alloc_vfsmnt+0x1bb/0x230
[    0.861547]  vfs_kern_mount.part.23+0x80/0x150
[    0.862240]  do_mount+0x5ea/0xd20
[    0.862764]  ? memdup_user+0x4f/0x80
[    0.863329]  SyS_mount+0x98/0xe0
[    0.863842]  mount_block_root+0x109/0x2da
[    0.864478]  ? set_debug_rodata+0x17/0x17
[    0.865107]  mount_root+0x6a/0x6d
[    0.865634]  prepare_namespace+0x13e/0x176
[    0.866287]  kernel_init_freeable+0x224/0x251
[    0.866971]  ? rest_init+0xb0/0xb0
[    0.867507]  kernel_init+0xe/0x101
[    0.868045]  ret_from_fork+0x35/0x40

I'm trying to compile it for running under qemu x86.

Another possibility can be get an android oreo image with the kernel debug symbols; do you know if there are precompiled images with kernel debug symbols?

Thanks!


Solution

  • If the problem is actually in kernel, below are instructions how to build it from sources.

    1. Obtain kernel sources

    First of all, you need to figure out the device which you want to build the kernel for. Knowing the device name, you can clone correct Android kernel sources for it and build it. For example, for goldfish you should use kernel from:

    https://android.googlesource.com/kernel/goldfish/

    If you're not sure which kernel to use for your device, try to use so called "Android common kernel":

    https://android.googlesource.com/kernel/common/

    Basically it works like this:

    • Android common kernel is based on regular Linux kernel, adding some Android-specific patches on top of it
    • Android kernels for specific devices are based on common kernel, adding some device-specific patches on top of it.

    Now that you "git cloned" the kernel, checkout to version branch you want to use:

    $ git checkout android-4.14
    

    2. Configure toolchain

    I assume you want to build kernel for x86_64 architecture. If so, configure your toolchain like this:

    $ export PATH=$AOSP_DIR/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/bin:$PATH
    $ export CROSS_COMPILE=x86_64-linux-androidkernel-
    $ export ARCH=x86_64
    

    where $AOSP_DIR -- path to your AOSP sources. If you don't have AOSP sources, you can obtain the toolchain separately (outside of kernel sources directory):

    $ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9
    

    3. Obtain Android kernel configs

    Download Android kernel configs (outside of your kernel directory):

    $ git clone https://android.googlesource.com/kernel/configs android-kernel-configs
    

    4. Configure and build the kernel

    Now, in your kernel source code directory, you can create kernel configuration (.config file) using corresponding defconfig file and Android config fragments. For example, for goldfish you should use this command:

    $ ./scripts/kconfig/merge_config.sh \
        arch/x86/configs/x86_64_ranchu_defconfig \
        ../android-kernel-configs/android-4.14/android-base.config \
        ../android-kernel-configs/android-4.14/android-recommended.config \
        ../android-kernel-configs/android-4.14/android-recommended-x86.config
    

    Now .config file is generated. At this point you may want to run make menuconfig and modify the kernel configuration for your needs (e.g. enable some debug options, etc).

    Build the kernel:

    $ make -j4
    

    It should built fine, but I didn't test it (neither the building, nor the running in the emulator). So if you can verify if those instructions work, please provide your comments.