上篇介紹了定制rpi os,在上篇的基礎上這裡介紹怎麼通過yocto進行應用開發,并在RPI OS上進行gdb調試。
Gdb調試組織架構
環境:
- ubuntu 22.04
- raspiberry 4B
- U盤 64G
- yocto 4.1.2
- vmware 16.2.4
- gdb 12.1
Rpi OS增加gdb特性
Rpi OS如果需要gdb進行遠端調試,需要在鏡像core-image-base-rpi.bb中添加"tools-debug" feature,添加以後就可以在Rpi開發闆上開啟gdbserver:
core-image-base-rpi.bb
建立myhello應用
- 建立meta-hello layer,并且把該layer添加到bblayers.conf:
cd xxx/poky
source oe-init-build-env rpi-build
bitbake-layers create-layer ../meta-hello
bitbake-layers add-layer ../meta-hello
目錄結構如下:
yocto/poky$ tree -L 2 -n
.
├── meta-hello
│ ├── conf
│ ├── COPYING.MIT
│ ├── README
│ └── recipes-example
├── meta-mylayer
...
├── meta-raspberrypi
...
├── oe-init-build-env
├── rpi-build
│ ├── bitbake-cookerdaemon.log
│ ├── cache
│ ├── conf
│ ├── downloads
│ ├── sstate-cache
│ └── tmp
2.在meta-hello/recipes-example目錄下建立myhello檔案夾,然後在myhello目錄下建立files檔案夾作為recipe的SRC_URI的路徑。在files目錄下建立myhello.c源檔案:
#include <stdio.h>
int main (void)
{
int i;
printf ("Enter Programme\n");
for(i=0;i<=10;i++) {
printf ("count:%d\n",i);
}
printf ("exit\n");
return 0;
}
- 建立myhello recipe檔案,内容如下,do_compile中需要加-g,表示帶有調試資訊:
DESCRIPTION = "Simple helloworld application"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://myhello.c"
S = "${WORKDIR}"
do_compile() {
${CC} myhello.c ${LDFLAGS} -o myhello -g
}
do_install() {
install -d ${D}${bindir}
install -m 0755 myhello ${D}${bindir}
}
- 編譯myhello,确認文法是否有誤:
bitbake myhello
myhello應用的目錄如下:
yocto/poky/meta-hello$ tree -L 4 -n
.
├── conf
│ └── layer.conf
├── COPYING.MIT
├── README
└── recipes-example
├── example
│ └── example_0.1.bb
└── myhello
├── files
│ └── myhello.c
└── myhello.bb
鏡像編譯
把myhello應用編譯到鏡像中,需要在local.conf中進行install:
IMAGE_INSTALL
注:如果不安裝到鏡像中,在gdb調試的時候也可以通過指令把myhello push到開發闆:remote put myhello myhello,為了友善調試直接安裝到了鏡像中。
編譯鏡像:
bitbake core-image-base-rpi -c cleansstate
bitbake core-image-base-rpi
RPI OS檢視是否包含myhello:
rpios确認存在myhello
RPI OS檢視是否有gdbserver功能:
rpios是否包含gdbserver
遠端調試
- 安裝gdb-multiarch,支援多種硬體體系架構的gdb版本,調試前需要設定體系參數。
sudo apt-get install gdb-multiarch
- rpi開發闆啟動gdbserver,在開發闆上執行:
gdbserver --multi :5555 myhello
- 确認myhello路徑:
yocto/poky/rpi-build/tmp/work$ find ./ -name myhello -type f
使用myhello包路徑下的二進制,該路徑下包含myhello.c源碼:
4.gdb-multiarch調試myhello:
$gdb-multiarch -q xxx/myhello
Reading symbols from xxx/myhello...
- 設定體系參數:
(gdb) set architecture arm
The target architecture is set to "arm".
6.連接配接遠端開發闆:
(gdb) target extended-remote 開發闆IP:5555
Remote debugging using xxx.xxx.xxx.xxx:5555
7.設定斷點,進行run:
(gdb) b main
Breakpoint 1 at 0x438: file /usr/include/bits/stdio2.h, line 86.
(gdb) run
Starting program:xxx/myhello
如下圖: