Visual Studio Code + LLDB: Remote Debugging on Android Devices

Visual Studio Code + LLDB: Remote Debugging on Android Devices

  • [1. Android NDK: android-ndk-r27d-linux.zip](#1. Android NDK: android-ndk-r27d-linux.zip)
    • [1.1. `lldb-server` and `lldb`](#1.1. lldb-server and lldb)
  • References

1. Android NDK: android-ndk-r27d-linux.zip

Android NDK

https://developer.android.com/ndk

NDK Downloads

https://developer.android.com/ndk/downloads

NDK Wiki

https://github.com/android/ndk/wiki

The Android Native Development Kit (NDK) provides a cross-compiling tool for compiling code written in C/C++ can be compiled to ARM, or x86 native code (or their 64-bit variants) for Android. The NDK uses the Clang compiler to compile C/C++. GCC was included until NDK r17, but removed in r18 in 2018.

Android Native Development Kit (Android NDK) 是一个交叉编译器,可以将 C/C++ 编写的代码编译为运行 Android 的 ARM 或 x86 的本地代码。NDK 使用 Clang 来编译C/C++。

  1. 下载并解压 android-ndk-r27d-linux.zip

wget https://dl.google.com/android/repository/android-ndk-r27d-linux.zip --no-check-certificate

复制代码
(base) yongqiang@yongqiang:~/software$ wget https://dl.google.com/android/repository/android-ndk-r27d-linux.zip --no-check-certificate
(base) yongqiang@yongqiang:~/software$ chmod a+x android-ndk-r27d-linux.zip
(base) yongqiang@yongqiang:~/software$ unzip android-ndk-r27d-linux.zip
(base) yongqiang@yongqiang:~/software$ rm -rf android-ndk-r27d-linux.zip
(base) yongqiang@yongqiang:~/software$ cd android-ndk-r27d/
(base) yongqiang@yongqiang:~/software/android-ndk-r27d$ pwd
/home/yongqiang/software/android-ndk-r27d
(base) yongqiang@yongqiang:~/software/android-ndk-r27d$
  1. 为当前用户永久配置环境变量

~/.bashrc

复制代码
(base) yongqiang@yongqiang:~$ vim ~/.bashrc
...
export NDKROOT=/home/yongqiang/software/android-ndk-r27d
export PATH=${NDKROOT}:${PATH}
...
(base) yongqiang@yongqiang:~$ source ~/.bashrc
  1. 为所有用户永久配置环境变量

/etc/profile

复制代码
(base) yongqiang@yongqiang:~$ vim /etc/profile
...
export NDKROOT=/home/yongqiang/software/android-ndk-r27d
export PATH=${NDKROOT}:${PATH}
...
(base) yongqiang@yongqiang:~$ source /etc/profile

/etc/profile 是用户登录后自动执行的第一个 shell 脚本,用来设置全局的环境变量,对所有用户有效。执行完 /etc/profile 脚本后,才回去执行用户自己的环境脚本,如 ~/.bashrc 等。

  1. 验证 NDK 安装

ndk-build -v

复制代码
(base) yongqiang@yongqiang:~$ ndk-build -v
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
(base) yongqiang@yongqiang:~$ echo ${PATH}
/home/yongqiang/software/android-ndk-r27d:/home/yongqiang/software/android-ndk-r27d:/home/yongqiang/miniconda3/bin:/home/yongqiang/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu_2404.1.68.0_x64__79rhkp1fndgsc:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/iCLS:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/iCLS:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Windows/System32/OpenSSH:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/Program Files/Intel/WiFi/bin:/mnt/c/Program Files/Common Files/Intel/WirelessCommon:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0:/mnt/c/WINDOWS/System32/OpenSSH:/mnt/e/software/platform-tools:/mnt/c/Users/cheng/AppData/Local/Microsoft/WindowsApps:/snap/bin:/home/yongqiang/software/platform-tools:/home/yongqiang/software/platform-tools:/home/yongqiang/software/platform-tools/
(base) yongqiang@yongqiang:~$

1.1. lldb-server and lldb

复制代码
(base) yongqiang@yongqiang:~/software$ find android-ndk-r27d/ -name "lldb-server"
android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/18/lib/linux/i386/lldb-server
android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/18/lib/linux/riscv64/lldb-server
android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/18/lib/linux/x86_64/lldb-server
android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/18/lib/linux/arm/lldb-server
android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/18/lib/linux/aarch64/lldb-server
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ find android-ndk-r27d/ -name "lldb"
android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/lib/python3.11/site-packages/lldb
android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/bin/lldb
(base) yongqiang@yongqiang:~/software$

References

1 Yongqiang Cheng (程永强), https://yongqiang.blog.csdn.net/

2 LLDB Remote Debugging, https://www.hidka.com/blog/lldb-server/

3 Android LLDB debugging, https://iree.dev/developers/debugging/android-with-lldb/

4 Debugging using VSCode and lldb on a remote machine or VM, https://bpfman.io/main/developer-guide/debugging/

5 LLDB Debugger, https://gitee.com/openharmony/docs/blob/1669e5eb1b136256c9dc0b6a519005f2a708f6ac/en/application-dev/napi/debug-lldb.md