gdb是linux c編程標配的調試工具,平時接觸比較多的可能是本機隨gcc一起安裝的調試工具。但是,即使是本機的gdb,也經常被printf代替,所以接觸也僅限於知道。
簡單程序固然可以用printf,但是復雜的,帶有圖形界面的程序,就不得不使用調試工具,比如,arm的跨平台圖形程序調試。幸好Qt Creator支持gdb+gdbserver的方式來進行跨平台調試。
基本原理:
1. 目標板使用gdb-server來啟動已經編譯好的代碼,執行斷點、分步等調試動作,同時通過網絡反饋調試需要的信息給host。
2. host上運行arm-linux-gdb,解析gdb-server傳來的信息,同時,發送斷點、分步等動作給目標板。
所以,首要的工作是工具的准備。
arm-linux-gdb 以及 gdb-server 的制作,參考這篇:QtCreator 環境使用 remote debug
gdb 的簡單使用,可以參考這篇:gdb 簡單使用
在遠程調試時,首先,需要在遠程使用 gdbserver 打開要調試的程序,有兩種方式:
1) 直接用 gdbserver 打開:
gdbserver 192.168.1.230:1234 packet_analyzer
上面一句的意思是,使用 gdbserver 運行程序 packet_analyzer, gdbserver 的監聽地址為 192.168.1.230:1234
2) attach 到已經在執行的進程上:
gdbserver --attach localhost:1234 16924
上面這句表示,用 attach 的方式打開 gdbserver,調試進程號為 16924 的程序。
然后,在另外一邊,使用相應版本的 gdb 來作為 client 與 gdbserver 連接即可:
# arm-linux-gdb
Python Exception <type 'exceptions.ImportError'> No module named gdb:
warning:
Could not load the Python gdb module from `/tmp/gdb-7.6/__install/share/gdb/python'.
Limited Python support is available from the _gdb module.
Suggest passing --data-directory=/path/to/gdb/data-directory.
GNU gdb (GDB) 7.6
Copyright (C) 2013 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. Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) target remote 192.168.1.230:1234
Remote debugging using 192.168.1.230:1234
0x400007c0 in ?? ()
(gdb) file packet_analyzer
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from /home/luo/Documents/time_analyzer_qt/packet_analyzer...done.
至此,gdb 與 gdbserver 就勾搭上了,可以像在本地調試一樣,對遠程機器上的程序進行調試。
需要說明的是,在調試過程中,gdbserver 側是不接受 ctrl-c 來終止程序的。退出 gdbserver 目前知道的就兩種方法,在 gdb 側執行 quit,或者在 remote 側使用 killall。