当前位置: 首页 > ds >正文

使用 Qemu 调试 LoongArch 应用程序

1.编译 Qemu

OS:Ubuntu 22.04

下载Qemu源码

git clone --depth=1 https://gitlab.com/qemu-project/qemu.git

编译

cd qemu
mkdir build
cd build
../configure --target-list=loongarch64-linux-user,loongarch64-softmmu --prefix=`pwd`/__install
make && make install

测试

$ cd __install/
$ ls
bin  include  libexec  share  var
$ cd bin/
$ ls
elf2dmp  hello  qemu-edid  qemu-ga  qemu-img  qemu-io  qemu-keymap  qemu-loongarch64  qemu-nbd  qemu-pr-helper  qemu-storage-daemon  qemu-system-loongarch64  qemu-vmsr-helper  sum
$ ./qemu-loongarch64 --version
qemu-loongarch64 version 10.0.50
Copyright (c) 2003-2025 Fabrice Bellard and the QEMU Project developers

2.应用程序调试

交叉工具链

gcc13: https://gitee.com/open-loongarch/cross-toolchain/tree/master/gcc-13

gcc14: https://github.com/loongson/build-tools/releases/tag/2025.02.21

本案例使用是的gcc13。

2.1 汇编程序

2.1.1 源码
$ cat sum.s .section .text.globl _start_start:addi.w  $t0, $zero, 0       # $t0 = 0 (累加器)addi.w  $t1, $zero, 1       # $t1 = 1 (计数器)loop:add.w   $t0, $t0, $t1       # $t0 = $t0 + $t1addi.w  $t1, $t1, 1         # $t1 = $t1 + 1addi.w  $t2, $zero, 6       # $t2 = 6 (终止值)slt     $t3, $t1, $t2       # $t3 = ($t1 < $t2) ? 1 : 0bnez    $t3, loop           # 若 $t3 != 0,跳转到 loop# 终止程序(LoongArch 系统调用)addi.w  $a7, $zero, 93      # exit 系统调用号 = 93addi.w  $a0, $zero, 0       # 退出码 = 0syscall 0                   # 调用内核
2.1.2 编译
$ loongarch64-linux-gcc -v
Using built-in specs.
COLLECT_GCC=/opt/loongarch64-linux-gnu-gcc13.3/bin/loongarch64-linux-gcc.br_real
COLLECT_LTO_WRAPPER=/opt/loongarch64-linux-gnu-gcc13.3/bin/../libexec/gcc/loongarch64-loongson-linux-gnu/13.3.0/lto-wrapper
Target: loongarch64-loongson-linux-gnu
Configured with: ./configure --prefix=/home/loongson/data1/work-tao/buildroot/buildroot-2024.08/output/host --sysconfdir=/home/loongson/data1/work-tao/buildroot/buildroot-2024.08/output/host/etc --enable-static --target=loongarch64-loongson-linux-gnu --with-sysroot=/home/loongson/data1/work-tao/buildroot/buildroot-2024.08/output/host/loongarch64-loongson-linux-gnu/sysroot --enable-__cxa_atexit --with-gnu-ld --disable-libssp --disable-multilib --disable-decimal-float --enable-plugins --enable-lto --with-gmp=/home/loongson/data1/work-tao/buildroot/buildroot-2024.08/output/host --with-mpc=/home/loongson/data1/work-tao/buildroot/buildroot-2024.08/output/host --with-mpfr=/home/loongson/data1/work-tao/buildroot/buildroot-2024.08/output/host --with-pkgversion='Buildroot -ge7e368b6-dirty' --with-bugurl=https://gitlab.com/buildroot.org/buildroot/-/issues --without-zstd --disable-libquadmath --disable-libquadmath-support --enable-tls --enable-threads --without-isl --without-cloog --with-abi=lp64d --enable-languages=c,c++ --with-build-time-tools=/home/loongson/data1/work-tao/buildroot/buildroot-2024.08/output/host/loongarch64-loongson-linux-gnu/bin --enable-shared --disable-libgomp
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.3.0 (Buildroot -ge7e368b6-dirty)$ loongarch64-linux-gcc -g sum.s -o sum -nostdlib
sum.s: Assembler messages:
sum.s: Warning: end of file in comment; newline inserted$ file sum
sum: ELF 64-bit LSB executable, LoongArch, version 1 (SYSV), statically linked, with debug_info, not stripped
2.1.3 调试

通过qemu-loongarch64 运行应用程序,并指定gdb 的端口号为 1234,同时指定库的路径,如果程序是静态编译的则不需要。

$ ./qemu-loongarch64 -L /opt/loongarch64-linux-gnu-gcc13.3/loongarch64-loongson-linux-gnu/sysroot/ -g 1234 ./sum

运行loongarch64-linux-gdb 进行在线debug

$ loongarch64-linux-gdb
GNU gdb (GDB) 14.2
Copyright (C) 2023 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=loongarch64-loongson-linux-gnu".
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".
(gdb) target remote :1234
Remote debugging using :1234
Reading /home/vm/work/qemu/qemu/build/__install/bin/sum from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /home/vm/work/qemu/qemu/build/__install/bin/sum from remote target...
Reading symbols from target:/home/vm/work/qemu/qemu/build/__install/bin/sum...
_start () at sum.s:5
5	    addi.w  $t0, $zero, 0       # $t0 = 0 (累加器)
(gdb) b _start
Breakpoint 1 at 0x120000078: file sum.s, line 5.
(gdb) n
_start () at sum.s:6
6	    addi.w  $t1, $zero, 1       # $t1 = 1 (计数器)
(gdb) 
loop () at sum.s:9
9	    add.w   $t0, $t0, $t1       # $t0 = $t0 + $t1
(gdb) 
loop () at sum.s:10
10	    addi.w  $t1, $t1, 1         # $t1 = $t1 + 1
(gdb) 
loop () at sum.s:11
11	    addi.w  $t2, $zero, 6       # $t2 = 6 (终止值)
(gdb) 
loop () at sum.s:13
13	    slt     $t3, $t1, $t2       # $t3 = ($t1 < $t2) ? 1 : 0
(gdb) i r
r0             0x0                 0
r1             0x0                 0x0
r2             0x0                 0x0
r3             0x76e842351000      0x76e842351000
r4             0x0                 0
r5             0x0                 0
r6             0x0                 0
r7             0x0                 0
r8             0x0                 0
r9             0x0                 0
r10            0x0                 0
r11            0x0                 0
r12            0x1                 1
r13            0x2                 2
r14            0x6                 6
r15            0x0                 0
r16            0x0                 0
r17            0x0                 0
r18            0x0                 0
r19            0x0                 0
r20            0x0                 0
r21            0x0                 0
r22            0x0                 0x0
r23            0x0                 0
r24            0x0                 0
r25            0x0                 0
r26            0x0                 0
r27            0x0                 0
r28            0x0                 0
r29            0x0                 0
r30            0x0                 0
r31            0x0                 0
orig_a0        0x0                 0
pc             0x12000008c         0x12000008c <loop+12>
badv           0x0                 0x0
(gdb)

2.2 C 程序

2.2.1 源码
$ cat hello.c 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int foo(int a, int b)
{int c = 0;c = a + b;return c;
}int main(int argc, char *argv[])
{int a = 10;int b = 20;int c = 0;c = foo(a, b);printf("%d + %d = %d\n", a, b);return 0;
}
2.2.2 编译
$ loongarch64-linux-gcc  -g hello.c -o hello
2.2.3 调试

通过qemu-loongarch64 运行应用程序,并指定gdb 的端口号为 1234,同时指定库的路径,如果程序是静态编译的则不需要。

$ ./qemu-loongarch64 -L /opt/loongarch64-linux-gnu-gcc13.3/loongarch64-loongson-linux-gnu/sysroot/ -g 1234 ./hello

运行loongarch64-linux-gdb 进行在线debug

$ loongarch64-linux-gdb
GNU gdb (GDB) 14.2
Copyright (C) 2023 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=loongarch64-loongson-linux-gnu".
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".
(gdb) target remote :1234
Remote debugging using :1234
Reading /home/vm/work/qemu/qemu/build/__install/bin/hello from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /home/vm/work/qemu/qemu/build/__install/bin/hello from remote target...
Reading symbols from target:/home/vm/work/qemu/qemu/build/__install/bin/hello...
Reading /lib64/ld-linux-loongarch-lp64d.so.1 from remote target...
Reading /lib64/ld-linux-loongarch-lp64d.so.1 from remote target...
Reading symbols from target:/lib64/ld-linux-loongarch-lp64d.so.1...
(No debugging symbols found in target:/lib64/ld-linux-loongarch-lp64d.so.1)
0x000070f166e68754 in _start () from target:/lib64/ld-linux-loongarch-lp64d.so.1
(gdb) b main
Breakpoint 1 at 0x120000648: file hello.c, line 15.
(gdb) c
Continuing.
Reading /lib64/libc.so.6 from remote target...Breakpoint 1, main (argc=1, argv=0x70f166016008) at hello.c:15
15		int a = 10;
(gdb) n
16		int b = 20;
(gdb) 
17		int c = 0;
(gdb) 
19		c = foo(a, b);
(gdb) s
foo (a=10, b=20) at hello.c:7
7		int c = 0;
(gdb) 
9		c = a + b;
(gdb) 
10		return c;
(gdb) 
11	}
(gdb) 
main (argc=1, argv=0x70f166016008) at hello.c:20
20		printf("%d + %d = %d\n", a, b);
(gdb) p c
$1 = 30
(gdb) 
http://www.xdnf.cn/news/9408.html

相关文章:

  • 物流项目第七期(路线规划之Neo4j的应用)
  • 本地socket排查
  • 详解MySQL调优
  • 高频面试--MySQL
  • 【Python打卡Day31】文件的拆分与使用@浙大疏锦行
  • 【C语言练习】066. 使用typedef定义新类型
  • 【每天一个知识点】智能体(Agent)”与“思维链(Chain of Thought, CoT)
  • 为什么选择迪宇电力厂家的绝缘胶垫?有什么优势以及产品参数
  • Python应用嵌套猜数字小游戏
  • 数据库大学实验二
  • PyTorch入门-Transorforms
  • 2.2.1 05年T3
  • python处理signal(信号)
  • 基于大模型的慢性胃炎全周期预测与诊疗方案研究报告
  • 联合索引与最左前缀原则详解
  • Springboot-基础
  • LY/T 2714-2016 木塑门套线检测
  • Spring Boot整合Spring AI全攻略:构建智能应用的工程实践
  • Java 并发编程通关秘籍——08死锁
  • webpack CDN打包优化
  • 有什么excel.js支持IE11,可以显示EXCEL单元格数据,支持单元格合并,边框线,单元格背景
  • LangGraph + LLM + stream_mode
  • WPF命令与MVVM模式:打造优雅的应用程序架构
  • 【AI News | 20250527】每日AI进展
  • springboot--实战--大事件--用户接口开发
  • 【机器学习基础】机器学习入门核心算法:支持向量机(SVM)
  • MySQL-查询测试
  • cf1703F
  • leetcode hot100刷题日记——18.搜索插入位置
  • Redis学习打卡-Day8-Redis实践