eclipse + qemu 编译调试Linux内核

本文介绍如何从源代码构建Linux内核2.6.23版本,并使用Eclipse CDT插件进行调试。通过配置内核选项、设置调试信息及指针,文章详细展示了构建过程。此外,还介绍了如何利用QEMU系统模拟器加载内核并借助GDB stub连接Eclipse进行逐行调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

A screencast demonstrating roughly the same thing is available at: http://blip.tv/file/586651
For iTunes users there's a videopodcast at: http://takis.blip.tv/rss/itunes/

Download the Linux kernel sourcecode from http://www.kernel.org/. For example, the current kernel version is 2.6.23, a direct link would be http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.9.tar.bz2

Extract the Linux kernel sourcecode:


cd /usr/local/src
tar xvjf linux-2.6.23.9.tar.bz2

 

We will build the Linux kernel in a different directory:

mkdir -p /mnt/build/linux-2.6

 

Then copy the provided kernel configuration into this directory giving it a new name ".config". The following commands will then use this as a base-configuration to start from.


Next, we'll configure the kernel. Just keep pressing enter to use the default answers to all the questions that the kernel configuration program will ask you.


cd /usr/local/src/linux-2.6.23
make oldconfig O=/mnt/build/linux-2.6



Next, make the kernel a bit easier to debug:


make menuconfig O=/mnt/build/linux-2.6



And enable the following options: In the "Kernel hacking" menu enable both "Compile the kernel with debug info" and "Compile the kernel with frame pointers".

 

Now, we'll fire up Eclipse with the CDT plugin. You can download Eclipse with the CDT plugin from http://www.eclipse.org/downloads/. You'll need to download "Eclipse IDE for C/C++ Developers".



Get rid of the intro screen.

You'll get an empty workspace as shown in the screenshot. First disable automatic building, by using the "Window->Preferences" menu, selecting "General->Workspace" and deselecting "Build automatically". Eclipse will perform a time consuming indexing operation which you can disable by using the "Window->Preferences" menu,  selecting "C/C++->Indexer" and switching from "Fast C/C++ Indexer" to "No Indexer".

 


Start a new project, by using File->New->Project...

Then select "C Project", "Makefile project", "Empty Project".

 



 


Now enter a project name and specify a specific directory for the project sourcecode. To do this, first uncheck the "Use default location" checkbox.

 



Finally click "Finish".


 

If you hadn't disabled indexing, Eclipse will now start indexing the Linux kernel sourcecode. This will take a long time.



 


You'll see a progressbar which might give you an indication on how long it might take to complete.

 


 


Eclipse finished indexing the kernel sourcecode. Now, we're ready to configure our debugger. Right-click on the project-name in the left pane (Project explorer) and select "Properties".

 


 


We want to modify the default build command and the location where the build files should go.

 

 


Uncheck "Use default build command" and enter make CC=gcc-3.4 O=/mnt/build/linux-2.6

 


 


Modify the build location by clicking the "File system..." button and browsing to /mnt/build/linux-2.6

 

 


Through the menu-bar select "Project->Build all" or press "Ctrl-b".

 


 


After some time the Linux kernel build will be completed and you see "bzImage is ready" appear in the Eclipse Console output.

 


 


Next, we'll run our kernel binary using the Qemu system emulator. The nice thing about Qemu is that besides the normal virtual HD, floppy and ISO image booting, it can also boot Linux kernels directly. And, Qemu provides a GDB-stub to which we can connect from our Eclipse debugger. The "-s" switch activates this GDB-stub. The "-S" switch makes sure Qemu doesn't start running before we're ready (it freezes the CPU at startup).

 


 


Because the CPU is "frozen" at startup, the Qemu window won't show anything useful yet.

 


 


Through the menubar, select "Run->Debug Configurations...". Double-click "C/C++ Local Application". Modify the "C/C++ Application" textentry to point to the actual Linux kernel, being /mnt/build/linux-2.6/vmlinux




 


Click on the "Debugger" tab, and in the "Debugger" listbox select the "gdbserver Debugger". Next, modify the "Stop on startup at:" to "start_kernel". Below this, you'll notice a frame named "Debugger Options"; click the "Connection" tab in this frame and modify the "Type" to "TCP" and the "Port number" to 1234. Continue by clicking the "Debug" button.

 


 


Eclipse might compile and link a bit, but will finally launch the debugger and ask if you want to switch to the debugging perspective. Say yes.

 


 


The next screenshot shows the debugging perspective. Just like with normal applications, you'll see that the line it is about the execute is highlighted.

 


 


In the Qemu window, you'll notice some output already. This is the output which happened in functions preceding the start_kernel() function.

 


 


...

 





 


By using "Run->Step over" or pressing the "F6" key, you can execute the kernel code line by line and examine what's happening.

 


 


If you want to see the assembly instructions which are being executed, you can add a view which displays this by selecting "Windows->Show View->Disassembly".

 


 


There's a register view too, as can be seen in the next screenshot. Registers who's contents has been altered by the previous execution step are highlighted in yellow.

 


 


You can add breakpoints, inspect variables, inspect memory and much more, but as you keep running the kernel you'll run in trouble as we did not specify a true harddisk image for Qemu. So, you'll get the following output in the Qemu window, because the Linux kernel could not find a root filesystem on our fake harddisk image "/dev/zero".

 


 


That's it. Hopefully the above is useful (and fun) for anyone :)

 

<think>嗯,用户想了解如何在Eclipse编译调试RISC-V内核的程序。首先,我需要回忆一下Eclipse对嵌入式开发的支持情况。记得Eclipse有插件比如CDT用于C/C++开发,还有可能需要安装其他插件来支持RISC-V架构。 接下来,用户可能需要配置交叉编译工具链。RISC-V通常需要特定的工具链,比如riscv-gnu-toolchain。我得确认在Eclipse中如何设置这个工具链的路径,可能需要修改项目的构建配置,指定编译器和链接器。 然后是调试部分。调试RISC-V程序可能需要使用OpenOCD和GDB服务器。用户需要在Eclipse中配置调试器,指向RISC-V的GDB工具,比如riscv64-unknown-elf-gdb。同时,可能需要设置OpenOCD的配置文件,连接目标硬件或模拟器,比如QEMU。 用户可能遇到的问题包括工具链路径错误、调试配置不正确或者硬件连接问题。需要提醒他们检查这些配置,并确保所有工具已正确安装。另外,如果使用硬件模拟器,如QEMU,可能需要启动相应的RISC-V模拟环境,并在Eclipse中正确指向它。 还要考虑是否需要创建Makefile项目,或者使用Eclipse的自动构建功能。对于复杂的项目结构,手动编写Makefile可能更可靠,然后在Eclipse中导入为现有项目。 最后,可能需要指导用户如何设置断点、查看寄存器状态和内存内容,这些都是调试内核程序时的常见需求。确保步骤清晰,分点说明,便于用户逐步操作。</think>在Eclipse编译调试RISC-V内核程序的步骤如下,分为环境配置、编译设置、调试配置三个阶段: 一、环境准备 1. 安装必要工具链 - RISC-V交叉编译器:`riscv-gnu-toolchain` - 调试工具:`openocd` + `riscv-openocd` - 模拟器(可选):`qemu-riscv` - Eclipse插件:`C/C++ Development Tools(CDT)` 2. 验证工具链 ```bash $ riscv64-unknown-elf-gcc --version $ openocd -v ``` 二、Eclipse工程配置 1. 创建C项目 - 菜单选择:File > New > C Project - 项目类型选择"Makefile Project" > Empty Project - 工具链选择"RISC-V Cross GCC" 2. 设置编译环境 ```properties # .project文件中需包含 <buildSpec> <buildCommand> <name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name> <arguments> <dictionary> <key>?name?</key> <value>all</value> </dictionary> </arguments> </buildCommand> </buildSpec> ``` 3. 配置交叉编译器 - 右键项目 > Properties - C/C++ Build > Settings - 设置工具链前缀为`riscv64-unknown-elf-` - 添加编译参数: $$ -march=rv64imac -mabi=lp64 -nostartfiles $$ 三、调试配置 1. 创建调试配置 - Run > Debug Configurations - 新建"GDB OpenOCD Debugging"配置 2. 关键参数设置 ```xml <configuration type="com.openocd.debug.gdbjtag.openocdConfigurationType"> <stringAttribute key="gdb_command" value="riscv64-unknown-elf-gdb"/> <stringAttribute key="openocd_command" value="openocd"/> <stringAttribute key="openocd_cfg_file" value="board/riscv.cfg"/> </configuration> ``` 四、调试技巧 1. 内存查看: - Window > Show View > Memory Monitor - 输入地址范围:0x80000000-0x8000FFFF 2. 寄存器监控: - Window > Show View > Registers - 启用RISC-V专用寄存器组 3. QEMU联调(可选) ```bash $ qemu-system-riscv64 -machine virt -kernel my_kernel.elf -S -gdb tcp::1234 ``` 在Eclipse调试配置中设置远程连接:localhost:1234 常见问题处理: 1. 链接错误:检查`.ld`链接脚本的MEMORY区域定义 2. 启动失败:确认`_start`入口点定义正确 3. 调试断点失效:确认编译时保留调试符号(`-g`参数) 4. 外设访问错误:检查MMU映射配置 建议使用JLink调试器时,在openocd配置中添加: ```cfg interface jlink transport select jtag adapter_khz 1000 set _CHIPNAME riscv jtag newtap $_CHIPNAME cpu -irlen 5 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值