前提條件
Xtensa 仿真器 (xt-run
) 是用于運(yùn)行 Xtensa ELF 可執(zhí)行文件的專有 Xtensa 工具剃浇。這份指南假設(shè)你已經(jīng)正確地安裝了它及你的平臺的核墩蔓。它描述如何使用 xt-run
攘烛。
為你的平臺運(yùn)行仿真舶赔,需要通過 XTENSA_CORE 環(huán)境變量設(shè)置你的核(就像其它的 Xtensa 工具湃交,如 xt-xcc
熟空、xt-clang
、xt-make
和 xt-gdb
一樣)搞莺。如果沒有設(shè)置 XTENSA_CORE 環(huán)境變量息罗,則需要為這些工具加上 --xtensa-core=xxxxxx 參數(shù)。如果你可以通過 xt-xcc
/xt-clang
編譯器成功構(gòu)建固件才沧,則所有的配置應(yīng)該都已經(jīng)準(zhǔn)備好了阱当。
xt-run
可執(zhí)行程序調(diào)用 RI-2022.10-linux/XtensaTools/lib/iss 目錄下的 iss
程序運(yùn)行仿真俏扩,ISS 即 Xtensa Instruction Set Simulator。
獨(dú)立程序
使用 xt-xcc
/xt-clang
和 xt-run
開發(fā)程序與常見的 *nix 程序開發(fā)類似弊添。
我們從一個(gè) "Hello World!" 示例開始录淡。把如下的代碼片段保存為 test.c:
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
要運(yùn)行這個(gè)程序,首先通過 xt-xcc
/xt-clang
構(gòu)建 Xtensa ELF:
xt-clang --xtensa-core=ABCDE_ADSP_1027 test.c -o test
默認(rèn)情況下油坝,用 xt-xcc
/xt-clang
編譯程序?qū)㈡溄訕?biāo)準(zhǔn) C 庫嫉戚。接下來,通過 xt-run
運(yùn)行輸出的二進(jìn)制文件:
sof$ xt-run --xtensa-core=ABCDE_ADSP_1027 test
Hello, world!
通過 printf()
向標(biāo)準(zhǔn)輸出打印的消息澈圈,將顯示在控制臺彬檀。你可以像這樣獨(dú)立運(yùn)行任何代碼,例如測試某些算法瞬女。
在 xt-run
中運(yùn)行的程序還支持 stdlib
(在通常的 FW 中不可用)窍帝,因此你可以使用 stdio
來打印輸出。xt-run
還支持所有的核特有特性诽偷,因而你可以在你的 C 程序中使用 intrinsics(比如 HiFi3)坤学。
通常的 FW 為了方便在沒有操作系統(tǒng)加載的裸機(jī)環(huán)境運(yùn)行,在編譯鏈接時(shí)會加上 "-nostdlib" 標(biāo)記报慕,以移除對標(biāo)準(zhǔn) C 庫的依賴深浮,如 SoF 在 sof/src/arch/xtensa/CMakeLists.txt 文件中加了這個(gè)標(biāo)記:
if(BUILD_UNIT_TESTS)
set(stdlib_flag "")
else()
set(stdlib_flag "-nostdlib")
endif()
stdlib
對于通常的 FW 中不可用,也就很難通過 printf()
之類的函數(shù)打印消息來調(diào)試通常的 FW眠冈。Xtensa 工具鏈中包含了 xt-gdb
飞苇,可以用來調(diào)試 Xtensa ELF 可執(zhí)行程序,包括通常的 FW蜗顽。
用 xt-gdb
調(diào)試上面的 test
程序布卡,和用 gdb 調(diào)試 PC 平臺的程序基本沒什么區(qū)別,如:
sof$ xt-gdb --xtensa-core=ABCDE_ADSP_1027 test
GNU gdb (GDB) 10.1 Xtensa Tools 14.10
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 "--host=x86_64-pc-linux-gnu --target=xtensa-elf".
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 test...
(xt-gdb) break main
Breakpoint 1 at 0x80000f4f
(xt-gdb) r
Starting program: /media/data/sof/test
Starting the ISS simulator.
Switching to remote protocol
Remote debugging using localhost:59226
_ResetVector () at /home/abc/RI-2022.10/ib/tools/swtools-x86_64-linux/xtensa-elf/src/xtos/xea2/reset-vector-xea2.S:81
81 /home/abc/RI-2022.10/ib/tools/swtools-x86_64-linux/xtensa-elf/src/xtos/xea2/reset-vector-xea2.S: 沒有那個(gè)文件或目錄.
Breakpoint 1, 0x80000f4f in main ()
(xt-gdb)
xt-gdb
可以啟動(dòng) xt-run
/iss
來運(yùn)行 Xtensa ELF雇盖。在 xt-gdb
中忿等,可以使用一般的 gdb 調(diào)試可用的斷點(diǎn)等各種命令。
對于那些通常的 FW刊懈,xt-gdb
和 xt-run
/iss
可以分開運(yùn)行來調(diào)試这弧。首先運(yùn)行 xt-run
/iss
:
$ xt-run --xtensa-core=ABCDE_ADSP_1027 --xtensa-system=/media/data/xtensa/XtDevTools/install/tools/RI-2022.10-linux/XtensaTools/config --xtensa-params= --console --gdbserve=0 --vector=0 dsp0.elf
SOCKET:53497
xt-run
/iss
開啟一個(gè)調(diào)試端口娃闲,如上面的 53497虚汛。然后運(yùn)行 xt-gdb
,如:
$ xt-gdb --xtensa-core=ABCDE_ADSP_1027 dsp0.elf
GNU gdb (GDB) 10.1 Xtensa Tools 14.10
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 "--host=x86_64-pc-linux-gnu --target=xtensa-elf".
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 dsp0.elf...
(xt-gdb) target remote localhost:53497
Remote debugging using localhost:53497
_ResetVector () at /home/abc/RI-2022.10/ib/tools/swtools-x86_64-linux/xtensa-elf/src/xtos/xea2/reset-vector-xea2.S:81
81 /home/abc/RI-2022.10/ib/tools/swtools-x86_64-linux/xtensa-elf/src/xtos/xea2/reset-vector-xea2.S: 沒有那個(gè)文件或目錄.
(xt-gdb) break main
Breakpoint 1 at 0x800665ff: file top/src/main/main.c, line 108.
(xt-gdb) r
The "remote" target does not support "run". Try "help target" or "continue".
(xt-gdb) c
Continuing.
Breakpoint 1, main (argc=1, argv=0x80440004) at top/src/main/main.c:108
在 xt-gdb
中通過 target remote localhost:53497
命令連接 xt-run
/iss
皇帮。之后在 xt-gdb
中可以使用 gdb 的各種調(diào)試命令來調(diào)試 FW卷哩。
Xtensa 提供的基于 Eclipse 的 IDE 工具 Xtensa Xplorer 使用相同的 xt-run
/iss
和 xt-gdb
工具來調(diào)試程序。
單元測試
在 SOF 項(xiàng)目中属拾,xt-run
被用作單元測試的執(zhí)行者将谊。
下面的示例演示了冷溶,如何為示例函數(shù)添加簡單的單元測試用例:math
模塊中的 my_add
。
首先尊浓,添加一個(gè)將成為單元測試主題的函數(shù):
Code Block 8 src/include/sof/math/numbers.h
int my_add(int a, int b);
Code Block 9 src/math/numbers.c
int my_add(int a, int b)
{
return a + b;
}
接下來逞频,添加單元測試實(shí)現(xiàn):
Code Block 10 test/cmocka/src/math/numbers/my_add.c
// header with function that we test
#include <sof/math/numbers.h>
// standard headers that have to be included in every cmocka's unit test
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
// one of test cases
static void my_add_2_plus_3_equals_5(void **state)
{
int result;
(void)state;
result = my_add(2, 3);
assert_int_equal(result, 5);
}
int main(void)
{
// list of all test cases, here we have just 1
const struct CMUnitTest tests[] = {
cmocka_unit_test(my_add_2_plus_3_equals_5),
};
cmocka_set_message_output(CM_OUTPUT_TAP);
return cmocka_run_group_tests(tests, NULL, NULL);
}
對每個(gè)進(jìn)行單元測試的函數(shù)使用一個(gè)單獨(dú)的文件;這也就是我們把代碼放進(jìn) test/cmocka/src/math/numbers 目錄下的 my_add.c 文件中的原因栋齿。
最后苗胀,讓 CMake 知道單元測試的存在。
Code Block 11 test/cmocka/src/math/numbers/CMakeLists.txt
cmocka_test(my_add
my_add.c
${PROJECT_SOURCE_DIR}/src/math/numbers.c
)
要運(yùn)行單元測試瓦堵,可以按照 單元測試 一文中的說明進(jìn)行基协。
如果你只想運(yùn)行你的測試用例 (而不是所有的測試),你可以把如下的命令:
make -j4 && ctest -j8
替換為:
make my_add && ctest -R my_add
在 Testing/Temporary/LastTest.log 可以找到運(yùn)行 ctest 時(shí)產(chǎn)生的日志菇用。
參考文檔
Xtensa Simulator (xt-run)
Done.