gdbgdbserverandroid-debugging

build gdb and gdbserver for android


I'm working on 64 bit linux, need to build gdbserver for my aarch64 Android phone.There is prebuilt gdbserver in NDK, but it uses the python in NDK package, not using my system python, I can't install other python plugins.

How to find which --target and --host parameter is required for ./configure? I tried the --help, and google like "build gdbserver aarch64" or "gdbserver configure android", but did't find any answer for aarch64 Android.

For gdb I can use ./configure --enable-targets=all, but what for gdbserver? Is there any "List" for all the available parameters?

Here's how I tried to build gdbserver

  1. downloaded the gcc-arm-none-eabi-9-2019-q4-major-x86_64-linux package, decompress and add it to PATH, add environment variable CC=arm-none-eabi-gcc, CXX=arm-none-eabi-g++, make the executable available in PATH
  2. I tried all of these:
    ../configure  
    ../configure --host=aarch64-linux             --target=aarch64-linux-androideabi
    ../configure --host=aarch64-linux-androideabi --target=aarch64-linux
    ../configure --host=aarch64-linux-androideabi --target=aarch64-linux-androideabi
  1. make, which results in:
    ...
    make[3]: Nothing to be done for 'all'.
    make[3]: Leaving directory '/opt/gdb-9.1/gdb/gdbserver/build/build-libiberty-gdbserver/testsuite'
    make[2]: Leaving directory '/opt/gdb-9.1/gdb/gdbserver/build/build-libiberty-gdbserver'
    make[1]: Leaving directory '/opt/gdb-9.1/gdb/gdbserver/build'
    make: *** No rule to make target '../alloc.c'.  Stop.
also tried:
    make CC=arm-none-eabi-gcc CXX=arm-none-eabi-g++
    or CC=aarch64-linux-android28-clang CXX=aarch64-linux-android28-clang++
But same result.

Solution

  • Got answer from somewhere else, put here in case who wants do the same.

    1. Download gdb source code:
       wget ftp://sourceware.org/pub/gdb/releases/gdb-9.1.tar.gz
    
    1. Extract file:
     tar xzvf gdb-9.1.tar.gz
    
    1. Move into source folder
     cd gdb-9.1
    
    1. Edit file gdb/gdbserver/linux-low.c:
    #define HAVE_ELF32_AUXV_T  //  Line 107 (Added)
    #ifndef HAVE_ELF32_AUXV_T
    
    #define HAVE_ELF64_AUXV_T // Line 122 (Added)
    #ifndef HAVE_ELF64_AUXV_T
    

    This modification is neccessary to build Android, since Android system libraries already define struct Elf32_auxv_t and Elf64_auxv_t .(Please see this for detail: https://github.com/android/ndk/issues/1008)

    static void
    linux_request_interrupt (void)
     {
       /* .... */
    -  kill (-signal_pid, SIGINT); // replace this line with next 3 lines
    +  int r = kill (-signal_pid, SIGINT);
    +  if (r != 0)
    +      kill (signal_pid, SIGINT);
     }
    

    This fixes bug "gdbserver not handling Ctrl+C", detail at: https://sourceware.org/bugzilla/show_bug.cgi?id=18772

    1. Build gdb for linux:
    sudo apt-get install build-essential \
      gcc g++ make autogen m4 \
      bison gettext libpython-dev
    
    mkdir build-linux
    
    cd build-linux/
    
    ../configure --enable-targets=all --with-python=/usr/bin/python
    
    make -j4
    
    sudo make install
    
    1. Build gdbserver for android:
    cd ~
    
    mkdir android
    
    cd android
    
    wget https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip
    
    unzip commandlinetools-linux-6200805_latest.zip
    
    export PATH=$PATH:~/android/tools/bin
    
    sdkmanager --install "ndk;21.0.6113669" --sdk_root=.
    
    cd ~/android/ndk/21.0.6113669/
    
    ./build/tools/make-standalone-toolchain.sh \ 
    --toolchain=aarch64-linux-android-4.9 \
    --install-dir=~/android/ndk_21
    

    This step create the standalone toolchain at: ~/android/ndk_21

    cd ~/gdb-9.1
    
    mkdir build-android
    
    cd build-android
    
    export PATH=$PATH:~/android/ndk_21/bin
    
    CC=aarch64-linux-android-gcc ../configure \
        --target=aarch64-linux-android \
        --host=aarch64-linux-android \
         LDFLAGS="-static-libstdc++"
    
    make -j4
    

    If get error related to "source-highlight", add --disable-source-highlight to the configure flag.

    After build finishes, gdbserver is located at: gdb/gdbserver/gdbserver