Host: ubuntu
Target: Android ICS
1. 將gdbserver和gdbclient分別放入target和host。兩者都可在android原始碼的prebuilt目錄下找到。
2. 在Target上啟動gdbserver,在adb shell中執行
$ /path/to/gdbserver :1111--attach PID
其中PID為想要debug程式的進程號,可以通過ps得到。1111為連接埠號碼,可以自訂。
3. 在Host上啟動gdbclient,首先將target上的庫pull到host上(如/home/jzj/debug/gdb)
$cd /home/jzj/debug/gdb
$adb pull /system/lib
$adb pull /system/bin/app_process
然後寫啟動指令碼init.gdb(這裡假設程式要用到的庫放在/home/jzj/debug/gdb/下,android的symbol放在/home/jzj/debug/ics/symbols/system/lib/下,android的源碼放在/home/jzj/workspace/ics下)
file /home/jzj/debug/gdb/app_process
set solib-absolute-prefix /home/jzj/debug/ics/symbols/
set solib-search-path /home/jzj/debug/ics/symbols/system/lib/:/home/jzj/debug/gdb
dir /home/jzj/workspace/ics
shell adb forward tcp:1111 tcp:1111
target remote :1111
最後執行
$ gdb -x init.gdb
就可以啟動gdb了。一切正常的話這時候就可以用gdb的準系統,如設斷點或者查看記憶體了。這時有些so庫的符號表可能還沒載入,可以執行
(gdb) shared
載入這些符號表。
最後附個關於gdb設定指令碼中兩個變數的官方解釋,一般solib-absolute-prefix先於solib-search-path尋找。
solib-absolute-prefix :
If this variable is set, path will be used as a prefix for any absolute shared library paths; many runtime loaders store the absolute paths to the shared library in the target program's memory. If you use `solib-absolute-prefix' to find shared libraries, they need to be laid out in the same way that they are on the target, with e.g. a `/usr/lib' hierarchy under path. You can set the default value of `solib-absolute-prefix' by using the configure-time `--with-sysroot' option.
solib-search-path :
If this variable is set, path is a colon-separated list of directories to search for shared libraries. `solib-search-path' is used after `solib-absolute-prefix' fails to locate the library, or if the path to the library is relative instead of absolute. If you want to use `solib-search-path' instead of `solib-absolute-prefix', be sure to set `solib-absolute-prefix' to a nonexistant directory to prevent GDB from finding your host's libraries.