SlideShare a Scribd company logo
Android GDB Debug
William.L
wiliwe@gmail.com
2010-11-16
Environment -
1. Ubuntu 9.10
2. Android 2.2 (Froyo)
3. nVidia T250 Ventana board
Introduction
•Depends on the Android tool : Android Debug Bridge (ADB)
•Android 2.2(Froyo)源碼目錄中,prebuild 目錄下有現成的 GDB 程式(目前使用的 ARM EABI 版
本為 4.4.0)
• AndroidFroyoSrc /prebuilt/linux‐‐‐‐x86/toolchain/arm‐‐‐‐eabi‐‐‐‐4.4.0/bin/arm‐‐‐‐eabi‐‐‐‐gdb
• ARM EABI GDB 用法 : arm‐‐‐‐eabi‐‐‐‐gdb ExecutableWithDebugSymbol
• ExecutableWithDebugSymbol 位在目錄 out/target/product/ventana/symbols/system/bin/
I) GDB Server 端(Android 裝置)使用步驟
• 使用 adb shell 登入遠端 Android 裝置
• 裝置後執行指令
# gdbserver :5039 /data/Executable & 或
# gdbserver :5039 ‐‐‐‐‐‐‐‐attach ExecutablePID &
• Port number 5039, 可自行替換其它埠號, 但不可跟其它網路服務使用的埠號衝突!
II) GDB Client 端(PC/NB)使用步驟
• arm‐‐‐‐eabi‐‐‐‐gdb ExecutableWithDebugSymbol,進入 GDB
• GDB 中設定偵錯用函式庫搜路徑 [AndroidSrc 使用絕對路徑使用絕對路徑使用絕對路徑使用絕對路徑]
# set solib‐‐‐‐absolute‐‐‐‐prefix AndroidFroyoSrc/out/target/product/BoardModel/symbols/
# set solib‐‐‐‐search‐‐‐‐path
AndroidFroyoSrc
/out/target/product/ventana/symbols/system/lib:AndroidSrc/out/target/product/ventana/symbols/s
ystem/bin
• target remote : 5039 此 port number 需跟 GDB Server 使用的相同
III) 常用 GDB 指令
* b–設中斷點(breakpoint)
* display–檢視(watch)變數 / 暫存器內容
* c–continue
* n(ext)–單步執行(step over, 不進入函式)
* s(tep)–單步執行(step into, 進入函式)
* bt–backtrace,查看 function 的 callstack,stack 左方有數字表示各 function 在 stack 中的層
數。
Standalone Program Debug
測試用程式,係將一自 framebuffer 取出的圖寫至 framebuffer。執行的程式名稱為 testFB。
Android 2.2 之後,已內建 GDB server。在 ADB shell 下執行 gdbserver,即可啟動 GDB server。
自己的工作機器(PC/NB),稱作 Host。
1. 將 Host 的 5039 埠映對到 模擬器模擬器模擬器模擬器 或 設備設備設備設備 的 5039 埠
( adb forward Host-Port Dev/Emulator-Port )
adb forward tcp:5039 tcp:5039
2. 使用 ADB 連至 模擬器模擬器模擬器模擬器 或 設備設備設備設備
adb shell
3. ADB 連線成功後,執行下列指令:
#> gdbserver :5039 ./testFB &
or
gdbserver :5039 --attach PID &
, where PID is the ID of the process executing testFB program.
4. 於 Host 上,執行 codebase 中的 arm-eabi-gdb client 程式。參數為含有 debugging symbol 的 testFB
程式。
AndroidFroyoSrc/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gdb
AnroidFroyoSrc/out/target/product/ventana/obj/EXECUTABLES/testFB_intermediates/LINKED/testFB
or
AndroidFroyoSrc /prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gdb
AnroidFroyoSrc/out/target/product/ventana/symbols/system/bin/testFB
5. 於 Host 上,執行 gdb-debug-android.sh script 以進入 GDB client 的畫面如下 :
gdb-debug-android.sh 內容如下 :
此 shell script 中,將事先用 DDMS 抓出的 snapshot 圖, ”snapshot.bmp”,adb push 到 Android 裝置
中“/data”目錄;此圖由 testFB 程式所使用。
6. 於 Host 的 GDB client 中,設定 debug 用的函式庫之絕對路徑
(gdb) set solib-absolute-prefix AnroidFroyoSrc /out/target/product/ventana/symbols/
#! /bin/bash
export ANDROID_SRC_ROOT=/home/william/Project/Android_SrcRoot
#export APP_PROCESS_TO_DEBUG=/out/target/product/ventana/obj/EXECUTABLES/testFB_intermediates/LINKED/testFB
#export APP_PROCESS_TO_DEBUG=/out/target/product/ventana/symbols/system/bin/HelloWorld
export APP_PROCESS_TO_DEBUG=/out/target/product/ventana/obj/EXECUTABLES/HelloWorld_intermediates/HelloWorld
adb push snapshot.bmp /data/snapshot.bmp
adb forward tcp:5039 tcp:5039
$ANDROID_SRC_ROOT/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gdb
$ANDROID_SRC_ROOT$APP_PROCESS_TO_DEBUG
(gdb) set solib-search-path
AnroidFroyoSrc/out/target/product/ventana/symbols/system/lib:AnroidSrc/out/target/product/ventana/sy
mbols/system/bin:AnroidSrc/out/target/product/ventana/system/lib
7. 現在開始偵錯動作...
於 Host 的 GDB client 中執行指令:
(gdb) target remote :5039
[ In Debugging ]
8. 於 main()設定中斷點(break point),讓 GDB 執行並於第一個中斷點停止執行程式。
於 Host 的 GDB client 中執行指令:
(gdb) b main
(gdb) c
顯示程式碼 :
(gdb) list
繼續執行下一行 statement
(gdb) next
使用 backtrace 指令,觀察 call stack
(gdb)bt
圖中 ”#0” 及 ”#1” 分別表示目前正在執行的 function 及呼叫此 function 的 function。若還有其它
數字,數字愈大,表示是 call stack 中愈下方的 function。
<Shared Library Debug>
1. 準備測試程式 HelloWorld 及一共享函式庫 libtest.so (詳情請見附加檔案)。
以下是 libtest.so 編譯過程及結果。
2. 分別將編譯好的 HelloWorld 及 libtest.so,用 ADB push 至 /data 及 /system/lib。
#> adb push AndroidFroyoSrc
/out/target/product/ventana/obj/EXECUTABLES/HelloWorld_intermediates/LINKED/HelloWorld
/data
#> adb push AndroidFroyoSrc
/out/target/product/ventana/obj/SHARED_LIBRARIES/Libtest_intermediates/LINKED/libtest.so
/system/lib
3. 在 Android 機器中,執行 GDB server 指令
gdbserver :5039 ./HelloWorld &
,之後再執行 ps 指令查看 HelloWorld process 的 ID。
4. 在 Android 機器中,執行指令
cat /proc/2820/maps
即可見到 libtest.so 被分配的記憶體位址 : 0x80000000。
記錄下此位址。
5. 接下來查看 libtest.so 的.text 區段中的偏移量(offset)
在 Host 中執行指令:
AndroidFroyoSrc/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-objdump -h
AndroidFroyoSrc/out/target/product/ventana/obj/SHARED_LIBRARIES/libtest_intermediates/LIN
KED/libtest.so |grep .text
記下位址 : 0x00000034。
6. Host 中的 GDB client 設定如同 <Standalone Program Debug> 中步驟 4,5,6,7,除了將 ”testFB”
改成 ”HelloWorld”。
7. 於 Host 中 GDB clientgdb 中載入動態函式庫。
先將上述步驟 4,5 取得的位址作加總 : 0x80000000 + 0x00000034 = 0x80000034。
在 GDB client 中執行命令:
#> add-symbol-file
AndroidFroyoSrc/out/target/product/ventana/obj/SHARED_LIBRARIES/libtest_intermediates/LIN
KED/libtest.so 0x80000034
8. 接下來,如同<Standalone Program Debug>中步驟 8,於 Host GDB client 中,執行各項 GDB 指令。
上方為 Host 上的 GDB client 中,進入 libtest.so 的原程式中執行 GDB 單步執行指令。
上方為程式執行結果。
上述測試用檔案載點 (GitHub) :
https://github.com/wiliwe/android-gdb-example.git
使用 Git 工具( http://git-scm.com/downloads )下載
git clone https://github.com/wiliwe/android-gdb-example.git
Reference
1. nVidia GDB document
http://developer.download.nvidia.com/tegra/docs/android_gdb_debugging.pdf
2. GDB usage
http://www.cmlab.csie.ntu.edu.tw/~daniel/linux/gdb.html
http://tetralet.luna.com.tw/index.php?op=ViewArticle&articleId=187&blogId=1
http://opencsl.openfoundry.org/Lab05_debugger.rst.html
3. Android GDB
http://blog.wjmjimmie.cn/2010/08/02/android%E7%B3%BB%E7%BB%9F%E4%B8%AD%E8%B0
%83%E8%AF%95%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93so%E6%96
%87%E4%BB%B6%E7%9A%84%E6%AD%A5%E9%AA%A4/
http://www.cprogramming.com/debugging/segfaults.html

More Related Content

More from William Lee (20)

PDF
Usage Note of Apache Thrift for C++ Java PHP Languages
William Lee
 
PDF
Usage Note of Qt ODBC Database Access on Linux
William Lee
 
PDF
Usage Note of SWIG for PHP
William Lee
 
PDF
Upgrade GCC & Install Qt 5.4 on CentOS 6.5
William Lee
 
PDF
Usage Notes of The Bro 2.2 / 2.3
William Lee
 
PDF
Viewing Android Source Files in Eclipse (Chinese)
William Lee
 
PDF
Usage Note of Microsoft Dependency Walker
William Lee
 
PDF
Usage Note of PlayCap
William Lee
 
PDF
Qt4 App - Sliding Window
William Lee
 
PDF
GTK+ 2.0 App - Desktop App Chooser
William Lee
 
PDF
GTK+ 2.0 App - Icon Chooser
William Lee
 
PDF
Note of CGI and ASP
William Lee
 
PDF
Moblin2 - Window Manager(Mutter) Plugin
William Lee
 
PDF
Asterisk (IP-PBX) CDR Log Rotation
William Lee
 
PDF
L.A.M.P Installation Note --- CentOS 6.5
William Lee
 
PDF
C Program Runs on Wrong Target Platform(CPU Architecture)
William Lee
 
PDF
Internationalization(i18n) of Web Page
William Lee
 
PDF
Notes for SQLite3 Usage
William Lee
 
PDF
Cygwin Install How-To (Chinese)
William Lee
 
PDF
Study of Chromium OS
William Lee
 
Usage Note of Apache Thrift for C++ Java PHP Languages
William Lee
 
Usage Note of Qt ODBC Database Access on Linux
William Lee
 
Usage Note of SWIG for PHP
William Lee
 
Upgrade GCC & Install Qt 5.4 on CentOS 6.5
William Lee
 
Usage Notes of The Bro 2.2 / 2.3
William Lee
 
Viewing Android Source Files in Eclipse (Chinese)
William Lee
 
Usage Note of Microsoft Dependency Walker
William Lee
 
Usage Note of PlayCap
William Lee
 
Qt4 App - Sliding Window
William Lee
 
GTK+ 2.0 App - Desktop App Chooser
William Lee
 
GTK+ 2.0 App - Icon Chooser
William Lee
 
Note of CGI and ASP
William Lee
 
Moblin2 - Window Manager(Mutter) Plugin
William Lee
 
Asterisk (IP-PBX) CDR Log Rotation
William Lee
 
L.A.M.P Installation Note --- CentOS 6.5
William Lee
 
C Program Runs on Wrong Target Platform(CPU Architecture)
William Lee
 
Internationalization(i18n) of Web Page
William Lee
 
Notes for SQLite3 Usage
William Lee
 
Cygwin Install How-To (Chinese)
William Lee
 
Study of Chromium OS
William Lee
 

Android GDB Debugging (Chinese)

  • 2. Environment - 1. Ubuntu 9.10 2. Android 2.2 (Froyo) 3. nVidia T250 Ventana board Introduction •Depends on the Android tool : Android Debug Bridge (ADB) •Android 2.2(Froyo)源碼目錄中,prebuild 目錄下有現成的 GDB 程式(目前使用的 ARM EABI 版 本為 4.4.0) • AndroidFroyoSrc /prebuilt/linux‐‐‐‐x86/toolchain/arm‐‐‐‐eabi‐‐‐‐4.4.0/bin/arm‐‐‐‐eabi‐‐‐‐gdb • ARM EABI GDB 用法 : arm‐‐‐‐eabi‐‐‐‐gdb ExecutableWithDebugSymbol • ExecutableWithDebugSymbol 位在目錄 out/target/product/ventana/symbols/system/bin/ I) GDB Server 端(Android 裝置)使用步驟 • 使用 adb shell 登入遠端 Android 裝置 • 裝置後執行指令 # gdbserver :5039 /data/Executable & 或 # gdbserver :5039 ‐‐‐‐‐‐‐‐attach ExecutablePID & • Port number 5039, 可自行替換其它埠號, 但不可跟其它網路服務使用的埠號衝突! II) GDB Client 端(PC/NB)使用步驟 • arm‐‐‐‐eabi‐‐‐‐gdb ExecutableWithDebugSymbol,進入 GDB • GDB 中設定偵錯用函式庫搜路徑 [AndroidSrc 使用絕對路徑使用絕對路徑使用絕對路徑使用絕對路徑] # set solib‐‐‐‐absolute‐‐‐‐prefix AndroidFroyoSrc/out/target/product/BoardModel/symbols/ # set solib‐‐‐‐search‐‐‐‐path AndroidFroyoSrc /out/target/product/ventana/symbols/system/lib:AndroidSrc/out/target/product/ventana/symbols/s ystem/bin • target remote : 5039 此 port number 需跟 GDB Server 使用的相同 III) 常用 GDB 指令 * b–設中斷點(breakpoint) * display–檢視(watch)變數 / 暫存器內容 * c–continue * n(ext)–單步執行(step over, 不進入函式) * s(tep)–單步執行(step into, 進入函式) * bt–backtrace,查看 function 的 callstack,stack 左方有數字表示各 function 在 stack 中的層 數。
  • 3. Standalone Program Debug 測試用程式,係將一自 framebuffer 取出的圖寫至 framebuffer。執行的程式名稱為 testFB。 Android 2.2 之後,已內建 GDB server。在 ADB shell 下執行 gdbserver,即可啟動 GDB server。 自己的工作機器(PC/NB),稱作 Host。 1. 將 Host 的 5039 埠映對到 模擬器模擬器模擬器模擬器 或 設備設備設備設備 的 5039 埠 ( adb forward Host-Port Dev/Emulator-Port ) adb forward tcp:5039 tcp:5039 2. 使用 ADB 連至 模擬器模擬器模擬器模擬器 或 設備設備設備設備 adb shell 3. ADB 連線成功後,執行下列指令: #> gdbserver :5039 ./testFB & or gdbserver :5039 --attach PID & , where PID is the ID of the process executing testFB program.
  • 4. 4. 於 Host 上,執行 codebase 中的 arm-eabi-gdb client 程式。參數為含有 debugging symbol 的 testFB 程式。 AndroidFroyoSrc/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gdb AnroidFroyoSrc/out/target/product/ventana/obj/EXECUTABLES/testFB_intermediates/LINKED/testFB or AndroidFroyoSrc /prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gdb AnroidFroyoSrc/out/target/product/ventana/symbols/system/bin/testFB 5. 於 Host 上,執行 gdb-debug-android.sh script 以進入 GDB client 的畫面如下 : gdb-debug-android.sh 內容如下 : 此 shell script 中,將事先用 DDMS 抓出的 snapshot 圖, ”snapshot.bmp”,adb push 到 Android 裝置 中“/data”目錄;此圖由 testFB 程式所使用。 6. 於 Host 的 GDB client 中,設定 debug 用的函式庫之絕對路徑 (gdb) set solib-absolute-prefix AnroidFroyoSrc /out/target/product/ventana/symbols/ #! /bin/bash export ANDROID_SRC_ROOT=/home/william/Project/Android_SrcRoot #export APP_PROCESS_TO_DEBUG=/out/target/product/ventana/obj/EXECUTABLES/testFB_intermediates/LINKED/testFB #export APP_PROCESS_TO_DEBUG=/out/target/product/ventana/symbols/system/bin/HelloWorld export APP_PROCESS_TO_DEBUG=/out/target/product/ventana/obj/EXECUTABLES/HelloWorld_intermediates/HelloWorld adb push snapshot.bmp /data/snapshot.bmp adb forward tcp:5039 tcp:5039 $ANDROID_SRC_ROOT/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gdb $ANDROID_SRC_ROOT$APP_PROCESS_TO_DEBUG
  • 5. (gdb) set solib-search-path AnroidFroyoSrc/out/target/product/ventana/symbols/system/lib:AnroidSrc/out/target/product/ventana/sy mbols/system/bin:AnroidSrc/out/target/product/ventana/system/lib 7. 現在開始偵錯動作... 於 Host 的 GDB client 中執行指令: (gdb) target remote :5039 [ In Debugging ] 8. 於 main()設定中斷點(break point),讓 GDB 執行並於第一個中斷點停止執行程式。 於 Host 的 GDB client 中執行指令: (gdb) b main (gdb) c 顯示程式碼 : (gdb) list
  • 6. 繼續執行下一行 statement (gdb) next 使用 backtrace 指令,觀察 call stack (gdb)bt 圖中 ”#0” 及 ”#1” 分別表示目前正在執行的 function 及呼叫此 function 的 function。若還有其它 數字,數字愈大,表示是 call stack 中愈下方的 function。
  • 7. <Shared Library Debug> 1. 準備測試程式 HelloWorld 及一共享函式庫 libtest.so (詳情請見附加檔案)。 以下是 libtest.so 編譯過程及結果。 2. 分別將編譯好的 HelloWorld 及 libtest.so,用 ADB push 至 /data 及 /system/lib。 #> adb push AndroidFroyoSrc /out/target/product/ventana/obj/EXECUTABLES/HelloWorld_intermediates/LINKED/HelloWorld /data #> adb push AndroidFroyoSrc /out/target/product/ventana/obj/SHARED_LIBRARIES/Libtest_intermediates/LINKED/libtest.so /system/lib
  • 8. 3. 在 Android 機器中,執行 GDB server 指令 gdbserver :5039 ./HelloWorld & ,之後再執行 ps 指令查看 HelloWorld process 的 ID。 4. 在 Android 機器中,執行指令 cat /proc/2820/maps 即可見到 libtest.so 被分配的記憶體位址 : 0x80000000。 記錄下此位址。
  • 9. 5. 接下來查看 libtest.so 的.text 區段中的偏移量(offset) 在 Host 中執行指令: AndroidFroyoSrc/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-objdump -h AndroidFroyoSrc/out/target/product/ventana/obj/SHARED_LIBRARIES/libtest_intermediates/LIN KED/libtest.so |grep .text 記下位址 : 0x00000034。 6. Host 中的 GDB client 設定如同 <Standalone Program Debug> 中步驟 4,5,6,7,除了將 ”testFB” 改成 ”HelloWorld”。 7. 於 Host 中 GDB clientgdb 中載入動態函式庫。 先將上述步驟 4,5 取得的位址作加總 : 0x80000000 + 0x00000034 = 0x80000034。 在 GDB client 中執行命令: #> add-symbol-file AndroidFroyoSrc/out/target/product/ventana/obj/SHARED_LIBRARIES/libtest_intermediates/LIN KED/libtest.so 0x80000034 8. 接下來,如同<Standalone Program Debug>中步驟 8,於 Host GDB client 中,執行各項 GDB 指令。 上方為 Host 上的 GDB client 中,進入 libtest.so 的原程式中執行 GDB 單步執行指令。
  • 10. 上方為程式執行結果。 上述測試用檔案載點 (GitHub) : https://github.com/wiliwe/android-gdb-example.git 使用 Git 工具( http://git-scm.com/downloads )下載 git clone https://github.com/wiliwe/android-gdb-example.git
  • 11. Reference 1. nVidia GDB document http://developer.download.nvidia.com/tegra/docs/android_gdb_debugging.pdf 2. GDB usage http://www.cmlab.csie.ntu.edu.tw/~daniel/linux/gdb.html http://tetralet.luna.com.tw/index.php?op=ViewArticle&articleId=187&blogId=1 http://opencsl.openfoundry.org/Lab05_debugger.rst.html 3. Android GDB http://blog.wjmjimmie.cn/2010/08/02/android%E7%B3%BB%E7%BB%9F%E4%B8%AD%E8%B0 %83%E8%AF%95%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93so%E6%96 %87%E4%BB%B6%E7%9A%84%E6%AD%A5%E9%AA%A4/ http://www.cprogramming.com/debugging/segfaults.html