owned this note
owned this note
Published
Linked with GitHub
---
title: raspberrypi_openbmc
---
## Summary
本文會示範如何在 Raspberry pi 上進行 openbmc 的建置及在有限記憶體下進行除錯。
- [COSCUP OpenBMC GDB 除錯 Slide](https://docs.google.com/presentation/d/1qjZdhjoTpmrEKkXHBRLmSIAvJ8QclsRXhpjgIcnwqrY/edit?usp=sharing)
- Speaker: Alan Tseng
- [Linkedin @ alanhc316](https://www.linkedin.com/in/alanhc316/)
- mail: alan.tseng.cs@gmail.com
- Date: 10 Aug 2025 (Sun.)
- [Slido (Q&A)](https://app.sli.do/event/8DF8a4mM3osdf2FScijxCd)
- 
- [Doc](https://hackmd.io/@alanhc/BJQAi7vLxx)
## Bagrounds
- bb file
- meta-layer
## 環境與前置作業
- 目標機:Raspberry Pi 3(MACHINE = raspberrypi3)
- Build host:Ubuntu(安裝 git, python3, build-essential, bmap-tools 等)
## 取得原始碼與建立自訂 Layer
```bash
git clone git@github.com:openbmc/openbmc.git
cd openbmc
mkdir -p meta-mytest/conf
```
- `meta-mytest/conf/layer.conf`
```
BBPATH .= ":${LAYERDIR}"
# 告訴 BitBake 去哪些路徑找 .bb / .bbappend
BBFILES += "\
${LAYERDIR}/recipes-*/*/*.bb \
${LAYERDIR}/recipes-*/*/*.bbappend \
"
BBFILE_COLLECTIONS += "evb"
# 用正則把這個 layer 底下的檔案都納入
BBFILE_PATTERN_evb = "^${LAYERDIR}/"
# 依你的 Yocto 版本調整
LAYERSERIES_COMPAT_evb = "scarthgap walnascar"
```
## 複製 RPi 範本並套用
若有 `meta-evb/meta-evb-raspberrypi` 可直接複製:
```
cp -r meta-evb/meta-evb-raspberrypi/* meta-mytest
```
## `TEMPLATECONF` 與初始化
```
export TEMPLATECONF=meta-mytest/conf/templates/default
. openbmc-env
```
成功後會看到:`Common targets are: obmc-phosphor-image`
:::info
備註 :openbmc-env 會自動建立 build/ 目錄並產生 local.conf、bblayers.conf 等檔案。
:::
## `local.conf` 關鍵設定
`local.conf` 是當你在本地測試時,Yocto 優先使用這層的設定
`build/conf/local.conf`:
```
INHERIT += "own-mirrors"
SOURCE_MIRROR_URL = "https://downloads.yoctoproject.org/mirror/sources/"
# 使用 raspberry 3 ,如果非 3b 版本要改掉這
MACHINE ??= "raspberrypi3"
DISTRO ?= "openbmc-phosphor"
PACKAGE_CLASSES ?= "package_ipk"
SANITY_TESTED_DISTROS:append ?= " *"
EXTRA_IMAGE_FEATURES ?= "allow-root-login"
USER_CLASSES ?= "buildstats"
PATCHRESOLVE = "noop"
BB_DISKMON_DIRS ??= "\
STOPTASKS,${TMPDIR},1G,100K \
STOPTASKS,${DL_DIR},1G,100K \
STOPTASKS,${SSTATE_DIR},1G,100K \
STOPTASKS,/tmp,100M,100K \
HALT,${TMPDIR},100M,1K \
HALT,${DL_DIR},100M,1K \
HALT,${SSTATE_DIR},100M,1K \
HALT,/tmp,10M,1K"
CONF_VERSION = "2"
# 指定 RPi 官方 kernel
PREFERRED_PROVIDER_virtual/kernel ?= "linux-raspberrypi"
# 同意特殊授權(Broadcom Wi‑Fi 韌體)
LICENSE_FLAGS_ACCEPTED:append = " synaptics-killswitch"
do_generate_static[noexec] = "1"
do_generate_static_tar[noexec] = "1"
do_generate_static_alltar[noexec] = "1"
# 產生 wic / wic.xz
IMAGE_FSTYPES += "wic wic.bmap"
# 產生 bmap 加速燒錄
WIC_CREATE_BMAP = "1"
MACHINEOVERRIDES:append = ":phosphor-evb"
# Set the root password to '0penBmc'
# Defaults from meta-phosphor/conf/distro/include/phosphor-defaults.inc
# require conf/machine/include/obmc-bsp-common.inc
# require conf/machine/include/obmc-evb-common.inc
# 啟用 UART、關閉 BT(方便 early console)
ENABLE_UART = "1"
RPI_EXTRA_CONFIG += "dtoverlay=disable-bt"
```
## 映像產出與燒錄
我們使用 `bmaptool` 燒錄 openbmc 映像到 SD 卡
在此之前我們先檢查 sd卡 掛載在哪
```
$ lsblk
sdb 8:16 1 7.4G 0 disk
├─sdb1 8:17 1 130M 0 part
└─sdb2 8:18 1 176.8M 0 part
# 使用 bmaptool
$ sudo bmaptool copy \
tmp/deploy/images/raspberrypi3/obmc-phosphor-image-raspberrypi3.wic \
/dev/sdc
$ sync # 確保寫入完成
```
## hello world
目標撰寫一個 hello world 的 recipe,會在 Systemd 註冊一個 Service 並可以印出 Hello World。
### 建立 Recipe Skeleton
在 `build`
```
# 建立 recipes-example/hello
$ bitbake-layers create-layer ../meta-mytest/recipes-example/hello
# 加入 Layer
$ bitbake-layers add-layer ../meta-mytest
```
會在 `openbmc/` 加上 `meta-myhello`
:::info
會在 `openbmc/` 加上 `meta-myhello`
加上 layer,會在 `openbmc/build/conf/bblayers.conf`
```
BBLAYERS ?= " \
...
/home/alanhc/workspace/openbmc/meta-myhello \
"
```
:::
建立以下檔案
```bash
tree '/home/alanhc/workspace/coscup/openbmc/meta-mytest/recipes-example/hello/'
/home/alanhc/workspace/coscup/openbmc/meta-mytest/recipes-example/hello/
├── conf
│ └── layer.conf
├── files
│ └── hello-1.0
│ ├── COPYING.MIT
│ ├── hello.service
│ ├── main.cpp
│ └── meson.build
└── README
4 directories, 6 files
```
:::info
內容可以參考這: https://github.com/alanhc/example/tree/master/bmc/meson/meta-mytest
:::
### 將範例加入映像
`openbmc/meta-mytest/recipes-phospher/images/obmc-phosphor-image.bbappend`
```
IMAGE_BASENAME = "obmc-phosphor-image"
DESCRIPTION = "OpenBMC image for Raspberry Pi 3 with Phosphor stack"
IMAGE_INSTALL:append = " hello"
```
build
`bitbake hello`
最後build image
```
bitbake obmc-phosphor-image
```
### 燒錄韌體
```
# 找 image
$ find . -name *.wic
# 找 sdk 掛載
$ lsblk
# 使用 bmaptool
$ sudo bmaptool copy \
tmp/deploy/images/raspberrypi3/obmc-phosphor-image-raspberrypi3.wic \
/dev/sdc
$ sync # 確保寫入完成
```
:::info
*.wic 檔是由 Yocto 的 WIC (Image Creator) 工具依照 .wks 版型產生的「完整磁碟映像」。
:::
## OpenBMC Cross Debug
目標:在映像中新增一個將觸發 `SIGSEGV` 的 Hello 範例,展示如何進行 cross‑debug。當
:::info
SIGSEGV 全名是 "Segmentation Fault"(段錯誤),是 Linux/Unix 系統中的一種訊號(signal),代碼為 11,用來通知程式:你試圖訪問不屬於你記憶體區域的位址了!
:::
由於我們只需要進行暫時性的除錯,因此是在`local.conf`加上
```
# 加入除錯套件
IMAGE_INSTALL:append = " coreutils"
EXTRA_OECONF += "ULIMIT_CORE=unlimited"
EXTRA_OECONF += "KERNEL_PARAM_CORE_PATTERN=/var/lib/systemd/coredump/core.%e.%p.%t"
IMAGE_INSTALL:append = " gdbserver"
```
在 `main.cpp` 新增一個有錯的範例
```cpp
// main.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
int* ptr = nullptr;
std::cout << *ptr << std::endl; // Dereferencing a null pointer causes a segmentation fault (core dump)
return 0;
}
```
在需要改的 bbfile 加上
```
EXTRA_OEMESON:append = " --buildtype debug --prefix=/usr"
# 方便除錯的旗標(保留 frame pointer;O0 + g3)
CFLAGS:append = " -O0 -g3 -fno-omit-frame-pointer"
CXXFLAGS:append = " -O0 -g3 -fno-omit-frame-pointer"
INHIBIT_PACKAGE_STRIP = "1"
INHIBIT_PACKAGE_DEBUG_SPLIT = "0"
```
產生sdk
```
bitbake -c populate_sdk obmc-phosphor-image
./tmp/deploy/sdk/oecore-obmc-phosphor-image-x86_64-cortexa7t2hf-neon-vfpv4-raspberrypi3-toolchain-nodistro.0.sh
```
開啟另一個視窗,路經是 `openbmc/build`:
```bash
$ . /usr/local/oecore-x86_64/environment-setup-cortexa7t2hf-neon-vfpv4-openbmc-linux-gnueabi
$ arm-openbmc-linux-gnueabi-gdb /usr/local/oecore-x86_64/sysroots/cortexa7t2hf-neon-vfpv4-openbmc-linux-gnueabi/usr/bin/hello
GNU gdb (GDB) 16.2
...
$ (gdb) set sysroot /usr/local/oecore-x86_64/sysroots/cortexa7t2hf-neon-vfpv4-openbmc-linux-gnueabi
$ (gdb) set substitute-path /usr/src/debug/hello/1.0 /home/alanhc/workspace/coscup/openbmc/meta-mytest/recipes-example/hello/files/hello-1.0
$ (gdb) target remote 192.168.5.6:1234
$ (gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x0040072e in main () at /usr/src/debug/hello/1.0/main.cpp:7
7 std::cout << *ptr << std::endl; // Dereferencing a null pointer causes a segmentation fault (core dump)
```
### coredump 分析
- 取得遠端 core file
```
$ scp -r root@192.168.5.6:/var/lib/systemd/coredump .
$ zstd -d core.hello.0.9ceb1a5ad74745adab0cf8f105fad21f.391.1754102934000000.zst -o hello.core
```
- `hello.gdb`
```
# === 1. 載入執行檔(含 .text 區段) ===
file /usr/local/oecore-x86_64/sysroots/cortexa7t2hf-neon-vfpv4-openbmc-linux-gnueabi/usr/bin/hello
# === 2. 載入完整除錯符號 ===
symbol-file /usr/local/oecore-x86_64/sysroots/cortexa7t2hf-neon-vfpv4-openbmc-linux-gnueabi/usr/bin/.debug/hello
# === 3. 載入 core dump ===
core-file hello.core
# === 4. 指定 sysroot 與共享函式庫路徑 ===
set sysroot /usr/local/oecore-x86_64/sysroots/cortexa7t2hf-neon-vfpv4-openbmc-linux-gnueabi
set solib-search-path $sysroot/usr/lib
set debug-file-directory $sysroot/lib/.debug
# === 5. 原始碼路徑映射 ===
set substitute-path /usr/src/debug/hello/1.0 /home/alanhc/workspace/coscup/openbmc/meta-mytest/recipes-example/hello/files/hello-1.0
# === 6. 顯示簡要狀態 ===
info sources
info sharedlibrary
bt
```
- 使用 `.gdb` 檔案進行除錯
```
arm-openbmc-linux-gnueabi-gdb -x hello.gdb
```
## Q&A
### 因為用GDB debug時,不像ide開發介面,能任意切換不同.c程式頁面,請問在openBMC 用GDB時,能跟IDE插件結合嗎?
- 我在開發 OpenBMC 時,通常會使用 vscode 連進 buildcode server 進行開發,雖然我使用 gdb 的時候大多是直接使用 Terminal ,不過我有找到 vscode 有相關的說明文件如何設定,可以朝著設定 `launch.json`(或 `tasks.json` 搭配外部啟動):https://code.visualstudio.com/docs/cpp/config-linux
### Objdump 可以用 -j .text 直接倒出 text 區段
- 可以使用以下方式:
```
arm-openbmc-linux-gnueabi-objdump -d -j .text hello.debug
```
- `-d`: 反組譯
- `-j .text`: 選擇 .text 區段
- 'hello.debug' 是 bmc 上的執行檔
### 可以簡單過一遍elf LMA/VMA嗎
先從定義來看
| 名稱 | 全名 | 意義 | 在 ELF 中的角色 |
| ------- | ---------------------- | ------------------------------------------ | ------------------------------ |
| **VMA** | Virtual Memory Address | **程式執行時**,該段資料在 **虛擬記憶體**(或 CPU 視角地址空間)的位址 | 給 CPU 取指令、訪問變數用 |
| **LMA** | Load Memory Address | **載入到記憶體** 時,該段資料的 **實際物理位置** | 給 Bootloader 或載入器使用,知道從哪搬到 VMA |
我們先來看 core dump 裡的 Program Header Table,
每一行 LOAD 對應到 core dump 中的一段記憶體映射(PT_LOAD segment),
就是程式在當時崩潰時,記憶體的某一連續區域(頁對齊)被存下來。
```
$ readelf -l hello.core | grep -nA3 'LOAD'
9: LOAD 0x001000 0x004ba000 0x00000000 0x01000 0x01000 R E 0x1000
10: LOAD 0x002000 0x004bb000 0x00000000 0x01000 0x01000 R 0x1000
11: LOAD 0x003000 0x004bc000 0x00000000 0x01000 0x01000 RW 0x1000
12: LOAD 0x004000 0x00fbc000 0x00000000 0x21000 0x21000 RW 0x1000
13: LOAD 0x025000 0x76cb6000 0x00000000 0x02000 0x02000 RW 0x1000
14: LOAD 0x027000 0x76cb8000 0x00000000 0x01000 0x45000 R E 0x1000
15: LOAD 0x028000 0x76cfd000 0x00000000 0x01000 0x01000 R 0x1000
16: LOAD 0x029000 0x76cfe000 0x00000000 0x01000 0x01000 RW 0x1000
17: LOAD 0x02a000 0x76cff000 0x00000000 0x01000 0x102000 R E 0x1000
18: LOAD 0x02b000 0x76e01000 0x00000000 0x02000 0x02000 R 0x1000
19: LOAD 0x02d000 0x76e03000 0x00000000 0x01000 0x01000 RW 0x1000
20: LOAD 0x02e000 0x76e04000 0x00000000 0x0a000 0x0a000 RW 0x1000
21: LOAD 0x038000 0x76e0e000 0x00000000 0x01000 0x18000 R E 0x1000
22: LOAD 0x039000 0x76e26000 0x00000000 0x01000 0x01000 R 0x1000
23: LOAD 0x03a000 0x76e27000 0x00000000 0x01000 0x01000 RW 0x1000
24: LOAD 0x03b000 0x76e28000 0x00000000 0x01000 0x18d000 R E 0x1000
25: LOAD 0x03c000 0x76fb5000 0x00000000 0x07000 0x07000 R 0x1000
26: LOAD 0x043000 0x76fbc000 0x00000000 0x01000 0x01000 RW 0x1000
27: LOAD 0x044000 0x76fbd000 0x00000000 0x02000 0x02000 RW 0x1000
28: LOAD 0x046000 0x76fc1000 0x00000000 0x02000 0x02000 RW 0x1000
29: LOAD 0x048000 0x76fc3000 0x00000000 0x01000 0x1c000 R E 0x1000
30: LOAD 0x049000 0x76fdf000 0x00000000 0x02000 0x02000 R 0x1000
31: LOAD 0x04b000 0x76fe1000 0x00000000 0x01000 0x01000 RW 0x1000
32: LOAD 0x04c000 0x7ee8b000 0x00000000 0x21000 0x21000 RW 0x1000
33: LOAD 0x06d000 0x7ef79000 0x00000000 0x01000 0x01000 R E 0x1000
34: LOAD 0x06e000 0x7ef7a000 0x00000000 0x01000 0x01000 R 0x1000
35: LOAD 0x06f000 0x7ef7b000 0x00000000 0x01000 0x01000 R E 0x1000
36: LOAD 0x070000 0xffff0000 0x00000000 0x01000 0x01000 R E 0x1000
```
來看第一行:
```
9: LOAD 0x001000 0x004ba000 0x00000000 0x01000 0x01000 R E 0x1000
```
| 欄位順序 | 值 | 意義 |
| ------------- | ------------ | ---------------------------------------- |
| **p\_offset** | `0x001000` | **core 檔案內的位元組偏移**(從檔案開頭算) |
| **p\_vaddr** | `0x004ba000` | **該段映射的虛擬位址起點**(當時在程式記憶體空間的位置) |
| **p\_paddr** | `0x00000000` | 在 core dump 通常無意義(實體位址在應用程式級別不重要) |
| **p\_filesz** | `0x01000` | core 檔案內這段資料的大小(通常為整數個頁) |
| **p\_memsz** | `0x01000` | 記憶體中這段映射的總大小(和 filesz 一樣時表示全部 dump 下來) |
| **flags** | `R E` | 權限:Readable + Executable(通常是程式碼 `.text`) |
| **p\_align** | `0x1000` | 對齊大小(頁大小,一般 Linux 是 4KB) |
整份表可理解為
```
0x004ba000 R E → 主程式 .text
0x004bb000 R → 主程式 .rodata
0x004bc000 RW → 主程式 .data / .bss
```
這三行連在一起(位址連續、權限分別是 RX → R → RW),非常典型就是主程式的程式碼、唯讀資料、可寫資料。
接下來我們的目標是:把 core file 裡對應 `.text` 的位元組切出來,再用 `objdump` 反組譯。
背後的觀念:
core 檔的每一個 PT_LOAD(前面表的 LOAD ...)都代表「把檔案從 p_offset 起的 p_filesz 位元組,對應到記憶體從 p_vaddr 起的 p_memsz 位元組」。
因此,某個目標位址 target_vaddr 落在該段時,它在檔案裡的偏移量就是:
```
file_offset = p_offset + (target_vaddr - p_vaddr)
```
理解完了之後,我們可以進行以下實驗:
0. 先設定所需的變數
```
BIN=hello.debug
CORE=hello.core
TEXT_OFF=0x604 # 從 objdump/nm 得知的 .text 起點相對位移
```
1. 從「執行檔」取得 .text 的資訊
1.1 .text 的大小(十六進位)
```
$ readelf -S $BIN | grep '\.text'
[12] .text PROGBITS 00000604 000604 000160 00 AX 0 0 4
$ TEXT_SIZE=$(readelf -SW $BIN | awk '/\.text[[:space:]]/ {print "0x"$6; exit}')
echo $TEXT_SIZE
0x000160
```
1.2 判斷是否 PIE(決定位址要不要加載入基底)
```
readelf -h $BIN | grep 'Type:'
# Type: DYN (Position-Independent Executable) → PIE → 需要“載入基底 + 相對位移”
# Type: EXEC → 非 PIE → 一般 .text VMA 就是執行時位址
```
以我的例子來看:
```
$ readelf -h $BIN | grep 'Type:'
Type: DYN (Position-Independent Executable file)
```
需要加入基底
1.3 取得 .text 的「連結時」位址(確認 TEXT_OFF 來源
```
$ readelf -S $BIN | grep '\.text'
[12] .text PROGBITS 00000604 000604 000160 00 AX 0 0 4
```
base 是 0x604
2. 從「core」找主程式 .text 的載入基底(RX 段)
2.1 用 readelf 快速掃描 RX 段
```
$ readelf -l $CORE | awk '/LOAD/ {print}' | nl
1 LOAD 0x001000 0x004ba000 0x00000000 0x01000 0x01000 R E 0x1000
2 LOAD 0x002000 0x004bb000 0x00000000 0x01000 0x01000 R 0x1000
3 LOAD 0x003000 0x004bc000 0x00000000 0x01000 0x01000 RW 0x1000
4 LOAD 0x004000 0x00fbc000 0x00000000 0x21000 0x21000 RW 0x1000
5 LOAD 0x025000 0x76cb6000 0x00000000 0x02000 0x02000 RW 0x1000
6 LOAD 0x027000 0x76cb8000 0x00000000 0x01000 0x45000 R E 0x1000
7 LOAD 0x028000 0x76cfd000 0x00000000 0x01000 0x01000 R 0x1000
8 LOAD 0x029000 0x76cfe000 0x00000000 0x01000 0x01000 RW 0x1000
9 LOAD 0x02a000 0x76cff000 0x00000000 0x01000 0x102000 R E 0x1000
10 LOAD 0x02b000 0x76e01000 0x00000000 0x02000 0x02000 R 0x1000
11 LOAD 0x02d000 0x76e03000 0x00000000 0x01000 0x01000 RW 0x1000
12 LOAD 0x02e000 0x76e04000 0x00000000 0x0a000 0x0a000 RW 0x1000
13 LOAD 0x038000 0x76e0e000 0x00000000 0x01000 0x18000 R E 0x1000
14 LOAD 0x039000 0x76e26000 0x00000000 0x01000 0x01000 R 0x1000
15 LOAD 0x03a000 0x76e27000 0x00000000 0x01000 0x01000 RW 0x1000
16 LOAD 0x03b000 0x76e28000 0x00000000 0x01000 0x18d000 R E 0x1000
17 LOAD 0x03c000 0x76fb5000 0x00000000 0x07000 0x07000 R 0x1000
18 LOAD 0x043000 0x76fbc000 0x00000000 0x01000 0x01000 RW 0x1000
19 LOAD 0x044000 0x76fbd000 0x00000000 0x02000 0x02000 RW 0x1000
20 LOAD 0x046000 0x76fc1000 0x00000000 0x02000 0x02000 RW 0x1000
21 LOAD 0x048000 0x76fc3000 0x00000000 0x01000 0x1c000 R E 0x1000
22 LOAD 0x049000 0x76fdf000 0x00000000 0x02000 0x02000 R 0x1000
23 LOAD 0x04b000 0x76fe1000 0x00000000 0x01000 0x01000 RW 0x1000
24 LOAD 0x04c000 0x7ee8b000 0x00000000 0x21000 0x21000 RW 0x1000
25 LOAD 0x06d000 0x7ef79000 0x00000000 0x01000 0x01000 R E 0x1000
26 LOAD 0x06e000 0x7ef7a000 0x00000000 0x01000 0x01000 R 0x1000
27 LOAD 0x06f000 0x7ef7b000 0x00000000 0x01000 0x01000 R E 0x1000
28 LOAD 0x070000 0xffff0000 0x00000000 0x01000 0x01000 R E 0x1000
```
裡面的
```
9: LOAD p_offset=0x001000 p_vaddr=0x004ba000 ... R E 0x1000
10: LOAD p_offset=0x002000 p_vaddr=0x004bb000 ... R 0x1000
11: LOAD p_offset=0x003000 p_vaddr=0x004bc000 ... RW 0x1000
```
這個 RX → R → RW 三連排非常像 主程式 的 .text/.rodata/.data;因此主程式 .text 的載入基底很大機率是:
```
TEXT_BASE=0x004ba000
```
3. 把「相對位移」變成「執行時位址」與「core 檔內偏移」
3.1 計算執行時 .text 起點
```
$ echo "text_start_runtime = 0x%X\n" $((0x004ba000 + $TEXT_OFF))
text_start_runtime = 0x%X\n 4957700
```
3.2 計算在 core 檔內的位元組偏移(關鍵公式)
```
file_offset = p_offset + (target_vaddr - p_vaddr)
```
用前面9:load那行算
```
p_offset = 0x001000
p_vaddr = 0x004ba000
target_vaddr = 0x004ba604
file_offset = 0x001000 + (0x004ba604 - 0x004ba000)
= 0x001000 + 0x604
= 0x001604
```
這代表:
在 core 檔案內的第 0x1604 個 byte,就是執行時位址 0x004ba604 的第一個 byte。
應用:
前面已經得知 p_vaddr 是 0x004ba000,且程式有錯發生在
PC = 0x004ba72e
Offset = 0x004ba72e - 0x004ba000 = 0x72e
可以透過 `addr2line` 轉換出原始位置
```
arm-openbmc-linux-gnueabi-addr2line -e /usr/local/oecore-x86_64/sysroots/cortexa7t2hf-neon-vfpv4-openbmc-linux-gnueabi/usr/bin/.debug/hello 0x72e -f -C
main
/usr/src/debug/hello/1.0/main.cpp:7
```