天天看點

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

寫這篇文章的目的在于幫助那些既要使用Visual Studio編寫程式又要開發和調試嵌入式Linux 程式的苦命程式員們!

第一步, 安裝 WinGDB ,下載下傳位置  

http://www.wingdb.com/

   ,連接配接是

http://www.wingdb.com/WinGDB-latest_trial.msi

 30天試用版在30天後 解除安裝掉, 删除所有gdb相關的系統資料庫, 然後重新安裝, 就又可以使用30天, 安裝過程如下圖示:

1、選擇同意許可協定

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

2、如果你機器上裝了足夠多的VS版本, 你都可以選擇, 主要看你用那個版本的VS

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

3、如果你僅作遠端Linux開發 , 則選第一項, 如果你要做嵌入式系統開發, 選擇第二項, 如果你想全部要, 則選第三項 , 我們這裡當然是選擇第三項了。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

第二步、建立WinGDB項目

1、建立WinGDB項目,選擇Multiplatform executable

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

2、選擇第一項, 我們要進行遠端開發,源碼在建構在遠端伺服器上, 調試也同樣在遠端主機上, 通過TCP/IP連接配接并使用SSH協定來通路這些機器。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

3、選擇第二項, gdb 和交叉編譯環境在一台伺服器上, 遠端調試在另外一台機器上, 這台機器可以是arm linux 也可以是其他支援gdbserver 的機器。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

4、填寫gdb和編譯環境的ssh位址和使用者名密碼 以及 gdbserver 所在的使用者名密碼和IP位址。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

5、惦記Browse 選擇我們事先編譯好的 gdb 調試器,這是運作在編譯環境那台伺服器上的gdb , 用來控制 gdbserver 的 , gdb和gdbserver必須是對應的。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

6、選擇本地源碼和遠端的源碼的同步方式, 這個根據情況來選擇, 如果你是共享的, 擇選第一項, 如果需要同步, 則選第二項, 如果不需要同步, 則選第三項, 如圖:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

 7、選擇遠端檔案路徑和本地檔案路徑:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

8、設定編譯 指令, 和編譯目錄, 這些目錄在點選browse時都是列出的編譯伺服器上的目錄

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

9、選擇遠端調試的目标目錄 , 如圖:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

 10、選擇額外和附加的源碼檔案和庫 ,這步直接跳過, 如果你有需要, 則可以再這裡設定。

11、設定調試啟動參數 ,與此同時, 建議把Stop in main 去掉, 這樣避免每次啟動程式時, 都要在main 函數哪裡停下來。 如圖:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

12、配置完成, 如圖:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

我的配置内容如下:

Project build command: make

Project rebuild command: make clean; make

Project clean command: make clean

Project deploy command: 

Build working directory: /root/ct/src/

Build environment variables: BUILD_CONFIGURATION="Debug"

Build before start debugging: Build project

Deploy before start debugging: yes

Deploy action: Transfer file to target

Deploy only if file changed: yes

Files to deploy: /root/ct/src//EMRCV4

Deploy target directory: /mnt/EMRCV4/BIN

Core file path: 

Working directory: /mnt/EMRCV4/BIN

Arguments: 

Environment variables: 

Stop in main(): no

Target specification: 

Server mode: Launch executable

Executable on target: /mnt/EMRCV4/BIN/EMRCV4

Launch GDB server automatically: yes

Launch server from sysroot: no

Path to gdbserver: /usr/bin/gdbserver

Server port: 2345

Server port is forwarded: no

Forwarded server port: 0

Executable path: /root/ct/src//EMRCV4

Byte order: Little endian

Initialization script: 

Additional source directories: 

Debug info directory: 

Sysroot on host: 

Sysroot on target: 

Shared library directories: 

Visualizer profile: 

Load sources from build host: no

Disable hang reporter: no

Debugger path: /usr/bin/arm-unknown-linux-gnueabi-gdb

Build toolchain paths: 

Intercept standard IDE commands: yes

Target type: Linux indirect debugging (SSH+remote target)

Debug host login: 

[email protected]:22

Build host login: 

Debug target login: 

[email protected]:22

至此完成項目的調試和編譯配置

第三部、代碼映射配置

此時輸出視窗中會輸出如下内容:

--------------------------------------------------------------------------------                          Transfer to remote host(s) --------------------------------------------------------------------------------

Errors were encountered during preparation: No files could be copied.

Could not map the path: D:\Works\RHY\Toll\Trunk\src\FunctionAssembly\Lane\EMRCV4\EMRCV4.vcxproj to remote filesystem. Use 'Map source directories' option to setup a mapping.

如果出現, 則打開代碼映射視窗

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

然後在下面添加一個Login , 再選擇添加一個映射, 如圖:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

 下面是編譯時的效果, 由于編譯器錯誤等級原因, 第一次編譯會有很多錯, 第二次編譯則錯誤消失 ,

如圖:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

調試時 , 啟動時會彈出下面的視窗, 點确定即可, 不用理會

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

下面是調試過程的情況, 可以檢視運作時目前程序的局部變量和滑鼠下面的變量值, 這個跟VS一樣, 也可以單步執行。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

第四步:頭檔案引用 , 上圖中你可以看到很多常量和函數都有紅線, 是因為VS找不到對應的頭檔案, 是以需要繼續做些設定。

打開項目屬性, 選中配置屬性, 選中VC++目錄,然後在包含目錄中加入 交叉編譯環境中的頭檔案,比如:

D:\Works\Linux\toolchain_R2_EABI\usr\arm-unknown-linux-gnueabi\sysroot\usr\include

然後, 你需要把你所有頭檔案的目錄全部添加在這裡, 如果頭檔案目錄比較多, 就比較費勁。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

設定完後你會看到VS正在解析頭檔案, 設定後效果如下: 

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

 最後, 如果向導設定有問題或者沒有儲存, 可能是由于不是管理者權限的原因, 是以你可能需要檢查下你的配置,

方法如下圖:

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

正常設定 debug host 和 build host 一樣

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

環境中設定調試器路徑

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

調試選項中, 要選擇好生産的可執行檔案 和 在 目标機器上的 應用程式工作目錄

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

調試目标

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

gdbserver 設定

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

下面一步很關鍵, 是項目生成設定, 生成目錄必須和路徑映射中的路徑一直, 否則報錯的錯誤無法定位檔案位置,

第七欄中設定 調試前生成項目, 第八項設定調試前需要部署, 第十項設定 部署操作為傳送檔案至目标,

第十一項選擇是, 當檔案改變則部署, 第十二項決定要部署的檔案, 第五項是 要部署的檔案位置。

使用Visual Studio 利用WinGDB編譯和遠端調試嵌入式Linux的程式

結束語

另外編譯arm 相關的gdbserver 和 gdb在以後的文章中也會介紹到!這篇文章就不做介紹了。 如果gdb和gdbserver 不一緻, 則同樣會無法調試

下面是我的gdbserver 的版本資訊:

[192.168.50.58:/mnt/EMRCV4/BIN]# gdbserver --version

GNU gdbserver (GDB) 7.2

Copyright (C) 2010 Free Software Foundation, Inc.

gdbserver is free software, covered by the GNU General Public License.

This gdbserver was configured as "arm-unknown-linux-gnueabi"

[192.168.50.58:/mnt/EMRCV4/BIN]#

下面是gdb的版本資訊

root@ubuntu:/usr/bin

#  arm-unknown-linux-gnueabi-gdb

GNU gdb (GDB) 7.2

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-unknown-linux-gnueabi".

For bug reporting instructions, please see:

<

http://www.gnu.org/software/gdb/bugs/

>.

注意, 一定要注意  This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-unknown-linux-gnueabi".

如果不一緻, 同樣會編譯失敗的。

至此完全配置完畢,在稍後的幾天裡, 我會根據情況來補充和完善這篇文章, 如有興趣, 請繼續關注吧!

繼續閱讀