天天看點

#夏日挑戰賽#OpenHarmony上移植gdb調試工具并使用gdb調試系統程

本文正在參加星光計劃3.0–夏日挑戰賽

作者:謝長鐘

1. 交叉編譯環境搭建

什麼是交叉編譯?

就是在一個平台上編譯出在另外一個平台上運作的程式。此處所說的平台,包含了硬體平台和軟體平台幾部分,硬體平台一般指CPU架構,諸如ARM、MIPSEL等;軟體平台指核心或作業系統。這裡編譯連結使用到的工具,我們統稱為交叉工具鍊。

交叉編譯一般應用于嵌入式領域,因為在目标嵌入式平台,資源有限,無法搭建一套編譯環境來編譯在自己平台上運作的程式,是以需要在其他資源更多、性能更強大的平台上使用交叉編譯工具鍊來編譯生成在目标嵌入式平台上運作的程式。

鴻蒙系統是最新的一個嵌入式系統平台,接下來教你在鴻蒙系統上如何搭建一套完整的gdb調試環境。

這裡我們使用到的交叉工具鍊是arm-none-linux-gnueabi。

首先,下載下傳交叉工具鍊:

http://www.360doc.com/content/16/0914/09/6828497_590676653.shtml
           

再使用root使用者權限解壓到Ubuntu主機或虛拟機上的/usr/local/路徑下。

配置環境變量:

更改了你使用者目錄下的~/.bashrc檔案:

vim ~/.bashrc
           

在檔案最後添加一句:

export PATH=$PATH:/usr/local/arm-2014.05/arm-none-linux-gnueabi/bin
           

使其立馬生效,執行一下指令:

source ~/.bashrc
           

這裡~代表目前使用者的目錄,如果想讓本Linux系統的所有使用者的環境變量都帶上交叉工具鍊的路徑,需要更改/etc/profile檔案,在profile檔案最後加上export PATH=$PATH:/usr/local/arm-2014.05/arm-none-linux-gnueabi/bin,重新開機你的Linux系統即可生效。

2. 交叉編譯gdb

gdb是Unix或linux下的程式調試工具,類似于win32下的visual studio。

運作在目标嵌入式平台上的gdb可以直接調試程式,通過 'gdb program' 即可輕松debug你的程式。

但如果目标嵌入式平台沒有操作終端,無法直接調試程式,也可以運作gdbserver,然後在主控端端遠端調試目标闆上的程式。在目标闆上通過 gdbserver 運作待調試的程式,在主控端端運作 gdb 通過 'target remote [ip]:[port]' 來遠端連接配接到目标闆上的 gdbserver,進而啟動遠端調試。在主控端上輸入各種調試指令,其目标闆程式執行結果展示在目标闆上,非常友善。

2.1 環境描述

虛拟機:

CPU:x86_64-linux-gnu

系統:Ubuntu 20.04
           

開發闆:

CPU:rk3566

系統:OpenHarmony
           

虛拟機上安裝的 C 交叉編譯器為 arm-none-linux-gnueabi-gcc,即交叉編譯工具鍊的字首為 arm-none-linux-gnueabi。

2.2 下載下傳源碼

從 ftp://ftp.gnu.org/gnu/gdb 下載下傳源碼 gdb-10.1.tar.gz。

解壓:

tar -zxvf ./gdb-10.1.tar.gz
           

2.3 交叉編譯 gdb和gdbserver

編譯 gdb:

cd gdb-10.1

./configure --prefix=/home/yourfolder/install --host=arm-none-linux-gnueabi 
--target=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc AS=arm-none-linux-gnueabi-as 
AR=arm-none-linux-gnueabi-ar CXX=arm-none-linux-gnueabi-g++ CFLAGS="-O3" LDFLAGS="-static -O3"
           

注意這裡LDFLAGS="-static",為了避免你的目标嵌入式系統使用的libc庫與arm-none-linux-gnueabi交叉工具鍊使用的不一緻,這裡采用靜态編譯。同時增加了-O3的編譯選項,優化gdb,使gdb可執行檔案盡可能小一些。

--prefix指定你編譯連結完成後,可執行檔案所安裝的目錄。

configure完成後,會生成Makefile。

按順序執行以下指令:

make

make install
           

最後在/home/yourfolder/install目錄下找到可執行檔案gdb和gdbserver,并将其推送到目标嵌入式開發闆上。

3. 使用GDB在鴻蒙系統上調試程式

3.1 使用gdb調試普通應用程式

調試普通應用程式,使用gdb [program]即可,很簡單。

./gdb test
GNU gdb (GDB) 10.1
Copyright (C) 2020 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 "arm-none-linux-gnueabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /data/gdbtest...
(gdb) b main.cpp:14
Breakpoint 1 at 0x164c: file main.cpp, line 14.
(gdb) r
Starting program: /data/gdbtest 
           

3.2 使用gdb調試鴻蒙系統的服務程式

調試鴻蒙系統的系統服務程式,首先需要了解鴻蒙系統中是分布式排程主程式排程啟停各個服務程式的,分布式排程主程式名為sa_main。

sa_main主程式通過配置/system/profile/camera_service.xml參數來啟停camera_service。

接下來,示範一下,如何調試camera_service。

./gdb /system/bin/sa_main
GNU gdb (GDB) 10.1
Copyright (C) 2020 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 "arm-none-linux-gnueabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /system/bin/sa_main...
(gdb)
(gdb) set args /system/profile/camera_service.xml
(gdb)
(gdb) r
Starting program: /system/bin/sa_main /system/profile/camera_service.xml
Thread 1 "camera_service" received signal SIGINT, Interrupt.
0xf7faf5a4 in ioctl () from /lib/ld-musl-arm.so.1
(gdb) bt
0  0xf7faf5a4 in ioctl () from /lib/ld-musl-arm.so.1
1  0xf7dc7318 in OHOS::BinderConnector::WriteBinder(unsigned long, void*) ()
  from /system/lib/libipc_core.z.so
2  0xf7dc86e4 in OHOS::BinderInvoker::TransactWithDriver(bool) ()
  from /system/lib/libipc_core.z.so
3  0xf7dc87a6 in OHOS::BinderInvoker::StartWorkLoop() ()
  from /system/lib/libipc_core.z.so
4  0xf7dc8edc in OHOS::BinderInvoker::JoinThread(bool) ()
  from /system/lib/libipc_core.z.so
5  0xf7f0b06e in OHOS::LocalAbilityManager::DoStartSAProcess(std::__h::basic_string<char,
std::__h::char_traits<char>, std::__h::allocator<char> > const&, int) () from
/system/lib/libsystem_ability_fwk.z.so
6  0x00403900 in main ()
           

可以看出gdb直接啟動的是sa_main,然後再給sa_main設定上camera_service.xml參數。如此,便啟動了camera_service程式,這樣就可以對camera_service進行調試了。

3.3 使用gdb遠端調試目标闆上的程式

開發闆:開發闆 IP 是 192.168.202.141,則輸入下述任一條指令皆可。

gdbserver :1234 test
gdbserver 127.0.0.1:1234 test
gdbserver 192.168.202.141:1234 test
           

主控端:首先在 SHELL 指令行裡運作 gdb 應用(注意此處的gdb是運作在主控端平台上的gdb,不是交叉編譯出來的gdb)。

./gdb
           

運作上一條指令後,SHELL 将進入 gdb 模式,下列幾條指令中 '(gdb)' 是提示符:

(gdb) target remote 192.168.202.141
(gdb) b main.c:55
(gdb) c
           

第一條指令是遠端連接配接到開發闆上的 gdbserver。連接配接之後,就是正常使用了。

第二條指令是設定斷點。

第三條指令是運作程式,注意等待目标調試程式真正已經在開發闆上成功運作了,是以此處要使用 'c' 指令,而不能使用 'r'/'run' 指令,如果執行 'r' 指令,可以看到提示 在remote 模式下不支援 'r' 指令:

(gdb) r
The "remote" target does not support "run".  Try "help target" or "continue".
           

更多原創内容請關注:深開鴻技術團隊

入門到精通、技巧到案例,系統化分享HarmonyOS開發技術,歡迎投稿和訂閱,讓我們一起攜手前行共建鴻蒙生态。

繼續閱讀