2013年12月1日 星期日

Android studio Eclipse jni C/C++ debug

Eclipse jni C/C++ debug provide breakpoint, watchpoint, watch value, memory value,
Arm register value, disassembly code.

Please refer this first :
http://fatalfeel.blogspot.com/2013/09/debug-android-jni-with-eclipse-gui-on.html

1. Install Ubuntu 16.04 x64 and openjdk 8
sudo apt install openjdk-8-jdk openjdk-8-jre
#check version
root@homelinux:~/CardView/Application/src/main# java -version
openjdk version "1.8.0_222"
root@homelinux:~/CardView/Application/src/main# javac -version
javac 1.8.0_222
root@homelinux:~/CardView/Application/src/main# javadoc -J-version
openjdk version "1.8.0_222"

2. Eclipse dependency
sudo apt install gtk2.0 libgtk2.0-dev

3. Android Studio 3.6.2 install to /opt/android-studio

4. Eclipse Luna SR2  in /opt/eclipse_luna
After CDT 9.x.x/Neon, the [Legacy Create Process Launcher] will be completely removed. Using Kepler, Luna, Mars, are OK.

(a) gedit /opt/eclipse_xxx/eclipse.ini
-XX:PermSize=64m
-XX:MaxPermSize=256m
-Xms256m
-Xmx2048m

(b) Eclipse Mars 2 Disassembly window mess fixed method
gedit /opt/eclipse_mars/eclipse.ini
#before line
--launcher.appendVmargs
#add
--launcher.GTK_version
2

Refer to:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=491384

5. android-sdk_r24.4.1-linux.tgz extract to  /opt/android-sdk-linux
https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz

6.  tools_r25.2.5-linux.zip extract to /opt/android-sdk-linux/tools
https://dl.google.com/android/repository/tools_r25.2.5-linux.zip

7. platform-tools_r25.0.6-linux.zip extarct to /opt/android-sdk-linux/platform-tools
dl.google.com/android/repository/platform-tools_r25.0.6-linux.zip

8. android-ndk-r19c-linux-x86_64.zip extract to /opt/android-ndk
https://developer.android.com/ndk/downloads/older_releases

9. install ADT-23.0.7  to Eclipse Luna SR2
https://dl.google.com/android/ADT-23.0.7.zip
Eclipse Luna SR2 -> window -> Perferences -> android
sdk location:/opt/android-sdk-linux
ndk location:/opt/android-ndk

10. install CDT in Eclipse page
Eclipse Luna SR2 -> help -> install new software
http://download.eclipse.org/releases/luna

Programming Languages->
#enable
programming -> c++ development tools
programming -> c++ development tools sdk

11. Eclipse Luna SR2
Window -> Preferences -> C/C++ -> Code Analysis
disable [Syntax and Semantic Errors]

12. Eclipse Luna SR2 [Java Perspective]
windows->Android SDK Manager
#enable, use  28.0.3 only but dx.jar need match with tools_r25.2.5-linux.zip
Android SDK Build-Tools 25.0.3
Android SDK Build-Tools 28.0.3

cp /opt/android-sdk-linux/build-tools/25.0.3/lib/dx.jar /opt/android-sdk-linux/build-tools/28.0.3/lib/dx.jar

#disable
Android SDK Build-Tools 25.0.3

#enable
SDK Platform Android 9, API 28

13. gedit /etc/environment
add path /opt/android-ndk
retoot system

14. NDK script ndk-gdb-eclipse point to ndk-gdb-eclipse.py
(a) gedit /opt/android-ndk/ndk-gdb-eclipse
#!/bin/bash
function TrapHandler()
{
    echo "$0 exit"
}
trap TrapHandler SIGTSTP
DIR="$(cd "$(dirname "$0")" && pwd)"
$DIR/prebuilt/linux-x86_64/bin/ndk-gdb-eclipse "$@"

(b) gedit /opt/android-ndk/prebuilt/linux-x86_64/bin/ndk-gdb-eclipse
#!/bin/bash
function TrapHandler()
{
    echo "$0 exit"
}
trap TrapHandler SIGTSTP
NDK_BIN_DIR=$(dirname $0)
${NDK_BIN_DIR}/python ${NDK_BIN_DIR}/ndk-gdb-eclipse.py $@

15. gedit /opt/android-ndk/prebuilt/linux-x86_64/bin/ndk-gdb-eclipse.py
(a) in function: def generate_gdb_script
#after
gdb_commands += "set solib-search-path {}\n".format(solib_search_path)
#add
print(gdb_commands)

(b)before function def main()
#add
def SignalHandler(signum, frame):
    print("\npython exit number =", signum)

(c)in function def main()
#comment
gdbrunner.start_gdb(gdb_path, gdb_commands, gdb_flags)
to
#gdbrunner.start_gdb(gdb_path, gdb_commands, gdb_flags)

#after comment line add
print("press ctrl+z to exit\n")
signal.signal(signal.SIGTSTP, SignalHandler)
signal.pause()

patch:
r20b~
http://www.mediafire.com/file/5cf5vhxi1wbktx2/ndk-gdb-eclipse%2528r20b%2529.py
r19c~
http://www.mediafire.com/file/863hsngow61rntv/ndk-gdb-eclipse%2528r19c%2529.py

16. Android Studio edit /root/CardView/Application/build.gradle
sourceSets
{
    main
    {
        dirs.each
        {
            dir ->
            java.srcDirs "src/${dir}/java"
            res.srcDirs "src/${dir}/res"
        }

        /*begin: add this for jni debug, by stone*/
        jni.srcDirs = []
        jniLibs.srcDirs = ['src/main/libs']
        /*end: add this for jni debug, by stone*/
    }
}

/*begin: add this for jni debug, by stone*/
buildTypes
{
    debug
    {
        jniDebuggable true
    }
}
/*end: add this for jni debug, by stone*/

full source:
http://www.mediafire.com/file/ectduimk9jdzyfw/CardView_JNI.tar.gz

17. Eclipse Luna SR2
File -> New -> Project -> C/C++ -> C++ Project
Project name: CardView_C++
Location: /root/CardView/Application/src/main
Project Type: Makefile Project -> Empty Project
Toolchains:  Android GCC
compile CardView_C++

18. Android Studio
build CardView Project and install to Target Board

19. Open Terminal
cd  /root/CardView/Application/src/main
ndk-gdb --launch --verbose
#then
(gdb) #press q
(gdb) #press y

#will generate
/root/CardView/Application/src/main/obj/local/armeabi-v7a/system/bin/app_process

20. cp /root/CardView/Application/src/main/libs/armeabi-v7a/gdb.setup /root/CardView/Application/src/main/obj/local/armeabi-v7a/gdb-eclipse.setup

21.  Android Studio
Set breakpoint in super.onCreate(savedInstanceState);
CardView run debug mode

22. When stop at super.onCreate(savedInstanceState);
Terminal at /root/CardView/Application/src/main
#run
ndk-gdb-eclipse
#show
set osabi GNU/Linux
file '/root/CardView/Application/src/main/obj/local/armeabi-v7a/system/bin/app_process'
set solib-absolute-prefix /root/CardView/Application/src/main/obj/local/armeabi-v7a
set solib-search-path /root/CardView/Application/src/main/obj/local/armeabi-v7a:/root/CardView/Application/src/main/obj/local/armeabi-v7a/system/bin:/root/CardView/Application/src/main/obj/local/armeabi-v7a/system/lib

gedit /root/CardView/Application/src/main/obj/local/armeabi-v7a/gdb-eclipse.setup
#keep line
directory /opt/android-ndk/toolchains/llvm... ... ...
#remove others

gedit /root/CardView/Application/src/main/obj/local/armeabi-v7a/gdb-eclipse.setup
#paste follows before directory /opt/android-ndk/toolchains/llvm... ... ...
set osabi GNU/Linux
file '/root/CardView/Application/src/main/obj/local/armeabi-v7a/system/bin/app_process'
set solib-absolute-prefix /root/CardView/Application/src/main/obj/local/armeabi-v7a
set solib-search-path /root/CardView/Application/src/main/obj/local/armeabi-v7a:/root/CardView/Application/src/main/obj/local/armeabi-v7a/system/bin:/root/CardView/Application/src/main/obj/local/armeabi-v7a/system/lib

example:
http://www.mediafire.com/file/ibilatv57gdcvmb/gdb-eclipse.setup

23. Eclipse Luna SR2
(1)right click project
(2)Debug As->Debug configurations, double click [C/C++ Application]
(3) click bottom line -Select other...
enable [Use configuration specific settings]
select [Legacy Create Process Launcher]

(4)[Main] > [C/C++ Application:] /root/CardView/Application/src/main/obj/local/armeabi-v7a/system/bin/app_process
disable auto build
(5)[Debugger:] GdbServer
(6)[Stop on starup at:] Java_com_example_android_cardview_CardViewActivity_nativeOnStart
(7)[GDB debugger:] /opt/android-ndk/prebuilt/linux-x86_64/bin/gdb
(8)[GDB command file:] /root/CardView/Application/src/main/obj/local/armeabi-v7a/gdb-eclipse.setup
(9)[Verbose console mode] enable it for tracing running content
(10)[Connection] > [Type:] TCP
[Host name or Ip address:] localhost
[Port number:] 5039

24. Android Studio
Set breakpoint in super.onCreate(savedInstanceState);
CardView run debug mode
When stop at super.onCreate(savedInstanceState);
open Terminal at /root/CardView/Application/src/main
#run
ndk-gdb-eclipse

goto Eclipse Luna SR2
Debug As->Debug configurations
[C/C++ Application] -> CardView_C++
Debug

Android Studio debug continue.
You can see Eclipse c++ breakpoint stop.

#all done

25. (option) Eclipse easy way to run ndk-gdb-eclipse
Run -> External Tools -> External Tools configurations...
double click Program
a. (Main tab)
[Name:] ndk-gdb-eclipse
[Location:] /opt/android-ndk/ndk-gdb-eclipse
[Working Directory:] ${workspace_loc:/CardView_C++}
b. (Build tab)
#disable->
Build before launch

26. (trick) How to do when Disassembly window freeze
click minimize window then maximize window
then continue c/c++ debug

Demo pics:
http://www.mediafire.com/file/yth31ui3pjsjxsz/jni_00.png
http://www.mediafire.com/file/wszvt4rrj5038c3/jni_01.png
http://www.mediafire.com/file/ohl5kudx44br4na/jni_02.png

沒有留言:

張貼留言