使用 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.pswp.cn/news/907135.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Unity 游戏优化(持续更新中...)

垃圾回收 是什么&#xff1f; 垃圾回收&#xff08;Garbage Collection&#xff09;GC 工作机制 1、Unity 为用户生成的代码和脚本采用了自动内存管理。 2、小块数据&#xff08;如值类型的局部变量&#xff09;分配在栈上。大块数据和长期存储分配在托管堆上。 3、垃圾收集…

python和java差异:关键数据类型与容器

2.0. 对象的类型&#xff1a;可变 (Mutable) 与不可变 (Immutable) 在Python中&#xff0c;理解对象的可变性 (mutability) 是至关重要的&#xff0c;它影响着变量如何被修改、函数参数如何传递以及数据结构的行为。 不可变对象 (Immutable Objects): 大白话定义&#xff1a;…

DAY 33

知识点回顾&#xff1a; 1. PyTorch和cuda的安装 2. 查看显卡信息的命令行命令&#xff08;cmd中使用&#xff09; 3. cuda的检查 4. 简单神经网络的流程 a. 数据预处理&#xff08;归一化、转换成张量&#xff09; b. 模型的定义 i. 继承nn.Module类 ii. 定义…

Minktec 柔性弯曲传感器,灵敏捕捉坐姿弓背、精准监测行走姿态,守护儿童背部健康,为科学健身提供数据支撑,开启职业健康与背痛 AI 干预新方向。

Minktec弯曲形变传感器通过创新的技术设计&#xff0c;为各种弯曲和形变检测需求提供了精确的解决方案。其核心技术基于薄膜柔性传感器的应用&#xff0c;能够捕捉物体在三维空间中的动态变化。传感器内部结合了多点排列的应变元件和专有算法&#xff0c;实现了形状的实时重建。…

快递鸟接口费用解析:中小电商成本控制方案

中小电商企业在物流环节的成本控制&#xff0c;直接影响着整体运营效率和利润空间。作为国内主流的物流数据服务商&#xff0c;快递鸟API接口凭借其聚合查询、电子面单、轨迹跟踪等功能&#xff0c;成为众多电商企业的选择。但如何精准解析其收费模式&#xff0c;并制定科学的成…

maven 最短路径依赖优先

问题描述&#xff1a; 项目在升级大版本后出现了&#xff0c;两个不同模块所引用的同一个依赖包版本不同 module A 引用了 module B&#xff0c;module B 引用了 A_1.0.jar->B_1.0.jar->C_1.0.jar(C 为B 里面的包) 在执行 mvn dependency:tree 后发现&#xff1a; modul…

游戏引擎学习第314天:将精灵拆分成多个层

回顾并为今天的工作做准备 我们今天继续昨天开始的工作&#xff0c;现在我们要回到渲染中处理 Z 值的最终环节。我们目前已经有一个我们认为还算合理的排序方式&#xff0c;虽然可能还需要在接下来的过程中进行一些调整&#xff0c;但总体上已经有了一个明确的方向。 我们已经…

HashSet的基本概念

ashSet的基本概念 - HashSet 是C#中用于存储唯一元素的泛型集合类&#xff0c;它基于哈希表实现&#xff0c;具有快速查找、添加和删除元素的特性。 - 核心特点&#xff1a; - 不允许存储重复元素 - 元素无序排列 - 查找、添加、删除操作的平均时间复杂度为O(1) - 实现了 IEnum…

信号与系统10-综合案例:智能信号处理系统

第7课&#xff1a;综合案例——智能信号处理系统 1. 案例1&#xff1a;基于傅里叶变换与AI的语音信号分类系统 1.1 理论基础 傅里叶变换是信号处理的核心工具之一&#xff0c;能够将时域信号转换为频域表示&#xff08;如频谱图&#xff09;。语音信号的频域特征&#xff08…

详解Kubernetes Scheduler 的调度策略

详解Kubernetes Scheduler 的调度策略 在 Kubernetes(K8s)中,Scheduler(调度器) 负责 决定 Pod 应该运行在哪个 Node(节点)。 👉 调度器的目标是什么? ✅ 最大化资源利用率(让 CPU、内存等资源不浪费) ✅ 保证 Pod 运行在合适的 Node 上(避免超载、满足亲和性)…

在 ElementUI 中实现 Table 单元格合并

在 ElementUI 中实现 Table 单元格合并 在使用 ElementUI 的 Table 组件时&#xff0c;有时我们需要合并相邻的单元格&#xff0c;以提高表格的可读性和简洁性。下面是一个关于如何在 Table 中根据特定字段合并单元格的实现方法。 逻辑分析 spanMethod 方法&#xff1a;这是 …

小土堆pytorch--现有网络模型的使用及修改

现有网络模型的使用及修改 一级目录二级目录三级目录 现有网络模型的使用及修改1.VGG16模型VGG16网络模型简介**核心特点****网络结构细节****优缺点与应用****变种与后续发展** 2. 使用vgg16模型 一级目录 二级目录 三级目录 现有网络模型的使用及修改 1.VGG16模型 VGG16…

Oracle 正则表达式匹配(Oracle 11g)

1、连续2词汉字重复或3词汉字重复&#xff08;不会忽略符号&#xff09; ([^ \u4e00-\u9fa5\S]{2,3})\1 例如&#xff1a;阿富、 SELECT REGEXP_replace(阿富、阿富、 阿富汗、 , ([^ \u4e00-\u9fa5\S]{2,3})\1, 重复) FROM dual结果&#xff1a; 2、连续2词汉字重复或3词…

对话魔数智擎CEO柴磊:昇腾AI赋能,大小模型融合开启金融风控新范式

导读&#xff1a;#昇腾逐梦人# AI已经成为金融机构核心竞争力的关键要素。专注AI金融赛道的魔数智擎&#xff0c;通过大小模型融合&#xff0c;让AI成为银行的“金融风控专家”。 作者 | 小葳 图片来源 | 摄图 在AI涌向产业的时代赛跑中&#xff0c;开发者是绝对的主角。 昇腾…

IDEA使用Git进行commit提交到本地git空间后撤回到commit版本之前

一、前言 Git作为最流行和最全面的版本控制工具&#xff0c;非常好用&#xff0c;但是操作也会比SVN复杂一些。毕竟有得有失嘛&#xff0c;所以我选择Git&#xff0c;最近在工作中&#xff0c;一不小心吧一些无关紧要的文件commit了。还好在Push之前看到&#xff0c;不过就算P…

GitHub 趋势日报 (2025年05月26日)

本日报由 TrendForge 系统生成 https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日整体趋势 Top 10 排名项目名称项目描述今日获星总星数语言1Fosowl/agenticSeek完全本地的马努斯AI。没有API&#xff0c;没有200美元的每…

Matlab实现LSTM-SVM时间序列预测,作者:机器学习之心

Matlab实现LSTM-SVM时间序列预测&#xff0c;作者&#xff1a;机器学习之心 目录 Matlab实现LSTM-SVM时间序列预测&#xff0c;作者&#xff1a;机器学习之心效果一览基本介绍程序设计参考资料 效果一览 基本介绍 该代码实现了一个结合LSTM和SVM的混合模型&#xff0c;用于时间…

深入解析Spring Boot与Redis集成:高效缓存与性能优化实践

深入解析Spring Boot与Redis集成&#xff1a;高效缓存与性能优化实践 引言 在现代Web应用开发中&#xff0c;缓存技术是提升系统性能的重要手段之一。Redis作为一款高性能的内存数据库&#xff0c;广泛应用于缓存、消息队列等场景。本文将深入探讨如何在Spring Boot项目中集成…

Jmeter一些元件使用的详细记录

1、csv数据文件处理 文件放在bin目录下&#xff0c;属于相对路径读取文件&#xff0c;如果使用绝对路径则需要改为l添加盘符的路径&#xff0c;如&#xff1a;D:/apach/jmeter/bin 说明&#xff1a; 当选择False时&#xff0c;如果请求的次数 大于 文件中的有效数据行数&…

使用 Cursor 从 0 到 1 开发一个全栈 chatbox 项目

大家好&#xff0c;我是 luckySnail&#xff0c;你肯定用过 AI 聊天工具。例如&#xff1a; Gemini&#xff0c;ChatGPT&#xff0c;claude AI 等产品&#xff0c;我们通过它们的 web 网站或者手机应用提出问题&#xff0c;得到答案。在之前如果你想要构建一个这样的 AI 聊天应…